xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision f6928cf45516503deb48f8175a982becc579dc8c)
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 
2865 TEST_F(FormatTest, BeforeWhile) {
2866   FormatStyle Style = getLLVMStyle();
2867   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2868 
2869   verifyFormat("do {\n"
2870                "  foo();\n"
2871                "} while (1);",
2872                Style);
2873   Style.BraceWrapping.BeforeWhile = true;
2874   verifyFormat("do {\n"
2875                "  foo();\n"
2876                "}\n"
2877                "while (1);",
2878                Style);
2879 }
2880 
2881 //===----------------------------------------------------------------------===//
2882 // Tests for classes, namespaces, etc.
2883 //===----------------------------------------------------------------------===//
2884 
2885 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
2886   verifyFormat("class A {};");
2887 }
2888 
2889 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
2890   verifyFormat("class A {\n"
2891                "public:\n"
2892                "public: // comment\n"
2893                "protected:\n"
2894                "private:\n"
2895                "  void f() {}\n"
2896                "};");
2897   verifyFormat("export class A {\n"
2898                "public:\n"
2899                "public: // comment\n"
2900                "protected:\n"
2901                "private:\n"
2902                "  void f() {}\n"
2903                "};");
2904   verifyGoogleFormat("class A {\n"
2905                      " public:\n"
2906                      " protected:\n"
2907                      " private:\n"
2908                      "  void f() {}\n"
2909                      "};");
2910   verifyGoogleFormat("export class A {\n"
2911                      " public:\n"
2912                      " protected:\n"
2913                      " private:\n"
2914                      "  void f() {}\n"
2915                      "};");
2916   verifyFormat("class A {\n"
2917                "public slots:\n"
2918                "  void f1() {}\n"
2919                "public Q_SLOTS:\n"
2920                "  void f2() {}\n"
2921                "protected slots:\n"
2922                "  void f3() {}\n"
2923                "protected Q_SLOTS:\n"
2924                "  void f4() {}\n"
2925                "private slots:\n"
2926                "  void f5() {}\n"
2927                "private Q_SLOTS:\n"
2928                "  void f6() {}\n"
2929                "signals:\n"
2930                "  void g1();\n"
2931                "Q_SIGNALS:\n"
2932                "  void g2();\n"
2933                "};");
2934 
2935   // Don't interpret 'signals' the wrong way.
2936   verifyFormat("signals.set();");
2937   verifyFormat("for (Signals signals : f()) {\n}");
2938   verifyFormat("{\n"
2939                "  signals.set(); // This needs indentation.\n"
2940                "}");
2941   verifyFormat("void f() {\n"
2942                "label:\n"
2943                "  signals.baz();\n"
2944                "}");
2945 }
2946 
2947 TEST_F(FormatTest, SeparatesLogicalBlocks) {
2948   EXPECT_EQ("class A {\n"
2949             "public:\n"
2950             "  void f();\n"
2951             "\n"
2952             "private:\n"
2953             "  void g() {}\n"
2954             "  // test\n"
2955             "protected:\n"
2956             "  int h;\n"
2957             "};",
2958             format("class A {\n"
2959                    "public:\n"
2960                    "void f();\n"
2961                    "private:\n"
2962                    "void g() {}\n"
2963                    "// test\n"
2964                    "protected:\n"
2965                    "int h;\n"
2966                    "};"));
2967   EXPECT_EQ("class A {\n"
2968             "protected:\n"
2969             "public:\n"
2970             "  void f();\n"
2971             "};",
2972             format("class A {\n"
2973                    "protected:\n"
2974                    "\n"
2975                    "public:\n"
2976                    "\n"
2977                    "  void f();\n"
2978                    "};"));
2979 
2980   // Even ensure proper spacing inside macros.
2981   EXPECT_EQ("#define B     \\\n"
2982             "  class A {   \\\n"
2983             "   protected: \\\n"
2984             "   public:    \\\n"
2985             "    void f(); \\\n"
2986             "  };",
2987             format("#define B     \\\n"
2988                    "  class A {   \\\n"
2989                    "   protected: \\\n"
2990                    "              \\\n"
2991                    "   public:    \\\n"
2992                    "              \\\n"
2993                    "    void f(); \\\n"
2994                    "  };",
2995                    getGoogleStyle()));
2996   // But don't remove empty lines after macros ending in access specifiers.
2997   EXPECT_EQ("#define A private:\n"
2998             "\n"
2999             "int i;",
3000             format("#define A         private:\n"
3001                    "\n"
3002                    "int              i;"));
3003 }
3004 
3005 TEST_F(FormatTest, FormatsClasses) {
3006   verifyFormat("class A : public B {};");
3007   verifyFormat("class A : public ::B {};");
3008 
3009   verifyFormat(
3010       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3011       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3012   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3013                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3014                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3015   verifyFormat(
3016       "class A : public B, public C, public D, public E, public F {};");
3017   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3018                "                     public C,\n"
3019                "                     public D,\n"
3020                "                     public E,\n"
3021                "                     public F,\n"
3022                "                     public G {};");
3023 
3024   verifyFormat("class\n"
3025                "    ReallyReallyLongClassName {\n"
3026                "  int i;\n"
3027                "};",
3028                getLLVMStyleWithColumns(32));
3029   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3030                "                           aaaaaaaaaaaaaaaa> {};");
3031   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3032                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3033                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3034   verifyFormat("template <class R, class C>\n"
3035                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3036                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3037   verifyFormat("class ::A::B {};");
3038 }
3039 
3040 TEST_F(FormatTest, BreakInheritanceStyle) {
3041   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3042   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3043       FormatStyle::BILS_BeforeComma;
3044   verifyFormat("class MyClass : public X {};",
3045                StyleWithInheritanceBreakBeforeComma);
3046   verifyFormat("class MyClass\n"
3047                "    : public X\n"
3048                "    , public Y {};",
3049                StyleWithInheritanceBreakBeforeComma);
3050   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3051                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3052                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3053                StyleWithInheritanceBreakBeforeComma);
3054   verifyFormat("struct aaaaaaaaaaaaa\n"
3055                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3056                "          aaaaaaaaaaaaaaaa> {};",
3057                StyleWithInheritanceBreakBeforeComma);
3058 
3059   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3060   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3061       FormatStyle::BILS_AfterColon;
3062   verifyFormat("class MyClass : public X {};",
3063                StyleWithInheritanceBreakAfterColon);
3064   verifyFormat("class MyClass : public X, public Y {};",
3065                StyleWithInheritanceBreakAfterColon);
3066   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3067                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3068                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3069                StyleWithInheritanceBreakAfterColon);
3070   verifyFormat("struct aaaaaaaaaaaaa :\n"
3071                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3072                "        aaaaaaaaaaaaaaaa> {};",
3073                StyleWithInheritanceBreakAfterColon);
3074 
3075   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3076   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3077       FormatStyle::BILS_AfterComma;
3078   verifyFormat("class MyClass : public X {};",
3079                StyleWithInheritanceBreakAfterComma);
3080   verifyFormat("class MyClass : public X,\n"
3081                "                public Y {};",
3082                StyleWithInheritanceBreakAfterComma);
3083   verifyFormat(
3084       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3085       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3086       "{};",
3087       StyleWithInheritanceBreakAfterComma);
3088   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3089                "                           aaaaaaaaaaaaaaaa> {};",
3090                StyleWithInheritanceBreakAfterComma);
3091   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3092                "    : public OnceBreak,\n"
3093                "      public AlwaysBreak,\n"
3094                "      EvenBasesFitInOneLine {};",
3095                StyleWithInheritanceBreakAfterComma);
3096 }
3097 
3098 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3099   verifyFormat("class A {\n} a, b;");
3100   verifyFormat("struct A {\n} a, b;");
3101   verifyFormat("union A {\n} a;");
3102 }
3103 
3104 TEST_F(FormatTest, FormatsEnum) {
3105   verifyFormat("enum {\n"
3106                "  Zero,\n"
3107                "  One = 1,\n"
3108                "  Two = One + 1,\n"
3109                "  Three = (One + Two),\n"
3110                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3111                "  Five = (One, Two, Three, Four, 5)\n"
3112                "};");
3113   verifyGoogleFormat("enum {\n"
3114                      "  Zero,\n"
3115                      "  One = 1,\n"
3116                      "  Two = One + 1,\n"
3117                      "  Three = (One + Two),\n"
3118                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3119                      "  Five = (One, Two, Three, Four, 5)\n"
3120                      "};");
3121   verifyFormat("enum Enum {};");
3122   verifyFormat("enum {};");
3123   verifyFormat("enum X E {} d;");
3124   verifyFormat("enum __attribute__((...)) E {} d;");
3125   verifyFormat("enum __declspec__((...)) E {} d;");
3126   verifyFormat("enum {\n"
3127                "  Bar = Foo<int, int>::value\n"
3128                "};",
3129                getLLVMStyleWithColumns(30));
3130 
3131   verifyFormat("enum ShortEnum { A, B, C };");
3132   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3133 
3134   EXPECT_EQ("enum KeepEmptyLines {\n"
3135             "  ONE,\n"
3136             "\n"
3137             "  TWO,\n"
3138             "\n"
3139             "  THREE\n"
3140             "}",
3141             format("enum KeepEmptyLines {\n"
3142                    "  ONE,\n"
3143                    "\n"
3144                    "  TWO,\n"
3145                    "\n"
3146                    "\n"
3147                    "  THREE\n"
3148                    "}"));
3149   verifyFormat("enum E { // comment\n"
3150                "  ONE,\n"
3151                "  TWO\n"
3152                "};\n"
3153                "int i;");
3154 
3155   FormatStyle EightIndent = getLLVMStyle();
3156   EightIndent.IndentWidth = 8;
3157   verifyFormat("enum {\n"
3158                "        VOID,\n"
3159                "        CHAR,\n"
3160                "        SHORT,\n"
3161                "        INT,\n"
3162                "        LONG,\n"
3163                "        SIGNED,\n"
3164                "        UNSIGNED,\n"
3165                "        BOOL,\n"
3166                "        FLOAT,\n"
3167                "        DOUBLE,\n"
3168                "        COMPLEX\n"
3169                "};",
3170                EightIndent);
3171 
3172   // Not enums.
3173   verifyFormat("enum X f() {\n"
3174                "  a();\n"
3175                "  return 42;\n"
3176                "}");
3177   verifyFormat("enum X Type::f() {\n"
3178                "  a();\n"
3179                "  return 42;\n"
3180                "}");
3181   verifyFormat("enum ::X f() {\n"
3182                "  a();\n"
3183                "  return 42;\n"
3184                "}");
3185   verifyFormat("enum ns::X f() {\n"
3186                "  a();\n"
3187                "  return 42;\n"
3188                "}");
3189 }
3190 
3191 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3192   verifyFormat("enum Type {\n"
3193                "  One = 0; // These semicolons should be commas.\n"
3194                "  Two = 1;\n"
3195                "};");
3196   verifyFormat("namespace n {\n"
3197                "enum Type {\n"
3198                "  One,\n"
3199                "  Two, // missing };\n"
3200                "  int i;\n"
3201                "}\n"
3202                "void g() {}");
3203 }
3204 
3205 TEST_F(FormatTest, FormatsEnumStruct) {
3206   verifyFormat("enum struct {\n"
3207                "  Zero,\n"
3208                "  One = 1,\n"
3209                "  Two = One + 1,\n"
3210                "  Three = (One + Two),\n"
3211                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3212                "  Five = (One, Two, Three, Four, 5)\n"
3213                "};");
3214   verifyFormat("enum struct Enum {};");
3215   verifyFormat("enum struct {};");
3216   verifyFormat("enum struct X E {} d;");
3217   verifyFormat("enum struct __attribute__((...)) E {} d;");
3218   verifyFormat("enum struct __declspec__((...)) E {} d;");
3219   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3220 }
3221 
3222 TEST_F(FormatTest, FormatsEnumClass) {
3223   verifyFormat("enum class {\n"
3224                "  Zero,\n"
3225                "  One = 1,\n"
3226                "  Two = One + 1,\n"
3227                "  Three = (One + Two),\n"
3228                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3229                "  Five = (One, Two, Three, Four, 5)\n"
3230                "};");
3231   verifyFormat("enum class Enum {};");
3232   verifyFormat("enum class {};");
3233   verifyFormat("enum class X E {} d;");
3234   verifyFormat("enum class __attribute__((...)) E {} d;");
3235   verifyFormat("enum class __declspec__((...)) E {} d;");
3236   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3237 }
3238 
3239 TEST_F(FormatTest, FormatsEnumTypes) {
3240   verifyFormat("enum X : int {\n"
3241                "  A, // Force multiple lines.\n"
3242                "  B\n"
3243                "};");
3244   verifyFormat("enum X : int { A, B };");
3245   verifyFormat("enum X : std::uint32_t { A, B };");
3246 }
3247 
3248 TEST_F(FormatTest, FormatsTypedefEnum) {
3249   FormatStyle Style = getLLVMStyle();
3250   Style.ColumnLimit = 40;
3251   verifyFormat("typedef enum {} EmptyEnum;");
3252   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3253   verifyFormat("typedef enum {\n"
3254                "  ZERO = 0,\n"
3255                "  ONE = 1,\n"
3256                "  TWO = 2,\n"
3257                "  THREE = 3\n"
3258                "} LongEnum;",
3259                Style);
3260   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3261   Style.BraceWrapping.AfterEnum = true;
3262   verifyFormat("typedef enum {} EmptyEnum;");
3263   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3264   verifyFormat("typedef enum\n"
3265                "{\n"
3266                "  ZERO = 0,\n"
3267                "  ONE = 1,\n"
3268                "  TWO = 2,\n"
3269                "  THREE = 3\n"
3270                "} LongEnum;",
3271                Style);
3272 }
3273 
3274 TEST_F(FormatTest, FormatsNSEnums) {
3275   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3276   verifyGoogleFormat(
3277       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3278   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3279                      "  // Information about someDecentlyLongValue.\n"
3280                      "  someDecentlyLongValue,\n"
3281                      "  // Information about anotherDecentlyLongValue.\n"
3282                      "  anotherDecentlyLongValue,\n"
3283                      "  // Information about aThirdDecentlyLongValue.\n"
3284                      "  aThirdDecentlyLongValue\n"
3285                      "};");
3286   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3287                      "  // Information about someDecentlyLongValue.\n"
3288                      "  someDecentlyLongValue,\n"
3289                      "  // Information about anotherDecentlyLongValue.\n"
3290                      "  anotherDecentlyLongValue,\n"
3291                      "  // Information about aThirdDecentlyLongValue.\n"
3292                      "  aThirdDecentlyLongValue\n"
3293                      "};");
3294   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3295                      "  a = 1,\n"
3296                      "  b = 2,\n"
3297                      "  c = 3,\n"
3298                      "};");
3299   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3300                      "  a = 1,\n"
3301                      "  b = 2,\n"
3302                      "  c = 3,\n"
3303                      "};");
3304   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3305                      "  a = 1,\n"
3306                      "  b = 2,\n"
3307                      "  c = 3,\n"
3308                      "};");
3309   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3310                      "  a = 1,\n"
3311                      "  b = 2,\n"
3312                      "  c = 3,\n"
3313                      "};");
3314 }
3315 
3316 TEST_F(FormatTest, FormatsBitfields) {
3317   verifyFormat("struct Bitfields {\n"
3318                "  unsigned sClass : 8;\n"
3319                "  unsigned ValueKind : 2;\n"
3320                "};");
3321   verifyFormat("struct A {\n"
3322                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3323                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3324                "};");
3325   verifyFormat("struct MyStruct {\n"
3326                "  uchar data;\n"
3327                "  uchar : 8;\n"
3328                "  uchar : 8;\n"
3329                "  uchar other;\n"
3330                "};");
3331   FormatStyle Style = getLLVMStyle();
3332   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3333   verifyFormat("struct Bitfields {\n"
3334                "  unsigned sClass:8;\n"
3335                "  unsigned ValueKind:2;\n"
3336                "  uchar other;\n"
3337                "};",
3338                Style);
3339   verifyFormat("struct A {\n"
3340                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3341                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3342                "};",
3343                Style);
3344   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3345   verifyFormat("struct Bitfields {\n"
3346                "  unsigned sClass :8;\n"
3347                "  unsigned ValueKind :2;\n"
3348                "  uchar other;\n"
3349                "};",
3350                Style);
3351   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3352   verifyFormat("struct Bitfields {\n"
3353                "  unsigned sClass: 8;\n"
3354                "  unsigned ValueKind: 2;\n"
3355                "  uchar other;\n"
3356                "};",
3357                Style);
3358 }
3359 
3360 TEST_F(FormatTest, FormatsNamespaces) {
3361   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3362   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3363 
3364   verifyFormat("namespace some_namespace {\n"
3365                "class A {};\n"
3366                "void f() { f(); }\n"
3367                "}",
3368                LLVMWithNoNamespaceFix);
3369   verifyFormat("namespace N::inline D {\n"
3370                "class A {};\n"
3371                "void f() { f(); }\n"
3372                "}",
3373                LLVMWithNoNamespaceFix);
3374   verifyFormat("namespace N::inline D::E {\n"
3375                "class A {};\n"
3376                "void f() { f(); }\n"
3377                "}",
3378                LLVMWithNoNamespaceFix);
3379   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3380                "class A {};\n"
3381                "void f() { f(); }\n"
3382                "}",
3383                LLVMWithNoNamespaceFix);
3384   verifyFormat("/* something */ namespace some_namespace {\n"
3385                "class A {};\n"
3386                "void f() { f(); }\n"
3387                "}",
3388                LLVMWithNoNamespaceFix);
3389   verifyFormat("namespace {\n"
3390                "class A {};\n"
3391                "void f() { f(); }\n"
3392                "}",
3393                LLVMWithNoNamespaceFix);
3394   verifyFormat("/* something */ namespace {\n"
3395                "class A {};\n"
3396                "void f() { f(); }\n"
3397                "}",
3398                LLVMWithNoNamespaceFix);
3399   verifyFormat("inline namespace X {\n"
3400                "class A {};\n"
3401                "void f() { f(); }\n"
3402                "}",
3403                LLVMWithNoNamespaceFix);
3404   verifyFormat("/* something */ inline namespace X {\n"
3405                "class A {};\n"
3406                "void f() { f(); }\n"
3407                "}",
3408                LLVMWithNoNamespaceFix);
3409   verifyFormat("export namespace X {\n"
3410                "class A {};\n"
3411                "void f() { f(); }\n"
3412                "}",
3413                LLVMWithNoNamespaceFix);
3414   verifyFormat("using namespace some_namespace;\n"
3415                "class A {};\n"
3416                "void f() { f(); }",
3417                LLVMWithNoNamespaceFix);
3418 
3419   // This code is more common than we thought; if we
3420   // layout this correctly the semicolon will go into
3421   // its own line, which is undesirable.
3422   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3423   verifyFormat("namespace {\n"
3424                "class A {};\n"
3425                "};",
3426                LLVMWithNoNamespaceFix);
3427 
3428   verifyFormat("namespace {\n"
3429                "int SomeVariable = 0; // comment\n"
3430                "} // namespace",
3431                LLVMWithNoNamespaceFix);
3432   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3433             "#define HEADER_GUARD\n"
3434             "namespace my_namespace {\n"
3435             "int i;\n"
3436             "} // my_namespace\n"
3437             "#endif // HEADER_GUARD",
3438             format("#ifndef HEADER_GUARD\n"
3439                    " #define HEADER_GUARD\n"
3440                    "   namespace my_namespace {\n"
3441                    "int i;\n"
3442                    "}    // my_namespace\n"
3443                    "#endif    // HEADER_GUARD",
3444                    LLVMWithNoNamespaceFix));
3445 
3446   EXPECT_EQ("namespace A::B {\n"
3447             "class C {};\n"
3448             "}",
3449             format("namespace A::B {\n"
3450                    "class C {};\n"
3451                    "}",
3452                    LLVMWithNoNamespaceFix));
3453 
3454   FormatStyle Style = getLLVMStyle();
3455   Style.NamespaceIndentation = FormatStyle::NI_All;
3456   EXPECT_EQ("namespace out {\n"
3457             "  int i;\n"
3458             "  namespace in {\n"
3459             "    int i;\n"
3460             "  } // namespace in\n"
3461             "} // namespace out",
3462             format("namespace out {\n"
3463                    "int i;\n"
3464                    "namespace in {\n"
3465                    "int i;\n"
3466                    "} // namespace in\n"
3467                    "} // namespace out",
3468                    Style));
3469 
3470   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3471   EXPECT_EQ("namespace out {\n"
3472             "int i;\n"
3473             "namespace in {\n"
3474             "  int i;\n"
3475             "} // namespace in\n"
3476             "} // namespace out",
3477             format("namespace out {\n"
3478                    "int i;\n"
3479                    "namespace in {\n"
3480                    "int i;\n"
3481                    "} // namespace in\n"
3482                    "} // namespace out",
3483                    Style));
3484 }
3485 
3486 TEST_F(FormatTest, NamespaceMacros) {
3487   FormatStyle Style = getLLVMStyle();
3488   Style.NamespaceMacros.push_back("TESTSUITE");
3489 
3490   verifyFormat("TESTSUITE(A) {\n"
3491                "int foo();\n"
3492                "} // TESTSUITE(A)",
3493                Style);
3494 
3495   verifyFormat("TESTSUITE(A, B) {\n"
3496                "int foo();\n"
3497                "} // TESTSUITE(A)",
3498                Style);
3499 
3500   // Properly indent according to NamespaceIndentation style
3501   Style.NamespaceIndentation = FormatStyle::NI_All;
3502   verifyFormat("TESTSUITE(A) {\n"
3503                "  int foo();\n"
3504                "} // TESTSUITE(A)",
3505                Style);
3506   verifyFormat("TESTSUITE(A) {\n"
3507                "  namespace B {\n"
3508                "    int foo();\n"
3509                "  } // namespace B\n"
3510                "} // TESTSUITE(A)",
3511                Style);
3512   verifyFormat("namespace A {\n"
3513                "  TESTSUITE(B) {\n"
3514                "    int foo();\n"
3515                "  } // TESTSUITE(B)\n"
3516                "} // namespace A",
3517                Style);
3518 
3519   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3520   verifyFormat("TESTSUITE(A) {\n"
3521                "TESTSUITE(B) {\n"
3522                "  int foo();\n"
3523                "} // TESTSUITE(B)\n"
3524                "} // TESTSUITE(A)",
3525                Style);
3526   verifyFormat("TESTSUITE(A) {\n"
3527                "namespace B {\n"
3528                "  int foo();\n"
3529                "} // namespace B\n"
3530                "} // TESTSUITE(A)",
3531                Style);
3532   verifyFormat("namespace A {\n"
3533                "TESTSUITE(B) {\n"
3534                "  int foo();\n"
3535                "} // TESTSUITE(B)\n"
3536                "} // namespace A",
3537                Style);
3538 
3539   // Properly merge namespace-macros blocks in CompactNamespaces mode
3540   Style.NamespaceIndentation = FormatStyle::NI_None;
3541   Style.CompactNamespaces = true;
3542   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3543                "}} // TESTSUITE(A::B)",
3544                Style);
3545 
3546   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3547             "}} // TESTSUITE(out::in)",
3548             format("TESTSUITE(out) {\n"
3549                    "TESTSUITE(in) {\n"
3550                    "} // TESTSUITE(in)\n"
3551                    "} // TESTSUITE(out)",
3552                    Style));
3553 
3554   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3555             "}} // TESTSUITE(out::in)",
3556             format("TESTSUITE(out) {\n"
3557                    "TESTSUITE(in) {\n"
3558                    "} // TESTSUITE(in)\n"
3559                    "} // TESTSUITE(out)",
3560                    Style));
3561 
3562   // Do not merge different namespaces/macros
3563   EXPECT_EQ("namespace out {\n"
3564             "TESTSUITE(in) {\n"
3565             "} // TESTSUITE(in)\n"
3566             "} // namespace out",
3567             format("namespace out {\n"
3568                    "TESTSUITE(in) {\n"
3569                    "} // TESTSUITE(in)\n"
3570                    "} // namespace out",
3571                    Style));
3572   EXPECT_EQ("TESTSUITE(out) {\n"
3573             "namespace in {\n"
3574             "} // namespace in\n"
3575             "} // TESTSUITE(out)",
3576             format("TESTSUITE(out) {\n"
3577                    "namespace in {\n"
3578                    "} // namespace in\n"
3579                    "} // TESTSUITE(out)",
3580                    Style));
3581   Style.NamespaceMacros.push_back("FOOBAR");
3582   EXPECT_EQ("TESTSUITE(out) {\n"
3583             "FOOBAR(in) {\n"
3584             "} // FOOBAR(in)\n"
3585             "} // TESTSUITE(out)",
3586             format("TESTSUITE(out) {\n"
3587                    "FOOBAR(in) {\n"
3588                    "} // FOOBAR(in)\n"
3589                    "} // TESTSUITE(out)",
3590                    Style));
3591 }
3592 
3593 TEST_F(FormatTest, FormatsCompactNamespaces) {
3594   FormatStyle Style = getLLVMStyle();
3595   Style.CompactNamespaces = true;
3596   Style.NamespaceMacros.push_back("TESTSUITE");
3597 
3598   verifyFormat("namespace A { namespace B {\n"
3599                "}} // namespace A::B",
3600                Style);
3601 
3602   EXPECT_EQ("namespace out { namespace in {\n"
3603             "}} // namespace out::in",
3604             format("namespace out {\n"
3605                    "namespace in {\n"
3606                    "} // namespace in\n"
3607                    "} // namespace out",
3608                    Style));
3609 
3610   // Only namespaces which have both consecutive opening and end get compacted
3611   EXPECT_EQ("namespace out {\n"
3612             "namespace in1 {\n"
3613             "} // namespace in1\n"
3614             "namespace in2 {\n"
3615             "} // namespace in2\n"
3616             "} // namespace out",
3617             format("namespace out {\n"
3618                    "namespace in1 {\n"
3619                    "} // namespace in1\n"
3620                    "namespace in2 {\n"
3621                    "} // namespace in2\n"
3622                    "} // namespace out",
3623                    Style));
3624 
3625   EXPECT_EQ("namespace out {\n"
3626             "int i;\n"
3627             "namespace in {\n"
3628             "int j;\n"
3629             "} // namespace in\n"
3630             "int k;\n"
3631             "} // namespace out",
3632             format("namespace out { int i;\n"
3633                    "namespace in { int j; } // namespace in\n"
3634                    "int k; } // namespace out",
3635                    Style));
3636 
3637   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3638             "}}} // namespace A::B::C\n",
3639             format("namespace A { namespace B {\n"
3640                    "namespace C {\n"
3641                    "}} // namespace B::C\n"
3642                    "} // namespace A\n",
3643                    Style));
3644 
3645   Style.ColumnLimit = 40;
3646   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3647             "namespace bbbbbbbbbb {\n"
3648             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3649             format("namespace aaaaaaaaaa {\n"
3650                    "namespace bbbbbbbbbb {\n"
3651                    "} // namespace bbbbbbbbbb\n"
3652                    "} // namespace aaaaaaaaaa",
3653                    Style));
3654 
3655   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3656             "namespace cccccc {\n"
3657             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3658             format("namespace aaaaaa {\n"
3659                    "namespace bbbbbb {\n"
3660                    "namespace cccccc {\n"
3661                    "} // namespace cccccc\n"
3662                    "} // namespace bbbbbb\n"
3663                    "} // namespace aaaaaa",
3664                    Style));
3665   Style.ColumnLimit = 80;
3666 
3667   // Extra semicolon after 'inner' closing brace prevents merging
3668   EXPECT_EQ("namespace out { namespace in {\n"
3669             "}; } // namespace out::in",
3670             format("namespace out {\n"
3671                    "namespace in {\n"
3672                    "}; // namespace in\n"
3673                    "} // namespace out",
3674                    Style));
3675 
3676   // Extra semicolon after 'outer' closing brace is conserved
3677   EXPECT_EQ("namespace out { namespace in {\n"
3678             "}}; // namespace out::in",
3679             format("namespace out {\n"
3680                    "namespace in {\n"
3681                    "} // namespace in\n"
3682                    "}; // namespace out",
3683                    Style));
3684 
3685   Style.NamespaceIndentation = FormatStyle::NI_All;
3686   EXPECT_EQ("namespace out { namespace in {\n"
3687             "  int i;\n"
3688             "}} // namespace out::in",
3689             format("namespace out {\n"
3690                    "namespace in {\n"
3691                    "int i;\n"
3692                    "} // namespace in\n"
3693                    "} // namespace out",
3694                    Style));
3695   EXPECT_EQ("namespace out { namespace mid {\n"
3696             "  namespace in {\n"
3697             "    int j;\n"
3698             "  } // namespace in\n"
3699             "  int k;\n"
3700             "}} // namespace out::mid",
3701             format("namespace out { namespace mid {\n"
3702                    "namespace in { int j; } // namespace in\n"
3703                    "int k; }} // namespace out::mid",
3704                    Style));
3705 
3706   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3707   EXPECT_EQ("namespace out { namespace in {\n"
3708             "  int i;\n"
3709             "}} // namespace out::in",
3710             format("namespace out {\n"
3711                    "namespace in {\n"
3712                    "int i;\n"
3713                    "} // namespace in\n"
3714                    "} // namespace out",
3715                    Style));
3716   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
3717             "  int i;\n"
3718             "}}} // namespace out::mid::in",
3719             format("namespace out {\n"
3720                    "namespace mid {\n"
3721                    "namespace in {\n"
3722                    "int i;\n"
3723                    "} // namespace in\n"
3724                    "} // namespace mid\n"
3725                    "} // namespace out",
3726                    Style));
3727 }
3728 
3729 TEST_F(FormatTest, FormatsExternC) {
3730   verifyFormat("extern \"C\" {\nint a;");
3731   verifyFormat("extern \"C\" {}");
3732   verifyFormat("extern \"C\" {\n"
3733                "int foo();\n"
3734                "}");
3735   verifyFormat("extern \"C\" int foo() {}");
3736   verifyFormat("extern \"C\" int foo();");
3737   verifyFormat("extern \"C\" int foo() {\n"
3738                "  int i = 42;\n"
3739                "  return i;\n"
3740                "}");
3741 
3742   FormatStyle Style = getLLVMStyle();
3743   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3744   Style.BraceWrapping.AfterFunction = true;
3745   verifyFormat("extern \"C\" int foo() {}", Style);
3746   verifyFormat("extern \"C\" int foo();", Style);
3747   verifyFormat("extern \"C\" int foo()\n"
3748                "{\n"
3749                "  int i = 42;\n"
3750                "  return i;\n"
3751                "}",
3752                Style);
3753 
3754   Style.BraceWrapping.AfterExternBlock = true;
3755   Style.BraceWrapping.SplitEmptyRecord = false;
3756   verifyFormat("extern \"C\"\n"
3757                "{}",
3758                Style);
3759   verifyFormat("extern \"C\"\n"
3760                "{\n"
3761                "  int foo();\n"
3762                "}",
3763                Style);
3764 }
3765 
3766 TEST_F(FormatTest, IndentExternBlockStyle) {
3767   FormatStyle Style = getLLVMStyle();
3768   Style.IndentWidth = 2;
3769 
3770   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3771   verifyFormat("extern \"C\" { /*9*/\n}", Style);
3772   verifyFormat("extern \"C\" {\n"
3773                "  int foo10();\n"
3774                "}",
3775                Style);
3776 
3777   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
3778   verifyFormat("extern \"C\" { /*11*/\n}", Style);
3779   verifyFormat("extern \"C\" {\n"
3780                "int foo12();\n"
3781                "}",
3782                Style);
3783 
3784   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3785   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3786   Style.BraceWrapping.AfterExternBlock = true;
3787   verifyFormat("extern \"C\"\n{ /*13*/\n}", Style);
3788   verifyFormat("extern \"C\"\n{\n"
3789                "  int foo14();\n"
3790                "}",
3791                Style);
3792 
3793   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3794   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3795   Style.BraceWrapping.AfterExternBlock = false;
3796   verifyFormat("extern \"C\" { /*15*/\n}", Style);
3797   verifyFormat("extern \"C\" {\n"
3798                "int foo16();\n"
3799                "}",
3800                Style);
3801 }
3802 
3803 TEST_F(FormatTest, FormatsInlineASM) {
3804   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
3805   verifyFormat("asm(\"nop\" ::: \"memory\");");
3806   verifyFormat(
3807       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
3808       "    \"cpuid\\n\\t\"\n"
3809       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
3810       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
3811       "    : \"a\"(value));");
3812   EXPECT_EQ(
3813       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
3814       "  __asm {\n"
3815       "        mov     edx,[that] // vtable in edx\n"
3816       "        mov     eax,methodIndex\n"
3817       "        call    [edx][eax*4] // stdcall\n"
3818       "  }\n"
3819       "}",
3820       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
3821              "    __asm {\n"
3822              "        mov     edx,[that] // vtable in edx\n"
3823              "        mov     eax,methodIndex\n"
3824              "        call    [edx][eax*4] // stdcall\n"
3825              "    }\n"
3826              "}"));
3827   EXPECT_EQ("_asm {\n"
3828             "  xor eax, eax;\n"
3829             "  cpuid;\n"
3830             "}",
3831             format("_asm {\n"
3832                    "  xor eax, eax;\n"
3833                    "  cpuid;\n"
3834                    "}"));
3835   verifyFormat("void function() {\n"
3836                "  // comment\n"
3837                "  asm(\"\");\n"
3838                "}");
3839   EXPECT_EQ("__asm {\n"
3840             "}\n"
3841             "int i;",
3842             format("__asm   {\n"
3843                    "}\n"
3844                    "int   i;"));
3845 }
3846 
3847 TEST_F(FormatTest, FormatTryCatch) {
3848   verifyFormat("try {\n"
3849                "  throw a * b;\n"
3850                "} catch (int a) {\n"
3851                "  // Do nothing.\n"
3852                "} catch (...) {\n"
3853                "  exit(42);\n"
3854                "}");
3855 
3856   // Function-level try statements.
3857   verifyFormat("int f() try { return 4; } catch (...) {\n"
3858                "  return 5;\n"
3859                "}");
3860   verifyFormat("class A {\n"
3861                "  int a;\n"
3862                "  A() try : a(0) {\n"
3863                "  } catch (...) {\n"
3864                "    throw;\n"
3865                "  }\n"
3866                "};\n");
3867   verifyFormat("class A {\n"
3868                "  int a;\n"
3869                "  A() try : a(0), b{1} {\n"
3870                "  } catch (...) {\n"
3871                "    throw;\n"
3872                "  }\n"
3873                "};\n");
3874   verifyFormat("class A {\n"
3875                "  int a;\n"
3876                "  A() try : a(0), b{1}, c{2} {\n"
3877                "  } catch (...) {\n"
3878                "    throw;\n"
3879                "  }\n"
3880                "};\n");
3881   verifyFormat("class A {\n"
3882                "  int a;\n"
3883                "  A() try : a(0), b{1}, c{2} {\n"
3884                "    { // New scope.\n"
3885                "    }\n"
3886                "  } catch (...) {\n"
3887                "    throw;\n"
3888                "  }\n"
3889                "};\n");
3890 
3891   // Incomplete try-catch blocks.
3892   verifyIncompleteFormat("try {} catch (");
3893 }
3894 
3895 TEST_F(FormatTest, FormatTryAsAVariable) {
3896   verifyFormat("int try;");
3897   verifyFormat("int try, size;");
3898   verifyFormat("try = foo();");
3899   verifyFormat("if (try < size) {\n  return true;\n}");
3900 
3901   verifyFormat("int catch;");
3902   verifyFormat("int catch, size;");
3903   verifyFormat("catch = foo();");
3904   verifyFormat("if (catch < size) {\n  return true;\n}");
3905 
3906   FormatStyle Style = getLLVMStyle();
3907   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3908   Style.BraceWrapping.AfterFunction = true;
3909   Style.BraceWrapping.BeforeCatch = true;
3910   verifyFormat("try {\n"
3911                "  int bar = 1;\n"
3912                "}\n"
3913                "catch (...) {\n"
3914                "  int bar = 1;\n"
3915                "}",
3916                Style);
3917   verifyFormat("#if NO_EX\n"
3918                "try\n"
3919                "#endif\n"
3920                "{\n"
3921                "}\n"
3922                "#if NO_EX\n"
3923                "catch (...) {\n"
3924                "}",
3925                Style);
3926   verifyFormat("try /* abc */ {\n"
3927                "  int bar = 1;\n"
3928                "}\n"
3929                "catch (...) {\n"
3930                "  int bar = 1;\n"
3931                "}",
3932                Style);
3933   verifyFormat("try\n"
3934                "// abc\n"
3935                "{\n"
3936                "  int bar = 1;\n"
3937                "}\n"
3938                "catch (...) {\n"
3939                "  int bar = 1;\n"
3940                "}",
3941                Style);
3942 }
3943 
3944 TEST_F(FormatTest, FormatSEHTryCatch) {
3945   verifyFormat("__try {\n"
3946                "  int a = b * c;\n"
3947                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
3948                "  // Do nothing.\n"
3949                "}");
3950 
3951   verifyFormat("__try {\n"
3952                "  int a = b * c;\n"
3953                "} __finally {\n"
3954                "  // Do nothing.\n"
3955                "}");
3956 
3957   verifyFormat("DEBUG({\n"
3958                "  __try {\n"
3959                "  } __finally {\n"
3960                "  }\n"
3961                "});\n");
3962 }
3963 
3964 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
3965   verifyFormat("try {\n"
3966                "  f();\n"
3967                "} catch {\n"
3968                "  g();\n"
3969                "}");
3970   verifyFormat("try {\n"
3971                "  f();\n"
3972                "} catch (A a) MACRO(x) {\n"
3973                "  g();\n"
3974                "} catch (B b) MACRO(x) {\n"
3975                "  g();\n"
3976                "}");
3977 }
3978 
3979 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
3980   FormatStyle Style = getLLVMStyle();
3981   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
3982                           FormatStyle::BS_WebKit}) {
3983     Style.BreakBeforeBraces = BraceStyle;
3984     verifyFormat("try {\n"
3985                  "  // something\n"
3986                  "} catch (...) {\n"
3987                  "  // something\n"
3988                  "}",
3989                  Style);
3990   }
3991   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
3992   verifyFormat("try {\n"
3993                "  // something\n"
3994                "}\n"
3995                "catch (...) {\n"
3996                "  // something\n"
3997                "}",
3998                Style);
3999   verifyFormat("__try {\n"
4000                "  // something\n"
4001                "}\n"
4002                "__finally {\n"
4003                "  // something\n"
4004                "}",
4005                Style);
4006   verifyFormat("@try {\n"
4007                "  // something\n"
4008                "}\n"
4009                "@finally {\n"
4010                "  // something\n"
4011                "}",
4012                Style);
4013   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4014   verifyFormat("try\n"
4015                "{\n"
4016                "  // something\n"
4017                "}\n"
4018                "catch (...)\n"
4019                "{\n"
4020                "  // something\n"
4021                "}",
4022                Style);
4023   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4024   verifyFormat("try\n"
4025                "  {\n"
4026                "  // something white\n"
4027                "  }\n"
4028                "catch (...)\n"
4029                "  {\n"
4030                "  // something white\n"
4031                "  }",
4032                Style);
4033   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4034   verifyFormat("try\n"
4035                "  {\n"
4036                "    // something\n"
4037                "  }\n"
4038                "catch (...)\n"
4039                "  {\n"
4040                "    // something\n"
4041                "  }",
4042                Style);
4043   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4044   Style.BraceWrapping.BeforeCatch = true;
4045   verifyFormat("try {\n"
4046                "  // something\n"
4047                "}\n"
4048                "catch (...) {\n"
4049                "  // something\n"
4050                "}",
4051                Style);
4052 }
4053 
4054 TEST_F(FormatTest, StaticInitializers) {
4055   verifyFormat("static SomeClass SC = {1, 'a'};");
4056 
4057   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4058                "    100000000, "
4059                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4060 
4061   // Here, everything other than the "}" would fit on a line.
4062   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4063                "    10000000000000000000000000};");
4064   EXPECT_EQ("S s = {a,\n"
4065             "\n"
4066             "       b};",
4067             format("S s = {\n"
4068                    "  a,\n"
4069                    "\n"
4070                    "  b\n"
4071                    "};"));
4072 
4073   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4074   // line. However, the formatting looks a bit off and this probably doesn't
4075   // happen often in practice.
4076   verifyFormat("static int Variable[1] = {\n"
4077                "    {1000000000000000000000000000000000000}};",
4078                getLLVMStyleWithColumns(40));
4079 }
4080 
4081 TEST_F(FormatTest, DesignatedInitializers) {
4082   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4083   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4084                "                    .bbbbbbbbbb = 2,\n"
4085                "                    .cccccccccc = 3,\n"
4086                "                    .dddddddddd = 4,\n"
4087                "                    .eeeeeeeeee = 5};");
4088   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4089                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4090                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4091                "    .ccccccccccccccccccccccccccc = 3,\n"
4092                "    .ddddddddddddddddddddddddddd = 4,\n"
4093                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4094 
4095   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4096 
4097   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4098   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4099                "                    [2] = bbbbbbbbbb,\n"
4100                "                    [3] = cccccccccc,\n"
4101                "                    [4] = dddddddddd,\n"
4102                "                    [5] = eeeeeeeeee};");
4103   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4104                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4105                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4106                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4107                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4108                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4109 }
4110 
4111 TEST_F(FormatTest, NestedStaticInitializers) {
4112   verifyFormat("static A x = {{{}}};\n");
4113   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4114                "               {init1, init2, init3, init4}}};",
4115                getLLVMStyleWithColumns(50));
4116 
4117   verifyFormat("somes Status::global_reps[3] = {\n"
4118                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4119                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4120                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4121                getLLVMStyleWithColumns(60));
4122   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4123                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4124                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4125                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4126   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4127                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4128                "rect.fTop}};");
4129 
4130   verifyFormat(
4131       "SomeArrayOfSomeType a = {\n"
4132       "    {{1, 2, 3},\n"
4133       "     {1, 2, 3},\n"
4134       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4135       "      333333333333333333333333333333},\n"
4136       "     {1, 2, 3},\n"
4137       "     {1, 2, 3}}};");
4138   verifyFormat(
4139       "SomeArrayOfSomeType a = {\n"
4140       "    {{1, 2, 3}},\n"
4141       "    {{1, 2, 3}},\n"
4142       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4143       "      333333333333333333333333333333}},\n"
4144       "    {{1, 2, 3}},\n"
4145       "    {{1, 2, 3}}};");
4146 
4147   verifyFormat("struct {\n"
4148                "  unsigned bit;\n"
4149                "  const char *const name;\n"
4150                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4151                "                 {kOsWin, \"Windows\"},\n"
4152                "                 {kOsLinux, \"Linux\"},\n"
4153                "                 {kOsCrOS, \"Chrome OS\"}};");
4154   verifyFormat("struct {\n"
4155                "  unsigned bit;\n"
4156                "  const char *const name;\n"
4157                "} kBitsToOs[] = {\n"
4158                "    {kOsMac, \"Mac\"},\n"
4159                "    {kOsWin, \"Windows\"},\n"
4160                "    {kOsLinux, \"Linux\"},\n"
4161                "    {kOsCrOS, \"Chrome OS\"},\n"
4162                "};");
4163 }
4164 
4165 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4166   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4167                "                      \\\n"
4168                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4169 }
4170 
4171 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4172   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4173                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4174 
4175   // Do break defaulted and deleted functions.
4176   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4177                "    default;",
4178                getLLVMStyleWithColumns(40));
4179   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4180                "    delete;",
4181                getLLVMStyleWithColumns(40));
4182 }
4183 
4184 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4185   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4186                getLLVMStyleWithColumns(40));
4187   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4188                getLLVMStyleWithColumns(40));
4189   EXPECT_EQ("#define Q                              \\\n"
4190             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4191             "  \"aaaaaaaa.cpp\"",
4192             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4193                    getLLVMStyleWithColumns(40)));
4194 }
4195 
4196 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4197   EXPECT_EQ("# 123 \"A string literal\"",
4198             format("   #     123    \"A string literal\""));
4199 }
4200 
4201 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4202   EXPECT_EQ("#;", format("#;"));
4203   verifyFormat("#\n;\n;\n;");
4204 }
4205 
4206 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4207   EXPECT_EQ("#line 42 \"test\"\n",
4208             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4209   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4210                                     getLLVMStyleWithColumns(12)));
4211 }
4212 
4213 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4214   EXPECT_EQ("#line 42 \"test\"",
4215             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4216   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4217 }
4218 
4219 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4220   verifyFormat("#define A \\x20");
4221   verifyFormat("#define A \\ x20");
4222   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4223   verifyFormat("#define A ''");
4224   verifyFormat("#define A ''qqq");
4225   verifyFormat("#define A `qqq");
4226   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4227   EXPECT_EQ("const char *c = STRINGIFY(\n"
4228             "\\na : b);",
4229             format("const char * c = STRINGIFY(\n"
4230                    "\\na : b);"));
4231 
4232   verifyFormat("a\r\\");
4233   verifyFormat("a\v\\");
4234   verifyFormat("a\f\\");
4235 }
4236 
4237 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4238   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4239   style.IndentWidth = 4;
4240   style.PPIndentWidth = 1;
4241 
4242   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4243   verifyFormat("#ifdef __linux__\n"
4244                "void foo() {\n"
4245                "    int x = 0;\n"
4246                "}\n"
4247                "#define FOO\n"
4248                "#endif\n"
4249                "void bar() {\n"
4250                "    int y = 0;\n"
4251                "}\n",
4252                style);
4253 
4254   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4255   verifyFormat("#ifdef __linux__\n"
4256                "void foo() {\n"
4257                "    int x = 0;\n"
4258                "}\n"
4259                "# define FOO foo\n"
4260                "#endif\n"
4261                "void bar() {\n"
4262                "    int y = 0;\n"
4263                "}\n",
4264                style);
4265 
4266   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4267   verifyFormat("#ifdef __linux__\n"
4268                "void foo() {\n"
4269                "    int x = 0;\n"
4270                "}\n"
4271                " #define FOO foo\n"
4272                "#endif\n"
4273                "void bar() {\n"
4274                "    int y = 0;\n"
4275                "}\n",
4276                style);
4277 }
4278 
4279 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4280   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4281   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4282   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4283   // FIXME: We never break before the macro name.
4284   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4285 
4286   verifyFormat("#define A A\n#define A A");
4287   verifyFormat("#define A(X) A\n#define A A");
4288 
4289   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4290   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4291 }
4292 
4293 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4294   EXPECT_EQ("// somecomment\n"
4295             "#include \"a.h\"\n"
4296             "#define A(  \\\n"
4297             "    A, B)\n"
4298             "#include \"b.h\"\n"
4299             "// somecomment\n",
4300             format("  // somecomment\n"
4301                    "  #include \"a.h\"\n"
4302                    "#define A(A,\\\n"
4303                    "    B)\n"
4304                    "    #include \"b.h\"\n"
4305                    " // somecomment\n",
4306                    getLLVMStyleWithColumns(13)));
4307 }
4308 
4309 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4310 
4311 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4312   EXPECT_EQ("#define A    \\\n"
4313             "  c;         \\\n"
4314             "  e;\n"
4315             "f;",
4316             format("#define A c; e;\n"
4317                    "f;",
4318                    getLLVMStyleWithColumns(14)));
4319 }
4320 
4321 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4322 
4323 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4324   EXPECT_EQ("int x,\n"
4325             "#define A\n"
4326             "    y;",
4327             format("int x,\n#define A\ny;"));
4328 }
4329 
4330 TEST_F(FormatTest, HashInMacroDefinition) {
4331   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4332   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4333   verifyFormat("#define A  \\\n"
4334                "  {        \\\n"
4335                "    f(#c); \\\n"
4336                "  }",
4337                getLLVMStyleWithColumns(11));
4338 
4339   verifyFormat("#define A(X)         \\\n"
4340                "  void function##X()",
4341                getLLVMStyleWithColumns(22));
4342 
4343   verifyFormat("#define A(a, b, c)   \\\n"
4344                "  void a##b##c()",
4345                getLLVMStyleWithColumns(22));
4346 
4347   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4348 }
4349 
4350 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4351   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4352   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4353 
4354   FormatStyle Style = getLLVMStyle();
4355   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4356   verifyFormat("#define true ((foo)1)", Style);
4357   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4358   verifyFormat("#define false((foo)0)", Style);
4359 }
4360 
4361 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4362   EXPECT_EQ("#define A b;", format("#define A \\\n"
4363                                    "          \\\n"
4364                                    "  b;",
4365                                    getLLVMStyleWithColumns(25)));
4366   EXPECT_EQ("#define A \\\n"
4367             "          \\\n"
4368             "  a;      \\\n"
4369             "  b;",
4370             format("#define A \\\n"
4371                    "          \\\n"
4372                    "  a;      \\\n"
4373                    "  b;",
4374                    getLLVMStyleWithColumns(11)));
4375   EXPECT_EQ("#define A \\\n"
4376             "  a;      \\\n"
4377             "          \\\n"
4378             "  b;",
4379             format("#define A \\\n"
4380                    "  a;      \\\n"
4381                    "          \\\n"
4382                    "  b;",
4383                    getLLVMStyleWithColumns(11)));
4384 }
4385 
4386 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4387   verifyIncompleteFormat("#define A :");
4388   verifyFormat("#define SOMECASES  \\\n"
4389                "  case 1:          \\\n"
4390                "  case 2\n",
4391                getLLVMStyleWithColumns(20));
4392   verifyFormat("#define MACRO(a) \\\n"
4393                "  if (a)         \\\n"
4394                "    f();         \\\n"
4395                "  else           \\\n"
4396                "    g()",
4397                getLLVMStyleWithColumns(18));
4398   verifyFormat("#define A template <typename T>");
4399   verifyIncompleteFormat("#define STR(x) #x\n"
4400                          "f(STR(this_is_a_string_literal{));");
4401   verifyFormat("#pragma omp threadprivate( \\\n"
4402                "    y)), // expected-warning",
4403                getLLVMStyleWithColumns(28));
4404   verifyFormat("#d, = };");
4405   verifyFormat("#if \"a");
4406   verifyIncompleteFormat("({\n"
4407                          "#define b     \\\n"
4408                          "  }           \\\n"
4409                          "  a\n"
4410                          "a",
4411                          getLLVMStyleWithColumns(15));
4412   verifyFormat("#define A     \\\n"
4413                "  {           \\\n"
4414                "    {\n"
4415                "#define B     \\\n"
4416                "  }           \\\n"
4417                "  }",
4418                getLLVMStyleWithColumns(15));
4419   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4420   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4421   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4422   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4423 }
4424 
4425 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4426   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4427   EXPECT_EQ("class A : public QObject {\n"
4428             "  Q_OBJECT\n"
4429             "\n"
4430             "  A() {}\n"
4431             "};",
4432             format("class A  :  public QObject {\n"
4433                    "     Q_OBJECT\n"
4434                    "\n"
4435                    "  A() {\n}\n"
4436                    "}  ;"));
4437   EXPECT_EQ("MACRO\n"
4438             "/*static*/ int i;",
4439             format("MACRO\n"
4440                    " /*static*/ int   i;"));
4441   EXPECT_EQ("SOME_MACRO\n"
4442             "namespace {\n"
4443             "void f();\n"
4444             "} // namespace",
4445             format("SOME_MACRO\n"
4446                    "  namespace    {\n"
4447                    "void   f(  );\n"
4448                    "} // namespace"));
4449   // Only if the identifier contains at least 5 characters.
4450   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4451   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4452   // Only if everything is upper case.
4453   EXPECT_EQ("class A : public QObject {\n"
4454             "  Q_Object A() {}\n"
4455             "};",
4456             format("class A  :  public QObject {\n"
4457                    "     Q_Object\n"
4458                    "  A() {\n}\n"
4459                    "}  ;"));
4460 
4461   // Only if the next line can actually start an unwrapped line.
4462   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4463             format("SOME_WEIRD_LOG_MACRO\n"
4464                    "<< SomeThing;"));
4465 
4466   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4467                "(n, buffers))\n",
4468                getChromiumStyle(FormatStyle::LK_Cpp));
4469 
4470   // See PR41483
4471   EXPECT_EQ("/**/ FOO(a)\n"
4472             "FOO(b)",
4473             format("/**/ FOO(a)\n"
4474                    "FOO(b)"));
4475 }
4476 
4477 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4478   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4479             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4480             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4481             "class X {};\n"
4482             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4483             "int *createScopDetectionPass() { return 0; }",
4484             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4485                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4486                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4487                    "  class X {};\n"
4488                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4489                    "  int *createScopDetectionPass() { return 0; }"));
4490   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4491   // braces, so that inner block is indented one level more.
4492   EXPECT_EQ("int q() {\n"
4493             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4494             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4495             "  IPC_END_MESSAGE_MAP()\n"
4496             "}",
4497             format("int q() {\n"
4498                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4499                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4500                    "  IPC_END_MESSAGE_MAP()\n"
4501                    "}"));
4502 
4503   // Same inside macros.
4504   EXPECT_EQ("#define LIST(L) \\\n"
4505             "  L(A)          \\\n"
4506             "  L(B)          \\\n"
4507             "  L(C)",
4508             format("#define LIST(L) \\\n"
4509                    "  L(A) \\\n"
4510                    "  L(B) \\\n"
4511                    "  L(C)",
4512                    getGoogleStyle()));
4513 
4514   // These must not be recognized as macros.
4515   EXPECT_EQ("int q() {\n"
4516             "  f(x);\n"
4517             "  f(x) {}\n"
4518             "  f(x)->g();\n"
4519             "  f(x)->*g();\n"
4520             "  f(x).g();\n"
4521             "  f(x) = x;\n"
4522             "  f(x) += x;\n"
4523             "  f(x) -= x;\n"
4524             "  f(x) *= x;\n"
4525             "  f(x) /= x;\n"
4526             "  f(x) %= x;\n"
4527             "  f(x) &= x;\n"
4528             "  f(x) |= x;\n"
4529             "  f(x) ^= x;\n"
4530             "  f(x) >>= x;\n"
4531             "  f(x) <<= x;\n"
4532             "  f(x)[y].z();\n"
4533             "  LOG(INFO) << x;\n"
4534             "  ifstream(x) >> x;\n"
4535             "}\n",
4536             format("int q() {\n"
4537                    "  f(x)\n;\n"
4538                    "  f(x)\n {}\n"
4539                    "  f(x)\n->g();\n"
4540                    "  f(x)\n->*g();\n"
4541                    "  f(x)\n.g();\n"
4542                    "  f(x)\n = x;\n"
4543                    "  f(x)\n += x;\n"
4544                    "  f(x)\n -= x;\n"
4545                    "  f(x)\n *= x;\n"
4546                    "  f(x)\n /= x;\n"
4547                    "  f(x)\n %= x;\n"
4548                    "  f(x)\n &= x;\n"
4549                    "  f(x)\n |= x;\n"
4550                    "  f(x)\n ^= x;\n"
4551                    "  f(x)\n >>= x;\n"
4552                    "  f(x)\n <<= x;\n"
4553                    "  f(x)\n[y].z();\n"
4554                    "  LOG(INFO)\n << x;\n"
4555                    "  ifstream(x)\n >> x;\n"
4556                    "}\n"));
4557   EXPECT_EQ("int q() {\n"
4558             "  F(x)\n"
4559             "  if (1) {\n"
4560             "  }\n"
4561             "  F(x)\n"
4562             "  while (1) {\n"
4563             "  }\n"
4564             "  F(x)\n"
4565             "  G(x);\n"
4566             "  F(x)\n"
4567             "  try {\n"
4568             "    Q();\n"
4569             "  } catch (...) {\n"
4570             "  }\n"
4571             "}\n",
4572             format("int q() {\n"
4573                    "F(x)\n"
4574                    "if (1) {}\n"
4575                    "F(x)\n"
4576                    "while (1) {}\n"
4577                    "F(x)\n"
4578                    "G(x);\n"
4579                    "F(x)\n"
4580                    "try { Q(); } catch (...) {}\n"
4581                    "}\n"));
4582   EXPECT_EQ("class A {\n"
4583             "  A() : t(0) {}\n"
4584             "  A(int i) noexcept() : {}\n"
4585             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4586             "  try : t(0) {\n"
4587             "  } catch (...) {\n"
4588             "  }\n"
4589             "};",
4590             format("class A {\n"
4591                    "  A()\n : t(0) {}\n"
4592                    "  A(int i)\n noexcept() : {}\n"
4593                    "  A(X x)\n"
4594                    "  try : t(0) {} catch (...) {}\n"
4595                    "};"));
4596   FormatStyle Style = getLLVMStyle();
4597   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4598   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4599   Style.BraceWrapping.AfterFunction = true;
4600   EXPECT_EQ("void f()\n"
4601             "try\n"
4602             "{\n"
4603             "}",
4604             format("void f() try {\n"
4605                    "}",
4606                    Style));
4607   EXPECT_EQ("class SomeClass {\n"
4608             "public:\n"
4609             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4610             "};",
4611             format("class SomeClass {\n"
4612                    "public:\n"
4613                    "  SomeClass()\n"
4614                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4615                    "};"));
4616   EXPECT_EQ("class SomeClass {\n"
4617             "public:\n"
4618             "  SomeClass()\n"
4619             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4620             "};",
4621             format("class SomeClass {\n"
4622                    "public:\n"
4623                    "  SomeClass()\n"
4624                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4625                    "};",
4626                    getLLVMStyleWithColumns(40)));
4627 
4628   verifyFormat("MACRO(>)");
4629 
4630   // Some macros contain an implicit semicolon.
4631   Style = getLLVMStyle();
4632   Style.StatementMacros.push_back("FOO");
4633   verifyFormat("FOO(a) int b = 0;");
4634   verifyFormat("FOO(a)\n"
4635                "int b = 0;",
4636                Style);
4637   verifyFormat("FOO(a);\n"
4638                "int b = 0;",
4639                Style);
4640   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
4641                "int b = 0;",
4642                Style);
4643   verifyFormat("FOO()\n"
4644                "int b = 0;",
4645                Style);
4646   verifyFormat("FOO\n"
4647                "int b = 0;",
4648                Style);
4649   verifyFormat("void f() {\n"
4650                "  FOO(a)\n"
4651                "  return a;\n"
4652                "}",
4653                Style);
4654   verifyFormat("FOO(a)\n"
4655                "FOO(b)",
4656                Style);
4657   verifyFormat("int a = 0;\n"
4658                "FOO(b)\n"
4659                "int c = 0;",
4660                Style);
4661   verifyFormat("int a = 0;\n"
4662                "int x = FOO(a)\n"
4663                "int b = 0;",
4664                Style);
4665   verifyFormat("void foo(int a) { FOO(a) }\n"
4666                "uint32_t bar() {}",
4667                Style);
4668 }
4669 
4670 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
4671   verifyFormat("#define A \\\n"
4672                "  f({     \\\n"
4673                "    g();  \\\n"
4674                "  });",
4675                getLLVMStyleWithColumns(11));
4676 }
4677 
4678 TEST_F(FormatTest, IndentPreprocessorDirectives) {
4679   FormatStyle Style = getLLVMStyle();
4680   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
4681   Style.ColumnLimit = 40;
4682   verifyFormat("#ifdef _WIN32\n"
4683                "#define A 0\n"
4684                "#ifdef VAR2\n"
4685                "#define B 1\n"
4686                "#include <someheader.h>\n"
4687                "#define MACRO                          \\\n"
4688                "  some_very_long_func_aaaaaaaaaa();\n"
4689                "#endif\n"
4690                "#else\n"
4691                "#define A 1\n"
4692                "#endif",
4693                Style);
4694   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
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   verifyFormat("#if A\n"
4708                "#  define MACRO                        \\\n"
4709                "    void a(int x) {                    \\\n"
4710                "      b();                             \\\n"
4711                "      c();                             \\\n"
4712                "      d();                             \\\n"
4713                "      e();                             \\\n"
4714                "      f();                             \\\n"
4715                "    }\n"
4716                "#endif",
4717                Style);
4718   // Comments before include guard.
4719   verifyFormat("// file comment\n"
4720                "// file comment\n"
4721                "#ifndef HEADER_H\n"
4722                "#define HEADER_H\n"
4723                "code();\n"
4724                "#endif",
4725                Style);
4726   // Test with include guards.
4727   verifyFormat("#ifndef HEADER_H\n"
4728                "#define HEADER_H\n"
4729                "code();\n"
4730                "#endif",
4731                Style);
4732   // Include guards must have a #define with the same variable immediately
4733   // after #ifndef.
4734   verifyFormat("#ifndef NOT_GUARD\n"
4735                "#  define FOO\n"
4736                "code();\n"
4737                "#endif",
4738                Style);
4739 
4740   // Include guards must cover the entire file.
4741   verifyFormat("code();\n"
4742                "code();\n"
4743                "#ifndef NOT_GUARD\n"
4744                "#  define NOT_GUARD\n"
4745                "code();\n"
4746                "#endif",
4747                Style);
4748   verifyFormat("#ifndef NOT_GUARD\n"
4749                "#  define NOT_GUARD\n"
4750                "code();\n"
4751                "#endif\n"
4752                "code();",
4753                Style);
4754   // Test with trailing blank lines.
4755   verifyFormat("#ifndef HEADER_H\n"
4756                "#define HEADER_H\n"
4757                "code();\n"
4758                "#endif\n",
4759                Style);
4760   // Include guards don't have #else.
4761   verifyFormat("#ifndef NOT_GUARD\n"
4762                "#  define NOT_GUARD\n"
4763                "code();\n"
4764                "#else\n"
4765                "#endif",
4766                Style);
4767   verifyFormat("#ifndef NOT_GUARD\n"
4768                "#  define NOT_GUARD\n"
4769                "code();\n"
4770                "#elif FOO\n"
4771                "#endif",
4772                Style);
4773   // Non-identifier #define after potential include guard.
4774   verifyFormat("#ifndef FOO\n"
4775                "#  define 1\n"
4776                "#endif\n",
4777                Style);
4778   // #if closes past last non-preprocessor line.
4779   verifyFormat("#ifndef FOO\n"
4780                "#define FOO\n"
4781                "#if 1\n"
4782                "int i;\n"
4783                "#  define A 0\n"
4784                "#endif\n"
4785                "#endif\n",
4786                Style);
4787   // Don't crash if there is an #elif directive without a condition.
4788   verifyFormat("#if 1\n"
4789                "int x;\n"
4790                "#elif\n"
4791                "int y;\n"
4792                "#else\n"
4793                "int z;\n"
4794                "#endif",
4795                Style);
4796   // FIXME: This doesn't handle the case where there's code between the
4797   // #ifndef and #define but all other conditions hold. This is because when
4798   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
4799   // previous code line yet, so we can't detect it.
4800   EXPECT_EQ("#ifndef NOT_GUARD\n"
4801             "code();\n"
4802             "#define NOT_GUARD\n"
4803             "code();\n"
4804             "#endif",
4805             format("#ifndef NOT_GUARD\n"
4806                    "code();\n"
4807                    "#  define NOT_GUARD\n"
4808                    "code();\n"
4809                    "#endif",
4810                    Style));
4811   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
4812   // be outside an include guard. Examples are #pragma once and
4813   // #pragma GCC diagnostic, or anything else that does not change the meaning
4814   // of the file if it's included multiple times.
4815   EXPECT_EQ("#ifdef WIN32\n"
4816             "#  pragma once\n"
4817             "#endif\n"
4818             "#ifndef HEADER_H\n"
4819             "#  define HEADER_H\n"
4820             "code();\n"
4821             "#endif",
4822             format("#ifdef WIN32\n"
4823                    "#  pragma once\n"
4824                    "#endif\n"
4825                    "#ifndef HEADER_H\n"
4826                    "#define HEADER_H\n"
4827                    "code();\n"
4828                    "#endif",
4829                    Style));
4830   // FIXME: This does not detect when there is a single non-preprocessor line
4831   // in front of an include-guard-like structure where other conditions hold
4832   // because ScopedLineState hides the line.
4833   EXPECT_EQ("code();\n"
4834             "#ifndef HEADER_H\n"
4835             "#define HEADER_H\n"
4836             "code();\n"
4837             "#endif",
4838             format("code();\n"
4839                    "#ifndef HEADER_H\n"
4840                    "#  define HEADER_H\n"
4841                    "code();\n"
4842                    "#endif",
4843                    Style));
4844   // Keep comments aligned with #, otherwise indent comments normally. These
4845   // tests cannot use verifyFormat because messUp manipulates leading
4846   // whitespace.
4847   {
4848     const char *Expected = ""
4849                            "void f() {\n"
4850                            "#if 1\n"
4851                            "// Preprocessor aligned.\n"
4852                            "#  define A 0\n"
4853                            "  // Code. Separated by blank line.\n"
4854                            "\n"
4855                            "#  define B 0\n"
4856                            "  // Code. Not aligned with #\n"
4857                            "#  define C 0\n"
4858                            "#endif";
4859     const char *ToFormat = ""
4860                            "void f() {\n"
4861                            "#if 1\n"
4862                            "// Preprocessor aligned.\n"
4863                            "#  define A 0\n"
4864                            "// Code. Separated by blank line.\n"
4865                            "\n"
4866                            "#  define B 0\n"
4867                            "   // Code. Not aligned with #\n"
4868                            "#  define C 0\n"
4869                            "#endif";
4870     EXPECT_EQ(Expected, format(ToFormat, Style));
4871     EXPECT_EQ(Expected, format(Expected, Style));
4872   }
4873   // Keep block quotes aligned.
4874   {
4875     const char *Expected = ""
4876                            "void f() {\n"
4877                            "#if 1\n"
4878                            "/* Preprocessor aligned. */\n"
4879                            "#  define A 0\n"
4880                            "  /* Code. Separated by blank line. */\n"
4881                            "\n"
4882                            "#  define B 0\n"
4883                            "  /* Code. Not aligned with # */\n"
4884                            "#  define C 0\n"
4885                            "#endif";
4886     const char *ToFormat = ""
4887                            "void f() {\n"
4888                            "#if 1\n"
4889                            "/* Preprocessor aligned. */\n"
4890                            "#  define A 0\n"
4891                            "/* Code. Separated by blank line. */\n"
4892                            "\n"
4893                            "#  define B 0\n"
4894                            "   /* Code. Not aligned with # */\n"
4895                            "#  define C 0\n"
4896                            "#endif";
4897     EXPECT_EQ(Expected, format(ToFormat, Style));
4898     EXPECT_EQ(Expected, format(Expected, Style));
4899   }
4900   // Keep comments aligned with un-indented directives.
4901   {
4902     const char *Expected = ""
4903                            "void f() {\n"
4904                            "// Preprocessor aligned.\n"
4905                            "#define A 0\n"
4906                            "  // Code. Separated by blank line.\n"
4907                            "\n"
4908                            "#define B 0\n"
4909                            "  // Code. Not aligned with #\n"
4910                            "#define C 0\n";
4911     const char *ToFormat = ""
4912                            "void f() {\n"
4913                            "// Preprocessor aligned.\n"
4914                            "#define A 0\n"
4915                            "// Code. Separated by blank line.\n"
4916                            "\n"
4917                            "#define B 0\n"
4918                            "   // Code. Not aligned with #\n"
4919                            "#define C 0\n";
4920     EXPECT_EQ(Expected, format(ToFormat, Style));
4921     EXPECT_EQ(Expected, format(Expected, Style));
4922   }
4923   // Test AfterHash with tabs.
4924   {
4925     FormatStyle Tabbed = Style;
4926     Tabbed.UseTab = FormatStyle::UT_Always;
4927     Tabbed.IndentWidth = 8;
4928     Tabbed.TabWidth = 8;
4929     verifyFormat("#ifdef _WIN32\n"
4930                  "#\tdefine A 0\n"
4931                  "#\tifdef VAR2\n"
4932                  "#\t\tdefine B 1\n"
4933                  "#\t\tinclude <someheader.h>\n"
4934                  "#\t\tdefine MACRO          \\\n"
4935                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
4936                  "#\tendif\n"
4937                  "#else\n"
4938                  "#\tdefine A 1\n"
4939                  "#endif",
4940                  Tabbed);
4941   }
4942 
4943   // Regression test: Multiline-macro inside include guards.
4944   verifyFormat("#ifndef HEADER_H\n"
4945                "#define HEADER_H\n"
4946                "#define A()        \\\n"
4947                "  int i;           \\\n"
4948                "  int j;\n"
4949                "#endif // HEADER_H",
4950                getLLVMStyleWithColumns(20));
4951 
4952   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4953   // Basic before hash indent tests
4954   verifyFormat("#ifdef _WIN32\n"
4955                "  #define A 0\n"
4956                "  #ifdef VAR2\n"
4957                "    #define B 1\n"
4958                "    #include <someheader.h>\n"
4959                "    #define MACRO                      \\\n"
4960                "      some_very_long_func_aaaaaaaaaa();\n"
4961                "  #endif\n"
4962                "#else\n"
4963                "  #define A 1\n"
4964                "#endif",
4965                Style);
4966   verifyFormat("#if A\n"
4967                "  #define MACRO                        \\\n"
4968                "    void a(int x) {                    \\\n"
4969                "      b();                             \\\n"
4970                "      c();                             \\\n"
4971                "      d();                             \\\n"
4972                "      e();                             \\\n"
4973                "      f();                             \\\n"
4974                "    }\n"
4975                "#endif",
4976                Style);
4977   // Keep comments aligned with indented directives. These
4978   // tests cannot use verifyFormat because messUp manipulates leading
4979   // whitespace.
4980   {
4981     const char *Expected = "void f() {\n"
4982                            "// Aligned to preprocessor.\n"
4983                            "#if 1\n"
4984                            "  // Aligned to code.\n"
4985                            "  int a;\n"
4986                            "  #if 1\n"
4987                            "    // Aligned to preprocessor.\n"
4988                            "    #define A 0\n"
4989                            "  // Aligned to code.\n"
4990                            "  int b;\n"
4991                            "  #endif\n"
4992                            "#endif\n"
4993                            "}";
4994     const char *ToFormat = "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     EXPECT_EQ(Expected, format(ToFormat, Style));
5008     EXPECT_EQ(Expected, format(Expected, Style));
5009   }
5010   {
5011     const char *Expected = "void f() {\n"
5012                            "/* Aligned to preprocessor. */\n"
5013                            "#if 1\n"
5014                            "  /* Aligned to code. */\n"
5015                            "  int a;\n"
5016                            "  #if 1\n"
5017                            "    /* Aligned to preprocessor. */\n"
5018                            "    #define A 0\n"
5019                            "  /* Aligned to code. */\n"
5020                            "  int b;\n"
5021                            "  #endif\n"
5022                            "#endif\n"
5023                            "}";
5024     const char *ToFormat = "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     EXPECT_EQ(Expected, format(ToFormat, Style));
5038     EXPECT_EQ(Expected, format(Expected, Style));
5039   }
5040 
5041   // Test single comment before preprocessor
5042   verifyFormat("// Comment\n"
5043                "\n"
5044                "#if 1\n"
5045                "#endif",
5046                Style);
5047 }
5048 
5049 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5050   verifyFormat("{\n  { a #c; }\n}");
5051 }
5052 
5053 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5054   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5055             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5056   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5057             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5058 }
5059 
5060 TEST_F(FormatTest, EscapedNewlines) {
5061   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5062   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5063             format("#define A \\\nint i;\\\n  int j;", Narrow));
5064   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5065   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5066   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5067   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5068 
5069   FormatStyle AlignLeft = getLLVMStyle();
5070   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5071   EXPECT_EQ("#define MACRO(x) \\\n"
5072             "private:         \\\n"
5073             "  int x(int a);\n",
5074             format("#define MACRO(x) \\\n"
5075                    "private:         \\\n"
5076                    "  int x(int a);\n",
5077                    AlignLeft));
5078 
5079   // CRLF line endings
5080   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5081             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5082   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5083   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5084   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5085   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5086   EXPECT_EQ("#define MACRO(x) \\\r\n"
5087             "private:         \\\r\n"
5088             "  int x(int a);\r\n",
5089             format("#define MACRO(x) \\\r\n"
5090                    "private:         \\\r\n"
5091                    "  int x(int a);\r\n",
5092                    AlignLeft));
5093 
5094   FormatStyle DontAlign = getLLVMStyle();
5095   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5096   DontAlign.MaxEmptyLinesToKeep = 3;
5097   // FIXME: can't use verifyFormat here because the newline before
5098   // "public:" is not inserted the first time it's reformatted
5099   EXPECT_EQ("#define A \\\n"
5100             "  class Foo { \\\n"
5101             "    void bar(); \\\n"
5102             "\\\n"
5103             "\\\n"
5104             "\\\n"
5105             "  public: \\\n"
5106             "    void baz(); \\\n"
5107             "  };",
5108             format("#define A \\\n"
5109                    "  class Foo { \\\n"
5110                    "    void bar(); \\\n"
5111                    "\\\n"
5112                    "\\\n"
5113                    "\\\n"
5114                    "  public: \\\n"
5115                    "    void baz(); \\\n"
5116                    "  };",
5117                    DontAlign));
5118 }
5119 
5120 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5121   verifyFormat("#define A \\\n"
5122                "  int v(  \\\n"
5123                "      a); \\\n"
5124                "  int i;",
5125                getLLVMStyleWithColumns(11));
5126 }
5127 
5128 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5129   EXPECT_EQ(
5130       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5131       "                      \\\n"
5132       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5133       "\n"
5134       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5135       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5136       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5137              "\\\n"
5138              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5139              "  \n"
5140              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5141              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5142 }
5143 
5144 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5145   EXPECT_EQ("int\n"
5146             "#define A\n"
5147             "    a;",
5148             format("int\n#define A\na;"));
5149   verifyFormat("functionCallTo(\n"
5150                "    someOtherFunction(\n"
5151                "        withSomeParameters, whichInSequence,\n"
5152                "        areLongerThanALine(andAnotherCall,\n"
5153                "#define A B\n"
5154                "                           withMoreParamters,\n"
5155                "                           whichStronglyInfluenceTheLayout),\n"
5156                "        andMoreParameters),\n"
5157                "    trailing);",
5158                getLLVMStyleWithColumns(69));
5159   verifyFormat("Foo::Foo()\n"
5160                "#ifdef BAR\n"
5161                "    : baz(0)\n"
5162                "#endif\n"
5163                "{\n"
5164                "}");
5165   verifyFormat("void f() {\n"
5166                "  if (true)\n"
5167                "#ifdef A\n"
5168                "    f(42);\n"
5169                "  x();\n"
5170                "#else\n"
5171                "    g();\n"
5172                "  x();\n"
5173                "#endif\n"
5174                "}");
5175   verifyFormat("void f(param1, param2,\n"
5176                "       param3,\n"
5177                "#ifdef A\n"
5178                "       param4(param5,\n"
5179                "#ifdef A1\n"
5180                "              param6,\n"
5181                "#ifdef A2\n"
5182                "              param7),\n"
5183                "#else\n"
5184                "              param8),\n"
5185                "       param9,\n"
5186                "#endif\n"
5187                "       param10,\n"
5188                "#endif\n"
5189                "       param11)\n"
5190                "#else\n"
5191                "       param12)\n"
5192                "#endif\n"
5193                "{\n"
5194                "  x();\n"
5195                "}",
5196                getLLVMStyleWithColumns(28));
5197   verifyFormat("#if 1\n"
5198                "int i;");
5199   verifyFormat("#if 1\n"
5200                "#endif\n"
5201                "#if 1\n"
5202                "#else\n"
5203                "#endif\n");
5204   verifyFormat("DEBUG({\n"
5205                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5206                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5207                "});\n"
5208                "#if a\n"
5209                "#else\n"
5210                "#endif");
5211 
5212   verifyIncompleteFormat("void f(\n"
5213                          "#if A\n"
5214                          ");\n"
5215                          "#else\n"
5216                          "#endif");
5217 }
5218 
5219 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5220   verifyFormat("#endif\n"
5221                "#if B");
5222 }
5223 
5224 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5225   FormatStyle SingleLine = getLLVMStyle();
5226   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5227   verifyFormat("#if 0\n"
5228                "#elif 1\n"
5229                "#endif\n"
5230                "void foo() {\n"
5231                "  if (test) foo2();\n"
5232                "}",
5233                SingleLine);
5234 }
5235 
5236 TEST_F(FormatTest, LayoutBlockInsideParens) {
5237   verifyFormat("functionCall({ int i; });");
5238   verifyFormat("functionCall({\n"
5239                "  int i;\n"
5240                "  int j;\n"
5241                "});");
5242   verifyFormat("functionCall(\n"
5243                "    {\n"
5244                "      int i;\n"
5245                "      int j;\n"
5246                "    },\n"
5247                "    aaaa, bbbb, cccc);");
5248   verifyFormat("functionA(functionB({\n"
5249                "            int i;\n"
5250                "            int j;\n"
5251                "          }),\n"
5252                "          aaaa, bbbb, cccc);");
5253   verifyFormat("functionCall(\n"
5254                "    {\n"
5255                "      int i;\n"
5256                "      int j;\n"
5257                "    },\n"
5258                "    aaaa, bbbb, // comment\n"
5259                "    cccc);");
5260   verifyFormat("functionA(functionB({\n"
5261                "            int i;\n"
5262                "            int j;\n"
5263                "          }),\n"
5264                "          aaaa, bbbb, // comment\n"
5265                "          cccc);");
5266   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5267   verifyFormat("functionCall(aaaa, bbbb, {\n"
5268                "  int i;\n"
5269                "  int j;\n"
5270                "});");
5271   verifyFormat(
5272       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5273       "    {\n"
5274       "      int i; // break\n"
5275       "    },\n"
5276       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5277       "                                     ccccccccccccccccc));");
5278   verifyFormat("DEBUG({\n"
5279                "  if (a)\n"
5280                "    f();\n"
5281                "});");
5282 }
5283 
5284 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5285   EXPECT_EQ("SOME_MACRO { int i; }\n"
5286             "int i;",
5287             format("  SOME_MACRO  {int i;}  int i;"));
5288 }
5289 
5290 TEST_F(FormatTest, LayoutNestedBlocks) {
5291   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5292                "  struct s {\n"
5293                "    int i;\n"
5294                "  };\n"
5295                "  s kBitsToOs[] = {{10}};\n"
5296                "  for (int i = 0; i < 10; ++i)\n"
5297                "    return;\n"
5298                "}");
5299   verifyFormat("call(parameter, {\n"
5300                "  something();\n"
5301                "  // Comment using all columns.\n"
5302                "  somethingelse();\n"
5303                "});",
5304                getLLVMStyleWithColumns(40));
5305   verifyFormat("DEBUG( //\n"
5306                "    { f(); }, a);");
5307   verifyFormat("DEBUG( //\n"
5308                "    {\n"
5309                "      f(); //\n"
5310                "    },\n"
5311                "    a);");
5312 
5313   EXPECT_EQ("call(parameter, {\n"
5314             "  something();\n"
5315             "  // Comment too\n"
5316             "  // looooooooooong.\n"
5317             "  somethingElse();\n"
5318             "});",
5319             format("call(parameter, {\n"
5320                    "  something();\n"
5321                    "  // Comment too looooooooooong.\n"
5322                    "  somethingElse();\n"
5323                    "});",
5324                    getLLVMStyleWithColumns(29)));
5325   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5326   EXPECT_EQ("DEBUG({ // comment\n"
5327             "  int i;\n"
5328             "});",
5329             format("DEBUG({ // comment\n"
5330                    "int  i;\n"
5331                    "});"));
5332   EXPECT_EQ("DEBUG({\n"
5333             "  int i;\n"
5334             "\n"
5335             "  // comment\n"
5336             "  int j;\n"
5337             "});",
5338             format("DEBUG({\n"
5339                    "  int  i;\n"
5340                    "\n"
5341                    "  // comment\n"
5342                    "  int  j;\n"
5343                    "});"));
5344 
5345   verifyFormat("DEBUG({\n"
5346                "  if (a)\n"
5347                "    return;\n"
5348                "});");
5349   verifyGoogleFormat("DEBUG({\n"
5350                      "  if (a) return;\n"
5351                      "});");
5352   FormatStyle Style = getGoogleStyle();
5353   Style.ColumnLimit = 45;
5354   verifyFormat("Debug(\n"
5355                "    aaaaa,\n"
5356                "    {\n"
5357                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5358                "    },\n"
5359                "    a);",
5360                Style);
5361 
5362   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5363 
5364   verifyNoCrash("^{v^{a}}");
5365 }
5366 
5367 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5368   EXPECT_EQ("#define MACRO()                     \\\n"
5369             "  Debug(aaa, /* force line break */ \\\n"
5370             "        {                           \\\n"
5371             "          int i;                    \\\n"
5372             "          int j;                    \\\n"
5373             "        })",
5374             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5375                    "          {  int   i;  int  j;   })",
5376                    getGoogleStyle()));
5377 
5378   EXPECT_EQ("#define A                                       \\\n"
5379             "  [] {                                          \\\n"
5380             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5381             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5382             "  }",
5383             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5384                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5385                    getGoogleStyle()));
5386 }
5387 
5388 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5389   EXPECT_EQ("{}", format("{}"));
5390   verifyFormat("enum E {};");
5391   verifyFormat("enum E {}");
5392   FormatStyle Style = getLLVMStyle();
5393   Style.SpaceInEmptyBlock = true;
5394   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5395   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5396   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5397 }
5398 
5399 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5400   FormatStyle Style = getLLVMStyle();
5401   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5402   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5403   verifyFormat("FOO_BEGIN\n"
5404                "  FOO_ENTRY\n"
5405                "FOO_END",
5406                Style);
5407   verifyFormat("FOO_BEGIN\n"
5408                "  NESTED_FOO_BEGIN\n"
5409                "    NESTED_FOO_ENTRY\n"
5410                "  NESTED_FOO_END\n"
5411                "FOO_END",
5412                Style);
5413   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5414                "  int x;\n"
5415                "  x = 1;\n"
5416                "FOO_END(Baz)",
5417                Style);
5418 }
5419 
5420 //===----------------------------------------------------------------------===//
5421 // Line break tests.
5422 //===----------------------------------------------------------------------===//
5423 
5424 TEST_F(FormatTest, PreventConfusingIndents) {
5425   verifyFormat(
5426       "void f() {\n"
5427       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5428       "                         parameter, parameter, parameter)),\n"
5429       "                     SecondLongCall(parameter));\n"
5430       "}");
5431   verifyFormat(
5432       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5433       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5434       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5435       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5436   verifyFormat(
5437       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5438       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5439       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5440       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5441   verifyFormat(
5442       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5443       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5444       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5445       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5446   verifyFormat("int a = bbbb && ccc &&\n"
5447                "        fffff(\n"
5448                "#define A Just forcing a new line\n"
5449                "            ddd);");
5450 }
5451 
5452 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5453   verifyFormat(
5454       "bool aaaaaaa =\n"
5455       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5456       "    bbbbbbbb();");
5457   verifyFormat(
5458       "bool aaaaaaa =\n"
5459       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5460       "    bbbbbbbb();");
5461 
5462   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5463                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5464                "    ccccccccc == ddddddddddd;");
5465   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5466                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5467                "    ccccccccc == ddddddddddd;");
5468   verifyFormat(
5469       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5470       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5471       "    ccccccccc == ddddddddddd;");
5472 
5473   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5474                "                 aaaaaa) &&\n"
5475                "         bbbbbb && cccccc;");
5476   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5477                "                 aaaaaa) >>\n"
5478                "         bbbbbb;");
5479   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5480                "    SourceMgr.getSpellingColumnNumber(\n"
5481                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5482                "    1);");
5483 
5484   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5485                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5486                "    cccccc) {\n}");
5487   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5488                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5489                "              cccccc) {\n}");
5490   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5491                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5492                "              cccccc) {\n}");
5493   verifyFormat("b = a &&\n"
5494                "    // Comment\n"
5495                "    b.c && d;");
5496 
5497   // If the LHS of a comparison is not a binary expression itself, the
5498   // additional linebreak confuses many people.
5499   verifyFormat(
5500       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5501       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5502       "}");
5503   verifyFormat(
5504       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5505       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5506       "}");
5507   verifyFormat(
5508       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5509       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5510       "}");
5511   verifyFormat(
5512       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5513       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5514       "}");
5515   // Even explicit parentheses stress the precedence enough to make the
5516   // additional break unnecessary.
5517   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5518                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5519                "}");
5520   // This cases is borderline, but with the indentation it is still readable.
5521   verifyFormat(
5522       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5523       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5524       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5525       "}",
5526       getLLVMStyleWithColumns(75));
5527 
5528   // If the LHS is a binary expression, we should still use the additional break
5529   // as otherwise the formatting hides the operator precedence.
5530   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5531                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5532                "    5) {\n"
5533                "}");
5534   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5535                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5536                "    5) {\n"
5537                "}");
5538 
5539   FormatStyle OnePerLine = getLLVMStyle();
5540   OnePerLine.BinPackParameters = false;
5541   verifyFormat(
5542       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5543       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5544       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5545       OnePerLine);
5546 
5547   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5548                "                .aaa(aaaaaaaaaaaaa) *\n"
5549                "            aaaaaaa +\n"
5550                "        aaaaaaa;",
5551                getLLVMStyleWithColumns(40));
5552 }
5553 
5554 TEST_F(FormatTest, ExpressionIndentation) {
5555   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5556                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5557                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5558                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5559                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5560                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5561                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5562                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5563                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5564   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5565                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5566                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5567                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5568   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5569                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5570                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5571                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5572   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5573                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5574                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5575                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5576   verifyFormat("if () {\n"
5577                "} else if (aaaaa && bbbbb > // break\n"
5578                "                        ccccc) {\n"
5579                "}");
5580   verifyFormat("if () {\n"
5581                "} else if constexpr (aaaaa && bbbbb > // break\n"
5582                "                                  ccccc) {\n"
5583                "}");
5584   verifyFormat("if () {\n"
5585                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5586                "                                  ccccc) {\n"
5587                "}");
5588   verifyFormat("if () {\n"
5589                "} else if (aaaaa &&\n"
5590                "           bbbbb > // break\n"
5591                "               ccccc &&\n"
5592                "           ddddd) {\n"
5593                "}");
5594 
5595   // Presence of a trailing comment used to change indentation of b.
5596   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
5597                "       b;\n"
5598                "return aaaaaaaaaaaaaaaaaaa +\n"
5599                "       b; //",
5600                getLLVMStyleWithColumns(30));
5601 }
5602 
5603 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
5604   // Not sure what the best system is here. Like this, the LHS can be found
5605   // immediately above an operator (everything with the same or a higher
5606   // indent). The RHS is aligned right of the operator and so compasses
5607   // everything until something with the same indent as the operator is found.
5608   // FIXME: Is this a good system?
5609   FormatStyle Style = getLLVMStyle();
5610   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5611   verifyFormat(
5612       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5613       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5614       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5615       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5616       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5617       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5618       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5619       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5620       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
5621       Style);
5622   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5623                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5624                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5625                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5626                Style);
5627   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5628                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5629                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5630                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5631                Style);
5632   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5633                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5634                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5635                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5636                Style);
5637   verifyFormat("if () {\n"
5638                "} else if (aaaaa\n"
5639                "           && bbbbb // break\n"
5640                "                  > ccccc) {\n"
5641                "}",
5642                Style);
5643   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5644                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5645                Style);
5646   verifyFormat("return (a)\n"
5647                "       // comment\n"
5648                "       + b;",
5649                Style);
5650   verifyFormat(
5651       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5652       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5653       "             + cc;",
5654       Style);
5655 
5656   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5657                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5658                Style);
5659 
5660   // Forced by comments.
5661   verifyFormat(
5662       "unsigned ContentSize =\n"
5663       "    sizeof(int16_t)   // DWARF ARange version number\n"
5664       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5665       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5666       "    + sizeof(int8_t); // Segment Size (in bytes)");
5667 
5668   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5669                "       == boost::fusion::at_c<1>(iiii).second;",
5670                Style);
5671 
5672   Style.ColumnLimit = 60;
5673   verifyFormat("zzzzzzzzzz\n"
5674                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5675                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5676                Style);
5677 
5678   Style.ColumnLimit = 80;
5679   Style.IndentWidth = 4;
5680   Style.TabWidth = 4;
5681   Style.UseTab = FormatStyle::UT_Always;
5682   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5683   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5684   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
5685             "\t&& (someOtherLongishConditionPart1\n"
5686             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
5687             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
5688                    "(someOtherLongishConditionPart1 || "
5689                    "someOtherEvenLongerNestedConditionPart2);",
5690                    Style));
5691 }
5692 
5693 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
5694   FormatStyle Style = getLLVMStyle();
5695   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5696   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
5697 
5698   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5699                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5700                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5701                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5702                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5703                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5704                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5705                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5706                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
5707                Style);
5708   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5709                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5710                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5711                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5712                Style);
5713   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5714                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5715                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5716                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5717                Style);
5718   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5719                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5720                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5721                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5722                Style);
5723   verifyFormat("if () {\n"
5724                "} else if (aaaaa\n"
5725                "           && bbbbb // break\n"
5726                "                  > ccccc) {\n"
5727                "}",
5728                Style);
5729   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5730                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5731                Style);
5732   verifyFormat("return (a)\n"
5733                "     // comment\n"
5734                "     + b;",
5735                Style);
5736   verifyFormat(
5737       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5738       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5739       "           + cc;",
5740       Style);
5741   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
5742                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5743                "                        : 3333333333333333;",
5744                Style);
5745   verifyFormat(
5746       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
5747       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
5748       "                                             : eeeeeeeeeeeeeeeeee)\n"
5749       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5750       "                        : 3333333333333333;",
5751       Style);
5752   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5753                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5754                Style);
5755 
5756   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5757                "    == boost::fusion::at_c<1>(iiii).second;",
5758                Style);
5759 
5760   Style.ColumnLimit = 60;
5761   verifyFormat("zzzzzzzzzzzzz\n"
5762                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5763                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5764                Style);
5765 
5766   // Forced by comments.
5767   Style.ColumnLimit = 80;
5768   verifyFormat(
5769       "unsigned ContentSize\n"
5770       "    = sizeof(int16_t) // DWARF ARange version number\n"
5771       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5772       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5773       "    + sizeof(int8_t); // Segment Size (in bytes)",
5774       Style);
5775 
5776   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5777   verifyFormat(
5778       "unsigned ContentSize =\n"
5779       "    sizeof(int16_t)   // DWARF ARange version number\n"
5780       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5781       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5782       "    + sizeof(int8_t); // Segment Size (in bytes)",
5783       Style);
5784 
5785   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5786   verifyFormat(
5787       "unsigned ContentSize =\n"
5788       "    sizeof(int16_t)   // DWARF ARange version number\n"
5789       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5790       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5791       "    + sizeof(int8_t); // Segment Size (in bytes)",
5792       Style);
5793 }
5794 
5795 TEST_F(FormatTest, EnforcedOperatorWraps) {
5796   // Here we'd like to wrap after the || operators, but a comment is forcing an
5797   // earlier wrap.
5798   verifyFormat("bool x = aaaaa //\n"
5799                "         || bbbbb\n"
5800                "         //\n"
5801                "         || cccc;");
5802 }
5803 
5804 TEST_F(FormatTest, NoOperandAlignment) {
5805   FormatStyle Style = getLLVMStyle();
5806   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5807   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
5808                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5809                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5810                Style);
5811   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5812   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5813                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5814                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5815                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5816                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5817                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5818                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5819                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5820                "        > ccccccccccccccccccccccccccccccccccccccccc;",
5821                Style);
5822 
5823   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5824                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5825                "    + cc;",
5826                Style);
5827   verifyFormat("int a = aa\n"
5828                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5829                "        * cccccccccccccccccccccccccccccccccccc;\n",
5830                Style);
5831 
5832   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5833   verifyFormat("return (a > b\n"
5834                "    // comment1\n"
5835                "    // comment2\n"
5836                "    || c);",
5837                Style);
5838 }
5839 
5840 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5841   FormatStyle Style = getLLVMStyle();
5842   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5843   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5844                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5845                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5846                Style);
5847 }
5848 
5849 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
5850   FormatStyle Style = getLLVMStyle();
5851   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5852   Style.BinPackArguments = false;
5853   Style.ColumnLimit = 40;
5854   verifyFormat("void test() {\n"
5855                "  someFunction(\n"
5856                "      this + argument + is + quite\n"
5857                "      + long + so + it + gets + wrapped\n"
5858                "      + but + remains + bin - packed);\n"
5859                "}",
5860                Style);
5861   verifyFormat("void test() {\n"
5862                "  someFunction(arg1,\n"
5863                "               this + argument + is\n"
5864                "                   + quite + long + so\n"
5865                "                   + it + gets + wrapped\n"
5866                "                   + but + remains + bin\n"
5867                "                   - packed,\n"
5868                "               arg3);\n"
5869                "}",
5870                Style);
5871   verifyFormat("void test() {\n"
5872                "  someFunction(\n"
5873                "      arg1,\n"
5874                "      this + argument + has\n"
5875                "          + anotherFunc(nested,\n"
5876                "                        calls + whose\n"
5877                "                            + arguments\n"
5878                "                            + are + also\n"
5879                "                            + wrapped,\n"
5880                "                        in + addition)\n"
5881                "          + to + being + bin - packed,\n"
5882                "      arg3);\n"
5883                "}",
5884                Style);
5885 
5886   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5887   verifyFormat("void test() {\n"
5888                "  someFunction(\n"
5889                "      arg1,\n"
5890                "      this + argument + has +\n"
5891                "          anotherFunc(nested,\n"
5892                "                      calls + whose +\n"
5893                "                          arguments +\n"
5894                "                          are + also +\n"
5895                "                          wrapped,\n"
5896                "                      in + addition) +\n"
5897                "          to + being + bin - packed,\n"
5898                "      arg3);\n"
5899                "}",
5900                Style);
5901 }
5902 
5903 TEST_F(FormatTest, ConstructorInitializers) {
5904   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5905   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
5906                getLLVMStyleWithColumns(45));
5907   verifyFormat("Constructor()\n"
5908                "    : Inttializer(FitsOnTheLine) {}",
5909                getLLVMStyleWithColumns(44));
5910   verifyFormat("Constructor()\n"
5911                "    : Inttializer(FitsOnTheLine) {}",
5912                getLLVMStyleWithColumns(43));
5913 
5914   verifyFormat("template <typename T>\n"
5915                "Constructor() : Initializer(FitsOnTheLine) {}",
5916                getLLVMStyleWithColumns(45));
5917 
5918   verifyFormat(
5919       "SomeClass::Constructor()\n"
5920       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5921 
5922   verifyFormat(
5923       "SomeClass::Constructor()\n"
5924       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5925       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
5926   verifyFormat(
5927       "SomeClass::Constructor()\n"
5928       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5929       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5930   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5931                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5932                "    : aaaaaaaaaa(aaaaaa) {}");
5933 
5934   verifyFormat("Constructor()\n"
5935                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5936                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5937                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5938                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
5939 
5940   verifyFormat("Constructor()\n"
5941                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5942                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5943 
5944   verifyFormat("Constructor(int Parameter = 0)\n"
5945                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5946                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
5947   verifyFormat("Constructor()\n"
5948                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5949                "}",
5950                getLLVMStyleWithColumns(60));
5951   verifyFormat("Constructor()\n"
5952                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5953                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
5954 
5955   // Here a line could be saved by splitting the second initializer onto two
5956   // lines, but that is not desirable.
5957   verifyFormat("Constructor()\n"
5958                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5959                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
5960                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5961 
5962   FormatStyle OnePerLine = getLLVMStyle();
5963   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5964   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
5965   verifyFormat("SomeClass::Constructor()\n"
5966                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5967                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5968                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5969                OnePerLine);
5970   verifyFormat("SomeClass::Constructor()\n"
5971                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
5972                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5973                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5974                OnePerLine);
5975   verifyFormat("MyClass::MyClass(int var)\n"
5976                "    : some_var_(var),            // 4 space indent\n"
5977                "      some_other_var_(var + 1) { // lined up\n"
5978                "}",
5979                OnePerLine);
5980   verifyFormat("Constructor()\n"
5981                "    : aaaaa(aaaaaa),\n"
5982                "      aaaaa(aaaaaa),\n"
5983                "      aaaaa(aaaaaa),\n"
5984                "      aaaaa(aaaaaa),\n"
5985                "      aaaaa(aaaaaa) {}",
5986                OnePerLine);
5987   verifyFormat("Constructor()\n"
5988                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5989                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
5990                OnePerLine);
5991   OnePerLine.BinPackParameters = false;
5992   verifyFormat(
5993       "Constructor()\n"
5994       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5995       "          aaaaaaaaaaa().aaa(),\n"
5996       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5997       OnePerLine);
5998   OnePerLine.ColumnLimit = 60;
5999   verifyFormat("Constructor()\n"
6000                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6001                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6002                OnePerLine);
6003 
6004   EXPECT_EQ("Constructor()\n"
6005             "    : // Comment forcing unwanted break.\n"
6006             "      aaaa(aaaa) {}",
6007             format("Constructor() :\n"
6008                    "    // Comment forcing unwanted break.\n"
6009                    "    aaaa(aaaa) {}"));
6010 }
6011 
6012 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6013   FormatStyle Style = getLLVMStyle();
6014   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6015   Style.ColumnLimit = 60;
6016   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6017   Style.AllowAllConstructorInitializersOnNextLine = true;
6018   Style.BinPackParameters = false;
6019 
6020   for (int i = 0; i < 4; ++i) {
6021     // Test all combinations of parameters that should not have an effect.
6022     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6023     Style.AllowAllArgumentsOnNextLine = i & 2;
6024 
6025     Style.AllowAllConstructorInitializersOnNextLine = true;
6026     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6027     verifyFormat("Constructor()\n"
6028                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6029                  Style);
6030     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6031 
6032     Style.AllowAllConstructorInitializersOnNextLine = false;
6033     verifyFormat("Constructor()\n"
6034                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6035                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6036                  Style);
6037     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6038 
6039     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6040     Style.AllowAllConstructorInitializersOnNextLine = true;
6041     verifyFormat("Constructor()\n"
6042                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6043                  Style);
6044 
6045     Style.AllowAllConstructorInitializersOnNextLine = false;
6046     verifyFormat("Constructor()\n"
6047                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6048                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6049                  Style);
6050 
6051     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6052     Style.AllowAllConstructorInitializersOnNextLine = true;
6053     verifyFormat("Constructor() :\n"
6054                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6055                  Style);
6056 
6057     Style.AllowAllConstructorInitializersOnNextLine = false;
6058     verifyFormat("Constructor() :\n"
6059                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6060                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6061                  Style);
6062   }
6063 
6064   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6065   // AllowAllConstructorInitializersOnNextLine in all
6066   // BreakConstructorInitializers modes
6067   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6068   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6069   Style.AllowAllConstructorInitializersOnNextLine = false;
6070   verifyFormat("SomeClassWithALongName::Constructor(\n"
6071                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6072                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6073                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6074                Style);
6075 
6076   Style.AllowAllConstructorInitializersOnNextLine = true;
6077   verifyFormat("SomeClassWithALongName::Constructor(\n"
6078                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6079                "    int bbbbbbbbbbbbb,\n"
6080                "    int cccccccccccccccc)\n"
6081                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6082                Style);
6083 
6084   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6085   Style.AllowAllConstructorInitializersOnNextLine = false;
6086   verifyFormat("SomeClassWithALongName::Constructor(\n"
6087                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6088                "    int bbbbbbbbbbbbb)\n"
6089                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6090                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6091                Style);
6092 
6093   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6094 
6095   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6096   verifyFormat("SomeClassWithALongName::Constructor(\n"
6097                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6098                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6099                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6100                Style);
6101 
6102   Style.AllowAllConstructorInitializersOnNextLine = true;
6103   verifyFormat("SomeClassWithALongName::Constructor(\n"
6104                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6105                "    int bbbbbbbbbbbbb,\n"
6106                "    int cccccccccccccccc)\n"
6107                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6108                Style);
6109 
6110   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6111   Style.AllowAllConstructorInitializersOnNextLine = false;
6112   verifyFormat("SomeClassWithALongName::Constructor(\n"
6113                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6114                "    int bbbbbbbbbbbbb)\n"
6115                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6116                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6117                Style);
6118 
6119   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6120   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6121   verifyFormat("SomeClassWithALongName::Constructor(\n"
6122                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6123                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6124                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6125                Style);
6126 
6127   Style.AllowAllConstructorInitializersOnNextLine = true;
6128   verifyFormat("SomeClassWithALongName::Constructor(\n"
6129                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6130                "    int bbbbbbbbbbbbb,\n"
6131                "    int cccccccccccccccc) :\n"
6132                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6133                Style);
6134 
6135   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6136   Style.AllowAllConstructorInitializersOnNextLine = false;
6137   verifyFormat("SomeClassWithALongName::Constructor(\n"
6138                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6139                "    int bbbbbbbbbbbbb) :\n"
6140                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6141                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6142                Style);
6143 }
6144 
6145 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6146   FormatStyle Style = getLLVMStyle();
6147   Style.ColumnLimit = 60;
6148   Style.BinPackArguments = false;
6149   for (int i = 0; i < 4; ++i) {
6150     // Test all combinations of parameters that should not have an effect.
6151     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6152     Style.AllowAllConstructorInitializersOnNextLine = i & 2;
6153 
6154     Style.AllowAllArgumentsOnNextLine = true;
6155     verifyFormat("void foo() {\n"
6156                  "  FunctionCallWithReallyLongName(\n"
6157                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6158                  "}",
6159                  Style);
6160     Style.AllowAllArgumentsOnNextLine = false;
6161     verifyFormat("void foo() {\n"
6162                  "  FunctionCallWithReallyLongName(\n"
6163                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6164                  "      bbbbbbbbbbbb);\n"
6165                  "}",
6166                  Style);
6167 
6168     Style.AllowAllArgumentsOnNextLine = true;
6169     verifyFormat("void foo() {\n"
6170                  "  auto VariableWithReallyLongName = {\n"
6171                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6172                  "}",
6173                  Style);
6174     Style.AllowAllArgumentsOnNextLine = false;
6175     verifyFormat("void foo() {\n"
6176                  "  auto VariableWithReallyLongName = {\n"
6177                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6178                  "      bbbbbbbbbbbb};\n"
6179                  "}",
6180                  Style);
6181   }
6182 
6183   // This parameter should not affect declarations.
6184   Style.BinPackParameters = false;
6185   Style.AllowAllArgumentsOnNextLine = false;
6186   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6187   verifyFormat("void FunctionCallWithReallyLongName(\n"
6188                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6189                Style);
6190   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6191   verifyFormat("void FunctionCallWithReallyLongName(\n"
6192                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6193                "    int bbbbbbbbbbbb);",
6194                Style);
6195 }
6196 
6197 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6198   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6199   // and BAS_Align.
6200   auto Style = getLLVMStyle();
6201   Style.ColumnLimit = 35;
6202   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6203                     "void functionDecl(int A, int B, int C);";
6204   Style.AllowAllArgumentsOnNextLine = false;
6205   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6206   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6207                       "    paramC);\n"
6208                       "void functionDecl(int A, int B,\n"
6209                       "    int C);"),
6210             format(Input, Style));
6211   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6212   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6213                       "             paramC);\n"
6214                       "void functionDecl(int A, int B,\n"
6215                       "                  int C);"),
6216             format(Input, Style));
6217   // However, BAS_AlwaysBreak should take precedence over
6218   // AllowAllArgumentsOnNextLine.
6219   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6220   EXPECT_EQ(StringRef("functionCall(\n"
6221                       "    paramA, paramB, paramC);\n"
6222                       "void functionDecl(\n"
6223                       "    int A, int B, int C);"),
6224             format(Input, Style));
6225 
6226   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6227   // first argument.
6228   Style.AllowAllArgumentsOnNextLine = true;
6229   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6230   EXPECT_EQ(StringRef("functionCall(\n"
6231                       "    paramA, paramB, paramC);\n"
6232                       "void functionDecl(\n"
6233                       "    int A, int B, int C);"),
6234             format(Input, Style));
6235   // It wouldn't fit on one line with aligned parameters so this setting
6236   // doesn't change anything for BAS_Align.
6237   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6238   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6239                       "             paramC);\n"
6240                       "void functionDecl(int A, int B,\n"
6241                       "                  int C);"),
6242             format(Input, Style));
6243   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6244   EXPECT_EQ(StringRef("functionCall(\n"
6245                       "    paramA, paramB, paramC);\n"
6246                       "void functionDecl(\n"
6247                       "    int A, int B, int C);"),
6248             format(Input, Style));
6249 }
6250 
6251 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6252   FormatStyle Style = getLLVMStyle();
6253   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6254 
6255   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6256   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6257                getStyleWithColumns(Style, 45));
6258   verifyFormat("Constructor() :\n"
6259                "    Initializer(FitsOnTheLine) {}",
6260                getStyleWithColumns(Style, 44));
6261   verifyFormat("Constructor() :\n"
6262                "    Initializer(FitsOnTheLine) {}",
6263                getStyleWithColumns(Style, 43));
6264 
6265   verifyFormat("template <typename T>\n"
6266                "Constructor() : Initializer(FitsOnTheLine) {}",
6267                getStyleWithColumns(Style, 50));
6268   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6269   verifyFormat(
6270       "SomeClass::Constructor() :\n"
6271       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6272       Style);
6273 
6274   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
6275   verifyFormat(
6276       "SomeClass::Constructor() :\n"
6277       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6278       Style);
6279 
6280   verifyFormat(
6281       "SomeClass::Constructor() :\n"
6282       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6283       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6284       Style);
6285   verifyFormat(
6286       "SomeClass::Constructor() :\n"
6287       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6288       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6289       Style);
6290   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6291                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6292                "    aaaaaaaaaa(aaaaaa) {}",
6293                Style);
6294 
6295   verifyFormat("Constructor() :\n"
6296                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6297                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6298                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6299                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6300                Style);
6301 
6302   verifyFormat("Constructor() :\n"
6303                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6304                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6305                Style);
6306 
6307   verifyFormat("Constructor(int Parameter = 0) :\n"
6308                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6309                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6310                Style);
6311   verifyFormat("Constructor() :\n"
6312                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6313                "}",
6314                getStyleWithColumns(Style, 60));
6315   verifyFormat("Constructor() :\n"
6316                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6317                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6318                Style);
6319 
6320   // Here a line could be saved by splitting the second initializer onto two
6321   // lines, but that is not desirable.
6322   verifyFormat("Constructor() :\n"
6323                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6324                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6325                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6326                Style);
6327 
6328   FormatStyle OnePerLine = Style;
6329   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6330   OnePerLine.AllowAllConstructorInitializersOnNextLine = false;
6331   verifyFormat("SomeClass::Constructor() :\n"
6332                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6333                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6334                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6335                OnePerLine);
6336   verifyFormat("SomeClass::Constructor() :\n"
6337                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6338                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6339                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6340                OnePerLine);
6341   verifyFormat("MyClass::MyClass(int var) :\n"
6342                "    some_var_(var),            // 4 space indent\n"
6343                "    some_other_var_(var + 1) { // lined up\n"
6344                "}",
6345                OnePerLine);
6346   verifyFormat("Constructor() :\n"
6347                "    aaaaa(aaaaaa),\n"
6348                "    aaaaa(aaaaaa),\n"
6349                "    aaaaa(aaaaaa),\n"
6350                "    aaaaa(aaaaaa),\n"
6351                "    aaaaa(aaaaaa) {}",
6352                OnePerLine);
6353   verifyFormat("Constructor() :\n"
6354                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6355                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6356                OnePerLine);
6357   OnePerLine.BinPackParameters = false;
6358   verifyFormat("Constructor() :\n"
6359                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6360                "        aaaaaaaaaaa().aaa(),\n"
6361                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6362                OnePerLine);
6363   OnePerLine.ColumnLimit = 60;
6364   verifyFormat("Constructor() :\n"
6365                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6366                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6367                OnePerLine);
6368 
6369   EXPECT_EQ("Constructor() :\n"
6370             "    // Comment forcing unwanted break.\n"
6371             "    aaaa(aaaa) {}",
6372             format("Constructor() :\n"
6373                    "    // Comment forcing unwanted break.\n"
6374                    "    aaaa(aaaa) {}",
6375                    Style));
6376 
6377   Style.ColumnLimit = 0;
6378   verifyFormat("SomeClass::Constructor() :\n"
6379                "    a(a) {}",
6380                Style);
6381   verifyFormat("SomeClass::Constructor() noexcept :\n"
6382                "    a(a) {}",
6383                Style);
6384   verifyFormat("SomeClass::Constructor() :\n"
6385                "    a(a), b(b), c(c) {}",
6386                Style);
6387   verifyFormat("SomeClass::Constructor() :\n"
6388                "    a(a) {\n"
6389                "  foo();\n"
6390                "  bar();\n"
6391                "}",
6392                Style);
6393 
6394   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6395   verifyFormat("SomeClass::Constructor() :\n"
6396                "    a(a), b(b), c(c) {\n"
6397                "}",
6398                Style);
6399   verifyFormat("SomeClass::Constructor() :\n"
6400                "    a(a) {\n"
6401                "}",
6402                Style);
6403 
6404   Style.ColumnLimit = 80;
6405   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6406   Style.ConstructorInitializerIndentWidth = 2;
6407   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6408   verifyFormat("SomeClass::Constructor() :\n"
6409                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6410                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6411                Style);
6412 
6413   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6414   // well
6415   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6416   verifyFormat(
6417       "class SomeClass\n"
6418       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6419       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6420       Style);
6421   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6422   verifyFormat(
6423       "class SomeClass\n"
6424       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6425       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6426       Style);
6427   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6428   verifyFormat(
6429       "class SomeClass :\n"
6430       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6431       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6432       Style);
6433   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6434   verifyFormat(
6435       "class SomeClass\n"
6436       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6437       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6438       Style);
6439 }
6440 
6441 #ifndef EXPENSIVE_CHECKS
6442 // Expensive checks enables libstdc++ checking which includes validating the
6443 // state of ranges used in std::priority_queue - this blows out the
6444 // runtime/scalability of the function and makes this test unacceptably slow.
6445 TEST_F(FormatTest, MemoizationTests) {
6446   // This breaks if the memoization lookup does not take \c Indent and
6447   // \c LastSpace into account.
6448   verifyFormat(
6449       "extern CFRunLoopTimerRef\n"
6450       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6451       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6452       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6453       "                     CFRunLoopTimerContext *context) {}");
6454 
6455   // Deep nesting somewhat works around our memoization.
6456   verifyFormat(
6457       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6458       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6459       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6460       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6461       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6462       getLLVMStyleWithColumns(65));
6463   verifyFormat(
6464       "aaaaa(\n"
6465       "    aaaaa,\n"
6466       "    aaaaa(\n"
6467       "        aaaaa,\n"
6468       "        aaaaa(\n"
6469       "            aaaaa,\n"
6470       "            aaaaa(\n"
6471       "                aaaaa,\n"
6472       "                aaaaa(\n"
6473       "                    aaaaa,\n"
6474       "                    aaaaa(\n"
6475       "                        aaaaa,\n"
6476       "                        aaaaa(\n"
6477       "                            aaaaa,\n"
6478       "                            aaaaa(\n"
6479       "                                aaaaa,\n"
6480       "                                aaaaa(\n"
6481       "                                    aaaaa,\n"
6482       "                                    aaaaa(\n"
6483       "                                        aaaaa,\n"
6484       "                                        aaaaa(\n"
6485       "                                            aaaaa,\n"
6486       "                                            aaaaa(\n"
6487       "                                                aaaaa,\n"
6488       "                                                aaaaa))))))))))));",
6489       getLLVMStyleWithColumns(65));
6490   verifyFormat(
6491       "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"
6492       "                                  a),\n"
6493       "                                a),\n"
6494       "                              a),\n"
6495       "                            a),\n"
6496       "                          a),\n"
6497       "                        a),\n"
6498       "                      a),\n"
6499       "                    a),\n"
6500       "                  a),\n"
6501       "                a),\n"
6502       "              a),\n"
6503       "            a),\n"
6504       "          a),\n"
6505       "        a),\n"
6506       "      a),\n"
6507       "    a),\n"
6508       "  a)",
6509       getLLVMStyleWithColumns(65));
6510 
6511   // This test takes VERY long when memoization is broken.
6512   FormatStyle OnePerLine = getLLVMStyle();
6513   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6514   OnePerLine.BinPackParameters = false;
6515   std::string input = "Constructor()\n"
6516                       "    : aaaa(a,\n";
6517   for (unsigned i = 0, e = 80; i != e; ++i) {
6518     input += "           a,\n";
6519   }
6520   input += "           a) {}";
6521   verifyFormat(input, OnePerLine);
6522 }
6523 #endif
6524 
6525 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6526   verifyFormat(
6527       "void f() {\n"
6528       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6529       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6530       "    f();\n"
6531       "}");
6532   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6533                "    Intervals[i - 1].getRange().getLast()) {\n}");
6534 }
6535 
6536 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6537   // Principially, we break function declarations in a certain order:
6538   // 1) break amongst arguments.
6539   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6540                "                              Cccccccccccccc cccccccccccccc);");
6541   verifyFormat("template <class TemplateIt>\n"
6542                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6543                "                            TemplateIt *stop) {}");
6544 
6545   // 2) break after return type.
6546   verifyFormat(
6547       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6548       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6549       getGoogleStyle());
6550 
6551   // 3) break after (.
6552   verifyFormat(
6553       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6554       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6555       getGoogleStyle());
6556 
6557   // 4) break before after nested name specifiers.
6558   verifyFormat(
6559       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6560       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6561       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6562       getGoogleStyle());
6563 
6564   // However, there are exceptions, if a sufficient amount of lines can be
6565   // saved.
6566   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6567   // more adjusting.
6568   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6569                "                                  Cccccccccccccc cccccccccc,\n"
6570                "                                  Cccccccccccccc cccccccccc,\n"
6571                "                                  Cccccccccccccc cccccccccc,\n"
6572                "                                  Cccccccccccccc cccccccccc);");
6573   verifyFormat(
6574       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6575       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6576       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6577       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6578       getGoogleStyle());
6579   verifyFormat(
6580       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6581       "                                          Cccccccccccccc cccccccccc,\n"
6582       "                                          Cccccccccccccc cccccccccc,\n"
6583       "                                          Cccccccccccccc cccccccccc,\n"
6584       "                                          Cccccccccccccc cccccccccc,\n"
6585       "                                          Cccccccccccccc cccccccccc,\n"
6586       "                                          Cccccccccccccc cccccccccc);");
6587   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6588                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6589                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6590                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6591                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
6592 
6593   // Break after multi-line parameters.
6594   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6595                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6596                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6597                "    bbbb bbbb);");
6598   verifyFormat("void SomeLoooooooooooongFunction(\n"
6599                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6600                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6601                "    int bbbbbbbbbbbbb);");
6602 
6603   // Treat overloaded operators like other functions.
6604   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6605                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
6606   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6607                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
6608   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6609                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
6610   verifyGoogleFormat(
6611       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
6612       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6613   verifyGoogleFormat(
6614       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
6615       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6616   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6617                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6618   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
6619                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6620   verifyGoogleFormat(
6621       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
6622       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6623       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
6624   verifyGoogleFormat("template <typename T>\n"
6625                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6626                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
6627                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
6628 
6629   FormatStyle Style = getLLVMStyle();
6630   Style.PointerAlignment = FormatStyle::PAS_Left;
6631   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6632                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
6633                Style);
6634   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
6635                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6636                Style);
6637 }
6638 
6639 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
6640   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
6641   // Prefer keeping `::` followed by `operator` together.
6642   EXPECT_EQ("const aaaa::bbbbbbb &\n"
6643             "ccccccccc::operator++() {\n"
6644             "  stuff();\n"
6645             "}",
6646             format("const aaaa::bbbbbbb\n"
6647                    "&ccccccccc::operator++() { stuff(); }",
6648                    getLLVMStyleWithColumns(40)));
6649 }
6650 
6651 TEST_F(FormatTest, TrailingReturnType) {
6652   verifyFormat("auto foo() -> int;\n");
6653   // correct trailing return type spacing
6654   verifyFormat("auto operator->() -> int;\n");
6655   verifyFormat("auto operator++(int) -> int;\n");
6656 
6657   verifyFormat("struct S {\n"
6658                "  auto bar() const -> int;\n"
6659                "};");
6660   verifyFormat("template <size_t Order, typename T>\n"
6661                "auto load_img(const std::string &filename)\n"
6662                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
6663   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
6664                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
6665   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
6666   verifyFormat("template <typename T>\n"
6667                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
6668                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
6669 
6670   // Not trailing return types.
6671   verifyFormat("void f() { auto a = b->c(); }");
6672 }
6673 
6674 TEST_F(FormatTest, DeductionGuides) {
6675   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
6676   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
6677   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
6678   verifyFormat(
6679       "template <class... T>\n"
6680       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
6681   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
6682   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
6683   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
6684   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
6685   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
6686   verifyFormat("template <class T> x() -> x<1>;");
6687   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
6688 
6689   // Ensure not deduction guides.
6690   verifyFormat("c()->f<int>();");
6691   verifyFormat("x()->foo<1>;");
6692   verifyFormat("x = p->foo<3>();");
6693   verifyFormat("x()->x<1>();");
6694   verifyFormat("x()->x<1>;");
6695 }
6696 
6697 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
6698   // Avoid breaking before trailing 'const' or other trailing annotations, if
6699   // they are not function-like.
6700   FormatStyle Style = getGoogleStyle();
6701   Style.ColumnLimit = 47;
6702   verifyFormat("void someLongFunction(\n"
6703                "    int someLoooooooooooooongParameter) const {\n}",
6704                getLLVMStyleWithColumns(47));
6705   verifyFormat("LoooooongReturnType\n"
6706                "someLoooooooongFunction() const {}",
6707                getLLVMStyleWithColumns(47));
6708   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
6709                "    const {}",
6710                Style);
6711   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6712                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
6713   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6714                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
6715   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6716                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
6717   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
6718                "                   aaaaaaaaaaa aaaaa) const override;");
6719   verifyGoogleFormat(
6720       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6721       "    const override;");
6722 
6723   // Even if the first parameter has to be wrapped.
6724   verifyFormat("void someLongFunction(\n"
6725                "    int someLongParameter) const {}",
6726                getLLVMStyleWithColumns(46));
6727   verifyFormat("void someLongFunction(\n"
6728                "    int someLongParameter) const {}",
6729                Style);
6730   verifyFormat("void someLongFunction(\n"
6731                "    int someLongParameter) override {}",
6732                Style);
6733   verifyFormat("void someLongFunction(\n"
6734                "    int someLongParameter) OVERRIDE {}",
6735                Style);
6736   verifyFormat("void someLongFunction(\n"
6737                "    int someLongParameter) final {}",
6738                Style);
6739   verifyFormat("void someLongFunction(\n"
6740                "    int someLongParameter) FINAL {}",
6741                Style);
6742   verifyFormat("void someLongFunction(\n"
6743                "    int parameter) const override {}",
6744                Style);
6745 
6746   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
6747   verifyFormat("void someLongFunction(\n"
6748                "    int someLongParameter) const\n"
6749                "{\n"
6750                "}",
6751                Style);
6752 
6753   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
6754   verifyFormat("void someLongFunction(\n"
6755                "    int someLongParameter) const\n"
6756                "  {\n"
6757                "  }",
6758                Style);
6759 
6760   // Unless these are unknown annotations.
6761   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
6762                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6763                "    LONG_AND_UGLY_ANNOTATION;");
6764 
6765   // Breaking before function-like trailing annotations is fine to keep them
6766   // close to their arguments.
6767   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6768                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6769   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6770                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6771   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6772                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
6773   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
6774                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
6775   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
6776 
6777   verifyFormat(
6778       "void aaaaaaaaaaaaaaaaaa()\n"
6779       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
6780       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
6781   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6782                "    __attribute__((unused));");
6783   verifyGoogleFormat(
6784       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6785       "    GUARDED_BY(aaaaaaaaaaaa);");
6786   verifyGoogleFormat(
6787       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6788       "    GUARDED_BY(aaaaaaaaaaaa);");
6789   verifyGoogleFormat(
6790       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6791       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6792   verifyGoogleFormat(
6793       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6794       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
6795 }
6796 
6797 TEST_F(FormatTest, FunctionAnnotations) {
6798   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6799                "int OldFunction(const string &parameter) {}");
6800   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6801                "string OldFunction(const string &parameter) {}");
6802   verifyFormat("template <typename T>\n"
6803                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6804                "string OldFunction(const string &parameter) {}");
6805 
6806   // Not function annotations.
6807   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6808                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
6809   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
6810                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
6811   verifyFormat("MACRO(abc).function() // wrap\n"
6812                "    << abc;");
6813   verifyFormat("MACRO(abc)->function() // wrap\n"
6814                "    << abc;");
6815   verifyFormat("MACRO(abc)::function() // wrap\n"
6816                "    << abc;");
6817 }
6818 
6819 TEST_F(FormatTest, BreaksDesireably) {
6820   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6821                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6822                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6823   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6824                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6825                "}");
6826 
6827   verifyFormat(
6828       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6829       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6830 
6831   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6832                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6833                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6834 
6835   verifyFormat(
6836       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6837       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6838       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6839       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6840       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
6841 
6842   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6843                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6844 
6845   verifyFormat(
6846       "void f() {\n"
6847       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6848       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6849       "}");
6850   verifyFormat(
6851       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6852       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6853   verifyFormat(
6854       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6855       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6856   verifyFormat(
6857       "aaaaaa(aaa,\n"
6858       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6859       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6860       "       aaaa);");
6861   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6862                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6863                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6864 
6865   // Indent consistently independent of call expression and unary operator.
6866   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6867                "    dddddddddddddddddddddddddddddd));");
6868   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6869                "    dddddddddddddddddddddddddddddd));");
6870   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
6871                "    dddddddddddddddddddddddddddddd));");
6872 
6873   // This test case breaks on an incorrect memoization, i.e. an optimization not
6874   // taking into account the StopAt value.
6875   verifyFormat(
6876       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6877       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6878       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6879       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6880 
6881   verifyFormat("{\n  {\n    {\n"
6882                "      Annotation.SpaceRequiredBefore =\n"
6883                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
6884                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
6885                "    }\n  }\n}");
6886 
6887   // Break on an outer level if there was a break on an inner level.
6888   EXPECT_EQ("f(g(h(a, // comment\n"
6889             "      b, c),\n"
6890             "    d, e),\n"
6891             "  x, y);",
6892             format("f(g(h(a, // comment\n"
6893                    "    b, c), d, e), x, y);"));
6894 
6895   // Prefer breaking similar line breaks.
6896   verifyFormat(
6897       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
6898       "                             NSTrackingMouseEnteredAndExited |\n"
6899       "                             NSTrackingActiveAlways;");
6900 }
6901 
6902 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
6903   FormatStyle NoBinPacking = getGoogleStyle();
6904   NoBinPacking.BinPackParameters = false;
6905   NoBinPacking.BinPackArguments = true;
6906   verifyFormat("void f() {\n"
6907                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
6908                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6909                "}",
6910                NoBinPacking);
6911   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
6912                "       int aaaaaaaaaaaaaaaaaaaa,\n"
6913                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6914                NoBinPacking);
6915 
6916   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6917   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6918                "                        vector<int> bbbbbbbbbbbbbbb);",
6919                NoBinPacking);
6920   // FIXME: This behavior difference is probably not wanted. However, currently
6921   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
6922   // template arguments from BreakBeforeParameter being set because of the
6923   // one-per-line formatting.
6924   verifyFormat(
6925       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6926       "                                             aaaaaaaaaa> aaaaaaaaaa);",
6927       NoBinPacking);
6928   verifyFormat(
6929       "void fffffffffff(\n"
6930       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
6931       "        aaaaaaaaaa);");
6932 }
6933 
6934 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
6935   FormatStyle NoBinPacking = getGoogleStyle();
6936   NoBinPacking.BinPackParameters = false;
6937   NoBinPacking.BinPackArguments = false;
6938   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
6939                "  aaaaaaaaaaaaaaaaaaaa,\n"
6940                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
6941                NoBinPacking);
6942   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
6943                "        aaaaaaaaaaaaa,\n"
6944                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
6945                NoBinPacking);
6946   verifyFormat(
6947       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6948       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6949       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6950       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6951       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
6952       NoBinPacking);
6953   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6954                "    .aaaaaaaaaaaaaaaaaa();",
6955                NoBinPacking);
6956   verifyFormat("void f() {\n"
6957                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6958                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
6959                "}",
6960                NoBinPacking);
6961 
6962   verifyFormat(
6963       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6964       "             aaaaaaaaaaaa,\n"
6965       "             aaaaaaaaaaaa);",
6966       NoBinPacking);
6967   verifyFormat(
6968       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
6969       "                               ddddddddddddddddddddddddddddd),\n"
6970       "             test);",
6971       NoBinPacking);
6972 
6973   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6974                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
6975                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
6976                "    aaaaaaaaaaaaaaaaaa;",
6977                NoBinPacking);
6978   verifyFormat("a(\"a\"\n"
6979                "  \"a\",\n"
6980                "  a);");
6981 
6982   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6983   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
6984                "                aaaaaaaaa,\n"
6985                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6986                NoBinPacking);
6987   verifyFormat(
6988       "void f() {\n"
6989       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6990       "      .aaaaaaa();\n"
6991       "}",
6992       NoBinPacking);
6993   verifyFormat(
6994       "template <class SomeType, class SomeOtherType>\n"
6995       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
6996       NoBinPacking);
6997 }
6998 
6999 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7000   FormatStyle Style = getLLVMStyleWithColumns(15);
7001   Style.ExperimentalAutoDetectBinPacking = true;
7002   EXPECT_EQ("aaa(aaaa,\n"
7003             "    aaaa,\n"
7004             "    aaaa);\n"
7005             "aaa(aaaa,\n"
7006             "    aaaa,\n"
7007             "    aaaa);",
7008             format("aaa(aaaa,\n" // one-per-line
7009                    "  aaaa,\n"
7010                    "    aaaa  );\n"
7011                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7012                    Style));
7013   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7014             "    aaaa);\n"
7015             "aaa(aaaa, aaaa,\n"
7016             "    aaaa);",
7017             format("aaa(aaaa,  aaaa,\n" // bin-packed
7018                    "    aaaa  );\n"
7019                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7020                    Style));
7021 }
7022 
7023 TEST_F(FormatTest, FormatsBuilderPattern) {
7024   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7025                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7026                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7027                "    .StartsWith(\".init\", ORDER_INIT)\n"
7028                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7029                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7030                "    .Default(ORDER_TEXT);\n");
7031 
7032   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7033                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7034   verifyFormat("aaaaaaa->aaaaaaa\n"
7035                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7036                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7037                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7038   verifyFormat(
7039       "aaaaaaa->aaaaaaa\n"
7040       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7041       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7042   verifyFormat(
7043       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7044       "    aaaaaaaaaaaaaa);");
7045   verifyFormat(
7046       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7047       "    aaaaaa->aaaaaaaaaaaa()\n"
7048       "        ->aaaaaaaaaaaaaaaa(\n"
7049       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7050       "        ->aaaaaaaaaaaaaaaaa();");
7051   verifyGoogleFormat(
7052       "void f() {\n"
7053       "  someo->Add((new util::filetools::Handler(dir))\n"
7054       "                 ->OnEvent1(NewPermanentCallback(\n"
7055       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7056       "                 ->OnEvent2(NewPermanentCallback(\n"
7057       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7058       "                 ->OnEvent3(NewPermanentCallback(\n"
7059       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7060       "                 ->OnEvent5(NewPermanentCallback(\n"
7061       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7062       "                 ->OnEvent6(NewPermanentCallback(\n"
7063       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7064       "}");
7065 
7066   verifyFormat(
7067       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7068   verifyFormat("aaaaaaaaaaaaaaa()\n"
7069                "    .aaaaaaaaaaaaaaa()\n"
7070                "    .aaaaaaaaaaaaaaa()\n"
7071                "    .aaaaaaaaaaaaaaa()\n"
7072                "    .aaaaaaaaaaaaaaa();");
7073   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7074                "    .aaaaaaaaaaaaaaa()\n"
7075                "    .aaaaaaaaaaaaaaa()\n"
7076                "    .aaaaaaaaaaaaaaa();");
7077   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7078                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7079                "    .aaaaaaaaaaaaaaa();");
7080   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7081                "    ->aaaaaaaaaaaaaae(0)\n"
7082                "    ->aaaaaaaaaaaaaaa();");
7083 
7084   // Don't linewrap after very short segments.
7085   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7086                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7087                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7088   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7089                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7090                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7091   verifyFormat("aaa()\n"
7092                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7093                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7094                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7095 
7096   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7097                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7098                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7099   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7100                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7101                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7102 
7103   // Prefer not to break after empty parentheses.
7104   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7105                "    First->LastNewlineOffset);");
7106 
7107   // Prefer not to create "hanging" indents.
7108   verifyFormat(
7109       "return !soooooooooooooome_map\n"
7110       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7111       "            .second;");
7112   verifyFormat(
7113       "return aaaaaaaaaaaaaaaa\n"
7114       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7115       "    .aaaa(aaaaaaaaaaaaaa);");
7116   // No hanging indent here.
7117   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7118                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7119   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7120                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7121   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7122                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7123                getLLVMStyleWithColumns(60));
7124   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7125                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7126                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7127                getLLVMStyleWithColumns(59));
7128   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7129                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7130                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7131 
7132   // Dont break if only closing statements before member call
7133   verifyFormat("test() {\n"
7134                "  ([]() -> {\n"
7135                "    int b = 32;\n"
7136                "    return 3;\n"
7137                "  }).foo();\n"
7138                "}");
7139   verifyFormat("test() {\n"
7140                "  (\n"
7141                "      []() -> {\n"
7142                "        int b = 32;\n"
7143                "        return 3;\n"
7144                "      },\n"
7145                "      foo, bar)\n"
7146                "      .foo();\n"
7147                "}");
7148   verifyFormat("test() {\n"
7149                "  ([]() -> {\n"
7150                "    int b = 32;\n"
7151                "    return 3;\n"
7152                "  })\n"
7153                "      .foo()\n"
7154                "      .bar();\n"
7155                "}");
7156   verifyFormat("test() {\n"
7157                "  ([]() -> {\n"
7158                "    int b = 32;\n"
7159                "    return 3;\n"
7160                "  })\n"
7161                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7162                "           \"bbbb\");\n"
7163                "}",
7164                getLLVMStyleWithColumns(30));
7165 }
7166 
7167 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7168   verifyFormat(
7169       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7170       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7171   verifyFormat(
7172       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7173       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7174 
7175   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7176                "    ccccccccccccccccccccccccc) {\n}");
7177   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7178                "    ccccccccccccccccccccccccc) {\n}");
7179 
7180   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7181                "    ccccccccccccccccccccccccc) {\n}");
7182   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7183                "    ccccccccccccccccccccccccc) {\n}");
7184 
7185   verifyFormat(
7186       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7187       "    ccccccccccccccccccccccccc) {\n}");
7188   verifyFormat(
7189       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7190       "    ccccccccccccccccccccccccc) {\n}");
7191 
7192   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7193                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7194                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7195                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7196   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7197                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7198                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7199                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7200 
7201   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7202                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7203                "    aaaaaaaaaaaaaaa != aa) {\n}");
7204   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7205                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7206                "    aaaaaaaaaaaaaaa != aa) {\n}");
7207 }
7208 
7209 TEST_F(FormatTest, BreaksAfterAssignments) {
7210   verifyFormat(
7211       "unsigned Cost =\n"
7212       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7213       "                        SI->getPointerAddressSpaceee());\n");
7214   verifyFormat(
7215       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7216       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7217 
7218   verifyFormat(
7219       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7220       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7221   verifyFormat("unsigned OriginalStartColumn =\n"
7222                "    SourceMgr.getSpellingColumnNumber(\n"
7223                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7224                "    1;");
7225 }
7226 
7227 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7228   FormatStyle Style = getLLVMStyle();
7229   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7230                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7231                Style);
7232 
7233   Style.PenaltyBreakAssignment = 20;
7234   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7235                "                                 cccccccccccccccccccccccccc;",
7236                Style);
7237 }
7238 
7239 TEST_F(FormatTest, AlignsAfterAssignments) {
7240   verifyFormat(
7241       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7242       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7243   verifyFormat(
7244       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7245       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7246   verifyFormat(
7247       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7248       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7249   verifyFormat(
7250       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7251       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7252   verifyFormat(
7253       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7254       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7255       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7256 }
7257 
7258 TEST_F(FormatTest, AlignsAfterReturn) {
7259   verifyFormat(
7260       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7261       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7262   verifyFormat(
7263       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7264       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7265   verifyFormat(
7266       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7267       "       aaaaaaaaaaaaaaaaaaaaaa();");
7268   verifyFormat(
7269       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7270       "        aaaaaaaaaaaaaaaaaaaaaa());");
7271   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7272                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7273   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7274                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7275                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7276   verifyFormat("return\n"
7277                "    // true if code is one of a or b.\n"
7278                "    code == a || code == b;");
7279 }
7280 
7281 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7282   verifyFormat(
7283       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7284       "                                                aaaaaaaaa aaaaaaa) {}");
7285   verifyFormat(
7286       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7287       "                                               aaaaaaaaaaa aaaaaaaaa);");
7288   verifyFormat(
7289       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7290       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7291   FormatStyle Style = getLLVMStyle();
7292   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7293   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7294                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7295                Style);
7296   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7297                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7298                Style);
7299   verifyFormat("SomeLongVariableName->someFunction(\n"
7300                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7301                Style);
7302   verifyFormat(
7303       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7304       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7305       Style);
7306   verifyFormat(
7307       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7308       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7309       Style);
7310   verifyFormat(
7311       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7312       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7313       Style);
7314 
7315   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7316                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7317                "        b));",
7318                Style);
7319 
7320   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7321   Style.BinPackArguments = false;
7322   Style.BinPackParameters = false;
7323   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7324                "    aaaaaaaaaaa aaaaaaaa,\n"
7325                "    aaaaaaaaa aaaaaaa,\n"
7326                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7327                Style);
7328   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7329                "    aaaaaaaaaaa aaaaaaaaa,\n"
7330                "    aaaaaaaaaaa aaaaaaaaa,\n"
7331                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7332                Style);
7333   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7334                "    aaaaaaaaaaaaaaa,\n"
7335                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7336                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7337                Style);
7338   verifyFormat(
7339       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7340       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7341       Style);
7342   verifyFormat(
7343       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7344       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7345       Style);
7346   verifyFormat(
7347       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7348       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7349       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7350       "    aaaaaaaaaaaaaaaa);",
7351       Style);
7352   verifyFormat(
7353       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7354       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7355       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7356       "    aaaaaaaaaaaaaaaa);",
7357       Style);
7358 }
7359 
7360 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7361   FormatStyle Style = getLLVMStyleWithColumns(40);
7362   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7363                "          bbbbbbbbbbbbbbbbbbbbbb);",
7364                Style);
7365   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7366   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7367   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7368                "          bbbbbbbbbbbbbbbbbbbbbb);",
7369                Style);
7370   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7371   Style.AlignOperands = FormatStyle::OAS_Align;
7372   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7373                "          bbbbbbbbbbbbbbbbbbbbbb);",
7374                Style);
7375   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7376   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7377   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7378                "    bbbbbbbbbbbbbbbbbbbbbb);",
7379                Style);
7380 }
7381 
7382 TEST_F(FormatTest, BreaksConditionalExpressions) {
7383   verifyFormat(
7384       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7385       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7386       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7387   verifyFormat(
7388       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7389       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7390       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7391   verifyFormat(
7392       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7393       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7394   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7395                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7396                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7397   verifyFormat(
7398       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7399       "                                                    : aaaaaaaaaaaaa);");
7400   verifyFormat(
7401       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7402       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7403       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7404       "                   aaaaaaaaaaaaa);");
7405   verifyFormat(
7406       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7407       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7408       "                   aaaaaaaaaaaaa);");
7409   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7410                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7411                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7412                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7413                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7414   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7415                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7416                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7417                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7418                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7419                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7420                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7421   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7422                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7423                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7424                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7425                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7426   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7427                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7428                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7429   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7430                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7431                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7432                "        : aaaaaaaaaaaaaaaa;");
7433   verifyFormat(
7434       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7435       "    ? aaaaaaaaaaaaaaa\n"
7436       "    : aaaaaaaaaaaaaaa;");
7437   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7438                "          aaaaaaaaa\n"
7439                "      ? b\n"
7440                "      : c);");
7441   verifyFormat("return aaaa == bbbb\n"
7442                "           // comment\n"
7443                "           ? aaaa\n"
7444                "           : bbbb;");
7445   verifyFormat("unsigned Indent =\n"
7446                "    format(TheLine.First,\n"
7447                "           IndentForLevel[TheLine.Level] >= 0\n"
7448                "               ? IndentForLevel[TheLine.Level]\n"
7449                "               : TheLine * 2,\n"
7450                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7451                getLLVMStyleWithColumns(60));
7452   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7453                "                  ? aaaaaaaaaaaaaaa\n"
7454                "                  : bbbbbbbbbbbbbbb //\n"
7455                "                        ? ccccccccccccccc\n"
7456                "                        : ddddddddddddddd;");
7457   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7458                "                  ? aaaaaaaaaaaaaaa\n"
7459                "                  : (bbbbbbbbbbbbbbb //\n"
7460                "                         ? ccccccccccccccc\n"
7461                "                         : ddddddddddddddd);");
7462   verifyFormat(
7463       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7464       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7465       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7466       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7467       "                                      : aaaaaaaaaa;");
7468   verifyFormat(
7469       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7470       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7471       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7472 
7473   FormatStyle NoBinPacking = getLLVMStyle();
7474   NoBinPacking.BinPackArguments = false;
7475   verifyFormat(
7476       "void f() {\n"
7477       "  g(aaa,\n"
7478       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7479       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7480       "        ? aaaaaaaaaaaaaaa\n"
7481       "        : aaaaaaaaaaaaaaa);\n"
7482       "}",
7483       NoBinPacking);
7484   verifyFormat(
7485       "void f() {\n"
7486       "  g(aaa,\n"
7487       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7488       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7489       "        ?: aaaaaaaaaaaaaaa);\n"
7490       "}",
7491       NoBinPacking);
7492 
7493   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7494                "             // comment.\n"
7495                "             ccccccccccccccccccccccccccccccccccccccc\n"
7496                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7497                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7498 
7499   // Assignments in conditional expressions. Apparently not uncommon :-(.
7500   verifyFormat("return a != b\n"
7501                "           // comment\n"
7502                "           ? a = b\n"
7503                "           : a = b;");
7504   verifyFormat("return a != b\n"
7505                "           // comment\n"
7506                "           ? a = a != b\n"
7507                "                     // comment\n"
7508                "                     ? a = b\n"
7509                "                     : a\n"
7510                "           : a;\n");
7511   verifyFormat("return a != b\n"
7512                "           // comment\n"
7513                "           ? a\n"
7514                "           : a = a != b\n"
7515                "                     // comment\n"
7516                "                     ? a = b\n"
7517                "                     : a;");
7518 
7519   // Chained conditionals
7520   FormatStyle Style = getLLVMStyle();
7521   Style.ColumnLimit = 70;
7522   Style.AlignOperands = FormatStyle::OAS_Align;
7523   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7524                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7525                "                        : 3333333333333333;",
7526                Style);
7527   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7528                "       : bbbbbbbbbb     ? 2222222222222222\n"
7529                "                        : 3333333333333333;",
7530                Style);
7531   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7532                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7533                "                          : 3333333333333333;",
7534                Style);
7535   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7536                "       : bbbbbbbbbbbbbb ? 222222\n"
7537                "                        : 333333;",
7538                Style);
7539   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7540                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7541                "       : cccccccccccccc ? 3333333333333333\n"
7542                "                        : 4444444444444444;",
7543                Style);
7544   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7545                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7546                "                        : 3333333333333333;",
7547                Style);
7548   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7549                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7550                "                        : (aaa ? bbb : ccc);",
7551                Style);
7552   verifyFormat(
7553       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7554       "                                             : cccccccccccccccccc)\n"
7555       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7556       "                        : 3333333333333333;",
7557       Style);
7558   verifyFormat(
7559       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7560       "                                             : cccccccccccccccccc)\n"
7561       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7562       "                        : 3333333333333333;",
7563       Style);
7564   verifyFormat(
7565       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7566       "                                             : dddddddddddddddddd)\n"
7567       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7568       "                        : 3333333333333333;",
7569       Style);
7570   verifyFormat(
7571       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7572       "                                             : dddddddddddddddddd)\n"
7573       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7574       "                        : 3333333333333333;",
7575       Style);
7576   verifyFormat(
7577       "return aaaaaaaaa        ? 1111111111111111\n"
7578       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7579       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7580       "                                             : dddddddddddddddddd)\n",
7581       Style);
7582   verifyFormat(
7583       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7584       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7585       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7586       "                                             : cccccccccccccccccc);",
7587       Style);
7588   verifyFormat(
7589       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7590       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7591       "                                             : eeeeeeeeeeeeeeeeee)\n"
7592       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7593       "                        : 3333333333333333;",
7594       Style);
7595   verifyFormat(
7596       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
7597       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7598       "                                             : eeeeeeeeeeeeeeeeee)\n"
7599       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7600       "                        : 3333333333333333;",
7601       Style);
7602   verifyFormat(
7603       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7604       "                           : cccccccccccc    ? dddddddddddddddddd\n"
7605       "                                             : eeeeeeeeeeeeeeeeee)\n"
7606       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7607       "                        : 3333333333333333;",
7608       Style);
7609   verifyFormat(
7610       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7611       "                                             : cccccccccccccccccc\n"
7612       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7613       "                        : 3333333333333333;",
7614       Style);
7615   verifyFormat(
7616       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7617       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
7618       "                                             : eeeeeeeeeeeeeeeeee\n"
7619       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7620       "                        : 3333333333333333;",
7621       Style);
7622   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
7623                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
7624                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
7625                "                                   : eeeeeeeeeeeeeeeeee)\n"
7626                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7627                "                             : 3333333333333333;",
7628                Style);
7629   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7630                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7631                "             : cccccccccccccccc ? dddddddddddddddddd\n"
7632                "                                : eeeeeeeeeeeeeeeeee\n"
7633                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7634                "                                 : 3333333333333333;",
7635                Style);
7636 
7637   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7638   Style.BreakBeforeTernaryOperators = false;
7639   // FIXME: Aligning the question marks is weird given DontAlign.
7640   // Consider disabling this alignment in this case. Also check whether this
7641   // will render the adjustment from https://reviews.llvm.org/D82199
7642   // unnecessary.
7643   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
7644                "    bbbb                ? cccccccccccccccccc :\n"
7645                "                          ddddd;\n",
7646                Style);
7647 
7648   EXPECT_EQ(
7649       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7650       "    /*\n"
7651       "     */\n"
7652       "    function() {\n"
7653       "      try {\n"
7654       "        return JJJJJJJJJJJJJJ(\n"
7655       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7656       "      }\n"
7657       "    } :\n"
7658       "    function() {};",
7659       format(
7660           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7661           "     /*\n"
7662           "      */\n"
7663           "     function() {\n"
7664           "      try {\n"
7665           "        return JJJJJJJJJJJJJJ(\n"
7666           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7667           "      }\n"
7668           "    } :\n"
7669           "    function() {};",
7670           getGoogleStyle(FormatStyle::LK_JavaScript)));
7671 }
7672 
7673 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
7674   FormatStyle Style = getLLVMStyle();
7675   Style.BreakBeforeTernaryOperators = false;
7676   Style.ColumnLimit = 70;
7677   verifyFormat(
7678       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7679       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7680       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7681       Style);
7682   verifyFormat(
7683       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7684       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7685       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7686       Style);
7687   verifyFormat(
7688       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7689       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7690       Style);
7691   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
7692                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7693                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7694                Style);
7695   verifyFormat(
7696       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
7697       "                                                      aaaaaaaaaaaaa);",
7698       Style);
7699   verifyFormat(
7700       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7701       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7702       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7703       "                   aaaaaaaaaaaaa);",
7704       Style);
7705   verifyFormat(
7706       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7707       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7708       "                   aaaaaaaaaaaaa);",
7709       Style);
7710   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7711                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7712                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7713                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7714                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7715                Style);
7716   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7717                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7718                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7719                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7720                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7721                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7722                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7723                Style);
7724   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7725                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
7726                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7727                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7728                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7729                Style);
7730   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7731                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7732                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7733                Style);
7734   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7735                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7736                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7737                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7738                Style);
7739   verifyFormat(
7740       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7741       "    aaaaaaaaaaaaaaa :\n"
7742       "    aaaaaaaaaaaaaaa;",
7743       Style);
7744   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7745                "          aaaaaaaaa ?\n"
7746                "      b :\n"
7747                "      c);",
7748                Style);
7749   verifyFormat("unsigned Indent =\n"
7750                "    format(TheLine.First,\n"
7751                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
7752                "               IndentForLevel[TheLine.Level] :\n"
7753                "               TheLine * 2,\n"
7754                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7755                Style);
7756   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7757                "                  aaaaaaaaaaaaaaa :\n"
7758                "                  bbbbbbbbbbbbbbb ? //\n"
7759                "                      ccccccccccccccc :\n"
7760                "                      ddddddddddddddd;",
7761                Style);
7762   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7763                "                  aaaaaaaaaaaaaaa :\n"
7764                "                  (bbbbbbbbbbbbbbb ? //\n"
7765                "                       ccccccccccccccc :\n"
7766                "                       ddddddddddddddd);",
7767                Style);
7768   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7769                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
7770                "            ccccccccccccccccccccccccccc;",
7771                Style);
7772   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7773                "           aaaaa :\n"
7774                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
7775                Style);
7776 
7777   // Chained conditionals
7778   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7779                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7780                "                          3333333333333333;",
7781                Style);
7782   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7783                "       bbbbbbbbbb       ? 2222222222222222 :\n"
7784                "                          3333333333333333;",
7785                Style);
7786   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
7787                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7788                "                          3333333333333333;",
7789                Style);
7790   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7791                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
7792                "                          333333;",
7793                Style);
7794   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7795                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7796                "       cccccccccccccccc ? 3333333333333333 :\n"
7797                "                          4444444444444444;",
7798                Style);
7799   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
7800                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7801                "                          3333333333333333;",
7802                Style);
7803   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7804                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7805                "                          (aaa ? bbb : ccc);",
7806                Style);
7807   verifyFormat(
7808       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7809       "                                               cccccccccccccccccc) :\n"
7810       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7811       "                          3333333333333333;",
7812       Style);
7813   verifyFormat(
7814       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7815       "                                               cccccccccccccccccc) :\n"
7816       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7817       "                          3333333333333333;",
7818       Style);
7819   verifyFormat(
7820       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7821       "                                               dddddddddddddddddd) :\n"
7822       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7823       "                          3333333333333333;",
7824       Style);
7825   verifyFormat(
7826       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7827       "                                               dddddddddddddddddd) :\n"
7828       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7829       "                          3333333333333333;",
7830       Style);
7831   verifyFormat(
7832       "return aaaaaaaaa        ? 1111111111111111 :\n"
7833       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7834       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7835       "                                               dddddddddddddddddd)\n",
7836       Style);
7837   verifyFormat(
7838       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7839       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7840       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7841       "                                               cccccccccccccccccc);",
7842       Style);
7843   verifyFormat(
7844       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7845       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7846       "                                               eeeeeeeeeeeeeeeeee) :\n"
7847       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7848       "                          3333333333333333;",
7849       Style);
7850   verifyFormat(
7851       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7852       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
7853       "                                               eeeeeeeeeeeeeeeeee) :\n"
7854       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7855       "                          3333333333333333;",
7856       Style);
7857   verifyFormat(
7858       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
7859       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7860       "                                               eeeeeeeeeeeeeeeeee) :\n"
7861       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7862       "                          3333333333333333;",
7863       Style);
7864   verifyFormat(
7865       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7866       "                                               cccccccccccccccccc :\n"
7867       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7868       "                          3333333333333333;",
7869       Style);
7870   verifyFormat(
7871       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7872       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
7873       "                                               eeeeeeeeeeeeeeeeee :\n"
7874       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7875       "                          3333333333333333;",
7876       Style);
7877   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7878                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7879                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
7880                "                                 eeeeeeeeeeeeeeeeee) :\n"
7881                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7882                "                               3333333333333333;",
7883                Style);
7884   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7885                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7886                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
7887                "                                  eeeeeeeeeeeeeeeeee :\n"
7888                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7889                "                               3333333333333333;",
7890                Style);
7891 }
7892 
7893 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
7894   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
7895                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
7896   verifyFormat("bool a = true, b = false;");
7897 
7898   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7899                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
7900                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
7901                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
7902   verifyFormat(
7903       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7904       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
7905       "     d = e && f;");
7906   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
7907                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
7908   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7909                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
7910   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
7911                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
7912 
7913   FormatStyle Style = getGoogleStyle();
7914   Style.PointerAlignment = FormatStyle::PAS_Left;
7915   Style.DerivePointerAlignment = false;
7916   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7917                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
7918                "    *b = bbbbbbbbbbbbbbbbbbb;",
7919                Style);
7920   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7921                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
7922                Style);
7923   verifyFormat("vector<int*> a, b;", Style);
7924   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
7925 }
7926 
7927 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
7928   verifyFormat("arr[foo ? bar : baz];");
7929   verifyFormat("f()[foo ? bar : baz];");
7930   verifyFormat("(a + b)[foo ? bar : baz];");
7931   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
7932 }
7933 
7934 TEST_F(FormatTest, AlignsStringLiterals) {
7935   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
7936                "                                      \"short literal\");");
7937   verifyFormat(
7938       "looooooooooooooooooooooooongFunction(\n"
7939       "    \"short literal\"\n"
7940       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
7941   verifyFormat("someFunction(\"Always break between multi-line\"\n"
7942                "             \" string literals\",\n"
7943                "             and, other, parameters);");
7944   EXPECT_EQ("fun + \"1243\" /* comment */\n"
7945             "      \"5678\";",
7946             format("fun + \"1243\" /* comment */\n"
7947                    "    \"5678\";",
7948                    getLLVMStyleWithColumns(28)));
7949   EXPECT_EQ(
7950       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7951       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
7952       "         \"aaaaaaaaaaaaaaaa\";",
7953       format("aaaaaa ="
7954              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7955              "aaaaaaaaaaaaaaaaaaaaa\" "
7956              "\"aaaaaaaaaaaaaaaa\";"));
7957   verifyFormat("a = a + \"a\"\n"
7958                "        \"a\"\n"
7959                "        \"a\";");
7960   verifyFormat("f(\"a\", \"b\"\n"
7961                "       \"c\");");
7962 
7963   verifyFormat(
7964       "#define LL_FORMAT \"ll\"\n"
7965       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
7966       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
7967 
7968   verifyFormat("#define A(X)          \\\n"
7969                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
7970                "  \"ccccc\"",
7971                getLLVMStyleWithColumns(23));
7972   verifyFormat("#define A \"def\"\n"
7973                "f(\"abc\" A \"ghi\"\n"
7974                "  \"jkl\");");
7975 
7976   verifyFormat("f(L\"a\"\n"
7977                "  L\"b\");");
7978   verifyFormat("#define A(X)            \\\n"
7979                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
7980                "  L\"ccccc\"",
7981                getLLVMStyleWithColumns(25));
7982 
7983   verifyFormat("f(@\"a\"\n"
7984                "  @\"b\");");
7985   verifyFormat("NSString s = @\"a\"\n"
7986                "             @\"b\"\n"
7987                "             @\"c\";");
7988   verifyFormat("NSString s = @\"a\"\n"
7989                "              \"b\"\n"
7990                "              \"c\";");
7991 }
7992 
7993 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
7994   FormatStyle Style = getLLVMStyle();
7995   // No declarations or definitions should be moved to own line.
7996   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
7997   verifyFormat("class A {\n"
7998                "  int f() { return 1; }\n"
7999                "  int g();\n"
8000                "};\n"
8001                "int f() { return 1; }\n"
8002                "int g();\n",
8003                Style);
8004 
8005   // All declarations and definitions should have the return type moved to its
8006   // own line.
8007   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8008   Style.TypenameMacros = {"LIST"};
8009   verifyFormat("SomeType\n"
8010                "funcdecl(LIST(uint64_t));",
8011                Style);
8012   verifyFormat("class E {\n"
8013                "  int\n"
8014                "  f() {\n"
8015                "    return 1;\n"
8016                "  }\n"
8017                "  int\n"
8018                "  g();\n"
8019                "};\n"
8020                "int\n"
8021                "f() {\n"
8022                "  return 1;\n"
8023                "}\n"
8024                "int\n"
8025                "g();\n",
8026                Style);
8027 
8028   // Top-level definitions, and no kinds of declarations should have the
8029   // return type moved to its own line.
8030   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8031   verifyFormat("class B {\n"
8032                "  int f() { return 1; }\n"
8033                "  int g();\n"
8034                "};\n"
8035                "int\n"
8036                "f() {\n"
8037                "  return 1;\n"
8038                "}\n"
8039                "int g();\n",
8040                Style);
8041 
8042   // Top-level definitions and declarations should have the return type moved
8043   // to its own line.
8044   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8045   verifyFormat("class C {\n"
8046                "  int f() { return 1; }\n"
8047                "  int g();\n"
8048                "};\n"
8049                "int\n"
8050                "f() {\n"
8051                "  return 1;\n"
8052                "}\n"
8053                "int\n"
8054                "g();\n",
8055                Style);
8056 
8057   // All definitions should have the return type moved to its own line, but no
8058   // kinds of declarations.
8059   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8060   verifyFormat("class D {\n"
8061                "  int\n"
8062                "  f() {\n"
8063                "    return 1;\n"
8064                "  }\n"
8065                "  int g();\n"
8066                "};\n"
8067                "int\n"
8068                "f() {\n"
8069                "  return 1;\n"
8070                "}\n"
8071                "int g();\n",
8072                Style);
8073   verifyFormat("const char *\n"
8074                "f(void) {\n" // Break here.
8075                "  return \"\";\n"
8076                "}\n"
8077                "const char *bar(void);\n", // No break here.
8078                Style);
8079   verifyFormat("template <class T>\n"
8080                "T *\n"
8081                "f(T &c) {\n" // Break here.
8082                "  return NULL;\n"
8083                "}\n"
8084                "template <class T> T *f(T &c);\n", // No break here.
8085                Style);
8086   verifyFormat("class C {\n"
8087                "  int\n"
8088                "  operator+() {\n"
8089                "    return 1;\n"
8090                "  }\n"
8091                "  int\n"
8092                "  operator()() {\n"
8093                "    return 1;\n"
8094                "  }\n"
8095                "};\n",
8096                Style);
8097   verifyFormat("void\n"
8098                "A::operator()() {}\n"
8099                "void\n"
8100                "A::operator>>() {}\n"
8101                "void\n"
8102                "A::operator+() {}\n"
8103                "void\n"
8104                "A::operator*() {}\n"
8105                "void\n"
8106                "A::operator->() {}\n"
8107                "void\n"
8108                "A::operator void *() {}\n"
8109                "void\n"
8110                "A::operator void &() {}\n"
8111                "void\n"
8112                "A::operator void &&() {}\n"
8113                "void\n"
8114                "A::operator char *() {}\n"
8115                "void\n"
8116                "A::operator[]() {}\n"
8117                "void\n"
8118                "A::operator!() {}\n"
8119                "void\n"
8120                "A::operator**() {}\n"
8121                "void\n"
8122                "A::operator<Foo> *() {}\n"
8123                "void\n"
8124                "A::operator<Foo> **() {}\n"
8125                "void\n"
8126                "A::operator<Foo> &() {}\n"
8127                "void\n"
8128                "A::operator void **() {}\n",
8129                Style);
8130   verifyFormat("constexpr auto\n"
8131                "operator()() const -> reference {}\n"
8132                "constexpr auto\n"
8133                "operator>>() const -> reference {}\n"
8134                "constexpr auto\n"
8135                "operator+() const -> reference {}\n"
8136                "constexpr auto\n"
8137                "operator*() const -> reference {}\n"
8138                "constexpr auto\n"
8139                "operator->() const -> reference {}\n"
8140                "constexpr auto\n"
8141                "operator++() const -> reference {}\n"
8142                "constexpr auto\n"
8143                "operator void *() const -> reference {}\n"
8144                "constexpr auto\n"
8145                "operator void **() const -> reference {}\n"
8146                "constexpr auto\n"
8147                "operator void *() const -> reference {}\n"
8148                "constexpr auto\n"
8149                "operator void &() const -> reference {}\n"
8150                "constexpr auto\n"
8151                "operator void &&() const -> reference {}\n"
8152                "constexpr auto\n"
8153                "operator char *() const -> reference {}\n"
8154                "constexpr auto\n"
8155                "operator!() const -> reference {}\n"
8156                "constexpr auto\n"
8157                "operator[]() const -> reference {}\n",
8158                Style);
8159   verifyFormat("void *operator new(std::size_t s);", // No break here.
8160                Style);
8161   verifyFormat("void *\n"
8162                "operator new(std::size_t s) {}",
8163                Style);
8164   verifyFormat("void *\n"
8165                "operator delete[](void *ptr) {}",
8166                Style);
8167   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8168   verifyFormat("const char *\n"
8169                "f(void)\n" // Break here.
8170                "{\n"
8171                "  return \"\";\n"
8172                "}\n"
8173                "const char *bar(void);\n", // No break here.
8174                Style);
8175   verifyFormat("template <class T>\n"
8176                "T *\n"     // Problem here: no line break
8177                "f(T &c)\n" // Break here.
8178                "{\n"
8179                "  return NULL;\n"
8180                "}\n"
8181                "template <class T> T *f(T &c);\n", // No break here.
8182                Style);
8183   verifyFormat("int\n"
8184                "foo(A<bool> a)\n"
8185                "{\n"
8186                "  return a;\n"
8187                "}\n",
8188                Style);
8189   verifyFormat("int\n"
8190                "foo(A<8> a)\n"
8191                "{\n"
8192                "  return a;\n"
8193                "}\n",
8194                Style);
8195   verifyFormat("int\n"
8196                "foo(A<B<bool>, 8> a)\n"
8197                "{\n"
8198                "  return a;\n"
8199                "}\n",
8200                Style);
8201   verifyFormat("int\n"
8202                "foo(A<B<8>, bool> a)\n"
8203                "{\n"
8204                "  return a;\n"
8205                "}\n",
8206                Style);
8207   verifyFormat("int\n"
8208                "foo(A<B<bool>, bool> a)\n"
8209                "{\n"
8210                "  return a;\n"
8211                "}\n",
8212                Style);
8213   verifyFormat("int\n"
8214                "foo(A<B<8>, 8> a)\n"
8215                "{\n"
8216                "  return a;\n"
8217                "}\n",
8218                Style);
8219 
8220   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8221   Style.BraceWrapping.AfterFunction = true;
8222   verifyFormat("int f(i);\n" // No break here.
8223                "int\n"       // Break here.
8224                "f(i)\n"
8225                "{\n"
8226                "  return i + 1;\n"
8227                "}\n"
8228                "int\n" // Break here.
8229                "f(i)\n"
8230                "{\n"
8231                "  return i + 1;\n"
8232                "};",
8233                Style);
8234   verifyFormat("int f(a, b, c);\n" // No break here.
8235                "int\n"             // Break here.
8236                "f(a, b, c)\n"      // Break here.
8237                "short a, b;\n"
8238                "float c;\n"
8239                "{\n"
8240                "  return a + b < c;\n"
8241                "}\n"
8242                "int\n"        // Break here.
8243                "f(a, b, c)\n" // Break here.
8244                "short a, b;\n"
8245                "float c;\n"
8246                "{\n"
8247                "  return a + b < c;\n"
8248                "};",
8249                Style);
8250   verifyFormat("byte *\n" // Break here.
8251                "f(a)\n"   // Break here.
8252                "byte a[];\n"
8253                "{\n"
8254                "  return a;\n"
8255                "}",
8256                Style);
8257   verifyFormat("bool f(int a, int) override;\n"
8258                "Bar g(int a, Bar) final;\n"
8259                "Bar h(a, Bar) final;",
8260                Style);
8261   verifyFormat("int\n"
8262                "f(a)",
8263                Style);
8264 
8265   // The return breaking style doesn't affect:
8266   // * function and object definitions with attribute-like macros
8267   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8268                "    ABSL_GUARDED_BY(mutex) = {};",
8269                getGoogleStyleWithColumns(40));
8270   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8271                "    ABSL_GUARDED_BY(mutex);  // comment",
8272                getGoogleStyleWithColumns(40));
8273   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8274                "    ABSL_GUARDED_BY(mutex1)\n"
8275                "        ABSL_GUARDED_BY(mutex2);",
8276                getGoogleStyleWithColumns(40));
8277   verifyFormat("Tttttt f(int a, int b)\n"
8278                "    ABSL_GUARDED_BY(mutex1)\n"
8279                "        ABSL_GUARDED_BY(mutex2);",
8280                getGoogleStyleWithColumns(40));
8281   // * typedefs
8282   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8283 
8284   Style = getGNUStyle();
8285 
8286   // Test for comments at the end of function declarations.
8287   verifyFormat("void\n"
8288                "foo (int a, /*abc*/ int b) // def\n"
8289                "{\n"
8290                "}\n",
8291                Style);
8292 
8293   verifyFormat("void\n"
8294                "foo (int a, /* abc */ int b) /* def */\n"
8295                "{\n"
8296                "}\n",
8297                Style);
8298 
8299   // Definitions that should not break after return type
8300   verifyFormat("void foo (int a, int b); // def\n", Style);
8301   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8302   verifyFormat("void foo (int a, int b);\n", Style);
8303 }
8304 
8305 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8306   FormatStyle NoBreak = getLLVMStyle();
8307   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8308   FormatStyle Break = getLLVMStyle();
8309   Break.AlwaysBreakBeforeMultilineStrings = true;
8310   verifyFormat("aaaa = \"bbbb\"\n"
8311                "       \"cccc\";",
8312                NoBreak);
8313   verifyFormat("aaaa =\n"
8314                "    \"bbbb\"\n"
8315                "    \"cccc\";",
8316                Break);
8317   verifyFormat("aaaa(\"bbbb\"\n"
8318                "     \"cccc\");",
8319                NoBreak);
8320   verifyFormat("aaaa(\n"
8321                "    \"bbbb\"\n"
8322                "    \"cccc\");",
8323                Break);
8324   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8325                "          \"cccc\");",
8326                NoBreak);
8327   verifyFormat("aaaa(qqq,\n"
8328                "     \"bbbb\"\n"
8329                "     \"cccc\");",
8330                Break);
8331   verifyFormat("aaaa(qqq,\n"
8332                "     L\"bbbb\"\n"
8333                "     L\"cccc\");",
8334                Break);
8335   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8336                "                      \"bbbb\"));",
8337                Break);
8338   verifyFormat("string s = someFunction(\n"
8339                "    \"abc\"\n"
8340                "    \"abc\");",
8341                Break);
8342 
8343   // As we break before unary operators, breaking right after them is bad.
8344   verifyFormat("string foo = abc ? \"x\"\n"
8345                "                   \"blah blah blah blah blah blah\"\n"
8346                "                 : \"y\";",
8347                Break);
8348 
8349   // Don't break if there is no column gain.
8350   verifyFormat("f(\"aaaa\"\n"
8351                "  \"bbbb\");",
8352                Break);
8353 
8354   // Treat literals with escaped newlines like multi-line string literals.
8355   EXPECT_EQ("x = \"a\\\n"
8356             "b\\\n"
8357             "c\";",
8358             format("x = \"a\\\n"
8359                    "b\\\n"
8360                    "c\";",
8361                    NoBreak));
8362   EXPECT_EQ("xxxx =\n"
8363             "    \"a\\\n"
8364             "b\\\n"
8365             "c\";",
8366             format("xxxx = \"a\\\n"
8367                    "b\\\n"
8368                    "c\";",
8369                    Break));
8370 
8371   EXPECT_EQ("NSString *const kString =\n"
8372             "    @\"aaaa\"\n"
8373             "    @\"bbbb\";",
8374             format("NSString *const kString = @\"aaaa\"\n"
8375                    "@\"bbbb\";",
8376                    Break));
8377 
8378   Break.ColumnLimit = 0;
8379   verifyFormat("const char *hello = \"hello llvm\";", Break);
8380 }
8381 
8382 TEST_F(FormatTest, AlignsPipes) {
8383   verifyFormat(
8384       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8385       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8386       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8387   verifyFormat(
8388       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8389       "                     << aaaaaaaaaaaaaaaaaaaa;");
8390   verifyFormat(
8391       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8392       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8393   verifyFormat(
8394       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8395       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8396   verifyFormat(
8397       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8398       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8399       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8400   verifyFormat(
8401       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8402       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8403       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8404   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8405                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8406                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8407                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8408   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8409                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8410   verifyFormat(
8411       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8412       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8413   verifyFormat(
8414       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8415       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8416 
8417   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8418                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8419   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8420                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8421                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8422                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8423   verifyFormat("LOG_IF(aaa == //\n"
8424                "       bbb)\n"
8425                "    << a << b;");
8426 
8427   // But sometimes, breaking before the first "<<" is desirable.
8428   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8429                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8430   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8431                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8432                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8433   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8434                "    << BEF << IsTemplate << Description << E->getType();");
8435   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8436                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8437                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8438   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8439                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8440                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8441                "    << aaa;");
8442 
8443   verifyFormat(
8444       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8445       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8446 
8447   // Incomplete string literal.
8448   EXPECT_EQ("llvm::errs() << \"\n"
8449             "             << a;",
8450             format("llvm::errs() << \"\n<<a;"));
8451 
8452   verifyFormat("void f() {\n"
8453                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8454                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8455                "}");
8456 
8457   // Handle 'endl'.
8458   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8459                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8460   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8461 
8462   // Handle '\n'.
8463   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8464                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8465   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8466                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8467   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8468                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8469   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8470 }
8471 
8472 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8473   verifyFormat("return out << \"somepacket = {\\n\"\n"
8474                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8475                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8476                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8477                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8478                "           << \"}\";");
8479 
8480   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8481                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8482                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8483   verifyFormat(
8484       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8485       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8486       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8487       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8488       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8489   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8490                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8491   verifyFormat(
8492       "void f() {\n"
8493       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8494       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8495       "}");
8496 
8497   // Breaking before the first "<<" is generally not desirable.
8498   verifyFormat(
8499       "llvm::errs()\n"
8500       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8501       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8502       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8503       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8504       getLLVMStyleWithColumns(70));
8505   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8506                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8507                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8508                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8509                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8510                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8511                getLLVMStyleWithColumns(70));
8512 
8513   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8514                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8515                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8516   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8517                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8518                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8519   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8520                "           (aaaa + aaaa);",
8521                getLLVMStyleWithColumns(40));
8522   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8523                "                  (aaaaaaa + aaaaa));",
8524                getLLVMStyleWithColumns(40));
8525   verifyFormat(
8526       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8527       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8528       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8529 }
8530 
8531 TEST_F(FormatTest, UnderstandsEquals) {
8532   verifyFormat(
8533       "aaaaaaaaaaaaaaaaa =\n"
8534       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8535   verifyFormat(
8536       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8537       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8538   verifyFormat(
8539       "if (a) {\n"
8540       "  f();\n"
8541       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8542       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8543       "}");
8544 
8545   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8546                "        100000000 + 10000000) {\n}");
8547 }
8548 
8549 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8550   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8551                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8552 
8553   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8554                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8555 
8556   verifyFormat(
8557       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8558       "                                                          Parameter2);");
8559 
8560   verifyFormat(
8561       "ShortObject->shortFunction(\n"
8562       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8563       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8564 
8565   verifyFormat("loooooooooooooongFunction(\n"
8566                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8567 
8568   verifyFormat(
8569       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8570       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8571 
8572   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8573                "    .WillRepeatedly(Return(SomeValue));");
8574   verifyFormat("void f() {\n"
8575                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8576                "      .Times(2)\n"
8577                "      .WillRepeatedly(Return(SomeValue));\n"
8578                "}");
8579   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
8580                "    ccccccccccccccccccccccc);");
8581   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8582                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8583                "          .aaaaa(aaaaa),\n"
8584                "      aaaaaaaaaaaaaaaaaaaaa);");
8585   verifyFormat("void f() {\n"
8586                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8587                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
8588                "}");
8589   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8590                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8591                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8592                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8593                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8594   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8595                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8596                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8597                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
8598                "}");
8599 
8600   // Here, it is not necessary to wrap at "." or "->".
8601   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
8602                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8603   verifyFormat(
8604       "aaaaaaaaaaa->aaaaaaaaa(\n"
8605       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8606       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
8607 
8608   verifyFormat(
8609       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8610       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
8611   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
8612                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8613   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
8614                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8615 
8616   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8617                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8618                "    .a();");
8619 
8620   FormatStyle NoBinPacking = getLLVMStyle();
8621   NoBinPacking.BinPackParameters = false;
8622   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8623                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8624                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
8625                "                         aaaaaaaaaaaaaaaaaaa,\n"
8626                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8627                NoBinPacking);
8628 
8629   // If there is a subsequent call, change to hanging indentation.
8630   verifyFormat(
8631       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8632       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
8633       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8634   verifyFormat(
8635       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8636       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
8637   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8638                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8639                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8640   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8641                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8642                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8643 }
8644 
8645 TEST_F(FormatTest, WrapsTemplateDeclarations) {
8646   verifyFormat("template <typename T>\n"
8647                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8648   verifyFormat("template <typename T>\n"
8649                "// T should be one of {A, B}.\n"
8650                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8651   verifyFormat(
8652       "template <typename T>\n"
8653       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
8654   verifyFormat("template <typename T>\n"
8655                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
8656                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
8657   verifyFormat(
8658       "template <typename T>\n"
8659       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
8660       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
8661   verifyFormat(
8662       "template <typename T>\n"
8663       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
8664       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
8665       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8666   verifyFormat("template <typename T>\n"
8667                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8668                "    int aaaaaaaaaaaaaaaaaaaaaa);");
8669   verifyFormat(
8670       "template <typename T1, typename T2 = char, typename T3 = char,\n"
8671       "          typename T4 = char>\n"
8672       "void f();");
8673   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
8674                "          template <typename> class cccccccccccccccccccccc,\n"
8675                "          typename ddddddddddddd>\n"
8676                "class C {};");
8677   verifyFormat(
8678       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
8679       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8680 
8681   verifyFormat("void f() {\n"
8682                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
8683                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
8684                "}");
8685 
8686   verifyFormat("template <typename T> class C {};");
8687   verifyFormat("template <typename T> void f();");
8688   verifyFormat("template <typename T> void f() {}");
8689   verifyFormat(
8690       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8691       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8692       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
8693       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8694       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8695       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
8696       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
8697       getLLVMStyleWithColumns(72));
8698   EXPECT_EQ("static_cast<A< //\n"
8699             "    B> *>(\n"
8700             "\n"
8701             ");",
8702             format("static_cast<A<//\n"
8703                    "    B>*>(\n"
8704                    "\n"
8705                    "    );"));
8706   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8707                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
8708 
8709   FormatStyle AlwaysBreak = getLLVMStyle();
8710   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8711   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
8712   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
8713   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
8714   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8715                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8716                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
8717   verifyFormat("template <template <typename> class Fooooooo,\n"
8718                "          template <typename> class Baaaaaaar>\n"
8719                "struct C {};",
8720                AlwaysBreak);
8721   verifyFormat("template <typename T> // T can be A, B or C.\n"
8722                "struct C {};",
8723                AlwaysBreak);
8724   verifyFormat("template <enum E> class A {\n"
8725                "public:\n"
8726                "  E *f();\n"
8727                "};");
8728 
8729   FormatStyle NeverBreak = getLLVMStyle();
8730   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
8731   verifyFormat("template <typename T> class C {};", NeverBreak);
8732   verifyFormat("template <typename T> void f();", NeverBreak);
8733   verifyFormat("template <typename T> void f() {}", NeverBreak);
8734   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8735                "bbbbbbbbbbbbbbbbbbbb) {}",
8736                NeverBreak);
8737   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8738                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8739                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
8740                NeverBreak);
8741   verifyFormat("template <template <typename> class Fooooooo,\n"
8742                "          template <typename> class Baaaaaaar>\n"
8743                "struct C {};",
8744                NeverBreak);
8745   verifyFormat("template <typename T> // T can be A, B or C.\n"
8746                "struct C {};",
8747                NeverBreak);
8748   verifyFormat("template <enum E> class A {\n"
8749                "public:\n"
8750                "  E *f();\n"
8751                "};",
8752                NeverBreak);
8753   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
8754   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8755                "bbbbbbbbbbbbbbbbbbbb) {}",
8756                NeverBreak);
8757 }
8758 
8759 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
8760   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
8761   Style.ColumnLimit = 60;
8762   EXPECT_EQ("// Baseline - no comments.\n"
8763             "template <\n"
8764             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8765             "void f() {}",
8766             format("// Baseline - no comments.\n"
8767                    "template <\n"
8768                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8769                    "void f() {}",
8770                    Style));
8771 
8772   EXPECT_EQ("template <\n"
8773             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8774             "void f() {}",
8775             format("template <\n"
8776                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8777                    "void f() {}",
8778                    Style));
8779 
8780   EXPECT_EQ(
8781       "template <\n"
8782       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
8783       "void f() {}",
8784       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
8785              "void f() {}",
8786              Style));
8787 
8788   EXPECT_EQ(
8789       "template <\n"
8790       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8791       "                                               // multiline\n"
8792       "void f() {}",
8793       format("template <\n"
8794              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8795              "                                              // multiline\n"
8796              "void f() {}",
8797              Style));
8798 
8799   EXPECT_EQ(
8800       "template <typename aaaaaaaaaa<\n"
8801       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
8802       "void f() {}",
8803       format(
8804           "template <\n"
8805           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
8806           "void f() {}",
8807           Style));
8808 }
8809 
8810 TEST_F(FormatTest, WrapsTemplateParameters) {
8811   FormatStyle Style = getLLVMStyle();
8812   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8813   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8814   verifyFormat(
8815       "template <typename... a> struct q {};\n"
8816       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8817       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8818       "    y;",
8819       Style);
8820   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8821   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8822   verifyFormat(
8823       "template <typename... a> struct r {};\n"
8824       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8825       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8826       "    y;",
8827       Style);
8828   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8829   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8830   verifyFormat("template <typename... a> struct s {};\n"
8831                "extern s<\n"
8832                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8833                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8834                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8835                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8836                "    y;",
8837                Style);
8838   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8839   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8840   verifyFormat("template <typename... a> struct t {};\n"
8841                "extern t<\n"
8842                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8843                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8844                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8845                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8846                "    y;",
8847                Style);
8848 }
8849 
8850 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
8851   verifyFormat(
8852       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8853       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8854   verifyFormat(
8855       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8856       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8857       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8858 
8859   // FIXME: Should we have the extra indent after the second break?
8860   verifyFormat(
8861       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8862       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8863       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8864 
8865   verifyFormat(
8866       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
8867       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
8868 
8869   // Breaking at nested name specifiers is generally not desirable.
8870   verifyFormat(
8871       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8872       "    aaaaaaaaaaaaaaaaaaaaaaa);");
8873 
8874   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
8875                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8876                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8877                "                   aaaaaaaaaaaaaaaaaaaaa);",
8878                getLLVMStyleWithColumns(74));
8879 
8880   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8881                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8882                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8883 }
8884 
8885 TEST_F(FormatTest, UnderstandsTemplateParameters) {
8886   verifyFormat("A<int> a;");
8887   verifyFormat("A<A<A<int>>> a;");
8888   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
8889   verifyFormat("bool x = a < 1 || 2 > a;");
8890   verifyFormat("bool x = 5 < f<int>();");
8891   verifyFormat("bool x = f<int>() > 5;");
8892   verifyFormat("bool x = 5 < a<int>::x;");
8893   verifyFormat("bool x = a < 4 ? a > 2 : false;");
8894   verifyFormat("bool x = f() ? a < 2 : a > 2;");
8895 
8896   verifyGoogleFormat("A<A<int>> a;");
8897   verifyGoogleFormat("A<A<A<int>>> a;");
8898   verifyGoogleFormat("A<A<A<A<int>>>> a;");
8899   verifyGoogleFormat("A<A<int> > a;");
8900   verifyGoogleFormat("A<A<A<int> > > a;");
8901   verifyGoogleFormat("A<A<A<A<int> > > > a;");
8902   verifyGoogleFormat("A<::A<int>> a;");
8903   verifyGoogleFormat("A<::A> a;");
8904   verifyGoogleFormat("A< ::A> a;");
8905   verifyGoogleFormat("A< ::A<int> > a;");
8906   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
8907   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
8908   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
8909   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
8910   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
8911             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
8912 
8913   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
8914 
8915   // template closer followed by a token that starts with > or =
8916   verifyFormat("bool b = a<1> > 1;");
8917   verifyFormat("bool b = a<1> >= 1;");
8918   verifyFormat("int i = a<1> >> 1;");
8919   FormatStyle Style = getLLVMStyle();
8920   Style.SpaceBeforeAssignmentOperators = false;
8921   verifyFormat("bool b= a<1> == 1;", Style);
8922   verifyFormat("a<int> = 1;", Style);
8923   verifyFormat("a<int> >>= 1;", Style);
8924 
8925   verifyFormat("test < a | b >> c;");
8926   verifyFormat("test<test<a | b>> c;");
8927   verifyFormat("test >> a >> b;");
8928   verifyFormat("test << a >> b;");
8929 
8930   verifyFormat("f<int>();");
8931   verifyFormat("template <typename T> void f() {}");
8932   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
8933   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
8934                "sizeof(char)>::type>;");
8935   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
8936   verifyFormat("f(a.operator()<A>());");
8937   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8938                "      .template operator()<A>());",
8939                getLLVMStyleWithColumns(35));
8940 
8941   // Not template parameters.
8942   verifyFormat("return a < b && c > d;");
8943   verifyFormat("void f() {\n"
8944                "  while (a < b && c > d) {\n"
8945                "  }\n"
8946                "}");
8947   verifyFormat("template <typename... Types>\n"
8948                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
8949 
8950   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8951                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
8952                getLLVMStyleWithColumns(60));
8953   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
8954   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
8955   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
8956   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
8957 }
8958 
8959 TEST_F(FormatTest, UnderstandsShiftOperators) {
8960   verifyFormat("if (i < x >> 1)");
8961   verifyFormat("while (i < x >> 1)");
8962   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
8963   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
8964   verifyFormat(
8965       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
8966   verifyFormat("Foo.call<Bar<Function>>()");
8967   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
8968   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
8969                "++i, v = v >> 1)");
8970   verifyFormat("if (w<u<v<x>>, 1>::t)");
8971 }
8972 
8973 TEST_F(FormatTest, BitshiftOperatorWidth) {
8974   EXPECT_EQ("int a = 1 << 2; /* foo\n"
8975             "                   bar */",
8976             format("int    a=1<<2;  /* foo\n"
8977                    "                   bar */"));
8978 
8979   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
8980             "                     bar */",
8981             format("int  b  =256>>1 ;  /* foo\n"
8982                    "                      bar */"));
8983 }
8984 
8985 TEST_F(FormatTest, UnderstandsBinaryOperators) {
8986   verifyFormat("COMPARE(a, ==, b);");
8987   verifyFormat("auto s = sizeof...(Ts) - 1;");
8988 }
8989 
8990 TEST_F(FormatTest, UnderstandsPointersToMembers) {
8991   verifyFormat("int A::*x;");
8992   verifyFormat("int (S::*func)(void *);");
8993   verifyFormat("void f() { int (S::*func)(void *); }");
8994   verifyFormat("typedef bool *(Class::*Member)() const;");
8995   verifyFormat("void f() {\n"
8996                "  (a->*f)();\n"
8997                "  a->*x;\n"
8998                "  (a.*f)();\n"
8999                "  ((*a).*f)();\n"
9000                "  a.*x;\n"
9001                "}");
9002   verifyFormat("void f() {\n"
9003                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9004                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9005                "}");
9006   verifyFormat(
9007       "(aaaaaaaaaa->*bbbbbbb)(\n"
9008       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9009   FormatStyle Style = getLLVMStyle();
9010   Style.PointerAlignment = FormatStyle::PAS_Left;
9011   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9012 }
9013 
9014 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9015   verifyFormat("int a = -2;");
9016   verifyFormat("f(-1, -2, -3);");
9017   verifyFormat("a[-1] = 5;");
9018   verifyFormat("int a = 5 + -2;");
9019   verifyFormat("if (i == -1) {\n}");
9020   verifyFormat("if (i != -1) {\n}");
9021   verifyFormat("if (i > -1) {\n}");
9022   verifyFormat("if (i < -1) {\n}");
9023   verifyFormat("++(a->f());");
9024   verifyFormat("--(a->f());");
9025   verifyFormat("(a->f())++;");
9026   verifyFormat("a[42]++;");
9027   verifyFormat("if (!(a->f())) {\n}");
9028   verifyFormat("if (!+i) {\n}");
9029   verifyFormat("~&a;");
9030 
9031   verifyFormat("a-- > b;");
9032   verifyFormat("b ? -a : c;");
9033   verifyFormat("n * sizeof char16;");
9034   verifyFormat("n * alignof char16;", getGoogleStyle());
9035   verifyFormat("sizeof(char);");
9036   verifyFormat("alignof(char);", getGoogleStyle());
9037 
9038   verifyFormat("return -1;");
9039   verifyFormat("throw -1;");
9040   verifyFormat("switch (a) {\n"
9041                "case -1:\n"
9042                "  break;\n"
9043                "}");
9044   verifyFormat("#define X -1");
9045   verifyFormat("#define X -kConstant");
9046 
9047   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9048   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9049 
9050   verifyFormat("int a = /* confusing comment */ -1;");
9051   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9052   verifyFormat("int a = i /* confusing comment */++;");
9053 
9054   verifyFormat("co_yield -1;");
9055   verifyFormat("co_return -1;");
9056 
9057   // Check that * is not treated as a binary operator when we set
9058   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9059   FormatStyle PASLeftStyle = getLLVMStyle();
9060   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9061   verifyFormat("co_return *a;", PASLeftStyle);
9062   verifyFormat("co_await *a;", PASLeftStyle);
9063   verifyFormat("co_yield *a", PASLeftStyle);
9064   verifyFormat("return *a;", PASLeftStyle);
9065 }
9066 
9067 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9068   verifyFormat("if (!aaaaaaaaaa( // break\n"
9069                "        aaaaa)) {\n"
9070                "}");
9071   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9072                "    aaaaa));");
9073   verifyFormat("*aaa = aaaaaaa( // break\n"
9074                "    bbbbbb);");
9075 }
9076 
9077 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9078   verifyFormat("bool operator<();");
9079   verifyFormat("bool operator>();");
9080   verifyFormat("bool operator=();");
9081   verifyFormat("bool operator==();");
9082   verifyFormat("bool operator!=();");
9083   verifyFormat("int operator+();");
9084   verifyFormat("int operator++();");
9085   verifyFormat("int operator++(int) volatile noexcept;");
9086   verifyFormat("bool operator,();");
9087   verifyFormat("bool operator();");
9088   verifyFormat("bool operator()();");
9089   verifyFormat("bool operator[]();");
9090   verifyFormat("operator bool();");
9091   verifyFormat("operator int();");
9092   verifyFormat("operator void *();");
9093   verifyFormat("operator SomeType<int>();");
9094   verifyFormat("operator SomeType<int, int>();");
9095   verifyFormat("operator SomeType<SomeType<int>>();");
9096   verifyFormat("void *operator new(std::size_t size);");
9097   verifyFormat("void *operator new[](std::size_t size);");
9098   verifyFormat("void operator delete(void *ptr);");
9099   verifyFormat("void operator delete[](void *ptr);");
9100   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9101                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9102   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9103                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9104 
9105   verifyFormat(
9106       "ostream &operator<<(ostream &OutputStream,\n"
9107       "                    SomeReallyLongType WithSomeReallyLongValue);");
9108   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9109                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9110                "  return left.group < right.group;\n"
9111                "}");
9112   verifyFormat("SomeType &operator=(const SomeType &S);");
9113   verifyFormat("f.template operator()<int>();");
9114 
9115   verifyGoogleFormat("operator void*();");
9116   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9117   verifyGoogleFormat("operator ::A();");
9118 
9119   verifyFormat("using A::operator+;");
9120   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9121                "int i;");
9122 
9123   // Calling an operator as a member function.
9124   verifyFormat("void f() { a.operator*(); }");
9125   verifyFormat("void f() { a.operator*(b & b); }");
9126   verifyFormat("void f() { a->operator&(a * b); }");
9127   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9128   // TODO: Calling an operator as a non-member function is hard to distinguish.
9129   // https://llvm.org/PR50629
9130   // verifyFormat("void f() { operator*(a & a); }");
9131   // verifyFormat("void f() { operator&(a, b * b); }");
9132 }
9133 
9134 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9135   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9136   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9137   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9138   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9139   verifyFormat("Deleted &operator=(const Deleted &) &;");
9140   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9141   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9142   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9143   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9144   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9145   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9146   verifyFormat("void Fn(T const &) const &;");
9147   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9148   verifyFormat("template <typename T>\n"
9149                "void F(T) && = delete;",
9150                getGoogleStyle());
9151 
9152   FormatStyle AlignLeft = getLLVMStyle();
9153   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9154   verifyFormat("void A::b() && {}", AlignLeft);
9155   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9156   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9157                AlignLeft);
9158   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9159   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9160   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9161   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9162   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9163   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9164   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9165   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9166 
9167   FormatStyle Spaces = getLLVMStyle();
9168   Spaces.SpacesInCStyleCastParentheses = true;
9169   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9170   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9171   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9172   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9173 
9174   Spaces.SpacesInCStyleCastParentheses = false;
9175   Spaces.SpacesInParentheses = true;
9176   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9177   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9178                Spaces);
9179   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9180   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9181 
9182   FormatStyle BreakTemplate = getLLVMStyle();
9183   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9184 
9185   verifyFormat("struct f {\n"
9186                "  template <class T>\n"
9187                "  int &foo(const std::string &str) &noexcept {}\n"
9188                "};",
9189                BreakTemplate);
9190 
9191   verifyFormat("struct f {\n"
9192                "  template <class T>\n"
9193                "  int &foo(const std::string &str) &&noexcept {}\n"
9194                "};",
9195                BreakTemplate);
9196 
9197   verifyFormat("struct f {\n"
9198                "  template <class T>\n"
9199                "  int &foo(const std::string &str) const &noexcept {}\n"
9200                "};",
9201                BreakTemplate);
9202 
9203   verifyFormat("struct f {\n"
9204                "  template <class T>\n"
9205                "  int &foo(const std::string &str) const &noexcept {}\n"
9206                "};",
9207                BreakTemplate);
9208 
9209   verifyFormat("struct f {\n"
9210                "  template <class T>\n"
9211                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9212                "};",
9213                BreakTemplate);
9214 
9215   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9216   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9217       FormatStyle::BTDS_Yes;
9218   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9219 
9220   verifyFormat("struct f {\n"
9221                "  template <class T>\n"
9222                "  int& foo(const std::string& str) & noexcept {}\n"
9223                "};",
9224                AlignLeftBreakTemplate);
9225 
9226   verifyFormat("struct f {\n"
9227                "  template <class T>\n"
9228                "  int& foo(const std::string& str) && noexcept {}\n"
9229                "};",
9230                AlignLeftBreakTemplate);
9231 
9232   verifyFormat("struct f {\n"
9233                "  template <class T>\n"
9234                "  int& foo(const std::string& str) const& noexcept {}\n"
9235                "};",
9236                AlignLeftBreakTemplate);
9237 
9238   verifyFormat("struct f {\n"
9239                "  template <class T>\n"
9240                "  int& foo(const std::string& str) const&& noexcept {}\n"
9241                "};",
9242                AlignLeftBreakTemplate);
9243 
9244   verifyFormat("struct f {\n"
9245                "  template <class T>\n"
9246                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9247                "};",
9248                AlignLeftBreakTemplate);
9249 
9250   // The `&` in `Type&` should not be confused with a trailing `&` of
9251   // DEPRECATED(reason) member function.
9252   verifyFormat("struct f {\n"
9253                "  template <class T>\n"
9254                "  DEPRECATED(reason)\n"
9255                "  Type &foo(arguments) {}\n"
9256                "};",
9257                BreakTemplate);
9258 
9259   verifyFormat("struct f {\n"
9260                "  template <class T>\n"
9261                "  DEPRECATED(reason)\n"
9262                "  Type& foo(arguments) {}\n"
9263                "};",
9264                AlignLeftBreakTemplate);
9265 
9266   verifyFormat("void (*foopt)(int) = &func;");
9267 }
9268 
9269 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9270   verifyFormat("void f() {\n"
9271                "  A *a = new A;\n"
9272                "  A *a = new (placement) A;\n"
9273                "  delete a;\n"
9274                "  delete (A *)a;\n"
9275                "}");
9276   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9277                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9278   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9279                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9280                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9281   verifyFormat("delete[] h->p;");
9282 }
9283 
9284 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9285   verifyFormat("int *f(int *a) {}");
9286   verifyFormat("int main(int argc, char **argv) {}");
9287   verifyFormat("Test::Test(int b) : a(b * b) {}");
9288   verifyIndependentOfContext("f(a, *a);");
9289   verifyFormat("void g() { f(*a); }");
9290   verifyIndependentOfContext("int a = b * 10;");
9291   verifyIndependentOfContext("int a = 10 * b;");
9292   verifyIndependentOfContext("int a = b * c;");
9293   verifyIndependentOfContext("int a += b * c;");
9294   verifyIndependentOfContext("int a -= b * c;");
9295   verifyIndependentOfContext("int a *= b * c;");
9296   verifyIndependentOfContext("int a /= b * c;");
9297   verifyIndependentOfContext("int a = *b;");
9298   verifyIndependentOfContext("int a = *b * c;");
9299   verifyIndependentOfContext("int a = b * *c;");
9300   verifyIndependentOfContext("int a = b * (10);");
9301   verifyIndependentOfContext("S << b * (10);");
9302   verifyIndependentOfContext("return 10 * b;");
9303   verifyIndependentOfContext("return *b * *c;");
9304   verifyIndependentOfContext("return a & ~b;");
9305   verifyIndependentOfContext("f(b ? *c : *d);");
9306   verifyIndependentOfContext("int a = b ? *c : *d;");
9307   verifyIndependentOfContext("*b = a;");
9308   verifyIndependentOfContext("a * ~b;");
9309   verifyIndependentOfContext("a * !b;");
9310   verifyIndependentOfContext("a * +b;");
9311   verifyIndependentOfContext("a * -b;");
9312   verifyIndependentOfContext("a * ++b;");
9313   verifyIndependentOfContext("a * --b;");
9314   verifyIndependentOfContext("a[4] * b;");
9315   verifyIndependentOfContext("a[a * a] = 1;");
9316   verifyIndependentOfContext("f() * b;");
9317   verifyIndependentOfContext("a * [self dostuff];");
9318   verifyIndependentOfContext("int x = a * (a + b);");
9319   verifyIndependentOfContext("(a *)(a + b);");
9320   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9321   verifyIndependentOfContext("int *pa = (int *)&a;");
9322   verifyIndependentOfContext("return sizeof(int **);");
9323   verifyIndependentOfContext("return sizeof(int ******);");
9324   verifyIndependentOfContext("return (int **&)a;");
9325   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9326   verifyFormat("void f(Type (*parameter)[10]) {}");
9327   verifyFormat("void f(Type (&parameter)[10]) {}");
9328   verifyGoogleFormat("return sizeof(int**);");
9329   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9330   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9331   verifyFormat("auto a = [](int **&, int ***) {};");
9332   verifyFormat("auto PointerBinding = [](const char *S) {};");
9333   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9334   verifyFormat("[](const decltype(*a) &value) {}");
9335   verifyFormat("[](const typeof(*a) &value) {}");
9336   verifyFormat("[](const _Atomic(a *) &value) {}");
9337   verifyFormat("[](const __underlying_type(a) &value) {}");
9338   verifyFormat("decltype(a * b) F();");
9339   verifyFormat("typeof(a * b) F();");
9340   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9341   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9342   verifyIndependentOfContext("typedef void (*f)(int *a);");
9343   verifyIndependentOfContext("int i{a * b};");
9344   verifyIndependentOfContext("aaa && aaa->f();");
9345   verifyIndependentOfContext("int x = ~*p;");
9346   verifyFormat("Constructor() : a(a), area(width * height) {}");
9347   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9348   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9349   verifyFormat("void f() { f(a, c * d); }");
9350   verifyFormat("void f() { f(new a(), c * d); }");
9351   verifyFormat("void f(const MyOverride &override);");
9352   verifyFormat("void f(const MyFinal &final);");
9353   verifyIndependentOfContext("bool a = f() && override.f();");
9354   verifyIndependentOfContext("bool a = f() && final.f();");
9355 
9356   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9357 
9358   verifyIndependentOfContext("A<int *> a;");
9359   verifyIndependentOfContext("A<int **> a;");
9360   verifyIndependentOfContext("A<int *, int *> a;");
9361   verifyIndependentOfContext("A<int *[]> a;");
9362   verifyIndependentOfContext(
9363       "const char *const p = reinterpret_cast<const char *const>(q);");
9364   verifyIndependentOfContext("A<int **, int **> a;");
9365   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9366   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9367   verifyFormat("for (; a && b;) {\n}");
9368   verifyFormat("bool foo = true && [] { return false; }();");
9369 
9370   verifyFormat(
9371       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9372       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9373 
9374   verifyGoogleFormat("int const* a = &b;");
9375   verifyGoogleFormat("**outparam = 1;");
9376   verifyGoogleFormat("*outparam = a * b;");
9377   verifyGoogleFormat("int main(int argc, char** argv) {}");
9378   verifyGoogleFormat("A<int*> a;");
9379   verifyGoogleFormat("A<int**> a;");
9380   verifyGoogleFormat("A<int*, int*> a;");
9381   verifyGoogleFormat("A<int**, int**> a;");
9382   verifyGoogleFormat("f(b ? *c : *d);");
9383   verifyGoogleFormat("int a = b ? *c : *d;");
9384   verifyGoogleFormat("Type* t = **x;");
9385   verifyGoogleFormat("Type* t = *++*x;");
9386   verifyGoogleFormat("*++*x;");
9387   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9388   verifyGoogleFormat("Type* t = x++ * y;");
9389   verifyGoogleFormat(
9390       "const char* const p = reinterpret_cast<const char* const>(q);");
9391   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9392   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9393   verifyGoogleFormat("template <typename T>\n"
9394                      "void f(int i = 0, SomeType** temps = NULL);");
9395 
9396   FormatStyle Left = getLLVMStyle();
9397   Left.PointerAlignment = FormatStyle::PAS_Left;
9398   verifyFormat("x = *a(x) = *a(y);", Left);
9399   verifyFormat("for (;; *a = b) {\n}", Left);
9400   verifyFormat("return *this += 1;", Left);
9401   verifyFormat("throw *x;", Left);
9402   verifyFormat("delete *x;", Left);
9403   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9404   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9405   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9406   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9407   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9408   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9409   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9410   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9411   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9412 
9413   verifyIndependentOfContext("a = *(x + y);");
9414   verifyIndependentOfContext("a = &(x + y);");
9415   verifyIndependentOfContext("*(x + y).call();");
9416   verifyIndependentOfContext("&(x + y)->call();");
9417   verifyFormat("void f() { &(*I).first; }");
9418 
9419   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9420   verifyFormat(
9421       "int *MyValues = {\n"
9422       "    *A, // Operator detection might be confused by the '{'\n"
9423       "    *BB // Operator detection might be confused by previous comment\n"
9424       "};");
9425 
9426   verifyIndependentOfContext("if (int *a = &b)");
9427   verifyIndependentOfContext("if (int &a = *b)");
9428   verifyIndependentOfContext("if (a & b[i])");
9429   verifyIndependentOfContext("if constexpr (a & b[i])");
9430   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9431   verifyIndependentOfContext("if (a * (b * c))");
9432   verifyIndependentOfContext("if constexpr (a * (b * c))");
9433   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9434   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9435   verifyIndependentOfContext("if (*b[i])");
9436   verifyIndependentOfContext("if (int *a = (&b))");
9437   verifyIndependentOfContext("while (int *a = &b)");
9438   verifyIndependentOfContext("while (a * (b * c))");
9439   verifyIndependentOfContext("size = sizeof *a;");
9440   verifyIndependentOfContext("if (a && (b = c))");
9441   verifyFormat("void f() {\n"
9442                "  for (const int &v : Values) {\n"
9443                "  }\n"
9444                "}");
9445   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9446   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9447   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9448 
9449   verifyFormat("#define A (!a * b)");
9450   verifyFormat("#define MACRO     \\\n"
9451                "  int *i = a * b; \\\n"
9452                "  void f(a *b);",
9453                getLLVMStyleWithColumns(19));
9454 
9455   verifyIndependentOfContext("A = new SomeType *[Length];");
9456   verifyIndependentOfContext("A = new SomeType *[Length]();");
9457   verifyIndependentOfContext("T **t = new T *;");
9458   verifyIndependentOfContext("T **t = new T *();");
9459   verifyGoogleFormat("A = new SomeType*[Length]();");
9460   verifyGoogleFormat("A = new SomeType*[Length];");
9461   verifyGoogleFormat("T** t = new T*;");
9462   verifyGoogleFormat("T** t = new T*();");
9463 
9464   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9465   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9466   verifyFormat("template <bool a, bool b> "
9467                "typename t::if<x && y>::type f() {}");
9468   verifyFormat("template <int *y> f() {}");
9469   verifyFormat("vector<int *> v;");
9470   verifyFormat("vector<int *const> v;");
9471   verifyFormat("vector<int *const **const *> v;");
9472   verifyFormat("vector<int *volatile> v;");
9473   verifyFormat("vector<a *_Nonnull> v;");
9474   verifyFormat("vector<a *_Nullable> v;");
9475   verifyFormat("vector<a *_Null_unspecified> v;");
9476   verifyFormat("vector<a *__ptr32> v;");
9477   verifyFormat("vector<a *__ptr64> v;");
9478   verifyFormat("vector<a *__capability> v;");
9479   FormatStyle TypeMacros = getLLVMStyle();
9480   TypeMacros.TypenameMacros = {"LIST"};
9481   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9482   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9483   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9484   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9485   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9486 
9487   FormatStyle CustomQualifier = getLLVMStyle();
9488   // Add identifiers that should not be parsed as a qualifier by default.
9489   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9490   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9491   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9492   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9493   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9494   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9495   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9496   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9497   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9498   verifyFormat("vector<a * _NotAQualifier> v;");
9499   verifyFormat("vector<a * __not_a_qualifier> v;");
9500   verifyFormat("vector<a * b> v;");
9501   verifyFormat("foo<b && false>();");
9502   verifyFormat("foo<b & 1>();");
9503   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9504   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9505   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9506   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9507   verifyFormat(
9508       "template <class T, class = typename std::enable_if<\n"
9509       "                       std::is_integral<T>::value &&\n"
9510       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9511       "void F();",
9512       getLLVMStyleWithColumns(70));
9513   verifyFormat("template <class T,\n"
9514                "          class = typename std::enable_if<\n"
9515                "              std::is_integral<T>::value &&\n"
9516                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9517                "          class U>\n"
9518                "void F();",
9519                getLLVMStyleWithColumns(70));
9520   verifyFormat(
9521       "template <class T,\n"
9522       "          class = typename ::std::enable_if<\n"
9523       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9524       "void F();",
9525       getGoogleStyleWithColumns(68));
9526 
9527   verifyIndependentOfContext("MACRO(int *i);");
9528   verifyIndependentOfContext("MACRO(auto *a);");
9529   verifyIndependentOfContext("MACRO(const A *a);");
9530   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9531   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9532   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9533   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9534   verifyIndependentOfContext("MACRO(A *const a);");
9535   verifyIndependentOfContext("MACRO(A *restrict a);");
9536   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9537   verifyIndependentOfContext("MACRO(A *__restrict a);");
9538   verifyIndependentOfContext("MACRO(A *volatile a);");
9539   verifyIndependentOfContext("MACRO(A *__volatile a);");
9540   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9541   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9542   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9543   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9544   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9545   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9546   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9547   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9548   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9549   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9550   verifyIndependentOfContext("MACRO(A *__capability);");
9551   verifyIndependentOfContext("MACRO(A &__capability);");
9552   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9553   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9554   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9555   // a type declaration:
9556   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
9557   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
9558   // Also check that TypenameMacros prevents parsing it as multiplication:
9559   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
9560   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
9561 
9562   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
9563   verifyFormat("void f() { f(float{1}, a * a); }");
9564   verifyFormat("void f() { f(float(1), a * a); }");
9565 
9566   verifyFormat("f((void (*)(int))g);");
9567   verifyFormat("f((void (&)(int))g);");
9568   verifyFormat("f((void (^)(int))g);");
9569 
9570   // FIXME: Is there a way to make this work?
9571   // verifyIndependentOfContext("MACRO(A *a);");
9572   verifyFormat("MACRO(A &B);");
9573   verifyFormat("MACRO(A *B);");
9574   verifyFormat("void f() { MACRO(A * B); }");
9575   verifyFormat("void f() { MACRO(A & B); }");
9576 
9577   // This lambda was mis-formatted after D88956 (treating it as a binop):
9578   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
9579   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
9580   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
9581   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
9582 
9583   verifyFormat("DatumHandle const *operator->() const { return input_; }");
9584   verifyFormat("return options != nullptr && operator==(*options);");
9585 
9586   EXPECT_EQ("#define OP(x)                                    \\\n"
9587             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
9588             "    return s << a.DebugString();                 \\\n"
9589             "  }",
9590             format("#define OP(x) \\\n"
9591                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
9592                    "    return s << a.DebugString(); \\\n"
9593                    "  }",
9594                    getLLVMStyleWithColumns(50)));
9595 
9596   // FIXME: We cannot handle this case yet; we might be able to figure out that
9597   // foo<x> d > v; doesn't make sense.
9598   verifyFormat("foo<a<b && c> d> v;");
9599 
9600   FormatStyle PointerMiddle = getLLVMStyle();
9601   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9602   verifyFormat("delete *x;", PointerMiddle);
9603   verifyFormat("int * x;", PointerMiddle);
9604   verifyFormat("int *[] x;", PointerMiddle);
9605   verifyFormat("template <int * y> f() {}", PointerMiddle);
9606   verifyFormat("int * f(int * a) {}", PointerMiddle);
9607   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
9608   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
9609   verifyFormat("A<int *> a;", PointerMiddle);
9610   verifyFormat("A<int **> a;", PointerMiddle);
9611   verifyFormat("A<int *, int *> a;", PointerMiddle);
9612   verifyFormat("A<int *[]> a;", PointerMiddle);
9613   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
9614   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
9615   verifyFormat("T ** t = new T *;", PointerMiddle);
9616 
9617   // Member function reference qualifiers aren't binary operators.
9618   verifyFormat("string // break\n"
9619                "operator()() & {}");
9620   verifyFormat("string // break\n"
9621                "operator()() && {}");
9622   verifyGoogleFormat("template <typename T>\n"
9623                      "auto x() & -> int {}");
9624 
9625   // Should be binary operators when used as an argument expression (overloaded
9626   // operator invoked as a member function).
9627   verifyFormat("void f() { a.operator()(a * a); }");
9628   verifyFormat("void f() { a->operator()(a & a); }");
9629   verifyFormat("void f() { a.operator()(*a & *a); }");
9630   verifyFormat("void f() { a->operator()(*a * *a); }");
9631 }
9632 
9633 TEST_F(FormatTest, UnderstandsAttributes) {
9634   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
9635   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
9636                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9637   FormatStyle AfterType = getLLVMStyle();
9638   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9639   verifyFormat("__attribute__((nodebug)) void\n"
9640                "foo() {}\n",
9641                AfterType);
9642   verifyFormat("__unused void\n"
9643                "foo() {}",
9644                AfterType);
9645 
9646   FormatStyle CustomAttrs = getLLVMStyle();
9647   CustomAttrs.AttributeMacros.push_back("__unused");
9648   CustomAttrs.AttributeMacros.push_back("__attr1");
9649   CustomAttrs.AttributeMacros.push_back("__attr2");
9650   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
9651   verifyFormat("vector<SomeType *__attribute((foo))> v;");
9652   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
9653   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
9654   // Check that it is parsed as a multiplication without AttributeMacros and
9655   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
9656   verifyFormat("vector<SomeType * __attr1> v;");
9657   verifyFormat("vector<SomeType __attr1 *> v;");
9658   verifyFormat("vector<SomeType __attr1 *const> v;");
9659   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
9660   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
9661   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
9662   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
9663   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
9664   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
9665   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
9666   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
9667 
9668   // Check that these are not parsed as function declarations:
9669   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9670   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
9671   verifyFormat("SomeType s(InitValue);", CustomAttrs);
9672   verifyFormat("SomeType s{InitValue};", CustomAttrs);
9673   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
9674   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
9675   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
9676   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
9677   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
9678   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
9679 }
9680 
9681 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
9682   // Check that qualifiers on pointers don't break parsing of casts.
9683   verifyFormat("x = (foo *const)*v;");
9684   verifyFormat("x = (foo *volatile)*v;");
9685   verifyFormat("x = (foo *restrict)*v;");
9686   verifyFormat("x = (foo *__attribute__((foo)))*v;");
9687   verifyFormat("x = (foo *_Nonnull)*v;");
9688   verifyFormat("x = (foo *_Nullable)*v;");
9689   verifyFormat("x = (foo *_Null_unspecified)*v;");
9690   verifyFormat("x = (foo *_Nonnull)*v;");
9691   verifyFormat("x = (foo *[[clang::attr]])*v;");
9692   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
9693   verifyFormat("x = (foo *__ptr32)*v;");
9694   verifyFormat("x = (foo *__ptr64)*v;");
9695   verifyFormat("x = (foo *__capability)*v;");
9696 
9697   // Check that we handle multiple trailing qualifiers and skip them all to
9698   // determine that the expression is a cast to a pointer type.
9699   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
9700   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
9701   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
9702   StringRef AllQualifiers =
9703       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
9704       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
9705   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
9706   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
9707 
9708   // Also check that address-of is not parsed as a binary bitwise-and:
9709   verifyFormat("x = (foo *const)&v;");
9710   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
9711   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
9712 
9713   // Check custom qualifiers:
9714   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
9715   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9716   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
9717   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
9718   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
9719                CustomQualifier);
9720   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
9721                CustomQualifier);
9722 
9723   // Check that unknown identifiers result in binary operator parsing:
9724   verifyFormat("x = (foo * __unknown_qualifier) * v;");
9725   verifyFormat("x = (foo * __unknown_qualifier) & v;");
9726 }
9727 
9728 TEST_F(FormatTest, UnderstandsSquareAttributes) {
9729   verifyFormat("SomeType s [[unused]] (InitValue);");
9730   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
9731   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
9732   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
9733   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
9734   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9735                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9736   verifyFormat("[[nodiscard]] bool f() { return false; }");
9737   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
9738   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
9739   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
9740 
9741   // Make sure we do not mistake attributes for array subscripts.
9742   verifyFormat("int a() {}\n"
9743                "[[unused]] int b() {}\n");
9744   verifyFormat("NSArray *arr;\n"
9745                "arr[[Foo() bar]];");
9746 
9747   // On the other hand, we still need to correctly find array subscripts.
9748   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
9749 
9750   // Make sure that we do not mistake Objective-C method inside array literals
9751   // as attributes, even if those method names are also keywords.
9752   verifyFormat("@[ [foo bar] ];");
9753   verifyFormat("@[ [NSArray class] ];");
9754   verifyFormat("@[ [foo enum] ];");
9755 
9756   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
9757 
9758   // Make sure we do not parse attributes as lambda introducers.
9759   FormatStyle MultiLineFunctions = getLLVMStyle();
9760   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9761   verifyFormat("[[unused]] int b() {\n"
9762                "  return 42;\n"
9763                "}\n",
9764                MultiLineFunctions);
9765 }
9766 
9767 TEST_F(FormatTest, AttributeClass) {
9768   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
9769   verifyFormat("class S {\n"
9770                "  S(S&&) = default;\n"
9771                "};",
9772                Style);
9773   verifyFormat("class [[nodiscard]] S {\n"
9774                "  S(S&&) = default;\n"
9775                "};",
9776                Style);
9777   verifyFormat("class __attribute((maybeunused)) S {\n"
9778                "  S(S&&) = default;\n"
9779                "};",
9780                Style);
9781   verifyFormat("struct S {\n"
9782                "  S(S&&) = default;\n"
9783                "};",
9784                Style);
9785   verifyFormat("struct [[nodiscard]] S {\n"
9786                "  S(S&&) = default;\n"
9787                "};",
9788                Style);
9789 }
9790 
9791 TEST_F(FormatTest, AttributesAfterMacro) {
9792   FormatStyle Style = getLLVMStyle();
9793   verifyFormat("MACRO;\n"
9794                "__attribute__((maybe_unused)) int foo() {\n"
9795                "  //...\n"
9796                "}");
9797 
9798   verifyFormat("MACRO;\n"
9799                "[[nodiscard]] int foo() {\n"
9800                "  //...\n"
9801                "}");
9802 
9803   EXPECT_EQ("MACRO\n\n"
9804             "__attribute__((maybe_unused)) int foo() {\n"
9805             "  //...\n"
9806             "}",
9807             format("MACRO\n\n"
9808                    "__attribute__((maybe_unused)) int foo() {\n"
9809                    "  //...\n"
9810                    "}"));
9811 
9812   EXPECT_EQ("MACRO\n\n"
9813             "[[nodiscard]] int foo() {\n"
9814             "  //...\n"
9815             "}",
9816             format("MACRO\n\n"
9817                    "[[nodiscard]] int foo() {\n"
9818                    "  //...\n"
9819                    "}"));
9820 }
9821 
9822 TEST_F(FormatTest, AttributePenaltyBreaking) {
9823   FormatStyle Style = getLLVMStyle();
9824   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
9825                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9826                Style);
9827   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
9828                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9829                Style);
9830   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
9831                "shared_ptr<ALongTypeName> &C d) {\n}",
9832                Style);
9833 }
9834 
9835 TEST_F(FormatTest, UnderstandsEllipsis) {
9836   FormatStyle Style = getLLVMStyle();
9837   verifyFormat("int printf(const char *fmt, ...);");
9838   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
9839   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
9840 
9841   verifyFormat("template <int *...PP> a;", Style);
9842 
9843   Style.PointerAlignment = FormatStyle::PAS_Left;
9844   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
9845 
9846   verifyFormat("template <int*... PP> a;", Style);
9847 
9848   Style.PointerAlignment = FormatStyle::PAS_Middle;
9849   verifyFormat("template <int *... PP> a;", Style);
9850 }
9851 
9852 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
9853   EXPECT_EQ("int *a;\n"
9854             "int *a;\n"
9855             "int *a;",
9856             format("int *a;\n"
9857                    "int* a;\n"
9858                    "int *a;",
9859                    getGoogleStyle()));
9860   EXPECT_EQ("int* a;\n"
9861             "int* a;\n"
9862             "int* a;",
9863             format("int* a;\n"
9864                    "int* a;\n"
9865                    "int *a;",
9866                    getGoogleStyle()));
9867   EXPECT_EQ("int *a;\n"
9868             "int *a;\n"
9869             "int *a;",
9870             format("int *a;\n"
9871                    "int * a;\n"
9872                    "int *  a;",
9873                    getGoogleStyle()));
9874   EXPECT_EQ("auto x = [] {\n"
9875             "  int *a;\n"
9876             "  int *a;\n"
9877             "  int *a;\n"
9878             "};",
9879             format("auto x=[]{int *a;\n"
9880                    "int * a;\n"
9881                    "int *  a;};",
9882                    getGoogleStyle()));
9883 }
9884 
9885 TEST_F(FormatTest, UnderstandsRvalueReferences) {
9886   verifyFormat("int f(int &&a) {}");
9887   verifyFormat("int f(int a, char &&b) {}");
9888   verifyFormat("void f() { int &&a = b; }");
9889   verifyGoogleFormat("int f(int a, char&& b) {}");
9890   verifyGoogleFormat("void f() { int&& a = b; }");
9891 
9892   verifyIndependentOfContext("A<int &&> a;");
9893   verifyIndependentOfContext("A<int &&, int &&> a;");
9894   verifyGoogleFormat("A<int&&> a;");
9895   verifyGoogleFormat("A<int&&, int&&> a;");
9896 
9897   // Not rvalue references:
9898   verifyFormat("template <bool B, bool C> class A {\n"
9899                "  static_assert(B && C, \"Something is wrong\");\n"
9900                "};");
9901   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
9902   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
9903   verifyFormat("#define A(a, b) (a && b)");
9904 }
9905 
9906 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
9907   verifyFormat("void f() {\n"
9908                "  x[aaaaaaaaa -\n"
9909                "    b] = 23;\n"
9910                "}",
9911                getLLVMStyleWithColumns(15));
9912 }
9913 
9914 TEST_F(FormatTest, FormatsCasts) {
9915   verifyFormat("Type *A = static_cast<Type *>(P);");
9916   verifyFormat("Type *A = (Type *)P;");
9917   verifyFormat("Type *A = (vector<Type *, int *>)P;");
9918   verifyFormat("int a = (int)(2.0f);");
9919   verifyFormat("int a = (int)2.0f;");
9920   verifyFormat("x[(int32)y];");
9921   verifyFormat("x = (int32)y;");
9922   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
9923   verifyFormat("int a = (int)*b;");
9924   verifyFormat("int a = (int)2.0f;");
9925   verifyFormat("int a = (int)~0;");
9926   verifyFormat("int a = (int)++a;");
9927   verifyFormat("int a = (int)sizeof(int);");
9928   verifyFormat("int a = (int)+2;");
9929   verifyFormat("my_int a = (my_int)2.0f;");
9930   verifyFormat("my_int a = (my_int)sizeof(int);");
9931   verifyFormat("return (my_int)aaa;");
9932   verifyFormat("#define x ((int)-1)");
9933   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
9934   verifyFormat("#define p(q) ((int *)&q)");
9935   verifyFormat("fn(a)(b) + 1;");
9936 
9937   verifyFormat("void f() { my_int a = (my_int)*b; }");
9938   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
9939   verifyFormat("my_int a = (my_int)~0;");
9940   verifyFormat("my_int a = (my_int)++a;");
9941   verifyFormat("my_int a = (my_int)-2;");
9942   verifyFormat("my_int a = (my_int)1;");
9943   verifyFormat("my_int a = (my_int *)1;");
9944   verifyFormat("my_int a = (const my_int)-1;");
9945   verifyFormat("my_int a = (const my_int *)-1;");
9946   verifyFormat("my_int a = (my_int)(my_int)-1;");
9947   verifyFormat("my_int a = (ns::my_int)-2;");
9948   verifyFormat("case (my_int)ONE:");
9949   verifyFormat("auto x = (X)this;");
9950   // Casts in Obj-C style calls used to not be recognized as such.
9951   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
9952 
9953   // FIXME: single value wrapped with paren will be treated as cast.
9954   verifyFormat("void f(int i = (kValue)*kMask) {}");
9955 
9956   verifyFormat("{ (void)F; }");
9957 
9958   // Don't break after a cast's
9959   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9960                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
9961                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
9962 
9963   // These are not casts.
9964   verifyFormat("void f(int *) {}");
9965   verifyFormat("f(foo)->b;");
9966   verifyFormat("f(foo).b;");
9967   verifyFormat("f(foo)(b);");
9968   verifyFormat("f(foo)[b];");
9969   verifyFormat("[](foo) { return 4; }(bar);");
9970   verifyFormat("(*funptr)(foo)[4];");
9971   verifyFormat("funptrs[4](foo)[4];");
9972   verifyFormat("void f(int *);");
9973   verifyFormat("void f(int *) = 0;");
9974   verifyFormat("void f(SmallVector<int>) {}");
9975   verifyFormat("void f(SmallVector<int>);");
9976   verifyFormat("void f(SmallVector<int>) = 0;");
9977   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
9978   verifyFormat("int a = sizeof(int) * b;");
9979   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
9980   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
9981   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
9982   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
9983 
9984   // These are not casts, but at some point were confused with casts.
9985   verifyFormat("virtual void foo(int *) override;");
9986   verifyFormat("virtual void foo(char &) const;");
9987   verifyFormat("virtual void foo(int *a, char *) const;");
9988   verifyFormat("int a = sizeof(int *) + b;");
9989   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
9990   verifyFormat("bool b = f(g<int>) && c;");
9991   verifyFormat("typedef void (*f)(int i) func;");
9992   verifyFormat("void operator++(int) noexcept;");
9993   verifyFormat("void operator++(int &) noexcept;");
9994   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
9995                "&) noexcept;");
9996   verifyFormat(
9997       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
9998   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
9999   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10000   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10001   verifyFormat("void operator delete(foo &) noexcept;");
10002   verifyFormat("void operator delete(foo) noexcept;");
10003   verifyFormat("void operator delete(int) noexcept;");
10004   verifyFormat("void operator delete(int &) noexcept;");
10005   verifyFormat("void operator delete(int &) volatile noexcept;");
10006   verifyFormat("void operator delete(int &) const");
10007   verifyFormat("void operator delete(int &) = default");
10008   verifyFormat("void operator delete(int &) = delete");
10009   verifyFormat("void operator delete(int &) [[noreturn]]");
10010   verifyFormat("void operator delete(int &) throw();");
10011   verifyFormat("void operator delete(int &) throw(int);");
10012   verifyFormat("auto operator delete(int &) -> int;");
10013   verifyFormat("auto operator delete(int &) override");
10014   verifyFormat("auto operator delete(int &) final");
10015 
10016   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10017                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10018   // FIXME: The indentation here is not ideal.
10019   verifyFormat(
10020       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10021       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10022       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10023 }
10024 
10025 TEST_F(FormatTest, FormatsFunctionTypes) {
10026   verifyFormat("A<bool()> a;");
10027   verifyFormat("A<SomeType()> a;");
10028   verifyFormat("A<void (*)(int, std::string)> a;");
10029   verifyFormat("A<void *(int)>;");
10030   verifyFormat("void *(*a)(int *, SomeType *);");
10031   verifyFormat("int (*func)(void *);");
10032   verifyFormat("void f() { int (*func)(void *); }");
10033   verifyFormat("template <class CallbackClass>\n"
10034                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10035 
10036   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10037   verifyGoogleFormat("void* (*a)(int);");
10038   verifyGoogleFormat(
10039       "template <class CallbackClass>\n"
10040       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10041 
10042   // Other constructs can look somewhat like function types:
10043   verifyFormat("A<sizeof(*x)> a;");
10044   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10045   verifyFormat("some_var = function(*some_pointer_var)[0];");
10046   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10047   verifyFormat("int x = f(&h)();");
10048   verifyFormat("returnsFunction(&param1, &param2)(param);");
10049   verifyFormat("std::function<\n"
10050                "    LooooooooooongTemplatedType<\n"
10051                "        SomeType>*(\n"
10052                "        LooooooooooooooooongType type)>\n"
10053                "    function;",
10054                getGoogleStyleWithColumns(40));
10055 }
10056 
10057 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10058   verifyFormat("A (*foo_)[6];");
10059   verifyFormat("vector<int> (*foo_)[6];");
10060 }
10061 
10062 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10063   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10064                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10065   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10066                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10067   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10068                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10069 
10070   // Different ways of ()-initializiation.
10071   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10072                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10073   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10074                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10075   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10076                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10077   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10078                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10079 
10080   // Lambdas should not confuse the variable declaration heuristic.
10081   verifyFormat("LooooooooooooooooongType\n"
10082                "    variable(nullptr, [](A *a) {});",
10083                getLLVMStyleWithColumns(40));
10084 }
10085 
10086 TEST_F(FormatTest, BreaksLongDeclarations) {
10087   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10088                "    AnotherNameForTheLongType;");
10089   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10090                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10091   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10092                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10093   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10094                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10095   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10096                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10097   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10098                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10099   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10100                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10101   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10102                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10103   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10104                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10105   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10106                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10107   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10108                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10109   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10110                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10111   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10112                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10113   FormatStyle Indented = getLLVMStyle();
10114   Indented.IndentWrappedFunctionNames = true;
10115   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10116                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10117                Indented);
10118   verifyFormat(
10119       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10120       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10121       Indented);
10122   verifyFormat(
10123       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10124       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10125       Indented);
10126   verifyFormat(
10127       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10128       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10129       Indented);
10130 
10131   // FIXME: Without the comment, this breaks after "(".
10132   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10133                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10134                getGoogleStyle());
10135 
10136   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10137                "                  int LoooooooooooooooooooongParam2) {}");
10138   verifyFormat(
10139       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10140       "                                   SourceLocation L, IdentifierIn *II,\n"
10141       "                                   Type *T) {}");
10142   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10143                "ReallyReaaallyLongFunctionName(\n"
10144                "    const std::string &SomeParameter,\n"
10145                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10146                "        &ReallyReallyLongParameterName,\n"
10147                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10148                "        &AnotherLongParameterName) {}");
10149   verifyFormat("template <typename A>\n"
10150                "SomeLoooooooooooooooooooooongType<\n"
10151                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10152                "Function() {}");
10153 
10154   verifyGoogleFormat(
10155       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10156       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10157   verifyGoogleFormat(
10158       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10159       "                                   SourceLocation L) {}");
10160   verifyGoogleFormat(
10161       "some_namespace::LongReturnType\n"
10162       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10163       "    int first_long_parameter, int second_parameter) {}");
10164 
10165   verifyGoogleFormat("template <typename T>\n"
10166                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10167                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10168   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10169                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10170 
10171   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10172                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10173                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10174   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10175                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10176                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10177   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10178                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10179                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10180                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10181 
10182   verifyFormat("template <typename T> // Templates on own line.\n"
10183                "static int            // Some comment.\n"
10184                "MyFunction(int a);",
10185                getLLVMStyle());
10186 }
10187 
10188 TEST_F(FormatTest, FormatsAccessModifiers) {
10189   FormatStyle Style = getLLVMStyle();
10190   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10191             FormatStyle::ELBAMS_LogicalBlock);
10192   verifyFormat("struct foo {\n"
10193                "private:\n"
10194                "  void f() {}\n"
10195                "\n"
10196                "private:\n"
10197                "  int i;\n"
10198                "\n"
10199                "protected:\n"
10200                "  int j;\n"
10201                "};\n",
10202                Style);
10203   verifyFormat("struct foo {\n"
10204                "private:\n"
10205                "  void f() {}\n"
10206                "\n"
10207                "private:\n"
10208                "  int i;\n"
10209                "\n"
10210                "protected:\n"
10211                "  int j;\n"
10212                "};\n",
10213                "struct foo {\n"
10214                "private:\n"
10215                "  void f() {}\n"
10216                "private:\n"
10217                "  int i;\n"
10218                "protected:\n"
10219                "  int j;\n"
10220                "};\n",
10221                Style);
10222   verifyFormat("struct foo { /* comment */\n"
10223                "private:\n"
10224                "  int i;\n"
10225                "  // comment\n"
10226                "private:\n"
10227                "  int j;\n"
10228                "};\n",
10229                Style);
10230   verifyFormat("struct foo {\n"
10231                "#ifdef FOO\n"
10232                "#endif\n"
10233                "private:\n"
10234                "  int i;\n"
10235                "#ifdef FOO\n"
10236                "private:\n"
10237                "#endif\n"
10238                "  int j;\n"
10239                "};\n",
10240                Style);
10241   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10242   verifyFormat("struct foo {\n"
10243                "private:\n"
10244                "  void f() {}\n"
10245                "private:\n"
10246                "  int i;\n"
10247                "protected:\n"
10248                "  int j;\n"
10249                "};\n",
10250                Style);
10251   verifyFormat("struct foo {\n"
10252                "private:\n"
10253                "  void f() {}\n"
10254                "private:\n"
10255                "  int i;\n"
10256                "protected:\n"
10257                "  int j;\n"
10258                "};\n",
10259                "struct foo {\n"
10260                "\n"
10261                "private:\n"
10262                "  void f() {}\n"
10263                "\n"
10264                "private:\n"
10265                "  int i;\n"
10266                "\n"
10267                "protected:\n"
10268                "  int j;\n"
10269                "};\n",
10270                Style);
10271   verifyFormat("struct foo { /* comment */\n"
10272                "private:\n"
10273                "  int i;\n"
10274                "  // comment\n"
10275                "private:\n"
10276                "  int j;\n"
10277                "};\n",
10278                "struct foo { /* comment */\n"
10279                "\n"
10280                "private:\n"
10281                "  int i;\n"
10282                "  // comment\n"
10283                "\n"
10284                "private:\n"
10285                "  int j;\n"
10286                "};\n",
10287                Style);
10288   verifyFormat("struct foo {\n"
10289                "#ifdef FOO\n"
10290                "#endif\n"
10291                "private:\n"
10292                "  int i;\n"
10293                "#ifdef FOO\n"
10294                "private:\n"
10295                "#endif\n"
10296                "  int j;\n"
10297                "};\n",
10298                "struct foo {\n"
10299                "#ifdef FOO\n"
10300                "#endif\n"
10301                "\n"
10302                "private:\n"
10303                "  int i;\n"
10304                "#ifdef FOO\n"
10305                "\n"
10306                "private:\n"
10307                "#endif\n"
10308                "  int j;\n"
10309                "};\n",
10310                Style);
10311   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10312   verifyFormat("struct foo {\n"
10313                "private:\n"
10314                "  void f() {}\n"
10315                "\n"
10316                "private:\n"
10317                "  int i;\n"
10318                "\n"
10319                "protected:\n"
10320                "  int j;\n"
10321                "};\n",
10322                Style);
10323   verifyFormat("struct foo {\n"
10324                "private:\n"
10325                "  void f() {}\n"
10326                "\n"
10327                "private:\n"
10328                "  int i;\n"
10329                "\n"
10330                "protected:\n"
10331                "  int j;\n"
10332                "};\n",
10333                "struct foo {\n"
10334                "private:\n"
10335                "  void f() {}\n"
10336                "private:\n"
10337                "  int i;\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                "\n"
10347                "private:\n"
10348                "  int j;\n"
10349                "};\n",
10350                "struct foo { /* comment */\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                "\n"
10363                "private:\n"
10364                "  int i;\n"
10365                "#ifdef FOO\n"
10366                "\n"
10367                "private:\n"
10368                "#endif\n"
10369                "  int j;\n"
10370                "};\n",
10371                "struct foo {\n"
10372                "#ifdef FOO\n"
10373                "#endif\n"
10374                "private:\n"
10375                "  int i;\n"
10376                "#ifdef FOO\n"
10377                "private:\n"
10378                "#endif\n"
10379                "  int j;\n"
10380                "};\n",
10381                Style);
10382   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10383   EXPECT_EQ("struct foo {\n"
10384             "\n"
10385             "private:\n"
10386             "  void f() {}\n"
10387             "\n"
10388             "private:\n"
10389             "  int i;\n"
10390             "\n"
10391             "protected:\n"
10392             "  int j;\n"
10393             "};\n",
10394             format("struct foo {\n"
10395                    "\n"
10396                    "private:\n"
10397                    "  void f() {}\n"
10398                    "\n"
10399                    "private:\n"
10400                    "  int i;\n"
10401                    "\n"
10402                    "protected:\n"
10403                    "  int j;\n"
10404                    "};\n",
10405                    Style));
10406   verifyFormat("struct foo {\n"
10407                "private:\n"
10408                "  void f() {}\n"
10409                "private:\n"
10410                "  int i;\n"
10411                "protected:\n"
10412                "  int j;\n"
10413                "};\n",
10414                Style);
10415   EXPECT_EQ("struct foo { /* comment */\n"
10416             "\n"
10417             "private:\n"
10418             "  int i;\n"
10419             "  // comment\n"
10420             "\n"
10421             "private:\n"
10422             "  int j;\n"
10423             "};\n",
10424             format("struct foo { /* comment */\n"
10425                    "\n"
10426                    "private:\n"
10427                    "  int i;\n"
10428                    "  // comment\n"
10429                    "\n"
10430                    "private:\n"
10431                    "  int j;\n"
10432                    "};\n",
10433                    Style));
10434   verifyFormat("struct foo { /* comment */\n"
10435                "private:\n"
10436                "  int i;\n"
10437                "  // comment\n"
10438                "private:\n"
10439                "  int j;\n"
10440                "};\n",
10441                Style);
10442   EXPECT_EQ("struct foo {\n"
10443             "#ifdef FOO\n"
10444             "#endif\n"
10445             "\n"
10446             "private:\n"
10447             "  int i;\n"
10448             "#ifdef FOO\n"
10449             "\n"
10450             "private:\n"
10451             "#endif\n"
10452             "  int j;\n"
10453             "};\n",
10454             format("struct foo {\n"
10455                    "#ifdef FOO\n"
10456                    "#endif\n"
10457                    "\n"
10458                    "private:\n"
10459                    "  int i;\n"
10460                    "#ifdef FOO\n"
10461                    "\n"
10462                    "private:\n"
10463                    "#endif\n"
10464                    "  int j;\n"
10465                    "};\n",
10466                    Style));
10467   verifyFormat("struct foo {\n"
10468                "#ifdef FOO\n"
10469                "#endif\n"
10470                "private:\n"
10471                "  int i;\n"
10472                "#ifdef FOO\n"
10473                "private:\n"
10474                "#endif\n"
10475                "  int j;\n"
10476                "};\n",
10477                Style);
10478 
10479   FormatStyle NoEmptyLines = getLLVMStyle();
10480   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10481   verifyFormat("struct foo {\n"
10482                "private:\n"
10483                "  void f() {}\n"
10484                "\n"
10485                "private:\n"
10486                "  int i;\n"
10487                "\n"
10488                "public:\n"
10489                "protected:\n"
10490                "  int j;\n"
10491                "};\n",
10492                NoEmptyLines);
10493 
10494   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10495   verifyFormat("struct foo {\n"
10496                "private:\n"
10497                "  void f() {}\n"
10498                "private:\n"
10499                "  int i;\n"
10500                "public:\n"
10501                "protected:\n"
10502                "  int j;\n"
10503                "};\n",
10504                NoEmptyLines);
10505 
10506   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10507   verifyFormat("struct foo {\n"
10508                "private:\n"
10509                "  void f() {}\n"
10510                "\n"
10511                "private:\n"
10512                "  int i;\n"
10513                "\n"
10514                "public:\n"
10515                "\n"
10516                "protected:\n"
10517                "  int j;\n"
10518                "};\n",
10519                NoEmptyLines);
10520 }
10521 
10522 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10523 
10524   FormatStyle Style = getLLVMStyle();
10525   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10526   verifyFormat("struct foo {\n"
10527                "private:\n"
10528                "  void f() {}\n"
10529                "\n"
10530                "private:\n"
10531                "  int i;\n"
10532                "\n"
10533                "protected:\n"
10534                "  int j;\n"
10535                "};\n",
10536                Style);
10537 
10538   // Check if lines are removed.
10539   verifyFormat("struct foo {\n"
10540                "private:\n"
10541                "  void f() {}\n"
10542                "\n"
10543                "private:\n"
10544                "  int i;\n"
10545                "\n"
10546                "protected:\n"
10547                "  int j;\n"
10548                "};\n",
10549                "struct foo {\n"
10550                "private:\n"
10551                "\n"
10552                "  void f() {}\n"
10553                "\n"
10554                "private:\n"
10555                "\n"
10556                "  int i;\n"
10557                "\n"
10558                "protected:\n"
10559                "\n"
10560                "  int j;\n"
10561                "};\n",
10562                Style);
10563 
10564   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10565   verifyFormat("struct foo {\n"
10566                "private:\n"
10567                "\n"
10568                "  void f() {}\n"
10569                "\n"
10570                "private:\n"
10571                "\n"
10572                "  int i;\n"
10573                "\n"
10574                "protected:\n"
10575                "\n"
10576                "  int j;\n"
10577                "};\n",
10578                Style);
10579 
10580   // Check if lines are added.
10581   verifyFormat("struct foo {\n"
10582                "private:\n"
10583                "\n"
10584                "  void f() {}\n"
10585                "\n"
10586                "private:\n"
10587                "\n"
10588                "  int i;\n"
10589                "\n"
10590                "protected:\n"
10591                "\n"
10592                "  int j;\n"
10593                "};\n",
10594                "struct foo {\n"
10595                "private:\n"
10596                "  void f() {}\n"
10597                "\n"
10598                "private:\n"
10599                "  int i;\n"
10600                "\n"
10601                "protected:\n"
10602                "  int j;\n"
10603                "};\n",
10604                Style);
10605 
10606   // Leave tests rely on the code layout, test::messUp can not be used.
10607   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10608   Style.MaxEmptyLinesToKeep = 0u;
10609   verifyFormat("struct foo {\n"
10610                "private:\n"
10611                "  void f() {}\n"
10612                "\n"
10613                "private:\n"
10614                "  int i;\n"
10615                "\n"
10616                "protected:\n"
10617                "  int j;\n"
10618                "};\n",
10619                Style);
10620 
10621   // Check if MaxEmptyLinesToKeep is respected.
10622   EXPECT_EQ("struct foo {\n"
10623             "private:\n"
10624             "  void f() {}\n"
10625             "\n"
10626             "private:\n"
10627             "  int i;\n"
10628             "\n"
10629             "protected:\n"
10630             "  int j;\n"
10631             "};\n",
10632             format("struct foo {\n"
10633                    "private:\n"
10634                    "\n\n\n"
10635                    "  void f() {}\n"
10636                    "\n"
10637                    "private:\n"
10638                    "\n\n\n"
10639                    "  int i;\n"
10640                    "\n"
10641                    "protected:\n"
10642                    "\n\n\n"
10643                    "  int j;\n"
10644                    "};\n",
10645                    Style));
10646 
10647   Style.MaxEmptyLinesToKeep = 1u;
10648   EXPECT_EQ("struct foo {\n"
10649             "private:\n"
10650             "\n"
10651             "  void f() {}\n"
10652             "\n"
10653             "private:\n"
10654             "\n"
10655             "  int i;\n"
10656             "\n"
10657             "protected:\n"
10658             "\n"
10659             "  int j;\n"
10660             "};\n",
10661             format("struct foo {\n"
10662                    "private:\n"
10663                    "\n"
10664                    "  void f() {}\n"
10665                    "\n"
10666                    "private:\n"
10667                    "\n"
10668                    "  int i;\n"
10669                    "\n"
10670                    "protected:\n"
10671                    "\n"
10672                    "  int j;\n"
10673                    "};\n",
10674                    Style));
10675   // Check if no lines are kept.
10676   EXPECT_EQ("struct foo {\n"
10677             "private:\n"
10678             "  void f() {}\n"
10679             "\n"
10680             "private:\n"
10681             "  int i;\n"
10682             "\n"
10683             "protected:\n"
10684             "  int j;\n"
10685             "};\n",
10686             format("struct foo {\n"
10687                    "private:\n"
10688                    "  void f() {}\n"
10689                    "\n"
10690                    "private:\n"
10691                    "  int i;\n"
10692                    "\n"
10693                    "protected:\n"
10694                    "  int j;\n"
10695                    "};\n",
10696                    Style));
10697   // Check if MaxEmptyLinesToKeep is respected.
10698   EXPECT_EQ("struct foo {\n"
10699             "private:\n"
10700             "\n"
10701             "  void f() {}\n"
10702             "\n"
10703             "private:\n"
10704             "\n"
10705             "  int i;\n"
10706             "\n"
10707             "protected:\n"
10708             "\n"
10709             "  int j;\n"
10710             "};\n",
10711             format("struct foo {\n"
10712                    "private:\n"
10713                    "\n\n\n"
10714                    "  void f() {}\n"
10715                    "\n"
10716                    "private:\n"
10717                    "\n\n\n"
10718                    "  int i;\n"
10719                    "\n"
10720                    "protected:\n"
10721                    "\n\n\n"
10722                    "  int j;\n"
10723                    "};\n",
10724                    Style));
10725 
10726   Style.MaxEmptyLinesToKeep = 10u;
10727   EXPECT_EQ("struct foo {\n"
10728             "private:\n"
10729             "\n\n\n"
10730             "  void f() {}\n"
10731             "\n"
10732             "private:\n"
10733             "\n\n\n"
10734             "  int i;\n"
10735             "\n"
10736             "protected:\n"
10737             "\n\n\n"
10738             "  int j;\n"
10739             "};\n",
10740             format("struct foo {\n"
10741                    "private:\n"
10742                    "\n\n\n"
10743                    "  void f() {}\n"
10744                    "\n"
10745                    "private:\n"
10746                    "\n\n\n"
10747                    "  int i;\n"
10748                    "\n"
10749                    "protected:\n"
10750                    "\n\n\n"
10751                    "  int j;\n"
10752                    "};\n",
10753                    Style));
10754 
10755   // Test with comments.
10756   Style = getLLVMStyle();
10757   verifyFormat("struct foo {\n"
10758                "private:\n"
10759                "  // comment\n"
10760                "  void f() {}\n"
10761                "\n"
10762                "private: /* comment */\n"
10763                "  int i;\n"
10764                "};\n",
10765                Style);
10766   verifyFormat("struct foo {\n"
10767                "private:\n"
10768                "  // comment\n"
10769                "  void f() {}\n"
10770                "\n"
10771                "private: /* comment */\n"
10772                "  int i;\n"
10773                "};\n",
10774                "struct foo {\n"
10775                "private:\n"
10776                "\n"
10777                "  // comment\n"
10778                "  void f() {}\n"
10779                "\n"
10780                "private: /* comment */\n"
10781                "\n"
10782                "  int i;\n"
10783                "};\n",
10784                Style);
10785 
10786   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10787   verifyFormat("struct foo {\n"
10788                "private:\n"
10789                "\n"
10790                "  // comment\n"
10791                "  void f() {}\n"
10792                "\n"
10793                "private: /* comment */\n"
10794                "\n"
10795                "  int i;\n"
10796                "};\n",
10797                "struct foo {\n"
10798                "private:\n"
10799                "  // comment\n"
10800                "  void f() {}\n"
10801                "\n"
10802                "private: /* comment */\n"
10803                "  int i;\n"
10804                "};\n",
10805                Style);
10806   verifyFormat("struct foo {\n"
10807                "private:\n"
10808                "\n"
10809                "  // comment\n"
10810                "  void f() {}\n"
10811                "\n"
10812                "private: /* comment */\n"
10813                "\n"
10814                "  int i;\n"
10815                "};\n",
10816                Style);
10817 
10818   // Test with preprocessor defines.
10819   Style = getLLVMStyle();
10820   verifyFormat("struct foo {\n"
10821                "private:\n"
10822                "#ifdef FOO\n"
10823                "#endif\n"
10824                "  void f() {}\n"
10825                "};\n",
10826                Style);
10827   verifyFormat("struct foo {\n"
10828                "private:\n"
10829                "#ifdef FOO\n"
10830                "#endif\n"
10831                "  void f() {}\n"
10832                "};\n",
10833                "struct foo {\n"
10834                "private:\n"
10835                "\n"
10836                "#ifdef FOO\n"
10837                "#endif\n"
10838                "  void f() {}\n"
10839                "};\n",
10840                Style);
10841 
10842   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10843   verifyFormat("struct foo {\n"
10844                "private:\n"
10845                "\n"
10846                "#ifdef FOO\n"
10847                "#endif\n"
10848                "  void f() {}\n"
10849                "};\n",
10850                "struct foo {\n"
10851                "private:\n"
10852                "#ifdef FOO\n"
10853                "#endif\n"
10854                "  void f() {}\n"
10855                "};\n",
10856                Style);
10857   verifyFormat("struct foo {\n"
10858                "private:\n"
10859                "\n"
10860                "#ifdef FOO\n"
10861                "#endif\n"
10862                "  void f() {}\n"
10863                "};\n",
10864                Style);
10865 }
10866 
10867 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
10868   // Combined tests of EmptyLineAfterAccessModifier and
10869   // EmptyLineBeforeAccessModifier.
10870   FormatStyle Style = getLLVMStyle();
10871   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10872   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10873   verifyFormat("struct foo {\n"
10874                "private:\n"
10875                "\n"
10876                "protected:\n"
10877                "};\n",
10878                Style);
10879 
10880   Style.MaxEmptyLinesToKeep = 10u;
10881   // Both remove all new lines.
10882   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10883   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10884   verifyFormat("struct foo {\n"
10885                "private:\n"
10886                "protected:\n"
10887                "};\n",
10888                "struct foo {\n"
10889                "private:\n"
10890                "\n\n\n"
10891                "protected:\n"
10892                "};\n",
10893                Style);
10894 
10895   // Leave tests rely on the code layout, test::messUp can not be used.
10896   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10897   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10898   Style.MaxEmptyLinesToKeep = 10u;
10899   EXPECT_EQ("struct foo {\n"
10900             "private:\n"
10901             "\n\n\n"
10902             "protected:\n"
10903             "};\n",
10904             format("struct foo {\n"
10905                    "private:\n"
10906                    "\n\n\n"
10907                    "protected:\n"
10908                    "};\n",
10909                    Style));
10910   Style.MaxEmptyLinesToKeep = 3u;
10911   EXPECT_EQ("struct foo {\n"
10912             "private:\n"
10913             "\n\n\n"
10914             "protected:\n"
10915             "};\n",
10916             format("struct foo {\n"
10917                    "private:\n"
10918                    "\n\n\n"
10919                    "protected:\n"
10920                    "};\n",
10921                    Style));
10922   Style.MaxEmptyLinesToKeep = 1u;
10923   EXPECT_EQ("struct foo {\n"
10924             "private:\n"
10925             "\n\n\n"
10926             "protected:\n"
10927             "};\n",
10928             format("struct foo {\n"
10929                    "private:\n"
10930                    "\n\n\n"
10931                    "protected:\n"
10932                    "};\n",
10933                    Style)); // Based on new lines in original document and not
10934                             // on the setting.
10935 
10936   Style.MaxEmptyLinesToKeep = 10u;
10937   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10938   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10939   // Newlines are kept if they are greater than zero,
10940   // test::messUp removes all new lines which changes the logic
10941   EXPECT_EQ("struct foo {\n"
10942             "private:\n"
10943             "\n\n\n"
10944             "protected:\n"
10945             "};\n",
10946             format("struct foo {\n"
10947                    "private:\n"
10948                    "\n\n\n"
10949                    "protected:\n"
10950                    "};\n",
10951                    Style));
10952 
10953   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10954   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10955   // test::messUp removes all new lines which changes the logic
10956   EXPECT_EQ("struct foo {\n"
10957             "private:\n"
10958             "\n\n\n"
10959             "protected:\n"
10960             "};\n",
10961             format("struct foo {\n"
10962                    "private:\n"
10963                    "\n\n\n"
10964                    "protected:\n"
10965                    "};\n",
10966                    Style));
10967 
10968   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10969   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
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)); // test::messUp removes all new lines which changes
10981                             // the logic.
10982 
10983   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10984   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10985   verifyFormat("struct foo {\n"
10986                "private:\n"
10987                "protected:\n"
10988                "};\n",
10989                "struct foo {\n"
10990                "private:\n"
10991                "\n\n\n"
10992                "protected:\n"
10993                "};\n",
10994                Style);
10995 
10996   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10997   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10998   EXPECT_EQ("struct foo {\n"
10999             "private:\n"
11000             "\n\n\n"
11001             "protected:\n"
11002             "};\n",
11003             format("struct foo {\n"
11004                    "private:\n"
11005                    "\n\n\n"
11006                    "protected:\n"
11007                    "};\n",
11008                    Style)); // test::messUp removes all new lines which changes
11009                             // the logic.
11010 
11011   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11012   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11013   verifyFormat("struct foo {\n"
11014                "private:\n"
11015                "protected:\n"
11016                "};\n",
11017                "struct foo {\n"
11018                "private:\n"
11019                "\n\n\n"
11020                "protected:\n"
11021                "};\n",
11022                Style);
11023 
11024   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11025   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11026   verifyFormat("struct foo {\n"
11027                "private:\n"
11028                "protected:\n"
11029                "};\n",
11030                "struct foo {\n"
11031                "private:\n"
11032                "\n\n\n"
11033                "protected:\n"
11034                "};\n",
11035                Style);
11036 
11037   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11038   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11039   verifyFormat("struct foo {\n"
11040                "private:\n"
11041                "protected:\n"
11042                "};\n",
11043                "struct foo {\n"
11044                "private:\n"
11045                "\n\n\n"
11046                "protected:\n"
11047                "};\n",
11048                Style);
11049 
11050   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11051   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11052   verifyFormat("struct foo {\n"
11053                "private:\n"
11054                "protected:\n"
11055                "};\n",
11056                "struct foo {\n"
11057                "private:\n"
11058                "\n\n\n"
11059                "protected:\n"
11060                "};\n",
11061                Style);
11062 }
11063 
11064 TEST_F(FormatTest, FormatsArrays) {
11065   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11066                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11067   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11068                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11069   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11070                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11071   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11072                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11073   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11074                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11075   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11076                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11077                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11078   verifyFormat(
11079       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11080       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11081       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11082   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11083                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11084 
11085   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11086                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11087   verifyFormat(
11088       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11089       "                                  .aaaaaaa[0]\n"
11090       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11091   verifyFormat("a[::b::c];");
11092 
11093   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11094 
11095   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11096   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11097 }
11098 
11099 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11100   verifyFormat("(a)->b();");
11101   verifyFormat("--a;");
11102 }
11103 
11104 TEST_F(FormatTest, HandlesIncludeDirectives) {
11105   verifyFormat("#include <string>\n"
11106                "#include <a/b/c.h>\n"
11107                "#include \"a/b/string\"\n"
11108                "#include \"string.h\"\n"
11109                "#include \"string.h\"\n"
11110                "#include <a-a>\n"
11111                "#include < path with space >\n"
11112                "#include_next <test.h>"
11113                "#include \"abc.h\" // this is included for ABC\n"
11114                "#include \"some long include\" // with a comment\n"
11115                "#include \"some very long include path\"\n"
11116                "#include <some/very/long/include/path>\n",
11117                getLLVMStyleWithColumns(35));
11118   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11119   EXPECT_EQ("#include <a>", format("#include<a>"));
11120 
11121   verifyFormat("#import <string>");
11122   verifyFormat("#import <a/b/c.h>");
11123   verifyFormat("#import \"a/b/string\"");
11124   verifyFormat("#import \"string.h\"");
11125   verifyFormat("#import \"string.h\"");
11126   verifyFormat("#if __has_include(<strstream>)\n"
11127                "#include <strstream>\n"
11128                "#endif");
11129 
11130   verifyFormat("#define MY_IMPORT <a/b>");
11131 
11132   verifyFormat("#if __has_include(<a/b>)");
11133   verifyFormat("#if __has_include_next(<a/b>)");
11134   verifyFormat("#define F __has_include(<a/b>)");
11135   verifyFormat("#define F __has_include_next(<a/b>)");
11136 
11137   // Protocol buffer definition or missing "#".
11138   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11139                getLLVMStyleWithColumns(30));
11140 
11141   FormatStyle Style = getLLVMStyle();
11142   Style.AlwaysBreakBeforeMultilineStrings = true;
11143   Style.ColumnLimit = 0;
11144   verifyFormat("#import \"abc.h\"", Style);
11145 
11146   // But 'import' might also be a regular C++ namespace.
11147   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11148                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11149 }
11150 
11151 //===----------------------------------------------------------------------===//
11152 // Error recovery tests.
11153 //===----------------------------------------------------------------------===//
11154 
11155 TEST_F(FormatTest, IncompleteParameterLists) {
11156   FormatStyle NoBinPacking = getLLVMStyle();
11157   NoBinPacking.BinPackParameters = false;
11158   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11159                "                        double *min_x,\n"
11160                "                        double *max_x,\n"
11161                "                        double *min_y,\n"
11162                "                        double *max_y,\n"
11163                "                        double *min_z,\n"
11164                "                        double *max_z, ) {}",
11165                NoBinPacking);
11166 }
11167 
11168 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11169   verifyFormat("void f() { return; }\n42");
11170   verifyFormat("void f() {\n"
11171                "  if (0)\n"
11172                "    return;\n"
11173                "}\n"
11174                "42");
11175   verifyFormat("void f() { return }\n42");
11176   verifyFormat("void f() {\n"
11177                "  if (0)\n"
11178                "    return\n"
11179                "}\n"
11180                "42");
11181 }
11182 
11183 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11184   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11185   EXPECT_EQ("void f() {\n"
11186             "  if (a)\n"
11187             "    return\n"
11188             "}",
11189             format("void  f  (  )  {  if  ( a )  return  }"));
11190   EXPECT_EQ("namespace N {\n"
11191             "void f()\n"
11192             "}",
11193             format("namespace  N  {  void f()  }"));
11194   EXPECT_EQ("namespace N {\n"
11195             "void f() {}\n"
11196             "void g()\n"
11197             "} // namespace N",
11198             format("namespace N  { void f( ) { } void g( ) }"));
11199 }
11200 
11201 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11202   verifyFormat("int aaaaaaaa =\n"
11203                "    // Overlylongcomment\n"
11204                "    b;",
11205                getLLVMStyleWithColumns(20));
11206   verifyFormat("function(\n"
11207                "    ShortArgument,\n"
11208                "    LoooooooooooongArgument);\n",
11209                getLLVMStyleWithColumns(20));
11210 }
11211 
11212 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11213   verifyFormat("public:");
11214   verifyFormat("class A {\n"
11215                "public\n"
11216                "  void f() {}\n"
11217                "};");
11218   verifyFormat("public\n"
11219                "int qwerty;");
11220   verifyFormat("public\n"
11221                "B {}");
11222   verifyFormat("public\n"
11223                "{}");
11224   verifyFormat("public\n"
11225                "B { int x; }");
11226 }
11227 
11228 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11229   verifyFormat("{");
11230   verifyFormat("#})");
11231   verifyNoCrash("(/**/[:!] ?[).");
11232 }
11233 
11234 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11235   // Found by oss-fuzz:
11236   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11237   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11238   Style.ColumnLimit = 60;
11239   verifyNoCrash(
11240       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11241       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11242       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11243       Style);
11244 }
11245 
11246 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11247   verifyFormat("do {\n}");
11248   verifyFormat("do {\n}\n"
11249                "f();");
11250   verifyFormat("do {\n}\n"
11251                "wheeee(fun);");
11252   verifyFormat("do {\n"
11253                "  f();\n"
11254                "}");
11255 }
11256 
11257 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11258   verifyFormat("if {\n  foo;\n  foo();\n}");
11259   verifyFormat("switch {\n  foo;\n  foo();\n}");
11260   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11261   verifyFormat("while {\n  foo;\n  foo();\n}");
11262   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11263 }
11264 
11265 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11266   verifyIncompleteFormat("namespace {\n"
11267                          "class Foo { Foo (\n"
11268                          "};\n"
11269                          "} // namespace");
11270 }
11271 
11272 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11273   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11274   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11275   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11276   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11277 
11278   EXPECT_EQ("{\n"
11279             "  {\n"
11280             "    breakme(\n"
11281             "        qwe);\n"
11282             "  }\n",
11283             format("{\n"
11284                    "    {\n"
11285                    " breakme(qwe);\n"
11286                    "}\n",
11287                    getLLVMStyleWithColumns(10)));
11288 }
11289 
11290 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11291   verifyFormat("int x = {\n"
11292                "    avariable,\n"
11293                "    b(alongervariable)};",
11294                getLLVMStyleWithColumns(25));
11295 }
11296 
11297 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11298   verifyFormat("return (a)(b){1, 2, 3};");
11299 }
11300 
11301 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11302   verifyFormat("vector<int> x{1, 2, 3, 4};");
11303   verifyFormat("vector<int> x{\n"
11304                "    1,\n"
11305                "    2,\n"
11306                "    3,\n"
11307                "    4,\n"
11308                "};");
11309   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11310   verifyFormat("f({1, 2});");
11311   verifyFormat("auto v = Foo{-1};");
11312   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11313   verifyFormat("Class::Class : member{1, 2, 3} {}");
11314   verifyFormat("new vector<int>{1, 2, 3};");
11315   verifyFormat("new int[3]{1, 2, 3};");
11316   verifyFormat("new int{1};");
11317   verifyFormat("return {arg1, arg2};");
11318   verifyFormat("return {arg1, SomeType{parameter}};");
11319   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11320   verifyFormat("new T{arg1, arg2};");
11321   verifyFormat("f(MyMap[{composite, key}]);");
11322   verifyFormat("class Class {\n"
11323                "  T member = {arg1, arg2};\n"
11324                "};");
11325   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11326   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11327   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11328   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11329   verifyFormat("int a = std::is_integral<int>{} + 0;");
11330 
11331   verifyFormat("int foo(int i) { return fo1{}(i); }");
11332   verifyFormat("int foo(int i) { return fo1{}(i); }");
11333   verifyFormat("auto i = decltype(x){};");
11334   verifyFormat("auto i = typeof(x){};");
11335   verifyFormat("auto i = _Atomic(x){};");
11336   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11337   verifyFormat("Node n{1, Node{1000}, //\n"
11338                "       2};");
11339   verifyFormat("Aaaa aaaaaaa{\n"
11340                "    {\n"
11341                "        aaaa,\n"
11342                "    },\n"
11343                "};");
11344   verifyFormat("class C : public D {\n"
11345                "  SomeClass SC{2};\n"
11346                "};");
11347   verifyFormat("class C : public A {\n"
11348                "  class D : public B {\n"
11349                "    void f() { int i{2}; }\n"
11350                "  };\n"
11351                "};");
11352   verifyFormat("#define A {a, a},");
11353 
11354   // Avoid breaking between equal sign and opening brace
11355   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11356   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11357   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11358                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11359                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11360                "     {\"ccccccccccccccccccccc\", 2}};",
11361                AvoidBreakingFirstArgument);
11362 
11363   // Binpacking only if there is no trailing comma
11364   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11365                "                      cccccccccc, dddddddddd};",
11366                getLLVMStyleWithColumns(50));
11367   verifyFormat("const Aaaaaa aaaaa = {\n"
11368                "    aaaaaaaaaaa,\n"
11369                "    bbbbbbbbbbb,\n"
11370                "    ccccccccccc,\n"
11371                "    ddddddddddd,\n"
11372                "};",
11373                getLLVMStyleWithColumns(50));
11374 
11375   // Cases where distinguising braced lists and blocks is hard.
11376   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11377   verifyFormat("void f() {\n"
11378                "  return; // comment\n"
11379                "}\n"
11380                "SomeType t;");
11381   verifyFormat("void f() {\n"
11382                "  if (a) {\n"
11383                "    f();\n"
11384                "  }\n"
11385                "}\n"
11386                "SomeType t;");
11387 
11388   // In combination with BinPackArguments = false.
11389   FormatStyle NoBinPacking = getLLVMStyle();
11390   NoBinPacking.BinPackArguments = false;
11391   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11392                "                      bbbbb,\n"
11393                "                      ccccc,\n"
11394                "                      ddddd,\n"
11395                "                      eeeee,\n"
11396                "                      ffffff,\n"
11397                "                      ggggg,\n"
11398                "                      hhhhhh,\n"
11399                "                      iiiiii,\n"
11400                "                      jjjjjj,\n"
11401                "                      kkkkkk};",
11402                NoBinPacking);
11403   verifyFormat("const Aaaaaa aaaaa = {\n"
11404                "    aaaaa,\n"
11405                "    bbbbb,\n"
11406                "    ccccc,\n"
11407                "    ddddd,\n"
11408                "    eeeee,\n"
11409                "    ffffff,\n"
11410                "    ggggg,\n"
11411                "    hhhhhh,\n"
11412                "    iiiiii,\n"
11413                "    jjjjjj,\n"
11414                "    kkkkkk,\n"
11415                "};",
11416                NoBinPacking);
11417   verifyFormat(
11418       "const Aaaaaa aaaaa = {\n"
11419       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11420       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11421       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11422       "};",
11423       NoBinPacking);
11424 
11425   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11426   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11427             "    CDDDP83848_BMCR_REGISTER,\n"
11428             "    CDDDP83848_BMSR_REGISTER,\n"
11429             "    CDDDP83848_RBR_REGISTER};",
11430             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11431                    "                                CDDDP83848_BMSR_REGISTER,\n"
11432                    "                                CDDDP83848_RBR_REGISTER};",
11433                    NoBinPacking));
11434 
11435   // FIXME: The alignment of these trailing comments might be bad. Then again,
11436   // this might be utterly useless in real code.
11437   verifyFormat("Constructor::Constructor()\n"
11438                "    : some_value{         //\n"
11439                "                 aaaaaaa, //\n"
11440                "                 bbbbbbb} {}");
11441 
11442   // In braced lists, the first comment is always assumed to belong to the
11443   // first element. Thus, it can be moved to the next or previous line as
11444   // appropriate.
11445   EXPECT_EQ("function({// First element:\n"
11446             "          1,\n"
11447             "          // Second element:\n"
11448             "          2});",
11449             format("function({\n"
11450                    "    // First element:\n"
11451                    "    1,\n"
11452                    "    // Second element:\n"
11453                    "    2});"));
11454   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11455             "    // First element:\n"
11456             "    1,\n"
11457             "    // Second element:\n"
11458             "    2};",
11459             format("std::vector<int> MyNumbers{// First element:\n"
11460                    "                           1,\n"
11461                    "                           // Second element:\n"
11462                    "                           2};",
11463                    getLLVMStyleWithColumns(30)));
11464   // A trailing comma should still lead to an enforced line break and no
11465   // binpacking.
11466   EXPECT_EQ("vector<int> SomeVector = {\n"
11467             "    // aaa\n"
11468             "    1,\n"
11469             "    2,\n"
11470             "};",
11471             format("vector<int> SomeVector = { // aaa\n"
11472                    "    1, 2, };"));
11473 
11474   // C++11 brace initializer list l-braces should not be treated any differently
11475   // when breaking before lambda bodies is enabled
11476   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11477   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11478   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11479   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11480   verifyFormat(
11481       "std::runtime_error{\n"
11482       "    \"Long string which will force a break onto the next line...\"};",
11483       BreakBeforeLambdaBody);
11484 
11485   FormatStyle ExtraSpaces = getLLVMStyle();
11486   ExtraSpaces.Cpp11BracedListStyle = false;
11487   ExtraSpaces.ColumnLimit = 75;
11488   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11489   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11490   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11491   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11492   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11493   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11494   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11495   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11496   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11497   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11498   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11499   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11500   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11501   verifyFormat("class Class {\n"
11502                "  T member = { arg1, arg2 };\n"
11503                "};",
11504                ExtraSpaces);
11505   verifyFormat(
11506       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11507       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11508       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11509       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11510       ExtraSpaces);
11511   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11512   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11513                ExtraSpaces);
11514   verifyFormat(
11515       "someFunction(OtherParam,\n"
11516       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11517       "                         param1, param2,\n"
11518       "                         // comment 2\n"
11519       "                         param3, param4 });",
11520       ExtraSpaces);
11521   verifyFormat(
11522       "std::this_thread::sleep_for(\n"
11523       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11524       ExtraSpaces);
11525   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11526                "    aaaaaaa,\n"
11527                "    aaaaaaaaaa,\n"
11528                "    aaaaa,\n"
11529                "    aaaaaaaaaaaaaaa,\n"
11530                "    aaa,\n"
11531                "    aaaaaaaaaa,\n"
11532                "    a,\n"
11533                "    aaaaaaaaaaaaaaaaaaaaa,\n"
11534                "    aaaaaaaaaaaa,\n"
11535                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
11536                "    aaaaaaa,\n"
11537                "    a};");
11538   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
11539   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
11540   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
11541 
11542   // Avoid breaking between initializer/equal sign and opening brace
11543   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
11544   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
11545                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11546                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11547                "  { \"ccccccccccccccccccccc\", 2 }\n"
11548                "};",
11549                ExtraSpaces);
11550   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
11551                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11552                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11553                "  { \"ccccccccccccccccccccc\", 2 }\n"
11554                "};",
11555                ExtraSpaces);
11556 
11557   FormatStyle SpaceBeforeBrace = getLLVMStyle();
11558   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
11559   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
11560   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
11561 
11562   FormatStyle SpaceBetweenBraces = getLLVMStyle();
11563   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
11564   SpaceBetweenBraces.SpacesInParentheses = true;
11565   SpaceBetweenBraces.SpacesInSquareBrackets = true;
11566   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
11567   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
11568   verifyFormat("vector< int > x{ // comment 1\n"
11569                "                 1, 2, 3, 4 };",
11570                SpaceBetweenBraces);
11571   SpaceBetweenBraces.ColumnLimit = 20;
11572   EXPECT_EQ("vector< int > x{\n"
11573             "    1, 2, 3, 4 };",
11574             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11575   SpaceBetweenBraces.ColumnLimit = 24;
11576   EXPECT_EQ("vector< int > x{ 1, 2,\n"
11577             "                 3, 4 };",
11578             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11579   EXPECT_EQ("vector< int > x{\n"
11580             "    1,\n"
11581             "    2,\n"
11582             "    3,\n"
11583             "    4,\n"
11584             "};",
11585             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
11586   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
11587   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
11588   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
11589 }
11590 
11591 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
11592   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11593                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11594                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11595                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11596                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11597                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11598   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
11599                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11600                "                 1, 22, 333, 4444, 55555, //\n"
11601                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11602                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11603   verifyFormat(
11604       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11605       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11606       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
11607       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11608       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11609       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11610       "                 7777777};");
11611   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11612                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11613                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11614   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11615                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11616                "    // Separating comment.\n"
11617                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
11618   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11619                "    // Leading comment\n"
11620                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11621                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11622   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11623                "                 1, 1, 1, 1};",
11624                getLLVMStyleWithColumns(39));
11625   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11626                "                 1, 1, 1, 1};",
11627                getLLVMStyleWithColumns(38));
11628   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
11629                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
11630                getLLVMStyleWithColumns(43));
11631   verifyFormat(
11632       "static unsigned SomeValues[10][3] = {\n"
11633       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
11634       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
11635   verifyFormat("static auto fields = new vector<string>{\n"
11636                "    \"aaaaaaaaaaaaa\",\n"
11637                "    \"aaaaaaaaaaaaa\",\n"
11638                "    \"aaaaaaaaaaaa\",\n"
11639                "    \"aaaaaaaaaaaaaa\",\n"
11640                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11641                "    \"aaaaaaaaaaaa\",\n"
11642                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11643                "};");
11644   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
11645   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
11646                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
11647                "                 3, cccccccccccccccccccccc};",
11648                getLLVMStyleWithColumns(60));
11649 
11650   // Trailing commas.
11651   verifyFormat("vector<int> x = {\n"
11652                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
11653                "};",
11654                getLLVMStyleWithColumns(39));
11655   verifyFormat("vector<int> x = {\n"
11656                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
11657                "};",
11658                getLLVMStyleWithColumns(39));
11659   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11660                "                 1, 1, 1, 1,\n"
11661                "                 /**/ /**/};",
11662                getLLVMStyleWithColumns(39));
11663 
11664   // Trailing comment in the first line.
11665   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
11666                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
11667                "    111111111,  222222222,  3333333333,  444444444,  //\n"
11668                "    11111111,   22222222,   333333333,   44444444};");
11669   // Trailing comment in the last line.
11670   verifyFormat("int aaaaa[] = {\n"
11671                "    1, 2, 3, // comment\n"
11672                "    4, 5, 6  // comment\n"
11673                "};");
11674 
11675   // With nested lists, we should either format one item per line or all nested
11676   // lists one on line.
11677   // FIXME: For some nested lists, we can do better.
11678   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
11679                "        {aaaaaaaaaaaaaaaaaaa},\n"
11680                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
11681                "        {aaaaaaaaaaaaaaaaa}};",
11682                getLLVMStyleWithColumns(60));
11683   verifyFormat(
11684       "SomeStruct my_struct_array = {\n"
11685       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
11686       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
11687       "    {aaa, aaa},\n"
11688       "    {aaa, aaa},\n"
11689       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
11690       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
11691       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
11692 
11693   // No column layout should be used here.
11694   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
11695                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
11696 
11697   verifyNoCrash("a<,");
11698 
11699   // No braced initializer here.
11700   verifyFormat("void f() {\n"
11701                "  struct Dummy {};\n"
11702                "  f(v);\n"
11703                "}");
11704 
11705   // Long lists should be formatted in columns even if they are nested.
11706   verifyFormat(
11707       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11708       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11709       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11710       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11711       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11712       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
11713 
11714   // Allow "single-column" layout even if that violates the column limit. There
11715   // isn't going to be a better way.
11716   verifyFormat("std::vector<int> a = {\n"
11717                "    aaaaaaaa,\n"
11718                "    aaaaaaaa,\n"
11719                "    aaaaaaaa,\n"
11720                "    aaaaaaaa,\n"
11721                "    aaaaaaaaaa,\n"
11722                "    aaaaaaaa,\n"
11723                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
11724                getLLVMStyleWithColumns(30));
11725   verifyFormat("vector<int> aaaa = {\n"
11726                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11727                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11728                "    aaaaaa.aaaaaaa,\n"
11729                "    aaaaaa.aaaaaaa,\n"
11730                "    aaaaaa.aaaaaaa,\n"
11731                "    aaaaaa.aaaaaaa,\n"
11732                "};");
11733 
11734   // Don't create hanging lists.
11735   verifyFormat("someFunction(Param, {List1, List2,\n"
11736                "                     List3});",
11737                getLLVMStyleWithColumns(35));
11738   verifyFormat("someFunction(Param, Param,\n"
11739                "             {List1, List2,\n"
11740                "              List3});",
11741                getLLVMStyleWithColumns(35));
11742   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
11743                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
11744 }
11745 
11746 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
11747   FormatStyle DoNotMerge = getLLVMStyle();
11748   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11749 
11750   verifyFormat("void f() { return 42; }");
11751   verifyFormat("void f() {\n"
11752                "  return 42;\n"
11753                "}",
11754                DoNotMerge);
11755   verifyFormat("void f() {\n"
11756                "  // Comment\n"
11757                "}");
11758   verifyFormat("{\n"
11759                "#error {\n"
11760                "  int a;\n"
11761                "}");
11762   verifyFormat("{\n"
11763                "  int a;\n"
11764                "#error {\n"
11765                "}");
11766   verifyFormat("void f() {} // comment");
11767   verifyFormat("void f() { int a; } // comment");
11768   verifyFormat("void f() {\n"
11769                "} // comment",
11770                DoNotMerge);
11771   verifyFormat("void f() {\n"
11772                "  int a;\n"
11773                "} // comment",
11774                DoNotMerge);
11775   verifyFormat("void f() {\n"
11776                "} // comment",
11777                getLLVMStyleWithColumns(15));
11778 
11779   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
11780   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
11781 
11782   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
11783   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
11784   verifyFormat("class C {\n"
11785                "  C()\n"
11786                "      : iiiiiiii(nullptr),\n"
11787                "        kkkkkkk(nullptr),\n"
11788                "        mmmmmmm(nullptr),\n"
11789                "        nnnnnnn(nullptr) {}\n"
11790                "};",
11791                getGoogleStyle());
11792 
11793   FormatStyle NoColumnLimit = getLLVMStyle();
11794   NoColumnLimit.ColumnLimit = 0;
11795   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
11796   EXPECT_EQ("class C {\n"
11797             "  A() : b(0) {}\n"
11798             "};",
11799             format("class C{A():b(0){}};", NoColumnLimit));
11800   EXPECT_EQ("A()\n"
11801             "    : b(0) {\n"
11802             "}",
11803             format("A()\n:b(0)\n{\n}", NoColumnLimit));
11804 
11805   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
11806   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
11807       FormatStyle::SFS_None;
11808   EXPECT_EQ("A()\n"
11809             "    : b(0) {\n"
11810             "}",
11811             format("A():b(0){}", DoNotMergeNoColumnLimit));
11812   EXPECT_EQ("A()\n"
11813             "    : b(0) {\n"
11814             "}",
11815             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
11816 
11817   verifyFormat("#define A          \\\n"
11818                "  void f() {       \\\n"
11819                "    int i;         \\\n"
11820                "  }",
11821                getLLVMStyleWithColumns(20));
11822   verifyFormat("#define A           \\\n"
11823                "  void f() { int i; }",
11824                getLLVMStyleWithColumns(21));
11825   verifyFormat("#define A            \\\n"
11826                "  void f() {         \\\n"
11827                "    int i;           \\\n"
11828                "  }                  \\\n"
11829                "  int j;",
11830                getLLVMStyleWithColumns(22));
11831   verifyFormat("#define A             \\\n"
11832                "  void f() { int i; } \\\n"
11833                "  int j;",
11834                getLLVMStyleWithColumns(23));
11835 }
11836 
11837 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
11838   FormatStyle MergeEmptyOnly = getLLVMStyle();
11839   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11840   verifyFormat("class C {\n"
11841                "  int f() {}\n"
11842                "};",
11843                MergeEmptyOnly);
11844   verifyFormat("class C {\n"
11845                "  int f() {\n"
11846                "    return 42;\n"
11847                "  }\n"
11848                "};",
11849                MergeEmptyOnly);
11850   verifyFormat("int f() {}", MergeEmptyOnly);
11851   verifyFormat("int f() {\n"
11852                "  return 42;\n"
11853                "}",
11854                MergeEmptyOnly);
11855 
11856   // Also verify behavior when BraceWrapping.AfterFunction = true
11857   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11858   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
11859   verifyFormat("int f() {}", MergeEmptyOnly);
11860   verifyFormat("class C {\n"
11861                "  int f() {}\n"
11862                "};",
11863                MergeEmptyOnly);
11864 }
11865 
11866 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
11867   FormatStyle MergeInlineOnly = getLLVMStyle();
11868   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11869   verifyFormat("class C {\n"
11870                "  int f() { return 42; }\n"
11871                "};",
11872                MergeInlineOnly);
11873   verifyFormat("int f() {\n"
11874                "  return 42;\n"
11875                "}",
11876                MergeInlineOnly);
11877 
11878   // SFS_Inline implies SFS_Empty
11879   verifyFormat("class C {\n"
11880                "  int f() {}\n"
11881                "};",
11882                MergeInlineOnly);
11883   verifyFormat("int f() {}", MergeInlineOnly);
11884 
11885   // Also verify behavior when BraceWrapping.AfterFunction = true
11886   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11887   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11888   verifyFormat("class C {\n"
11889                "  int f() { return 42; }\n"
11890                "};",
11891                MergeInlineOnly);
11892   verifyFormat("int f()\n"
11893                "{\n"
11894                "  return 42;\n"
11895                "}",
11896                MergeInlineOnly);
11897 
11898   // SFS_Inline implies SFS_Empty
11899   verifyFormat("int f() {}", MergeInlineOnly);
11900   verifyFormat("class C {\n"
11901                "  int f() {}\n"
11902                "};",
11903                MergeInlineOnly);
11904 }
11905 
11906 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
11907   FormatStyle MergeInlineOnly = getLLVMStyle();
11908   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
11909       FormatStyle::SFS_InlineOnly;
11910   verifyFormat("class C {\n"
11911                "  int f() { return 42; }\n"
11912                "};",
11913                MergeInlineOnly);
11914   verifyFormat("int f() {\n"
11915                "  return 42;\n"
11916                "}",
11917                MergeInlineOnly);
11918 
11919   // SFS_InlineOnly does not imply SFS_Empty
11920   verifyFormat("class C {\n"
11921                "  int f() {}\n"
11922                "};",
11923                MergeInlineOnly);
11924   verifyFormat("int f() {\n"
11925                "}",
11926                MergeInlineOnly);
11927 
11928   // Also verify behavior when BraceWrapping.AfterFunction = true
11929   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11930   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11931   verifyFormat("class C {\n"
11932                "  int f() { return 42; }\n"
11933                "};",
11934                MergeInlineOnly);
11935   verifyFormat("int f()\n"
11936                "{\n"
11937                "  return 42;\n"
11938                "}",
11939                MergeInlineOnly);
11940 
11941   // SFS_InlineOnly does not imply SFS_Empty
11942   verifyFormat("int f()\n"
11943                "{\n"
11944                "}",
11945                MergeInlineOnly);
11946   verifyFormat("class C {\n"
11947                "  int f() {}\n"
11948                "};",
11949                MergeInlineOnly);
11950 }
11951 
11952 TEST_F(FormatTest, SplitEmptyFunction) {
11953   FormatStyle Style = getLLVMStyle();
11954   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11955   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11956   Style.BraceWrapping.AfterFunction = true;
11957   Style.BraceWrapping.SplitEmptyFunction = false;
11958   Style.ColumnLimit = 40;
11959 
11960   verifyFormat("int f()\n"
11961                "{}",
11962                Style);
11963   verifyFormat("int f()\n"
11964                "{\n"
11965                "  return 42;\n"
11966                "}",
11967                Style);
11968   verifyFormat("int f()\n"
11969                "{\n"
11970                "  // some comment\n"
11971                "}",
11972                Style);
11973 
11974   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11975   verifyFormat("int f() {}", Style);
11976   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11977                "{}",
11978                Style);
11979   verifyFormat("int f()\n"
11980                "{\n"
11981                "  return 0;\n"
11982                "}",
11983                Style);
11984 
11985   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11986   verifyFormat("class Foo {\n"
11987                "  int f() {}\n"
11988                "};\n",
11989                Style);
11990   verifyFormat("class Foo {\n"
11991                "  int f() { return 0; }\n"
11992                "};\n",
11993                Style);
11994   verifyFormat("class Foo {\n"
11995                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11996                "  {}\n"
11997                "};\n",
11998                Style);
11999   verifyFormat("class Foo {\n"
12000                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12001                "  {\n"
12002                "    return 0;\n"
12003                "  }\n"
12004                "};\n",
12005                Style);
12006 
12007   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12008   verifyFormat("int f() {}", Style);
12009   verifyFormat("int f() { return 0; }", Style);
12010   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12011                "{}",
12012                Style);
12013   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12014                "{\n"
12015                "  return 0;\n"
12016                "}",
12017                Style);
12018 }
12019 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12020   FormatStyle Style = getLLVMStyle();
12021   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12022   verifyFormat("#ifdef A\n"
12023                "int f() {}\n"
12024                "#else\n"
12025                "int g() {}\n"
12026                "#endif",
12027                Style);
12028 }
12029 
12030 TEST_F(FormatTest, SplitEmptyClass) {
12031   FormatStyle Style = getLLVMStyle();
12032   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12033   Style.BraceWrapping.AfterClass = true;
12034   Style.BraceWrapping.SplitEmptyRecord = false;
12035 
12036   verifyFormat("class Foo\n"
12037                "{};",
12038                Style);
12039   verifyFormat("/* something */ class Foo\n"
12040                "{};",
12041                Style);
12042   verifyFormat("template <typename X> class Foo\n"
12043                "{};",
12044                Style);
12045   verifyFormat("class Foo\n"
12046                "{\n"
12047                "  Foo();\n"
12048                "};",
12049                Style);
12050   verifyFormat("typedef class Foo\n"
12051                "{\n"
12052                "} Foo_t;",
12053                Style);
12054 
12055   Style.BraceWrapping.SplitEmptyRecord = true;
12056   Style.BraceWrapping.AfterStruct = true;
12057   verifyFormat("class rep\n"
12058                "{\n"
12059                "};",
12060                Style);
12061   verifyFormat("struct rep\n"
12062                "{\n"
12063                "};",
12064                Style);
12065   verifyFormat("template <typename T> class rep\n"
12066                "{\n"
12067                "};",
12068                Style);
12069   verifyFormat("template <typename T> struct rep\n"
12070                "{\n"
12071                "};",
12072                Style);
12073   verifyFormat("class rep\n"
12074                "{\n"
12075                "  int x;\n"
12076                "};",
12077                Style);
12078   verifyFormat("struct rep\n"
12079                "{\n"
12080                "  int x;\n"
12081                "};",
12082                Style);
12083   verifyFormat("template <typename T> class rep\n"
12084                "{\n"
12085                "  int x;\n"
12086                "};",
12087                Style);
12088   verifyFormat("template <typename T> struct rep\n"
12089                "{\n"
12090                "  int x;\n"
12091                "};",
12092                Style);
12093   verifyFormat("template <typename T> class rep // Foo\n"
12094                "{\n"
12095                "  int x;\n"
12096                "};",
12097                Style);
12098   verifyFormat("template <typename T> struct rep // Bar\n"
12099                "{\n"
12100                "  int x;\n"
12101                "};",
12102                Style);
12103 
12104   verifyFormat("template <typename T> class rep<T>\n"
12105                "{\n"
12106                "  int x;\n"
12107                "};",
12108                Style);
12109 
12110   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12111                "{\n"
12112                "  int x;\n"
12113                "};",
12114                Style);
12115   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12116                "{\n"
12117                "};",
12118                Style);
12119 
12120   verifyFormat("#include \"stdint.h\"\n"
12121                "namespace rep {}",
12122                Style);
12123   verifyFormat("#include <stdint.h>\n"
12124                "namespace rep {}",
12125                Style);
12126   verifyFormat("#include <stdint.h>\n"
12127                "namespace rep {}",
12128                "#include <stdint.h>\n"
12129                "namespace rep {\n"
12130                "\n"
12131                "\n"
12132                "}",
12133                Style);
12134 }
12135 
12136 TEST_F(FormatTest, SplitEmptyStruct) {
12137   FormatStyle Style = getLLVMStyle();
12138   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12139   Style.BraceWrapping.AfterStruct = true;
12140   Style.BraceWrapping.SplitEmptyRecord = false;
12141 
12142   verifyFormat("struct Foo\n"
12143                "{};",
12144                Style);
12145   verifyFormat("/* something */ struct Foo\n"
12146                "{};",
12147                Style);
12148   verifyFormat("template <typename X> struct Foo\n"
12149                "{};",
12150                Style);
12151   verifyFormat("struct Foo\n"
12152                "{\n"
12153                "  Foo();\n"
12154                "};",
12155                Style);
12156   verifyFormat("typedef struct Foo\n"
12157                "{\n"
12158                "} Foo_t;",
12159                Style);
12160   // typedef struct Bar {} Bar_t;
12161 }
12162 
12163 TEST_F(FormatTest, SplitEmptyUnion) {
12164   FormatStyle Style = getLLVMStyle();
12165   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12166   Style.BraceWrapping.AfterUnion = true;
12167   Style.BraceWrapping.SplitEmptyRecord = false;
12168 
12169   verifyFormat("union Foo\n"
12170                "{};",
12171                Style);
12172   verifyFormat("/* something */ union Foo\n"
12173                "{};",
12174                Style);
12175   verifyFormat("union Foo\n"
12176                "{\n"
12177                "  A,\n"
12178                "};",
12179                Style);
12180   verifyFormat("typedef union Foo\n"
12181                "{\n"
12182                "} Foo_t;",
12183                Style);
12184 }
12185 
12186 TEST_F(FormatTest, SplitEmptyNamespace) {
12187   FormatStyle Style = getLLVMStyle();
12188   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12189   Style.BraceWrapping.AfterNamespace = true;
12190   Style.BraceWrapping.SplitEmptyNamespace = false;
12191 
12192   verifyFormat("namespace Foo\n"
12193                "{};",
12194                Style);
12195   verifyFormat("/* something */ namespace Foo\n"
12196                "{};",
12197                Style);
12198   verifyFormat("inline namespace Foo\n"
12199                "{};",
12200                Style);
12201   verifyFormat("/* something */ inline namespace Foo\n"
12202                "{};",
12203                Style);
12204   verifyFormat("export namespace Foo\n"
12205                "{};",
12206                Style);
12207   verifyFormat("namespace Foo\n"
12208                "{\n"
12209                "void Bar();\n"
12210                "};",
12211                Style);
12212 }
12213 
12214 TEST_F(FormatTest, NeverMergeShortRecords) {
12215   FormatStyle Style = getLLVMStyle();
12216 
12217   verifyFormat("class Foo {\n"
12218                "  Foo();\n"
12219                "};",
12220                Style);
12221   verifyFormat("typedef class Foo {\n"
12222                "  Foo();\n"
12223                "} Foo_t;",
12224                Style);
12225   verifyFormat("struct Foo {\n"
12226                "  Foo();\n"
12227                "};",
12228                Style);
12229   verifyFormat("typedef struct Foo {\n"
12230                "  Foo();\n"
12231                "} Foo_t;",
12232                Style);
12233   verifyFormat("union Foo {\n"
12234                "  A,\n"
12235                "};",
12236                Style);
12237   verifyFormat("typedef union Foo {\n"
12238                "  A,\n"
12239                "} Foo_t;",
12240                Style);
12241   verifyFormat("namespace Foo {\n"
12242                "void Bar();\n"
12243                "};",
12244                Style);
12245 
12246   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12247   Style.BraceWrapping.AfterClass = true;
12248   Style.BraceWrapping.AfterStruct = true;
12249   Style.BraceWrapping.AfterUnion = true;
12250   Style.BraceWrapping.AfterNamespace = true;
12251   verifyFormat("class Foo\n"
12252                "{\n"
12253                "  Foo();\n"
12254                "};",
12255                Style);
12256   verifyFormat("typedef class Foo\n"
12257                "{\n"
12258                "  Foo();\n"
12259                "} Foo_t;",
12260                Style);
12261   verifyFormat("struct Foo\n"
12262                "{\n"
12263                "  Foo();\n"
12264                "};",
12265                Style);
12266   verifyFormat("typedef struct Foo\n"
12267                "{\n"
12268                "  Foo();\n"
12269                "} Foo_t;",
12270                Style);
12271   verifyFormat("union Foo\n"
12272                "{\n"
12273                "  A,\n"
12274                "};",
12275                Style);
12276   verifyFormat("typedef union Foo\n"
12277                "{\n"
12278                "  A,\n"
12279                "} Foo_t;",
12280                Style);
12281   verifyFormat("namespace Foo\n"
12282                "{\n"
12283                "void Bar();\n"
12284                "};",
12285                Style);
12286 }
12287 
12288 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12289   // Elaborate type variable declarations.
12290   verifyFormat("struct foo a = {bar};\nint n;");
12291   verifyFormat("class foo a = {bar};\nint n;");
12292   verifyFormat("union foo a = {bar};\nint n;");
12293 
12294   // Elaborate types inside function definitions.
12295   verifyFormat("struct foo f() {}\nint n;");
12296   verifyFormat("class foo f() {}\nint n;");
12297   verifyFormat("union foo f() {}\nint n;");
12298 
12299   // Templates.
12300   verifyFormat("template <class X> void f() {}\nint n;");
12301   verifyFormat("template <struct X> void f() {}\nint n;");
12302   verifyFormat("template <union X> void f() {}\nint n;");
12303 
12304   // Actual definitions...
12305   verifyFormat("struct {\n} n;");
12306   verifyFormat(
12307       "template <template <class T, class Y>, class Z> class X {\n} n;");
12308   verifyFormat("union Z {\n  int n;\n} x;");
12309   verifyFormat("class MACRO Z {\n} n;");
12310   verifyFormat("class MACRO(X) Z {\n} n;");
12311   verifyFormat("class __attribute__(X) Z {\n} n;");
12312   verifyFormat("class __declspec(X) Z {\n} n;");
12313   verifyFormat("class A##B##C {\n} n;");
12314   verifyFormat("class alignas(16) Z {\n} n;");
12315   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12316   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12317 
12318   // Redefinition from nested context:
12319   verifyFormat("class A::B::C {\n} n;");
12320 
12321   // Template definitions.
12322   verifyFormat(
12323       "template <typename F>\n"
12324       "Matcher(const Matcher<F> &Other,\n"
12325       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12326       "                             !is_same<F, T>::value>::type * = 0)\n"
12327       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12328 
12329   // FIXME: This is still incorrectly handled at the formatter side.
12330   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12331   verifyFormat("int i = SomeFunction(a<b, a> b);");
12332 
12333   // FIXME:
12334   // This now gets parsed incorrectly as class definition.
12335   // verifyFormat("class A<int> f() {\n}\nint n;");
12336 
12337   // Elaborate types where incorrectly parsing the structural element would
12338   // break the indent.
12339   verifyFormat("if (true)\n"
12340                "  class X x;\n"
12341                "else\n"
12342                "  f();\n");
12343 
12344   // This is simply incomplete. Formatting is not important, but must not crash.
12345   verifyFormat("class A:");
12346 }
12347 
12348 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12349   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12350             format("#error Leave     all         white!!!!! space* alone!\n"));
12351   EXPECT_EQ(
12352       "#warning Leave     all         white!!!!! space* alone!\n",
12353       format("#warning Leave     all         white!!!!! space* alone!\n"));
12354   EXPECT_EQ("#error 1", format("  #  error   1"));
12355   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12356 }
12357 
12358 TEST_F(FormatTest, FormatHashIfExpressions) {
12359   verifyFormat("#if AAAA && BBBB");
12360   verifyFormat("#if (AAAA && BBBB)");
12361   verifyFormat("#elif (AAAA && BBBB)");
12362   // FIXME: Come up with a better indentation for #elif.
12363   verifyFormat(
12364       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12365       "    defined(BBBBBBBB)\n"
12366       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12367       "    defined(BBBBBBBB)\n"
12368       "#endif",
12369       getLLVMStyleWithColumns(65));
12370 }
12371 
12372 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12373   FormatStyle AllowsMergedIf = getGoogleStyle();
12374   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12375       FormatStyle::SIS_WithoutElse;
12376   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12377   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12378   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12379   EXPECT_EQ("if (true) return 42;",
12380             format("if (true)\nreturn 42;", AllowsMergedIf));
12381   FormatStyle ShortMergedIf = AllowsMergedIf;
12382   ShortMergedIf.ColumnLimit = 25;
12383   verifyFormat("#define A \\\n"
12384                "  if (true) return 42;",
12385                ShortMergedIf);
12386   verifyFormat("#define A \\\n"
12387                "  f();    \\\n"
12388                "  if (true)\n"
12389                "#define B",
12390                ShortMergedIf);
12391   verifyFormat("#define A \\\n"
12392                "  f();    \\\n"
12393                "  if (true)\n"
12394                "g();",
12395                ShortMergedIf);
12396   verifyFormat("{\n"
12397                "#ifdef A\n"
12398                "  // Comment\n"
12399                "  if (true) continue;\n"
12400                "#endif\n"
12401                "  // Comment\n"
12402                "  if (true) continue;\n"
12403                "}",
12404                ShortMergedIf);
12405   ShortMergedIf.ColumnLimit = 33;
12406   verifyFormat("#define A \\\n"
12407                "  if constexpr (true) return 42;",
12408                ShortMergedIf);
12409   verifyFormat("#define A \\\n"
12410                "  if CONSTEXPR (true) return 42;",
12411                ShortMergedIf);
12412   ShortMergedIf.ColumnLimit = 29;
12413   verifyFormat("#define A                   \\\n"
12414                "  if (aaaaaaaaaa) return 1; \\\n"
12415                "  return 2;",
12416                ShortMergedIf);
12417   ShortMergedIf.ColumnLimit = 28;
12418   verifyFormat("#define A         \\\n"
12419                "  if (aaaaaaaaaa) \\\n"
12420                "    return 1;     \\\n"
12421                "  return 2;",
12422                ShortMergedIf);
12423   verifyFormat("#define A                \\\n"
12424                "  if constexpr (aaaaaaa) \\\n"
12425                "    return 1;            \\\n"
12426                "  return 2;",
12427                ShortMergedIf);
12428   verifyFormat("#define A                \\\n"
12429                "  if CONSTEXPR (aaaaaaa) \\\n"
12430                "    return 1;            \\\n"
12431                "  return 2;",
12432                ShortMergedIf);
12433 }
12434 
12435 TEST_F(FormatTest, FormatStarDependingOnContext) {
12436   verifyFormat("void f(int *a);");
12437   verifyFormat("void f() { f(fint * b); }");
12438   verifyFormat("class A {\n  void f(int *a);\n};");
12439   verifyFormat("class A {\n  int *a;\n};");
12440   verifyFormat("namespace a {\n"
12441                "namespace b {\n"
12442                "class A {\n"
12443                "  void f() {}\n"
12444                "  int *a;\n"
12445                "};\n"
12446                "} // namespace b\n"
12447                "} // namespace a");
12448 }
12449 
12450 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12451   verifyFormat("while");
12452   verifyFormat("operator");
12453 }
12454 
12455 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12456   // This code would be painfully slow to format if we didn't skip it.
12457   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
12458                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12459                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12460                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12461                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12462                    "A(1, 1)\n"
12463                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12464                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12465                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12466                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12467                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12468                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12469                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12470                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12471                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12472                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
12473   // Deeply nested part is untouched, rest is formatted.
12474   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
12475             format(std::string("int    i;\n") + Code + "int    j;\n",
12476                    getLLVMStyle(), SC_ExpectIncomplete));
12477 }
12478 
12479 //===----------------------------------------------------------------------===//
12480 // Objective-C tests.
12481 //===----------------------------------------------------------------------===//
12482 
12483 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
12484   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
12485   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
12486             format("-(NSUInteger)indexOfObject:(id)anObject;"));
12487   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
12488   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
12489   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
12490             format("-(NSInteger)Method3:(id)anObject;"));
12491   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
12492             format("-(NSInteger)Method4:(id)anObject;"));
12493   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
12494             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
12495   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
12496             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
12497   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12498             "forAllCells:(BOOL)flag;",
12499             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12500                    "forAllCells:(BOOL)flag;"));
12501 
12502   // Very long objectiveC method declaration.
12503   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
12504                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
12505   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
12506                "                    inRange:(NSRange)range\n"
12507                "                   outRange:(NSRange)out_range\n"
12508                "                  outRange1:(NSRange)out_range1\n"
12509                "                  outRange2:(NSRange)out_range2\n"
12510                "                  outRange3:(NSRange)out_range3\n"
12511                "                  outRange4:(NSRange)out_range4\n"
12512                "                  outRange5:(NSRange)out_range5\n"
12513                "                  outRange6:(NSRange)out_range6\n"
12514                "                  outRange7:(NSRange)out_range7\n"
12515                "                  outRange8:(NSRange)out_range8\n"
12516                "                  outRange9:(NSRange)out_range9;");
12517 
12518   // When the function name has to be wrapped.
12519   FormatStyle Style = getLLVMStyle();
12520   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
12521   // and always indents instead.
12522   Style.IndentWrappedFunctionNames = false;
12523   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12524                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
12525                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
12526                "}",
12527                Style);
12528   Style.IndentWrappedFunctionNames = true;
12529   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12530                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
12531                "               anotherName:(NSString)dddddddddddddd {\n"
12532                "}",
12533                Style);
12534 
12535   verifyFormat("- (int)sum:(vector<int>)numbers;");
12536   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
12537   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
12538   // protocol lists (but not for template classes):
12539   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
12540 
12541   verifyFormat("- (int (*)())foo:(int (*)())f;");
12542   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
12543 
12544   // If there's no return type (very rare in practice!), LLVM and Google style
12545   // agree.
12546   verifyFormat("- foo;");
12547   verifyFormat("- foo:(int)f;");
12548   verifyGoogleFormat("- foo:(int)foo;");
12549 }
12550 
12551 TEST_F(FormatTest, BreaksStringLiterals) {
12552   EXPECT_EQ("\"some text \"\n"
12553             "\"other\";",
12554             format("\"some text other\";", getLLVMStyleWithColumns(12)));
12555   EXPECT_EQ("\"some text \"\n"
12556             "\"other\";",
12557             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
12558   EXPECT_EQ(
12559       "#define A  \\\n"
12560       "  \"some \"  \\\n"
12561       "  \"text \"  \\\n"
12562       "  \"other\";",
12563       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
12564   EXPECT_EQ(
12565       "#define A  \\\n"
12566       "  \"so \"    \\\n"
12567       "  \"text \"  \\\n"
12568       "  \"other\";",
12569       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
12570 
12571   EXPECT_EQ("\"some text\"",
12572             format("\"some text\"", getLLVMStyleWithColumns(1)));
12573   EXPECT_EQ("\"some text\"",
12574             format("\"some text\"", getLLVMStyleWithColumns(11)));
12575   EXPECT_EQ("\"some \"\n"
12576             "\"text\"",
12577             format("\"some text\"", getLLVMStyleWithColumns(10)));
12578   EXPECT_EQ("\"some \"\n"
12579             "\"text\"",
12580             format("\"some text\"", getLLVMStyleWithColumns(7)));
12581   EXPECT_EQ("\"some\"\n"
12582             "\" tex\"\n"
12583             "\"t\"",
12584             format("\"some text\"", getLLVMStyleWithColumns(6)));
12585   EXPECT_EQ("\"some\"\n"
12586             "\" tex\"\n"
12587             "\" and\"",
12588             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
12589   EXPECT_EQ("\"some\"\n"
12590             "\"/tex\"\n"
12591             "\"/and\"",
12592             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
12593 
12594   EXPECT_EQ("variable =\n"
12595             "    \"long string \"\n"
12596             "    \"literal\";",
12597             format("variable = \"long string literal\";",
12598                    getLLVMStyleWithColumns(20)));
12599 
12600   EXPECT_EQ("variable = f(\n"
12601             "    \"long string \"\n"
12602             "    \"literal\",\n"
12603             "    short,\n"
12604             "    loooooooooooooooooooong);",
12605             format("variable = f(\"long string literal\", short, "
12606                    "loooooooooooooooooooong);",
12607                    getLLVMStyleWithColumns(20)));
12608 
12609   EXPECT_EQ(
12610       "f(g(\"long string \"\n"
12611       "    \"literal\"),\n"
12612       "  b);",
12613       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
12614   EXPECT_EQ("f(g(\"long string \"\n"
12615             "    \"literal\",\n"
12616             "    a),\n"
12617             "  b);",
12618             format("f(g(\"long string literal\", a), b);",
12619                    getLLVMStyleWithColumns(20)));
12620   EXPECT_EQ(
12621       "f(\"one two\".split(\n"
12622       "    variable));",
12623       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
12624   EXPECT_EQ("f(\"one two three four five six \"\n"
12625             "  \"seven\".split(\n"
12626             "      really_looooong_variable));",
12627             format("f(\"one two three four five six seven\"."
12628                    "split(really_looooong_variable));",
12629                    getLLVMStyleWithColumns(33)));
12630 
12631   EXPECT_EQ("f(\"some \"\n"
12632             "  \"text\",\n"
12633             "  other);",
12634             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
12635 
12636   // Only break as a last resort.
12637   verifyFormat(
12638       "aaaaaaaaaaaaaaaaaaaa(\n"
12639       "    aaaaaaaaaaaaaaaaaaaa,\n"
12640       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
12641 
12642   EXPECT_EQ("\"splitmea\"\n"
12643             "\"trandomp\"\n"
12644             "\"oint\"",
12645             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
12646 
12647   EXPECT_EQ("\"split/\"\n"
12648             "\"pathat/\"\n"
12649             "\"slashes\"",
12650             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12651 
12652   EXPECT_EQ("\"split/\"\n"
12653             "\"pathat/\"\n"
12654             "\"slashes\"",
12655             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12656   EXPECT_EQ("\"split at \"\n"
12657             "\"spaces/at/\"\n"
12658             "\"slashes.at.any$\"\n"
12659             "\"non-alphanumeric%\"\n"
12660             "\"1111111111characte\"\n"
12661             "\"rs\"",
12662             format("\"split at "
12663                    "spaces/at/"
12664                    "slashes.at."
12665                    "any$non-"
12666                    "alphanumeric%"
12667                    "1111111111characte"
12668                    "rs\"",
12669                    getLLVMStyleWithColumns(20)));
12670 
12671   // Verify that splitting the strings understands
12672   // Style::AlwaysBreakBeforeMultilineStrings.
12673   EXPECT_EQ("aaaaaaaaaaaa(\n"
12674             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
12675             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
12676             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
12677                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12678                    "aaaaaaaaaaaaaaaaaaaaaa\");",
12679                    getGoogleStyle()));
12680   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12681             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
12682             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
12683                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12684                    "aaaaaaaaaaaaaaaaaaaaaa\";",
12685                    getGoogleStyle()));
12686   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12687             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12688             format("llvm::outs() << "
12689                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
12690                    "aaaaaaaaaaaaaaaaaaa\";"));
12691   EXPECT_EQ("ffff(\n"
12692             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12693             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12694             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
12695                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12696                    getGoogleStyle()));
12697 
12698   FormatStyle Style = getLLVMStyleWithColumns(12);
12699   Style.BreakStringLiterals = false;
12700   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
12701 
12702   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
12703   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12704   EXPECT_EQ("#define A \\\n"
12705             "  \"some \" \\\n"
12706             "  \"text \" \\\n"
12707             "  \"other\";",
12708             format("#define A \"some text other\";", AlignLeft));
12709 }
12710 
12711 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
12712   EXPECT_EQ("C a = \"some more \"\n"
12713             "      \"text\";",
12714             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
12715 }
12716 
12717 TEST_F(FormatTest, FullyRemoveEmptyLines) {
12718   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
12719   NoEmptyLines.MaxEmptyLinesToKeep = 0;
12720   EXPECT_EQ("int i = a(b());",
12721             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
12722 }
12723 
12724 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
12725   EXPECT_EQ(
12726       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12727       "(\n"
12728       "    \"x\t\");",
12729       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12730              "aaaaaaa("
12731              "\"x\t\");"));
12732 }
12733 
12734 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
12735   EXPECT_EQ(
12736       "u8\"utf8 string \"\n"
12737       "u8\"literal\";",
12738       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
12739   EXPECT_EQ(
12740       "u\"utf16 string \"\n"
12741       "u\"literal\";",
12742       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
12743   EXPECT_EQ(
12744       "U\"utf32 string \"\n"
12745       "U\"literal\";",
12746       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
12747   EXPECT_EQ("L\"wide string \"\n"
12748             "L\"literal\";",
12749             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
12750   EXPECT_EQ("@\"NSString \"\n"
12751             "@\"literal\";",
12752             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
12753   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
12754 
12755   // This input makes clang-format try to split the incomplete unicode escape
12756   // sequence, which used to lead to a crasher.
12757   verifyNoCrash(
12758       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12759       getLLVMStyleWithColumns(60));
12760 }
12761 
12762 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
12763   FormatStyle Style = getGoogleStyleWithColumns(15);
12764   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
12765   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
12766   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
12767   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
12768   EXPECT_EQ("u8R\"x(raw literal)x\";",
12769             format("u8R\"x(raw literal)x\";", Style));
12770 }
12771 
12772 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
12773   FormatStyle Style = getLLVMStyleWithColumns(20);
12774   EXPECT_EQ(
12775       "_T(\"aaaaaaaaaaaaaa\")\n"
12776       "_T(\"aaaaaaaaaaaaaa\")\n"
12777       "_T(\"aaaaaaaaaaaa\")",
12778       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
12779   EXPECT_EQ("f(x,\n"
12780             "  _T(\"aaaaaaaaaaaa\")\n"
12781             "  _T(\"aaa\"),\n"
12782             "  z);",
12783             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
12784 
12785   // FIXME: Handle embedded spaces in one iteration.
12786   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
12787   //            "_T(\"aaaaaaaaaaaaa\")\n"
12788   //            "_T(\"aaaaaaaaaaaaa\")\n"
12789   //            "_T(\"a\")",
12790   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12791   //                   getLLVMStyleWithColumns(20)));
12792   EXPECT_EQ(
12793       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12794       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
12795   EXPECT_EQ("f(\n"
12796             "#if !TEST\n"
12797             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12798             "#endif\n"
12799             ");",
12800             format("f(\n"
12801                    "#if !TEST\n"
12802                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12803                    "#endif\n"
12804                    ");"));
12805   EXPECT_EQ("f(\n"
12806             "\n"
12807             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
12808             format("f(\n"
12809                    "\n"
12810                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
12811 }
12812 
12813 TEST_F(FormatTest, BreaksStringLiteralOperands) {
12814   // In a function call with two operands, the second can be broken with no line
12815   // break before it.
12816   EXPECT_EQ(
12817       "func(a, \"long long \"\n"
12818       "        \"long long\");",
12819       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
12820   // In a function call with three operands, the second must be broken with a
12821   // line break before it.
12822   EXPECT_EQ("func(a,\n"
12823             "     \"long long long \"\n"
12824             "     \"long\",\n"
12825             "     c);",
12826             format("func(a, \"long long long long\", c);",
12827                    getLLVMStyleWithColumns(24)));
12828   // In a function call with three operands, the third must be broken with a
12829   // line break before it.
12830   EXPECT_EQ("func(a, b,\n"
12831             "     \"long long long \"\n"
12832             "     \"long\");",
12833             format("func(a, b, \"long long long long\");",
12834                    getLLVMStyleWithColumns(24)));
12835   // In a function call with three operands, both the second and the third must
12836   // be broken with a line break before them.
12837   EXPECT_EQ("func(a,\n"
12838             "     \"long long long \"\n"
12839             "     \"long\",\n"
12840             "     \"long long long \"\n"
12841             "     \"long\");",
12842             format("func(a, \"long long long long\", \"long long long long\");",
12843                    getLLVMStyleWithColumns(24)));
12844   // In a chain of << with two operands, the second can be broken with no line
12845   // break before it.
12846   EXPECT_EQ("a << \"line line \"\n"
12847             "     \"line\";",
12848             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
12849   // In a chain of << with three operands, the second can be broken with no line
12850   // break before it.
12851   EXPECT_EQ(
12852       "abcde << \"line \"\n"
12853       "         \"line line\"\n"
12854       "      << c;",
12855       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
12856   // In a chain of << with three operands, the third must be broken with a line
12857   // break before it.
12858   EXPECT_EQ(
12859       "a << b\n"
12860       "  << \"line line \"\n"
12861       "     \"line\";",
12862       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
12863   // In a chain of << with three operands, the second can be broken with no line
12864   // break before it and the third must be broken with a line break before it.
12865   EXPECT_EQ("abcd << \"line line \"\n"
12866             "        \"line\"\n"
12867             "     << \"line line \"\n"
12868             "        \"line\";",
12869             format("abcd << \"line line line\" << \"line line line\";",
12870                    getLLVMStyleWithColumns(20)));
12871   // In a chain of binary operators with two operands, the second can be broken
12872   // with no line break before it.
12873   EXPECT_EQ(
12874       "abcd + \"line line \"\n"
12875       "       \"line line\";",
12876       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
12877   // In a chain of binary operators with three operands, the second must be
12878   // broken with a line break before it.
12879   EXPECT_EQ("abcd +\n"
12880             "    \"line line \"\n"
12881             "    \"line line\" +\n"
12882             "    e;",
12883             format("abcd + \"line line line line\" + e;",
12884                    getLLVMStyleWithColumns(20)));
12885   // In a function call with two operands, with AlignAfterOpenBracket enabled,
12886   // the first must be broken with a line break before it.
12887   FormatStyle Style = getLLVMStyleWithColumns(25);
12888   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12889   EXPECT_EQ("someFunction(\n"
12890             "    \"long long long \"\n"
12891             "    \"long\",\n"
12892             "    a);",
12893             format("someFunction(\"long long long long\", a);", Style));
12894 }
12895 
12896 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
12897   EXPECT_EQ(
12898       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12899       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12900       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12901       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12902              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12903              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
12904 }
12905 
12906 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
12907   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
12908             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
12909   EXPECT_EQ("fffffffffff(g(R\"x(\n"
12910             "multiline raw string literal xxxxxxxxxxxxxx\n"
12911             ")x\",\n"
12912             "              a),\n"
12913             "            b);",
12914             format("fffffffffff(g(R\"x(\n"
12915                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12916                    ")x\", a), b);",
12917                    getGoogleStyleWithColumns(20)));
12918   EXPECT_EQ("fffffffffff(\n"
12919             "    g(R\"x(qqq\n"
12920             "multiline raw string literal xxxxxxxxxxxxxx\n"
12921             ")x\",\n"
12922             "      a),\n"
12923             "    b);",
12924             format("fffffffffff(g(R\"x(qqq\n"
12925                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12926                    ")x\", a), b);",
12927                    getGoogleStyleWithColumns(20)));
12928 
12929   EXPECT_EQ("fffffffffff(R\"x(\n"
12930             "multiline raw string literal xxxxxxxxxxxxxx\n"
12931             ")x\");",
12932             format("fffffffffff(R\"x(\n"
12933                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12934                    ")x\");",
12935                    getGoogleStyleWithColumns(20)));
12936   EXPECT_EQ("fffffffffff(R\"x(\n"
12937             "multiline raw string literal xxxxxxxxxxxxxx\n"
12938             ")x\" + bbbbbb);",
12939             format("fffffffffff(R\"x(\n"
12940                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12941                    ")x\" +   bbbbbb);",
12942                    getGoogleStyleWithColumns(20)));
12943   EXPECT_EQ("fffffffffff(\n"
12944             "    R\"x(\n"
12945             "multiline raw string literal xxxxxxxxxxxxxx\n"
12946             ")x\" +\n"
12947             "    bbbbbb);",
12948             format("fffffffffff(\n"
12949                    " R\"x(\n"
12950                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12951                    ")x\" + bbbbbb);",
12952                    getGoogleStyleWithColumns(20)));
12953   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
12954             format("fffffffffff(\n"
12955                    " R\"(single line raw string)\" + bbbbbb);"));
12956 }
12957 
12958 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
12959   verifyFormat("string a = \"unterminated;");
12960   EXPECT_EQ("function(\"unterminated,\n"
12961             "         OtherParameter);",
12962             format("function(  \"unterminated,\n"
12963                    "    OtherParameter);"));
12964 }
12965 
12966 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
12967   FormatStyle Style = getLLVMStyle();
12968   Style.Standard = FormatStyle::LS_Cpp03;
12969   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
12970             format("#define x(_a) printf(\"foo\"_a);", Style));
12971 }
12972 
12973 TEST_F(FormatTest, CppLexVersion) {
12974   FormatStyle Style = getLLVMStyle();
12975   // Formatting of x * y differs if x is a type.
12976   verifyFormat("void foo() { MACRO(a * b); }", Style);
12977   verifyFormat("void foo() { MACRO(int *b); }", Style);
12978 
12979   // LLVM style uses latest lexer.
12980   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
12981   Style.Standard = FormatStyle::LS_Cpp17;
12982   // But in c++17, char8_t isn't a keyword.
12983   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
12984 }
12985 
12986 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
12987 
12988 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
12989   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
12990             "             \"ddeeefff\");",
12991             format("someFunction(\"aaabbbcccdddeeefff\");",
12992                    getLLVMStyleWithColumns(25)));
12993   EXPECT_EQ("someFunction1234567890(\n"
12994             "    \"aaabbbcccdddeeefff\");",
12995             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12996                    getLLVMStyleWithColumns(26)));
12997   EXPECT_EQ("someFunction1234567890(\n"
12998             "    \"aaabbbcccdddeeeff\"\n"
12999             "    \"f\");",
13000             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13001                    getLLVMStyleWithColumns(25)));
13002   EXPECT_EQ("someFunction1234567890(\n"
13003             "    \"aaabbbcccdddeeeff\"\n"
13004             "    \"f\");",
13005             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13006                    getLLVMStyleWithColumns(24)));
13007   EXPECT_EQ("someFunction(\n"
13008             "    \"aaabbbcc ddde \"\n"
13009             "    \"efff\");",
13010             format("someFunction(\"aaabbbcc ddde efff\");",
13011                    getLLVMStyleWithColumns(25)));
13012   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13013             "             \"ddeeefff\");",
13014             format("someFunction(\"aaabbbccc ddeeefff\");",
13015                    getLLVMStyleWithColumns(25)));
13016   EXPECT_EQ("someFunction1234567890(\n"
13017             "    \"aaabb \"\n"
13018             "    \"cccdddeeefff\");",
13019             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13020                    getLLVMStyleWithColumns(25)));
13021   EXPECT_EQ("#define A          \\\n"
13022             "  string s =       \\\n"
13023             "      \"123456789\"  \\\n"
13024             "      \"0\";         \\\n"
13025             "  int i;",
13026             format("#define A string s = \"1234567890\"; int i;",
13027                    getLLVMStyleWithColumns(20)));
13028   EXPECT_EQ("someFunction(\n"
13029             "    \"aaabbbcc \"\n"
13030             "    \"dddeeefff\");",
13031             format("someFunction(\"aaabbbcc dddeeefff\");",
13032                    getLLVMStyleWithColumns(25)));
13033 }
13034 
13035 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13036   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13037   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13038   EXPECT_EQ("\"test\"\n"
13039             "\"\\n\"",
13040             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13041   EXPECT_EQ("\"tes\\\\\"\n"
13042             "\"n\"",
13043             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13044   EXPECT_EQ("\"\\\\\\\\\"\n"
13045             "\"\\n\"",
13046             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13047   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13048   EXPECT_EQ("\"\\uff01\"\n"
13049             "\"test\"",
13050             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13051   EXPECT_EQ("\"\\Uff01ff02\"",
13052             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13053   EXPECT_EQ("\"\\x000000000001\"\n"
13054             "\"next\"",
13055             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13056   EXPECT_EQ("\"\\x000000000001next\"",
13057             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13058   EXPECT_EQ("\"\\x000000000001\"",
13059             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13060   EXPECT_EQ("\"test\"\n"
13061             "\"\\000000\"\n"
13062             "\"000001\"",
13063             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13064   EXPECT_EQ("\"test\\000\"\n"
13065             "\"00000000\"\n"
13066             "\"1\"",
13067             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13068 }
13069 
13070 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13071   verifyFormat("void f() {\n"
13072                "  return g() {}\n"
13073                "  void h() {}");
13074   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13075                "g();\n"
13076                "}");
13077 }
13078 
13079 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13080   verifyFormat(
13081       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13082 }
13083 
13084 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13085   verifyFormat("class X {\n"
13086                "  void f() {\n"
13087                "  }\n"
13088                "};",
13089                getLLVMStyleWithColumns(12));
13090 }
13091 
13092 TEST_F(FormatTest, ConfigurableIndentWidth) {
13093   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13094   EightIndent.IndentWidth = 8;
13095   EightIndent.ContinuationIndentWidth = 8;
13096   verifyFormat("void f() {\n"
13097                "        someFunction();\n"
13098                "        if (true) {\n"
13099                "                f();\n"
13100                "        }\n"
13101                "}",
13102                EightIndent);
13103   verifyFormat("class X {\n"
13104                "        void f() {\n"
13105                "        }\n"
13106                "};",
13107                EightIndent);
13108   verifyFormat("int x[] = {\n"
13109                "        call(),\n"
13110                "        call()};",
13111                EightIndent);
13112 }
13113 
13114 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13115   verifyFormat("double\n"
13116                "f();",
13117                getLLVMStyleWithColumns(8));
13118 }
13119 
13120 TEST_F(FormatTest, ConfigurableUseOfTab) {
13121   FormatStyle Tab = getLLVMStyleWithColumns(42);
13122   Tab.IndentWidth = 8;
13123   Tab.UseTab = FormatStyle::UT_Always;
13124   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13125 
13126   EXPECT_EQ("if (aaaaaaaa && // q\n"
13127             "    bb)\t\t// w\n"
13128             "\t;",
13129             format("if (aaaaaaaa &&// q\n"
13130                    "bb)// w\n"
13131                    ";",
13132                    Tab));
13133   EXPECT_EQ("if (aaa && bbb) // w\n"
13134             "\t;",
13135             format("if(aaa&&bbb)// w\n"
13136                    ";",
13137                    Tab));
13138 
13139   verifyFormat("class X {\n"
13140                "\tvoid f() {\n"
13141                "\t\tsomeFunction(parameter1,\n"
13142                "\t\t\t     parameter2);\n"
13143                "\t}\n"
13144                "};",
13145                Tab);
13146   verifyFormat("#define A                        \\\n"
13147                "\tvoid f() {               \\\n"
13148                "\t\tsomeFunction(    \\\n"
13149                "\t\t    parameter1,  \\\n"
13150                "\t\t    parameter2); \\\n"
13151                "\t}",
13152                Tab);
13153   verifyFormat("int a;\t      // x\n"
13154                "int bbbbbbbb; // x\n",
13155                Tab);
13156 
13157   Tab.TabWidth = 4;
13158   Tab.IndentWidth = 8;
13159   verifyFormat("class TabWidth4Indent8 {\n"
13160                "\t\tvoid f() {\n"
13161                "\t\t\t\tsomeFunction(parameter1,\n"
13162                "\t\t\t\t\t\t\t parameter2);\n"
13163                "\t\t}\n"
13164                "};",
13165                Tab);
13166 
13167   Tab.TabWidth = 4;
13168   Tab.IndentWidth = 4;
13169   verifyFormat("class TabWidth4Indent4 {\n"
13170                "\tvoid f() {\n"
13171                "\t\tsomeFunction(parameter1,\n"
13172                "\t\t\t\t\t parameter2);\n"
13173                "\t}\n"
13174                "};",
13175                Tab);
13176 
13177   Tab.TabWidth = 8;
13178   Tab.IndentWidth = 4;
13179   verifyFormat("class TabWidth8Indent4 {\n"
13180                "    void f() {\n"
13181                "\tsomeFunction(parameter1,\n"
13182                "\t\t     parameter2);\n"
13183                "    }\n"
13184                "};",
13185                Tab);
13186 
13187   Tab.TabWidth = 8;
13188   Tab.IndentWidth = 8;
13189   EXPECT_EQ("/*\n"
13190             "\t      a\t\tcomment\n"
13191             "\t      in multiple lines\n"
13192             "       */",
13193             format("   /*\t \t \n"
13194                    " \t \t a\t\tcomment\t \t\n"
13195                    " \t \t in multiple lines\t\n"
13196                    " \t  */",
13197                    Tab));
13198 
13199   Tab.UseTab = FormatStyle::UT_ForIndentation;
13200   verifyFormat("{\n"
13201                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13202                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13203                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13204                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13205                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13206                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13207                "};",
13208                Tab);
13209   verifyFormat("enum AA {\n"
13210                "\ta1, // Force multiple lines\n"
13211                "\ta2,\n"
13212                "\ta3\n"
13213                "};",
13214                Tab);
13215   EXPECT_EQ("if (aaaaaaaa && // q\n"
13216             "    bb)         // w\n"
13217             "\t;",
13218             format("if (aaaaaaaa &&// q\n"
13219                    "bb)// w\n"
13220                    ";",
13221                    Tab));
13222   verifyFormat("class X {\n"
13223                "\tvoid f() {\n"
13224                "\t\tsomeFunction(parameter1,\n"
13225                "\t\t             parameter2);\n"
13226                "\t}\n"
13227                "};",
13228                Tab);
13229   verifyFormat("{\n"
13230                "\tQ(\n"
13231                "\t    {\n"
13232                "\t\t    int a;\n"
13233                "\t\t    someFunction(aaaaaaaa,\n"
13234                "\t\t                 bbbbbbb);\n"
13235                "\t    },\n"
13236                "\t    p);\n"
13237                "}",
13238                Tab);
13239   EXPECT_EQ("{\n"
13240             "\t/* aaaa\n"
13241             "\t   bbbb */\n"
13242             "}",
13243             format("{\n"
13244                    "/* aaaa\n"
13245                    "   bbbb */\n"
13246                    "}",
13247                    Tab));
13248   EXPECT_EQ("{\n"
13249             "\t/*\n"
13250             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13251             "\t  bbbbbbbbbbbbb\n"
13252             "\t*/\n"
13253             "}",
13254             format("{\n"
13255                    "/*\n"
13256                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13257                    "*/\n"
13258                    "}",
13259                    Tab));
13260   EXPECT_EQ("{\n"
13261             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13262             "\t// bbbbbbbbbbbbb\n"
13263             "}",
13264             format("{\n"
13265                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13266                    "}",
13267                    Tab));
13268   EXPECT_EQ("{\n"
13269             "\t/*\n"
13270             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13271             "\t  bbbbbbbbbbbbb\n"
13272             "\t*/\n"
13273             "}",
13274             format("{\n"
13275                    "\t/*\n"
13276                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13277                    "\t*/\n"
13278                    "}",
13279                    Tab));
13280   EXPECT_EQ("{\n"
13281             "\t/*\n"
13282             "\n"
13283             "\t*/\n"
13284             "}",
13285             format("{\n"
13286                    "\t/*\n"
13287                    "\n"
13288                    "\t*/\n"
13289                    "}",
13290                    Tab));
13291   EXPECT_EQ("{\n"
13292             "\t/*\n"
13293             " asdf\n"
13294             "\t*/\n"
13295             "}",
13296             format("{\n"
13297                    "\t/*\n"
13298                    " asdf\n"
13299                    "\t*/\n"
13300                    "}",
13301                    Tab));
13302 
13303   Tab.UseTab = FormatStyle::UT_Never;
13304   EXPECT_EQ("/*\n"
13305             "              a\t\tcomment\n"
13306             "              in multiple lines\n"
13307             "       */",
13308             format("   /*\t \t \n"
13309                    " \t \t a\t\tcomment\t \t\n"
13310                    " \t \t in multiple lines\t\n"
13311                    " \t  */",
13312                    Tab));
13313   EXPECT_EQ("/* some\n"
13314             "   comment */",
13315             format(" \t \t /* some\n"
13316                    " \t \t    comment */",
13317                    Tab));
13318   EXPECT_EQ("int a; /* some\n"
13319             "   comment */",
13320             format(" \t \t int a; /* some\n"
13321                    " \t \t    comment */",
13322                    Tab));
13323 
13324   EXPECT_EQ("int a; /* some\n"
13325             "comment */",
13326             format(" \t \t int\ta; /* some\n"
13327                    " \t \t    comment */",
13328                    Tab));
13329   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13330             "    comment */",
13331             format(" \t \t f(\"\t\t\"); /* some\n"
13332                    " \t \t    comment */",
13333                    Tab));
13334   EXPECT_EQ("{\n"
13335             "        /*\n"
13336             "         * Comment\n"
13337             "         */\n"
13338             "        int i;\n"
13339             "}",
13340             format("{\n"
13341                    "\t/*\n"
13342                    "\t * Comment\n"
13343                    "\t */\n"
13344                    "\t int i;\n"
13345                    "}",
13346                    Tab));
13347 
13348   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13349   Tab.TabWidth = 8;
13350   Tab.IndentWidth = 8;
13351   EXPECT_EQ("if (aaaaaaaa && // q\n"
13352             "    bb)         // w\n"
13353             "\t;",
13354             format("if (aaaaaaaa &&// q\n"
13355                    "bb)// w\n"
13356                    ";",
13357                    Tab));
13358   EXPECT_EQ("if (aaa && bbb) // w\n"
13359             "\t;",
13360             format("if(aaa&&bbb)// w\n"
13361                    ";",
13362                    Tab));
13363   verifyFormat("class X {\n"
13364                "\tvoid f() {\n"
13365                "\t\tsomeFunction(parameter1,\n"
13366                "\t\t\t     parameter2);\n"
13367                "\t}\n"
13368                "};",
13369                Tab);
13370   verifyFormat("#define A                        \\\n"
13371                "\tvoid f() {               \\\n"
13372                "\t\tsomeFunction(    \\\n"
13373                "\t\t    parameter1,  \\\n"
13374                "\t\t    parameter2); \\\n"
13375                "\t}",
13376                Tab);
13377   Tab.TabWidth = 4;
13378   Tab.IndentWidth = 8;
13379   verifyFormat("class TabWidth4Indent8 {\n"
13380                "\t\tvoid f() {\n"
13381                "\t\t\t\tsomeFunction(parameter1,\n"
13382                "\t\t\t\t\t\t\t parameter2);\n"
13383                "\t\t}\n"
13384                "};",
13385                Tab);
13386   Tab.TabWidth = 4;
13387   Tab.IndentWidth = 4;
13388   verifyFormat("class TabWidth4Indent4 {\n"
13389                "\tvoid f() {\n"
13390                "\t\tsomeFunction(parameter1,\n"
13391                "\t\t\t\t\t parameter2);\n"
13392                "\t}\n"
13393                "};",
13394                Tab);
13395   Tab.TabWidth = 8;
13396   Tab.IndentWidth = 4;
13397   verifyFormat("class TabWidth8Indent4 {\n"
13398                "    void f() {\n"
13399                "\tsomeFunction(parameter1,\n"
13400                "\t\t     parameter2);\n"
13401                "    }\n"
13402                "};",
13403                Tab);
13404   Tab.TabWidth = 8;
13405   Tab.IndentWidth = 8;
13406   EXPECT_EQ("/*\n"
13407             "\t      a\t\tcomment\n"
13408             "\t      in multiple lines\n"
13409             "       */",
13410             format("   /*\t \t \n"
13411                    " \t \t a\t\tcomment\t \t\n"
13412                    " \t \t in multiple lines\t\n"
13413                    " \t  */",
13414                    Tab));
13415   verifyFormat("{\n"
13416                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13417                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13418                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13419                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13420                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13421                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13422                "};",
13423                Tab);
13424   verifyFormat("enum AA {\n"
13425                "\ta1, // Force multiple lines\n"
13426                "\ta2,\n"
13427                "\ta3\n"
13428                "};",
13429                Tab);
13430   EXPECT_EQ("if (aaaaaaaa && // q\n"
13431             "    bb)         // w\n"
13432             "\t;",
13433             format("if (aaaaaaaa &&// q\n"
13434                    "bb)// w\n"
13435                    ";",
13436                    Tab));
13437   verifyFormat("class X {\n"
13438                "\tvoid f() {\n"
13439                "\t\tsomeFunction(parameter1,\n"
13440                "\t\t\t     parameter2);\n"
13441                "\t}\n"
13442                "};",
13443                Tab);
13444   verifyFormat("{\n"
13445                "\tQ(\n"
13446                "\t    {\n"
13447                "\t\t    int a;\n"
13448                "\t\t    someFunction(aaaaaaaa,\n"
13449                "\t\t\t\t bbbbbbb);\n"
13450                "\t    },\n"
13451                "\t    p);\n"
13452                "}",
13453                Tab);
13454   EXPECT_EQ("{\n"
13455             "\t/* aaaa\n"
13456             "\t   bbbb */\n"
13457             "}",
13458             format("{\n"
13459                    "/* aaaa\n"
13460                    "   bbbb */\n"
13461                    "}",
13462                    Tab));
13463   EXPECT_EQ("{\n"
13464             "\t/*\n"
13465             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13466             "\t  bbbbbbbbbbbbb\n"
13467             "\t*/\n"
13468             "}",
13469             format("{\n"
13470                    "/*\n"
13471                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13472                    "*/\n"
13473                    "}",
13474                    Tab));
13475   EXPECT_EQ("{\n"
13476             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13477             "\t// bbbbbbbbbbbbb\n"
13478             "}",
13479             format("{\n"
13480                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13481                    "}",
13482                    Tab));
13483   EXPECT_EQ("{\n"
13484             "\t/*\n"
13485             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13486             "\t  bbbbbbbbbbbbb\n"
13487             "\t*/\n"
13488             "}",
13489             format("{\n"
13490                    "\t/*\n"
13491                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13492                    "\t*/\n"
13493                    "}",
13494                    Tab));
13495   EXPECT_EQ("{\n"
13496             "\t/*\n"
13497             "\n"
13498             "\t*/\n"
13499             "}",
13500             format("{\n"
13501                    "\t/*\n"
13502                    "\n"
13503                    "\t*/\n"
13504                    "}",
13505                    Tab));
13506   EXPECT_EQ("{\n"
13507             "\t/*\n"
13508             " asdf\n"
13509             "\t*/\n"
13510             "}",
13511             format("{\n"
13512                    "\t/*\n"
13513                    " asdf\n"
13514                    "\t*/\n"
13515                    "}",
13516                    Tab));
13517   EXPECT_EQ("/* some\n"
13518             "   comment */",
13519             format(" \t \t /* some\n"
13520                    " \t \t    comment */",
13521                    Tab));
13522   EXPECT_EQ("int a; /* some\n"
13523             "   comment */",
13524             format(" \t \t int a; /* some\n"
13525                    " \t \t    comment */",
13526                    Tab));
13527   EXPECT_EQ("int a; /* some\n"
13528             "comment */",
13529             format(" \t \t int\ta; /* some\n"
13530                    " \t \t    comment */",
13531                    Tab));
13532   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13533             "    comment */",
13534             format(" \t \t f(\"\t\t\"); /* some\n"
13535                    " \t \t    comment */",
13536                    Tab));
13537   EXPECT_EQ("{\n"
13538             "\t/*\n"
13539             "\t * Comment\n"
13540             "\t */\n"
13541             "\tint i;\n"
13542             "}",
13543             format("{\n"
13544                    "\t/*\n"
13545                    "\t * Comment\n"
13546                    "\t */\n"
13547                    "\t int i;\n"
13548                    "}",
13549                    Tab));
13550   Tab.TabWidth = 2;
13551   Tab.IndentWidth = 2;
13552   EXPECT_EQ("{\n"
13553             "\t/* aaaa\n"
13554             "\t\t bbbb */\n"
13555             "}",
13556             format("{\n"
13557                    "/* aaaa\n"
13558                    "\t bbbb */\n"
13559                    "}",
13560                    Tab));
13561   EXPECT_EQ("{\n"
13562             "\t/*\n"
13563             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13564             "\t\tbbbbbbbbbbbbb\n"
13565             "\t*/\n"
13566             "}",
13567             format("{\n"
13568                    "/*\n"
13569                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13570                    "*/\n"
13571                    "}",
13572                    Tab));
13573   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13574   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13575   Tab.TabWidth = 4;
13576   Tab.IndentWidth = 4;
13577   verifyFormat("class Assign {\n"
13578                "\tvoid f() {\n"
13579                "\t\tint         x      = 123;\n"
13580                "\t\tint         random = 4;\n"
13581                "\t\tstd::string alphabet =\n"
13582                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13583                "\t}\n"
13584                "};",
13585                Tab);
13586 
13587   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13588   Tab.TabWidth = 8;
13589   Tab.IndentWidth = 8;
13590   EXPECT_EQ("if (aaaaaaaa && // q\n"
13591             "    bb)         // w\n"
13592             "\t;",
13593             format("if (aaaaaaaa &&// q\n"
13594                    "bb)// w\n"
13595                    ";",
13596                    Tab));
13597   EXPECT_EQ("if (aaa && bbb) // w\n"
13598             "\t;",
13599             format("if(aaa&&bbb)// w\n"
13600                    ";",
13601                    Tab));
13602   verifyFormat("class X {\n"
13603                "\tvoid f() {\n"
13604                "\t\tsomeFunction(parameter1,\n"
13605                "\t\t             parameter2);\n"
13606                "\t}\n"
13607                "};",
13608                Tab);
13609   verifyFormat("#define A                        \\\n"
13610                "\tvoid f() {               \\\n"
13611                "\t\tsomeFunction(    \\\n"
13612                "\t\t    parameter1,  \\\n"
13613                "\t\t    parameter2); \\\n"
13614                "\t}",
13615                Tab);
13616   Tab.TabWidth = 4;
13617   Tab.IndentWidth = 8;
13618   verifyFormat("class TabWidth4Indent8 {\n"
13619                "\t\tvoid f() {\n"
13620                "\t\t\t\tsomeFunction(parameter1,\n"
13621                "\t\t\t\t             parameter2);\n"
13622                "\t\t}\n"
13623                "};",
13624                Tab);
13625   Tab.TabWidth = 4;
13626   Tab.IndentWidth = 4;
13627   verifyFormat("class TabWidth4Indent4 {\n"
13628                "\tvoid f() {\n"
13629                "\t\tsomeFunction(parameter1,\n"
13630                "\t\t             parameter2);\n"
13631                "\t}\n"
13632                "};",
13633                Tab);
13634   Tab.TabWidth = 8;
13635   Tab.IndentWidth = 4;
13636   verifyFormat("class TabWidth8Indent4 {\n"
13637                "    void f() {\n"
13638                "\tsomeFunction(parameter1,\n"
13639                "\t             parameter2);\n"
13640                "    }\n"
13641                "};",
13642                Tab);
13643   Tab.TabWidth = 8;
13644   Tab.IndentWidth = 8;
13645   EXPECT_EQ("/*\n"
13646             "              a\t\tcomment\n"
13647             "              in multiple lines\n"
13648             "       */",
13649             format("   /*\t \t \n"
13650                    " \t \t a\t\tcomment\t \t\n"
13651                    " \t \t in multiple lines\t\n"
13652                    " \t  */",
13653                    Tab));
13654   verifyFormat("{\n"
13655                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13656                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13657                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13658                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13659                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13660                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13661                "};",
13662                Tab);
13663   verifyFormat("enum AA {\n"
13664                "\ta1, // Force multiple lines\n"
13665                "\ta2,\n"
13666                "\ta3\n"
13667                "};",
13668                Tab);
13669   EXPECT_EQ("if (aaaaaaaa && // q\n"
13670             "    bb)         // w\n"
13671             "\t;",
13672             format("if (aaaaaaaa &&// q\n"
13673                    "bb)// w\n"
13674                    ";",
13675                    Tab));
13676   verifyFormat("class X {\n"
13677                "\tvoid f() {\n"
13678                "\t\tsomeFunction(parameter1,\n"
13679                "\t\t             parameter2);\n"
13680                "\t}\n"
13681                "};",
13682                Tab);
13683   verifyFormat("{\n"
13684                "\tQ(\n"
13685                "\t    {\n"
13686                "\t\t    int a;\n"
13687                "\t\t    someFunction(aaaaaaaa,\n"
13688                "\t\t                 bbbbbbb);\n"
13689                "\t    },\n"
13690                "\t    p);\n"
13691                "}",
13692                Tab);
13693   EXPECT_EQ("{\n"
13694             "\t/* aaaa\n"
13695             "\t   bbbb */\n"
13696             "}",
13697             format("{\n"
13698                    "/* aaaa\n"
13699                    "   bbbb */\n"
13700                    "}",
13701                    Tab));
13702   EXPECT_EQ("{\n"
13703             "\t/*\n"
13704             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13705             "\t  bbbbbbbbbbbbb\n"
13706             "\t*/\n"
13707             "}",
13708             format("{\n"
13709                    "/*\n"
13710                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13711                    "*/\n"
13712                    "}",
13713                    Tab));
13714   EXPECT_EQ("{\n"
13715             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13716             "\t// bbbbbbbbbbbbb\n"
13717             "}",
13718             format("{\n"
13719                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13720                    "}",
13721                    Tab));
13722   EXPECT_EQ("{\n"
13723             "\t/*\n"
13724             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13725             "\t  bbbbbbbbbbbbb\n"
13726             "\t*/\n"
13727             "}",
13728             format("{\n"
13729                    "\t/*\n"
13730                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13731                    "\t*/\n"
13732                    "}",
13733                    Tab));
13734   EXPECT_EQ("{\n"
13735             "\t/*\n"
13736             "\n"
13737             "\t*/\n"
13738             "}",
13739             format("{\n"
13740                    "\t/*\n"
13741                    "\n"
13742                    "\t*/\n"
13743                    "}",
13744                    Tab));
13745   EXPECT_EQ("{\n"
13746             "\t/*\n"
13747             " asdf\n"
13748             "\t*/\n"
13749             "}",
13750             format("{\n"
13751                    "\t/*\n"
13752                    " asdf\n"
13753                    "\t*/\n"
13754                    "}",
13755                    Tab));
13756   EXPECT_EQ("/* some\n"
13757             "   comment */",
13758             format(" \t \t /* some\n"
13759                    " \t \t    comment */",
13760                    Tab));
13761   EXPECT_EQ("int a; /* some\n"
13762             "   comment */",
13763             format(" \t \t int a; /* some\n"
13764                    " \t \t    comment */",
13765                    Tab));
13766   EXPECT_EQ("int a; /* some\n"
13767             "comment */",
13768             format(" \t \t int\ta; /* some\n"
13769                    " \t \t    comment */",
13770                    Tab));
13771   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13772             "    comment */",
13773             format(" \t \t f(\"\t\t\"); /* some\n"
13774                    " \t \t    comment */",
13775                    Tab));
13776   EXPECT_EQ("{\n"
13777             "\t/*\n"
13778             "\t * Comment\n"
13779             "\t */\n"
13780             "\tint i;\n"
13781             "}",
13782             format("{\n"
13783                    "\t/*\n"
13784                    "\t * Comment\n"
13785                    "\t */\n"
13786                    "\t int i;\n"
13787                    "}",
13788                    Tab));
13789   Tab.TabWidth = 2;
13790   Tab.IndentWidth = 2;
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   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13813   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13814   Tab.TabWidth = 4;
13815   Tab.IndentWidth = 4;
13816   verifyFormat("class Assign {\n"
13817                "\tvoid f() {\n"
13818                "\t\tint         x      = 123;\n"
13819                "\t\tint         random = 4;\n"
13820                "\t\tstd::string alphabet =\n"
13821                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13822                "\t}\n"
13823                "};",
13824                Tab);
13825   Tab.AlignOperands = FormatStyle::OAS_Align;
13826   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
13827                "                 cccccccccccccccccccc;",
13828                Tab);
13829   // no alignment
13830   verifyFormat("int aaaaaaaaaa =\n"
13831                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
13832                Tab);
13833   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
13834                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
13835                "                        : 333333333333333;",
13836                Tab);
13837   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
13838   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
13839   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
13840                "               + cccccccccccccccccccc;",
13841                Tab);
13842 }
13843 
13844 TEST_F(FormatTest, ZeroTabWidth) {
13845   FormatStyle Tab = getLLVMStyleWithColumns(42);
13846   Tab.IndentWidth = 8;
13847   Tab.UseTab = FormatStyle::UT_Never;
13848   Tab.TabWidth = 0;
13849   EXPECT_EQ("void a(){\n"
13850             "    // line starts with '\t'\n"
13851             "};",
13852             format("void a(){\n"
13853                    "\t// line starts with '\t'\n"
13854                    "};",
13855                    Tab));
13856 
13857   EXPECT_EQ("void a(){\n"
13858             "    // line starts with '\t'\n"
13859             "};",
13860             format("void a(){\n"
13861                    "\t\t// line starts with '\t'\n"
13862                    "};",
13863                    Tab));
13864 
13865   Tab.UseTab = FormatStyle::UT_ForIndentation;
13866   EXPECT_EQ("void a(){\n"
13867             "    // line starts with '\t'\n"
13868             "};",
13869             format("void a(){\n"
13870                    "\t// line starts with '\t'\n"
13871                    "};",
13872                    Tab));
13873 
13874   EXPECT_EQ("void a(){\n"
13875             "    // line starts with '\t'\n"
13876             "};",
13877             format("void a(){\n"
13878                    "\t\t// line starts with '\t'\n"
13879                    "};",
13880                    Tab));
13881 
13882   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13883   EXPECT_EQ("void a(){\n"
13884             "    // line starts with '\t'\n"
13885             "};",
13886             format("void a(){\n"
13887                    "\t// line starts with '\t'\n"
13888                    "};",
13889                    Tab));
13890 
13891   EXPECT_EQ("void a(){\n"
13892             "    // line starts with '\t'\n"
13893             "};",
13894             format("void a(){\n"
13895                    "\t\t// line starts with '\t'\n"
13896                    "};",
13897                    Tab));
13898 
13899   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13900   EXPECT_EQ("void a(){\n"
13901             "    // line starts with '\t'\n"
13902             "};",
13903             format("void a(){\n"
13904                    "\t// line starts with '\t'\n"
13905                    "};",
13906                    Tab));
13907 
13908   EXPECT_EQ("void a(){\n"
13909             "    // line starts with '\t'\n"
13910             "};",
13911             format("void a(){\n"
13912                    "\t\t// line starts with '\t'\n"
13913                    "};",
13914                    Tab));
13915 
13916   Tab.UseTab = FormatStyle::UT_Always;
13917   EXPECT_EQ("void a(){\n"
13918             "// line starts with '\t'\n"
13919             "};",
13920             format("void a(){\n"
13921                    "\t// line starts with '\t'\n"
13922                    "};",
13923                    Tab));
13924 
13925   EXPECT_EQ("void a(){\n"
13926             "// line starts with '\t'\n"
13927             "};",
13928             format("void a(){\n"
13929                    "\t\t// line starts with '\t'\n"
13930                    "};",
13931                    Tab));
13932 }
13933 
13934 TEST_F(FormatTest, CalculatesOriginalColumn) {
13935   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13936             "q\"; /* some\n"
13937             "       comment */",
13938             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13939                    "q\"; /* some\n"
13940                    "       comment */",
13941                    getLLVMStyle()));
13942   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13943             "/* some\n"
13944             "   comment */",
13945             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13946                    " /* some\n"
13947                    "    comment */",
13948                    getLLVMStyle()));
13949   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13950             "qqq\n"
13951             "/* some\n"
13952             "   comment */",
13953             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13954                    "qqq\n"
13955                    " /* some\n"
13956                    "    comment */",
13957                    getLLVMStyle()));
13958   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13959             "wwww; /* some\n"
13960             "         comment */",
13961             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13962                    "wwww; /* some\n"
13963                    "         comment */",
13964                    getLLVMStyle()));
13965 }
13966 
13967 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
13968   FormatStyle NoSpace = getLLVMStyle();
13969   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
13970 
13971   verifyFormat("while(true)\n"
13972                "  continue;",
13973                NoSpace);
13974   verifyFormat("for(;;)\n"
13975                "  continue;",
13976                NoSpace);
13977   verifyFormat("if(true)\n"
13978                "  f();\n"
13979                "else if(true)\n"
13980                "  f();",
13981                NoSpace);
13982   verifyFormat("do {\n"
13983                "  do_something();\n"
13984                "} while(something());",
13985                NoSpace);
13986   verifyFormat("switch(x) {\n"
13987                "default:\n"
13988                "  break;\n"
13989                "}",
13990                NoSpace);
13991   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
13992   verifyFormat("size_t x = sizeof(x);", NoSpace);
13993   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
13994   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
13995   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
13996   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
13997   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
13998   verifyFormat("alignas(128) char a[128];", NoSpace);
13999   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14000   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14001   verifyFormat("int f() throw(Deprecated);", NoSpace);
14002   verifyFormat("typedef void (*cb)(int);", NoSpace);
14003   verifyFormat("T A::operator()();", NoSpace);
14004   verifyFormat("X A::operator++(T);", NoSpace);
14005   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14006 
14007   FormatStyle Space = getLLVMStyle();
14008   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14009 
14010   verifyFormat("int f ();", Space);
14011   verifyFormat("void f (int a, T b) {\n"
14012                "  while (true)\n"
14013                "    continue;\n"
14014                "}",
14015                Space);
14016   verifyFormat("if (true)\n"
14017                "  f ();\n"
14018                "else if (true)\n"
14019                "  f ();",
14020                Space);
14021   verifyFormat("do {\n"
14022                "  do_something ();\n"
14023                "} while (something ());",
14024                Space);
14025   verifyFormat("switch (x) {\n"
14026                "default:\n"
14027                "  break;\n"
14028                "}",
14029                Space);
14030   verifyFormat("A::A () : a (1) {}", Space);
14031   verifyFormat("void f () __attribute__ ((asdf));", Space);
14032   verifyFormat("*(&a + 1);\n"
14033                "&((&a)[1]);\n"
14034                "a[(b + c) * d];\n"
14035                "(((a + 1) * 2) + 3) * 4;",
14036                Space);
14037   verifyFormat("#define A(x) x", Space);
14038   verifyFormat("#define A (x) x", Space);
14039   verifyFormat("#if defined(x)\n"
14040                "#endif",
14041                Space);
14042   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14043   verifyFormat("size_t x = sizeof (x);", Space);
14044   verifyFormat("auto f (int x) -> decltype (x);", Space);
14045   verifyFormat("auto f (int x) -> typeof (x);", Space);
14046   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14047   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14048   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14049   verifyFormat("alignas (128) char a[128];", Space);
14050   verifyFormat("size_t x = alignof (MyType);", Space);
14051   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14052   verifyFormat("int f () throw (Deprecated);", Space);
14053   verifyFormat("typedef void (*cb) (int);", Space);
14054   verifyFormat("T A::operator() ();", Space);
14055   verifyFormat("X A::operator++ (T);", Space);
14056   verifyFormat("auto lambda = [] () { return 0; };", Space);
14057   verifyFormat("int x = int (y);", Space);
14058 
14059   FormatStyle SomeSpace = getLLVMStyle();
14060   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14061 
14062   verifyFormat("[]() -> float {}", SomeSpace);
14063   verifyFormat("[] (auto foo) {}", SomeSpace);
14064   verifyFormat("[foo]() -> int {}", SomeSpace);
14065   verifyFormat("int f();", SomeSpace);
14066   verifyFormat("void f (int a, T b) {\n"
14067                "  while (true)\n"
14068                "    continue;\n"
14069                "}",
14070                SomeSpace);
14071   verifyFormat("if (true)\n"
14072                "  f();\n"
14073                "else if (true)\n"
14074                "  f();",
14075                SomeSpace);
14076   verifyFormat("do {\n"
14077                "  do_something();\n"
14078                "} while (something());",
14079                SomeSpace);
14080   verifyFormat("switch (x) {\n"
14081                "default:\n"
14082                "  break;\n"
14083                "}",
14084                SomeSpace);
14085   verifyFormat("A::A() : a (1) {}", SomeSpace);
14086   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14087   verifyFormat("*(&a + 1);\n"
14088                "&((&a)[1]);\n"
14089                "a[(b + c) * d];\n"
14090                "(((a + 1) * 2) + 3) * 4;",
14091                SomeSpace);
14092   verifyFormat("#define A(x) x", SomeSpace);
14093   verifyFormat("#define A (x) x", SomeSpace);
14094   verifyFormat("#if defined(x)\n"
14095                "#endif",
14096                SomeSpace);
14097   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14098   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14099   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14100   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14101   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14102   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14103   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14104   verifyFormat("alignas (128) char a[128];", SomeSpace);
14105   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14106   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14107                SomeSpace);
14108   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14109   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14110   verifyFormat("T A::operator()();", SomeSpace);
14111   verifyFormat("X A::operator++ (T);", SomeSpace);
14112   verifyFormat("int x = int (y);", SomeSpace);
14113   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14114 }
14115 
14116 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14117   FormatStyle Spaces = getLLVMStyle();
14118   Spaces.SpaceAfterLogicalNot = true;
14119 
14120   verifyFormat("bool x = ! y", Spaces);
14121   verifyFormat("if (! isFailure())", Spaces);
14122   verifyFormat("if (! (a && b))", Spaces);
14123   verifyFormat("\"Error!\"", Spaces);
14124   verifyFormat("! ! x", Spaces);
14125 }
14126 
14127 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14128   FormatStyle Spaces = getLLVMStyle();
14129 
14130   Spaces.SpacesInParentheses = true;
14131   verifyFormat("do_something( ::globalVar );", Spaces);
14132   verifyFormat("call( x, y, z );", Spaces);
14133   verifyFormat("call();", Spaces);
14134   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14135   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14136                Spaces);
14137   verifyFormat("while ( (bool)1 )\n"
14138                "  continue;",
14139                Spaces);
14140   verifyFormat("for ( ;; )\n"
14141                "  continue;",
14142                Spaces);
14143   verifyFormat("if ( true )\n"
14144                "  f();\n"
14145                "else if ( true )\n"
14146                "  f();",
14147                Spaces);
14148   verifyFormat("do {\n"
14149                "  do_something( (int)i );\n"
14150                "} while ( something() );",
14151                Spaces);
14152   verifyFormat("switch ( x ) {\n"
14153                "default:\n"
14154                "  break;\n"
14155                "}",
14156                Spaces);
14157 
14158   Spaces.SpacesInParentheses = false;
14159   Spaces.SpacesInCStyleCastParentheses = true;
14160   verifyFormat("Type *A = ( Type * )P;", Spaces);
14161   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14162   verifyFormat("x = ( int32 )y;", Spaces);
14163   verifyFormat("int a = ( int )(2.0f);", Spaces);
14164   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14165   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14166   verifyFormat("#define x (( int )-1)", Spaces);
14167 
14168   // Run the first set of tests again with:
14169   Spaces.SpacesInParentheses = false;
14170   Spaces.SpaceInEmptyParentheses = true;
14171   Spaces.SpacesInCStyleCastParentheses = true;
14172   verifyFormat("call(x, y, z);", Spaces);
14173   verifyFormat("call( );", Spaces);
14174   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14175   verifyFormat("while (( bool )1)\n"
14176                "  continue;",
14177                Spaces);
14178   verifyFormat("for (;;)\n"
14179                "  continue;",
14180                Spaces);
14181   verifyFormat("if (true)\n"
14182                "  f( );\n"
14183                "else if (true)\n"
14184                "  f( );",
14185                Spaces);
14186   verifyFormat("do {\n"
14187                "  do_something(( int )i);\n"
14188                "} while (something( ));",
14189                Spaces);
14190   verifyFormat("switch (x) {\n"
14191                "default:\n"
14192                "  break;\n"
14193                "}",
14194                Spaces);
14195 
14196   // Run the first set of tests again with:
14197   Spaces.SpaceAfterCStyleCast = true;
14198   verifyFormat("call(x, y, z);", Spaces);
14199   verifyFormat("call( );", Spaces);
14200   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14201   verifyFormat("while (( bool ) 1)\n"
14202                "  continue;",
14203                Spaces);
14204   verifyFormat("for (;;)\n"
14205                "  continue;",
14206                Spaces);
14207   verifyFormat("if (true)\n"
14208                "  f( );\n"
14209                "else if (true)\n"
14210                "  f( );",
14211                Spaces);
14212   verifyFormat("do {\n"
14213                "  do_something(( int ) i);\n"
14214                "} while (something( ));",
14215                Spaces);
14216   verifyFormat("switch (x) {\n"
14217                "default:\n"
14218                "  break;\n"
14219                "}",
14220                Spaces);
14221 
14222   // Run subset of tests again with:
14223   Spaces.SpacesInCStyleCastParentheses = false;
14224   Spaces.SpaceAfterCStyleCast = true;
14225   verifyFormat("while ((bool) 1)\n"
14226                "  continue;",
14227                Spaces);
14228   verifyFormat("do {\n"
14229                "  do_something((int) i);\n"
14230                "} while (something( ));",
14231                Spaces);
14232 
14233   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14234   verifyFormat("size_t idx = (size_t) a;", Spaces);
14235   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14236   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14237   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14238   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14239   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14240   Spaces.ColumnLimit = 80;
14241   Spaces.IndentWidth = 4;
14242   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14243   verifyFormat("void foo( ) {\n"
14244                "    size_t foo = (*(function))(\n"
14245                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14246                "BarrrrrrrrrrrrLong,\n"
14247                "        FoooooooooLooooong);\n"
14248                "}",
14249                Spaces);
14250   Spaces.SpaceAfterCStyleCast = false;
14251   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
14252   verifyFormat("size_t idx = (size_t)a;", Spaces);
14253   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
14254   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14255   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14256   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14257   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14258 
14259   verifyFormat("void foo( ) {\n"
14260                "    size_t foo = (*(function))(\n"
14261                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14262                "BarrrrrrrrrrrrLong,\n"
14263                "        FoooooooooLooooong);\n"
14264                "}",
14265                Spaces);
14266 }
14267 
14268 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
14269   verifyFormat("int a[5];");
14270   verifyFormat("a[3] += 42;");
14271 
14272   FormatStyle Spaces = getLLVMStyle();
14273   Spaces.SpacesInSquareBrackets = true;
14274   // Not lambdas.
14275   verifyFormat("int a[ 5 ];", Spaces);
14276   verifyFormat("a[ 3 ] += 42;", Spaces);
14277   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
14278   verifyFormat("double &operator[](int i) { return 0; }\n"
14279                "int i;",
14280                Spaces);
14281   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
14282   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
14283   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
14284   // Lambdas.
14285   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
14286   verifyFormat("return [ i, args... ] {};", Spaces);
14287   verifyFormat("int foo = [ &bar ]() {};", Spaces);
14288   verifyFormat("int foo = [ = ]() {};", Spaces);
14289   verifyFormat("int foo = [ & ]() {};", Spaces);
14290   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
14291   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
14292 }
14293 
14294 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
14295   FormatStyle NoSpaceStyle = getLLVMStyle();
14296   verifyFormat("int a[5];", NoSpaceStyle);
14297   verifyFormat("a[3] += 42;", NoSpaceStyle);
14298 
14299   verifyFormat("int a[1];", NoSpaceStyle);
14300   verifyFormat("int 1 [a];", NoSpaceStyle);
14301   verifyFormat("int a[1][2];", NoSpaceStyle);
14302   verifyFormat("a[7] = 5;", NoSpaceStyle);
14303   verifyFormat("int a = (f())[23];", NoSpaceStyle);
14304   verifyFormat("f([] {})", NoSpaceStyle);
14305 
14306   FormatStyle Space = getLLVMStyle();
14307   Space.SpaceBeforeSquareBrackets = true;
14308   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
14309   verifyFormat("return [i, args...] {};", Space);
14310 
14311   verifyFormat("int a [5];", Space);
14312   verifyFormat("a [3] += 42;", Space);
14313   verifyFormat("constexpr char hello []{\"hello\"};", Space);
14314   verifyFormat("double &operator[](int i) { return 0; }\n"
14315                "int i;",
14316                Space);
14317   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
14318   verifyFormat("int i = a [a][a]->f();", Space);
14319   verifyFormat("int i = (*b) [a]->f();", Space);
14320 
14321   verifyFormat("int a [1];", Space);
14322   verifyFormat("int 1 [a];", Space);
14323   verifyFormat("int a [1][2];", Space);
14324   verifyFormat("a [7] = 5;", Space);
14325   verifyFormat("int a = (f()) [23];", Space);
14326   verifyFormat("f([] {})", Space);
14327 }
14328 
14329 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
14330   verifyFormat("int a = 5;");
14331   verifyFormat("a += 42;");
14332   verifyFormat("a or_eq 8;");
14333 
14334   FormatStyle Spaces = getLLVMStyle();
14335   Spaces.SpaceBeforeAssignmentOperators = false;
14336   verifyFormat("int a= 5;", Spaces);
14337   verifyFormat("a+= 42;", Spaces);
14338   verifyFormat("a or_eq 8;", Spaces);
14339 }
14340 
14341 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
14342   verifyFormat("class Foo : public Bar {};");
14343   verifyFormat("Foo::Foo() : foo(1) {}");
14344   verifyFormat("for (auto a : b) {\n}");
14345   verifyFormat("int x = a ? b : c;");
14346   verifyFormat("{\n"
14347                "label0:\n"
14348                "  int x = 0;\n"
14349                "}");
14350   verifyFormat("switch (x) {\n"
14351                "case 1:\n"
14352                "default:\n"
14353                "}");
14354   verifyFormat("switch (allBraces) {\n"
14355                "case 1: {\n"
14356                "  break;\n"
14357                "}\n"
14358                "case 2: {\n"
14359                "  [[fallthrough]];\n"
14360                "}\n"
14361                "default: {\n"
14362                "  break;\n"
14363                "}\n"
14364                "}");
14365 
14366   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
14367   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
14368   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
14369   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
14370   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
14371   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
14372   verifyFormat("{\n"
14373                "label1:\n"
14374                "  int x = 0;\n"
14375                "}",
14376                CtorInitializerStyle);
14377   verifyFormat("switch (x) {\n"
14378                "case 1:\n"
14379                "default:\n"
14380                "}",
14381                CtorInitializerStyle);
14382   verifyFormat("switch (allBraces) {\n"
14383                "case 1: {\n"
14384                "  break;\n"
14385                "}\n"
14386                "case 2: {\n"
14387                "  [[fallthrough]];\n"
14388                "}\n"
14389                "default: {\n"
14390                "  break;\n"
14391                "}\n"
14392                "}",
14393                CtorInitializerStyle);
14394   CtorInitializerStyle.BreakConstructorInitializers =
14395       FormatStyle::BCIS_AfterColon;
14396   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
14397                "    aaaaaaaaaaaaaaaa(1),\n"
14398                "    bbbbbbbbbbbbbbbb(2) {}",
14399                CtorInitializerStyle);
14400   CtorInitializerStyle.BreakConstructorInitializers =
14401       FormatStyle::BCIS_BeforeComma;
14402   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14403                "    : aaaaaaaaaaaaaaaa(1)\n"
14404                "    , bbbbbbbbbbbbbbbb(2) {}",
14405                CtorInitializerStyle);
14406   CtorInitializerStyle.BreakConstructorInitializers =
14407       FormatStyle::BCIS_BeforeColon;
14408   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14409                "    : aaaaaaaaaaaaaaaa(1),\n"
14410                "      bbbbbbbbbbbbbbbb(2) {}",
14411                CtorInitializerStyle);
14412   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
14413   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14414                ": aaaaaaaaaaaaaaaa(1),\n"
14415                "  bbbbbbbbbbbbbbbb(2) {}",
14416                CtorInitializerStyle);
14417 
14418   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
14419   InheritanceStyle.SpaceBeforeInheritanceColon = false;
14420   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
14421   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
14422   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
14423   verifyFormat("int x = a ? b : c;", InheritanceStyle);
14424   verifyFormat("{\n"
14425                "label2:\n"
14426                "  int x = 0;\n"
14427                "}",
14428                InheritanceStyle);
14429   verifyFormat("switch (x) {\n"
14430                "case 1:\n"
14431                "default:\n"
14432                "}",
14433                InheritanceStyle);
14434   verifyFormat("switch (allBraces) {\n"
14435                "case 1: {\n"
14436                "  break;\n"
14437                "}\n"
14438                "case 2: {\n"
14439                "  [[fallthrough]];\n"
14440                "}\n"
14441                "default: {\n"
14442                "  break;\n"
14443                "}\n"
14444                "}",
14445                InheritanceStyle);
14446   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
14447   verifyFormat("class Foooooooooooooooooooooo\n"
14448                "    : public aaaaaaaaaaaaaaaaaa,\n"
14449                "      public bbbbbbbbbbbbbbbbbb {\n"
14450                "}",
14451                InheritanceStyle);
14452   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
14453   verifyFormat("class Foooooooooooooooooooooo:\n"
14454                "    public aaaaaaaaaaaaaaaaaa,\n"
14455                "    public bbbbbbbbbbbbbbbbbb {\n"
14456                "}",
14457                InheritanceStyle);
14458   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
14459   verifyFormat("class Foooooooooooooooooooooo\n"
14460                "    : public aaaaaaaaaaaaaaaaaa\n"
14461                "    , public bbbbbbbbbbbbbbbbbb {\n"
14462                "}",
14463                InheritanceStyle);
14464   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
14465   verifyFormat("class Foooooooooooooooooooooo\n"
14466                "    : public aaaaaaaaaaaaaaaaaa,\n"
14467                "      public bbbbbbbbbbbbbbbbbb {\n"
14468                "}",
14469                InheritanceStyle);
14470   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
14471   verifyFormat("class Foooooooooooooooooooooo\n"
14472                ": public aaaaaaaaaaaaaaaaaa,\n"
14473                "  public bbbbbbbbbbbbbbbbbb {}",
14474                InheritanceStyle);
14475 
14476   FormatStyle ForLoopStyle = getLLVMStyle();
14477   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
14478   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
14479   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
14480   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
14481   verifyFormat("int x = a ? b : c;", ForLoopStyle);
14482   verifyFormat("{\n"
14483                "label2:\n"
14484                "  int x = 0;\n"
14485                "}",
14486                ForLoopStyle);
14487   verifyFormat("switch (x) {\n"
14488                "case 1:\n"
14489                "default:\n"
14490                "}",
14491                ForLoopStyle);
14492   verifyFormat("switch (allBraces) {\n"
14493                "case 1: {\n"
14494                "  break;\n"
14495                "}\n"
14496                "case 2: {\n"
14497                "  [[fallthrough]];\n"
14498                "}\n"
14499                "default: {\n"
14500                "  break;\n"
14501                "}\n"
14502                "}",
14503                ForLoopStyle);
14504 
14505   FormatStyle CaseStyle = getLLVMStyle();
14506   CaseStyle.SpaceBeforeCaseColon = true;
14507   verifyFormat("class Foo : public Bar {};", CaseStyle);
14508   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
14509   verifyFormat("for (auto a : b) {\n}", CaseStyle);
14510   verifyFormat("int x = a ? b : c;", CaseStyle);
14511   verifyFormat("switch (x) {\n"
14512                "case 1 :\n"
14513                "default :\n"
14514                "}",
14515                CaseStyle);
14516   verifyFormat("switch (allBraces) {\n"
14517                "case 1 : {\n"
14518                "  break;\n"
14519                "}\n"
14520                "case 2 : {\n"
14521                "  [[fallthrough]];\n"
14522                "}\n"
14523                "default : {\n"
14524                "  break;\n"
14525                "}\n"
14526                "}",
14527                CaseStyle);
14528 
14529   FormatStyle NoSpaceStyle = getLLVMStyle();
14530   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
14531   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14532   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
14533   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14534   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
14535   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
14536   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
14537   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
14538   verifyFormat("{\n"
14539                "label3:\n"
14540                "  int x = 0;\n"
14541                "}",
14542                NoSpaceStyle);
14543   verifyFormat("switch (x) {\n"
14544                "case 1:\n"
14545                "default:\n"
14546                "}",
14547                NoSpaceStyle);
14548   verifyFormat("switch (allBraces) {\n"
14549                "case 1: {\n"
14550                "  break;\n"
14551                "}\n"
14552                "case 2: {\n"
14553                "  [[fallthrough]];\n"
14554                "}\n"
14555                "default: {\n"
14556                "  break;\n"
14557                "}\n"
14558                "}",
14559                NoSpaceStyle);
14560 
14561   FormatStyle InvertedSpaceStyle = getLLVMStyle();
14562   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
14563   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14564   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
14565   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14566   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
14567   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
14568   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
14569   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
14570   verifyFormat("{\n"
14571                "label3:\n"
14572                "  int x = 0;\n"
14573                "}",
14574                InvertedSpaceStyle);
14575   verifyFormat("switch (x) {\n"
14576                "case 1 :\n"
14577                "case 2 : {\n"
14578                "  break;\n"
14579                "}\n"
14580                "default :\n"
14581                "  break;\n"
14582                "}",
14583                InvertedSpaceStyle);
14584   verifyFormat("switch (allBraces) {\n"
14585                "case 1 : {\n"
14586                "  break;\n"
14587                "}\n"
14588                "case 2 : {\n"
14589                "  [[fallthrough]];\n"
14590                "}\n"
14591                "default : {\n"
14592                "  break;\n"
14593                "}\n"
14594                "}",
14595                InvertedSpaceStyle);
14596 }
14597 
14598 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
14599   FormatStyle Style = getLLVMStyle();
14600 
14601   Style.PointerAlignment = FormatStyle::PAS_Left;
14602   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14603   verifyFormat("void* const* x = NULL;", Style);
14604 
14605 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
14606   do {                                                                         \
14607     Style.PointerAlignment = FormatStyle::Pointers;                            \
14608     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
14609     verifyFormat(Code, Style);                                                 \
14610   } while (false)
14611 
14612   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
14613   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
14614   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
14615 
14616   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
14617   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
14618   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
14619 
14620   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
14621   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
14622   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
14623 
14624   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
14625   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
14626   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
14627 
14628   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
14629   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14630                         SAPQ_Default);
14631   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14632                         SAPQ_Default);
14633 
14634   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
14635   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14636                         SAPQ_Before);
14637   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14638                         SAPQ_Before);
14639 
14640   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
14641   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
14642   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14643                         SAPQ_After);
14644 
14645   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
14646   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
14647   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
14648 
14649 #undef verifyQualifierSpaces
14650 
14651   FormatStyle Spaces = getLLVMStyle();
14652   Spaces.AttributeMacros.push_back("qualified");
14653   Spaces.PointerAlignment = FormatStyle::PAS_Right;
14654   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14655   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
14656   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
14657   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
14658   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
14659   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14660   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14661   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
14662   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
14663   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14664   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14665   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14666 
14667   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
14668   Spaces.PointerAlignment = FormatStyle::PAS_Left;
14669   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14670   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
14671   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
14672   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
14673   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
14674   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14675   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
14676   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14677   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
14678   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
14679   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
14680   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
14681   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14682 
14683   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
14684   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
14685   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14686   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
14687   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
14688   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14689   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14690   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14691 }
14692 
14693 TEST_F(FormatTest, AlignConsecutiveMacros) {
14694   FormatStyle Style = getLLVMStyle();
14695   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14696   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14697   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14698 
14699   verifyFormat("#define a 3\n"
14700                "#define bbbb 4\n"
14701                "#define ccc (5)",
14702                Style);
14703 
14704   verifyFormat("#define f(x) (x * x)\n"
14705                "#define fff(x, y, z) (x * y + z)\n"
14706                "#define ffff(x, y) (x - y)",
14707                Style);
14708 
14709   verifyFormat("#define foo(x, y) (x + y)\n"
14710                "#define bar (5, 6)(2 + 2)",
14711                Style);
14712 
14713   verifyFormat("#define a 3\n"
14714                "#define bbbb 4\n"
14715                "#define ccc (5)\n"
14716                "#define f(x) (x * x)\n"
14717                "#define fff(x, y, z) (x * y + z)\n"
14718                "#define ffff(x, y) (x - y)",
14719                Style);
14720 
14721   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14722   verifyFormat("#define a    3\n"
14723                "#define bbbb 4\n"
14724                "#define ccc  (5)",
14725                Style);
14726 
14727   verifyFormat("#define f(x)         (x * x)\n"
14728                "#define fff(x, y, z) (x * y + z)\n"
14729                "#define ffff(x, y)   (x - y)",
14730                Style);
14731 
14732   verifyFormat("#define foo(x, y) (x + y)\n"
14733                "#define bar       (5, 6)(2 + 2)",
14734                Style);
14735 
14736   verifyFormat("#define a            3\n"
14737                "#define bbbb         4\n"
14738                "#define ccc          (5)\n"
14739                "#define f(x)         (x * x)\n"
14740                "#define fff(x, y, z) (x * y + z)\n"
14741                "#define ffff(x, y)   (x - y)",
14742                Style);
14743 
14744   verifyFormat("#define a         5\n"
14745                "#define foo(x, y) (x + y)\n"
14746                "#define CCC       (6)\n"
14747                "auto lambda = []() {\n"
14748                "  auto  ii = 0;\n"
14749                "  float j  = 0;\n"
14750                "  return 0;\n"
14751                "};\n"
14752                "int   i  = 0;\n"
14753                "float i2 = 0;\n"
14754                "auto  v  = type{\n"
14755                "    i = 1,   //\n"
14756                "    (i = 2), //\n"
14757                "    i = 3    //\n"
14758                "};",
14759                Style);
14760 
14761   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14762   Style.ColumnLimit = 20;
14763 
14764   verifyFormat("#define a          \\\n"
14765                "  \"aabbbbbbbbbbbb\"\n"
14766                "#define D          \\\n"
14767                "  \"aabbbbbbbbbbbb\" \\\n"
14768                "  \"ccddeeeeeeeee\"\n"
14769                "#define B          \\\n"
14770                "  \"QQQQQQQQQQQQQ\"  \\\n"
14771                "  \"FFFFFFFFFFFFF\"  \\\n"
14772                "  \"LLLLLLLL\"\n",
14773                Style);
14774 
14775   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14776   verifyFormat("#define a          \\\n"
14777                "  \"aabbbbbbbbbbbb\"\n"
14778                "#define D          \\\n"
14779                "  \"aabbbbbbbbbbbb\" \\\n"
14780                "  \"ccddeeeeeeeee\"\n"
14781                "#define B          \\\n"
14782                "  \"QQQQQQQQQQQQQ\"  \\\n"
14783                "  \"FFFFFFFFFFFFF\"  \\\n"
14784                "  \"LLLLLLLL\"\n",
14785                Style);
14786 
14787   // Test across comments
14788   Style.MaxEmptyLinesToKeep = 10;
14789   Style.ReflowComments = false;
14790   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
14791   EXPECT_EQ("#define a    3\n"
14792             "// line comment\n"
14793             "#define bbbb 4\n"
14794             "#define ccc  (5)",
14795             format("#define a 3\n"
14796                    "// line comment\n"
14797                    "#define bbbb 4\n"
14798                    "#define ccc (5)",
14799                    Style));
14800 
14801   EXPECT_EQ("#define a    3\n"
14802             "/* block comment */\n"
14803             "#define bbbb 4\n"
14804             "#define ccc  (5)",
14805             format("#define a  3\n"
14806                    "/* block comment */\n"
14807                    "#define bbbb 4\n"
14808                    "#define ccc (5)",
14809                    Style));
14810 
14811   EXPECT_EQ("#define a    3\n"
14812             "/* multi-line *\n"
14813             " * block comment */\n"
14814             "#define bbbb 4\n"
14815             "#define ccc  (5)",
14816             format("#define a 3\n"
14817                    "/* multi-line *\n"
14818                    " * block comment */\n"
14819                    "#define bbbb 4\n"
14820                    "#define ccc (5)",
14821                    Style));
14822 
14823   EXPECT_EQ("#define a    3\n"
14824             "// multi-line line comment\n"
14825             "//\n"
14826             "#define bbbb 4\n"
14827             "#define ccc  (5)",
14828             format("#define a  3\n"
14829                    "// multi-line line comment\n"
14830                    "//\n"
14831                    "#define bbbb 4\n"
14832                    "#define ccc (5)",
14833                    Style));
14834 
14835   EXPECT_EQ("#define a 3\n"
14836             "// empty lines still break.\n"
14837             "\n"
14838             "#define bbbb 4\n"
14839             "#define ccc  (5)",
14840             format("#define a     3\n"
14841                    "// empty lines still break.\n"
14842                    "\n"
14843                    "#define bbbb     4\n"
14844                    "#define ccc  (5)",
14845                    Style));
14846 
14847   // Test across empty lines
14848   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
14849   EXPECT_EQ("#define a    3\n"
14850             "\n"
14851             "#define bbbb 4\n"
14852             "#define ccc  (5)",
14853             format("#define a 3\n"
14854                    "\n"
14855                    "#define bbbb 4\n"
14856                    "#define ccc (5)",
14857                    Style));
14858 
14859   EXPECT_EQ("#define a    3\n"
14860             "\n"
14861             "\n"
14862             "\n"
14863             "#define bbbb 4\n"
14864             "#define ccc  (5)",
14865             format("#define a        3\n"
14866                    "\n"
14867                    "\n"
14868                    "\n"
14869                    "#define bbbb 4\n"
14870                    "#define ccc (5)",
14871                    Style));
14872 
14873   EXPECT_EQ("#define a 3\n"
14874             "// comments should break alignment\n"
14875             "//\n"
14876             "#define bbbb 4\n"
14877             "#define ccc  (5)",
14878             format("#define a        3\n"
14879                    "// comments should break alignment\n"
14880                    "//\n"
14881                    "#define bbbb 4\n"
14882                    "#define ccc (5)",
14883                    Style));
14884 
14885   // Test across empty lines and comments
14886   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
14887   verifyFormat("#define a    3\n"
14888                "\n"
14889                "// line comment\n"
14890                "#define bbbb 4\n"
14891                "#define ccc  (5)",
14892                Style);
14893 
14894   EXPECT_EQ("#define a    3\n"
14895             "\n"
14896             "\n"
14897             "/* multi-line *\n"
14898             " * block comment */\n"
14899             "\n"
14900             "\n"
14901             "#define bbbb 4\n"
14902             "#define ccc  (5)",
14903             format("#define a 3\n"
14904                    "\n"
14905                    "\n"
14906                    "/* multi-line *\n"
14907                    " * block comment */\n"
14908                    "\n"
14909                    "\n"
14910                    "#define bbbb 4\n"
14911                    "#define ccc (5)",
14912                    Style));
14913 
14914   EXPECT_EQ("#define a    3\n"
14915             "\n"
14916             "\n"
14917             "/* multi-line *\n"
14918             " * block comment */\n"
14919             "\n"
14920             "\n"
14921             "#define bbbb 4\n"
14922             "#define ccc  (5)",
14923             format("#define a 3\n"
14924                    "\n"
14925                    "\n"
14926                    "/* multi-line *\n"
14927                    " * block comment */\n"
14928                    "\n"
14929                    "\n"
14930                    "#define bbbb 4\n"
14931                    "#define ccc       (5)",
14932                    Style));
14933 }
14934 
14935 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
14936   FormatStyle Alignment = getLLVMStyle();
14937   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14938   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
14939 
14940   Alignment.MaxEmptyLinesToKeep = 10;
14941   /* Test alignment across empty lines */
14942   EXPECT_EQ("int a           = 5;\n"
14943             "\n"
14944             "int oneTwoThree = 123;",
14945             format("int a       = 5;\n"
14946                    "\n"
14947                    "int oneTwoThree= 123;",
14948                    Alignment));
14949   EXPECT_EQ("int a           = 5;\n"
14950             "int one         = 1;\n"
14951             "\n"
14952             "int oneTwoThree = 123;",
14953             format("int a = 5;\n"
14954                    "int one = 1;\n"
14955                    "\n"
14956                    "int oneTwoThree = 123;",
14957                    Alignment));
14958   EXPECT_EQ("int a           = 5;\n"
14959             "int one         = 1;\n"
14960             "\n"
14961             "int oneTwoThree = 123;\n"
14962             "int oneTwo      = 12;",
14963             format("int a = 5;\n"
14964                    "int one = 1;\n"
14965                    "\n"
14966                    "int oneTwoThree = 123;\n"
14967                    "int oneTwo = 12;",
14968                    Alignment));
14969 
14970   /* Test across comments */
14971   EXPECT_EQ("int a = 5;\n"
14972             "/* block comment */\n"
14973             "int oneTwoThree = 123;",
14974             format("int a = 5;\n"
14975                    "/* block comment */\n"
14976                    "int oneTwoThree=123;",
14977                    Alignment));
14978 
14979   EXPECT_EQ("int a = 5;\n"
14980             "// line comment\n"
14981             "int oneTwoThree = 123;",
14982             format("int a = 5;\n"
14983                    "// line comment\n"
14984                    "int oneTwoThree=123;",
14985                    Alignment));
14986 
14987   /* Test across comments and newlines */
14988   EXPECT_EQ("int a = 5;\n"
14989             "\n"
14990             "/* block comment */\n"
14991             "int oneTwoThree = 123;",
14992             format("int a = 5;\n"
14993                    "\n"
14994                    "/* block comment */\n"
14995                    "int oneTwoThree=123;",
14996                    Alignment));
14997 
14998   EXPECT_EQ("int a = 5;\n"
14999             "\n"
15000             "// line comment\n"
15001             "int oneTwoThree = 123;",
15002             format("int a = 5;\n"
15003                    "\n"
15004                    "// line comment\n"
15005                    "int oneTwoThree=123;",
15006                    Alignment));
15007 }
15008 
15009 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15010   FormatStyle Alignment = getLLVMStyle();
15011   Alignment.AlignConsecutiveDeclarations =
15012       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15013   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15014 
15015   Alignment.MaxEmptyLinesToKeep = 10;
15016   /* Test alignment across empty lines */
15017   EXPECT_EQ("int         a = 5;\n"
15018             "\n"
15019             "float const oneTwoThree = 123;",
15020             format("int a = 5;\n"
15021                    "\n"
15022                    "float const oneTwoThree = 123;",
15023                    Alignment));
15024   EXPECT_EQ("int         a = 5;\n"
15025             "float const one = 1;\n"
15026             "\n"
15027             "int         oneTwoThree = 123;",
15028             format("int a = 5;\n"
15029                    "float const one = 1;\n"
15030                    "\n"
15031                    "int oneTwoThree = 123;",
15032                    Alignment));
15033 
15034   /* Test across comments */
15035   EXPECT_EQ("float const a = 5;\n"
15036             "/* block comment */\n"
15037             "int         oneTwoThree = 123;",
15038             format("float const a = 5;\n"
15039                    "/* block comment */\n"
15040                    "int oneTwoThree=123;",
15041                    Alignment));
15042 
15043   EXPECT_EQ("float const a = 5;\n"
15044             "// line comment\n"
15045             "int         oneTwoThree = 123;",
15046             format("float const a = 5;\n"
15047                    "// line comment\n"
15048                    "int oneTwoThree=123;",
15049                    Alignment));
15050 
15051   /* Test across comments and newlines */
15052   EXPECT_EQ("float const a = 5;\n"
15053             "\n"
15054             "/* block comment */\n"
15055             "int         oneTwoThree = 123;",
15056             format("float const a = 5;\n"
15057                    "\n"
15058                    "/* block comment */\n"
15059                    "int         oneTwoThree=123;",
15060                    Alignment));
15061 
15062   EXPECT_EQ("float const a = 5;\n"
15063             "\n"
15064             "// line comment\n"
15065             "int         oneTwoThree = 123;",
15066             format("float const a = 5;\n"
15067                    "\n"
15068                    "// line comment\n"
15069                    "int oneTwoThree=123;",
15070                    Alignment));
15071 }
15072 
15073 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15074   FormatStyle Alignment = getLLVMStyle();
15075   Alignment.AlignConsecutiveBitFields =
15076       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15077 
15078   Alignment.MaxEmptyLinesToKeep = 10;
15079   /* Test alignment across empty lines */
15080   EXPECT_EQ("int a            : 5;\n"
15081             "\n"
15082             "int longbitfield : 6;",
15083             format("int a : 5;\n"
15084                    "\n"
15085                    "int longbitfield : 6;",
15086                    Alignment));
15087   EXPECT_EQ("int a            : 5;\n"
15088             "int one          : 1;\n"
15089             "\n"
15090             "int longbitfield : 6;",
15091             format("int a : 5;\n"
15092                    "int one : 1;\n"
15093                    "\n"
15094                    "int longbitfield : 6;",
15095                    Alignment));
15096 
15097   /* Test across comments */
15098   EXPECT_EQ("int a            : 5;\n"
15099             "/* block comment */\n"
15100             "int longbitfield : 6;",
15101             format("int a : 5;\n"
15102                    "/* block comment */\n"
15103                    "int longbitfield : 6;",
15104                    Alignment));
15105   EXPECT_EQ("int a            : 5;\n"
15106             "int one          : 1;\n"
15107             "// line comment\n"
15108             "int longbitfield : 6;",
15109             format("int a : 5;\n"
15110                    "int one : 1;\n"
15111                    "// line comment\n"
15112                    "int longbitfield : 6;",
15113                    Alignment));
15114 
15115   /* Test across comments and newlines */
15116   EXPECT_EQ("int a            : 5;\n"
15117             "/* block comment */\n"
15118             "\n"
15119             "int longbitfield : 6;",
15120             format("int a : 5;\n"
15121                    "/* block comment */\n"
15122                    "\n"
15123                    "int longbitfield : 6;",
15124                    Alignment));
15125   EXPECT_EQ("int a            : 5;\n"
15126             "int one          : 1;\n"
15127             "\n"
15128             "// line comment\n"
15129             "\n"
15130             "int longbitfield : 6;",
15131             format("int a : 5;\n"
15132                    "int one : 1;\n"
15133                    "\n"
15134                    "// line comment \n"
15135                    "\n"
15136                    "int longbitfield : 6;",
15137                    Alignment));
15138 }
15139 
15140 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15141   FormatStyle Alignment = getLLVMStyle();
15142   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15143   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15144 
15145   Alignment.MaxEmptyLinesToKeep = 10;
15146   /* Test alignment across empty lines */
15147   EXPECT_EQ("int a = 5;\n"
15148             "\n"
15149             "int oneTwoThree = 123;",
15150             format("int a       = 5;\n"
15151                    "\n"
15152                    "int oneTwoThree= 123;",
15153                    Alignment));
15154   EXPECT_EQ("int a   = 5;\n"
15155             "int one = 1;\n"
15156             "\n"
15157             "int oneTwoThree = 123;",
15158             format("int a = 5;\n"
15159                    "int one = 1;\n"
15160                    "\n"
15161                    "int oneTwoThree = 123;",
15162                    Alignment));
15163 
15164   /* Test across comments */
15165   EXPECT_EQ("int a           = 5;\n"
15166             "/* block comment */\n"
15167             "int oneTwoThree = 123;",
15168             format("int a = 5;\n"
15169                    "/* block comment */\n"
15170                    "int oneTwoThree=123;",
15171                    Alignment));
15172 
15173   EXPECT_EQ("int a           = 5;\n"
15174             "// line comment\n"
15175             "int oneTwoThree = 123;",
15176             format("int a = 5;\n"
15177                    "// line comment\n"
15178                    "int oneTwoThree=123;",
15179                    Alignment));
15180 
15181   EXPECT_EQ("int a           = 5;\n"
15182             "/*\n"
15183             " * multi-line block comment\n"
15184             " */\n"
15185             "int oneTwoThree = 123;",
15186             format("int a = 5;\n"
15187                    "/*\n"
15188                    " * multi-line block comment\n"
15189                    " */\n"
15190                    "int oneTwoThree=123;",
15191                    Alignment));
15192 
15193   EXPECT_EQ("int a           = 5;\n"
15194             "//\n"
15195             "// multi-line line comment\n"
15196             "//\n"
15197             "int oneTwoThree = 123;",
15198             format("int a = 5;\n"
15199                    "//\n"
15200                    "// multi-line line comment\n"
15201                    "//\n"
15202                    "int oneTwoThree=123;",
15203                    Alignment));
15204 
15205   /* Test across comments and newlines */
15206   EXPECT_EQ("int a = 5;\n"
15207             "\n"
15208             "/* block comment */\n"
15209             "int oneTwoThree = 123;",
15210             format("int a = 5;\n"
15211                    "\n"
15212                    "/* block comment */\n"
15213                    "int oneTwoThree=123;",
15214                    Alignment));
15215 
15216   EXPECT_EQ("int a = 5;\n"
15217             "\n"
15218             "// line comment\n"
15219             "int oneTwoThree = 123;",
15220             format("int a = 5;\n"
15221                    "\n"
15222                    "// line comment\n"
15223                    "int oneTwoThree=123;",
15224                    Alignment));
15225 }
15226 
15227 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15228   FormatStyle Alignment = getLLVMStyle();
15229   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15230   Alignment.AlignConsecutiveAssignments =
15231       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15232   verifyFormat("int a           = 5;\n"
15233                "int oneTwoThree = 123;",
15234                Alignment);
15235   verifyFormat("int a           = method();\n"
15236                "int oneTwoThree = 133;",
15237                Alignment);
15238   verifyFormat("a &= 5;\n"
15239                "bcd *= 5;\n"
15240                "ghtyf += 5;\n"
15241                "dvfvdb -= 5;\n"
15242                "a /= 5;\n"
15243                "vdsvsv %= 5;\n"
15244                "sfdbddfbdfbb ^= 5;\n"
15245                "dvsdsv |= 5;\n"
15246                "int dsvvdvsdvvv = 123;",
15247                Alignment);
15248   verifyFormat("int i = 1, j = 10;\n"
15249                "something = 2000;",
15250                Alignment);
15251   verifyFormat("something = 2000;\n"
15252                "int i = 1, j = 10;\n",
15253                Alignment);
15254   verifyFormat("something = 2000;\n"
15255                "another   = 911;\n"
15256                "int i = 1, j = 10;\n"
15257                "oneMore = 1;\n"
15258                "i       = 2;",
15259                Alignment);
15260   verifyFormat("int a   = 5;\n"
15261                "int one = 1;\n"
15262                "method();\n"
15263                "int oneTwoThree = 123;\n"
15264                "int oneTwo      = 12;",
15265                Alignment);
15266   verifyFormat("int oneTwoThree = 123;\n"
15267                "int oneTwo      = 12;\n"
15268                "method();\n",
15269                Alignment);
15270   verifyFormat("int oneTwoThree = 123; // comment\n"
15271                "int oneTwo      = 12;  // comment",
15272                Alignment);
15273 
15274   // Bug 25167
15275   /* Uncomment when fixed
15276     verifyFormat("#if A\n"
15277                  "#else\n"
15278                  "int aaaaaaaa = 12;\n"
15279                  "#endif\n"
15280                  "#if B\n"
15281                  "#else\n"
15282                  "int a = 12;\n"
15283                  "#endif\n",
15284                  Alignment);
15285     verifyFormat("enum foo {\n"
15286                  "#if A\n"
15287                  "#else\n"
15288                  "  aaaaaaaa = 12;\n"
15289                  "#endif\n"
15290                  "#if B\n"
15291                  "#else\n"
15292                  "  a = 12;\n"
15293                  "#endif\n"
15294                  "};\n",
15295                  Alignment);
15296   */
15297 
15298   Alignment.MaxEmptyLinesToKeep = 10;
15299   /* Test alignment across empty lines */
15300   EXPECT_EQ("int a           = 5;\n"
15301             "\n"
15302             "int oneTwoThree = 123;",
15303             format("int a       = 5;\n"
15304                    "\n"
15305                    "int oneTwoThree= 123;",
15306                    Alignment));
15307   EXPECT_EQ("int a           = 5;\n"
15308             "int one         = 1;\n"
15309             "\n"
15310             "int oneTwoThree = 123;",
15311             format("int a = 5;\n"
15312                    "int one = 1;\n"
15313                    "\n"
15314                    "int oneTwoThree = 123;",
15315                    Alignment));
15316   EXPECT_EQ("int a           = 5;\n"
15317             "int one         = 1;\n"
15318             "\n"
15319             "int oneTwoThree = 123;\n"
15320             "int oneTwo      = 12;",
15321             format("int a = 5;\n"
15322                    "int one = 1;\n"
15323                    "\n"
15324                    "int oneTwoThree = 123;\n"
15325                    "int oneTwo = 12;",
15326                    Alignment));
15327 
15328   /* Test across comments */
15329   EXPECT_EQ("int a           = 5;\n"
15330             "/* block comment */\n"
15331             "int oneTwoThree = 123;",
15332             format("int a = 5;\n"
15333                    "/* block comment */\n"
15334                    "int oneTwoThree=123;",
15335                    Alignment));
15336 
15337   EXPECT_EQ("int a           = 5;\n"
15338             "// line comment\n"
15339             "int oneTwoThree = 123;",
15340             format("int a = 5;\n"
15341                    "// line comment\n"
15342                    "int oneTwoThree=123;",
15343                    Alignment));
15344 
15345   /* Test across comments and newlines */
15346   EXPECT_EQ("int a           = 5;\n"
15347             "\n"
15348             "/* block comment */\n"
15349             "int oneTwoThree = 123;",
15350             format("int a = 5;\n"
15351                    "\n"
15352                    "/* block comment */\n"
15353                    "int oneTwoThree=123;",
15354                    Alignment));
15355 
15356   EXPECT_EQ("int a           = 5;\n"
15357             "\n"
15358             "// line comment\n"
15359             "int oneTwoThree = 123;",
15360             format("int a = 5;\n"
15361                    "\n"
15362                    "// line comment\n"
15363                    "int oneTwoThree=123;",
15364                    Alignment));
15365 
15366   EXPECT_EQ("int a           = 5;\n"
15367             "//\n"
15368             "// multi-line line comment\n"
15369             "//\n"
15370             "int oneTwoThree = 123;",
15371             format("int a = 5;\n"
15372                    "//\n"
15373                    "// multi-line line comment\n"
15374                    "//\n"
15375                    "int oneTwoThree=123;",
15376                    Alignment));
15377 
15378   EXPECT_EQ("int a           = 5;\n"
15379             "/*\n"
15380             " *  multi-line block comment\n"
15381             " */\n"
15382             "int oneTwoThree = 123;",
15383             format("int a = 5;\n"
15384                    "/*\n"
15385                    " *  multi-line block comment\n"
15386                    " */\n"
15387                    "int oneTwoThree=123;",
15388                    Alignment));
15389 
15390   EXPECT_EQ("int a           = 5;\n"
15391             "\n"
15392             "/* block comment */\n"
15393             "\n"
15394             "\n"
15395             "\n"
15396             "int oneTwoThree = 123;",
15397             format("int a = 5;\n"
15398                    "\n"
15399                    "/* block comment */\n"
15400                    "\n"
15401                    "\n"
15402                    "\n"
15403                    "int oneTwoThree=123;",
15404                    Alignment));
15405 
15406   EXPECT_EQ("int a           = 5;\n"
15407             "\n"
15408             "// line comment\n"
15409             "\n"
15410             "\n"
15411             "\n"
15412             "int oneTwoThree = 123;",
15413             format("int a = 5;\n"
15414                    "\n"
15415                    "// line comment\n"
15416                    "\n"
15417                    "\n"
15418                    "\n"
15419                    "int oneTwoThree=123;",
15420                    Alignment));
15421 
15422   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15423   verifyFormat("#define A \\\n"
15424                "  int aaaa       = 12; \\\n"
15425                "  int b          = 23; \\\n"
15426                "  int ccc        = 234; \\\n"
15427                "  int dddddddddd = 2345;",
15428                Alignment);
15429   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15430   verifyFormat("#define A               \\\n"
15431                "  int aaaa       = 12;  \\\n"
15432                "  int b          = 23;  \\\n"
15433                "  int ccc        = 234; \\\n"
15434                "  int dddddddddd = 2345;",
15435                Alignment);
15436   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15437   verifyFormat("#define A                                                      "
15438                "                \\\n"
15439                "  int aaaa       = 12;                                         "
15440                "                \\\n"
15441                "  int b          = 23;                                         "
15442                "                \\\n"
15443                "  int ccc        = 234;                                        "
15444                "                \\\n"
15445                "  int dddddddddd = 2345;",
15446                Alignment);
15447   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15448                "k = 4, int l = 5,\n"
15449                "                  int m = 6) {\n"
15450                "  int j      = 10;\n"
15451                "  otherThing = 1;\n"
15452                "}",
15453                Alignment);
15454   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15455                "  int i   = 1;\n"
15456                "  int j   = 2;\n"
15457                "  int big = 10000;\n"
15458                "}",
15459                Alignment);
15460   verifyFormat("class C {\n"
15461                "public:\n"
15462                "  int i            = 1;\n"
15463                "  virtual void f() = 0;\n"
15464                "};",
15465                Alignment);
15466   verifyFormat("int i = 1;\n"
15467                "if (SomeType t = getSomething()) {\n"
15468                "}\n"
15469                "int j   = 2;\n"
15470                "int big = 10000;",
15471                Alignment);
15472   verifyFormat("int j = 7;\n"
15473                "for (int k = 0; k < N; ++k) {\n"
15474                "}\n"
15475                "int j   = 2;\n"
15476                "int big = 10000;\n"
15477                "}",
15478                Alignment);
15479   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15480   verifyFormat("int i = 1;\n"
15481                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15482                "    = someLooooooooooooooooongFunction();\n"
15483                "int j = 2;",
15484                Alignment);
15485   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15486   verifyFormat("int i = 1;\n"
15487                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15488                "    someLooooooooooooooooongFunction();\n"
15489                "int j = 2;",
15490                Alignment);
15491 
15492   verifyFormat("auto lambda = []() {\n"
15493                "  auto i = 0;\n"
15494                "  return 0;\n"
15495                "};\n"
15496                "int i  = 0;\n"
15497                "auto v = type{\n"
15498                "    i = 1,   //\n"
15499                "    (i = 2), //\n"
15500                "    i = 3    //\n"
15501                "};",
15502                Alignment);
15503 
15504   verifyFormat(
15505       "int i      = 1;\n"
15506       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15507       "                          loooooooooooooooooooooongParameterB);\n"
15508       "int j      = 2;",
15509       Alignment);
15510 
15511   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15512                "          typename B   = very_long_type_name_1,\n"
15513                "          typename T_2 = very_long_type_name_2>\n"
15514                "auto foo() {}\n",
15515                Alignment);
15516   verifyFormat("int a, b = 1;\n"
15517                "int c  = 2;\n"
15518                "int dd = 3;\n",
15519                Alignment);
15520   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15521                "float b[1][] = {{3.f}};\n",
15522                Alignment);
15523   verifyFormat("for (int i = 0; i < 1; i++)\n"
15524                "  int x = 1;\n",
15525                Alignment);
15526   verifyFormat("for (i = 0; i < 1; i++)\n"
15527                "  x = 1;\n"
15528                "y = 1;\n",
15529                Alignment);
15530 
15531   Alignment.ReflowComments = true;
15532   Alignment.ColumnLimit = 50;
15533   EXPECT_EQ("int x   = 0;\n"
15534             "int yy  = 1; /// specificlennospace\n"
15535             "int zzz = 2;\n",
15536             format("int x   = 0;\n"
15537                    "int yy  = 1; ///specificlennospace\n"
15538                    "int zzz = 2;\n",
15539                    Alignment));
15540 }
15541 
15542 TEST_F(FormatTest, AlignConsecutiveAssignments) {
15543   FormatStyle Alignment = getLLVMStyle();
15544   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15545   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15546   verifyFormat("int a = 5;\n"
15547                "int oneTwoThree = 123;",
15548                Alignment);
15549   verifyFormat("int a = 5;\n"
15550                "int oneTwoThree = 123;",
15551                Alignment);
15552 
15553   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15554   verifyFormat("int a           = 5;\n"
15555                "int oneTwoThree = 123;",
15556                Alignment);
15557   verifyFormat("int a           = method();\n"
15558                "int oneTwoThree = 133;",
15559                Alignment);
15560   verifyFormat("a &= 5;\n"
15561                "bcd *= 5;\n"
15562                "ghtyf += 5;\n"
15563                "dvfvdb -= 5;\n"
15564                "a /= 5;\n"
15565                "vdsvsv %= 5;\n"
15566                "sfdbddfbdfbb ^= 5;\n"
15567                "dvsdsv |= 5;\n"
15568                "int dsvvdvsdvvv = 123;",
15569                Alignment);
15570   verifyFormat("int i = 1, j = 10;\n"
15571                "something = 2000;",
15572                Alignment);
15573   verifyFormat("something = 2000;\n"
15574                "int i = 1, j = 10;\n",
15575                Alignment);
15576   verifyFormat("something = 2000;\n"
15577                "another   = 911;\n"
15578                "int i = 1, j = 10;\n"
15579                "oneMore = 1;\n"
15580                "i       = 2;",
15581                Alignment);
15582   verifyFormat("int a   = 5;\n"
15583                "int one = 1;\n"
15584                "method();\n"
15585                "int oneTwoThree = 123;\n"
15586                "int oneTwo      = 12;",
15587                Alignment);
15588   verifyFormat("int oneTwoThree = 123;\n"
15589                "int oneTwo      = 12;\n"
15590                "method();\n",
15591                Alignment);
15592   verifyFormat("int oneTwoThree = 123; // comment\n"
15593                "int oneTwo      = 12;  // comment",
15594                Alignment);
15595 
15596   // Bug 25167
15597   /* Uncomment when fixed
15598     verifyFormat("#if A\n"
15599                  "#else\n"
15600                  "int aaaaaaaa = 12;\n"
15601                  "#endif\n"
15602                  "#if B\n"
15603                  "#else\n"
15604                  "int a = 12;\n"
15605                  "#endif\n",
15606                  Alignment);
15607     verifyFormat("enum foo {\n"
15608                  "#if A\n"
15609                  "#else\n"
15610                  "  aaaaaaaa = 12;\n"
15611                  "#endif\n"
15612                  "#if B\n"
15613                  "#else\n"
15614                  "  a = 12;\n"
15615                  "#endif\n"
15616                  "};\n",
15617                  Alignment);
15618   */
15619 
15620   EXPECT_EQ("int a = 5;\n"
15621             "\n"
15622             "int oneTwoThree = 123;",
15623             format("int a       = 5;\n"
15624                    "\n"
15625                    "int oneTwoThree= 123;",
15626                    Alignment));
15627   EXPECT_EQ("int a   = 5;\n"
15628             "int one = 1;\n"
15629             "\n"
15630             "int oneTwoThree = 123;",
15631             format("int a = 5;\n"
15632                    "int one = 1;\n"
15633                    "\n"
15634                    "int oneTwoThree = 123;",
15635                    Alignment));
15636   EXPECT_EQ("int a   = 5;\n"
15637             "int one = 1;\n"
15638             "\n"
15639             "int oneTwoThree = 123;\n"
15640             "int oneTwo      = 12;",
15641             format("int a = 5;\n"
15642                    "int one = 1;\n"
15643                    "\n"
15644                    "int oneTwoThree = 123;\n"
15645                    "int oneTwo = 12;",
15646                    Alignment));
15647   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15648   verifyFormat("#define A \\\n"
15649                "  int aaaa       = 12; \\\n"
15650                "  int b          = 23; \\\n"
15651                "  int ccc        = 234; \\\n"
15652                "  int dddddddddd = 2345;",
15653                Alignment);
15654   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15655   verifyFormat("#define A               \\\n"
15656                "  int aaaa       = 12;  \\\n"
15657                "  int b          = 23;  \\\n"
15658                "  int ccc        = 234; \\\n"
15659                "  int dddddddddd = 2345;",
15660                Alignment);
15661   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15662   verifyFormat("#define A                                                      "
15663                "                \\\n"
15664                "  int aaaa       = 12;                                         "
15665                "                \\\n"
15666                "  int b          = 23;                                         "
15667                "                \\\n"
15668                "  int ccc        = 234;                                        "
15669                "                \\\n"
15670                "  int dddddddddd = 2345;",
15671                Alignment);
15672   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15673                "k = 4, int l = 5,\n"
15674                "                  int m = 6) {\n"
15675                "  int j      = 10;\n"
15676                "  otherThing = 1;\n"
15677                "}",
15678                Alignment);
15679   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15680                "  int i   = 1;\n"
15681                "  int j   = 2;\n"
15682                "  int big = 10000;\n"
15683                "}",
15684                Alignment);
15685   verifyFormat("class C {\n"
15686                "public:\n"
15687                "  int i            = 1;\n"
15688                "  virtual void f() = 0;\n"
15689                "};",
15690                Alignment);
15691   verifyFormat("int i = 1;\n"
15692                "if (SomeType t = getSomething()) {\n"
15693                "}\n"
15694                "int j   = 2;\n"
15695                "int big = 10000;",
15696                Alignment);
15697   verifyFormat("int j = 7;\n"
15698                "for (int k = 0; k < N; ++k) {\n"
15699                "}\n"
15700                "int j   = 2;\n"
15701                "int big = 10000;\n"
15702                "}",
15703                Alignment);
15704   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15705   verifyFormat("int i = 1;\n"
15706                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15707                "    = someLooooooooooooooooongFunction();\n"
15708                "int j = 2;",
15709                Alignment);
15710   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15711   verifyFormat("int i = 1;\n"
15712                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15713                "    someLooooooooooooooooongFunction();\n"
15714                "int j = 2;",
15715                Alignment);
15716 
15717   verifyFormat("auto lambda = []() {\n"
15718                "  auto i = 0;\n"
15719                "  return 0;\n"
15720                "};\n"
15721                "int i  = 0;\n"
15722                "auto v = type{\n"
15723                "    i = 1,   //\n"
15724                "    (i = 2), //\n"
15725                "    i = 3    //\n"
15726                "};",
15727                Alignment);
15728 
15729   verifyFormat(
15730       "int i      = 1;\n"
15731       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15732       "                          loooooooooooooooooooooongParameterB);\n"
15733       "int j      = 2;",
15734       Alignment);
15735 
15736   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15737                "          typename B   = very_long_type_name_1,\n"
15738                "          typename T_2 = very_long_type_name_2>\n"
15739                "auto foo() {}\n",
15740                Alignment);
15741   verifyFormat("int a, b = 1;\n"
15742                "int c  = 2;\n"
15743                "int dd = 3;\n",
15744                Alignment);
15745   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15746                "float b[1][] = {{3.f}};\n",
15747                Alignment);
15748   verifyFormat("for (int i = 0; i < 1; i++)\n"
15749                "  int x = 1;\n",
15750                Alignment);
15751   verifyFormat("for (i = 0; i < 1; i++)\n"
15752                "  x = 1;\n"
15753                "y = 1;\n",
15754                Alignment);
15755 
15756   Alignment.ReflowComments = true;
15757   Alignment.ColumnLimit = 50;
15758   EXPECT_EQ("int x   = 0;\n"
15759             "int yy  = 1; /// specificlennospace\n"
15760             "int zzz = 2;\n",
15761             format("int x   = 0;\n"
15762                    "int yy  = 1; ///specificlennospace\n"
15763                    "int zzz = 2;\n",
15764                    Alignment));
15765 }
15766 
15767 TEST_F(FormatTest, AlignConsecutiveBitFields) {
15768   FormatStyle Alignment = getLLVMStyle();
15769   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
15770   verifyFormat("int const a     : 5;\n"
15771                "int oneTwoThree : 23;",
15772                Alignment);
15773 
15774   // Initializers are allowed starting with c++2a
15775   verifyFormat("int const a     : 5 = 1;\n"
15776                "int oneTwoThree : 23 = 0;",
15777                Alignment);
15778 
15779   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15780   verifyFormat("int const a           : 5;\n"
15781                "int       oneTwoThree : 23;",
15782                Alignment);
15783 
15784   verifyFormat("int const a           : 5;  // comment\n"
15785                "int       oneTwoThree : 23; // comment",
15786                Alignment);
15787 
15788   verifyFormat("int const a           : 5 = 1;\n"
15789                "int       oneTwoThree : 23 = 0;",
15790                Alignment);
15791 
15792   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15793   verifyFormat("int const a           : 5  = 1;\n"
15794                "int       oneTwoThree : 23 = 0;",
15795                Alignment);
15796   verifyFormat("int const a           : 5  = {1};\n"
15797                "int       oneTwoThree : 23 = 0;",
15798                Alignment);
15799 
15800   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
15801   verifyFormat("int const a          :5;\n"
15802                "int       oneTwoThree:23;",
15803                Alignment);
15804 
15805   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
15806   verifyFormat("int const a           :5;\n"
15807                "int       oneTwoThree :23;",
15808                Alignment);
15809 
15810   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
15811   verifyFormat("int const a          : 5;\n"
15812                "int       oneTwoThree: 23;",
15813                Alignment);
15814 
15815   // Known limitations: ':' is only recognized as a bitfield colon when
15816   // followed by a number.
15817   /*
15818   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
15819                "int a           : 5;",
15820                Alignment);
15821   */
15822 }
15823 
15824 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
15825   FormatStyle Alignment = getLLVMStyle();
15826   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15827   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
15828   Alignment.PointerAlignment = FormatStyle::PAS_Right;
15829   verifyFormat("float const a = 5;\n"
15830                "int oneTwoThree = 123;",
15831                Alignment);
15832   verifyFormat("int a = 5;\n"
15833                "float const oneTwoThree = 123;",
15834                Alignment);
15835 
15836   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15837   verifyFormat("float const a = 5;\n"
15838                "int         oneTwoThree = 123;",
15839                Alignment);
15840   verifyFormat("int         a = method();\n"
15841                "float const oneTwoThree = 133;",
15842                Alignment);
15843   verifyFormat("int i = 1, j = 10;\n"
15844                "something = 2000;",
15845                Alignment);
15846   verifyFormat("something = 2000;\n"
15847                "int i = 1, j = 10;\n",
15848                Alignment);
15849   verifyFormat("float      something = 2000;\n"
15850                "double     another = 911;\n"
15851                "int        i = 1, j = 10;\n"
15852                "const int *oneMore = 1;\n"
15853                "unsigned   i = 2;",
15854                Alignment);
15855   verifyFormat("float a = 5;\n"
15856                "int   one = 1;\n"
15857                "method();\n"
15858                "const double       oneTwoThree = 123;\n"
15859                "const unsigned int oneTwo = 12;",
15860                Alignment);
15861   verifyFormat("int      oneTwoThree{0}; // comment\n"
15862                "unsigned oneTwo;         // comment",
15863                Alignment);
15864   verifyFormat("unsigned int       *a;\n"
15865                "int                *b;\n"
15866                "unsigned int Const *c;\n"
15867                "unsigned int const *d;\n"
15868                "unsigned int Const &e;\n"
15869                "unsigned int const &f;",
15870                Alignment);
15871   verifyFormat("Const unsigned int *c;\n"
15872                "const unsigned int *d;\n"
15873                "Const unsigned int &e;\n"
15874                "const unsigned int &f;\n"
15875                "const unsigned      g;\n"
15876                "Const unsigned      h;",
15877                Alignment);
15878   EXPECT_EQ("float const a = 5;\n"
15879             "\n"
15880             "int oneTwoThree = 123;",
15881             format("float const   a = 5;\n"
15882                    "\n"
15883                    "int           oneTwoThree= 123;",
15884                    Alignment));
15885   EXPECT_EQ("float a = 5;\n"
15886             "int   one = 1;\n"
15887             "\n"
15888             "unsigned oneTwoThree = 123;",
15889             format("float    a = 5;\n"
15890                    "int      one = 1;\n"
15891                    "\n"
15892                    "unsigned oneTwoThree = 123;",
15893                    Alignment));
15894   EXPECT_EQ("float a = 5;\n"
15895             "int   one = 1;\n"
15896             "\n"
15897             "unsigned oneTwoThree = 123;\n"
15898             "int      oneTwo = 12;",
15899             format("float    a = 5;\n"
15900                    "int one = 1;\n"
15901                    "\n"
15902                    "unsigned oneTwoThree = 123;\n"
15903                    "int oneTwo = 12;",
15904                    Alignment));
15905   // Function prototype alignment
15906   verifyFormat("int    a();\n"
15907                "double b();",
15908                Alignment);
15909   verifyFormat("int    a(int x);\n"
15910                "double b();",
15911                Alignment);
15912   unsigned OldColumnLimit = Alignment.ColumnLimit;
15913   // We need to set ColumnLimit to zero, in order to stress nested alignments,
15914   // otherwise the function parameters will be re-flowed onto a single line.
15915   Alignment.ColumnLimit = 0;
15916   EXPECT_EQ("int    a(int   x,\n"
15917             "         float y);\n"
15918             "double b(int    x,\n"
15919             "         double y);",
15920             format("int a(int x,\n"
15921                    " float y);\n"
15922                    "double b(int x,\n"
15923                    " double y);",
15924                    Alignment));
15925   // This ensures that function parameters of function declarations are
15926   // correctly indented when their owning functions are indented.
15927   // The failure case here is for 'double y' to not be indented enough.
15928   EXPECT_EQ("double a(int x);\n"
15929             "int    b(int    y,\n"
15930             "         double z);",
15931             format("double a(int x);\n"
15932                    "int b(int y,\n"
15933                    " double z);",
15934                    Alignment));
15935   // Set ColumnLimit low so that we induce wrapping immediately after
15936   // the function name and opening paren.
15937   Alignment.ColumnLimit = 13;
15938   verifyFormat("int function(\n"
15939                "    int  x,\n"
15940                "    bool y);",
15941                Alignment);
15942   Alignment.ColumnLimit = OldColumnLimit;
15943   // Ensure function pointers don't screw up recursive alignment
15944   verifyFormat("int    a(int x, void (*fp)(int y));\n"
15945                "double b();",
15946                Alignment);
15947   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15948   // Ensure recursive alignment is broken by function braces, so that the
15949   // "a = 1" does not align with subsequent assignments inside the function
15950   // body.
15951   verifyFormat("int func(int a = 1) {\n"
15952                "  int b  = 2;\n"
15953                "  int cc = 3;\n"
15954                "}",
15955                Alignment);
15956   verifyFormat("float      something = 2000;\n"
15957                "double     another   = 911;\n"
15958                "int        i = 1, j = 10;\n"
15959                "const int *oneMore = 1;\n"
15960                "unsigned   i       = 2;",
15961                Alignment);
15962   verifyFormat("int      oneTwoThree = {0}; // comment\n"
15963                "unsigned oneTwo      = 0;   // comment",
15964                Alignment);
15965   // Make sure that scope is correctly tracked, in the absence of braces
15966   verifyFormat("for (int i = 0; i < n; i++)\n"
15967                "  j = i;\n"
15968                "double x = 1;\n",
15969                Alignment);
15970   verifyFormat("if (int i = 0)\n"
15971                "  j = i;\n"
15972                "double x = 1;\n",
15973                Alignment);
15974   // Ensure operator[] and operator() are comprehended
15975   verifyFormat("struct test {\n"
15976                "  long long int foo();\n"
15977                "  int           operator[](int a);\n"
15978                "  double        bar();\n"
15979                "};\n",
15980                Alignment);
15981   verifyFormat("struct test {\n"
15982                "  long long int foo();\n"
15983                "  int           operator()(int a);\n"
15984                "  double        bar();\n"
15985                "};\n",
15986                Alignment);
15987 
15988   // PAS_Right
15989   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15990             "  int const i   = 1;\n"
15991             "  int      *j   = 2;\n"
15992             "  int       big = 10000;\n"
15993             "\n"
15994             "  unsigned oneTwoThree = 123;\n"
15995             "  int      oneTwo      = 12;\n"
15996             "  method();\n"
15997             "  float k  = 2;\n"
15998             "  int   ll = 10000;\n"
15999             "}",
16000             format("void SomeFunction(int parameter= 0) {\n"
16001                    " int const  i= 1;\n"
16002                    "  int *j=2;\n"
16003                    " int big  =  10000;\n"
16004                    "\n"
16005                    "unsigned oneTwoThree  =123;\n"
16006                    "int oneTwo = 12;\n"
16007                    "  method();\n"
16008                    "float k= 2;\n"
16009                    "int ll=10000;\n"
16010                    "}",
16011                    Alignment));
16012   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16013             "  int const i   = 1;\n"
16014             "  int     **j   = 2, ***k;\n"
16015             "  int      &k   = i;\n"
16016             "  int     &&l   = i + j;\n"
16017             "  int       big = 10000;\n"
16018             "\n"
16019             "  unsigned oneTwoThree = 123;\n"
16020             "  int      oneTwo      = 12;\n"
16021             "  method();\n"
16022             "  float k  = 2;\n"
16023             "  int   ll = 10000;\n"
16024             "}",
16025             format("void SomeFunction(int parameter= 0) {\n"
16026                    " int const  i= 1;\n"
16027                    "  int **j=2,***k;\n"
16028                    "int &k=i;\n"
16029                    "int &&l=i+j;\n"
16030                    " int big  =  10000;\n"
16031                    "\n"
16032                    "unsigned oneTwoThree  =123;\n"
16033                    "int oneTwo = 12;\n"
16034                    "  method();\n"
16035                    "float k= 2;\n"
16036                    "int ll=10000;\n"
16037                    "}",
16038                    Alignment));
16039   // variables are aligned at their name, pointers are at the right most
16040   // position
16041   verifyFormat("int   *a;\n"
16042                "int  **b;\n"
16043                "int ***c;\n"
16044                "int    foobar;\n",
16045                Alignment);
16046 
16047   // PAS_Left
16048   FormatStyle AlignmentLeft = Alignment;
16049   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16050   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16051             "  int const i   = 1;\n"
16052             "  int*      j   = 2;\n"
16053             "  int       big = 10000;\n"
16054             "\n"
16055             "  unsigned oneTwoThree = 123;\n"
16056             "  int      oneTwo      = 12;\n"
16057             "  method();\n"
16058             "  float k  = 2;\n"
16059             "  int   ll = 10000;\n"
16060             "}",
16061             format("void SomeFunction(int parameter= 0) {\n"
16062                    " int const  i= 1;\n"
16063                    "  int *j=2;\n"
16064                    " int big  =  10000;\n"
16065                    "\n"
16066                    "unsigned oneTwoThree  =123;\n"
16067                    "int oneTwo = 12;\n"
16068                    "  method();\n"
16069                    "float k= 2;\n"
16070                    "int ll=10000;\n"
16071                    "}",
16072                    AlignmentLeft));
16073   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16074             "  int const i   = 1;\n"
16075             "  int**     j   = 2;\n"
16076             "  int&      k   = i;\n"
16077             "  int&&     l   = i + j;\n"
16078             "  int       big = 10000;\n"
16079             "\n"
16080             "  unsigned oneTwoThree = 123;\n"
16081             "  int      oneTwo      = 12;\n"
16082             "  method();\n"
16083             "  float k  = 2;\n"
16084             "  int   ll = 10000;\n"
16085             "}",
16086             format("void SomeFunction(int parameter= 0) {\n"
16087                    " int const  i= 1;\n"
16088                    "  int **j=2;\n"
16089                    "int &k=i;\n"
16090                    "int &&l=i+j;\n"
16091                    " int big  =  10000;\n"
16092                    "\n"
16093                    "unsigned oneTwoThree  =123;\n"
16094                    "int oneTwo = 12;\n"
16095                    "  method();\n"
16096                    "float k= 2;\n"
16097                    "int ll=10000;\n"
16098                    "}",
16099                    AlignmentLeft));
16100   // variables are aligned at their name, pointers are at the left most position
16101   verifyFormat("int*   a;\n"
16102                "int**  b;\n"
16103                "int*** c;\n"
16104                "int    foobar;\n",
16105                AlignmentLeft);
16106 
16107   // PAS_Middle
16108   FormatStyle AlignmentMiddle = Alignment;
16109   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16110   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16111             "  int const i   = 1;\n"
16112             "  int *     j   = 2;\n"
16113             "  int       big = 10000;\n"
16114             "\n"
16115             "  unsigned oneTwoThree = 123;\n"
16116             "  int      oneTwo      = 12;\n"
16117             "  method();\n"
16118             "  float k  = 2;\n"
16119             "  int   ll = 10000;\n"
16120             "}",
16121             format("void SomeFunction(int parameter= 0) {\n"
16122                    " int const  i= 1;\n"
16123                    "  int *j=2;\n"
16124                    " int big  =  10000;\n"
16125                    "\n"
16126                    "unsigned oneTwoThree  =123;\n"
16127                    "int oneTwo = 12;\n"
16128                    "  method();\n"
16129                    "float k= 2;\n"
16130                    "int ll=10000;\n"
16131                    "}",
16132                    AlignmentMiddle));
16133   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16134             "  int const i   = 1;\n"
16135             "  int **    j   = 2, ***k;\n"
16136             "  int &     k   = i;\n"
16137             "  int &&    l   = i + j;\n"
16138             "  int       big = 10000;\n"
16139             "\n"
16140             "  unsigned oneTwoThree = 123;\n"
16141             "  int      oneTwo      = 12;\n"
16142             "  method();\n"
16143             "  float k  = 2;\n"
16144             "  int   ll = 10000;\n"
16145             "}",
16146             format("void SomeFunction(int parameter= 0) {\n"
16147                    " int const  i= 1;\n"
16148                    "  int **j=2,***k;\n"
16149                    "int &k=i;\n"
16150                    "int &&l=i+j;\n"
16151                    " int big  =  10000;\n"
16152                    "\n"
16153                    "unsigned oneTwoThree  =123;\n"
16154                    "int oneTwo = 12;\n"
16155                    "  method();\n"
16156                    "float k= 2;\n"
16157                    "int ll=10000;\n"
16158                    "}",
16159                    AlignmentMiddle));
16160   // variables are aligned at their name, pointers are in the middle
16161   verifyFormat("int *   a;\n"
16162                "int *   b;\n"
16163                "int *** c;\n"
16164                "int     foobar;\n",
16165                AlignmentMiddle);
16166 
16167   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16168   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16169   verifyFormat("#define A \\\n"
16170                "  int       aaaa = 12; \\\n"
16171                "  float     b = 23; \\\n"
16172                "  const int ccc = 234; \\\n"
16173                "  unsigned  dddddddddd = 2345;",
16174                Alignment);
16175   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16176   verifyFormat("#define A              \\\n"
16177                "  int       aaaa = 12; \\\n"
16178                "  float     b = 23;    \\\n"
16179                "  const int ccc = 234; \\\n"
16180                "  unsigned  dddddddddd = 2345;",
16181                Alignment);
16182   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16183   Alignment.ColumnLimit = 30;
16184   verifyFormat("#define A                    \\\n"
16185                "  int       aaaa = 12;       \\\n"
16186                "  float     b = 23;          \\\n"
16187                "  const int ccc = 234;       \\\n"
16188                "  int       dddddddddd = 2345;",
16189                Alignment);
16190   Alignment.ColumnLimit = 80;
16191   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16192                "k = 4, int l = 5,\n"
16193                "                  int m = 6) {\n"
16194                "  const int j = 10;\n"
16195                "  otherThing = 1;\n"
16196                "}",
16197                Alignment);
16198   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16199                "  int const i = 1;\n"
16200                "  int      *j = 2;\n"
16201                "  int       big = 10000;\n"
16202                "}",
16203                Alignment);
16204   verifyFormat("class C {\n"
16205                "public:\n"
16206                "  int          i = 1;\n"
16207                "  virtual void f() = 0;\n"
16208                "};",
16209                Alignment);
16210   verifyFormat("float i = 1;\n"
16211                "if (SomeType t = getSomething()) {\n"
16212                "}\n"
16213                "const unsigned j = 2;\n"
16214                "int            big = 10000;",
16215                Alignment);
16216   verifyFormat("float j = 7;\n"
16217                "for (int k = 0; k < N; ++k) {\n"
16218                "}\n"
16219                "unsigned j = 2;\n"
16220                "int      big = 10000;\n"
16221                "}",
16222                Alignment);
16223   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16224   verifyFormat("float              i = 1;\n"
16225                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16226                "    = someLooooooooooooooooongFunction();\n"
16227                "int j = 2;",
16228                Alignment);
16229   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16230   verifyFormat("int                i = 1;\n"
16231                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16232                "    someLooooooooooooooooongFunction();\n"
16233                "int j = 2;",
16234                Alignment);
16235 
16236   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16237   verifyFormat("auto lambda = []() {\n"
16238                "  auto  ii = 0;\n"
16239                "  float j  = 0;\n"
16240                "  return 0;\n"
16241                "};\n"
16242                "int   i  = 0;\n"
16243                "float i2 = 0;\n"
16244                "auto  v  = type{\n"
16245                "    i = 1,   //\n"
16246                "    (i = 2), //\n"
16247                "    i = 3    //\n"
16248                "};",
16249                Alignment);
16250   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16251 
16252   verifyFormat(
16253       "int      i = 1;\n"
16254       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16255       "                          loooooooooooooooooooooongParameterB);\n"
16256       "int      j = 2;",
16257       Alignment);
16258 
16259   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
16260   // We expect declarations and assignments to align, as long as it doesn't
16261   // exceed the column limit, starting a new alignment sequence whenever it
16262   // happens.
16263   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16264   Alignment.ColumnLimit = 30;
16265   verifyFormat("float    ii              = 1;\n"
16266                "unsigned j               = 2;\n"
16267                "int someVerylongVariable = 1;\n"
16268                "AnotherLongType  ll = 123456;\n"
16269                "VeryVeryLongType k  = 2;\n"
16270                "int              myvar = 1;",
16271                Alignment);
16272   Alignment.ColumnLimit = 80;
16273   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16274 
16275   verifyFormat(
16276       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
16277       "          typename LongType, typename B>\n"
16278       "auto foo() {}\n",
16279       Alignment);
16280   verifyFormat("float a, b = 1;\n"
16281                "int   c = 2;\n"
16282                "int   dd = 3;\n",
16283                Alignment);
16284   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
16285                "float b[1][] = {{3.f}};\n",
16286                Alignment);
16287   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16288   verifyFormat("float a, b = 1;\n"
16289                "int   c  = 2;\n"
16290                "int   dd = 3;\n",
16291                Alignment);
16292   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
16293                "float b[1][] = {{3.f}};\n",
16294                Alignment);
16295   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16296 
16297   Alignment.ColumnLimit = 30;
16298   Alignment.BinPackParameters = false;
16299   verifyFormat("void foo(float     a,\n"
16300                "         float     b,\n"
16301                "         int       c,\n"
16302                "         uint32_t *d) {\n"
16303                "  int   *e = 0;\n"
16304                "  float  f = 0;\n"
16305                "  double g = 0;\n"
16306                "}\n"
16307                "void bar(ino_t     a,\n"
16308                "         int       b,\n"
16309                "         uint32_t *c,\n"
16310                "         bool      d) {}\n",
16311                Alignment);
16312   Alignment.BinPackParameters = true;
16313   Alignment.ColumnLimit = 80;
16314 
16315   // Bug 33507
16316   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16317   verifyFormat(
16318       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
16319       "  static const Version verVs2017;\n"
16320       "  return true;\n"
16321       "});\n",
16322       Alignment);
16323   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16324 
16325   // See llvm.org/PR35641
16326   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16327   verifyFormat("int func() { //\n"
16328                "  int      b;\n"
16329                "  unsigned c;\n"
16330                "}",
16331                Alignment);
16332 
16333   // See PR37175
16334   FormatStyle Style = getMozillaStyle();
16335   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16336   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
16337             "foo(int a);",
16338             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
16339 
16340   Alignment.PointerAlignment = FormatStyle::PAS_Left;
16341   verifyFormat("unsigned int*       a;\n"
16342                "int*                b;\n"
16343                "unsigned int Const* c;\n"
16344                "unsigned int const* d;\n"
16345                "unsigned int Const& e;\n"
16346                "unsigned int const& f;",
16347                Alignment);
16348   verifyFormat("Const unsigned int* c;\n"
16349                "const unsigned int* d;\n"
16350                "Const unsigned int& e;\n"
16351                "const unsigned int& f;\n"
16352                "const unsigned      g;\n"
16353                "Const unsigned      h;",
16354                Alignment);
16355 
16356   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16357   verifyFormat("unsigned int *       a;\n"
16358                "int *                b;\n"
16359                "unsigned int Const * c;\n"
16360                "unsigned int const * d;\n"
16361                "unsigned int Const & e;\n"
16362                "unsigned int const & f;",
16363                Alignment);
16364   verifyFormat("Const unsigned int * c;\n"
16365                "const unsigned int * d;\n"
16366                "Const unsigned int & e;\n"
16367                "const unsigned int & f;\n"
16368                "const unsigned       g;\n"
16369                "Const unsigned       h;",
16370                Alignment);
16371 }
16372 
16373 TEST_F(FormatTest, AlignWithLineBreaks) {
16374   auto Style = getLLVMStyleWithColumns(120);
16375 
16376   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
16377   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16378   verifyFormat("void foo() {\n"
16379                "  int myVar = 5;\n"
16380                "  double x = 3.14;\n"
16381                "  auto str = \"Hello \"\n"
16382                "             \"World\";\n"
16383                "  auto s = \"Hello \"\n"
16384                "           \"Again\";\n"
16385                "}",
16386                Style);
16387 
16388   // clang-format off
16389   verifyFormat("void foo() {\n"
16390                "  const int capacityBefore = Entries.capacity();\n"
16391                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16392                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16393                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16394                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16395                "}",
16396                Style);
16397   // clang-format on
16398 
16399   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16400   verifyFormat("void foo() {\n"
16401                "  int myVar = 5;\n"
16402                "  double x  = 3.14;\n"
16403                "  auto str  = \"Hello \"\n"
16404                "              \"World\";\n"
16405                "  auto s    = \"Hello \"\n"
16406                "              \"Again\";\n"
16407                "}",
16408                Style);
16409 
16410   // clang-format off
16411   verifyFormat("void foo() {\n"
16412                "  const int capacityBefore = Entries.capacity();\n"
16413                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16414                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16415                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16416                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16417                "}",
16418                Style);
16419   // clang-format on
16420 
16421   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16422   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16423   verifyFormat("void foo() {\n"
16424                "  int    myVar = 5;\n"
16425                "  double x = 3.14;\n"
16426                "  auto   str = \"Hello \"\n"
16427                "               \"World\";\n"
16428                "  auto   s = \"Hello \"\n"
16429                "             \"Again\";\n"
16430                "}",
16431                Style);
16432 
16433   // clang-format off
16434   verifyFormat("void foo() {\n"
16435                "  const int  capacityBefore = Entries.capacity();\n"
16436                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16437                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16438                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16439                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16440                "}",
16441                Style);
16442   // clang-format on
16443 
16444   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16445   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16446 
16447   verifyFormat("void foo() {\n"
16448                "  int    myVar = 5;\n"
16449                "  double x     = 3.14;\n"
16450                "  auto   str   = \"Hello \"\n"
16451                "                 \"World\";\n"
16452                "  auto   s     = \"Hello \"\n"
16453                "                 \"Again\";\n"
16454                "}",
16455                Style);
16456 
16457   // clang-format off
16458   verifyFormat("void foo() {\n"
16459                "  const int  capacityBefore = Entries.capacity();\n"
16460                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16461                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16462                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16463                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16464                "}",
16465                Style);
16466   // clang-format on
16467 
16468   Style = getLLVMStyleWithColumns(120);
16469   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16470   Style.ContinuationIndentWidth = 4;
16471   Style.IndentWidth = 4;
16472 
16473   // clang-format off
16474   verifyFormat("void SomeFunc() {\n"
16475                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16476                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16477                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16478                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16479                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16480                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16481                "}",
16482                Style);
16483   // clang-format on
16484 
16485   Style.BinPackArguments = false;
16486 
16487   // clang-format off
16488   verifyFormat("void SomeFunc() {\n"
16489                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
16490                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16491                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
16492                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16493                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
16494                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16495                "}",
16496                Style);
16497   // clang-format on
16498 }
16499 
16500 TEST_F(FormatTest, AlignWithInitializerPeriods) {
16501   auto Style = getLLVMStyleWithColumns(60);
16502 
16503   verifyFormat("void foo1(void) {\n"
16504                "  BYTE p[1] = 1;\n"
16505                "  A B = {.one_foooooooooooooooo = 2,\n"
16506                "         .two_fooooooooooooo = 3,\n"
16507                "         .three_fooooooooooooo = 4};\n"
16508                "  BYTE payload = 2;\n"
16509                "}",
16510                Style);
16511 
16512   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16513   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16514   verifyFormat("void foo2(void) {\n"
16515                "  BYTE p[1]    = 1;\n"
16516                "  A B          = {.one_foooooooooooooooo = 2,\n"
16517                "                  .two_fooooooooooooo    = 3,\n"
16518                "                  .three_fooooooooooooo  = 4};\n"
16519                "  BYTE payload = 2;\n"
16520                "}",
16521                Style);
16522 
16523   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16524   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16525   verifyFormat("void foo3(void) {\n"
16526                "  BYTE p[1] = 1;\n"
16527                "  A    B = {.one_foooooooooooooooo = 2,\n"
16528                "            .two_fooooooooooooo = 3,\n"
16529                "            .three_fooooooooooooo = 4};\n"
16530                "  BYTE payload = 2;\n"
16531                "}",
16532                Style);
16533 
16534   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16535   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16536   verifyFormat("void foo4(void) {\n"
16537                "  BYTE p[1]    = 1;\n"
16538                "  A    B       = {.one_foooooooooooooooo = 2,\n"
16539                "                  .two_fooooooooooooo    = 3,\n"
16540                "                  .three_fooooooooooooo  = 4};\n"
16541                "  BYTE payload = 2;\n"
16542                "}",
16543                Style);
16544 }
16545 
16546 TEST_F(FormatTest, LinuxBraceBreaking) {
16547   FormatStyle LinuxBraceStyle = getLLVMStyle();
16548   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
16549   verifyFormat("namespace a\n"
16550                "{\n"
16551                "class A\n"
16552                "{\n"
16553                "  void f()\n"
16554                "  {\n"
16555                "    if (true) {\n"
16556                "      a();\n"
16557                "      b();\n"
16558                "    } else {\n"
16559                "      a();\n"
16560                "    }\n"
16561                "  }\n"
16562                "  void g() { return; }\n"
16563                "};\n"
16564                "struct B {\n"
16565                "  int x;\n"
16566                "};\n"
16567                "} // namespace a\n",
16568                LinuxBraceStyle);
16569   verifyFormat("enum X {\n"
16570                "  Y = 0,\n"
16571                "}\n",
16572                LinuxBraceStyle);
16573   verifyFormat("struct S {\n"
16574                "  int Type;\n"
16575                "  union {\n"
16576                "    int x;\n"
16577                "    double y;\n"
16578                "  } Value;\n"
16579                "  class C\n"
16580                "  {\n"
16581                "    MyFavoriteType Value;\n"
16582                "  } Class;\n"
16583                "}\n",
16584                LinuxBraceStyle);
16585 }
16586 
16587 TEST_F(FormatTest, MozillaBraceBreaking) {
16588   FormatStyle MozillaBraceStyle = getLLVMStyle();
16589   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
16590   MozillaBraceStyle.FixNamespaceComments = false;
16591   verifyFormat("namespace a {\n"
16592                "class A\n"
16593                "{\n"
16594                "  void f()\n"
16595                "  {\n"
16596                "    if (true) {\n"
16597                "      a();\n"
16598                "      b();\n"
16599                "    }\n"
16600                "  }\n"
16601                "  void g() { return; }\n"
16602                "};\n"
16603                "enum E\n"
16604                "{\n"
16605                "  A,\n"
16606                "  // foo\n"
16607                "  B,\n"
16608                "  C\n"
16609                "};\n"
16610                "struct B\n"
16611                "{\n"
16612                "  int x;\n"
16613                "};\n"
16614                "}\n",
16615                MozillaBraceStyle);
16616   verifyFormat("struct S\n"
16617                "{\n"
16618                "  int Type;\n"
16619                "  union\n"
16620                "  {\n"
16621                "    int x;\n"
16622                "    double y;\n"
16623                "  } Value;\n"
16624                "  class C\n"
16625                "  {\n"
16626                "    MyFavoriteType Value;\n"
16627                "  } Class;\n"
16628                "}\n",
16629                MozillaBraceStyle);
16630 }
16631 
16632 TEST_F(FormatTest, StroustrupBraceBreaking) {
16633   FormatStyle StroustrupBraceStyle = getLLVMStyle();
16634   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
16635   verifyFormat("namespace a {\n"
16636                "class A {\n"
16637                "  void f()\n"
16638                "  {\n"
16639                "    if (true) {\n"
16640                "      a();\n"
16641                "      b();\n"
16642                "    }\n"
16643                "  }\n"
16644                "  void g() { return; }\n"
16645                "};\n"
16646                "struct B {\n"
16647                "  int x;\n"
16648                "};\n"
16649                "} // namespace a\n",
16650                StroustrupBraceStyle);
16651 
16652   verifyFormat("void foo()\n"
16653                "{\n"
16654                "  if (a) {\n"
16655                "    a();\n"
16656                "  }\n"
16657                "  else {\n"
16658                "    b();\n"
16659                "  }\n"
16660                "}\n",
16661                StroustrupBraceStyle);
16662 
16663   verifyFormat("#ifdef _DEBUG\n"
16664                "int foo(int i = 0)\n"
16665                "#else\n"
16666                "int foo(int i = 5)\n"
16667                "#endif\n"
16668                "{\n"
16669                "  return i;\n"
16670                "}",
16671                StroustrupBraceStyle);
16672 
16673   verifyFormat("void foo() {}\n"
16674                "void bar()\n"
16675                "#ifdef _DEBUG\n"
16676                "{\n"
16677                "  foo();\n"
16678                "}\n"
16679                "#else\n"
16680                "{\n"
16681                "}\n"
16682                "#endif",
16683                StroustrupBraceStyle);
16684 
16685   verifyFormat("void foobar() { int i = 5; }\n"
16686                "#ifdef _DEBUG\n"
16687                "void bar() {}\n"
16688                "#else\n"
16689                "void bar() { foobar(); }\n"
16690                "#endif",
16691                StroustrupBraceStyle);
16692 }
16693 
16694 TEST_F(FormatTest, AllmanBraceBreaking) {
16695   FormatStyle AllmanBraceStyle = getLLVMStyle();
16696   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
16697 
16698   EXPECT_EQ("namespace a\n"
16699             "{\n"
16700             "void f();\n"
16701             "void g();\n"
16702             "} // namespace a\n",
16703             format("namespace a\n"
16704                    "{\n"
16705                    "void f();\n"
16706                    "void g();\n"
16707                    "}\n",
16708                    AllmanBraceStyle));
16709 
16710   verifyFormat("namespace a\n"
16711                "{\n"
16712                "class A\n"
16713                "{\n"
16714                "  void f()\n"
16715                "  {\n"
16716                "    if (true)\n"
16717                "    {\n"
16718                "      a();\n"
16719                "      b();\n"
16720                "    }\n"
16721                "  }\n"
16722                "  void g() { return; }\n"
16723                "};\n"
16724                "struct B\n"
16725                "{\n"
16726                "  int x;\n"
16727                "};\n"
16728                "union C\n"
16729                "{\n"
16730                "};\n"
16731                "} // namespace a",
16732                AllmanBraceStyle);
16733 
16734   verifyFormat("void f()\n"
16735                "{\n"
16736                "  if (true)\n"
16737                "  {\n"
16738                "    a();\n"
16739                "  }\n"
16740                "  else if (false)\n"
16741                "  {\n"
16742                "    b();\n"
16743                "  }\n"
16744                "  else\n"
16745                "  {\n"
16746                "    c();\n"
16747                "  }\n"
16748                "}\n",
16749                AllmanBraceStyle);
16750 
16751   verifyFormat("void f()\n"
16752                "{\n"
16753                "  for (int i = 0; i < 10; ++i)\n"
16754                "  {\n"
16755                "    a();\n"
16756                "  }\n"
16757                "  while (false)\n"
16758                "  {\n"
16759                "    b();\n"
16760                "  }\n"
16761                "  do\n"
16762                "  {\n"
16763                "    c();\n"
16764                "  } while (false)\n"
16765                "}\n",
16766                AllmanBraceStyle);
16767 
16768   verifyFormat("void f(int a)\n"
16769                "{\n"
16770                "  switch (a)\n"
16771                "  {\n"
16772                "  case 0:\n"
16773                "    break;\n"
16774                "  case 1:\n"
16775                "  {\n"
16776                "    break;\n"
16777                "  }\n"
16778                "  case 2:\n"
16779                "  {\n"
16780                "  }\n"
16781                "  break;\n"
16782                "  default:\n"
16783                "    break;\n"
16784                "  }\n"
16785                "}\n",
16786                AllmanBraceStyle);
16787 
16788   verifyFormat("enum X\n"
16789                "{\n"
16790                "  Y = 0,\n"
16791                "}\n",
16792                AllmanBraceStyle);
16793   verifyFormat("enum X\n"
16794                "{\n"
16795                "  Y = 0\n"
16796                "}\n",
16797                AllmanBraceStyle);
16798 
16799   verifyFormat("@interface BSApplicationController ()\n"
16800                "{\n"
16801                "@private\n"
16802                "  id _extraIvar;\n"
16803                "}\n"
16804                "@end\n",
16805                AllmanBraceStyle);
16806 
16807   verifyFormat("#ifdef _DEBUG\n"
16808                "int foo(int i = 0)\n"
16809                "#else\n"
16810                "int foo(int i = 5)\n"
16811                "#endif\n"
16812                "{\n"
16813                "  return i;\n"
16814                "}",
16815                AllmanBraceStyle);
16816 
16817   verifyFormat("void foo() {}\n"
16818                "void bar()\n"
16819                "#ifdef _DEBUG\n"
16820                "{\n"
16821                "  foo();\n"
16822                "}\n"
16823                "#else\n"
16824                "{\n"
16825                "}\n"
16826                "#endif",
16827                AllmanBraceStyle);
16828 
16829   verifyFormat("void foobar() { int i = 5; }\n"
16830                "#ifdef _DEBUG\n"
16831                "void bar() {}\n"
16832                "#else\n"
16833                "void bar() { foobar(); }\n"
16834                "#endif",
16835                AllmanBraceStyle);
16836 
16837   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
16838             FormatStyle::SLS_All);
16839 
16840   verifyFormat("[](int i) { return i + 2; };\n"
16841                "[](int i, int j)\n"
16842                "{\n"
16843                "  auto x = i + j;\n"
16844                "  auto y = i * j;\n"
16845                "  return x ^ y;\n"
16846                "};\n"
16847                "void foo()\n"
16848                "{\n"
16849                "  auto shortLambda = [](int i) { return i + 2; };\n"
16850                "  auto longLambda = [](int i, int j)\n"
16851                "  {\n"
16852                "    auto x = i + j;\n"
16853                "    auto y = i * j;\n"
16854                "    return x ^ y;\n"
16855                "  };\n"
16856                "}",
16857                AllmanBraceStyle);
16858 
16859   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
16860 
16861   verifyFormat("[](int i)\n"
16862                "{\n"
16863                "  return i + 2;\n"
16864                "};\n"
16865                "[](int i, int j)\n"
16866                "{\n"
16867                "  auto x = i + j;\n"
16868                "  auto y = i * j;\n"
16869                "  return x ^ y;\n"
16870                "};\n"
16871                "void foo()\n"
16872                "{\n"
16873                "  auto shortLambda = [](int i)\n"
16874                "  {\n"
16875                "    return i + 2;\n"
16876                "  };\n"
16877                "  auto longLambda = [](int i, int j)\n"
16878                "  {\n"
16879                "    auto x = i + j;\n"
16880                "    auto y = i * j;\n"
16881                "    return x ^ y;\n"
16882                "  };\n"
16883                "}",
16884                AllmanBraceStyle);
16885 
16886   // Reset
16887   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
16888 
16889   // This shouldn't affect ObjC blocks..
16890   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
16891                "  // ...\n"
16892                "  int i;\n"
16893                "}];",
16894                AllmanBraceStyle);
16895   verifyFormat("void (^block)(void) = ^{\n"
16896                "  // ...\n"
16897                "  int i;\n"
16898                "};",
16899                AllmanBraceStyle);
16900   // .. or dict literals.
16901   verifyFormat("void f()\n"
16902                "{\n"
16903                "  // ...\n"
16904                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
16905                "}",
16906                AllmanBraceStyle);
16907   verifyFormat("void f()\n"
16908                "{\n"
16909                "  // ...\n"
16910                "  [object someMethod:@{a : @\"b\"}];\n"
16911                "}",
16912                AllmanBraceStyle);
16913   verifyFormat("int f()\n"
16914                "{ // comment\n"
16915                "  return 42;\n"
16916                "}",
16917                AllmanBraceStyle);
16918 
16919   AllmanBraceStyle.ColumnLimit = 19;
16920   verifyFormat("void f() { int i; }", AllmanBraceStyle);
16921   AllmanBraceStyle.ColumnLimit = 18;
16922   verifyFormat("void f()\n"
16923                "{\n"
16924                "  int i;\n"
16925                "}",
16926                AllmanBraceStyle);
16927   AllmanBraceStyle.ColumnLimit = 80;
16928 
16929   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
16930   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
16931       FormatStyle::SIS_WithoutElse;
16932   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
16933   verifyFormat("void f(bool b)\n"
16934                "{\n"
16935                "  if (b)\n"
16936                "  {\n"
16937                "    return;\n"
16938                "  }\n"
16939                "}\n",
16940                BreakBeforeBraceShortIfs);
16941   verifyFormat("void f(bool b)\n"
16942                "{\n"
16943                "  if constexpr (b)\n"
16944                "  {\n"
16945                "    return;\n"
16946                "  }\n"
16947                "}\n",
16948                BreakBeforeBraceShortIfs);
16949   verifyFormat("void f(bool b)\n"
16950                "{\n"
16951                "  if CONSTEXPR (b)\n"
16952                "  {\n"
16953                "    return;\n"
16954                "  }\n"
16955                "}\n",
16956                BreakBeforeBraceShortIfs);
16957   verifyFormat("void f(bool b)\n"
16958                "{\n"
16959                "  if (b) return;\n"
16960                "}\n",
16961                BreakBeforeBraceShortIfs);
16962   verifyFormat("void f(bool b)\n"
16963                "{\n"
16964                "  if constexpr (b) return;\n"
16965                "}\n",
16966                BreakBeforeBraceShortIfs);
16967   verifyFormat("void f(bool b)\n"
16968                "{\n"
16969                "  if CONSTEXPR (b) return;\n"
16970                "}\n",
16971                BreakBeforeBraceShortIfs);
16972   verifyFormat("void f(bool b)\n"
16973                "{\n"
16974                "  while (b)\n"
16975                "  {\n"
16976                "    return;\n"
16977                "  }\n"
16978                "}\n",
16979                BreakBeforeBraceShortIfs);
16980 }
16981 
16982 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
16983   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
16984   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
16985 
16986   // Make a few changes to the style for testing purposes
16987   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
16988       FormatStyle::SFS_Empty;
16989   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
16990   WhitesmithsBraceStyle.ColumnLimit = 0;
16991 
16992   // FIXME: this test case can't decide whether there should be a blank line
16993   // after the ~D() line or not. It adds one if one doesn't exist in the test
16994   // and it removes the line if one exists.
16995   /*
16996   verifyFormat("class A;\n"
16997                "namespace B\n"
16998                "  {\n"
16999                "class C;\n"
17000                "// Comment\n"
17001                "class D\n"
17002                "  {\n"
17003                "public:\n"
17004                "  D();\n"
17005                "  ~D() {}\n"
17006                "private:\n"
17007                "  enum E\n"
17008                "    {\n"
17009                "    F\n"
17010                "    }\n"
17011                "  };\n"
17012                "  } // namespace B\n",
17013                WhitesmithsBraceStyle);
17014   */
17015 
17016   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17017   verifyFormat("namespace a\n"
17018                "  {\n"
17019                "class A\n"
17020                "  {\n"
17021                "  void f()\n"
17022                "    {\n"
17023                "    if (true)\n"
17024                "      {\n"
17025                "      a();\n"
17026                "      b();\n"
17027                "      }\n"
17028                "    }\n"
17029                "  void g()\n"
17030                "    {\n"
17031                "    return;\n"
17032                "    }\n"
17033                "  };\n"
17034                "struct B\n"
17035                "  {\n"
17036                "  int x;\n"
17037                "  };\n"
17038                "  } // namespace a",
17039                WhitesmithsBraceStyle);
17040 
17041   verifyFormat("namespace a\n"
17042                "  {\n"
17043                "namespace b\n"
17044                "  {\n"
17045                "class A\n"
17046                "  {\n"
17047                "  void f()\n"
17048                "    {\n"
17049                "    if (true)\n"
17050                "      {\n"
17051                "      a();\n"
17052                "      b();\n"
17053                "      }\n"
17054                "    }\n"
17055                "  void g()\n"
17056                "    {\n"
17057                "    return;\n"
17058                "    }\n"
17059                "  };\n"
17060                "struct B\n"
17061                "  {\n"
17062                "  int x;\n"
17063                "  };\n"
17064                "  } // namespace b\n"
17065                "  } // namespace a",
17066                WhitesmithsBraceStyle);
17067 
17068   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
17069   verifyFormat("namespace a\n"
17070                "  {\n"
17071                "namespace b\n"
17072                "  {\n"
17073                "  class A\n"
17074                "    {\n"
17075                "    void f()\n"
17076                "      {\n"
17077                "      if (true)\n"
17078                "        {\n"
17079                "        a();\n"
17080                "        b();\n"
17081                "        }\n"
17082                "      }\n"
17083                "    void g()\n"
17084                "      {\n"
17085                "      return;\n"
17086                "      }\n"
17087                "    };\n"
17088                "  struct B\n"
17089                "    {\n"
17090                "    int x;\n"
17091                "    };\n"
17092                "  } // namespace b\n"
17093                "  } // namespace a",
17094                WhitesmithsBraceStyle);
17095 
17096   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17097   verifyFormat("namespace a\n"
17098                "  {\n"
17099                "  namespace b\n"
17100                "    {\n"
17101                "    class A\n"
17102                "      {\n"
17103                "      void f()\n"
17104                "        {\n"
17105                "        if (true)\n"
17106                "          {\n"
17107                "          a();\n"
17108                "          b();\n"
17109                "          }\n"
17110                "        }\n"
17111                "      void g()\n"
17112                "        {\n"
17113                "        return;\n"
17114                "        }\n"
17115                "      };\n"
17116                "    struct B\n"
17117                "      {\n"
17118                "      int x;\n"
17119                "      };\n"
17120                "    } // namespace b\n"
17121                "  }   // namespace a",
17122                WhitesmithsBraceStyle);
17123 
17124   verifyFormat("void f()\n"
17125                "  {\n"
17126                "  if (true)\n"
17127                "    {\n"
17128                "    a();\n"
17129                "    }\n"
17130                "  else if (false)\n"
17131                "    {\n"
17132                "    b();\n"
17133                "    }\n"
17134                "  else\n"
17135                "    {\n"
17136                "    c();\n"
17137                "    }\n"
17138                "  }\n",
17139                WhitesmithsBraceStyle);
17140 
17141   verifyFormat("void f()\n"
17142                "  {\n"
17143                "  for (int i = 0; i < 10; ++i)\n"
17144                "    {\n"
17145                "    a();\n"
17146                "    }\n"
17147                "  while (false)\n"
17148                "    {\n"
17149                "    b();\n"
17150                "    }\n"
17151                "  do\n"
17152                "    {\n"
17153                "    c();\n"
17154                "    } while (false)\n"
17155                "  }\n",
17156                WhitesmithsBraceStyle);
17157 
17158   WhitesmithsBraceStyle.IndentCaseLabels = true;
17159   verifyFormat("void switchTest1(int a)\n"
17160                "  {\n"
17161                "  switch (a)\n"
17162                "    {\n"
17163                "    case 2:\n"
17164                "      {\n"
17165                "      }\n"
17166                "      break;\n"
17167                "    }\n"
17168                "  }\n",
17169                WhitesmithsBraceStyle);
17170 
17171   verifyFormat("void switchTest2(int a)\n"
17172                "  {\n"
17173                "  switch (a)\n"
17174                "    {\n"
17175                "    case 0:\n"
17176                "      break;\n"
17177                "    case 1:\n"
17178                "      {\n"
17179                "      break;\n"
17180                "      }\n"
17181                "    case 2:\n"
17182                "      {\n"
17183                "      }\n"
17184                "      break;\n"
17185                "    default:\n"
17186                "      break;\n"
17187                "    }\n"
17188                "  }\n",
17189                WhitesmithsBraceStyle);
17190 
17191   verifyFormat("void switchTest3(int a)\n"
17192                "  {\n"
17193                "  switch (a)\n"
17194                "    {\n"
17195                "    case 0:\n"
17196                "      {\n"
17197                "      foo(x);\n"
17198                "      }\n"
17199                "      break;\n"
17200                "    default:\n"
17201                "      {\n"
17202                "      foo(1);\n"
17203                "      }\n"
17204                "      break;\n"
17205                "    }\n"
17206                "  }\n",
17207                WhitesmithsBraceStyle);
17208 
17209   WhitesmithsBraceStyle.IndentCaseLabels = false;
17210 
17211   verifyFormat("void switchTest4(int a)\n"
17212                "  {\n"
17213                "  switch (a)\n"
17214                "    {\n"
17215                "  case 2:\n"
17216                "    {\n"
17217                "    }\n"
17218                "    break;\n"
17219                "    }\n"
17220                "  }\n",
17221                WhitesmithsBraceStyle);
17222 
17223   verifyFormat("void switchTest5(int a)\n"
17224                "  {\n"
17225                "  switch (a)\n"
17226                "    {\n"
17227                "  case 0:\n"
17228                "    break;\n"
17229                "  case 1:\n"
17230                "    {\n"
17231                "    foo();\n"
17232                "    break;\n"
17233                "    }\n"
17234                "  case 2:\n"
17235                "    {\n"
17236                "    }\n"
17237                "    break;\n"
17238                "  default:\n"
17239                "    break;\n"
17240                "    }\n"
17241                "  }\n",
17242                WhitesmithsBraceStyle);
17243 
17244   verifyFormat("void switchTest6(int a)\n"
17245                "  {\n"
17246                "  switch (a)\n"
17247                "    {\n"
17248                "  case 0:\n"
17249                "    {\n"
17250                "    foo(x);\n"
17251                "    }\n"
17252                "    break;\n"
17253                "  default:\n"
17254                "    {\n"
17255                "    foo(1);\n"
17256                "    }\n"
17257                "    break;\n"
17258                "    }\n"
17259                "  }\n",
17260                WhitesmithsBraceStyle);
17261 
17262   verifyFormat("enum X\n"
17263                "  {\n"
17264                "  Y = 0, // testing\n"
17265                "  }\n",
17266                WhitesmithsBraceStyle);
17267 
17268   verifyFormat("enum X\n"
17269                "  {\n"
17270                "  Y = 0\n"
17271                "  }\n",
17272                WhitesmithsBraceStyle);
17273   verifyFormat("enum X\n"
17274                "  {\n"
17275                "  Y = 0,\n"
17276                "  Z = 1\n"
17277                "  };\n",
17278                WhitesmithsBraceStyle);
17279 
17280   verifyFormat("@interface BSApplicationController ()\n"
17281                "  {\n"
17282                "@private\n"
17283                "  id _extraIvar;\n"
17284                "  }\n"
17285                "@end\n",
17286                WhitesmithsBraceStyle);
17287 
17288   verifyFormat("#ifdef _DEBUG\n"
17289                "int foo(int i = 0)\n"
17290                "#else\n"
17291                "int foo(int i = 5)\n"
17292                "#endif\n"
17293                "  {\n"
17294                "  return i;\n"
17295                "  }",
17296                WhitesmithsBraceStyle);
17297 
17298   verifyFormat("void foo() {}\n"
17299                "void bar()\n"
17300                "#ifdef _DEBUG\n"
17301                "  {\n"
17302                "  foo();\n"
17303                "  }\n"
17304                "#else\n"
17305                "  {\n"
17306                "  }\n"
17307                "#endif",
17308                WhitesmithsBraceStyle);
17309 
17310   verifyFormat("void foobar()\n"
17311                "  {\n"
17312                "  int i = 5;\n"
17313                "  }\n"
17314                "#ifdef _DEBUG\n"
17315                "void bar()\n"
17316                "  {\n"
17317                "  }\n"
17318                "#else\n"
17319                "void bar()\n"
17320                "  {\n"
17321                "  foobar();\n"
17322                "  }\n"
17323                "#endif",
17324                WhitesmithsBraceStyle);
17325 
17326   // This shouldn't affect ObjC blocks..
17327   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17328                "  // ...\n"
17329                "  int i;\n"
17330                "}];",
17331                WhitesmithsBraceStyle);
17332   verifyFormat("void (^block)(void) = ^{\n"
17333                "  // ...\n"
17334                "  int i;\n"
17335                "};",
17336                WhitesmithsBraceStyle);
17337   // .. or dict literals.
17338   verifyFormat("void f()\n"
17339                "  {\n"
17340                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17341                "  }",
17342                WhitesmithsBraceStyle);
17343 
17344   verifyFormat("int f()\n"
17345                "  { // comment\n"
17346                "  return 42;\n"
17347                "  }",
17348                WhitesmithsBraceStyle);
17349 
17350   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
17351   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17352       FormatStyle::SIS_OnlyFirstIf;
17353   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17354   verifyFormat("void f(bool b)\n"
17355                "  {\n"
17356                "  if (b)\n"
17357                "    {\n"
17358                "    return;\n"
17359                "    }\n"
17360                "  }\n",
17361                BreakBeforeBraceShortIfs);
17362   verifyFormat("void f(bool b)\n"
17363                "  {\n"
17364                "  if (b) return;\n"
17365                "  }\n",
17366                BreakBeforeBraceShortIfs);
17367   verifyFormat("void f(bool b)\n"
17368                "  {\n"
17369                "  while (b)\n"
17370                "    {\n"
17371                "    return;\n"
17372                "    }\n"
17373                "  }\n",
17374                BreakBeforeBraceShortIfs);
17375 }
17376 
17377 TEST_F(FormatTest, GNUBraceBreaking) {
17378   FormatStyle GNUBraceStyle = getLLVMStyle();
17379   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
17380   verifyFormat("namespace a\n"
17381                "{\n"
17382                "class A\n"
17383                "{\n"
17384                "  void f()\n"
17385                "  {\n"
17386                "    int a;\n"
17387                "    {\n"
17388                "      int b;\n"
17389                "    }\n"
17390                "    if (true)\n"
17391                "      {\n"
17392                "        a();\n"
17393                "        b();\n"
17394                "      }\n"
17395                "  }\n"
17396                "  void g() { return; }\n"
17397                "}\n"
17398                "} // namespace a",
17399                GNUBraceStyle);
17400 
17401   verifyFormat("void f()\n"
17402                "{\n"
17403                "  if (true)\n"
17404                "    {\n"
17405                "      a();\n"
17406                "    }\n"
17407                "  else if (false)\n"
17408                "    {\n"
17409                "      b();\n"
17410                "    }\n"
17411                "  else\n"
17412                "    {\n"
17413                "      c();\n"
17414                "    }\n"
17415                "}\n",
17416                GNUBraceStyle);
17417 
17418   verifyFormat("void f()\n"
17419                "{\n"
17420                "  for (int i = 0; i < 10; ++i)\n"
17421                "    {\n"
17422                "      a();\n"
17423                "    }\n"
17424                "  while (false)\n"
17425                "    {\n"
17426                "      b();\n"
17427                "    }\n"
17428                "  do\n"
17429                "    {\n"
17430                "      c();\n"
17431                "    }\n"
17432                "  while (false);\n"
17433                "}\n",
17434                GNUBraceStyle);
17435 
17436   verifyFormat("void f(int a)\n"
17437                "{\n"
17438                "  switch (a)\n"
17439                "    {\n"
17440                "    case 0:\n"
17441                "      break;\n"
17442                "    case 1:\n"
17443                "      {\n"
17444                "        break;\n"
17445                "      }\n"
17446                "    case 2:\n"
17447                "      {\n"
17448                "      }\n"
17449                "      break;\n"
17450                "    default:\n"
17451                "      break;\n"
17452                "    }\n"
17453                "}\n",
17454                GNUBraceStyle);
17455 
17456   verifyFormat("enum X\n"
17457                "{\n"
17458                "  Y = 0,\n"
17459                "}\n",
17460                GNUBraceStyle);
17461 
17462   verifyFormat("@interface BSApplicationController ()\n"
17463                "{\n"
17464                "@private\n"
17465                "  id _extraIvar;\n"
17466                "}\n"
17467                "@end\n",
17468                GNUBraceStyle);
17469 
17470   verifyFormat("#ifdef _DEBUG\n"
17471                "int foo(int i = 0)\n"
17472                "#else\n"
17473                "int foo(int i = 5)\n"
17474                "#endif\n"
17475                "{\n"
17476                "  return i;\n"
17477                "}",
17478                GNUBraceStyle);
17479 
17480   verifyFormat("void foo() {}\n"
17481                "void bar()\n"
17482                "#ifdef _DEBUG\n"
17483                "{\n"
17484                "  foo();\n"
17485                "}\n"
17486                "#else\n"
17487                "{\n"
17488                "}\n"
17489                "#endif",
17490                GNUBraceStyle);
17491 
17492   verifyFormat("void foobar() { int i = 5; }\n"
17493                "#ifdef _DEBUG\n"
17494                "void bar() {}\n"
17495                "#else\n"
17496                "void bar() { foobar(); }\n"
17497                "#endif",
17498                GNUBraceStyle);
17499 }
17500 
17501 TEST_F(FormatTest, WebKitBraceBreaking) {
17502   FormatStyle WebKitBraceStyle = getLLVMStyle();
17503   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
17504   WebKitBraceStyle.FixNamespaceComments = false;
17505   verifyFormat("namespace a {\n"
17506                "class A {\n"
17507                "  void f()\n"
17508                "  {\n"
17509                "    if (true) {\n"
17510                "      a();\n"
17511                "      b();\n"
17512                "    }\n"
17513                "  }\n"
17514                "  void g() { return; }\n"
17515                "};\n"
17516                "enum E {\n"
17517                "  A,\n"
17518                "  // foo\n"
17519                "  B,\n"
17520                "  C\n"
17521                "};\n"
17522                "struct B {\n"
17523                "  int x;\n"
17524                "};\n"
17525                "}\n",
17526                WebKitBraceStyle);
17527   verifyFormat("struct S {\n"
17528                "  int Type;\n"
17529                "  union {\n"
17530                "    int x;\n"
17531                "    double y;\n"
17532                "  } Value;\n"
17533                "  class C {\n"
17534                "    MyFavoriteType Value;\n"
17535                "  } Class;\n"
17536                "};\n",
17537                WebKitBraceStyle);
17538 }
17539 
17540 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
17541   verifyFormat("void f() {\n"
17542                "  try {\n"
17543                "  } catch (const Exception &e) {\n"
17544                "  }\n"
17545                "}\n",
17546                getLLVMStyle());
17547 }
17548 
17549 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
17550   auto Style = getLLVMStyle();
17551   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17552   Style.AlignConsecutiveAssignments =
17553       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17554   Style.AlignConsecutiveDeclarations =
17555       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17556   verifyFormat("struct test demo[] = {\n"
17557                "    {56,    23, \"hello\"},\n"
17558                "    {-1, 93463, \"world\"},\n"
17559                "    { 7,     5,    \"!!\"}\n"
17560                "};\n",
17561                Style);
17562 
17563   verifyFormat("struct test demo[] = {\n"
17564                "    {56,    23, \"hello\"}, // first line\n"
17565                "    {-1, 93463, \"world\"}, // second line\n"
17566                "    { 7,     5,    \"!!\"}  // third line\n"
17567                "};\n",
17568                Style);
17569 
17570   verifyFormat("struct test demo[4] = {\n"
17571                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17572                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17573                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17574                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17575                "};\n",
17576                Style);
17577 
17578   verifyFormat("struct test demo[3] = {\n"
17579                "    {56,    23, \"hello\"},\n"
17580                "    {-1, 93463, \"world\"},\n"
17581                "    { 7,     5,    \"!!\"}\n"
17582                "};\n",
17583                Style);
17584 
17585   verifyFormat("struct test demo[3] = {\n"
17586                "    {int{56},    23, \"hello\"},\n"
17587                "    {int{-1}, 93463, \"world\"},\n"
17588                "    { int{7},     5,    \"!!\"}\n"
17589                "};\n",
17590                Style);
17591 
17592   verifyFormat("struct test demo[] = {\n"
17593                "    {56,    23, \"hello\"},\n"
17594                "    {-1, 93463, \"world\"},\n"
17595                "    { 7,     5,    \"!!\"},\n"
17596                "};\n",
17597                Style);
17598 
17599   verifyFormat("test demo[] = {\n"
17600                "    {56,    23, \"hello\"},\n"
17601                "    {-1, 93463, \"world\"},\n"
17602                "    { 7,     5,    \"!!\"},\n"
17603                "};\n",
17604                Style);
17605 
17606   verifyFormat("demo = std::array<struct test, 3>{\n"
17607                "    test{56,    23, \"hello\"},\n"
17608                "    test{-1, 93463, \"world\"},\n"
17609                "    test{ 7,     5,    \"!!\"},\n"
17610                "};\n",
17611                Style);
17612 
17613   verifyFormat("test demo[] = {\n"
17614                "    {56,    23, \"hello\"},\n"
17615                "#if X\n"
17616                "    {-1, 93463, \"world\"},\n"
17617                "#endif\n"
17618                "    { 7,     5,    \"!!\"}\n"
17619                "};\n",
17620                Style);
17621 
17622   verifyFormat(
17623       "test demo[] = {\n"
17624       "    { 7,    23,\n"
17625       "     \"hello world i am a very long line that really, in any\"\n"
17626       "     \"just world, ought to be split over multiple lines\"},\n"
17627       "    {-1, 93463,                                  \"world\"},\n"
17628       "    {56,     5,                                     \"!!\"}\n"
17629       "};\n",
17630       Style);
17631 
17632   verifyFormat("return GradForUnaryCwise(g, {\n"
17633                "                                {{\"sign\"}, \"Sign\",  "
17634                "  {\"x\", \"dy\"}},\n"
17635                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
17636                ", \"sign\"}},\n"
17637                "});\n",
17638                Style);
17639 
17640   Style.ColumnLimit = 0;
17641   EXPECT_EQ(
17642       "test demo[] = {\n"
17643       "    {56,    23, \"hello world i am a very long line that really, "
17644       "in any just world, ought to be split over multiple lines\"},\n"
17645       "    {-1, 93463,                                                  "
17646       "                                                 \"world\"},\n"
17647       "    { 7,     5,                                                  "
17648       "                                                    \"!!\"},\n"
17649       "};",
17650       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17651              "that really, in any just world, ought to be split over multiple "
17652              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17653              Style));
17654 
17655   Style.ColumnLimit = 80;
17656   verifyFormat("test demo[] = {\n"
17657                "    {56,    23, /* a comment */ \"hello\"},\n"
17658                "    {-1, 93463,                 \"world\"},\n"
17659                "    { 7,     5,                    \"!!\"}\n"
17660                "};\n",
17661                Style);
17662 
17663   verifyFormat("test demo[] = {\n"
17664                "    {56,    23,                    \"hello\"},\n"
17665                "    {-1, 93463, \"world\" /* comment here */},\n"
17666                "    { 7,     5,                       \"!!\"}\n"
17667                "};\n",
17668                Style);
17669 
17670   verifyFormat("test demo[] = {\n"
17671                "    {56, /* a comment */ 23, \"hello\"},\n"
17672                "    {-1,              93463, \"world\"},\n"
17673                "    { 7,                  5,    \"!!\"}\n"
17674                "};\n",
17675                Style);
17676 
17677   Style.ColumnLimit = 20;
17678   EXPECT_EQ(
17679       "demo = std::array<\n"
17680       "    struct test, 3>{\n"
17681       "    test{\n"
17682       "         56,    23,\n"
17683       "         \"hello \"\n"
17684       "         \"world i \"\n"
17685       "         \"am a very \"\n"
17686       "         \"long line \"\n"
17687       "         \"that \"\n"
17688       "         \"really, \"\n"
17689       "         \"in any \"\n"
17690       "         \"just \"\n"
17691       "         \"world, \"\n"
17692       "         \"ought to \"\n"
17693       "         \"be split \"\n"
17694       "         \"over \"\n"
17695       "         \"multiple \"\n"
17696       "         \"lines\"},\n"
17697       "    test{-1, 93463,\n"
17698       "         \"world\"},\n"
17699       "    test{ 7,     5,\n"
17700       "         \"!!\"   },\n"
17701       "};",
17702       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17703              "i am a very long line that really, in any just world, ought "
17704              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17705              "test{7, 5, \"!!\"},};",
17706              Style));
17707   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17708   Style = getLLVMStyleWithColumns(50);
17709   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17710   verifyFormat("static A x = {\n"
17711                "    {{init1, init2, init3, init4},\n"
17712                "     {init1, init2, init3, init4}}\n"
17713                "};",
17714                Style);
17715   Style.ColumnLimit = 100;
17716   EXPECT_EQ(
17717       "test demo[] = {\n"
17718       "    {56,    23,\n"
17719       "     \"hello world i am a very long line that really, in any just world"
17720       ", ought to be split over \"\n"
17721       "     \"multiple lines\"  },\n"
17722       "    {-1, 93463, \"world\"},\n"
17723       "    { 7,     5,    \"!!\"},\n"
17724       "};",
17725       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17726              "that really, in any just world, ought to be split over multiple "
17727              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17728              Style));
17729 
17730   Style = getLLVMStyleWithColumns(50);
17731   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17732   Style.AlignConsecutiveAssignments =
17733       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17734   Style.AlignConsecutiveDeclarations =
17735       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17736   verifyFormat("struct test demo[] = {\n"
17737                "    {56,    23, \"hello\"},\n"
17738                "    {-1, 93463, \"world\"},\n"
17739                "    { 7,     5,    \"!!\"}\n"
17740                "};\n"
17741                "static A x = {\n"
17742                "    {{init1, init2, init3, init4},\n"
17743                "     {init1, init2, init3, init4}}\n"
17744                "};",
17745                Style);
17746   Style.ColumnLimit = 100;
17747   Style.AlignConsecutiveAssignments =
17748       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17749   Style.AlignConsecutiveDeclarations =
17750       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17751   verifyFormat("struct test demo[] = {\n"
17752                "    {56,    23, \"hello\"},\n"
17753                "    {-1, 93463, \"world\"},\n"
17754                "    { 7,     5,    \"!!\"}\n"
17755                "};\n"
17756                "struct test demo[4] = {\n"
17757                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17758                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17759                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17760                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17761                "};\n",
17762                Style);
17763   EXPECT_EQ(
17764       "test demo[] = {\n"
17765       "    {56,\n"
17766       "     \"hello world i am a very long line that really, in any just world"
17767       ", ought to be split over \"\n"
17768       "     \"multiple lines\",    23},\n"
17769       "    {-1,      \"world\", 93463},\n"
17770       "    { 7,         \"!!\",     5},\n"
17771       "};",
17772       format("test demo[] = {{56, \"hello world i am a very long line "
17773              "that really, in any just world, ought to be split over multiple "
17774              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
17775              Style));
17776 }
17777 
17778 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
17779   auto Style = getLLVMStyle();
17780   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
17781   verifyFormat("struct test demo[] = {\n"
17782                "    {56, 23,    \"hello\"},\n"
17783                "    {-1, 93463, \"world\"},\n"
17784                "    {7,  5,     \"!!\"   }\n"
17785                "};\n",
17786                Style);
17787 
17788   verifyFormat("struct test demo[] = {\n"
17789                "    {56, 23,    \"hello\"}, // first line\n"
17790                "    {-1, 93463, \"world\"}, // second line\n"
17791                "    {7,  5,     \"!!\"   }  // third line\n"
17792                "};\n",
17793                Style);
17794   verifyFormat("struct test demo[4] = {\n"
17795                "    {56,  23,    21, \"oh\"      }, // first line\n"
17796                "    {-1,  93463, 22, \"my\"      }, // second line\n"
17797                "    {7,   5,     1,  \"goodness\"}  // third line\n"
17798                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
17799                "};\n",
17800                Style);
17801   verifyFormat("struct test demo[3] = {\n"
17802                "    {56, 23,    \"hello\"},\n"
17803                "    {-1, 93463, \"world\"},\n"
17804                "    {7,  5,     \"!!\"   }\n"
17805                "};\n",
17806                Style);
17807 
17808   verifyFormat("struct test demo[3] = {\n"
17809                "    {int{56}, 23,    \"hello\"},\n"
17810                "    {int{-1}, 93463, \"world\"},\n"
17811                "    {int{7},  5,     \"!!\"   }\n"
17812                "};\n",
17813                Style);
17814   verifyFormat("struct test demo[] = {\n"
17815                "    {56, 23,    \"hello\"},\n"
17816                "    {-1, 93463, \"world\"},\n"
17817                "    {7,  5,     \"!!\"   },\n"
17818                "};\n",
17819                Style);
17820   verifyFormat("test demo[] = {\n"
17821                "    {56, 23,    \"hello\"},\n"
17822                "    {-1, 93463, \"world\"},\n"
17823                "    {7,  5,     \"!!\"   },\n"
17824                "};\n",
17825                Style);
17826   verifyFormat("demo = std::array<struct test, 3>{\n"
17827                "    test{56, 23,    \"hello\"},\n"
17828                "    test{-1, 93463, \"world\"},\n"
17829                "    test{7,  5,     \"!!\"   },\n"
17830                "};\n",
17831                Style);
17832   verifyFormat("test demo[] = {\n"
17833                "    {56, 23,    \"hello\"},\n"
17834                "#if X\n"
17835                "    {-1, 93463, \"world\"},\n"
17836                "#endif\n"
17837                "    {7,  5,     \"!!\"   }\n"
17838                "};\n",
17839                Style);
17840   verifyFormat(
17841       "test demo[] = {\n"
17842       "    {7,  23,\n"
17843       "     \"hello world i am a very long line that really, in any\"\n"
17844       "     \"just world, ought to be split over multiple lines\"},\n"
17845       "    {-1, 93463, \"world\"                                 },\n"
17846       "    {56, 5,     \"!!\"                                    }\n"
17847       "};\n",
17848       Style);
17849 
17850   verifyFormat("return GradForUnaryCwise(g, {\n"
17851                "                                {{\"sign\"}, \"Sign\", {\"x\", "
17852                "\"dy\"}   },\n"
17853                "                                {{\"dx\"},   \"Mul\",  "
17854                "{\"dy\", \"sign\"}},\n"
17855                "});\n",
17856                Style);
17857 
17858   Style.ColumnLimit = 0;
17859   EXPECT_EQ(
17860       "test demo[] = {\n"
17861       "    {56, 23,    \"hello world i am a very long line that really, in any "
17862       "just world, ought to be split over multiple lines\"},\n"
17863       "    {-1, 93463, \"world\"                                               "
17864       "                                                   },\n"
17865       "    {7,  5,     \"!!\"                                                  "
17866       "                                                   },\n"
17867       "};",
17868       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17869              "that really, in any just world, ought to be split over multiple "
17870              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17871              Style));
17872 
17873   Style.ColumnLimit = 80;
17874   verifyFormat("test demo[] = {\n"
17875                "    {56, 23,    /* a comment */ \"hello\"},\n"
17876                "    {-1, 93463, \"world\"                },\n"
17877                "    {7,  5,     \"!!\"                   }\n"
17878                "};\n",
17879                Style);
17880 
17881   verifyFormat("test demo[] = {\n"
17882                "    {56, 23,    \"hello\"                   },\n"
17883                "    {-1, 93463, \"world\" /* comment here */},\n"
17884                "    {7,  5,     \"!!\"                      }\n"
17885                "};\n",
17886                Style);
17887 
17888   verifyFormat("test demo[] = {\n"
17889                "    {56, /* a comment */ 23, \"hello\"},\n"
17890                "    {-1, 93463,              \"world\"},\n"
17891                "    {7,  5,                  \"!!\"   }\n"
17892                "};\n",
17893                Style);
17894 
17895   Style.ColumnLimit = 20;
17896   EXPECT_EQ(
17897       "demo = std::array<\n"
17898       "    struct test, 3>{\n"
17899       "    test{\n"
17900       "         56, 23,\n"
17901       "         \"hello \"\n"
17902       "         \"world i \"\n"
17903       "         \"am a very \"\n"
17904       "         \"long line \"\n"
17905       "         \"that \"\n"
17906       "         \"really, \"\n"
17907       "         \"in any \"\n"
17908       "         \"just \"\n"
17909       "         \"world, \"\n"
17910       "         \"ought to \"\n"
17911       "         \"be split \"\n"
17912       "         \"over \"\n"
17913       "         \"multiple \"\n"
17914       "         \"lines\"},\n"
17915       "    test{-1, 93463,\n"
17916       "         \"world\"},\n"
17917       "    test{7,  5,\n"
17918       "         \"!!\"   },\n"
17919       "};",
17920       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17921              "i am a very long line that really, in any just world, ought "
17922              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17923              "test{7, 5, \"!!\"},};",
17924              Style));
17925 
17926   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17927   Style = getLLVMStyleWithColumns(50);
17928   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
17929   verifyFormat("static A x = {\n"
17930                "    {{init1, init2, init3, init4},\n"
17931                "     {init1, init2, init3, init4}}\n"
17932                "};",
17933                Style);
17934   Style.ColumnLimit = 100;
17935   EXPECT_EQ(
17936       "test demo[] = {\n"
17937       "    {56, 23,\n"
17938       "     \"hello world i am a very long line that really, in any just world"
17939       ", ought to be split over \"\n"
17940       "     \"multiple lines\"  },\n"
17941       "    {-1, 93463, \"world\"},\n"
17942       "    {7,  5,     \"!!\"   },\n"
17943       "};",
17944       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17945              "that really, in any just world, ought to be split over multiple "
17946              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17947              Style));
17948 }
17949 
17950 TEST_F(FormatTest, UnderstandsPragmas) {
17951   verifyFormat("#pragma omp reduction(| : var)");
17952   verifyFormat("#pragma omp reduction(+ : var)");
17953 
17954   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
17955             "(including parentheses).",
17956             format("#pragma    mark   Any non-hyphenated or hyphenated string "
17957                    "(including parentheses)."));
17958 }
17959 
17960 TEST_F(FormatTest, UnderstandPragmaOption) {
17961   verifyFormat("#pragma option -C -A");
17962 
17963   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
17964 }
17965 
17966 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
17967   FormatStyle Style = getLLVMStyle();
17968   Style.ColumnLimit = 20;
17969 
17970   // See PR41213
17971   EXPECT_EQ("/*\n"
17972             " *\t9012345\n"
17973             " * /8901\n"
17974             " */",
17975             format("/*\n"
17976                    " *\t9012345 /8901\n"
17977                    " */",
17978                    Style));
17979   EXPECT_EQ("/*\n"
17980             " *345678\n"
17981             " *\t/8901\n"
17982             " */",
17983             format("/*\n"
17984                    " *345678\t/8901\n"
17985                    " */",
17986                    Style));
17987 
17988   verifyFormat("int a; // the\n"
17989                "       // comment",
17990                Style);
17991   EXPECT_EQ("int a; /* first line\n"
17992             "        * second\n"
17993             "        * line third\n"
17994             "        * line\n"
17995             "        */",
17996             format("int a; /* first line\n"
17997                    "        * second\n"
17998                    "        * line third\n"
17999                    "        * line\n"
18000                    "        */",
18001                    Style));
18002   EXPECT_EQ("int a; // first line\n"
18003             "       // second\n"
18004             "       // line third\n"
18005             "       // line",
18006             format("int a; // first line\n"
18007                    "       // second line\n"
18008                    "       // third line",
18009                    Style));
18010 
18011   Style.PenaltyExcessCharacter = 90;
18012   verifyFormat("int a; // the comment", Style);
18013   EXPECT_EQ("int a; // the comment\n"
18014             "       // aaa",
18015             format("int a; // the comment aaa", Style));
18016   EXPECT_EQ("int a; /* first line\n"
18017             "        * second line\n"
18018             "        * third line\n"
18019             "        */",
18020             format("int a; /* first line\n"
18021                    "        * second line\n"
18022                    "        * third line\n"
18023                    "        */",
18024                    Style));
18025   EXPECT_EQ("int a; // first line\n"
18026             "       // second line\n"
18027             "       // third line",
18028             format("int a; // first line\n"
18029                    "       // second line\n"
18030                    "       // third line",
18031                    Style));
18032   // FIXME: Investigate why this is not getting the same layout as the test
18033   // above.
18034   EXPECT_EQ("int a; /* first line\n"
18035             "        * second line\n"
18036             "        * third line\n"
18037             "        */",
18038             format("int a; /* first line second line third line"
18039                    "\n*/",
18040                    Style));
18041 
18042   EXPECT_EQ("// foo bar baz bazfoo\n"
18043             "// foo bar foo bar\n",
18044             format("// foo bar baz bazfoo\n"
18045                    "// foo bar foo           bar\n",
18046                    Style));
18047   EXPECT_EQ("// foo bar baz bazfoo\n"
18048             "// foo bar foo bar\n",
18049             format("// foo bar baz      bazfoo\n"
18050                    "// foo            bar foo bar\n",
18051                    Style));
18052 
18053   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
18054   // next one.
18055   EXPECT_EQ("// foo bar baz bazfoo\n"
18056             "// bar foo bar\n",
18057             format("// foo bar baz      bazfoo bar\n"
18058                    "// foo            bar\n",
18059                    Style));
18060 
18061   EXPECT_EQ("// foo bar baz bazfoo\n"
18062             "// foo bar baz bazfoo\n"
18063             "// bar foo bar\n",
18064             format("// foo bar baz      bazfoo\n"
18065                    "// foo bar baz      bazfoo bar\n"
18066                    "// foo bar\n",
18067                    Style));
18068 
18069   EXPECT_EQ("// foo bar baz bazfoo\n"
18070             "// foo bar baz bazfoo\n"
18071             "// bar foo bar\n",
18072             format("// foo bar baz      bazfoo\n"
18073                    "// foo bar baz      bazfoo bar\n"
18074                    "// foo           bar\n",
18075                    Style));
18076 
18077   // Make sure we do not keep protruding characters if strict mode reflow is
18078   // cheaper than keeping protruding characters.
18079   Style.ColumnLimit = 21;
18080   EXPECT_EQ(
18081       "// foo foo foo foo\n"
18082       "// foo foo foo foo\n"
18083       "// foo foo foo foo\n",
18084       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18085 
18086   EXPECT_EQ("int a = /* long block\n"
18087             "           comment */\n"
18088             "    42;",
18089             format("int a = /* long block comment */ 42;", Style));
18090 }
18091 
18092 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
18093   for (size_t i = 1; i < Styles.size(); ++i)                                   \
18094   EXPECT_EQ(Styles[0], Styles[i])                                              \
18095       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
18096 
18097 TEST_F(FormatTest, GetsPredefinedStyleByName) {
18098   SmallVector<FormatStyle, 3> Styles;
18099   Styles.resize(3);
18100 
18101   Styles[0] = getLLVMStyle();
18102   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
18103   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
18104   EXPECT_ALL_STYLES_EQUAL(Styles);
18105 
18106   Styles[0] = getGoogleStyle();
18107   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
18108   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
18109   EXPECT_ALL_STYLES_EQUAL(Styles);
18110 
18111   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18112   EXPECT_TRUE(
18113       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
18114   EXPECT_TRUE(
18115       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
18116   EXPECT_ALL_STYLES_EQUAL(Styles);
18117 
18118   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
18119   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
18120   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
18121   EXPECT_ALL_STYLES_EQUAL(Styles);
18122 
18123   Styles[0] = getMozillaStyle();
18124   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
18125   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
18126   EXPECT_ALL_STYLES_EQUAL(Styles);
18127 
18128   Styles[0] = getWebKitStyle();
18129   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
18130   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
18131   EXPECT_ALL_STYLES_EQUAL(Styles);
18132 
18133   Styles[0] = getGNUStyle();
18134   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
18135   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
18136   EXPECT_ALL_STYLES_EQUAL(Styles);
18137 
18138   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
18139 }
18140 
18141 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
18142   SmallVector<FormatStyle, 8> Styles;
18143   Styles.resize(2);
18144 
18145   Styles[0] = getGoogleStyle();
18146   Styles[1] = getLLVMStyle();
18147   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18148   EXPECT_ALL_STYLES_EQUAL(Styles);
18149 
18150   Styles.resize(5);
18151   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18152   Styles[1] = getLLVMStyle();
18153   Styles[1].Language = FormatStyle::LK_JavaScript;
18154   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18155 
18156   Styles[2] = getLLVMStyle();
18157   Styles[2].Language = FormatStyle::LK_JavaScript;
18158   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
18159                                   "BasedOnStyle: Google",
18160                                   &Styles[2])
18161                    .value());
18162 
18163   Styles[3] = getLLVMStyle();
18164   Styles[3].Language = FormatStyle::LK_JavaScript;
18165   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
18166                                   "Language: JavaScript",
18167                                   &Styles[3])
18168                    .value());
18169 
18170   Styles[4] = getLLVMStyle();
18171   Styles[4].Language = FormatStyle::LK_JavaScript;
18172   EXPECT_EQ(0, parseConfiguration("---\n"
18173                                   "BasedOnStyle: LLVM\n"
18174                                   "IndentWidth: 123\n"
18175                                   "---\n"
18176                                   "BasedOnStyle: Google\n"
18177                                   "Language: JavaScript",
18178                                   &Styles[4])
18179                    .value());
18180   EXPECT_ALL_STYLES_EQUAL(Styles);
18181 }
18182 
18183 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
18184   Style.FIELD = false;                                                         \
18185   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
18186   EXPECT_TRUE(Style.FIELD);                                                    \
18187   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
18188   EXPECT_FALSE(Style.FIELD);
18189 
18190 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
18191 
18192 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
18193   Style.STRUCT.FIELD = false;                                                  \
18194   EXPECT_EQ(0,                                                                 \
18195             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
18196                 .value());                                                     \
18197   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
18198   EXPECT_EQ(0,                                                                 \
18199             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
18200                 .value());                                                     \
18201   EXPECT_FALSE(Style.STRUCT.FIELD);
18202 
18203 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
18204   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
18205 
18206 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
18207   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
18208   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
18209   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
18210 
18211 TEST_F(FormatTest, ParsesConfigurationBools) {
18212   FormatStyle Style = {};
18213   Style.Language = FormatStyle::LK_Cpp;
18214   CHECK_PARSE_BOOL(AlignTrailingComments);
18215   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
18216   CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
18217   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
18218   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
18219   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
18220   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
18221   CHECK_PARSE_BOOL(BinPackArguments);
18222   CHECK_PARSE_BOOL(BinPackParameters);
18223   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
18224   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
18225   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
18226   CHECK_PARSE_BOOL(BreakStringLiterals);
18227   CHECK_PARSE_BOOL(CompactNamespaces);
18228   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
18229   CHECK_PARSE_BOOL(DeriveLineEnding);
18230   CHECK_PARSE_BOOL(DerivePointerAlignment);
18231   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
18232   CHECK_PARSE_BOOL(DisableFormat);
18233   CHECK_PARSE_BOOL(IndentAccessModifiers);
18234   CHECK_PARSE_BOOL(IndentCaseLabels);
18235   CHECK_PARSE_BOOL(IndentCaseBlocks);
18236   CHECK_PARSE_BOOL(IndentGotoLabels);
18237   CHECK_PARSE_BOOL(IndentRequires);
18238   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
18239   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
18240   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
18241   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
18242   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
18243   CHECK_PARSE_BOOL(ReflowComments);
18244   CHECK_PARSE_BOOL(SortUsingDeclarations);
18245   CHECK_PARSE_BOOL(SpacesInParentheses);
18246   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
18247   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
18248   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
18249   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
18250   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
18251   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
18252   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
18253   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
18254   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
18255   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
18256   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
18257   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
18258   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
18259   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
18260   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
18261   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
18262   CHECK_PARSE_BOOL(UseCRLF);
18263 
18264   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
18265   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
18266   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
18267   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
18268   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
18269   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
18270   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
18271   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
18272   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
18273   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
18274   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
18275   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
18276   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
18277   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
18278   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
18279   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
18280   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
18281 }
18282 
18283 #undef CHECK_PARSE_BOOL
18284 
18285 TEST_F(FormatTest, ParsesConfiguration) {
18286   FormatStyle Style = {};
18287   Style.Language = FormatStyle::LK_Cpp;
18288   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
18289   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
18290               ConstructorInitializerIndentWidth, 1234u);
18291   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
18292   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
18293   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
18294   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
18295   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
18296               PenaltyBreakBeforeFirstCallParameter, 1234u);
18297   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
18298               PenaltyBreakTemplateDeclaration, 1234u);
18299   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
18300   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
18301               PenaltyReturnTypeOnItsOwnLine, 1234u);
18302   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
18303               SpacesBeforeTrailingComments, 1234u);
18304   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
18305   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
18306   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
18307 
18308   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
18309   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
18310               FormatStyle::ACS_None);
18311   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
18312               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
18313   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
18314               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
18315   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
18316               AlignConsecutiveAssignments,
18317               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18318   // For backwards compability, false / true should still parse
18319   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
18320               FormatStyle::ACS_None);
18321   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
18322               FormatStyle::ACS_Consecutive);
18323 
18324   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
18325   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
18326               FormatStyle::ACS_None);
18327   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
18328               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
18329   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
18330               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
18331   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
18332               AlignConsecutiveBitFields,
18333               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18334   // For backwards compability, false / true should still parse
18335   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
18336               FormatStyle::ACS_None);
18337   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
18338               FormatStyle::ACS_Consecutive);
18339 
18340   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
18341   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
18342               FormatStyle::ACS_None);
18343   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
18344               FormatStyle::ACS_Consecutive);
18345   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
18346               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
18347   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
18348               AlignConsecutiveMacros,
18349               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18350   // For backwards compability, false / true should still parse
18351   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
18352               FormatStyle::ACS_None);
18353   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
18354               FormatStyle::ACS_Consecutive);
18355 
18356   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
18357   CHECK_PARSE("AlignConsecutiveDeclarations: None",
18358               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18359   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
18360               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18361   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
18362               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
18363   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
18364               AlignConsecutiveDeclarations,
18365               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18366   // For backwards compability, false / true should still parse
18367   CHECK_PARSE("AlignConsecutiveDeclarations: false",
18368               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18369   CHECK_PARSE("AlignConsecutiveDeclarations: true",
18370               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18371 
18372   Style.PointerAlignment = FormatStyle::PAS_Middle;
18373   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
18374               FormatStyle::PAS_Left);
18375   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
18376               FormatStyle::PAS_Right);
18377   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
18378               FormatStyle::PAS_Middle);
18379   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
18380   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
18381               FormatStyle::RAS_Pointer);
18382   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
18383               FormatStyle::RAS_Left);
18384   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
18385               FormatStyle::RAS_Right);
18386   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
18387               FormatStyle::RAS_Middle);
18388   // For backward compatibility:
18389   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
18390               FormatStyle::PAS_Left);
18391   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
18392               FormatStyle::PAS_Right);
18393   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
18394               FormatStyle::PAS_Middle);
18395 
18396   Style.Standard = FormatStyle::LS_Auto;
18397   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
18398   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
18399   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
18400   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
18401   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
18402   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
18403   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
18404   // Legacy aliases:
18405   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
18406   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
18407   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
18408   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
18409 
18410   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18411   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
18412               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
18413   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
18414               FormatStyle::BOS_None);
18415   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
18416               FormatStyle::BOS_All);
18417   // For backward compatibility:
18418   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
18419               FormatStyle::BOS_None);
18420   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
18421               FormatStyle::BOS_All);
18422 
18423   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
18424   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
18425               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18426   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
18427               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
18428   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
18429               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
18430   // For backward compatibility:
18431   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
18432               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18433 
18434   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18435   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
18436               FormatStyle::BILS_AfterComma);
18437   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
18438               FormatStyle::BILS_BeforeComma);
18439   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
18440               FormatStyle::BILS_AfterColon);
18441   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
18442               FormatStyle::BILS_BeforeColon);
18443   // For backward compatibility:
18444   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
18445               FormatStyle::BILS_BeforeComma);
18446 
18447   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
18448   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
18449               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
18450   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
18451               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
18452   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
18453               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
18454   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
18455               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
18456 
18457   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18458   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
18459               FormatStyle::BAS_Align);
18460   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
18461               FormatStyle::BAS_DontAlign);
18462   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
18463               FormatStyle::BAS_AlwaysBreak);
18464   // For backward compatibility:
18465   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
18466               FormatStyle::BAS_DontAlign);
18467   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
18468               FormatStyle::BAS_Align);
18469 
18470   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18471   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
18472               FormatStyle::ENAS_DontAlign);
18473   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
18474               FormatStyle::ENAS_Left);
18475   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
18476               FormatStyle::ENAS_Right);
18477   // For backward compatibility:
18478   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
18479               FormatStyle::ENAS_Left);
18480   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
18481               FormatStyle::ENAS_Right);
18482 
18483   Style.AlignOperands = FormatStyle::OAS_Align;
18484   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
18485               FormatStyle::OAS_DontAlign);
18486   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
18487   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
18488               FormatStyle::OAS_AlignAfterOperator);
18489   // For backward compatibility:
18490   CHECK_PARSE("AlignOperands: false", AlignOperands,
18491               FormatStyle::OAS_DontAlign);
18492   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
18493 
18494   Style.UseTab = FormatStyle::UT_ForIndentation;
18495   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
18496   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
18497   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
18498   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
18499               FormatStyle::UT_ForContinuationAndIndentation);
18500   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
18501               FormatStyle::UT_AlignWithSpaces);
18502   // For backward compatibility:
18503   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
18504   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
18505 
18506   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
18507   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
18508               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18509   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
18510               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
18511   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
18512               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18513   // For backward compatibility:
18514   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
18515               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18516   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
18517               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18518 
18519   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18520   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
18521               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18522   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
18523               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
18524   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
18525               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
18526   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
18527               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18528   // For backward compatibility:
18529   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
18530               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18531   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
18532               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18533 
18534   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
18535   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
18536               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
18537   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
18538               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
18539   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
18540               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
18541   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
18542               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
18543 
18544   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
18545   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
18546               FormatStyle::SBPO_Never);
18547   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
18548               FormatStyle::SBPO_Always);
18549   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
18550               FormatStyle::SBPO_ControlStatements);
18551   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
18552               SpaceBeforeParens,
18553               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18554   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
18555               FormatStyle::SBPO_NonEmptyParentheses);
18556   // For backward compatibility:
18557   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
18558               FormatStyle::SBPO_Never);
18559   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
18560               FormatStyle::SBPO_ControlStatements);
18561   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
18562               SpaceBeforeParens,
18563               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18564 
18565   Style.ColumnLimit = 123;
18566   FormatStyle BaseStyle = getLLVMStyle();
18567   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
18568   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
18569 
18570   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
18571   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
18572               FormatStyle::BS_Attach);
18573   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
18574               FormatStyle::BS_Linux);
18575   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
18576               FormatStyle::BS_Mozilla);
18577   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
18578               FormatStyle::BS_Stroustrup);
18579   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
18580               FormatStyle::BS_Allman);
18581   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
18582               FormatStyle::BS_Whitesmiths);
18583   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
18584   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
18585               FormatStyle::BS_WebKit);
18586   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
18587               FormatStyle::BS_Custom);
18588 
18589   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
18590   CHECK_PARSE("BraceWrapping:\n"
18591               "  AfterControlStatement: MultiLine",
18592               BraceWrapping.AfterControlStatement,
18593               FormatStyle::BWACS_MultiLine);
18594   CHECK_PARSE("BraceWrapping:\n"
18595               "  AfterControlStatement: Always",
18596               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18597   CHECK_PARSE("BraceWrapping:\n"
18598               "  AfterControlStatement: Never",
18599               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18600   // For backward compatibility:
18601   CHECK_PARSE("BraceWrapping:\n"
18602               "  AfterControlStatement: true",
18603               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18604   CHECK_PARSE("BraceWrapping:\n"
18605               "  AfterControlStatement: false",
18606               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18607 
18608   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
18609   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
18610               FormatStyle::RTBS_None);
18611   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
18612               FormatStyle::RTBS_All);
18613   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
18614               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
18615   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
18616               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
18617   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
18618               AlwaysBreakAfterReturnType,
18619               FormatStyle::RTBS_TopLevelDefinitions);
18620 
18621   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
18622   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
18623               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
18624   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
18625               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18626   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
18627               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18628   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
18629               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18630   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
18631               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18632 
18633   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
18634   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
18635               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
18636   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
18637               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
18638   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
18639               AlwaysBreakAfterDefinitionReturnType,
18640               FormatStyle::DRTBS_TopLevel);
18641 
18642   Style.NamespaceIndentation = FormatStyle::NI_All;
18643   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
18644               FormatStyle::NI_None);
18645   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
18646               FormatStyle::NI_Inner);
18647   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
18648               FormatStyle::NI_All);
18649 
18650   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
18651   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
18652               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18653   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
18654               AllowShortIfStatementsOnASingleLine,
18655               FormatStyle::SIS_WithoutElse);
18656   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
18657               AllowShortIfStatementsOnASingleLine,
18658               FormatStyle::SIS_OnlyFirstIf);
18659   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
18660               AllowShortIfStatementsOnASingleLine,
18661               FormatStyle::SIS_AllIfsAndElse);
18662   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
18663               AllowShortIfStatementsOnASingleLine,
18664               FormatStyle::SIS_OnlyFirstIf);
18665   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
18666               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18667   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
18668               AllowShortIfStatementsOnASingleLine,
18669               FormatStyle::SIS_WithoutElse);
18670 
18671   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
18672   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
18673               FormatStyle::IEBS_AfterExternBlock);
18674   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
18675               FormatStyle::IEBS_Indent);
18676   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
18677               FormatStyle::IEBS_NoIndent);
18678   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
18679               FormatStyle::IEBS_Indent);
18680   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
18681               FormatStyle::IEBS_NoIndent);
18682 
18683   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
18684   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
18685               FormatStyle::BFCS_Both);
18686   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
18687               FormatStyle::BFCS_None);
18688   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
18689               FormatStyle::BFCS_Before);
18690   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
18691               FormatStyle::BFCS_After);
18692 
18693   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
18694   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
18695               FormatStyle::SJSIO_After);
18696   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
18697               FormatStyle::SJSIO_Before);
18698 
18699   // FIXME: This is required because parsing a configuration simply overwrites
18700   // the first N elements of the list instead of resetting it.
18701   Style.ForEachMacros.clear();
18702   std::vector<std::string> BoostForeach;
18703   BoostForeach.push_back("BOOST_FOREACH");
18704   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
18705   std::vector<std::string> BoostAndQForeach;
18706   BoostAndQForeach.push_back("BOOST_FOREACH");
18707   BoostAndQForeach.push_back("Q_FOREACH");
18708   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
18709               BoostAndQForeach);
18710 
18711   Style.IfMacros.clear();
18712   std::vector<std::string> CustomIfs;
18713   CustomIfs.push_back("MYIF");
18714   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
18715 
18716   Style.AttributeMacros.clear();
18717   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
18718               std::vector<std::string>{"__capability"});
18719   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
18720               std::vector<std::string>({"attr1", "attr2"}));
18721 
18722   Style.StatementAttributeLikeMacros.clear();
18723   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
18724               StatementAttributeLikeMacros,
18725               std::vector<std::string>({"emit", "Q_EMIT"}));
18726 
18727   Style.StatementMacros.clear();
18728   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
18729               std::vector<std::string>{"QUNUSED"});
18730   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
18731               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
18732 
18733   Style.NamespaceMacros.clear();
18734   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
18735               std::vector<std::string>{"TESTSUITE"});
18736   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
18737               std::vector<std::string>({"TESTSUITE", "SUITE"}));
18738 
18739   Style.WhitespaceSensitiveMacros.clear();
18740   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
18741               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
18742   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
18743               WhitespaceSensitiveMacros,
18744               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
18745   Style.WhitespaceSensitiveMacros.clear();
18746   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
18747               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
18748   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
18749               WhitespaceSensitiveMacros,
18750               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
18751 
18752   Style.IncludeStyle.IncludeCategories.clear();
18753   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
18754       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
18755   CHECK_PARSE("IncludeCategories:\n"
18756               "  - Regex: abc/.*\n"
18757               "    Priority: 2\n"
18758               "  - Regex: .*\n"
18759               "    Priority: 1\n"
18760               "    CaseSensitive: true\n",
18761               IncludeStyle.IncludeCategories, ExpectedCategories);
18762   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
18763               "abc$");
18764   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
18765               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
18766 
18767   Style.SortIncludes = FormatStyle::SI_Never;
18768   CHECK_PARSE("SortIncludes: true", SortIncludes,
18769               FormatStyle::SI_CaseSensitive);
18770   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
18771   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
18772               FormatStyle::SI_CaseInsensitive);
18773   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
18774               FormatStyle::SI_CaseSensitive);
18775   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
18776 
18777   Style.RawStringFormats.clear();
18778   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
18779       {
18780           FormatStyle::LK_TextProto,
18781           {"pb", "proto"},
18782           {"PARSE_TEXT_PROTO"},
18783           /*CanonicalDelimiter=*/"",
18784           "llvm",
18785       },
18786       {
18787           FormatStyle::LK_Cpp,
18788           {"cc", "cpp"},
18789           {"C_CODEBLOCK", "CPPEVAL"},
18790           /*CanonicalDelimiter=*/"cc",
18791           /*BasedOnStyle=*/"",
18792       },
18793   };
18794 
18795   CHECK_PARSE("RawStringFormats:\n"
18796               "  - Language: TextProto\n"
18797               "    Delimiters:\n"
18798               "      - 'pb'\n"
18799               "      - 'proto'\n"
18800               "    EnclosingFunctions:\n"
18801               "      - 'PARSE_TEXT_PROTO'\n"
18802               "    BasedOnStyle: llvm\n"
18803               "  - Language: Cpp\n"
18804               "    Delimiters:\n"
18805               "      - 'cc'\n"
18806               "      - 'cpp'\n"
18807               "    EnclosingFunctions:\n"
18808               "      - 'C_CODEBLOCK'\n"
18809               "      - 'CPPEVAL'\n"
18810               "    CanonicalDelimiter: 'cc'",
18811               RawStringFormats, ExpectedRawStringFormats);
18812 
18813   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18814               "  Minimum: 0\n"
18815               "  Maximum: 0",
18816               SpacesInLineCommentPrefix.Minimum, 0u);
18817   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
18818   Style.SpacesInLineCommentPrefix.Minimum = 1;
18819   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18820               "  Minimum: 2",
18821               SpacesInLineCommentPrefix.Minimum, 0u);
18822   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18823               "  Maximum: -1",
18824               SpacesInLineCommentPrefix.Maximum, -1u);
18825   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18826               "  Minimum: 2",
18827               SpacesInLineCommentPrefix.Minimum, 2u);
18828   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18829               "  Maximum: 1",
18830               SpacesInLineCommentPrefix.Maximum, 1u);
18831   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
18832 
18833   Style.SpacesInAngles = FormatStyle::SIAS_Always;
18834   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
18835   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
18836               FormatStyle::SIAS_Always);
18837   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
18838   // For backward compatibility:
18839   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
18840   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
18841 }
18842 
18843 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
18844   FormatStyle Style = {};
18845   Style.Language = FormatStyle::LK_Cpp;
18846   CHECK_PARSE("Language: Cpp\n"
18847               "IndentWidth: 12",
18848               IndentWidth, 12u);
18849   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
18850                                "IndentWidth: 34",
18851                                &Style),
18852             ParseError::Unsuitable);
18853   FormatStyle BinPackedTCS = {};
18854   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
18855   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
18856                                "InsertTrailingCommas: Wrapped",
18857                                &BinPackedTCS),
18858             ParseError::BinPackTrailingCommaConflict);
18859   EXPECT_EQ(12u, Style.IndentWidth);
18860   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
18861   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
18862 
18863   Style.Language = FormatStyle::LK_JavaScript;
18864   CHECK_PARSE("Language: JavaScript\n"
18865               "IndentWidth: 12",
18866               IndentWidth, 12u);
18867   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
18868   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
18869                                "IndentWidth: 34",
18870                                &Style),
18871             ParseError::Unsuitable);
18872   EXPECT_EQ(23u, Style.IndentWidth);
18873   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
18874   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
18875 
18876   CHECK_PARSE("BasedOnStyle: LLVM\n"
18877               "IndentWidth: 67",
18878               IndentWidth, 67u);
18879 
18880   CHECK_PARSE("---\n"
18881               "Language: JavaScript\n"
18882               "IndentWidth: 12\n"
18883               "---\n"
18884               "Language: Cpp\n"
18885               "IndentWidth: 34\n"
18886               "...\n",
18887               IndentWidth, 12u);
18888 
18889   Style.Language = FormatStyle::LK_Cpp;
18890   CHECK_PARSE("---\n"
18891               "Language: JavaScript\n"
18892               "IndentWidth: 12\n"
18893               "---\n"
18894               "Language: Cpp\n"
18895               "IndentWidth: 34\n"
18896               "...\n",
18897               IndentWidth, 34u);
18898   CHECK_PARSE("---\n"
18899               "IndentWidth: 78\n"
18900               "---\n"
18901               "Language: JavaScript\n"
18902               "IndentWidth: 56\n"
18903               "...\n",
18904               IndentWidth, 78u);
18905 
18906   Style.ColumnLimit = 123;
18907   Style.IndentWidth = 234;
18908   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
18909   Style.TabWidth = 345;
18910   EXPECT_FALSE(parseConfiguration("---\n"
18911                                   "IndentWidth: 456\n"
18912                                   "BreakBeforeBraces: Allman\n"
18913                                   "---\n"
18914                                   "Language: JavaScript\n"
18915                                   "IndentWidth: 111\n"
18916                                   "TabWidth: 111\n"
18917                                   "---\n"
18918                                   "Language: Cpp\n"
18919                                   "BreakBeforeBraces: Stroustrup\n"
18920                                   "TabWidth: 789\n"
18921                                   "...\n",
18922                                   &Style));
18923   EXPECT_EQ(123u, Style.ColumnLimit);
18924   EXPECT_EQ(456u, Style.IndentWidth);
18925   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
18926   EXPECT_EQ(789u, Style.TabWidth);
18927 
18928   EXPECT_EQ(parseConfiguration("---\n"
18929                                "Language: JavaScript\n"
18930                                "IndentWidth: 56\n"
18931                                "---\n"
18932                                "IndentWidth: 78\n"
18933                                "...\n",
18934                                &Style),
18935             ParseError::Error);
18936   EXPECT_EQ(parseConfiguration("---\n"
18937                                "Language: JavaScript\n"
18938                                "IndentWidth: 56\n"
18939                                "---\n"
18940                                "Language: JavaScript\n"
18941                                "IndentWidth: 78\n"
18942                                "...\n",
18943                                &Style),
18944             ParseError::Error);
18945 
18946   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
18947 }
18948 
18949 #undef CHECK_PARSE
18950 
18951 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
18952   FormatStyle Style = {};
18953   Style.Language = FormatStyle::LK_JavaScript;
18954   Style.BreakBeforeTernaryOperators = true;
18955   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
18956   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
18957 
18958   Style.BreakBeforeTernaryOperators = true;
18959   EXPECT_EQ(0, parseConfiguration("---\n"
18960                                   "BasedOnStyle: Google\n"
18961                                   "---\n"
18962                                   "Language: JavaScript\n"
18963                                   "IndentWidth: 76\n"
18964                                   "...\n",
18965                                   &Style)
18966                    .value());
18967   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
18968   EXPECT_EQ(76u, Style.IndentWidth);
18969   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
18970 }
18971 
18972 TEST_F(FormatTest, ConfigurationRoundTripTest) {
18973   FormatStyle Style = getLLVMStyle();
18974   std::string YAML = configurationAsText(Style);
18975   FormatStyle ParsedStyle = {};
18976   ParsedStyle.Language = FormatStyle::LK_Cpp;
18977   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
18978   EXPECT_EQ(Style, ParsedStyle);
18979 }
18980 
18981 TEST_F(FormatTest, WorksFor8bitEncodings) {
18982   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
18983             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
18984             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
18985             "\"\xef\xee\xf0\xf3...\"",
18986             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
18987                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
18988                    "\xef\xee\xf0\xf3...\"",
18989                    getLLVMStyleWithColumns(12)));
18990 }
18991 
18992 TEST_F(FormatTest, HandlesUTF8BOM) {
18993   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
18994   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
18995             format("\xef\xbb\xbf#include <iostream>"));
18996   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
18997             format("\xef\xbb\xbf\n#include <iostream>"));
18998 }
18999 
19000 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
19001 #if !defined(_MSC_VER)
19002 
19003 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
19004   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
19005                getLLVMStyleWithColumns(35));
19006   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
19007                getLLVMStyleWithColumns(31));
19008   verifyFormat("// Однажды в студёную зимнюю пору...",
19009                getLLVMStyleWithColumns(36));
19010   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
19011   verifyFormat("/* Однажды в студёную зимнюю пору... */",
19012                getLLVMStyleWithColumns(39));
19013   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
19014                getLLVMStyleWithColumns(35));
19015 }
19016 
19017 TEST_F(FormatTest, SplitsUTF8Strings) {
19018   // Non-printable characters' width is currently considered to be the length in
19019   // bytes in UTF8. The characters can be displayed in very different manner
19020   // (zero-width, single width with a substitution glyph, expanded to their code
19021   // (e.g. "<8d>"), so there's no single correct way to handle them.
19022   EXPECT_EQ("\"aaaaÄ\"\n"
19023             "\"\xc2\x8d\";",
19024             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19025   EXPECT_EQ("\"aaaaaaaÄ\"\n"
19026             "\"\xc2\x8d\";",
19027             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19028   EXPECT_EQ("\"Однажды, в \"\n"
19029             "\"студёную \"\n"
19030             "\"зимнюю \"\n"
19031             "\"пору,\"",
19032             format("\"Однажды, в студёную зимнюю пору,\"",
19033                    getLLVMStyleWithColumns(13)));
19034   EXPECT_EQ(
19035       "\"一 二 三 \"\n"
19036       "\"四 五六 \"\n"
19037       "\"七 八 九 \"\n"
19038       "\"十\"",
19039       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
19040   EXPECT_EQ("\"一\t\"\n"
19041             "\"二 \t\"\n"
19042             "\"三 四 \"\n"
19043             "\"五\t\"\n"
19044             "\"六 \t\"\n"
19045             "\"七 \"\n"
19046             "\"八九十\tqq\"",
19047             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
19048                    getLLVMStyleWithColumns(11)));
19049 
19050   // UTF8 character in an escape sequence.
19051   EXPECT_EQ("\"aaaaaa\"\n"
19052             "\"\\\xC2\x8D\"",
19053             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
19054 }
19055 
19056 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
19057   EXPECT_EQ("const char *sssss =\n"
19058             "    \"一二三四五六七八\\\n"
19059             " 九 十\";",
19060             format("const char *sssss = \"一二三四五六七八\\\n"
19061                    " 九 十\";",
19062                    getLLVMStyleWithColumns(30)));
19063 }
19064 
19065 TEST_F(FormatTest, SplitsUTF8LineComments) {
19066   EXPECT_EQ("// aaaaÄ\xc2\x8d",
19067             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
19068   EXPECT_EQ("// Я из лесу\n"
19069             "// вышел; был\n"
19070             "// сильный\n"
19071             "// мороз.",
19072             format("// Я из лесу вышел; был сильный мороз.",
19073                    getLLVMStyleWithColumns(13)));
19074   EXPECT_EQ("// 一二三\n"
19075             "// 四五六七\n"
19076             "// 八  九\n"
19077             "// 十",
19078             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
19079 }
19080 
19081 TEST_F(FormatTest, SplitsUTF8BlockComments) {
19082   EXPECT_EQ("/* Гляжу,\n"
19083             " * поднимается\n"
19084             " * медленно в\n"
19085             " * гору\n"
19086             " * Лошадка,\n"
19087             " * везущая\n"
19088             " * хворосту\n"
19089             " * воз. */",
19090             format("/* Гляжу, поднимается медленно в гору\n"
19091                    " * Лошадка, везущая хворосту воз. */",
19092                    getLLVMStyleWithColumns(13)));
19093   EXPECT_EQ(
19094       "/* 一二三\n"
19095       " * 四五六七\n"
19096       " * 八  九\n"
19097       " * 十  */",
19098       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
19099   EXPECT_EQ("/* �������� ��������\n"
19100             " * ��������\n"
19101             " * ������-�� */",
19102             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
19103 }
19104 
19105 #endif // _MSC_VER
19106 
19107 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
19108   FormatStyle Style = getLLVMStyle();
19109 
19110   Style.ConstructorInitializerIndentWidth = 4;
19111   verifyFormat(
19112       "SomeClass::Constructor()\n"
19113       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19114       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19115       Style);
19116 
19117   Style.ConstructorInitializerIndentWidth = 2;
19118   verifyFormat(
19119       "SomeClass::Constructor()\n"
19120       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19121       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19122       Style);
19123 
19124   Style.ConstructorInitializerIndentWidth = 0;
19125   verifyFormat(
19126       "SomeClass::Constructor()\n"
19127       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19128       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19129       Style);
19130   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19131   verifyFormat(
19132       "SomeLongTemplateVariableName<\n"
19133       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
19134       Style);
19135   verifyFormat("bool smaller = 1 < "
19136                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
19137                "                       "
19138                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
19139                Style);
19140 
19141   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
19142   verifyFormat("SomeClass::Constructor() :\n"
19143                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
19144                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
19145                Style);
19146 }
19147 
19148 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
19149   FormatStyle Style = getLLVMStyle();
19150   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19151   Style.ConstructorInitializerIndentWidth = 4;
19152   verifyFormat("SomeClass::Constructor()\n"
19153                "    : a(a)\n"
19154                "    , b(b)\n"
19155                "    , c(c) {}",
19156                Style);
19157   verifyFormat("SomeClass::Constructor()\n"
19158                "    : a(a) {}",
19159                Style);
19160 
19161   Style.ColumnLimit = 0;
19162   verifyFormat("SomeClass::Constructor()\n"
19163                "    : a(a) {}",
19164                Style);
19165   verifyFormat("SomeClass::Constructor() noexcept\n"
19166                "    : a(a) {}",
19167                Style);
19168   verifyFormat("SomeClass::Constructor()\n"
19169                "    : a(a)\n"
19170                "    , b(b)\n"
19171                "    , c(c) {}",
19172                Style);
19173   verifyFormat("SomeClass::Constructor()\n"
19174                "    : a(a) {\n"
19175                "  foo();\n"
19176                "  bar();\n"
19177                "}",
19178                Style);
19179 
19180   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
19181   verifyFormat("SomeClass::Constructor()\n"
19182                "    : a(a)\n"
19183                "    , b(b)\n"
19184                "    , c(c) {\n}",
19185                Style);
19186   verifyFormat("SomeClass::Constructor()\n"
19187                "    : a(a) {\n}",
19188                Style);
19189 
19190   Style.ColumnLimit = 80;
19191   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
19192   Style.ConstructorInitializerIndentWidth = 2;
19193   verifyFormat("SomeClass::Constructor()\n"
19194                "  : a(a)\n"
19195                "  , b(b)\n"
19196                "  , c(c) {}",
19197                Style);
19198 
19199   Style.ConstructorInitializerIndentWidth = 0;
19200   verifyFormat("SomeClass::Constructor()\n"
19201                ": a(a)\n"
19202                ", b(b)\n"
19203                ", c(c) {}",
19204                Style);
19205 
19206   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
19207   Style.ConstructorInitializerIndentWidth = 4;
19208   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
19209   verifyFormat(
19210       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
19211       Style);
19212   verifyFormat(
19213       "SomeClass::Constructor()\n"
19214       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
19215       Style);
19216   Style.ConstructorInitializerIndentWidth = 4;
19217   Style.ColumnLimit = 60;
19218   verifyFormat("SomeClass::Constructor()\n"
19219                "    : aaaaaaaa(aaaaaaaa)\n"
19220                "    , aaaaaaaa(aaaaaaaa)\n"
19221                "    , aaaaaaaa(aaaaaaaa) {}",
19222                Style);
19223 }
19224 
19225 TEST_F(FormatTest, Destructors) {
19226   verifyFormat("void F(int &i) { i.~int(); }");
19227   verifyFormat("void F(int &i) { i->~int(); }");
19228 }
19229 
19230 TEST_F(FormatTest, FormatsWithWebKitStyle) {
19231   FormatStyle Style = getWebKitStyle();
19232 
19233   // Don't indent in outer namespaces.
19234   verifyFormat("namespace outer {\n"
19235                "int i;\n"
19236                "namespace inner {\n"
19237                "    int i;\n"
19238                "} // namespace inner\n"
19239                "} // namespace outer\n"
19240                "namespace other_outer {\n"
19241                "int i;\n"
19242                "}",
19243                Style);
19244 
19245   // Don't indent case labels.
19246   verifyFormat("switch (variable) {\n"
19247                "case 1:\n"
19248                "case 2:\n"
19249                "    doSomething();\n"
19250                "    break;\n"
19251                "default:\n"
19252                "    ++variable;\n"
19253                "}",
19254                Style);
19255 
19256   // Wrap before binary operators.
19257   EXPECT_EQ("void f()\n"
19258             "{\n"
19259             "    if (aaaaaaaaaaaaaaaa\n"
19260             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
19261             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19262             "        return;\n"
19263             "}",
19264             format("void f() {\n"
19265                    "if (aaaaaaaaaaaaaaaa\n"
19266                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
19267                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19268                    "return;\n"
19269                    "}",
19270                    Style));
19271 
19272   // Allow functions on a single line.
19273   verifyFormat("void f() { return; }", Style);
19274 
19275   // Allow empty blocks on a single line and insert a space in empty blocks.
19276   EXPECT_EQ("void f() { }", format("void f() {}", Style));
19277   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
19278   // However, don't merge non-empty short loops.
19279   EXPECT_EQ("while (true) {\n"
19280             "    continue;\n"
19281             "}",
19282             format("while (true) { continue; }", Style));
19283 
19284   // Constructor initializers are formatted one per line with the "," on the
19285   // new line.
19286   verifyFormat("Constructor()\n"
19287                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
19288                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
19289                "          aaaaaaaaaaaaaa)\n"
19290                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
19291                "{\n"
19292                "}",
19293                Style);
19294   verifyFormat("SomeClass::Constructor()\n"
19295                "    : a(a)\n"
19296                "{\n"
19297                "}",
19298                Style);
19299   EXPECT_EQ("SomeClass::Constructor()\n"
19300             "    : a(a)\n"
19301             "{\n"
19302             "}",
19303             format("SomeClass::Constructor():a(a){}", Style));
19304   verifyFormat("SomeClass::Constructor()\n"
19305                "    : a(a)\n"
19306                "    , b(b)\n"
19307                "    , c(c)\n"
19308                "{\n"
19309                "}",
19310                Style);
19311   verifyFormat("SomeClass::Constructor()\n"
19312                "    : a(a)\n"
19313                "{\n"
19314                "    foo();\n"
19315                "    bar();\n"
19316                "}",
19317                Style);
19318 
19319   // Access specifiers should be aligned left.
19320   verifyFormat("class C {\n"
19321                "public:\n"
19322                "    int i;\n"
19323                "};",
19324                Style);
19325 
19326   // Do not align comments.
19327   verifyFormat("int a; // Do not\n"
19328                "double b; // align comments.",
19329                Style);
19330 
19331   // Do not align operands.
19332   EXPECT_EQ("ASSERT(aaaa\n"
19333             "    || bbbb);",
19334             format("ASSERT ( aaaa\n||bbbb);", Style));
19335 
19336   // Accept input's line breaks.
19337   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
19338             "    || bbbbbbbbbbbbbbb) {\n"
19339             "    i++;\n"
19340             "}",
19341             format("if (aaaaaaaaaaaaaaa\n"
19342                    "|| bbbbbbbbbbbbbbb) { i++; }",
19343                    Style));
19344   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
19345             "    i++;\n"
19346             "}",
19347             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
19348 
19349   // Don't automatically break all macro definitions (llvm.org/PR17842).
19350   verifyFormat("#define aNumber 10", Style);
19351   // However, generally keep the line breaks that the user authored.
19352   EXPECT_EQ("#define aNumber \\\n"
19353             "    10",
19354             format("#define aNumber \\\n"
19355                    " 10",
19356                    Style));
19357 
19358   // Keep empty and one-element array literals on a single line.
19359   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
19360             "                                  copyItems:YES];",
19361             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
19362                    "copyItems:YES];",
19363                    Style));
19364   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
19365             "                                  copyItems:YES];",
19366             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
19367                    "             copyItems:YES];",
19368                    Style));
19369   // FIXME: This does not seem right, there should be more indentation before
19370   // the array literal's entries. Nested blocks have the same problem.
19371   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19372             "    @\"a\",\n"
19373             "    @\"a\"\n"
19374             "]\n"
19375             "                                  copyItems:YES];",
19376             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19377                    "     @\"a\",\n"
19378                    "     @\"a\"\n"
19379                    "     ]\n"
19380                    "       copyItems:YES];",
19381                    Style));
19382   EXPECT_EQ(
19383       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19384       "                                  copyItems:YES];",
19385       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19386              "   copyItems:YES];",
19387              Style));
19388 
19389   verifyFormat("[self.a b:c c:d];", Style);
19390   EXPECT_EQ("[self.a b:c\n"
19391             "        c:d];",
19392             format("[self.a b:c\n"
19393                    "c:d];",
19394                    Style));
19395 }
19396 
19397 TEST_F(FormatTest, FormatsLambdas) {
19398   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
19399   verifyFormat(
19400       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
19401   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
19402   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
19403   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
19404   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
19405   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
19406   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
19407   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
19408   verifyFormat("int x = f(*+[] {});");
19409   verifyFormat("void f() {\n"
19410                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
19411                "}\n");
19412   verifyFormat("void f() {\n"
19413                "  other(x.begin(), //\n"
19414                "        x.end(),   //\n"
19415                "        [&](int, int) { return 1; });\n"
19416                "}\n");
19417   verifyFormat("void f() {\n"
19418                "  other.other.other.other.other(\n"
19419                "      x.begin(), x.end(),\n"
19420                "      [something, rather](int, int, int, int, int, int, int) { "
19421                "return 1; });\n"
19422                "}\n");
19423   verifyFormat(
19424       "void f() {\n"
19425       "  other.other.other.other.other(\n"
19426       "      x.begin(), x.end(),\n"
19427       "      [something, rather](int, int, int, int, int, int, int) {\n"
19428       "        //\n"
19429       "      });\n"
19430       "}\n");
19431   verifyFormat("SomeFunction([]() { // A cool function...\n"
19432                "  return 43;\n"
19433                "});");
19434   EXPECT_EQ("SomeFunction([]() {\n"
19435             "#define A a\n"
19436             "  return 43;\n"
19437             "});",
19438             format("SomeFunction([](){\n"
19439                    "#define A a\n"
19440                    "return 43;\n"
19441                    "});"));
19442   verifyFormat("void f() {\n"
19443                "  SomeFunction([](decltype(x), A *a) {});\n"
19444                "  SomeFunction([](typeof(x), A *a) {});\n"
19445                "  SomeFunction([](_Atomic(x), A *a) {});\n"
19446                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
19447                "}");
19448   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19449                "    [](const aaaaaaaaaa &a) { return a; });");
19450   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
19451                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
19452                "});");
19453   verifyFormat("Constructor()\n"
19454                "    : Field([] { // comment\n"
19455                "        int i;\n"
19456                "      }) {}");
19457   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
19458                "  return some_parameter.size();\n"
19459                "};");
19460   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
19461                "    [](const string &s) { return s; };");
19462   verifyFormat("int i = aaaaaa ? 1 //\n"
19463                "               : [] {\n"
19464                "                   return 2; //\n"
19465                "                 }();");
19466   verifyFormat("llvm::errs() << \"number of twos is \"\n"
19467                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
19468                "                  return x == 2; // force break\n"
19469                "                });");
19470   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19471                "    [=](int iiiiiiiiiiii) {\n"
19472                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
19473                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
19474                "    });",
19475                getLLVMStyleWithColumns(60));
19476 
19477   verifyFormat("SomeFunction({[&] {\n"
19478                "                // comment\n"
19479                "              },\n"
19480                "              [&] {\n"
19481                "                // comment\n"
19482                "              }});");
19483   verifyFormat("SomeFunction({[&] {\n"
19484                "  // comment\n"
19485                "}});");
19486   verifyFormat(
19487       "virtual aaaaaaaaaaaaaaaa(\n"
19488       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
19489       "    aaaaa aaaaaaaaa);");
19490 
19491   // Lambdas with return types.
19492   verifyFormat("int c = []() -> int { return 2; }();\n");
19493   verifyFormat("int c = []() -> int * { return 2; }();\n");
19494   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
19495   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
19496   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
19497   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
19498   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
19499   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
19500   verifyFormat("[a, a]() -> a<1> {};");
19501   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
19502   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
19503   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
19504   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
19505   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
19506   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
19507   verifyFormat("[]() -> foo<!5> { return {}; };");
19508   verifyFormat("[]() -> foo<~5> { return {}; };");
19509   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
19510   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
19511   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
19512   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
19513   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
19514   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
19515   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
19516   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
19517   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
19518   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
19519   verifyFormat("namespace bar {\n"
19520                "// broken:\n"
19521                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
19522                "} // namespace bar");
19523   verifyFormat("namespace bar {\n"
19524                "// broken:\n"
19525                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
19526                "} // namespace bar");
19527   verifyFormat("namespace bar {\n"
19528                "// broken:\n"
19529                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
19530                "} // namespace bar");
19531   verifyFormat("namespace bar {\n"
19532                "// broken:\n"
19533                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
19534                "} // namespace bar");
19535   verifyFormat("namespace bar {\n"
19536                "// broken:\n"
19537                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
19538                "} // namespace bar");
19539   verifyFormat("namespace bar {\n"
19540                "// broken:\n"
19541                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
19542                "} // namespace bar");
19543   verifyFormat("namespace bar {\n"
19544                "// broken:\n"
19545                "auto foo{[]() -> foo<!5> { return {}; }};\n"
19546                "} // namespace bar");
19547   verifyFormat("namespace bar {\n"
19548                "// broken:\n"
19549                "auto foo{[]() -> foo<~5> { return {}; }};\n"
19550                "} // namespace bar");
19551   verifyFormat("namespace bar {\n"
19552                "// broken:\n"
19553                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
19554                "} // namespace bar");
19555   verifyFormat("namespace bar {\n"
19556                "// broken:\n"
19557                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
19558                "} // namespace bar");
19559   verifyFormat("namespace bar {\n"
19560                "// broken:\n"
19561                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
19562                "} // namespace bar");
19563   verifyFormat("namespace bar {\n"
19564                "// broken:\n"
19565                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
19566                "} // namespace bar");
19567   verifyFormat("namespace bar {\n"
19568                "// broken:\n"
19569                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
19570                "} // namespace bar");
19571   verifyFormat("namespace bar {\n"
19572                "// broken:\n"
19573                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
19574                "} // namespace bar");
19575   verifyFormat("namespace bar {\n"
19576                "// broken:\n"
19577                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
19578                "} // namespace bar");
19579   verifyFormat("namespace bar {\n"
19580                "// broken:\n"
19581                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
19582                "} // namespace bar");
19583   verifyFormat("namespace bar {\n"
19584                "// broken:\n"
19585                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
19586                "} // namespace bar");
19587   verifyFormat("namespace bar {\n"
19588                "// broken:\n"
19589                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
19590                "} // namespace bar");
19591   verifyFormat("[]() -> a<1> {};");
19592   verifyFormat("[]() -> a<1> { ; };");
19593   verifyFormat("[]() -> a<1> { ; }();");
19594   verifyFormat("[a, a]() -> a<true> {};");
19595   verifyFormat("[]() -> a<true> {};");
19596   verifyFormat("[]() -> a<true> { ; };");
19597   verifyFormat("[]() -> a<true> { ; }();");
19598   verifyFormat("[a, a]() -> a<false> {};");
19599   verifyFormat("[]() -> a<false> {};");
19600   verifyFormat("[]() -> a<false> { ; };");
19601   verifyFormat("[]() -> a<false> { ; }();");
19602   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
19603   verifyFormat("namespace bar {\n"
19604                "auto foo{[]() -> foo<false> { ; }};\n"
19605                "} // namespace bar");
19606   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
19607                "                   int j) -> int {\n"
19608                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
19609                "};");
19610   verifyFormat(
19611       "aaaaaaaaaaaaaaaaaaaaaa(\n"
19612       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
19613       "      return aaaaaaaaaaaaaaaaa;\n"
19614       "    });",
19615       getLLVMStyleWithColumns(70));
19616   verifyFormat("[]() //\n"
19617                "    -> int {\n"
19618                "  return 1; //\n"
19619                "};");
19620   verifyFormat("[]() -> Void<T...> {};");
19621   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
19622 
19623   // Lambdas with explicit template argument lists.
19624   verifyFormat(
19625       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
19626 
19627   // Multiple lambdas in the same parentheses change indentation rules. These
19628   // lambdas are forced to start on new lines.
19629   verifyFormat("SomeFunction(\n"
19630                "    []() {\n"
19631                "      //\n"
19632                "    },\n"
19633                "    []() {\n"
19634                "      //\n"
19635                "    });");
19636 
19637   // A lambda passed as arg0 is always pushed to the next line.
19638   verifyFormat("SomeFunction(\n"
19639                "    [this] {\n"
19640                "      //\n"
19641                "    },\n"
19642                "    1);\n");
19643 
19644   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
19645   // the arg0 case above.
19646   auto Style = getGoogleStyle();
19647   Style.BinPackArguments = false;
19648   verifyFormat("SomeFunction(\n"
19649                "    a,\n"
19650                "    [this] {\n"
19651                "      //\n"
19652                "    },\n"
19653                "    b);\n",
19654                Style);
19655   verifyFormat("SomeFunction(\n"
19656                "    a,\n"
19657                "    [this] {\n"
19658                "      //\n"
19659                "    },\n"
19660                "    b);\n");
19661 
19662   // A lambda with a very long line forces arg0 to be pushed out irrespective of
19663   // the BinPackArguments value (as long as the code is wide enough).
19664   verifyFormat(
19665       "something->SomeFunction(\n"
19666       "    a,\n"
19667       "    [this] {\n"
19668       "      "
19669       "D0000000000000000000000000000000000000000000000000000000000001();\n"
19670       "    },\n"
19671       "    b);\n");
19672 
19673   // A multi-line lambda is pulled up as long as the introducer fits on the
19674   // previous line and there are no further args.
19675   verifyFormat("function(1, [this, that] {\n"
19676                "  //\n"
19677                "});\n");
19678   verifyFormat("function([this, that] {\n"
19679                "  //\n"
19680                "});\n");
19681   // FIXME: this format is not ideal and we should consider forcing the first
19682   // arg onto its own line.
19683   verifyFormat("function(a, b, c, //\n"
19684                "         d, [this, that] {\n"
19685                "           //\n"
19686                "         });\n");
19687 
19688   // Multiple lambdas are treated correctly even when there is a short arg0.
19689   verifyFormat("SomeFunction(\n"
19690                "    1,\n"
19691                "    [this] {\n"
19692                "      //\n"
19693                "    },\n"
19694                "    [this] {\n"
19695                "      //\n"
19696                "    },\n"
19697                "    1);\n");
19698 
19699   // More complex introducers.
19700   verifyFormat("return [i, args...] {};");
19701 
19702   // Not lambdas.
19703   verifyFormat("constexpr char hello[]{\"hello\"};");
19704   verifyFormat("double &operator[](int i) { return 0; }\n"
19705                "int i;");
19706   verifyFormat("std::unique_ptr<int[]> foo() {}");
19707   verifyFormat("int i = a[a][a]->f();");
19708   verifyFormat("int i = (*b)[a]->f();");
19709 
19710   // Other corner cases.
19711   verifyFormat("void f() {\n"
19712                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
19713                "  );\n"
19714                "}");
19715 
19716   // Lambdas created through weird macros.
19717   verifyFormat("void f() {\n"
19718                "  MACRO((const AA &a) { return 1; });\n"
19719                "  MACRO((AA &a) { return 1; });\n"
19720                "}");
19721 
19722   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
19723                "      doo_dah();\n"
19724                "      doo_dah();\n"
19725                "    })) {\n"
19726                "}");
19727   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
19728                "                doo_dah();\n"
19729                "                doo_dah();\n"
19730                "              })) {\n"
19731                "}");
19732   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
19733                "                doo_dah();\n"
19734                "                doo_dah();\n"
19735                "              })) {\n"
19736                "}");
19737   verifyFormat("auto lambda = []() {\n"
19738                "  int a = 2\n"
19739                "#if A\n"
19740                "          + 2\n"
19741                "#endif\n"
19742                "      ;\n"
19743                "};");
19744 
19745   // Lambdas with complex multiline introducers.
19746   verifyFormat(
19747       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19748       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
19749       "        -> ::std::unordered_set<\n"
19750       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
19751       "      //\n"
19752       "    });");
19753 
19754   FormatStyle DoNotMerge = getLLVMStyle();
19755   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
19756   verifyFormat("auto c = []() {\n"
19757                "  return b;\n"
19758                "};",
19759                "auto c = []() { return b; };", DoNotMerge);
19760   verifyFormat("auto c = []() {\n"
19761                "};",
19762                " auto c = []() {};", DoNotMerge);
19763 
19764   FormatStyle MergeEmptyOnly = getLLVMStyle();
19765   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
19766   verifyFormat("auto c = []() {\n"
19767                "  return b;\n"
19768                "};",
19769                "auto c = []() {\n"
19770                "  return b;\n"
19771                " };",
19772                MergeEmptyOnly);
19773   verifyFormat("auto c = []() {};",
19774                "auto c = []() {\n"
19775                "};",
19776                MergeEmptyOnly);
19777 
19778   FormatStyle MergeInline = getLLVMStyle();
19779   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
19780   verifyFormat("auto c = []() {\n"
19781                "  return b;\n"
19782                "};",
19783                "auto c = []() { return b; };", MergeInline);
19784   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
19785                MergeInline);
19786   verifyFormat("function([]() { return b; }, a)",
19787                "function([]() { return b; }, a)", MergeInline);
19788   verifyFormat("function(a, []() { return b; })",
19789                "function(a, []() { return b; })", MergeInline);
19790 
19791   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
19792   // AllowShortLambdasOnASingleLine
19793   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
19794   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
19795   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
19796   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19797       FormatStyle::ShortLambdaStyle::SLS_None;
19798   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
19799                "    []()\n"
19800                "    {\n"
19801                "      return 17;\n"
19802                "    });",
19803                LLVMWithBeforeLambdaBody);
19804   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
19805                "    []()\n"
19806                "    {\n"
19807                "    });",
19808                LLVMWithBeforeLambdaBody);
19809   verifyFormat("auto fct_SLS_None = []()\n"
19810                "{\n"
19811                "  return 17;\n"
19812                "};",
19813                LLVMWithBeforeLambdaBody);
19814   verifyFormat("TwoNestedLambdas_SLS_None(\n"
19815                "    []()\n"
19816                "    {\n"
19817                "      return Call(\n"
19818                "          []()\n"
19819                "          {\n"
19820                "            return 17;\n"
19821                "          });\n"
19822                "    });",
19823                LLVMWithBeforeLambdaBody);
19824   verifyFormat("void Fct() {\n"
19825                "  return {[]()\n"
19826                "          {\n"
19827                "            return 17;\n"
19828                "          }};\n"
19829                "}",
19830                LLVMWithBeforeLambdaBody);
19831 
19832   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19833       FormatStyle::ShortLambdaStyle::SLS_Empty;
19834   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
19835                "    []()\n"
19836                "    {\n"
19837                "      return 17;\n"
19838                "    });",
19839                LLVMWithBeforeLambdaBody);
19840   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
19841                LLVMWithBeforeLambdaBody);
19842   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
19843                "ongFunctionName_SLS_Empty(\n"
19844                "    []() {});",
19845                LLVMWithBeforeLambdaBody);
19846   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
19847                "                                []()\n"
19848                "                                {\n"
19849                "                                  return 17;\n"
19850                "                                });",
19851                LLVMWithBeforeLambdaBody);
19852   verifyFormat("auto fct_SLS_Empty = []()\n"
19853                "{\n"
19854                "  return 17;\n"
19855                "};",
19856                LLVMWithBeforeLambdaBody);
19857   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
19858                "    []()\n"
19859                "    {\n"
19860                "      return Call([]() {});\n"
19861                "    });",
19862                LLVMWithBeforeLambdaBody);
19863   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
19864                "                           []()\n"
19865                "                           {\n"
19866                "                             return Call([]() {});\n"
19867                "                           });",
19868                LLVMWithBeforeLambdaBody);
19869   verifyFormat(
19870       "FctWithLongLineInLambda_SLS_Empty(\n"
19871       "    []()\n"
19872       "    {\n"
19873       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19874       "                               AndShouldNotBeConsiderAsInline,\n"
19875       "                               LambdaBodyMustBeBreak);\n"
19876       "    });",
19877       LLVMWithBeforeLambdaBody);
19878 
19879   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19880       FormatStyle::ShortLambdaStyle::SLS_Inline;
19881   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
19882                LLVMWithBeforeLambdaBody);
19883   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
19884                LLVMWithBeforeLambdaBody);
19885   verifyFormat("auto fct_SLS_Inline = []()\n"
19886                "{\n"
19887                "  return 17;\n"
19888                "};",
19889                LLVMWithBeforeLambdaBody);
19890   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
19891                "17; }); });",
19892                LLVMWithBeforeLambdaBody);
19893   verifyFormat(
19894       "FctWithLongLineInLambda_SLS_Inline(\n"
19895       "    []()\n"
19896       "    {\n"
19897       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19898       "                               AndShouldNotBeConsiderAsInline,\n"
19899       "                               LambdaBodyMustBeBreak);\n"
19900       "    });",
19901       LLVMWithBeforeLambdaBody);
19902   verifyFormat("FctWithMultipleParams_SLS_Inline("
19903                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
19904                "                                 []() { return 17; });",
19905                LLVMWithBeforeLambdaBody);
19906   verifyFormat(
19907       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
19908       LLVMWithBeforeLambdaBody);
19909 
19910   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19911       FormatStyle::ShortLambdaStyle::SLS_All;
19912   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
19913                LLVMWithBeforeLambdaBody);
19914   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
19915                LLVMWithBeforeLambdaBody);
19916   verifyFormat("auto fct_SLS_All = []() { return 17; };",
19917                LLVMWithBeforeLambdaBody);
19918   verifyFormat("FctWithOneParam_SLS_All(\n"
19919                "    []()\n"
19920                "    {\n"
19921                "      // A cool function...\n"
19922                "      return 43;\n"
19923                "    });",
19924                LLVMWithBeforeLambdaBody);
19925   verifyFormat("FctWithMultipleParams_SLS_All("
19926                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
19927                "                              []() { return 17; });",
19928                LLVMWithBeforeLambdaBody);
19929   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
19930                LLVMWithBeforeLambdaBody);
19931   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
19932                LLVMWithBeforeLambdaBody);
19933   verifyFormat(
19934       "FctWithLongLineInLambda_SLS_All(\n"
19935       "    []()\n"
19936       "    {\n"
19937       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19938       "                               AndShouldNotBeConsiderAsInline,\n"
19939       "                               LambdaBodyMustBeBreak);\n"
19940       "    });",
19941       LLVMWithBeforeLambdaBody);
19942   verifyFormat(
19943       "auto fct_SLS_All = []()\n"
19944       "{\n"
19945       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19946       "                           AndShouldNotBeConsiderAsInline,\n"
19947       "                           LambdaBodyMustBeBreak);\n"
19948       "};",
19949       LLVMWithBeforeLambdaBody);
19950   LLVMWithBeforeLambdaBody.BinPackParameters = false;
19951   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
19952                LLVMWithBeforeLambdaBody);
19953   verifyFormat(
19954       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
19955       "                                FirstParam,\n"
19956       "                                SecondParam,\n"
19957       "                                ThirdParam,\n"
19958       "                                FourthParam);",
19959       LLVMWithBeforeLambdaBody);
19960   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19961                "    []() { return "
19962                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
19963                "    FirstParam,\n"
19964                "    SecondParam,\n"
19965                "    ThirdParam,\n"
19966                "    FourthParam);",
19967                LLVMWithBeforeLambdaBody);
19968   verifyFormat(
19969       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
19970       "                                SecondParam,\n"
19971       "                                ThirdParam,\n"
19972       "                                FourthParam,\n"
19973       "                                []() { return SomeValueNotSoLong; });",
19974       LLVMWithBeforeLambdaBody);
19975   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19976                "    []()\n"
19977                "    {\n"
19978                "      return "
19979                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
19980                "eConsiderAsInline;\n"
19981                "    });",
19982                LLVMWithBeforeLambdaBody);
19983   verifyFormat(
19984       "FctWithLongLineInLambda_SLS_All(\n"
19985       "    []()\n"
19986       "    {\n"
19987       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19988       "                               AndShouldNotBeConsiderAsInline,\n"
19989       "                               LambdaBodyMustBeBreak);\n"
19990       "    });",
19991       LLVMWithBeforeLambdaBody);
19992   verifyFormat("FctWithTwoParams_SLS_All(\n"
19993                "    []()\n"
19994                "    {\n"
19995                "      // A cool function...\n"
19996                "      return 43;\n"
19997                "    },\n"
19998                "    87);",
19999                LLVMWithBeforeLambdaBody);
20000   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
20001                LLVMWithBeforeLambdaBody);
20002   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
20003                LLVMWithBeforeLambdaBody);
20004   verifyFormat(
20005       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
20006       LLVMWithBeforeLambdaBody);
20007   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
20008                "}); }, x);",
20009                LLVMWithBeforeLambdaBody);
20010   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20011                "    []()\n"
20012                "    {\n"
20013                "      // A cool function...\n"
20014                "      return Call([]() { return 17; });\n"
20015                "    });",
20016                LLVMWithBeforeLambdaBody);
20017   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20018                "    []()\n"
20019                "    {\n"
20020                "      return Call(\n"
20021                "          []()\n"
20022                "          {\n"
20023                "            // A cool function...\n"
20024                "            return 17;\n"
20025                "          });\n"
20026                "    });",
20027                LLVMWithBeforeLambdaBody);
20028 
20029   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20030       FormatStyle::ShortLambdaStyle::SLS_None;
20031 
20032   verifyFormat("auto select = [this]() -> const Library::Object *\n"
20033                "{\n"
20034                "  return MyAssignment::SelectFromList(this);\n"
20035                "};\n",
20036                LLVMWithBeforeLambdaBody);
20037 
20038   verifyFormat("auto select = [this]() -> const Library::Object &\n"
20039                "{\n"
20040                "  return MyAssignment::SelectFromList(this);\n"
20041                "};\n",
20042                LLVMWithBeforeLambdaBody);
20043 
20044   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
20045                "{\n"
20046                "  return MyAssignment::SelectFromList(this);\n"
20047                "};\n",
20048                LLVMWithBeforeLambdaBody);
20049 
20050   verifyFormat("namespace test {\n"
20051                "class Test {\n"
20052                "public:\n"
20053                "  Test() = default;\n"
20054                "};\n"
20055                "} // namespace test",
20056                LLVMWithBeforeLambdaBody);
20057 
20058   // Lambdas with different indentation styles.
20059   Style = getLLVMStyleWithColumns(100);
20060   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20061             "  return promise.then(\n"
20062             "      [this, &someVariable, someObject = "
20063             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20064             "        return someObject.startAsyncAction().then(\n"
20065             "            [this, &someVariable](AsyncActionResult result) "
20066             "mutable { result.processMore(); });\n"
20067             "      });\n"
20068             "}\n",
20069             format("SomeResult doSomething(SomeObject promise) {\n"
20070                    "  return promise.then([this, &someVariable, someObject = "
20071                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20072                    "    return someObject.startAsyncAction().then([this, "
20073                    "&someVariable](AsyncActionResult result) mutable {\n"
20074                    "      result.processMore();\n"
20075                    "    });\n"
20076                    "  });\n"
20077                    "}\n",
20078                    Style));
20079   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20080   verifyFormat("test() {\n"
20081                "  ([]() -> {\n"
20082                "    int b = 32;\n"
20083                "    return 3;\n"
20084                "  }).foo();\n"
20085                "}",
20086                Style);
20087   verifyFormat("test() {\n"
20088                "  []() -> {\n"
20089                "    int b = 32;\n"
20090                "    return 3;\n"
20091                "  }\n"
20092                "}",
20093                Style);
20094   verifyFormat("std::sort(v.begin(), v.end(),\n"
20095                "          [](const auto &someLongArgumentName, const auto "
20096                "&someOtherLongArgumentName) {\n"
20097                "  return someLongArgumentName.someMemberVariable < "
20098                "someOtherLongArgumentName.someMemberVariable;\n"
20099                "});",
20100                Style);
20101   verifyFormat("test() {\n"
20102                "  (\n"
20103                "      []() -> {\n"
20104                "        int b = 32;\n"
20105                "        return 3;\n"
20106                "      },\n"
20107                "      foo, bar)\n"
20108                "      .foo();\n"
20109                "}",
20110                Style);
20111   verifyFormat("test() {\n"
20112                "  ([]() -> {\n"
20113                "    int b = 32;\n"
20114                "    return 3;\n"
20115                "  })\n"
20116                "      .foo()\n"
20117                "      .bar();\n"
20118                "}",
20119                Style);
20120   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20121             "  return promise.then(\n"
20122             "      [this, &someVariable, someObject = "
20123             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20124             "    return someObject.startAsyncAction().then(\n"
20125             "        [this, &someVariable](AsyncActionResult result) mutable { "
20126             "result.processMore(); });\n"
20127             "  });\n"
20128             "}\n",
20129             format("SomeResult doSomething(SomeObject promise) {\n"
20130                    "  return promise.then([this, &someVariable, someObject = "
20131                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20132                    "    return someObject.startAsyncAction().then([this, "
20133                    "&someVariable](AsyncActionResult result) mutable {\n"
20134                    "      result.processMore();\n"
20135                    "    });\n"
20136                    "  });\n"
20137                    "}\n",
20138                    Style));
20139   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20140             "  return promise.then([this, &someVariable] {\n"
20141             "    return someObject.startAsyncAction().then(\n"
20142             "        [this, &someVariable](AsyncActionResult result) mutable { "
20143             "result.processMore(); });\n"
20144             "  });\n"
20145             "}\n",
20146             format("SomeResult doSomething(SomeObject promise) {\n"
20147                    "  return promise.then([this, &someVariable] {\n"
20148                    "    return someObject.startAsyncAction().then([this, "
20149                    "&someVariable](AsyncActionResult result) mutable {\n"
20150                    "      result.processMore();\n"
20151                    "    });\n"
20152                    "  });\n"
20153                    "}\n",
20154                    Style));
20155   Style = getGoogleStyle();
20156   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20157   EXPECT_EQ("#define A                                       \\\n"
20158             "  [] {                                          \\\n"
20159             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
20160             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
20161             "      }",
20162             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
20163                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
20164                    Style));
20165   // TODO: The current formatting has a minor issue that's not worth fixing
20166   // right now whereby the closing brace is indented relative to the signature
20167   // instead of being aligned. This only happens with macros.
20168 }
20169 
20170 TEST_F(FormatTest, LambdaWithLineComments) {
20171   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20172   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20173   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20174   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20175       FormatStyle::ShortLambdaStyle::SLS_All;
20176 
20177   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
20178   verifyFormat("auto k = []() // comment\n"
20179                "{ return; }",
20180                LLVMWithBeforeLambdaBody);
20181   verifyFormat("auto k = []() /* comment */ { return; }",
20182                LLVMWithBeforeLambdaBody);
20183   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
20184                LLVMWithBeforeLambdaBody);
20185   verifyFormat("auto k = []() // X\n"
20186                "{ return; }",
20187                LLVMWithBeforeLambdaBody);
20188   verifyFormat(
20189       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
20190       "{ return; }",
20191       LLVMWithBeforeLambdaBody);
20192 }
20193 
20194 TEST_F(FormatTest, EmptyLinesInLambdas) {
20195   verifyFormat("auto lambda = []() {\n"
20196                "  x(); //\n"
20197                "};",
20198                "auto lambda = []() {\n"
20199                "\n"
20200                "  x(); //\n"
20201                "\n"
20202                "};");
20203 }
20204 
20205 TEST_F(FormatTest, FormatsBlocks) {
20206   FormatStyle ShortBlocks = getLLVMStyle();
20207   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20208   verifyFormat("int (^Block)(int, int);", ShortBlocks);
20209   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
20210   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
20211   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
20212   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
20213   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
20214 
20215   verifyFormat("foo(^{ bar(); });", ShortBlocks);
20216   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
20217   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
20218 
20219   verifyFormat("[operation setCompletionBlock:^{\n"
20220                "  [self onOperationDone];\n"
20221                "}];");
20222   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
20223                "  [self onOperationDone];\n"
20224                "}]};");
20225   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
20226                "  f();\n"
20227                "}];");
20228   verifyFormat("int a = [operation block:^int(int *i) {\n"
20229                "  return 1;\n"
20230                "}];");
20231   verifyFormat("[myObject doSomethingWith:arg1\n"
20232                "                      aaa:^int(int *a) {\n"
20233                "                        return 1;\n"
20234                "                      }\n"
20235                "                      bbb:f(a * bbbbbbbb)];");
20236 
20237   verifyFormat("[operation setCompletionBlock:^{\n"
20238                "  [self.delegate newDataAvailable];\n"
20239                "}];",
20240                getLLVMStyleWithColumns(60));
20241   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
20242                "  NSString *path = [self sessionFilePath];\n"
20243                "  if (path) {\n"
20244                "    // ...\n"
20245                "  }\n"
20246                "});");
20247   verifyFormat("[[SessionService sharedService]\n"
20248                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20249                "      if (window) {\n"
20250                "        [self windowDidLoad:window];\n"
20251                "      } else {\n"
20252                "        [self errorLoadingWindow];\n"
20253                "      }\n"
20254                "    }];");
20255   verifyFormat("void (^largeBlock)(void) = ^{\n"
20256                "  // ...\n"
20257                "};\n",
20258                getLLVMStyleWithColumns(40));
20259   verifyFormat("[[SessionService sharedService]\n"
20260                "    loadWindowWithCompletionBlock: //\n"
20261                "        ^(SessionWindow *window) {\n"
20262                "          if (window) {\n"
20263                "            [self windowDidLoad:window];\n"
20264                "          } else {\n"
20265                "            [self errorLoadingWindow];\n"
20266                "          }\n"
20267                "        }];",
20268                getLLVMStyleWithColumns(60));
20269   verifyFormat("[myObject doSomethingWith:arg1\n"
20270                "    firstBlock:^(Foo *a) {\n"
20271                "      // ...\n"
20272                "      int i;\n"
20273                "    }\n"
20274                "    secondBlock:^(Bar *b) {\n"
20275                "      // ...\n"
20276                "      int i;\n"
20277                "    }\n"
20278                "    thirdBlock:^Foo(Bar *b) {\n"
20279                "      // ...\n"
20280                "      int i;\n"
20281                "    }];");
20282   verifyFormat("[myObject doSomethingWith:arg1\n"
20283                "               firstBlock:-1\n"
20284                "              secondBlock:^(Bar *b) {\n"
20285                "                // ...\n"
20286                "                int i;\n"
20287                "              }];");
20288 
20289   verifyFormat("f(^{\n"
20290                "  @autoreleasepool {\n"
20291                "    if (a) {\n"
20292                "      g();\n"
20293                "    }\n"
20294                "  }\n"
20295                "});");
20296   verifyFormat("Block b = ^int *(A *a, B *b) {}");
20297   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
20298                "};");
20299 
20300   FormatStyle FourIndent = getLLVMStyle();
20301   FourIndent.ObjCBlockIndentWidth = 4;
20302   verifyFormat("[operation setCompletionBlock:^{\n"
20303                "    [self onOperationDone];\n"
20304                "}];",
20305                FourIndent);
20306 }
20307 
20308 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
20309   FormatStyle ZeroColumn = getLLVMStyle();
20310   ZeroColumn.ColumnLimit = 0;
20311 
20312   verifyFormat("[[SessionService sharedService] "
20313                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20314                "  if (window) {\n"
20315                "    [self windowDidLoad:window];\n"
20316                "  } else {\n"
20317                "    [self errorLoadingWindow];\n"
20318                "  }\n"
20319                "}];",
20320                ZeroColumn);
20321   EXPECT_EQ("[[SessionService sharedService]\n"
20322             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20323             "      if (window) {\n"
20324             "        [self windowDidLoad:window];\n"
20325             "      } else {\n"
20326             "        [self errorLoadingWindow];\n"
20327             "      }\n"
20328             "    }];",
20329             format("[[SessionService sharedService]\n"
20330                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20331                    "                if (window) {\n"
20332                    "    [self windowDidLoad:window];\n"
20333                    "  } else {\n"
20334                    "    [self errorLoadingWindow];\n"
20335                    "  }\n"
20336                    "}];",
20337                    ZeroColumn));
20338   verifyFormat("[myObject doSomethingWith:arg1\n"
20339                "    firstBlock:^(Foo *a) {\n"
20340                "      // ...\n"
20341                "      int i;\n"
20342                "    }\n"
20343                "    secondBlock:^(Bar *b) {\n"
20344                "      // ...\n"
20345                "      int i;\n"
20346                "    }\n"
20347                "    thirdBlock:^Foo(Bar *b) {\n"
20348                "      // ...\n"
20349                "      int i;\n"
20350                "    }];",
20351                ZeroColumn);
20352   verifyFormat("f(^{\n"
20353                "  @autoreleasepool {\n"
20354                "    if (a) {\n"
20355                "      g();\n"
20356                "    }\n"
20357                "  }\n"
20358                "});",
20359                ZeroColumn);
20360   verifyFormat("void (^largeBlock)(void) = ^{\n"
20361                "  // ...\n"
20362                "};",
20363                ZeroColumn);
20364 
20365   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20366   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
20367             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20368   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
20369   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
20370             "  int i;\n"
20371             "};",
20372             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20373 }
20374 
20375 TEST_F(FormatTest, SupportsCRLF) {
20376   EXPECT_EQ("int a;\r\n"
20377             "int b;\r\n"
20378             "int c;\r\n",
20379             format("int a;\r\n"
20380                    "  int b;\r\n"
20381                    "    int c;\r\n",
20382                    getLLVMStyle()));
20383   EXPECT_EQ("int a;\r\n"
20384             "int b;\r\n"
20385             "int c;\r\n",
20386             format("int a;\r\n"
20387                    "  int b;\n"
20388                    "    int c;\r\n",
20389                    getLLVMStyle()));
20390   EXPECT_EQ("int a;\n"
20391             "int b;\n"
20392             "int c;\n",
20393             format("int a;\r\n"
20394                    "  int b;\n"
20395                    "    int c;\n",
20396                    getLLVMStyle()));
20397   EXPECT_EQ("\"aaaaaaa \"\r\n"
20398             "\"bbbbbbb\";\r\n",
20399             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
20400   EXPECT_EQ("#define A \\\r\n"
20401             "  b;      \\\r\n"
20402             "  c;      \\\r\n"
20403             "  d;\r\n",
20404             format("#define A \\\r\n"
20405                    "  b; \\\r\n"
20406                    "  c; d; \r\n",
20407                    getGoogleStyle()));
20408 
20409   EXPECT_EQ("/*\r\n"
20410             "multi line block comments\r\n"
20411             "should not introduce\r\n"
20412             "an extra carriage return\r\n"
20413             "*/\r\n",
20414             format("/*\r\n"
20415                    "multi line block comments\r\n"
20416                    "should not introduce\r\n"
20417                    "an extra carriage return\r\n"
20418                    "*/\r\n"));
20419   EXPECT_EQ("/*\r\n"
20420             "\r\n"
20421             "*/",
20422             format("/*\r\n"
20423                    "    \r\r\r\n"
20424                    "*/"));
20425 
20426   FormatStyle style = getLLVMStyle();
20427 
20428   style.DeriveLineEnding = true;
20429   style.UseCRLF = false;
20430   EXPECT_EQ("union FooBarBazQux {\n"
20431             "  int foo;\n"
20432             "  int bar;\n"
20433             "  int baz;\n"
20434             "};",
20435             format("union FooBarBazQux {\r\n"
20436                    "  int foo;\n"
20437                    "  int bar;\r\n"
20438                    "  int baz;\n"
20439                    "};",
20440                    style));
20441   style.UseCRLF = true;
20442   EXPECT_EQ("union FooBarBazQux {\r\n"
20443             "  int foo;\r\n"
20444             "  int bar;\r\n"
20445             "  int baz;\r\n"
20446             "};",
20447             format("union FooBarBazQux {\r\n"
20448                    "  int foo;\n"
20449                    "  int bar;\r\n"
20450                    "  int baz;\n"
20451                    "};",
20452                    style));
20453 
20454   style.DeriveLineEnding = false;
20455   style.UseCRLF = false;
20456   EXPECT_EQ("union FooBarBazQux {\n"
20457             "  int foo;\n"
20458             "  int bar;\n"
20459             "  int baz;\n"
20460             "  int qux;\n"
20461             "};",
20462             format("union FooBarBazQux {\r\n"
20463                    "  int foo;\n"
20464                    "  int bar;\r\n"
20465                    "  int baz;\n"
20466                    "  int qux;\r\n"
20467                    "};",
20468                    style));
20469   style.UseCRLF = true;
20470   EXPECT_EQ("union FooBarBazQux {\r\n"
20471             "  int foo;\r\n"
20472             "  int bar;\r\n"
20473             "  int baz;\r\n"
20474             "  int qux;\r\n"
20475             "};",
20476             format("union FooBarBazQux {\r\n"
20477                    "  int foo;\n"
20478                    "  int bar;\r\n"
20479                    "  int baz;\n"
20480                    "  int qux;\n"
20481                    "};",
20482                    style));
20483 
20484   style.DeriveLineEnding = true;
20485   style.UseCRLF = false;
20486   EXPECT_EQ("union FooBarBazQux {\r\n"
20487             "  int foo;\r\n"
20488             "  int bar;\r\n"
20489             "  int baz;\r\n"
20490             "  int qux;\r\n"
20491             "};",
20492             format("union FooBarBazQux {\r\n"
20493                    "  int foo;\n"
20494                    "  int bar;\r\n"
20495                    "  int baz;\n"
20496                    "  int qux;\r\n"
20497                    "};",
20498                    style));
20499   style.UseCRLF = true;
20500   EXPECT_EQ("union FooBarBazQux {\n"
20501             "  int foo;\n"
20502             "  int bar;\n"
20503             "  int baz;\n"
20504             "  int qux;\n"
20505             "};",
20506             format("union FooBarBazQux {\r\n"
20507                    "  int foo;\n"
20508                    "  int bar;\r\n"
20509                    "  int baz;\n"
20510                    "  int qux;\n"
20511                    "};",
20512                    style));
20513 }
20514 
20515 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
20516   verifyFormat("MY_CLASS(C) {\n"
20517                "  int i;\n"
20518                "  int j;\n"
20519                "};");
20520 }
20521 
20522 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
20523   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
20524   TwoIndent.ContinuationIndentWidth = 2;
20525 
20526   EXPECT_EQ("int i =\n"
20527             "  longFunction(\n"
20528             "    arg);",
20529             format("int i = longFunction(arg);", TwoIndent));
20530 
20531   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
20532   SixIndent.ContinuationIndentWidth = 6;
20533 
20534   EXPECT_EQ("int i =\n"
20535             "      longFunction(\n"
20536             "            arg);",
20537             format("int i = longFunction(arg);", SixIndent));
20538 }
20539 
20540 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
20541   FormatStyle Style = getLLVMStyle();
20542   verifyFormat("int Foo::getter(\n"
20543                "    //\n"
20544                ") const {\n"
20545                "  return foo;\n"
20546                "}",
20547                Style);
20548   verifyFormat("void Foo::setter(\n"
20549                "    //\n"
20550                ") {\n"
20551                "  foo = 1;\n"
20552                "}",
20553                Style);
20554 }
20555 
20556 TEST_F(FormatTest, SpacesInAngles) {
20557   FormatStyle Spaces = getLLVMStyle();
20558   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20559 
20560   verifyFormat("vector< ::std::string > x1;", Spaces);
20561   verifyFormat("Foo< int, Bar > x2;", Spaces);
20562   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
20563 
20564   verifyFormat("static_cast< int >(arg);", Spaces);
20565   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
20566   verifyFormat("f< int, float >();", Spaces);
20567   verifyFormat("template <> g() {}", Spaces);
20568   verifyFormat("template < std::vector< int > > f() {}", Spaces);
20569   verifyFormat("std::function< void(int, int) > fct;", Spaces);
20570   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
20571                Spaces);
20572 
20573   Spaces.Standard = FormatStyle::LS_Cpp03;
20574   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20575   verifyFormat("A< A< int > >();", Spaces);
20576 
20577   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20578   verifyFormat("A<A<int> >();", Spaces);
20579 
20580   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20581   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
20582                Spaces);
20583   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
20584                Spaces);
20585 
20586   verifyFormat("A<A<int> >();", Spaces);
20587   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
20588   verifyFormat("A< A< int > >();", Spaces);
20589 
20590   Spaces.Standard = FormatStyle::LS_Cpp11;
20591   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20592   verifyFormat("A< A< int > >();", Spaces);
20593 
20594   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20595   verifyFormat("vector<::std::string> x4;", Spaces);
20596   verifyFormat("vector<int> x5;", Spaces);
20597   verifyFormat("Foo<int, Bar> x6;", Spaces);
20598   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20599 
20600   verifyFormat("A<A<int>>();", Spaces);
20601 
20602   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20603   verifyFormat("vector<::std::string> x4;", Spaces);
20604   verifyFormat("vector< ::std::string > x4;", Spaces);
20605   verifyFormat("vector<int> x5;", Spaces);
20606   verifyFormat("vector< int > x5;", Spaces);
20607   verifyFormat("Foo<int, Bar> x6;", Spaces);
20608   verifyFormat("Foo< int, Bar > x6;", Spaces);
20609   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20610   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
20611 
20612   verifyFormat("A<A<int>>();", Spaces);
20613   verifyFormat("A< A< int > >();", Spaces);
20614   verifyFormat("A<A<int > >();", Spaces);
20615   verifyFormat("A< A< int>>();", Spaces);
20616 }
20617 
20618 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
20619   FormatStyle Style = getLLVMStyle();
20620   Style.SpaceAfterTemplateKeyword = false;
20621   verifyFormat("template<int> void foo();", Style);
20622 }
20623 
20624 TEST_F(FormatTest, TripleAngleBrackets) {
20625   verifyFormat("f<<<1, 1>>>();");
20626   verifyFormat("f<<<1, 1, 1, s>>>();");
20627   verifyFormat("f<<<a, b, c, d>>>();");
20628   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
20629   verifyFormat("f<param><<<1, 1>>>();");
20630   verifyFormat("f<1><<<1, 1>>>();");
20631   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
20632   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20633                "aaaaaaaaaaa<<<\n    1, 1>>>();");
20634   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
20635                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
20636 }
20637 
20638 TEST_F(FormatTest, MergeLessLessAtEnd) {
20639   verifyFormat("<<");
20640   EXPECT_EQ("< < <", format("\\\n<<<"));
20641   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20642                "aaallvm::outs() <<");
20643   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20644                "aaaallvm::outs()\n    <<");
20645 }
20646 
20647 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
20648   std::string code = "#if A\n"
20649                      "#if B\n"
20650                      "a.\n"
20651                      "#endif\n"
20652                      "    a = 1;\n"
20653                      "#else\n"
20654                      "#endif\n"
20655                      "#if C\n"
20656                      "#else\n"
20657                      "#endif\n";
20658   EXPECT_EQ(code, format(code));
20659 }
20660 
20661 TEST_F(FormatTest, HandleConflictMarkers) {
20662   // Git/SVN conflict markers.
20663   EXPECT_EQ("int a;\n"
20664             "void f() {\n"
20665             "  callme(some(parameter1,\n"
20666             "<<<<<<< text by the vcs\n"
20667             "              parameter2),\n"
20668             "||||||| text by the vcs\n"
20669             "              parameter2),\n"
20670             "         parameter3,\n"
20671             "======= text by the vcs\n"
20672             "              parameter2, parameter3),\n"
20673             ">>>>>>> text by the vcs\n"
20674             "         otherparameter);\n",
20675             format("int a;\n"
20676                    "void f() {\n"
20677                    "  callme(some(parameter1,\n"
20678                    "<<<<<<< text by the vcs\n"
20679                    "  parameter2),\n"
20680                    "||||||| text by the vcs\n"
20681                    "  parameter2),\n"
20682                    "  parameter3,\n"
20683                    "======= text by the vcs\n"
20684                    "  parameter2,\n"
20685                    "  parameter3),\n"
20686                    ">>>>>>> text by the vcs\n"
20687                    "  otherparameter);\n"));
20688 
20689   // Perforce markers.
20690   EXPECT_EQ("void f() {\n"
20691             "  function(\n"
20692             ">>>> text by the vcs\n"
20693             "      parameter,\n"
20694             "==== text by the vcs\n"
20695             "      parameter,\n"
20696             "==== text by the vcs\n"
20697             "      parameter,\n"
20698             "<<<< text by the vcs\n"
20699             "      parameter);\n",
20700             format("void f() {\n"
20701                    "  function(\n"
20702                    ">>>> text by the vcs\n"
20703                    "  parameter,\n"
20704                    "==== text by the vcs\n"
20705                    "  parameter,\n"
20706                    "==== text by the vcs\n"
20707                    "  parameter,\n"
20708                    "<<<< text by the vcs\n"
20709                    "  parameter);\n"));
20710 
20711   EXPECT_EQ("<<<<<<<\n"
20712             "|||||||\n"
20713             "=======\n"
20714             ">>>>>>>",
20715             format("<<<<<<<\n"
20716                    "|||||||\n"
20717                    "=======\n"
20718                    ">>>>>>>"));
20719 
20720   EXPECT_EQ("<<<<<<<\n"
20721             "|||||||\n"
20722             "int i;\n"
20723             "=======\n"
20724             ">>>>>>>",
20725             format("<<<<<<<\n"
20726                    "|||||||\n"
20727                    "int i;\n"
20728                    "=======\n"
20729                    ">>>>>>>"));
20730 
20731   // FIXME: Handle parsing of macros around conflict markers correctly:
20732   EXPECT_EQ("#define Macro \\\n"
20733             "<<<<<<<\n"
20734             "Something \\\n"
20735             "|||||||\n"
20736             "Else \\\n"
20737             "=======\n"
20738             "Other \\\n"
20739             ">>>>>>>\n"
20740             "    End int i;\n",
20741             format("#define Macro \\\n"
20742                    "<<<<<<<\n"
20743                    "  Something \\\n"
20744                    "|||||||\n"
20745                    "  Else \\\n"
20746                    "=======\n"
20747                    "  Other \\\n"
20748                    ">>>>>>>\n"
20749                    "  End\n"
20750                    "int i;\n"));
20751 }
20752 
20753 TEST_F(FormatTest, DisableRegions) {
20754   EXPECT_EQ("int i;\n"
20755             "// clang-format off\n"
20756             "  int j;\n"
20757             "// clang-format on\n"
20758             "int k;",
20759             format(" int  i;\n"
20760                    "   // clang-format off\n"
20761                    "  int j;\n"
20762                    " // clang-format on\n"
20763                    "   int   k;"));
20764   EXPECT_EQ("int i;\n"
20765             "/* clang-format off */\n"
20766             "  int j;\n"
20767             "/* clang-format on */\n"
20768             "int k;",
20769             format(" int  i;\n"
20770                    "   /* clang-format off */\n"
20771                    "  int j;\n"
20772                    " /* clang-format on */\n"
20773                    "   int   k;"));
20774 
20775   // Don't reflow comments within disabled regions.
20776   EXPECT_EQ("// clang-format off\n"
20777             "// long long long long long long line\n"
20778             "/* clang-format on */\n"
20779             "/* long long long\n"
20780             " * long long long\n"
20781             " * line */\n"
20782             "int i;\n"
20783             "/* clang-format off */\n"
20784             "/* long long long long long long line */\n",
20785             format("// clang-format off\n"
20786                    "// long long long long long long line\n"
20787                    "/* clang-format on */\n"
20788                    "/* long long long long long long line */\n"
20789                    "int i;\n"
20790                    "/* clang-format off */\n"
20791                    "/* long long long long long long line */\n",
20792                    getLLVMStyleWithColumns(20)));
20793 }
20794 
20795 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
20796   format("? ) =");
20797   verifyNoCrash("#define a\\\n /**/}");
20798 }
20799 
20800 TEST_F(FormatTest, FormatsTableGenCode) {
20801   FormatStyle Style = getLLVMStyle();
20802   Style.Language = FormatStyle::LK_TableGen;
20803   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
20804 }
20805 
20806 TEST_F(FormatTest, ArrayOfTemplates) {
20807   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
20808             format("auto a = new unique_ptr<int > [ 10];"));
20809 
20810   FormatStyle Spaces = getLLVMStyle();
20811   Spaces.SpacesInSquareBrackets = true;
20812   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
20813             format("auto a = new unique_ptr<int > [10];", Spaces));
20814 }
20815 
20816 TEST_F(FormatTest, ArrayAsTemplateType) {
20817   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
20818             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
20819 
20820   FormatStyle Spaces = getLLVMStyle();
20821   Spaces.SpacesInSquareBrackets = true;
20822   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
20823             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
20824 }
20825 
20826 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
20827 
20828 TEST(FormatStyle, GetStyleWithEmptyFileName) {
20829   llvm::vfs::InMemoryFileSystem FS;
20830   auto Style1 = getStyle("file", "", "Google", "", &FS);
20831   ASSERT_TRUE((bool)Style1);
20832   ASSERT_EQ(*Style1, getGoogleStyle());
20833 }
20834 
20835 TEST(FormatStyle, GetStyleOfFile) {
20836   llvm::vfs::InMemoryFileSystem FS;
20837   // Test 1: format file in the same directory.
20838   ASSERT_TRUE(
20839       FS.addFile("/a/.clang-format", 0,
20840                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
20841   ASSERT_TRUE(
20842       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20843   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
20844   ASSERT_TRUE((bool)Style1);
20845   ASSERT_EQ(*Style1, getLLVMStyle());
20846 
20847   // Test 2.1: fallback to default.
20848   ASSERT_TRUE(
20849       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20850   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
20851   ASSERT_TRUE((bool)Style2);
20852   ASSERT_EQ(*Style2, getMozillaStyle());
20853 
20854   // Test 2.2: no format on 'none' fallback style.
20855   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
20856   ASSERT_TRUE((bool)Style2);
20857   ASSERT_EQ(*Style2, getNoStyle());
20858 
20859   // Test 2.3: format if config is found with no based style while fallback is
20860   // 'none'.
20861   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
20862                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
20863   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
20864   ASSERT_TRUE((bool)Style2);
20865   ASSERT_EQ(*Style2, getLLVMStyle());
20866 
20867   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
20868   Style2 = getStyle("{}", "a.h", "none", "", &FS);
20869   ASSERT_TRUE((bool)Style2);
20870   ASSERT_EQ(*Style2, getLLVMStyle());
20871 
20872   // Test 3: format file in parent directory.
20873   ASSERT_TRUE(
20874       FS.addFile("/c/.clang-format", 0,
20875                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
20876   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
20877                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20878   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
20879   ASSERT_TRUE((bool)Style3);
20880   ASSERT_EQ(*Style3, getGoogleStyle());
20881 
20882   // Test 4: error on invalid fallback style
20883   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
20884   ASSERT_FALSE((bool)Style4);
20885   llvm::consumeError(Style4.takeError());
20886 
20887   // Test 5: error on invalid yaml on command line
20888   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
20889   ASSERT_FALSE((bool)Style5);
20890   llvm::consumeError(Style5.takeError());
20891 
20892   // Test 6: error on invalid style
20893   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
20894   ASSERT_FALSE((bool)Style6);
20895   llvm::consumeError(Style6.takeError());
20896 
20897   // Test 7: found config file, error on parsing it
20898   ASSERT_TRUE(
20899       FS.addFile("/d/.clang-format", 0,
20900                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
20901                                                   "InvalidKey: InvalidValue")));
20902   ASSERT_TRUE(
20903       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20904   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
20905   ASSERT_FALSE((bool)Style7a);
20906   llvm::consumeError(Style7a.takeError());
20907 
20908   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
20909   ASSERT_TRUE((bool)Style7b);
20910 
20911   // Test 8: inferred per-language defaults apply.
20912   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
20913   ASSERT_TRUE((bool)StyleTd);
20914   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
20915 
20916   // Test 9.1: overwriting a file style, when parent no file exists with no
20917   // fallback style
20918   ASSERT_TRUE(FS.addFile(
20919       "/e/sub/.clang-format", 0,
20920       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
20921                                        "ColumnLimit: 20")));
20922   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
20923                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20924   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
20925   ASSERT_TRUE(static_cast<bool>(Style9));
20926   ASSERT_EQ(*Style9, [] {
20927     auto Style = getNoStyle();
20928     Style.ColumnLimit = 20;
20929     return Style;
20930   }());
20931 
20932   // Test 9.2: with LLVM fallback style
20933   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
20934   ASSERT_TRUE(static_cast<bool>(Style9));
20935   ASSERT_EQ(*Style9, [] {
20936     auto Style = getLLVMStyle();
20937     Style.ColumnLimit = 20;
20938     return Style;
20939   }());
20940 
20941   // Test 9.3: with a parent file
20942   ASSERT_TRUE(
20943       FS.addFile("/e/.clang-format", 0,
20944                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
20945                                                   "UseTab: Always")));
20946   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
20947   ASSERT_TRUE(static_cast<bool>(Style9));
20948   ASSERT_EQ(*Style9, [] {
20949     auto Style = getGoogleStyle();
20950     Style.ColumnLimit = 20;
20951     Style.UseTab = FormatStyle::UT_Always;
20952     return Style;
20953   }());
20954 
20955   // Test 9.4: propagate more than one level
20956   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
20957                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20958   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
20959                          llvm::MemoryBuffer::getMemBuffer(
20960                              "BasedOnStyle: InheritParentConfig\n"
20961                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
20962   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
20963 
20964   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
20965     auto Style = getGoogleStyle();
20966     Style.ColumnLimit = 20;
20967     Style.UseTab = FormatStyle::UT_Always;
20968     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
20969     return Style;
20970   }();
20971 
20972   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
20973   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
20974   ASSERT_TRUE(static_cast<bool>(Style9));
20975   ASSERT_EQ(*Style9, SubSubStyle);
20976 
20977   // Test 9.5: use InheritParentConfig as style name
20978   Style9 =
20979       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
20980   ASSERT_TRUE(static_cast<bool>(Style9));
20981   ASSERT_EQ(*Style9, SubSubStyle);
20982 
20983   // Test 9.6: use command line style with inheritance
20984   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
20985                     "none", "", &FS);
20986   ASSERT_TRUE(static_cast<bool>(Style9));
20987   ASSERT_EQ(*Style9, SubSubStyle);
20988 
20989   // Test 9.7: use command line style with inheritance and own config
20990   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
20991                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
20992                     "/e/sub/code.cpp", "none", "", &FS);
20993   ASSERT_TRUE(static_cast<bool>(Style9));
20994   ASSERT_EQ(*Style9, SubSubStyle);
20995 
20996   // Test 9.8: use inheritance from a file without BasedOnStyle
20997   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
20998                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
20999   ASSERT_TRUE(
21000       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
21001                  llvm::MemoryBuffer::getMemBuffer(
21002                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
21003   // Make sure we do not use the fallback style
21004   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
21005   ASSERT_TRUE(static_cast<bool>(Style9));
21006   ASSERT_EQ(*Style9, [] {
21007     auto Style = getLLVMStyle();
21008     Style.ColumnLimit = 123;
21009     return Style;
21010   }());
21011 
21012   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
21013   ASSERT_TRUE(static_cast<bool>(Style9));
21014   ASSERT_EQ(*Style9, [] {
21015     auto Style = getLLVMStyle();
21016     Style.ColumnLimit = 123;
21017     Style.IndentWidth = 7;
21018     return Style;
21019   }());
21020 }
21021 
21022 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
21023   // Column limit is 20.
21024   std::string Code = "Type *a =\n"
21025                      "    new Type();\n"
21026                      "g(iiiii, 0, jjjjj,\n"
21027                      "  0, kkkkk, 0, mm);\n"
21028                      "int  bad     = format   ;";
21029   std::string Expected = "auto a = new Type();\n"
21030                          "g(iiiii, nullptr,\n"
21031                          "  jjjjj, nullptr,\n"
21032                          "  kkkkk, nullptr,\n"
21033                          "  mm);\n"
21034                          "int  bad     = format   ;";
21035   FileID ID = Context.createInMemoryFile("format.cpp", Code);
21036   tooling::Replacements Replaces = toReplacements(
21037       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
21038                             "auto "),
21039        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
21040                             "nullptr"),
21041        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
21042                             "nullptr"),
21043        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
21044                             "nullptr")});
21045 
21046   format::FormatStyle Style = format::getLLVMStyle();
21047   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
21048   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21049   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21050       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21051   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21052   EXPECT_TRUE(static_cast<bool>(Result));
21053   EXPECT_EQ(Expected, *Result);
21054 }
21055 
21056 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
21057   std::string Code = "#include \"a.h\"\n"
21058                      "#include \"c.h\"\n"
21059                      "\n"
21060                      "int main() {\n"
21061                      "  return 0;\n"
21062                      "}";
21063   std::string Expected = "#include \"a.h\"\n"
21064                          "#include \"b.h\"\n"
21065                          "#include \"c.h\"\n"
21066                          "\n"
21067                          "int main() {\n"
21068                          "  return 0;\n"
21069                          "}";
21070   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
21071   tooling::Replacements Replaces = toReplacements(
21072       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
21073                             "#include \"b.h\"\n")});
21074 
21075   format::FormatStyle Style = format::getLLVMStyle();
21076   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
21077   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21078   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21079       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21080   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21081   EXPECT_TRUE(static_cast<bool>(Result));
21082   EXPECT_EQ(Expected, *Result);
21083 }
21084 
21085 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
21086   EXPECT_EQ("using std::cin;\n"
21087             "using std::cout;",
21088             format("using std::cout;\n"
21089                    "using std::cin;",
21090                    getGoogleStyle()));
21091 }
21092 
21093 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
21094   format::FormatStyle Style = format::getLLVMStyle();
21095   Style.Standard = FormatStyle::LS_Cpp03;
21096   // cpp03 recognize this string as identifier u8 and literal character 'a'
21097   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
21098 }
21099 
21100 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
21101   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
21102   // all modes, including C++11, C++14 and C++17
21103   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
21104 }
21105 
21106 TEST_F(FormatTest, DoNotFormatLikelyXml) {
21107   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
21108   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
21109 }
21110 
21111 TEST_F(FormatTest, StructuredBindings) {
21112   // Structured bindings is a C++17 feature.
21113   // all modes, including C++11, C++14 and C++17
21114   verifyFormat("auto [a, b] = f();");
21115   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
21116   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
21117   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
21118   EXPECT_EQ("auto const volatile [a, b] = f();",
21119             format("auto  const   volatile[a, b] = f();"));
21120   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
21121   EXPECT_EQ("auto &[a, b, c] = f();",
21122             format("auto   &[  a  ,  b,c   ] = f();"));
21123   EXPECT_EQ("auto &&[a, b, c] = f();",
21124             format("auto   &&[  a  ,  b,c   ] = f();"));
21125   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
21126   EXPECT_EQ("auto const volatile &&[a, b] = f();",
21127             format("auto  const  volatile  &&[a, b] = f();"));
21128   EXPECT_EQ("auto const &&[a, b] = f();",
21129             format("auto  const   &&  [a, b] = f();"));
21130   EXPECT_EQ("const auto &[a, b] = f();",
21131             format("const  auto  &  [a, b] = f();"));
21132   EXPECT_EQ("const auto volatile &&[a, b] = f();",
21133             format("const  auto   volatile  &&[a, b] = f();"));
21134   EXPECT_EQ("volatile const auto &&[a, b] = f();",
21135             format("volatile  const  auto   &&[a, b] = f();"));
21136   EXPECT_EQ("const auto &&[a, b] = f();",
21137             format("const  auto  &&  [a, b] = f();"));
21138 
21139   // Make sure we don't mistake structured bindings for lambdas.
21140   FormatStyle PointerMiddle = getLLVMStyle();
21141   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
21142   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
21143   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
21144   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
21145   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
21146   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
21147   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
21148   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
21149   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
21150   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
21151   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
21152   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
21153   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
21154 
21155   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
21156             format("for (const auto   &&   [a, b] : some_range) {\n}"));
21157   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
21158             format("for (const auto   &   [a, b] : some_range) {\n}"));
21159   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
21160             format("for (const auto[a, b] : some_range) {\n}"));
21161   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
21162   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
21163   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
21164   EXPECT_EQ("auto const &[x, y](expr);",
21165             format("auto  const  &  [x,y]  (expr);"));
21166   EXPECT_EQ("auto const &&[x, y](expr);",
21167             format("auto  const  &&  [x,y]  (expr);"));
21168   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
21169   EXPECT_EQ("auto const &[x, y]{expr};",
21170             format("auto  const  &  [x,y]  {expr};"));
21171   EXPECT_EQ("auto const &&[x, y]{expr};",
21172             format("auto  const  &&  [x,y]  {expr};"));
21173 
21174   format::FormatStyle Spaces = format::getLLVMStyle();
21175   Spaces.SpacesInSquareBrackets = true;
21176   verifyFormat("auto [ a, b ] = f();", Spaces);
21177   verifyFormat("auto &&[ a, b ] = f();", Spaces);
21178   verifyFormat("auto &[ a, b ] = f();", Spaces);
21179   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
21180   verifyFormat("auto const &[ a, b ] = f();", Spaces);
21181 }
21182 
21183 TEST_F(FormatTest, FileAndCode) {
21184   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
21185   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
21186   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
21187   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
21188   EXPECT_EQ(FormatStyle::LK_ObjC,
21189             guessLanguage("foo.h", "@interface Foo\n@end\n"));
21190   EXPECT_EQ(
21191       FormatStyle::LK_ObjC,
21192       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
21193   EXPECT_EQ(FormatStyle::LK_ObjC,
21194             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
21195   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
21196   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
21197   EXPECT_EQ(FormatStyle::LK_ObjC,
21198             guessLanguage("foo", "@interface Foo\n@end\n"));
21199   EXPECT_EQ(FormatStyle::LK_ObjC,
21200             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
21201   EXPECT_EQ(
21202       FormatStyle::LK_ObjC,
21203       guessLanguage("foo.h",
21204                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
21205   EXPECT_EQ(
21206       FormatStyle::LK_Cpp,
21207       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
21208 }
21209 
21210 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
21211   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
21212   EXPECT_EQ(FormatStyle::LK_ObjC,
21213             guessLanguage("foo.h", "array[[calculator getIndex]];"));
21214   EXPECT_EQ(FormatStyle::LK_Cpp,
21215             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
21216   EXPECT_EQ(
21217       FormatStyle::LK_Cpp,
21218       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
21219   EXPECT_EQ(FormatStyle::LK_ObjC,
21220             guessLanguage("foo.h", "[[noreturn foo] bar];"));
21221   EXPECT_EQ(FormatStyle::LK_Cpp,
21222             guessLanguage("foo.h", "[[clang::fallthrough]];"));
21223   EXPECT_EQ(FormatStyle::LK_ObjC,
21224             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
21225   EXPECT_EQ(FormatStyle::LK_Cpp,
21226             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
21227   EXPECT_EQ(FormatStyle::LK_Cpp,
21228             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
21229   EXPECT_EQ(FormatStyle::LK_ObjC,
21230             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
21231   EXPECT_EQ(FormatStyle::LK_Cpp,
21232             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
21233   EXPECT_EQ(
21234       FormatStyle::LK_Cpp,
21235       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
21236   EXPECT_EQ(
21237       FormatStyle::LK_Cpp,
21238       guessLanguage("foo.h",
21239                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
21240   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
21241 }
21242 
21243 TEST_F(FormatTest, GuessLanguageWithCaret) {
21244   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
21245   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
21246   EXPECT_EQ(FormatStyle::LK_ObjC,
21247             guessLanguage("foo.h", "int(^)(char, float);"));
21248   EXPECT_EQ(FormatStyle::LK_ObjC,
21249             guessLanguage("foo.h", "int(^foo)(char, float);"));
21250   EXPECT_EQ(FormatStyle::LK_ObjC,
21251             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
21252   EXPECT_EQ(FormatStyle::LK_ObjC,
21253             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
21254   EXPECT_EQ(
21255       FormatStyle::LK_ObjC,
21256       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
21257 }
21258 
21259 TEST_F(FormatTest, GuessLanguageWithPragmas) {
21260   EXPECT_EQ(FormatStyle::LK_Cpp,
21261             guessLanguage("foo.h", "__pragma(warning(disable:))"));
21262   EXPECT_EQ(FormatStyle::LK_Cpp,
21263             guessLanguage("foo.h", "#pragma(warning(disable:))"));
21264   EXPECT_EQ(FormatStyle::LK_Cpp,
21265             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
21266 }
21267 
21268 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
21269   // ASM symbolic names are identifiers that must be surrounded by [] without
21270   // space in between:
21271   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
21272 
21273   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
21274   verifyFormat(R"(//
21275 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
21276 )");
21277 
21278   // A list of several ASM symbolic names.
21279   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
21280 
21281   // ASM symbolic names in inline ASM with inputs and outputs.
21282   verifyFormat(R"(//
21283 asm("cmoveq %1, %2, %[result]"
21284     : [result] "=r"(result)
21285     : "r"(test), "r"(new), "[result]"(old));
21286 )");
21287 
21288   // ASM symbolic names in inline ASM with no outputs.
21289   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
21290 }
21291 
21292 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
21293   EXPECT_EQ(FormatStyle::LK_Cpp,
21294             guessLanguage("foo.h", "void f() {\n"
21295                                    "  asm (\"mov %[e], %[d]\"\n"
21296                                    "     : [d] \"=rm\" (d)\n"
21297                                    "       [e] \"rm\" (*e));\n"
21298                                    "}"));
21299   EXPECT_EQ(FormatStyle::LK_Cpp,
21300             guessLanguage("foo.h", "void f() {\n"
21301                                    "  _asm (\"mov %[e], %[d]\"\n"
21302                                    "     : [d] \"=rm\" (d)\n"
21303                                    "       [e] \"rm\" (*e));\n"
21304                                    "}"));
21305   EXPECT_EQ(FormatStyle::LK_Cpp,
21306             guessLanguage("foo.h", "void f() {\n"
21307                                    "  __asm (\"mov %[e], %[d]\"\n"
21308                                    "     : [d] \"=rm\" (d)\n"
21309                                    "       [e] \"rm\" (*e));\n"
21310                                    "}"));
21311   EXPECT_EQ(FormatStyle::LK_Cpp,
21312             guessLanguage("foo.h", "void f() {\n"
21313                                    "  __asm__ (\"mov %[e], %[d]\"\n"
21314                                    "     : [d] \"=rm\" (d)\n"
21315                                    "       [e] \"rm\" (*e));\n"
21316                                    "}"));
21317   EXPECT_EQ(FormatStyle::LK_Cpp,
21318             guessLanguage("foo.h", "void f() {\n"
21319                                    "  asm (\"mov %[e], %[d]\"\n"
21320                                    "     : [d] \"=rm\" (d),\n"
21321                                    "       [e] \"rm\" (*e));\n"
21322                                    "}"));
21323   EXPECT_EQ(FormatStyle::LK_Cpp,
21324             guessLanguage("foo.h", "void f() {\n"
21325                                    "  asm volatile (\"mov %[e], %[d]\"\n"
21326                                    "     : [d] \"=rm\" (d)\n"
21327                                    "       [e] \"rm\" (*e));\n"
21328                                    "}"));
21329 }
21330 
21331 TEST_F(FormatTest, GuessLanguageWithChildLines) {
21332   EXPECT_EQ(FormatStyle::LK_Cpp,
21333             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
21334   EXPECT_EQ(FormatStyle::LK_ObjC,
21335             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
21336   EXPECT_EQ(
21337       FormatStyle::LK_Cpp,
21338       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
21339   EXPECT_EQ(
21340       FormatStyle::LK_ObjC,
21341       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
21342 }
21343 
21344 TEST_F(FormatTest, TypenameMacros) {
21345   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
21346 
21347   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
21348   FormatStyle Google = getGoogleStyleWithColumns(0);
21349   Google.TypenameMacros = TypenameMacros;
21350   verifyFormat("struct foo {\n"
21351                "  int bar;\n"
21352                "  TAILQ_ENTRY(a) bleh;\n"
21353                "};",
21354                Google);
21355 
21356   FormatStyle Macros = getLLVMStyle();
21357   Macros.TypenameMacros = TypenameMacros;
21358 
21359   verifyFormat("STACK_OF(int) a;", Macros);
21360   verifyFormat("STACK_OF(int) *a;", Macros);
21361   verifyFormat("STACK_OF(int const *) *a;", Macros);
21362   verifyFormat("STACK_OF(int *const) *a;", Macros);
21363   verifyFormat("STACK_OF(int, string) a;", Macros);
21364   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
21365   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
21366   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
21367   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
21368   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
21369   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
21370 
21371   Macros.PointerAlignment = FormatStyle::PAS_Left;
21372   verifyFormat("STACK_OF(int)* a;", Macros);
21373   verifyFormat("STACK_OF(int*)* a;", Macros);
21374   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
21375   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
21376   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
21377 }
21378 
21379 TEST_F(FormatTest, AtomicQualifier) {
21380   // Check that we treate _Atomic as a type and not a function call
21381   FormatStyle Google = getGoogleStyleWithColumns(0);
21382   verifyFormat("struct foo {\n"
21383                "  int a1;\n"
21384                "  _Atomic(a) a2;\n"
21385                "  _Atomic(_Atomic(int) *const) a3;\n"
21386                "};",
21387                Google);
21388   verifyFormat("_Atomic(uint64_t) a;");
21389   verifyFormat("_Atomic(uint64_t) *a;");
21390   verifyFormat("_Atomic(uint64_t const *) *a;");
21391   verifyFormat("_Atomic(uint64_t *const) *a;");
21392   verifyFormat("_Atomic(const uint64_t *) *a;");
21393   verifyFormat("_Atomic(uint64_t) a;");
21394   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
21395   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
21396   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
21397   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
21398 
21399   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
21400   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
21401   FormatStyle Style = getLLVMStyle();
21402   Style.PointerAlignment = FormatStyle::PAS_Left;
21403   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
21404   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
21405   verifyFormat("_Atomic(int)* a;", Style);
21406   verifyFormat("_Atomic(int*)* a;", Style);
21407   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
21408 
21409   Style.SpacesInCStyleCastParentheses = true;
21410   Style.SpacesInParentheses = false;
21411   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
21412   Style.SpacesInCStyleCastParentheses = false;
21413   Style.SpacesInParentheses = true;
21414   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
21415   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
21416 }
21417 
21418 TEST_F(FormatTest, AmbersandInLamda) {
21419   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
21420   FormatStyle AlignStyle = getLLVMStyle();
21421   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
21422   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21423   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
21424   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21425 }
21426 
21427 TEST_F(FormatTest, SpacesInConditionalStatement) {
21428   FormatStyle Spaces = getLLVMStyle();
21429   Spaces.IfMacros.clear();
21430   Spaces.IfMacros.push_back("MYIF");
21431   Spaces.SpacesInConditionalStatement = true;
21432   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
21433   verifyFormat("if ( !a )\n  return;", Spaces);
21434   verifyFormat("if ( a )\n  return;", Spaces);
21435   verifyFormat("if constexpr ( a )\n  return;", Spaces);
21436   verifyFormat("MYIF ( a )\n  return;", Spaces);
21437   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
21438   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
21439   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
21440   verifyFormat("while ( a )\n  return;", Spaces);
21441   verifyFormat("while ( (a && b) )\n  return;", Spaces);
21442   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
21443   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
21444   // Check that space on the left of "::" is inserted as expected at beginning
21445   // of condition.
21446   verifyFormat("while ( ::func() )\n  return;", Spaces);
21447 
21448   // Check impact of ControlStatementsExceptControlMacros is honored.
21449   Spaces.SpaceBeforeParens =
21450       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
21451   verifyFormat("MYIF( a )\n  return;", Spaces);
21452   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
21453   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
21454 }
21455 
21456 TEST_F(FormatTest, AlternativeOperators) {
21457   // Test case for ensuring alternate operators are not
21458   // combined with their right most neighbour.
21459   verifyFormat("int a and b;");
21460   verifyFormat("int a and_eq b;");
21461   verifyFormat("int a bitand b;");
21462   verifyFormat("int a bitor b;");
21463   verifyFormat("int a compl b;");
21464   verifyFormat("int a not b;");
21465   verifyFormat("int a not_eq b;");
21466   verifyFormat("int a or b;");
21467   verifyFormat("int a xor b;");
21468   verifyFormat("int a xor_eq b;");
21469   verifyFormat("return this not_eq bitand other;");
21470   verifyFormat("bool operator not_eq(const X bitand other)");
21471 
21472   verifyFormat("int a and 5;");
21473   verifyFormat("int a and_eq 5;");
21474   verifyFormat("int a bitand 5;");
21475   verifyFormat("int a bitor 5;");
21476   verifyFormat("int a compl 5;");
21477   verifyFormat("int a not 5;");
21478   verifyFormat("int a not_eq 5;");
21479   verifyFormat("int a or 5;");
21480   verifyFormat("int a xor 5;");
21481   verifyFormat("int a xor_eq 5;");
21482 
21483   verifyFormat("int a compl(5);");
21484   verifyFormat("int a not(5);");
21485 
21486   /* FIXME handle alternate tokens
21487    * https://en.cppreference.com/w/cpp/language/operator_alternative
21488   // alternative tokens
21489   verifyFormat("compl foo();");     //  ~foo();
21490   verifyFormat("foo() <%%>;");      // foo();
21491   verifyFormat("void foo() <%%>;"); // void foo(){}
21492   verifyFormat("int a <:1:>;");     // int a[1];[
21493   verifyFormat("%:define ABC abc"); // #define ABC abc
21494   verifyFormat("%:%:");             // ##
21495   */
21496 }
21497 
21498 TEST_F(FormatTest, STLWhileNotDefineChed) {
21499   verifyFormat("#if defined(while)\n"
21500                "#define while EMIT WARNING C4005\n"
21501                "#endif // while");
21502 }
21503 
21504 TEST_F(FormatTest, OperatorSpacing) {
21505   FormatStyle Style = getLLVMStyle();
21506   Style.PointerAlignment = FormatStyle::PAS_Right;
21507   verifyFormat("Foo::operator*();", Style);
21508   verifyFormat("Foo::operator void *();", Style);
21509   verifyFormat("Foo::operator void **();", Style);
21510   verifyFormat("Foo::operator void *&();", Style);
21511   verifyFormat("Foo::operator void *&&();", Style);
21512   verifyFormat("Foo::operator void const *();", Style);
21513   verifyFormat("Foo::operator void const **();", Style);
21514   verifyFormat("Foo::operator void const *&();", Style);
21515   verifyFormat("Foo::operator void const *&&();", Style);
21516   verifyFormat("Foo::operator()(void *);", Style);
21517   verifyFormat("Foo::operator*(void *);", Style);
21518   verifyFormat("Foo::operator*();", Style);
21519   verifyFormat("Foo::operator**();", Style);
21520   verifyFormat("Foo::operator&();", Style);
21521   verifyFormat("Foo::operator<int> *();", Style);
21522   verifyFormat("Foo::operator<Foo> *();", Style);
21523   verifyFormat("Foo::operator<int> **();", Style);
21524   verifyFormat("Foo::operator<Foo> **();", Style);
21525   verifyFormat("Foo::operator<int> &();", Style);
21526   verifyFormat("Foo::operator<Foo> &();", Style);
21527   verifyFormat("Foo::operator<int> &&();", Style);
21528   verifyFormat("Foo::operator<Foo> &&();", Style);
21529   verifyFormat("Foo::operator<int> *&();", Style);
21530   verifyFormat("Foo::operator<Foo> *&();", Style);
21531   verifyFormat("Foo::operator<int> *&&();", Style);
21532   verifyFormat("Foo::operator<Foo> *&&();", Style);
21533   verifyFormat("operator*(int (*)(), class Foo);", Style);
21534 
21535   verifyFormat("Foo::operator&();", Style);
21536   verifyFormat("Foo::operator void &();", Style);
21537   verifyFormat("Foo::operator void const &();", Style);
21538   verifyFormat("Foo::operator()(void &);", Style);
21539   verifyFormat("Foo::operator&(void &);", Style);
21540   verifyFormat("Foo::operator&();", Style);
21541   verifyFormat("operator&(int (&)(), class Foo);", Style);
21542 
21543   verifyFormat("Foo::operator&&();", Style);
21544   verifyFormat("Foo::operator**();", Style);
21545   verifyFormat("Foo::operator void &&();", Style);
21546   verifyFormat("Foo::operator void const &&();", Style);
21547   verifyFormat("Foo::operator()(void &&);", Style);
21548   verifyFormat("Foo::operator&&(void &&);", Style);
21549   verifyFormat("Foo::operator&&();", Style);
21550   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21551   verifyFormat("operator const nsTArrayRight<E> &()", Style);
21552   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
21553                Style);
21554   verifyFormat("operator void **()", Style);
21555   verifyFormat("operator const FooRight<Object> &()", Style);
21556   verifyFormat("operator const FooRight<Object> *()", Style);
21557   verifyFormat("operator const FooRight<Object> **()", Style);
21558   verifyFormat("operator const FooRight<Object> *&()", Style);
21559   verifyFormat("operator const FooRight<Object> *&&()", Style);
21560 
21561   Style.PointerAlignment = FormatStyle::PAS_Left;
21562   verifyFormat("Foo::operator*();", Style);
21563   verifyFormat("Foo::operator**();", Style);
21564   verifyFormat("Foo::operator void*();", Style);
21565   verifyFormat("Foo::operator void**();", Style);
21566   verifyFormat("Foo::operator void*&();", Style);
21567   verifyFormat("Foo::operator void*&&();", Style);
21568   verifyFormat("Foo::operator void const*();", Style);
21569   verifyFormat("Foo::operator void const**();", Style);
21570   verifyFormat("Foo::operator void const*&();", Style);
21571   verifyFormat("Foo::operator void const*&&();", Style);
21572   verifyFormat("Foo::operator/*comment*/ void*();", Style);
21573   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
21574   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
21575   verifyFormat("Foo::operator()(void*);", Style);
21576   verifyFormat("Foo::operator*(void*);", Style);
21577   verifyFormat("Foo::operator*();", Style);
21578   verifyFormat("Foo::operator<int>*();", Style);
21579   verifyFormat("Foo::operator<Foo>*();", Style);
21580   verifyFormat("Foo::operator<int>**();", Style);
21581   verifyFormat("Foo::operator<Foo>**();", Style);
21582   verifyFormat("Foo::operator<Foo>*&();", Style);
21583   verifyFormat("Foo::operator<int>&();", Style);
21584   verifyFormat("Foo::operator<Foo>&();", Style);
21585   verifyFormat("Foo::operator<int>&&();", Style);
21586   verifyFormat("Foo::operator<Foo>&&();", Style);
21587   verifyFormat("Foo::operator<int>*&();", Style);
21588   verifyFormat("Foo::operator<Foo>*&();", Style);
21589   verifyFormat("operator*(int (*)(), class Foo);", Style);
21590 
21591   verifyFormat("Foo::operator&();", Style);
21592   verifyFormat("Foo::operator void&();", Style);
21593   verifyFormat("Foo::operator void const&();", Style);
21594   verifyFormat("Foo::operator/*comment*/ void&();", Style);
21595   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
21596   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
21597   verifyFormat("Foo::operator()(void&);", Style);
21598   verifyFormat("Foo::operator&(void&);", Style);
21599   verifyFormat("Foo::operator&();", Style);
21600   verifyFormat("operator&(int (&)(), class Foo);", Style);
21601 
21602   verifyFormat("Foo::operator&&();", Style);
21603   verifyFormat("Foo::operator void&&();", Style);
21604   verifyFormat("Foo::operator void const&&();", Style);
21605   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
21606   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
21607   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
21608   verifyFormat("Foo::operator()(void&&);", Style);
21609   verifyFormat("Foo::operator&&(void&&);", Style);
21610   verifyFormat("Foo::operator&&();", Style);
21611   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21612   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
21613   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
21614                Style);
21615   verifyFormat("operator void**()", Style);
21616   verifyFormat("operator const FooLeft<Object>&()", Style);
21617   verifyFormat("operator const FooLeft<Object>*()", Style);
21618   verifyFormat("operator const FooLeft<Object>**()", Style);
21619   verifyFormat("operator const FooLeft<Object>*&()", Style);
21620   verifyFormat("operator const FooLeft<Object>*&&()", Style);
21621 
21622   // PR45107
21623   verifyFormat("operator Vector<String>&();", Style);
21624   verifyFormat("operator const Vector<String>&();", Style);
21625   verifyFormat("operator foo::Bar*();", Style);
21626   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
21627   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
21628                Style);
21629 
21630   Style.PointerAlignment = FormatStyle::PAS_Middle;
21631   verifyFormat("Foo::operator*();", Style);
21632   verifyFormat("Foo::operator void *();", Style);
21633   verifyFormat("Foo::operator()(void *);", Style);
21634   verifyFormat("Foo::operator*(void *);", Style);
21635   verifyFormat("Foo::operator*();", Style);
21636   verifyFormat("operator*(int (*)(), class Foo);", Style);
21637 
21638   verifyFormat("Foo::operator&();", Style);
21639   verifyFormat("Foo::operator void &();", Style);
21640   verifyFormat("Foo::operator void const &();", Style);
21641   verifyFormat("Foo::operator()(void &);", Style);
21642   verifyFormat("Foo::operator&(void &);", Style);
21643   verifyFormat("Foo::operator&();", Style);
21644   verifyFormat("operator&(int (&)(), class Foo);", Style);
21645 
21646   verifyFormat("Foo::operator&&();", Style);
21647   verifyFormat("Foo::operator void &&();", Style);
21648   verifyFormat("Foo::operator void const &&();", Style);
21649   verifyFormat("Foo::operator()(void &&);", Style);
21650   verifyFormat("Foo::operator&&(void &&);", Style);
21651   verifyFormat("Foo::operator&&();", Style);
21652   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21653 }
21654 
21655 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
21656   FormatStyle Style = getLLVMStyle();
21657   // PR46157
21658   verifyFormat("foo(operator+, -42);", Style);
21659   verifyFormat("foo(operator++, -42);", Style);
21660   verifyFormat("foo(operator--, -42);", Style);
21661   verifyFormat("foo(-42, operator--);", Style);
21662   verifyFormat("foo(-42, operator, );", Style);
21663   verifyFormat("foo(operator, , -42);", Style);
21664 }
21665 
21666 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
21667   FormatStyle Style = getLLVMStyle();
21668   Style.WhitespaceSensitiveMacros.push_back("FOO");
21669 
21670   // Don't use the helpers here, since 'mess up' will change the whitespace
21671   // and these are all whitespace sensitive by definition
21672   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
21673             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
21674   EXPECT_EQ(
21675       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
21676       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
21677   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
21678             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
21679   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
21680             "       Still=Intentional);",
21681             format("FOO(String-ized&Messy+But,: :\n"
21682                    "       Still=Intentional);",
21683                    Style));
21684   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
21685   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
21686             "       Still=Intentional);",
21687             format("FOO(String-ized=&Messy+But,: :\n"
21688                    "       Still=Intentional);",
21689                    Style));
21690 
21691   Style.ColumnLimit = 21;
21692   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
21693             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
21694 }
21695 
21696 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
21697   // These tests are not in NamespaceFixer because that doesn't
21698   // test its interaction with line wrapping
21699   FormatStyle Style = getLLVMStyle();
21700   Style.ColumnLimit = 80;
21701   verifyFormat("namespace {\n"
21702                "int i;\n"
21703                "int j;\n"
21704                "} // namespace",
21705                Style);
21706 
21707   verifyFormat("namespace AAA {\n"
21708                "int i;\n"
21709                "int j;\n"
21710                "} // namespace AAA",
21711                Style);
21712 
21713   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
21714             "int i;\n"
21715             "int j;\n"
21716             "} // namespace Averyveryveryverylongnamespace",
21717             format("namespace Averyveryveryverylongnamespace {\n"
21718                    "int i;\n"
21719                    "int j;\n"
21720                    "}",
21721                    Style));
21722 
21723   EXPECT_EQ(
21724       "namespace "
21725       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
21726       "    went::mad::now {\n"
21727       "int i;\n"
21728       "int j;\n"
21729       "} // namespace\n"
21730       "  // "
21731       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
21732       "went::mad::now",
21733       format("namespace "
21734              "would::it::save::you::a::lot::of::time::if_::i::"
21735              "just::gave::up::and_::went::mad::now {\n"
21736              "int i;\n"
21737              "int j;\n"
21738              "}",
21739              Style));
21740 
21741   // This used to duplicate the comment again and again on subsequent runs
21742   EXPECT_EQ(
21743       "namespace "
21744       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
21745       "    went::mad::now {\n"
21746       "int i;\n"
21747       "int j;\n"
21748       "} // namespace\n"
21749       "  // "
21750       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
21751       "went::mad::now",
21752       format("namespace "
21753              "would::it::save::you::a::lot::of::time::if_::i::"
21754              "just::gave::up::and_::went::mad::now {\n"
21755              "int i;\n"
21756              "int j;\n"
21757              "} // namespace\n"
21758              "  // "
21759              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
21760              "and_::went::mad::now",
21761              Style));
21762 }
21763 
21764 TEST_F(FormatTest, LikelyUnlikely) {
21765   FormatStyle Style = getLLVMStyle();
21766 
21767   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21768                "  return 29;\n"
21769                "}",
21770                Style);
21771 
21772   verifyFormat("if (argc > 5) [[likely]] {\n"
21773                "  return 29;\n"
21774                "}",
21775                Style);
21776 
21777   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21778                "  return 29;\n"
21779                "} else [[likely]] {\n"
21780                "  return 42;\n"
21781                "}\n",
21782                Style);
21783 
21784   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21785                "  return 29;\n"
21786                "} else if (argc > 10) [[likely]] {\n"
21787                "  return 99;\n"
21788                "} else {\n"
21789                "  return 42;\n"
21790                "}\n",
21791                Style);
21792 
21793   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
21794                "  return 29;\n"
21795                "}",
21796                Style);
21797 }
21798 
21799 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
21800   verifyFormat("Constructor()\n"
21801                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21802                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
21803                "aaaaaaaaaaaaaaaaaat))");
21804   verifyFormat("Constructor()\n"
21805                "    : aaaaaaaaaaaaa(aaaaaa), "
21806                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
21807 
21808   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
21809   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
21810   verifyFormat("Constructor()\n"
21811                "    : aaaaaa(aaaaaa),\n"
21812                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21813                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
21814                StyleWithWhitespacePenalty);
21815   verifyFormat("Constructor()\n"
21816                "    : aaaaaaaaaaaaa(aaaaaa), "
21817                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
21818                StyleWithWhitespacePenalty);
21819 }
21820 
21821 TEST_F(FormatTest, LLVMDefaultStyle) {
21822   FormatStyle Style = getLLVMStyle();
21823   verifyFormat("extern \"C\" {\n"
21824                "int foo();\n"
21825                "}",
21826                Style);
21827 }
21828 TEST_F(FormatTest, GNUDefaultStyle) {
21829   FormatStyle Style = getGNUStyle();
21830   verifyFormat("extern \"C\"\n"
21831                "{\n"
21832                "  int foo ();\n"
21833                "}",
21834                Style);
21835 }
21836 TEST_F(FormatTest, MozillaDefaultStyle) {
21837   FormatStyle Style = getMozillaStyle();
21838   verifyFormat("extern \"C\"\n"
21839                "{\n"
21840                "  int foo();\n"
21841                "}",
21842                Style);
21843 }
21844 TEST_F(FormatTest, GoogleDefaultStyle) {
21845   FormatStyle Style = getGoogleStyle();
21846   verifyFormat("extern \"C\" {\n"
21847                "int foo();\n"
21848                "}",
21849                Style);
21850 }
21851 TEST_F(FormatTest, ChromiumDefaultStyle) {
21852   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
21853   verifyFormat("extern \"C\" {\n"
21854                "int foo();\n"
21855                "}",
21856                Style);
21857 }
21858 TEST_F(FormatTest, MicrosoftDefaultStyle) {
21859   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
21860   verifyFormat("extern \"C\"\n"
21861                "{\n"
21862                "    int foo();\n"
21863                "}",
21864                Style);
21865 }
21866 TEST_F(FormatTest, WebKitDefaultStyle) {
21867   FormatStyle Style = getWebKitStyle();
21868   verifyFormat("extern \"C\" {\n"
21869                "int foo();\n"
21870                "}",
21871                Style);
21872 }
21873 
21874 TEST_F(FormatTest, ConceptsAndRequires) {
21875   FormatStyle Style = getLLVMStyle();
21876   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
21877 
21878   verifyFormat("template <typename T>\n"
21879                "concept Hashable = requires(T a) {\n"
21880                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
21881                "};",
21882                Style);
21883   verifyFormat("template <typename T>\n"
21884                "concept EqualityComparable = requires(T a, T b) {\n"
21885                "  { a == b } -> bool;\n"
21886                "};",
21887                Style);
21888   verifyFormat("template <typename T>\n"
21889                "concept EqualityComparable = requires(T a, T b) {\n"
21890                "  { a == b } -> bool;\n"
21891                "  { a != b } -> bool;\n"
21892                "};",
21893                Style);
21894   verifyFormat("template <typename T>\n"
21895                "concept EqualityComparable = requires(T a, T b) {\n"
21896                "  { a == b } -> bool;\n"
21897                "  { a != b } -> bool;\n"
21898                "};",
21899                Style);
21900 
21901   verifyFormat("template <typename It>\n"
21902                "requires Iterator<It>\n"
21903                "void sort(It begin, It end) {\n"
21904                "  //....\n"
21905                "}",
21906                Style);
21907 
21908   verifyFormat("template <typename T>\n"
21909                "concept Large = sizeof(T) > 10;",
21910                Style);
21911 
21912   verifyFormat("template <typename T, typename U>\n"
21913                "concept FooableWith = requires(T t, U u) {\n"
21914                "  typename T::foo_type;\n"
21915                "  { t.foo(u) } -> typename T::foo_type;\n"
21916                "  t++;\n"
21917                "};\n"
21918                "void doFoo(FooableWith<int> auto t) {\n"
21919                "  t.foo(3);\n"
21920                "}",
21921                Style);
21922   verifyFormat("template <typename T>\n"
21923                "concept Context = sizeof(T) == 1;",
21924                Style);
21925   verifyFormat("template <typename T>\n"
21926                "concept Context = is_specialization_of_v<context, T>;",
21927                Style);
21928   verifyFormat("template <typename T>\n"
21929                "concept Node = std::is_object_v<T>;",
21930                Style);
21931   verifyFormat("template <typename T>\n"
21932                "concept Tree = true;",
21933                Style);
21934 
21935   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
21936                "  //...\n"
21937                "}",
21938                Style);
21939 
21940   verifyFormat(
21941       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
21942       "  //...\n"
21943       "}",
21944       Style);
21945 
21946   verifyFormat(
21947       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
21948       "  //...\n"
21949       "}",
21950       Style);
21951 
21952   verifyFormat("template <typename T>\n"
21953                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
21954                "Concept2<I> {\n"
21955                "  //...\n"
21956                "}",
21957                Style);
21958 
21959   verifyFormat("template <typename T>\n"
21960                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
21961                "Concept2<I> {\n"
21962                "  //...\n"
21963                "}",
21964                Style);
21965 
21966   verifyFormat(
21967       "template <typename T>\n"
21968       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
21969       "  //...\n"
21970       "}",
21971       Style);
21972 
21973   verifyFormat(
21974       "template <typename T>\n"
21975       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
21976       "  //...\n"
21977       "}",
21978       Style);
21979 
21980   verifyFormat("template <typename It>\n"
21981                "requires Foo<It>() && Bar<It> {\n"
21982                "  //....\n"
21983                "}",
21984                Style);
21985 
21986   verifyFormat("template <typename It>\n"
21987                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
21988                "  //....\n"
21989                "}",
21990                Style);
21991 
21992   verifyFormat("template <typename It>\n"
21993                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
21994                "  //....\n"
21995                "}",
21996                Style);
21997 
21998   verifyFormat(
21999       "template <typename It>\n"
22000       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
22001       "  //....\n"
22002       "}",
22003       Style);
22004 
22005   Style.IndentRequires = true;
22006   verifyFormat("template <typename It>\n"
22007                "  requires Iterator<It>\n"
22008                "void sort(It begin, It end) {\n"
22009                "  //....\n"
22010                "}",
22011                Style);
22012   verifyFormat("template <std::size index_>\n"
22013                "  requires(index_ < sizeof...(Children_))\n"
22014                "Tree auto &child() {\n"
22015                "  // ...\n"
22016                "}",
22017                Style);
22018 
22019   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
22020   verifyFormat("template <typename T>\n"
22021                "concept Hashable = requires (T a) {\n"
22022                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22023                "};",
22024                Style);
22025 
22026   verifyFormat("template <class T = void>\n"
22027                "  requires EqualityComparable<T> || Same<T, void>\n"
22028                "struct equal_to;",
22029                Style);
22030 
22031   verifyFormat("template <class T>\n"
22032                "  requires requires {\n"
22033                "    T{};\n"
22034                "    T (int);\n"
22035                "  }\n",
22036                Style);
22037 
22038   Style.ColumnLimit = 78;
22039   verifyFormat("template <typename T>\n"
22040                "concept Context = Traits<typename T::traits_type> and\n"
22041                "    Interface<typename T::interface_type> and\n"
22042                "    Request<typename T::request_type> and\n"
22043                "    Response<typename T::response_type> and\n"
22044                "    ContextExtension<typename T::extension_type> and\n"
22045                "    ::std::is_copy_constructable<T> and "
22046                "::std::is_move_constructable<T> and\n"
22047                "    requires (T c) {\n"
22048                "  { c.response; } -> Response;\n"
22049                "} and requires (T c) {\n"
22050                "  { c.request; } -> Request;\n"
22051                "}\n",
22052                Style);
22053 
22054   verifyFormat("template <typename T>\n"
22055                "concept Context = Traits<typename T::traits_type> or\n"
22056                "    Interface<typename T::interface_type> or\n"
22057                "    Request<typename T::request_type> or\n"
22058                "    Response<typename T::response_type> or\n"
22059                "    ContextExtension<typename T::extension_type> or\n"
22060                "    ::std::is_copy_constructable<T> or "
22061                "::std::is_move_constructable<T> or\n"
22062                "    requires (T c) {\n"
22063                "  { c.response; } -> Response;\n"
22064                "} or requires (T c) {\n"
22065                "  { c.request; } -> Request;\n"
22066                "}\n",
22067                Style);
22068 
22069   verifyFormat("template <typename T>\n"
22070                "concept Context = Traits<typename T::traits_type> &&\n"
22071                "    Interface<typename T::interface_type> &&\n"
22072                "    Request<typename T::request_type> &&\n"
22073                "    Response<typename T::response_type> &&\n"
22074                "    ContextExtension<typename T::extension_type> &&\n"
22075                "    ::std::is_copy_constructable<T> && "
22076                "::std::is_move_constructable<T> &&\n"
22077                "    requires (T c) {\n"
22078                "  { c.response; } -> Response;\n"
22079                "} && requires (T c) {\n"
22080                "  { c.request; } -> Request;\n"
22081                "}\n",
22082                Style);
22083 
22084   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
22085                "Constraint2<T>;");
22086 
22087   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
22088   Style.BraceWrapping.AfterFunction = true;
22089   Style.BraceWrapping.AfterClass = true;
22090   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
22091   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
22092   verifyFormat("void Foo () requires (std::copyable<T>)\n"
22093                "{\n"
22094                "  return\n"
22095                "}\n",
22096                Style);
22097 
22098   verifyFormat("void Foo () requires std::copyable<T>\n"
22099                "{\n"
22100                "  return\n"
22101                "}\n",
22102                Style);
22103 
22104   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22105                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
22106                "struct constant;",
22107                Style);
22108 
22109   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22110                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
22111                "struct constant;",
22112                Style);
22113 
22114   verifyFormat("template <class T>\n"
22115                "class plane_with_very_very_very_long_name\n"
22116                "{\n"
22117                "  constexpr plane_with_very_very_very_long_name () requires "
22118                "std::copyable<T>\n"
22119                "      : plane_with_very_very_very_long_name (1)\n"
22120                "  {\n"
22121                "  }\n"
22122                "}\n",
22123                Style);
22124 
22125   verifyFormat("template <class T>\n"
22126                "class plane_with_long_name\n"
22127                "{\n"
22128                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
22129                "      : plane_with_long_name (1)\n"
22130                "  {\n"
22131                "  }\n"
22132                "}\n",
22133                Style);
22134 
22135   Style.BreakBeforeConceptDeclarations = false;
22136   verifyFormat("template <typename T> concept Tree = true;", Style);
22137 
22138   Style.IndentRequires = false;
22139   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22140                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
22141                "struct constant;",
22142                Style);
22143 }
22144 
22145 TEST_F(FormatTest, StatementAttributeLikeMacros) {
22146   FormatStyle Style = getLLVMStyle();
22147   StringRef Source = "void Foo::slot() {\n"
22148                      "  unsigned char MyChar = 'x';\n"
22149                      "  emit signal(MyChar);\n"
22150                      "  Q_EMIT signal(MyChar);\n"
22151                      "}";
22152 
22153   EXPECT_EQ(Source, format(Source, Style));
22154 
22155   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
22156   EXPECT_EQ("void Foo::slot() {\n"
22157             "  unsigned char MyChar = 'x';\n"
22158             "  emit          signal(MyChar);\n"
22159             "  Q_EMIT signal(MyChar);\n"
22160             "}",
22161             format(Source, Style));
22162 
22163   Style.StatementAttributeLikeMacros.push_back("emit");
22164   EXPECT_EQ(Source, format(Source, Style));
22165 
22166   Style.StatementAttributeLikeMacros = {};
22167   EXPECT_EQ("void Foo::slot() {\n"
22168             "  unsigned char MyChar = 'x';\n"
22169             "  emit          signal(MyChar);\n"
22170             "  Q_EMIT        signal(MyChar);\n"
22171             "}",
22172             format(Source, Style));
22173 }
22174 
22175 TEST_F(FormatTest, IndentAccessModifiers) {
22176   FormatStyle Style = getLLVMStyle();
22177   Style.IndentAccessModifiers = true;
22178   // Members are *two* levels below the record;
22179   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
22180   verifyFormat("class C {\n"
22181                "    int i;\n"
22182                "};\n",
22183                Style);
22184   verifyFormat("union C {\n"
22185                "    int i;\n"
22186                "    unsigned u;\n"
22187                "};\n",
22188                Style);
22189   // Access modifiers should be indented one level below the record.
22190   verifyFormat("class C {\n"
22191                "  public:\n"
22192                "    int i;\n"
22193                "};\n",
22194                Style);
22195   verifyFormat("struct S {\n"
22196                "  private:\n"
22197                "    class C {\n"
22198                "        int j;\n"
22199                "\n"
22200                "      public:\n"
22201                "        C();\n"
22202                "    };\n"
22203                "\n"
22204                "  public:\n"
22205                "    int i;\n"
22206                "};\n",
22207                Style);
22208   // Enumerations are not records and should be unaffected.
22209   Style.AllowShortEnumsOnASingleLine = false;
22210   verifyFormat("enum class E {\n"
22211                "  A,\n"
22212                "  B\n"
22213                "};\n",
22214                Style);
22215   // Test with a different indentation width;
22216   // also proves that the result is Style.AccessModifierOffset agnostic.
22217   Style.IndentWidth = 3;
22218   verifyFormat("class C {\n"
22219                "   public:\n"
22220                "      int i;\n"
22221                "};\n",
22222                Style);
22223 }
22224 
22225 TEST_F(FormatTest, LimitlessStringsAndComments) {
22226   auto Style = getLLVMStyleWithColumns(0);
22227   constexpr StringRef Code =
22228       "/**\n"
22229       " * This is a multiline comment with quite some long lines, at least for "
22230       "the LLVM Style.\n"
22231       " * We will redo this with strings and line comments. Just to  check if "
22232       "everything is working.\n"
22233       " */\n"
22234       "bool foo() {\n"
22235       "  /* Single line multi line comment. */\n"
22236       "  const std::string String = \"This is a multiline string with quite "
22237       "some long lines, at least for the LLVM Style.\"\n"
22238       "                             \"We already did it with multi line "
22239       "comments, and we will do it with line comments. Just to check if "
22240       "everything is working.\";\n"
22241       "  // This is a line comment (block) with quite some long lines, at "
22242       "least for the LLVM Style.\n"
22243       "  // We already did this with multi line comments and strings. Just to "
22244       "check if everything is working.\n"
22245       "  const std::string SmallString = \"Hello World\";\n"
22246       "  // Small line comment\n"
22247       "  return String.size() > SmallString.size();\n"
22248       "}";
22249   EXPECT_EQ(Code, format(Code, Style));
22250 }
22251 } // namespace
22252 } // namespace format
22253 } // namespace clang
22254