xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision 9da70ab3d43c79116f80fc06aa7cf517374ce42c)
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                "{\n"
2456                "  A,\n"
2457                "  B,\n"
2458                "  C\n"
2459                "} ShortEnum1, ShortEnum2;",
2460                Style);
2461 }
2462 
2463 TEST_F(FormatTest, ShortCaseLabels) {
2464   FormatStyle Style = getLLVMStyle();
2465   Style.AllowShortCaseLabelsOnASingleLine = true;
2466   verifyFormat("switch (a) {\n"
2467                "case 1: x = 1; break;\n"
2468                "case 2: return;\n"
2469                "case 3:\n"
2470                "case 4:\n"
2471                "case 5: return;\n"
2472                "case 6: // comment\n"
2473                "  return;\n"
2474                "case 7:\n"
2475                "  // comment\n"
2476                "  return;\n"
2477                "case 8:\n"
2478                "  x = 8; // comment\n"
2479                "  break;\n"
2480                "default: y = 1; break;\n"
2481                "}",
2482                Style);
2483   verifyFormat("switch (a) {\n"
2484                "case 0: return; // comment\n"
2485                "case 1: break;  // comment\n"
2486                "case 2: return;\n"
2487                "// comment\n"
2488                "case 3: return;\n"
2489                "// comment 1\n"
2490                "// comment 2\n"
2491                "// comment 3\n"
2492                "case 4: break; /* comment */\n"
2493                "case 5:\n"
2494                "  // comment\n"
2495                "  break;\n"
2496                "case 6: /* comment */ x = 1; break;\n"
2497                "case 7: x = /* comment */ 1; break;\n"
2498                "case 8:\n"
2499                "  x = 1; /* comment */\n"
2500                "  break;\n"
2501                "case 9:\n"
2502                "  break; // comment line 1\n"
2503                "         // comment line 2\n"
2504                "}",
2505                Style);
2506   EXPECT_EQ("switch (a) {\n"
2507             "case 1:\n"
2508             "  x = 8;\n"
2509             "  // fall through\n"
2510             "case 2: x = 8;\n"
2511             "// comment\n"
2512             "case 3:\n"
2513             "  return; /* comment line 1\n"
2514             "           * comment line 2 */\n"
2515             "case 4: i = 8;\n"
2516             "// something else\n"
2517             "#if FOO\n"
2518             "case 5: break;\n"
2519             "#endif\n"
2520             "}",
2521             format("switch (a) {\n"
2522                    "case 1: x = 8;\n"
2523                    "  // fall through\n"
2524                    "case 2:\n"
2525                    "  x = 8;\n"
2526                    "// comment\n"
2527                    "case 3:\n"
2528                    "  return; /* comment line 1\n"
2529                    "           * comment line 2 */\n"
2530                    "case 4:\n"
2531                    "  i = 8;\n"
2532                    "// something else\n"
2533                    "#if FOO\n"
2534                    "case 5: break;\n"
2535                    "#endif\n"
2536                    "}",
2537                    Style));
2538   EXPECT_EQ("switch (a) {\n"
2539             "case 0:\n"
2540             "  return; // long long long long long long long long long long "
2541             "long long comment\n"
2542             "          // line\n"
2543             "}",
2544             format("switch (a) {\n"
2545                    "case 0: return; // long long long long long long long long "
2546                    "long long long long comment line\n"
2547                    "}",
2548                    Style));
2549   EXPECT_EQ("switch (a) {\n"
2550             "case 0:\n"
2551             "  return; /* long long long long long long long long long long "
2552             "long long comment\n"
2553             "             line */\n"
2554             "}",
2555             format("switch (a) {\n"
2556                    "case 0: return; /* long long long long long long long long "
2557                    "long long long long comment line */\n"
2558                    "}",
2559                    Style));
2560   verifyFormat("switch (a) {\n"
2561                "#if FOO\n"
2562                "case 0: return 0;\n"
2563                "#endif\n"
2564                "}",
2565                Style);
2566   verifyFormat("switch (a) {\n"
2567                "case 1: {\n"
2568                "}\n"
2569                "case 2: {\n"
2570                "  return;\n"
2571                "}\n"
2572                "case 3: {\n"
2573                "  x = 1;\n"
2574                "  return;\n"
2575                "}\n"
2576                "case 4:\n"
2577                "  if (x)\n"
2578                "    return;\n"
2579                "}",
2580                Style);
2581   Style.ColumnLimit = 21;
2582   verifyFormat("switch (a) {\n"
2583                "case 1: x = 1; break;\n"
2584                "case 2: return;\n"
2585                "case 3:\n"
2586                "case 4:\n"
2587                "case 5: return;\n"
2588                "default:\n"
2589                "  y = 1;\n"
2590                "  break;\n"
2591                "}",
2592                Style);
2593   Style.ColumnLimit = 80;
2594   Style.AllowShortCaseLabelsOnASingleLine = false;
2595   Style.IndentCaseLabels = true;
2596   EXPECT_EQ("switch (n) {\n"
2597             "  default /*comments*/:\n"
2598             "    return true;\n"
2599             "  case 0:\n"
2600             "    return false;\n"
2601             "}",
2602             format("switch (n) {\n"
2603                    "default/*comments*/:\n"
2604                    "  return true;\n"
2605                    "case 0:\n"
2606                    "  return false;\n"
2607                    "}",
2608                    Style));
2609   Style.AllowShortCaseLabelsOnASingleLine = true;
2610   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2611   Style.BraceWrapping.AfterCaseLabel = true;
2612   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2613   EXPECT_EQ("switch (n)\n"
2614             "{\n"
2615             "  case 0:\n"
2616             "  {\n"
2617             "    return false;\n"
2618             "  }\n"
2619             "  default:\n"
2620             "  {\n"
2621             "    return true;\n"
2622             "  }\n"
2623             "}",
2624             format("switch (n) {\n"
2625                    "  case 0: {\n"
2626                    "    return false;\n"
2627                    "  }\n"
2628                    "  default:\n"
2629                    "  {\n"
2630                    "    return true;\n"
2631                    "  }\n"
2632                    "}",
2633                    Style));
2634 }
2635 
2636 TEST_F(FormatTest, FormatsLabels) {
2637   verifyFormat("void f() {\n"
2638                "  some_code();\n"
2639                "test_label:\n"
2640                "  some_other_code();\n"
2641                "  {\n"
2642                "    some_more_code();\n"
2643                "  another_label:\n"
2644                "    some_more_code();\n"
2645                "  }\n"
2646                "}");
2647   verifyFormat("{\n"
2648                "  some_code();\n"
2649                "test_label:\n"
2650                "  some_other_code();\n"
2651                "}");
2652   verifyFormat("{\n"
2653                "  some_code();\n"
2654                "test_label:;\n"
2655                "  int i = 0;\n"
2656                "}");
2657   FormatStyle Style = getLLVMStyle();
2658   Style.IndentGotoLabels = false;
2659   verifyFormat("void f() {\n"
2660                "  some_code();\n"
2661                "test_label:\n"
2662                "  some_other_code();\n"
2663                "  {\n"
2664                "    some_more_code();\n"
2665                "another_label:\n"
2666                "    some_more_code();\n"
2667                "  }\n"
2668                "}",
2669                Style);
2670   verifyFormat("{\n"
2671                "  some_code();\n"
2672                "test_label:\n"
2673                "  some_other_code();\n"
2674                "}",
2675                Style);
2676   verifyFormat("{\n"
2677                "  some_code();\n"
2678                "test_label:;\n"
2679                "  int i = 0;\n"
2680                "}");
2681 }
2682 
2683 TEST_F(FormatTest, MultiLineControlStatements) {
2684   FormatStyle Style = getLLVMStyle();
2685   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2686   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2687   Style.ColumnLimit = 20;
2688   // Short lines should keep opening brace on same line.
2689   EXPECT_EQ("if (foo) {\n"
2690             "  bar();\n"
2691             "}",
2692             format("if(foo){bar();}", Style));
2693   EXPECT_EQ("if (foo) {\n"
2694             "  bar();\n"
2695             "} else {\n"
2696             "  baz();\n"
2697             "}",
2698             format("if(foo){bar();}else{baz();}", Style));
2699   EXPECT_EQ("if (foo && bar) {\n"
2700             "  baz();\n"
2701             "}",
2702             format("if(foo&&bar){baz();}", Style));
2703   EXPECT_EQ("if (foo) {\n"
2704             "  bar();\n"
2705             "} else if (baz) {\n"
2706             "  quux();\n"
2707             "}",
2708             format("if(foo){bar();}else if(baz){quux();}", Style));
2709   EXPECT_EQ(
2710       "if (foo) {\n"
2711       "  bar();\n"
2712       "} else if (baz) {\n"
2713       "  quux();\n"
2714       "} else {\n"
2715       "  foobar();\n"
2716       "}",
2717       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2718   EXPECT_EQ("for (;;) {\n"
2719             "  foo();\n"
2720             "}",
2721             format("for(;;){foo();}"));
2722   EXPECT_EQ("while (1) {\n"
2723             "  foo();\n"
2724             "}",
2725             format("while(1){foo();}", Style));
2726   EXPECT_EQ("switch (foo) {\n"
2727             "case bar:\n"
2728             "  return;\n"
2729             "}",
2730             format("switch(foo){case bar:return;}", Style));
2731   EXPECT_EQ("try {\n"
2732             "  foo();\n"
2733             "} catch (...) {\n"
2734             "  bar();\n"
2735             "}",
2736             format("try{foo();}catch(...){bar();}", Style));
2737   EXPECT_EQ("do {\n"
2738             "  foo();\n"
2739             "} while (bar &&\n"
2740             "         baz);",
2741             format("do{foo();}while(bar&&baz);", Style));
2742   // Long lines should put opening brace on new line.
2743   EXPECT_EQ("if (foo && bar &&\n"
2744             "    baz)\n"
2745             "{\n"
2746             "  quux();\n"
2747             "}",
2748             format("if(foo&&bar&&baz){quux();}", Style));
2749   EXPECT_EQ("if (foo && bar &&\n"
2750             "    baz)\n"
2751             "{\n"
2752             "  quux();\n"
2753             "}",
2754             format("if (foo && bar &&\n"
2755                    "    baz) {\n"
2756                    "  quux();\n"
2757                    "}",
2758                    Style));
2759   EXPECT_EQ("if (foo) {\n"
2760             "  bar();\n"
2761             "} else if (baz ||\n"
2762             "           quux)\n"
2763             "{\n"
2764             "  foobar();\n"
2765             "}",
2766             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2767   EXPECT_EQ(
2768       "if (foo) {\n"
2769       "  bar();\n"
2770       "} else if (baz ||\n"
2771       "           quux)\n"
2772       "{\n"
2773       "  foobar();\n"
2774       "} else {\n"
2775       "  barbaz();\n"
2776       "}",
2777       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2778              Style));
2779   EXPECT_EQ("for (int i = 0;\n"
2780             "     i < 10; ++i)\n"
2781             "{\n"
2782             "  foo();\n"
2783             "}",
2784             format("for(int i=0;i<10;++i){foo();}", Style));
2785   EXPECT_EQ("foreach (int i,\n"
2786             "         list)\n"
2787             "{\n"
2788             "  foo();\n"
2789             "}",
2790             format("foreach(int i, list){foo();}", Style));
2791   Style.ColumnLimit =
2792       40; // to concentrate at brace wrapping, not line wrap due to column limit
2793   EXPECT_EQ("foreach (int i, list) {\n"
2794             "  foo();\n"
2795             "}",
2796             format("foreach(int i, list){foo();}", Style));
2797   Style.ColumnLimit =
2798       20; // to concentrate at brace wrapping, not line wrap due to column limit
2799   EXPECT_EQ("while (foo || bar ||\n"
2800             "       baz)\n"
2801             "{\n"
2802             "  quux();\n"
2803             "}",
2804             format("while(foo||bar||baz){quux();}", Style));
2805   EXPECT_EQ("switch (\n"
2806             "    foo = barbaz)\n"
2807             "{\n"
2808             "case quux:\n"
2809             "  return;\n"
2810             "}",
2811             format("switch(foo=barbaz){case quux:return;}", Style));
2812   EXPECT_EQ("try {\n"
2813             "  foo();\n"
2814             "} catch (\n"
2815             "    Exception &bar)\n"
2816             "{\n"
2817             "  baz();\n"
2818             "}",
2819             format("try{foo();}catch(Exception&bar){baz();}", Style));
2820   Style.ColumnLimit =
2821       40; // to concentrate at brace wrapping, not line wrap due to column limit
2822   EXPECT_EQ("try {\n"
2823             "  foo();\n"
2824             "} catch (Exception &bar) {\n"
2825             "  baz();\n"
2826             "}",
2827             format("try{foo();}catch(Exception&bar){baz();}", Style));
2828   Style.ColumnLimit =
2829       20; // to concentrate at brace wrapping, not line wrap due to column limit
2830 
2831   Style.BraceWrapping.BeforeElse = true;
2832   EXPECT_EQ(
2833       "if (foo) {\n"
2834       "  bar();\n"
2835       "}\n"
2836       "else if (baz ||\n"
2837       "         quux)\n"
2838       "{\n"
2839       "  foobar();\n"
2840       "}\n"
2841       "else {\n"
2842       "  barbaz();\n"
2843       "}",
2844       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2845              Style));
2846 
2847   Style.BraceWrapping.BeforeCatch = true;
2848   EXPECT_EQ("try {\n"
2849             "  foo();\n"
2850             "}\n"
2851             "catch (...) {\n"
2852             "  baz();\n"
2853             "}",
2854             format("try{foo();}catch(...){baz();}", Style));
2855 }
2856 
2857 TEST_F(FormatTest, BeforeWhile) {
2858   FormatStyle Style = getLLVMStyle();
2859   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2860 
2861   verifyFormat("do {\n"
2862                "  foo();\n"
2863                "} while (1);",
2864                Style);
2865   Style.BraceWrapping.BeforeWhile = true;
2866   verifyFormat("do {\n"
2867                "  foo();\n"
2868                "}\n"
2869                "while (1);",
2870                Style);
2871 }
2872 
2873 //===----------------------------------------------------------------------===//
2874 // Tests for classes, namespaces, etc.
2875 //===----------------------------------------------------------------------===//
2876 
2877 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
2878   verifyFormat("class A {};");
2879 }
2880 
2881 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
2882   verifyFormat("class A {\n"
2883                "public:\n"
2884                "public: // comment\n"
2885                "protected:\n"
2886                "private:\n"
2887                "  void f() {}\n"
2888                "};");
2889   verifyFormat("export class A {\n"
2890                "public:\n"
2891                "public: // comment\n"
2892                "protected:\n"
2893                "private:\n"
2894                "  void f() {}\n"
2895                "};");
2896   verifyGoogleFormat("class A {\n"
2897                      " public:\n"
2898                      " protected:\n"
2899                      " private:\n"
2900                      "  void f() {}\n"
2901                      "};");
2902   verifyGoogleFormat("export class A {\n"
2903                      " public:\n"
2904                      " protected:\n"
2905                      " private:\n"
2906                      "  void f() {}\n"
2907                      "};");
2908   verifyFormat("class A {\n"
2909                "public slots:\n"
2910                "  void f1() {}\n"
2911                "public Q_SLOTS:\n"
2912                "  void f2() {}\n"
2913                "protected slots:\n"
2914                "  void f3() {}\n"
2915                "protected Q_SLOTS:\n"
2916                "  void f4() {}\n"
2917                "private slots:\n"
2918                "  void f5() {}\n"
2919                "private Q_SLOTS:\n"
2920                "  void f6() {}\n"
2921                "signals:\n"
2922                "  void g1();\n"
2923                "Q_SIGNALS:\n"
2924                "  void g2();\n"
2925                "};");
2926 
2927   // Don't interpret 'signals' the wrong way.
2928   verifyFormat("signals.set();");
2929   verifyFormat("for (Signals signals : f()) {\n}");
2930   verifyFormat("{\n"
2931                "  signals.set(); // This needs indentation.\n"
2932                "}");
2933   verifyFormat("void f() {\n"
2934                "label:\n"
2935                "  signals.baz();\n"
2936                "}");
2937 }
2938 
2939 TEST_F(FormatTest, SeparatesLogicalBlocks) {
2940   EXPECT_EQ("class A {\n"
2941             "public:\n"
2942             "  void f();\n"
2943             "\n"
2944             "private:\n"
2945             "  void g() {}\n"
2946             "  // test\n"
2947             "protected:\n"
2948             "  int h;\n"
2949             "};",
2950             format("class A {\n"
2951                    "public:\n"
2952                    "void f();\n"
2953                    "private:\n"
2954                    "void g() {}\n"
2955                    "// test\n"
2956                    "protected:\n"
2957                    "int h;\n"
2958                    "};"));
2959   EXPECT_EQ("class A {\n"
2960             "protected:\n"
2961             "public:\n"
2962             "  void f();\n"
2963             "};",
2964             format("class A {\n"
2965                    "protected:\n"
2966                    "\n"
2967                    "public:\n"
2968                    "\n"
2969                    "  void f();\n"
2970                    "};"));
2971 
2972   // Even ensure proper spacing inside macros.
2973   EXPECT_EQ("#define B     \\\n"
2974             "  class A {   \\\n"
2975             "   protected: \\\n"
2976             "   public:    \\\n"
2977             "    void f(); \\\n"
2978             "  };",
2979             format("#define B     \\\n"
2980                    "  class A {   \\\n"
2981                    "   protected: \\\n"
2982                    "              \\\n"
2983                    "   public:    \\\n"
2984                    "              \\\n"
2985                    "    void f(); \\\n"
2986                    "  };",
2987                    getGoogleStyle()));
2988   // But don't remove empty lines after macros ending in access specifiers.
2989   EXPECT_EQ("#define A private:\n"
2990             "\n"
2991             "int i;",
2992             format("#define A         private:\n"
2993                    "\n"
2994                    "int              i;"));
2995 }
2996 
2997 TEST_F(FormatTest, FormatsClasses) {
2998   verifyFormat("class A : public B {};");
2999   verifyFormat("class A : public ::B {};");
3000 
3001   verifyFormat(
3002       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3003       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3004   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3005                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3006                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3007   verifyFormat(
3008       "class A : public B, public C, public D, public E, public F {};");
3009   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3010                "                     public C,\n"
3011                "                     public D,\n"
3012                "                     public E,\n"
3013                "                     public F,\n"
3014                "                     public G {};");
3015 
3016   verifyFormat("class\n"
3017                "    ReallyReallyLongClassName {\n"
3018                "  int i;\n"
3019                "};",
3020                getLLVMStyleWithColumns(32));
3021   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3022                "                           aaaaaaaaaaaaaaaa> {};");
3023   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3024                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3025                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3026   verifyFormat("template <class R, class C>\n"
3027                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3028                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3029   verifyFormat("class ::A::B {};");
3030 }
3031 
3032 TEST_F(FormatTest, BreakInheritanceStyle) {
3033   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3034   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3035       FormatStyle::BILS_BeforeComma;
3036   verifyFormat("class MyClass : public X {};",
3037                StyleWithInheritanceBreakBeforeComma);
3038   verifyFormat("class MyClass\n"
3039                "    : public X\n"
3040                "    , public Y {};",
3041                StyleWithInheritanceBreakBeforeComma);
3042   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3043                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3044                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3045                StyleWithInheritanceBreakBeforeComma);
3046   verifyFormat("struct aaaaaaaaaaaaa\n"
3047                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3048                "          aaaaaaaaaaaaaaaa> {};",
3049                StyleWithInheritanceBreakBeforeComma);
3050 
3051   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3052   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3053       FormatStyle::BILS_AfterColon;
3054   verifyFormat("class MyClass : public X {};",
3055                StyleWithInheritanceBreakAfterColon);
3056   verifyFormat("class MyClass : public X, public Y {};",
3057                StyleWithInheritanceBreakAfterColon);
3058   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3059                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3060                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3061                StyleWithInheritanceBreakAfterColon);
3062   verifyFormat("struct aaaaaaaaaaaaa :\n"
3063                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3064                "        aaaaaaaaaaaaaaaa> {};",
3065                StyleWithInheritanceBreakAfterColon);
3066 
3067   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3068   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3069       FormatStyle::BILS_AfterComma;
3070   verifyFormat("class MyClass : public X {};",
3071                StyleWithInheritanceBreakAfterComma);
3072   verifyFormat("class MyClass : public X,\n"
3073                "                public Y {};",
3074                StyleWithInheritanceBreakAfterComma);
3075   verifyFormat(
3076       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3077       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3078       "{};",
3079       StyleWithInheritanceBreakAfterComma);
3080   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3081                "                           aaaaaaaaaaaaaaaa> {};",
3082                StyleWithInheritanceBreakAfterComma);
3083   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3084                "    : public OnceBreak,\n"
3085                "      public AlwaysBreak,\n"
3086                "      EvenBasesFitInOneLine {};",
3087                StyleWithInheritanceBreakAfterComma);
3088 }
3089 
3090 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3091   verifyFormat("class A {\n} a, b;");
3092   verifyFormat("struct A {\n} a, b;");
3093   verifyFormat("union A {\n} a;");
3094 }
3095 
3096 TEST_F(FormatTest, FormatsEnum) {
3097   verifyFormat("enum {\n"
3098                "  Zero,\n"
3099                "  One = 1,\n"
3100                "  Two = One + 1,\n"
3101                "  Three = (One + Two),\n"
3102                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3103                "  Five = (One, Two, Three, Four, 5)\n"
3104                "};");
3105   verifyGoogleFormat("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   verifyFormat("enum Enum {};");
3114   verifyFormat("enum {};");
3115   verifyFormat("enum X E {} d;");
3116   verifyFormat("enum __attribute__((...)) E {} d;");
3117   verifyFormat("enum __declspec__((...)) E {} d;");
3118   verifyFormat("enum {\n"
3119                "  Bar = Foo<int, int>::value\n"
3120                "};",
3121                getLLVMStyleWithColumns(30));
3122 
3123   verifyFormat("enum ShortEnum { A, B, C };");
3124   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3125 
3126   EXPECT_EQ("enum KeepEmptyLines {\n"
3127             "  ONE,\n"
3128             "\n"
3129             "  TWO,\n"
3130             "\n"
3131             "  THREE\n"
3132             "}",
3133             format("enum KeepEmptyLines {\n"
3134                    "  ONE,\n"
3135                    "\n"
3136                    "  TWO,\n"
3137                    "\n"
3138                    "\n"
3139                    "  THREE\n"
3140                    "}"));
3141   verifyFormat("enum E { // comment\n"
3142                "  ONE,\n"
3143                "  TWO\n"
3144                "};\n"
3145                "int i;");
3146 
3147   FormatStyle EightIndent = getLLVMStyle();
3148   EightIndent.IndentWidth = 8;
3149   verifyFormat("enum {\n"
3150                "        VOID,\n"
3151                "        CHAR,\n"
3152                "        SHORT,\n"
3153                "        INT,\n"
3154                "        LONG,\n"
3155                "        SIGNED,\n"
3156                "        UNSIGNED,\n"
3157                "        BOOL,\n"
3158                "        FLOAT,\n"
3159                "        DOUBLE,\n"
3160                "        COMPLEX\n"
3161                "};",
3162                EightIndent);
3163 
3164   // Not enums.
3165   verifyFormat("enum X f() {\n"
3166                "  a();\n"
3167                "  return 42;\n"
3168                "}");
3169   verifyFormat("enum X Type::f() {\n"
3170                "  a();\n"
3171                "  return 42;\n"
3172                "}");
3173   verifyFormat("enum ::X f() {\n"
3174                "  a();\n"
3175                "  return 42;\n"
3176                "}");
3177   verifyFormat("enum ns::X f() {\n"
3178                "  a();\n"
3179                "  return 42;\n"
3180                "}");
3181 }
3182 
3183 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3184   verifyFormat("enum Type {\n"
3185                "  One = 0; // These semicolons should be commas.\n"
3186                "  Two = 1;\n"
3187                "};");
3188   verifyFormat("namespace n {\n"
3189                "enum Type {\n"
3190                "  One,\n"
3191                "  Two, // missing };\n"
3192                "  int i;\n"
3193                "}\n"
3194                "void g() {}");
3195 }
3196 
3197 TEST_F(FormatTest, FormatsEnumStruct) {
3198   verifyFormat("enum struct {\n"
3199                "  Zero,\n"
3200                "  One = 1,\n"
3201                "  Two = One + 1,\n"
3202                "  Three = (One + Two),\n"
3203                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3204                "  Five = (One, Two, Three, Four, 5)\n"
3205                "};");
3206   verifyFormat("enum struct Enum {};");
3207   verifyFormat("enum struct {};");
3208   verifyFormat("enum struct X E {} d;");
3209   verifyFormat("enum struct __attribute__((...)) E {} d;");
3210   verifyFormat("enum struct __declspec__((...)) E {} d;");
3211   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3212 }
3213 
3214 TEST_F(FormatTest, FormatsEnumClass) {
3215   verifyFormat("enum class {\n"
3216                "  Zero,\n"
3217                "  One = 1,\n"
3218                "  Two = One + 1,\n"
3219                "  Three = (One + Two),\n"
3220                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3221                "  Five = (One, Two, Three, Four, 5)\n"
3222                "};");
3223   verifyFormat("enum class Enum {};");
3224   verifyFormat("enum class {};");
3225   verifyFormat("enum class X E {} d;");
3226   verifyFormat("enum class __attribute__((...)) E {} d;");
3227   verifyFormat("enum class __declspec__((...)) E {} d;");
3228   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3229 }
3230 
3231 TEST_F(FormatTest, FormatsEnumTypes) {
3232   verifyFormat("enum X : int {\n"
3233                "  A, // Force multiple lines.\n"
3234                "  B\n"
3235                "};");
3236   verifyFormat("enum X : int { A, B };");
3237   verifyFormat("enum X : std::uint32_t { A, B };");
3238 }
3239 
3240 TEST_F(FormatTest, FormatsTypedefEnum) {
3241   FormatStyle Style = getLLVMStyle();
3242   Style.ColumnLimit = 40;
3243   verifyFormat("typedef enum {} EmptyEnum;");
3244   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3245   verifyFormat("typedef enum {\n"
3246                "  ZERO = 0,\n"
3247                "  ONE = 1,\n"
3248                "  TWO = 2,\n"
3249                "  THREE = 3\n"
3250                "} LongEnum;",
3251                Style);
3252   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3253   Style.BraceWrapping.AfterEnum = true;
3254   verifyFormat("typedef enum {} EmptyEnum;");
3255   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3256   verifyFormat("typedef enum\n"
3257                "{\n"
3258                "  ZERO = 0,\n"
3259                "  ONE = 1,\n"
3260                "  TWO = 2,\n"
3261                "  THREE = 3\n"
3262                "} LongEnum;",
3263                Style);
3264 }
3265 
3266 TEST_F(FormatTest, FormatsNSEnums) {
3267   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3268   verifyGoogleFormat(
3269       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3270   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3271                      "  // Information about someDecentlyLongValue.\n"
3272                      "  someDecentlyLongValue,\n"
3273                      "  // Information about anotherDecentlyLongValue.\n"
3274                      "  anotherDecentlyLongValue,\n"
3275                      "  // Information about aThirdDecentlyLongValue.\n"
3276                      "  aThirdDecentlyLongValue\n"
3277                      "};");
3278   verifyGoogleFormat("typedef NS_CLOSED_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_OPTIONS(NSInteger, MyType) {\n"
3287                      "  a = 1,\n"
3288                      "  b = 2,\n"
3289                      "  c = 3,\n"
3290                      "};");
3291   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3292                      "  a = 1,\n"
3293                      "  b = 2,\n"
3294                      "  c = 3,\n"
3295                      "};");
3296   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3297                      "  a = 1,\n"
3298                      "  b = 2,\n"
3299                      "  c = 3,\n"
3300                      "};");
3301   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3302                      "  a = 1,\n"
3303                      "  b = 2,\n"
3304                      "  c = 3,\n"
3305                      "};");
3306 }
3307 
3308 TEST_F(FormatTest, FormatsBitfields) {
3309   verifyFormat("struct Bitfields {\n"
3310                "  unsigned sClass : 8;\n"
3311                "  unsigned ValueKind : 2;\n"
3312                "};");
3313   verifyFormat("struct A {\n"
3314                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3315                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3316                "};");
3317   verifyFormat("struct MyStruct {\n"
3318                "  uchar data;\n"
3319                "  uchar : 8;\n"
3320                "  uchar : 8;\n"
3321                "  uchar other;\n"
3322                "};");
3323   FormatStyle Style = getLLVMStyle();
3324   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3325   verifyFormat("struct Bitfields {\n"
3326                "  unsigned sClass:8;\n"
3327                "  unsigned ValueKind:2;\n"
3328                "  uchar other;\n"
3329                "};",
3330                Style);
3331   verifyFormat("struct A {\n"
3332                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3333                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3334                "};",
3335                Style);
3336   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3337   verifyFormat("struct Bitfields {\n"
3338                "  unsigned sClass :8;\n"
3339                "  unsigned ValueKind :2;\n"
3340                "  uchar other;\n"
3341                "};",
3342                Style);
3343   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3344   verifyFormat("struct Bitfields {\n"
3345                "  unsigned sClass: 8;\n"
3346                "  unsigned ValueKind: 2;\n"
3347                "  uchar other;\n"
3348                "};",
3349                Style);
3350 }
3351 
3352 TEST_F(FormatTest, FormatsNamespaces) {
3353   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3354   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3355 
3356   verifyFormat("namespace some_namespace {\n"
3357                "class A {};\n"
3358                "void f() { f(); }\n"
3359                "}",
3360                LLVMWithNoNamespaceFix);
3361   verifyFormat("namespace N::inline D {\n"
3362                "class A {};\n"
3363                "void f() { f(); }\n"
3364                "}",
3365                LLVMWithNoNamespaceFix);
3366   verifyFormat("namespace N::inline D::E {\n"
3367                "class A {};\n"
3368                "void f() { f(); }\n"
3369                "}",
3370                LLVMWithNoNamespaceFix);
3371   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3372                "class A {};\n"
3373                "void f() { f(); }\n"
3374                "}",
3375                LLVMWithNoNamespaceFix);
3376   verifyFormat("/* something */ namespace some_namespace {\n"
3377                "class A {};\n"
3378                "void f() { f(); }\n"
3379                "}",
3380                LLVMWithNoNamespaceFix);
3381   verifyFormat("namespace {\n"
3382                "class A {};\n"
3383                "void f() { f(); }\n"
3384                "}",
3385                LLVMWithNoNamespaceFix);
3386   verifyFormat("/* something */ namespace {\n"
3387                "class A {};\n"
3388                "void f() { f(); }\n"
3389                "}",
3390                LLVMWithNoNamespaceFix);
3391   verifyFormat("inline namespace X {\n"
3392                "class A {};\n"
3393                "void f() { f(); }\n"
3394                "}",
3395                LLVMWithNoNamespaceFix);
3396   verifyFormat("/* something */ inline namespace X {\n"
3397                "class A {};\n"
3398                "void f() { f(); }\n"
3399                "}",
3400                LLVMWithNoNamespaceFix);
3401   verifyFormat("export namespace X {\n"
3402                "class A {};\n"
3403                "void f() { f(); }\n"
3404                "}",
3405                LLVMWithNoNamespaceFix);
3406   verifyFormat("using namespace some_namespace;\n"
3407                "class A {};\n"
3408                "void f() { f(); }",
3409                LLVMWithNoNamespaceFix);
3410 
3411   // This code is more common than we thought; if we
3412   // layout this correctly the semicolon will go into
3413   // its own line, which is undesirable.
3414   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3415   verifyFormat("namespace {\n"
3416                "class A {};\n"
3417                "};",
3418                LLVMWithNoNamespaceFix);
3419 
3420   verifyFormat("namespace {\n"
3421                "int SomeVariable = 0; // comment\n"
3422                "} // namespace",
3423                LLVMWithNoNamespaceFix);
3424   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3425             "#define HEADER_GUARD\n"
3426             "namespace my_namespace {\n"
3427             "int i;\n"
3428             "} // my_namespace\n"
3429             "#endif // HEADER_GUARD",
3430             format("#ifndef HEADER_GUARD\n"
3431                    " #define HEADER_GUARD\n"
3432                    "   namespace my_namespace {\n"
3433                    "int i;\n"
3434                    "}    // my_namespace\n"
3435                    "#endif    // HEADER_GUARD",
3436                    LLVMWithNoNamespaceFix));
3437 
3438   EXPECT_EQ("namespace A::B {\n"
3439             "class C {};\n"
3440             "}",
3441             format("namespace A::B {\n"
3442                    "class C {};\n"
3443                    "}",
3444                    LLVMWithNoNamespaceFix));
3445 
3446   FormatStyle Style = getLLVMStyle();
3447   Style.NamespaceIndentation = FormatStyle::NI_All;
3448   EXPECT_EQ("namespace out {\n"
3449             "  int i;\n"
3450             "  namespace in {\n"
3451             "    int i;\n"
3452             "  } // namespace in\n"
3453             "} // namespace out",
3454             format("namespace out {\n"
3455                    "int i;\n"
3456                    "namespace in {\n"
3457                    "int i;\n"
3458                    "} // namespace in\n"
3459                    "} // namespace out",
3460                    Style));
3461 
3462   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3463   EXPECT_EQ("namespace out {\n"
3464             "int i;\n"
3465             "namespace in {\n"
3466             "  int i;\n"
3467             "} // namespace in\n"
3468             "} // namespace out",
3469             format("namespace out {\n"
3470                    "int i;\n"
3471                    "namespace in {\n"
3472                    "int i;\n"
3473                    "} // namespace in\n"
3474                    "} // namespace out",
3475                    Style));
3476 }
3477 
3478 TEST_F(FormatTest, NamespaceMacros) {
3479   FormatStyle Style = getLLVMStyle();
3480   Style.NamespaceMacros.push_back("TESTSUITE");
3481 
3482   verifyFormat("TESTSUITE(A) {\n"
3483                "int foo();\n"
3484                "} // TESTSUITE(A)",
3485                Style);
3486 
3487   verifyFormat("TESTSUITE(A, B) {\n"
3488                "int foo();\n"
3489                "} // TESTSUITE(A)",
3490                Style);
3491 
3492   // Properly indent according to NamespaceIndentation style
3493   Style.NamespaceIndentation = FormatStyle::NI_All;
3494   verifyFormat("TESTSUITE(A) {\n"
3495                "  int foo();\n"
3496                "} // TESTSUITE(A)",
3497                Style);
3498   verifyFormat("TESTSUITE(A) {\n"
3499                "  namespace B {\n"
3500                "    int foo();\n"
3501                "  } // namespace B\n"
3502                "} // TESTSUITE(A)",
3503                Style);
3504   verifyFormat("namespace A {\n"
3505                "  TESTSUITE(B) {\n"
3506                "    int foo();\n"
3507                "  } // TESTSUITE(B)\n"
3508                "} // namespace A",
3509                Style);
3510 
3511   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3512   verifyFormat("TESTSUITE(A) {\n"
3513                "TESTSUITE(B) {\n"
3514                "  int foo();\n"
3515                "} // TESTSUITE(B)\n"
3516                "} // TESTSUITE(A)",
3517                Style);
3518   verifyFormat("TESTSUITE(A) {\n"
3519                "namespace B {\n"
3520                "  int foo();\n"
3521                "} // namespace B\n"
3522                "} // TESTSUITE(A)",
3523                Style);
3524   verifyFormat("namespace A {\n"
3525                "TESTSUITE(B) {\n"
3526                "  int foo();\n"
3527                "} // TESTSUITE(B)\n"
3528                "} // namespace A",
3529                Style);
3530 
3531   // Properly merge namespace-macros blocks in CompactNamespaces mode
3532   Style.NamespaceIndentation = FormatStyle::NI_None;
3533   Style.CompactNamespaces = true;
3534   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3535                "}} // TESTSUITE(A::B)",
3536                Style);
3537 
3538   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3539             "}} // TESTSUITE(out::in)",
3540             format("TESTSUITE(out) {\n"
3541                    "TESTSUITE(in) {\n"
3542                    "} // TESTSUITE(in)\n"
3543                    "} // TESTSUITE(out)",
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   // Do not merge different namespaces/macros
3555   EXPECT_EQ("namespace out {\n"
3556             "TESTSUITE(in) {\n"
3557             "} // TESTSUITE(in)\n"
3558             "} // namespace out",
3559             format("namespace out {\n"
3560                    "TESTSUITE(in) {\n"
3561                    "} // TESTSUITE(in)\n"
3562                    "} // namespace out",
3563                    Style));
3564   EXPECT_EQ("TESTSUITE(out) {\n"
3565             "namespace in {\n"
3566             "} // namespace in\n"
3567             "} // TESTSUITE(out)",
3568             format("TESTSUITE(out) {\n"
3569                    "namespace in {\n"
3570                    "} // namespace in\n"
3571                    "} // TESTSUITE(out)",
3572                    Style));
3573   Style.NamespaceMacros.push_back("FOOBAR");
3574   EXPECT_EQ("TESTSUITE(out) {\n"
3575             "FOOBAR(in) {\n"
3576             "} // FOOBAR(in)\n"
3577             "} // TESTSUITE(out)",
3578             format("TESTSUITE(out) {\n"
3579                    "FOOBAR(in) {\n"
3580                    "} // FOOBAR(in)\n"
3581                    "} // TESTSUITE(out)",
3582                    Style));
3583 }
3584 
3585 TEST_F(FormatTest, FormatsCompactNamespaces) {
3586   FormatStyle Style = getLLVMStyle();
3587   Style.CompactNamespaces = true;
3588   Style.NamespaceMacros.push_back("TESTSUITE");
3589 
3590   verifyFormat("namespace A { namespace B {\n"
3591                "}} // namespace A::B",
3592                Style);
3593 
3594   EXPECT_EQ("namespace out { namespace in {\n"
3595             "}} // namespace out::in",
3596             format("namespace out {\n"
3597                    "namespace in {\n"
3598                    "} // namespace in\n"
3599                    "} // namespace out",
3600                    Style));
3601 
3602   // Only namespaces which have both consecutive opening and end get compacted
3603   EXPECT_EQ("namespace out {\n"
3604             "namespace in1 {\n"
3605             "} // namespace in1\n"
3606             "namespace in2 {\n"
3607             "} // namespace in2\n"
3608             "} // namespace out",
3609             format("namespace out {\n"
3610                    "namespace in1 {\n"
3611                    "} // namespace in1\n"
3612                    "namespace in2 {\n"
3613                    "} // namespace in2\n"
3614                    "} // namespace out",
3615                    Style));
3616 
3617   EXPECT_EQ("namespace out {\n"
3618             "int i;\n"
3619             "namespace in {\n"
3620             "int j;\n"
3621             "} // namespace in\n"
3622             "int k;\n"
3623             "} // namespace out",
3624             format("namespace out { int i;\n"
3625                    "namespace in { int j; } // namespace in\n"
3626                    "int k; } // namespace out",
3627                    Style));
3628 
3629   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3630             "}}} // namespace A::B::C\n",
3631             format("namespace A { namespace B {\n"
3632                    "namespace C {\n"
3633                    "}} // namespace B::C\n"
3634                    "} // namespace A\n",
3635                    Style));
3636 
3637   Style.ColumnLimit = 40;
3638   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3639             "namespace bbbbbbbbbb {\n"
3640             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3641             format("namespace aaaaaaaaaa {\n"
3642                    "namespace bbbbbbbbbb {\n"
3643                    "} // namespace bbbbbbbbbb\n"
3644                    "} // namespace aaaaaaaaaa",
3645                    Style));
3646 
3647   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3648             "namespace cccccc {\n"
3649             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3650             format("namespace aaaaaa {\n"
3651                    "namespace bbbbbb {\n"
3652                    "namespace cccccc {\n"
3653                    "} // namespace cccccc\n"
3654                    "} // namespace bbbbbb\n"
3655                    "} // namespace aaaaaa",
3656                    Style));
3657   Style.ColumnLimit = 80;
3658 
3659   // Extra semicolon after 'inner' closing brace prevents merging
3660   EXPECT_EQ("namespace out { namespace in {\n"
3661             "}; } // namespace out::in",
3662             format("namespace out {\n"
3663                    "namespace in {\n"
3664                    "}; // namespace in\n"
3665                    "} // namespace out",
3666                    Style));
3667 
3668   // Extra semicolon after 'outer' closing brace is conserved
3669   EXPECT_EQ("namespace out { namespace in {\n"
3670             "}}; // namespace out::in",
3671             format("namespace out {\n"
3672                    "namespace in {\n"
3673                    "} // namespace in\n"
3674                    "}; // namespace out",
3675                    Style));
3676 
3677   Style.NamespaceIndentation = FormatStyle::NI_All;
3678   EXPECT_EQ("namespace out { namespace in {\n"
3679             "  int i;\n"
3680             "}} // namespace out::in",
3681             format("namespace out {\n"
3682                    "namespace in {\n"
3683                    "int i;\n"
3684                    "} // namespace in\n"
3685                    "} // namespace out",
3686                    Style));
3687   EXPECT_EQ("namespace out { namespace mid {\n"
3688             "  namespace in {\n"
3689             "    int j;\n"
3690             "  } // namespace in\n"
3691             "  int k;\n"
3692             "}} // namespace out::mid",
3693             format("namespace out { namespace mid {\n"
3694                    "namespace in { int j; } // namespace in\n"
3695                    "int k; }} // namespace out::mid",
3696                    Style));
3697 
3698   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3699   EXPECT_EQ("namespace out { namespace in {\n"
3700             "  int i;\n"
3701             "}} // namespace out::in",
3702             format("namespace out {\n"
3703                    "namespace in {\n"
3704                    "int i;\n"
3705                    "} // namespace in\n"
3706                    "} // namespace out",
3707                    Style));
3708   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
3709             "  int i;\n"
3710             "}}} // namespace out::mid::in",
3711             format("namespace out {\n"
3712                    "namespace mid {\n"
3713                    "namespace in {\n"
3714                    "int i;\n"
3715                    "} // namespace in\n"
3716                    "} // namespace mid\n"
3717                    "} // namespace out",
3718                    Style));
3719 }
3720 
3721 TEST_F(FormatTest, FormatsExternC) {
3722   verifyFormat("extern \"C\" {\nint a;");
3723   verifyFormat("extern \"C\" {}");
3724   verifyFormat("extern \"C\" {\n"
3725                "int foo();\n"
3726                "}");
3727   verifyFormat("extern \"C\" int foo() {}");
3728   verifyFormat("extern \"C\" int foo();");
3729   verifyFormat("extern \"C\" int foo() {\n"
3730                "  int i = 42;\n"
3731                "  return i;\n"
3732                "}");
3733 
3734   FormatStyle Style = getLLVMStyle();
3735   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3736   Style.BraceWrapping.AfterFunction = true;
3737   verifyFormat("extern \"C\" int foo() {}", Style);
3738   verifyFormat("extern \"C\" int foo();", Style);
3739   verifyFormat("extern \"C\" int foo()\n"
3740                "{\n"
3741                "  int i = 42;\n"
3742                "  return i;\n"
3743                "}",
3744                Style);
3745 
3746   Style.BraceWrapping.AfterExternBlock = true;
3747   Style.BraceWrapping.SplitEmptyRecord = false;
3748   verifyFormat("extern \"C\"\n"
3749                "{}",
3750                Style);
3751   verifyFormat("extern \"C\"\n"
3752                "{\n"
3753                "  int foo();\n"
3754                "}",
3755                Style);
3756 }
3757 
3758 TEST_F(FormatTest, IndentExternBlockStyle) {
3759   FormatStyle Style = getLLVMStyle();
3760   Style.IndentWidth = 2;
3761 
3762   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3763   verifyFormat("extern \"C\" { /*9*/\n}", Style);
3764   verifyFormat("extern \"C\" {\n"
3765                "  int foo10();\n"
3766                "}",
3767                Style);
3768 
3769   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
3770   verifyFormat("extern \"C\" { /*11*/\n}", Style);
3771   verifyFormat("extern \"C\" {\n"
3772                "int foo12();\n"
3773                "}",
3774                Style);
3775 
3776   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3777   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3778   Style.BraceWrapping.AfterExternBlock = true;
3779   verifyFormat("extern \"C\"\n{ /*13*/\n}", Style);
3780   verifyFormat("extern \"C\"\n{\n"
3781                "  int foo14();\n"
3782                "}",
3783                Style);
3784 
3785   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3786   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3787   Style.BraceWrapping.AfterExternBlock = false;
3788   verifyFormat("extern \"C\" { /*15*/\n}", Style);
3789   verifyFormat("extern \"C\" {\n"
3790                "int foo16();\n"
3791                "}",
3792                Style);
3793 }
3794 
3795 TEST_F(FormatTest, FormatsInlineASM) {
3796   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
3797   verifyFormat("asm(\"nop\" ::: \"memory\");");
3798   verifyFormat(
3799       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
3800       "    \"cpuid\\n\\t\"\n"
3801       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
3802       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
3803       "    : \"a\"(value));");
3804   EXPECT_EQ(
3805       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
3806       "  __asm {\n"
3807       "        mov     edx,[that] // vtable in edx\n"
3808       "        mov     eax,methodIndex\n"
3809       "        call    [edx][eax*4] // stdcall\n"
3810       "  }\n"
3811       "}",
3812       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
3813              "    __asm {\n"
3814              "        mov     edx,[that] // vtable in edx\n"
3815              "        mov     eax,methodIndex\n"
3816              "        call    [edx][eax*4] // stdcall\n"
3817              "    }\n"
3818              "}"));
3819   EXPECT_EQ("_asm {\n"
3820             "  xor eax, eax;\n"
3821             "  cpuid;\n"
3822             "}",
3823             format("_asm {\n"
3824                    "  xor eax, eax;\n"
3825                    "  cpuid;\n"
3826                    "}"));
3827   verifyFormat("void function() {\n"
3828                "  // comment\n"
3829                "  asm(\"\");\n"
3830                "}");
3831   EXPECT_EQ("__asm {\n"
3832             "}\n"
3833             "int i;",
3834             format("__asm   {\n"
3835                    "}\n"
3836                    "int   i;"));
3837 }
3838 
3839 TEST_F(FormatTest, FormatTryCatch) {
3840   verifyFormat("try {\n"
3841                "  throw a * b;\n"
3842                "} catch (int a) {\n"
3843                "  // Do nothing.\n"
3844                "} catch (...) {\n"
3845                "  exit(42);\n"
3846                "}");
3847 
3848   // Function-level try statements.
3849   verifyFormat("int f() try { return 4; } catch (...) {\n"
3850                "  return 5;\n"
3851                "}");
3852   verifyFormat("class A {\n"
3853                "  int a;\n"
3854                "  A() try : a(0) {\n"
3855                "  } catch (...) {\n"
3856                "    throw;\n"
3857                "  }\n"
3858                "};\n");
3859   verifyFormat("class A {\n"
3860                "  int a;\n"
3861                "  A() try : a(0), b{1} {\n"
3862                "  } catch (...) {\n"
3863                "    throw;\n"
3864                "  }\n"
3865                "};\n");
3866   verifyFormat("class A {\n"
3867                "  int a;\n"
3868                "  A() try : a(0), b{1}, c{2} {\n"
3869                "  } catch (...) {\n"
3870                "    throw;\n"
3871                "  }\n"
3872                "};\n");
3873   verifyFormat("class A {\n"
3874                "  int a;\n"
3875                "  A() try : a(0), b{1}, c{2} {\n"
3876                "    { // New scope.\n"
3877                "    }\n"
3878                "  } catch (...) {\n"
3879                "    throw;\n"
3880                "  }\n"
3881                "};\n");
3882 
3883   // Incomplete try-catch blocks.
3884   verifyIncompleteFormat("try {} catch (");
3885 }
3886 
3887 TEST_F(FormatTest, FormatTryAsAVariable) {
3888   verifyFormat("int try;");
3889   verifyFormat("int try, size;");
3890   verifyFormat("try = foo();");
3891   verifyFormat("if (try < size) {\n  return true;\n}");
3892 
3893   verifyFormat("int catch;");
3894   verifyFormat("int catch, size;");
3895   verifyFormat("catch = foo();");
3896   verifyFormat("if (catch < size) {\n  return true;\n}");
3897 
3898   FormatStyle Style = getLLVMStyle();
3899   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3900   Style.BraceWrapping.AfterFunction = true;
3901   Style.BraceWrapping.BeforeCatch = true;
3902   verifyFormat("try {\n"
3903                "  int bar = 1;\n"
3904                "}\n"
3905                "catch (...) {\n"
3906                "  int bar = 1;\n"
3907                "}",
3908                Style);
3909   verifyFormat("#if NO_EX\n"
3910                "try\n"
3911                "#endif\n"
3912                "{\n"
3913                "}\n"
3914                "#if NO_EX\n"
3915                "catch (...) {\n"
3916                "}",
3917                Style);
3918   verifyFormat("try /* abc */ {\n"
3919                "  int bar = 1;\n"
3920                "}\n"
3921                "catch (...) {\n"
3922                "  int bar = 1;\n"
3923                "}",
3924                Style);
3925   verifyFormat("try\n"
3926                "// abc\n"
3927                "{\n"
3928                "  int bar = 1;\n"
3929                "}\n"
3930                "catch (...) {\n"
3931                "  int bar = 1;\n"
3932                "}",
3933                Style);
3934 }
3935 
3936 TEST_F(FormatTest, FormatSEHTryCatch) {
3937   verifyFormat("__try {\n"
3938                "  int a = b * c;\n"
3939                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
3940                "  // Do nothing.\n"
3941                "}");
3942 
3943   verifyFormat("__try {\n"
3944                "  int a = b * c;\n"
3945                "} __finally {\n"
3946                "  // Do nothing.\n"
3947                "}");
3948 
3949   verifyFormat("DEBUG({\n"
3950                "  __try {\n"
3951                "  } __finally {\n"
3952                "  }\n"
3953                "});\n");
3954 }
3955 
3956 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
3957   verifyFormat("try {\n"
3958                "  f();\n"
3959                "} catch {\n"
3960                "  g();\n"
3961                "}");
3962   verifyFormat("try {\n"
3963                "  f();\n"
3964                "} catch (A a) MACRO(x) {\n"
3965                "  g();\n"
3966                "} catch (B b) MACRO(x) {\n"
3967                "  g();\n"
3968                "}");
3969 }
3970 
3971 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
3972   FormatStyle Style = getLLVMStyle();
3973   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
3974                           FormatStyle::BS_WebKit}) {
3975     Style.BreakBeforeBraces = BraceStyle;
3976     verifyFormat("try {\n"
3977                  "  // something\n"
3978                  "} catch (...) {\n"
3979                  "  // something\n"
3980                  "}",
3981                  Style);
3982   }
3983   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
3984   verifyFormat("try {\n"
3985                "  // something\n"
3986                "}\n"
3987                "catch (...) {\n"
3988                "  // something\n"
3989                "}",
3990                Style);
3991   verifyFormat("__try {\n"
3992                "  // something\n"
3993                "}\n"
3994                "__finally {\n"
3995                "  // something\n"
3996                "}",
3997                Style);
3998   verifyFormat("@try {\n"
3999                "  // something\n"
4000                "}\n"
4001                "@finally {\n"
4002                "  // something\n"
4003                "}",
4004                Style);
4005   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4006   verifyFormat("try\n"
4007                "{\n"
4008                "  // something\n"
4009                "}\n"
4010                "catch (...)\n"
4011                "{\n"
4012                "  // something\n"
4013                "}",
4014                Style);
4015   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4016   verifyFormat("try\n"
4017                "  {\n"
4018                "  // something white\n"
4019                "  }\n"
4020                "catch (...)\n"
4021                "  {\n"
4022                "  // something white\n"
4023                "  }",
4024                Style);
4025   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4026   verifyFormat("try\n"
4027                "  {\n"
4028                "    // something\n"
4029                "  }\n"
4030                "catch (...)\n"
4031                "  {\n"
4032                "    // something\n"
4033                "  }",
4034                Style);
4035   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4036   Style.BraceWrapping.BeforeCatch = true;
4037   verifyFormat("try {\n"
4038                "  // something\n"
4039                "}\n"
4040                "catch (...) {\n"
4041                "  // something\n"
4042                "}",
4043                Style);
4044 }
4045 
4046 TEST_F(FormatTest, StaticInitializers) {
4047   verifyFormat("static SomeClass SC = {1, 'a'};");
4048 
4049   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4050                "    100000000, "
4051                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4052 
4053   // Here, everything other than the "}" would fit on a line.
4054   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4055                "    10000000000000000000000000};");
4056   EXPECT_EQ("S s = {a,\n"
4057             "\n"
4058             "       b};",
4059             format("S s = {\n"
4060                    "  a,\n"
4061                    "\n"
4062                    "  b\n"
4063                    "};"));
4064 
4065   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4066   // line. However, the formatting looks a bit off and this probably doesn't
4067   // happen often in practice.
4068   verifyFormat("static int Variable[1] = {\n"
4069                "    {1000000000000000000000000000000000000}};",
4070                getLLVMStyleWithColumns(40));
4071 }
4072 
4073 TEST_F(FormatTest, DesignatedInitializers) {
4074   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4075   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4076                "                    .bbbbbbbbbb = 2,\n"
4077                "                    .cccccccccc = 3,\n"
4078                "                    .dddddddddd = 4,\n"
4079                "                    .eeeeeeeeee = 5};");
4080   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4081                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4082                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4083                "    .ccccccccccccccccccccccccccc = 3,\n"
4084                "    .ddddddddddddddddddddddddddd = 4,\n"
4085                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4086 
4087   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4088 
4089   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4090   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4091                "                    [2] = bbbbbbbbbb,\n"
4092                "                    [3] = cccccccccc,\n"
4093                "                    [4] = dddddddddd,\n"
4094                "                    [5] = eeeeeeeeee};");
4095   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4096                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4097                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4098                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4099                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4100                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4101 }
4102 
4103 TEST_F(FormatTest, NestedStaticInitializers) {
4104   verifyFormat("static A x = {{{}}};\n");
4105   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4106                "               {init1, init2, init3, init4}}};",
4107                getLLVMStyleWithColumns(50));
4108 
4109   verifyFormat("somes Status::global_reps[3] = {\n"
4110                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4111                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4112                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4113                getLLVMStyleWithColumns(60));
4114   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4115                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4116                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4117                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4118   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4119                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4120                "rect.fTop}};");
4121 
4122   verifyFormat(
4123       "SomeArrayOfSomeType a = {\n"
4124       "    {{1, 2, 3},\n"
4125       "     {1, 2, 3},\n"
4126       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4127       "      333333333333333333333333333333},\n"
4128       "     {1, 2, 3},\n"
4129       "     {1, 2, 3}}};");
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 
4139   verifyFormat("struct {\n"
4140                "  unsigned bit;\n"
4141                "  const char *const name;\n"
4142                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4143                "                 {kOsWin, \"Windows\"},\n"
4144                "                 {kOsLinux, \"Linux\"},\n"
4145                "                 {kOsCrOS, \"Chrome OS\"}};");
4146   verifyFormat("struct {\n"
4147                "  unsigned bit;\n"
4148                "  const char *const name;\n"
4149                "} kBitsToOs[] = {\n"
4150                "    {kOsMac, \"Mac\"},\n"
4151                "    {kOsWin, \"Windows\"},\n"
4152                "    {kOsLinux, \"Linux\"},\n"
4153                "    {kOsCrOS, \"Chrome OS\"},\n"
4154                "};");
4155 }
4156 
4157 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4158   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4159                "                      \\\n"
4160                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4161 }
4162 
4163 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4164   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4165                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4166 
4167   // Do break defaulted and deleted functions.
4168   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4169                "    default;",
4170                getLLVMStyleWithColumns(40));
4171   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4172                "    delete;",
4173                getLLVMStyleWithColumns(40));
4174 }
4175 
4176 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4177   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4178                getLLVMStyleWithColumns(40));
4179   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4180                getLLVMStyleWithColumns(40));
4181   EXPECT_EQ("#define Q                              \\\n"
4182             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4183             "  \"aaaaaaaa.cpp\"",
4184             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4185                    getLLVMStyleWithColumns(40)));
4186 }
4187 
4188 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4189   EXPECT_EQ("# 123 \"A string literal\"",
4190             format("   #     123    \"A string literal\""));
4191 }
4192 
4193 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4194   EXPECT_EQ("#;", format("#;"));
4195   verifyFormat("#\n;\n;\n;");
4196 }
4197 
4198 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4199   EXPECT_EQ("#line 42 \"test\"\n",
4200             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4201   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4202                                     getLLVMStyleWithColumns(12)));
4203 }
4204 
4205 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4206   EXPECT_EQ("#line 42 \"test\"",
4207             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4208   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4209 }
4210 
4211 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4212   verifyFormat("#define A \\x20");
4213   verifyFormat("#define A \\ x20");
4214   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4215   verifyFormat("#define A ''");
4216   verifyFormat("#define A ''qqq");
4217   verifyFormat("#define A `qqq");
4218   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4219   EXPECT_EQ("const char *c = STRINGIFY(\n"
4220             "\\na : b);",
4221             format("const char * c = STRINGIFY(\n"
4222                    "\\na : b);"));
4223 
4224   verifyFormat("a\r\\");
4225   verifyFormat("a\v\\");
4226   verifyFormat("a\f\\");
4227 }
4228 
4229 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4230   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4231   style.IndentWidth = 4;
4232   style.PPIndentWidth = 1;
4233 
4234   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4235   verifyFormat("#ifdef __linux__\n"
4236                "void foo() {\n"
4237                "    int x = 0;\n"
4238                "}\n"
4239                "#define FOO\n"
4240                "#endif\n"
4241                "void bar() {\n"
4242                "    int y = 0;\n"
4243                "}\n",
4244                style);
4245 
4246   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4247   verifyFormat("#ifdef __linux__\n"
4248                "void foo() {\n"
4249                "    int x = 0;\n"
4250                "}\n"
4251                "# define FOO foo\n"
4252                "#endif\n"
4253                "void bar() {\n"
4254                "    int y = 0;\n"
4255                "}\n",
4256                style);
4257 
4258   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4259   verifyFormat("#ifdef __linux__\n"
4260                "void foo() {\n"
4261                "    int x = 0;\n"
4262                "}\n"
4263                " #define FOO foo\n"
4264                "#endif\n"
4265                "void bar() {\n"
4266                "    int y = 0;\n"
4267                "}\n",
4268                style);
4269 }
4270 
4271 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4272   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4273   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4274   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4275   // FIXME: We never break before the macro name.
4276   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4277 
4278   verifyFormat("#define A A\n#define A A");
4279   verifyFormat("#define A(X) A\n#define A A");
4280 
4281   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4282   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4283 }
4284 
4285 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4286   EXPECT_EQ("// somecomment\n"
4287             "#include \"a.h\"\n"
4288             "#define A(  \\\n"
4289             "    A, B)\n"
4290             "#include \"b.h\"\n"
4291             "// somecomment\n",
4292             format("  // somecomment\n"
4293                    "  #include \"a.h\"\n"
4294                    "#define A(A,\\\n"
4295                    "    B)\n"
4296                    "    #include \"b.h\"\n"
4297                    " // somecomment\n",
4298                    getLLVMStyleWithColumns(13)));
4299 }
4300 
4301 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4302 
4303 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4304   EXPECT_EQ("#define A    \\\n"
4305             "  c;         \\\n"
4306             "  e;\n"
4307             "f;",
4308             format("#define A c; e;\n"
4309                    "f;",
4310                    getLLVMStyleWithColumns(14)));
4311 }
4312 
4313 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4314 
4315 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4316   EXPECT_EQ("int x,\n"
4317             "#define A\n"
4318             "    y;",
4319             format("int x,\n#define A\ny;"));
4320 }
4321 
4322 TEST_F(FormatTest, HashInMacroDefinition) {
4323   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4324   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4325   verifyFormat("#define A  \\\n"
4326                "  {        \\\n"
4327                "    f(#c); \\\n"
4328                "  }",
4329                getLLVMStyleWithColumns(11));
4330 
4331   verifyFormat("#define A(X)         \\\n"
4332                "  void function##X()",
4333                getLLVMStyleWithColumns(22));
4334 
4335   verifyFormat("#define A(a, b, c)   \\\n"
4336                "  void a##b##c()",
4337                getLLVMStyleWithColumns(22));
4338 
4339   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4340 }
4341 
4342 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4343   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4344   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4345 
4346   FormatStyle Style = getLLVMStyle();
4347   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4348   verifyFormat("#define true ((foo)1)", Style);
4349   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4350   verifyFormat("#define false((foo)0)", Style);
4351 }
4352 
4353 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4354   EXPECT_EQ("#define A b;", format("#define A \\\n"
4355                                    "          \\\n"
4356                                    "  b;",
4357                                    getLLVMStyleWithColumns(25)));
4358   EXPECT_EQ("#define A \\\n"
4359             "          \\\n"
4360             "  a;      \\\n"
4361             "  b;",
4362             format("#define A \\\n"
4363                    "          \\\n"
4364                    "  a;      \\\n"
4365                    "  b;",
4366                    getLLVMStyleWithColumns(11)));
4367   EXPECT_EQ("#define A \\\n"
4368             "  a;      \\\n"
4369             "          \\\n"
4370             "  b;",
4371             format("#define A \\\n"
4372                    "  a;      \\\n"
4373                    "          \\\n"
4374                    "  b;",
4375                    getLLVMStyleWithColumns(11)));
4376 }
4377 
4378 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4379   verifyIncompleteFormat("#define A :");
4380   verifyFormat("#define SOMECASES  \\\n"
4381                "  case 1:          \\\n"
4382                "  case 2\n",
4383                getLLVMStyleWithColumns(20));
4384   verifyFormat("#define MACRO(a) \\\n"
4385                "  if (a)         \\\n"
4386                "    f();         \\\n"
4387                "  else           \\\n"
4388                "    g()",
4389                getLLVMStyleWithColumns(18));
4390   verifyFormat("#define A template <typename T>");
4391   verifyIncompleteFormat("#define STR(x) #x\n"
4392                          "f(STR(this_is_a_string_literal{));");
4393   verifyFormat("#pragma omp threadprivate( \\\n"
4394                "    y)), // expected-warning",
4395                getLLVMStyleWithColumns(28));
4396   verifyFormat("#d, = };");
4397   verifyFormat("#if \"a");
4398   verifyIncompleteFormat("({\n"
4399                          "#define b     \\\n"
4400                          "  }           \\\n"
4401                          "  a\n"
4402                          "a",
4403                          getLLVMStyleWithColumns(15));
4404   verifyFormat("#define A     \\\n"
4405                "  {           \\\n"
4406                "    {\n"
4407                "#define B     \\\n"
4408                "  }           \\\n"
4409                "  }",
4410                getLLVMStyleWithColumns(15));
4411   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4412   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4413   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4414   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4415 }
4416 
4417 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4418   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4419   EXPECT_EQ("class A : public QObject {\n"
4420             "  Q_OBJECT\n"
4421             "\n"
4422             "  A() {}\n"
4423             "};",
4424             format("class A  :  public QObject {\n"
4425                    "     Q_OBJECT\n"
4426                    "\n"
4427                    "  A() {\n}\n"
4428                    "}  ;"));
4429   EXPECT_EQ("MACRO\n"
4430             "/*static*/ int i;",
4431             format("MACRO\n"
4432                    " /*static*/ int   i;"));
4433   EXPECT_EQ("SOME_MACRO\n"
4434             "namespace {\n"
4435             "void f();\n"
4436             "} // namespace",
4437             format("SOME_MACRO\n"
4438                    "  namespace    {\n"
4439                    "void   f(  );\n"
4440                    "} // namespace"));
4441   // Only if the identifier contains at least 5 characters.
4442   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4443   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4444   // Only if everything is upper case.
4445   EXPECT_EQ("class A : public QObject {\n"
4446             "  Q_Object A() {}\n"
4447             "};",
4448             format("class A  :  public QObject {\n"
4449                    "     Q_Object\n"
4450                    "  A() {\n}\n"
4451                    "}  ;"));
4452 
4453   // Only if the next line can actually start an unwrapped line.
4454   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4455             format("SOME_WEIRD_LOG_MACRO\n"
4456                    "<< SomeThing;"));
4457 
4458   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4459                "(n, buffers))\n",
4460                getChromiumStyle(FormatStyle::LK_Cpp));
4461 
4462   // See PR41483
4463   EXPECT_EQ("/**/ FOO(a)\n"
4464             "FOO(b)",
4465             format("/**/ FOO(a)\n"
4466                    "FOO(b)"));
4467 }
4468 
4469 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4470   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4471             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4472             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4473             "class X {};\n"
4474             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4475             "int *createScopDetectionPass() { return 0; }",
4476             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4477                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4478                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4479                    "  class X {};\n"
4480                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4481                    "  int *createScopDetectionPass() { return 0; }"));
4482   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4483   // braces, so that inner block is indented one level more.
4484   EXPECT_EQ("int q() {\n"
4485             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4486             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4487             "  IPC_END_MESSAGE_MAP()\n"
4488             "}",
4489             format("int q() {\n"
4490                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4491                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4492                    "  IPC_END_MESSAGE_MAP()\n"
4493                    "}"));
4494 
4495   // Same inside macros.
4496   EXPECT_EQ("#define LIST(L) \\\n"
4497             "  L(A)          \\\n"
4498             "  L(B)          \\\n"
4499             "  L(C)",
4500             format("#define LIST(L) \\\n"
4501                    "  L(A) \\\n"
4502                    "  L(B) \\\n"
4503                    "  L(C)",
4504                    getGoogleStyle()));
4505 
4506   // These must not be recognized as macros.
4507   EXPECT_EQ("int q() {\n"
4508             "  f(x);\n"
4509             "  f(x) {}\n"
4510             "  f(x)->g();\n"
4511             "  f(x)->*g();\n"
4512             "  f(x).g();\n"
4513             "  f(x) = x;\n"
4514             "  f(x) += x;\n"
4515             "  f(x) -= x;\n"
4516             "  f(x) *= x;\n"
4517             "  f(x) /= x;\n"
4518             "  f(x) %= x;\n"
4519             "  f(x) &= x;\n"
4520             "  f(x) |= x;\n"
4521             "  f(x) ^= x;\n"
4522             "  f(x) >>= x;\n"
4523             "  f(x) <<= x;\n"
4524             "  f(x)[y].z();\n"
4525             "  LOG(INFO) << x;\n"
4526             "  ifstream(x) >> x;\n"
4527             "}\n",
4528             format("int q() {\n"
4529                    "  f(x)\n;\n"
4530                    "  f(x)\n {}\n"
4531                    "  f(x)\n->g();\n"
4532                    "  f(x)\n->*g();\n"
4533                    "  f(x)\n.g();\n"
4534                    "  f(x)\n = x;\n"
4535                    "  f(x)\n += x;\n"
4536                    "  f(x)\n -= x;\n"
4537                    "  f(x)\n *= x;\n"
4538                    "  f(x)\n /= x;\n"
4539                    "  f(x)\n %= x;\n"
4540                    "  f(x)\n &= x;\n"
4541                    "  f(x)\n |= x;\n"
4542                    "  f(x)\n ^= x;\n"
4543                    "  f(x)\n >>= x;\n"
4544                    "  f(x)\n <<= x;\n"
4545                    "  f(x)\n[y].z();\n"
4546                    "  LOG(INFO)\n << x;\n"
4547                    "  ifstream(x)\n >> x;\n"
4548                    "}\n"));
4549   EXPECT_EQ("int q() {\n"
4550             "  F(x)\n"
4551             "  if (1) {\n"
4552             "  }\n"
4553             "  F(x)\n"
4554             "  while (1) {\n"
4555             "  }\n"
4556             "  F(x)\n"
4557             "  G(x);\n"
4558             "  F(x)\n"
4559             "  try {\n"
4560             "    Q();\n"
4561             "  } catch (...) {\n"
4562             "  }\n"
4563             "}\n",
4564             format("int q() {\n"
4565                    "F(x)\n"
4566                    "if (1) {}\n"
4567                    "F(x)\n"
4568                    "while (1) {}\n"
4569                    "F(x)\n"
4570                    "G(x);\n"
4571                    "F(x)\n"
4572                    "try { Q(); } catch (...) {}\n"
4573                    "}\n"));
4574   EXPECT_EQ("class A {\n"
4575             "  A() : t(0) {}\n"
4576             "  A(int i) noexcept() : {}\n"
4577             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4578             "  try : t(0) {\n"
4579             "  } catch (...) {\n"
4580             "  }\n"
4581             "};",
4582             format("class A {\n"
4583                    "  A()\n : t(0) {}\n"
4584                    "  A(int i)\n noexcept() : {}\n"
4585                    "  A(X x)\n"
4586                    "  try : t(0) {} catch (...) {}\n"
4587                    "};"));
4588   FormatStyle Style = getLLVMStyle();
4589   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4590   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4591   Style.BraceWrapping.AfterFunction = true;
4592   EXPECT_EQ("void f()\n"
4593             "try\n"
4594             "{\n"
4595             "}",
4596             format("void f() try {\n"
4597                    "}",
4598                    Style));
4599   EXPECT_EQ("class SomeClass {\n"
4600             "public:\n"
4601             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4602             "};",
4603             format("class SomeClass {\n"
4604                    "public:\n"
4605                    "  SomeClass()\n"
4606                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4607                    "};"));
4608   EXPECT_EQ("class SomeClass {\n"
4609             "public:\n"
4610             "  SomeClass()\n"
4611             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4612             "};",
4613             format("class SomeClass {\n"
4614                    "public:\n"
4615                    "  SomeClass()\n"
4616                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4617                    "};",
4618                    getLLVMStyleWithColumns(40)));
4619 
4620   verifyFormat("MACRO(>)");
4621 
4622   // Some macros contain an implicit semicolon.
4623   Style = getLLVMStyle();
4624   Style.StatementMacros.push_back("FOO");
4625   verifyFormat("FOO(a) int b = 0;");
4626   verifyFormat("FOO(a)\n"
4627                "int b = 0;",
4628                Style);
4629   verifyFormat("FOO(a);\n"
4630                "int b = 0;",
4631                Style);
4632   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
4633                "int b = 0;",
4634                Style);
4635   verifyFormat("FOO()\n"
4636                "int b = 0;",
4637                Style);
4638   verifyFormat("FOO\n"
4639                "int b = 0;",
4640                Style);
4641   verifyFormat("void f() {\n"
4642                "  FOO(a)\n"
4643                "  return a;\n"
4644                "}",
4645                Style);
4646   verifyFormat("FOO(a)\n"
4647                "FOO(b)",
4648                Style);
4649   verifyFormat("int a = 0;\n"
4650                "FOO(b)\n"
4651                "int c = 0;",
4652                Style);
4653   verifyFormat("int a = 0;\n"
4654                "int x = FOO(a)\n"
4655                "int b = 0;",
4656                Style);
4657   verifyFormat("void foo(int a) { FOO(a) }\n"
4658                "uint32_t bar() {}",
4659                Style);
4660 }
4661 
4662 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
4663   verifyFormat("#define A \\\n"
4664                "  f({     \\\n"
4665                "    g();  \\\n"
4666                "  });",
4667                getLLVMStyleWithColumns(11));
4668 }
4669 
4670 TEST_F(FormatTest, IndentPreprocessorDirectives) {
4671   FormatStyle Style = getLLVMStyle();
4672   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
4673   Style.ColumnLimit = 40;
4674   verifyFormat("#ifdef _WIN32\n"
4675                "#define A 0\n"
4676                "#ifdef VAR2\n"
4677                "#define B 1\n"
4678                "#include <someheader.h>\n"
4679                "#define MACRO                          \\\n"
4680                "  some_very_long_func_aaaaaaaaaa();\n"
4681                "#endif\n"
4682                "#else\n"
4683                "#define A 1\n"
4684                "#endif",
4685                Style);
4686   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4687   verifyFormat("#ifdef _WIN32\n"
4688                "#  define A 0\n"
4689                "#  ifdef VAR2\n"
4690                "#    define B 1\n"
4691                "#    include <someheader.h>\n"
4692                "#    define MACRO                      \\\n"
4693                "      some_very_long_func_aaaaaaaaaa();\n"
4694                "#  endif\n"
4695                "#else\n"
4696                "#  define A 1\n"
4697                "#endif",
4698                Style);
4699   verifyFormat("#if A\n"
4700                "#  define MACRO                        \\\n"
4701                "    void a(int x) {                    \\\n"
4702                "      b();                             \\\n"
4703                "      c();                             \\\n"
4704                "      d();                             \\\n"
4705                "      e();                             \\\n"
4706                "      f();                             \\\n"
4707                "    }\n"
4708                "#endif",
4709                Style);
4710   // Comments before include guard.
4711   verifyFormat("// file comment\n"
4712                "// file comment\n"
4713                "#ifndef HEADER_H\n"
4714                "#define HEADER_H\n"
4715                "code();\n"
4716                "#endif",
4717                Style);
4718   // Test with include guards.
4719   verifyFormat("#ifndef HEADER_H\n"
4720                "#define HEADER_H\n"
4721                "code();\n"
4722                "#endif",
4723                Style);
4724   // Include guards must have a #define with the same variable immediately
4725   // after #ifndef.
4726   verifyFormat("#ifndef NOT_GUARD\n"
4727                "#  define FOO\n"
4728                "code();\n"
4729                "#endif",
4730                Style);
4731 
4732   // Include guards must cover the entire file.
4733   verifyFormat("code();\n"
4734                "code();\n"
4735                "#ifndef NOT_GUARD\n"
4736                "#  define NOT_GUARD\n"
4737                "code();\n"
4738                "#endif",
4739                Style);
4740   verifyFormat("#ifndef NOT_GUARD\n"
4741                "#  define NOT_GUARD\n"
4742                "code();\n"
4743                "#endif\n"
4744                "code();",
4745                Style);
4746   // Test with trailing blank lines.
4747   verifyFormat("#ifndef HEADER_H\n"
4748                "#define HEADER_H\n"
4749                "code();\n"
4750                "#endif\n",
4751                Style);
4752   // Include guards don't have #else.
4753   verifyFormat("#ifndef NOT_GUARD\n"
4754                "#  define NOT_GUARD\n"
4755                "code();\n"
4756                "#else\n"
4757                "#endif",
4758                Style);
4759   verifyFormat("#ifndef NOT_GUARD\n"
4760                "#  define NOT_GUARD\n"
4761                "code();\n"
4762                "#elif FOO\n"
4763                "#endif",
4764                Style);
4765   // Non-identifier #define after potential include guard.
4766   verifyFormat("#ifndef FOO\n"
4767                "#  define 1\n"
4768                "#endif\n",
4769                Style);
4770   // #if closes past last non-preprocessor line.
4771   verifyFormat("#ifndef FOO\n"
4772                "#define FOO\n"
4773                "#if 1\n"
4774                "int i;\n"
4775                "#  define A 0\n"
4776                "#endif\n"
4777                "#endif\n",
4778                Style);
4779   // Don't crash if there is an #elif directive without a condition.
4780   verifyFormat("#if 1\n"
4781                "int x;\n"
4782                "#elif\n"
4783                "int y;\n"
4784                "#else\n"
4785                "int z;\n"
4786                "#endif",
4787                Style);
4788   // FIXME: This doesn't handle the case where there's code between the
4789   // #ifndef and #define but all other conditions hold. This is because when
4790   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
4791   // previous code line yet, so we can't detect it.
4792   EXPECT_EQ("#ifndef NOT_GUARD\n"
4793             "code();\n"
4794             "#define NOT_GUARD\n"
4795             "code();\n"
4796             "#endif",
4797             format("#ifndef NOT_GUARD\n"
4798                    "code();\n"
4799                    "#  define NOT_GUARD\n"
4800                    "code();\n"
4801                    "#endif",
4802                    Style));
4803   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
4804   // be outside an include guard. Examples are #pragma once and
4805   // #pragma GCC diagnostic, or anything else that does not change the meaning
4806   // of the file if it's included multiple times.
4807   EXPECT_EQ("#ifdef WIN32\n"
4808             "#  pragma once\n"
4809             "#endif\n"
4810             "#ifndef HEADER_H\n"
4811             "#  define HEADER_H\n"
4812             "code();\n"
4813             "#endif",
4814             format("#ifdef WIN32\n"
4815                    "#  pragma once\n"
4816                    "#endif\n"
4817                    "#ifndef HEADER_H\n"
4818                    "#define HEADER_H\n"
4819                    "code();\n"
4820                    "#endif",
4821                    Style));
4822   // FIXME: This does not detect when there is a single non-preprocessor line
4823   // in front of an include-guard-like structure where other conditions hold
4824   // because ScopedLineState hides the line.
4825   EXPECT_EQ("code();\n"
4826             "#ifndef HEADER_H\n"
4827             "#define HEADER_H\n"
4828             "code();\n"
4829             "#endif",
4830             format("code();\n"
4831                    "#ifndef HEADER_H\n"
4832                    "#  define HEADER_H\n"
4833                    "code();\n"
4834                    "#endif",
4835                    Style));
4836   // Keep comments aligned with #, otherwise indent comments normally. These
4837   // tests cannot use verifyFormat because messUp manipulates leading
4838   // whitespace.
4839   {
4840     const char *Expected = ""
4841                            "void f() {\n"
4842                            "#if 1\n"
4843                            "// Preprocessor aligned.\n"
4844                            "#  define A 0\n"
4845                            "  // Code. Separated by blank line.\n"
4846                            "\n"
4847                            "#  define B 0\n"
4848                            "  // Code. Not aligned with #\n"
4849                            "#  define C 0\n"
4850                            "#endif";
4851     const char *ToFormat = ""
4852                            "void f() {\n"
4853                            "#if 1\n"
4854                            "// Preprocessor aligned.\n"
4855                            "#  define A 0\n"
4856                            "// Code. Separated by blank line.\n"
4857                            "\n"
4858                            "#  define B 0\n"
4859                            "   // Code. Not aligned with #\n"
4860                            "#  define C 0\n"
4861                            "#endif";
4862     EXPECT_EQ(Expected, format(ToFormat, Style));
4863     EXPECT_EQ(Expected, format(Expected, Style));
4864   }
4865   // Keep block quotes aligned.
4866   {
4867     const char *Expected = ""
4868                            "void f() {\n"
4869                            "#if 1\n"
4870                            "/* Preprocessor aligned. */\n"
4871                            "#  define A 0\n"
4872                            "  /* Code. Separated by blank line. */\n"
4873                            "\n"
4874                            "#  define B 0\n"
4875                            "  /* Code. Not aligned with # */\n"
4876                            "#  define C 0\n"
4877                            "#endif";
4878     const char *ToFormat = ""
4879                            "void f() {\n"
4880                            "#if 1\n"
4881                            "/* Preprocessor aligned. */\n"
4882                            "#  define A 0\n"
4883                            "/* Code. Separated by blank line. */\n"
4884                            "\n"
4885                            "#  define B 0\n"
4886                            "   /* Code. Not aligned with # */\n"
4887                            "#  define C 0\n"
4888                            "#endif";
4889     EXPECT_EQ(Expected, format(ToFormat, Style));
4890     EXPECT_EQ(Expected, format(Expected, Style));
4891   }
4892   // Keep comments aligned with un-indented directives.
4893   {
4894     const char *Expected = ""
4895                            "void f() {\n"
4896                            "// Preprocessor aligned.\n"
4897                            "#define A 0\n"
4898                            "  // Code. Separated by blank line.\n"
4899                            "\n"
4900                            "#define B 0\n"
4901                            "  // Code. Not aligned with #\n"
4902                            "#define C 0\n";
4903     const char *ToFormat = ""
4904                            "void f() {\n"
4905                            "// Preprocessor aligned.\n"
4906                            "#define A 0\n"
4907                            "// Code. Separated by blank line.\n"
4908                            "\n"
4909                            "#define B 0\n"
4910                            "   // Code. Not aligned with #\n"
4911                            "#define C 0\n";
4912     EXPECT_EQ(Expected, format(ToFormat, Style));
4913     EXPECT_EQ(Expected, format(Expected, Style));
4914   }
4915   // Test AfterHash with tabs.
4916   {
4917     FormatStyle Tabbed = Style;
4918     Tabbed.UseTab = FormatStyle::UT_Always;
4919     Tabbed.IndentWidth = 8;
4920     Tabbed.TabWidth = 8;
4921     verifyFormat("#ifdef _WIN32\n"
4922                  "#\tdefine A 0\n"
4923                  "#\tifdef VAR2\n"
4924                  "#\t\tdefine B 1\n"
4925                  "#\t\tinclude <someheader.h>\n"
4926                  "#\t\tdefine MACRO          \\\n"
4927                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
4928                  "#\tendif\n"
4929                  "#else\n"
4930                  "#\tdefine A 1\n"
4931                  "#endif",
4932                  Tabbed);
4933   }
4934 
4935   // Regression test: Multiline-macro inside include guards.
4936   verifyFormat("#ifndef HEADER_H\n"
4937                "#define HEADER_H\n"
4938                "#define A()        \\\n"
4939                "  int i;           \\\n"
4940                "  int j;\n"
4941                "#endif // HEADER_H",
4942                getLLVMStyleWithColumns(20));
4943 
4944   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4945   // Basic before hash indent tests
4946   verifyFormat("#ifdef _WIN32\n"
4947                "  #define A 0\n"
4948                "  #ifdef VAR2\n"
4949                "    #define B 1\n"
4950                "    #include <someheader.h>\n"
4951                "    #define MACRO                      \\\n"
4952                "      some_very_long_func_aaaaaaaaaa();\n"
4953                "  #endif\n"
4954                "#else\n"
4955                "  #define A 1\n"
4956                "#endif",
4957                Style);
4958   verifyFormat("#if A\n"
4959                "  #define MACRO                        \\\n"
4960                "    void a(int x) {                    \\\n"
4961                "      b();                             \\\n"
4962                "      c();                             \\\n"
4963                "      d();                             \\\n"
4964                "      e();                             \\\n"
4965                "      f();                             \\\n"
4966                "    }\n"
4967                "#endif",
4968                Style);
4969   // Keep comments aligned with indented directives. These
4970   // tests cannot use verifyFormat because messUp manipulates leading
4971   // whitespace.
4972   {
4973     const char *Expected = "void f() {\n"
4974                            "// Aligned to preprocessor.\n"
4975                            "#if 1\n"
4976                            "  // Aligned to code.\n"
4977                            "  int a;\n"
4978                            "  #if 1\n"
4979                            "    // Aligned to preprocessor.\n"
4980                            "    #define A 0\n"
4981                            "  // Aligned to code.\n"
4982                            "  int b;\n"
4983                            "  #endif\n"
4984                            "#endif\n"
4985                            "}";
4986     const char *ToFormat = "void f() {\n"
4987                            "// Aligned to preprocessor.\n"
4988                            "#if 1\n"
4989                            "// Aligned to code.\n"
4990                            "int a;\n"
4991                            "#if 1\n"
4992                            "// Aligned to preprocessor.\n"
4993                            "#define A 0\n"
4994                            "// Aligned to code.\n"
4995                            "int b;\n"
4996                            "#endif\n"
4997                            "#endif\n"
4998                            "}";
4999     EXPECT_EQ(Expected, format(ToFormat, Style));
5000     EXPECT_EQ(Expected, format(Expected, Style));
5001   }
5002   {
5003     const char *Expected = "void f() {\n"
5004                            "/* Aligned to preprocessor. */\n"
5005                            "#if 1\n"
5006                            "  /* Aligned to code. */\n"
5007                            "  int a;\n"
5008                            "  #if 1\n"
5009                            "    /* Aligned to preprocessor. */\n"
5010                            "    #define A 0\n"
5011                            "  /* Aligned to code. */\n"
5012                            "  int b;\n"
5013                            "  #endif\n"
5014                            "#endif\n"
5015                            "}";
5016     const char *ToFormat = "void f() {\n"
5017                            "/* Aligned to preprocessor. */\n"
5018                            "#if 1\n"
5019                            "/* Aligned to code. */\n"
5020                            "int a;\n"
5021                            "#if 1\n"
5022                            "/* Aligned to preprocessor. */\n"
5023                            "#define A 0\n"
5024                            "/* Aligned to code. */\n"
5025                            "int b;\n"
5026                            "#endif\n"
5027                            "#endif\n"
5028                            "}";
5029     EXPECT_EQ(Expected, format(ToFormat, Style));
5030     EXPECT_EQ(Expected, format(Expected, Style));
5031   }
5032 
5033   // Test single comment before preprocessor
5034   verifyFormat("// Comment\n"
5035                "\n"
5036                "#if 1\n"
5037                "#endif",
5038                Style);
5039 }
5040 
5041 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5042   verifyFormat("{\n  { a #c; }\n}");
5043 }
5044 
5045 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5046   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5047             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5048   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5049             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5050 }
5051 
5052 TEST_F(FormatTest, EscapedNewlines) {
5053   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5054   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5055             format("#define A \\\nint i;\\\n  int j;", Narrow));
5056   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5057   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5058   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5059   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5060 
5061   FormatStyle AlignLeft = getLLVMStyle();
5062   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5063   EXPECT_EQ("#define MACRO(x) \\\n"
5064             "private:         \\\n"
5065             "  int x(int a);\n",
5066             format("#define MACRO(x) \\\n"
5067                    "private:         \\\n"
5068                    "  int x(int a);\n",
5069                    AlignLeft));
5070 
5071   // CRLF line endings
5072   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5073             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5074   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5075   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5076   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5077   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5078   EXPECT_EQ("#define MACRO(x) \\\r\n"
5079             "private:         \\\r\n"
5080             "  int x(int a);\r\n",
5081             format("#define MACRO(x) \\\r\n"
5082                    "private:         \\\r\n"
5083                    "  int x(int a);\r\n",
5084                    AlignLeft));
5085 
5086   FormatStyle DontAlign = getLLVMStyle();
5087   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5088   DontAlign.MaxEmptyLinesToKeep = 3;
5089   // FIXME: can't use verifyFormat here because the newline before
5090   // "public:" is not inserted the first time it's reformatted
5091   EXPECT_EQ("#define A \\\n"
5092             "  class Foo { \\\n"
5093             "    void bar(); \\\n"
5094             "\\\n"
5095             "\\\n"
5096             "\\\n"
5097             "  public: \\\n"
5098             "    void baz(); \\\n"
5099             "  };",
5100             format("#define A \\\n"
5101                    "  class Foo { \\\n"
5102                    "    void bar(); \\\n"
5103                    "\\\n"
5104                    "\\\n"
5105                    "\\\n"
5106                    "  public: \\\n"
5107                    "    void baz(); \\\n"
5108                    "  };",
5109                    DontAlign));
5110 }
5111 
5112 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5113   verifyFormat("#define A \\\n"
5114                "  int v(  \\\n"
5115                "      a); \\\n"
5116                "  int i;",
5117                getLLVMStyleWithColumns(11));
5118 }
5119 
5120 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5121   EXPECT_EQ(
5122       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5123       "                      \\\n"
5124       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5125       "\n"
5126       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5127       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5128       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5129              "\\\n"
5130              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5131              "  \n"
5132              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5133              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5134 }
5135 
5136 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5137   EXPECT_EQ("int\n"
5138             "#define A\n"
5139             "    a;",
5140             format("int\n#define A\na;"));
5141   verifyFormat("functionCallTo(\n"
5142                "    someOtherFunction(\n"
5143                "        withSomeParameters, whichInSequence,\n"
5144                "        areLongerThanALine(andAnotherCall,\n"
5145                "#define A B\n"
5146                "                           withMoreParamters,\n"
5147                "                           whichStronglyInfluenceTheLayout),\n"
5148                "        andMoreParameters),\n"
5149                "    trailing);",
5150                getLLVMStyleWithColumns(69));
5151   verifyFormat("Foo::Foo()\n"
5152                "#ifdef BAR\n"
5153                "    : baz(0)\n"
5154                "#endif\n"
5155                "{\n"
5156                "}");
5157   verifyFormat("void f() {\n"
5158                "  if (true)\n"
5159                "#ifdef A\n"
5160                "    f(42);\n"
5161                "  x();\n"
5162                "#else\n"
5163                "    g();\n"
5164                "  x();\n"
5165                "#endif\n"
5166                "}");
5167   verifyFormat("void f(param1, param2,\n"
5168                "       param3,\n"
5169                "#ifdef A\n"
5170                "       param4(param5,\n"
5171                "#ifdef A1\n"
5172                "              param6,\n"
5173                "#ifdef A2\n"
5174                "              param7),\n"
5175                "#else\n"
5176                "              param8),\n"
5177                "       param9,\n"
5178                "#endif\n"
5179                "       param10,\n"
5180                "#endif\n"
5181                "       param11)\n"
5182                "#else\n"
5183                "       param12)\n"
5184                "#endif\n"
5185                "{\n"
5186                "  x();\n"
5187                "}",
5188                getLLVMStyleWithColumns(28));
5189   verifyFormat("#if 1\n"
5190                "int i;");
5191   verifyFormat("#if 1\n"
5192                "#endif\n"
5193                "#if 1\n"
5194                "#else\n"
5195                "#endif\n");
5196   verifyFormat("DEBUG({\n"
5197                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5198                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5199                "});\n"
5200                "#if a\n"
5201                "#else\n"
5202                "#endif");
5203 
5204   verifyIncompleteFormat("void f(\n"
5205                          "#if A\n"
5206                          ");\n"
5207                          "#else\n"
5208                          "#endif");
5209 }
5210 
5211 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5212   verifyFormat("#endif\n"
5213                "#if B");
5214 }
5215 
5216 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5217   FormatStyle SingleLine = getLLVMStyle();
5218   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5219   verifyFormat("#if 0\n"
5220                "#elif 1\n"
5221                "#endif\n"
5222                "void foo() {\n"
5223                "  if (test) foo2();\n"
5224                "}",
5225                SingleLine);
5226 }
5227 
5228 TEST_F(FormatTest, LayoutBlockInsideParens) {
5229   verifyFormat("functionCall({ int i; });");
5230   verifyFormat("functionCall({\n"
5231                "  int i;\n"
5232                "  int j;\n"
5233                "});");
5234   verifyFormat("functionCall(\n"
5235                "    {\n"
5236                "      int i;\n"
5237                "      int j;\n"
5238                "    },\n"
5239                "    aaaa, bbbb, cccc);");
5240   verifyFormat("functionA(functionB({\n"
5241                "            int i;\n"
5242                "            int j;\n"
5243                "          }),\n"
5244                "          aaaa, bbbb, cccc);");
5245   verifyFormat("functionCall(\n"
5246                "    {\n"
5247                "      int i;\n"
5248                "      int j;\n"
5249                "    },\n"
5250                "    aaaa, bbbb, // comment\n"
5251                "    cccc);");
5252   verifyFormat("functionA(functionB({\n"
5253                "            int i;\n"
5254                "            int j;\n"
5255                "          }),\n"
5256                "          aaaa, bbbb, // comment\n"
5257                "          cccc);");
5258   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5259   verifyFormat("functionCall(aaaa, bbbb, {\n"
5260                "  int i;\n"
5261                "  int j;\n"
5262                "});");
5263   verifyFormat(
5264       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5265       "    {\n"
5266       "      int i; // break\n"
5267       "    },\n"
5268       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5269       "                                     ccccccccccccccccc));");
5270   verifyFormat("DEBUG({\n"
5271                "  if (a)\n"
5272                "    f();\n"
5273                "});");
5274 }
5275 
5276 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5277   EXPECT_EQ("SOME_MACRO { int i; }\n"
5278             "int i;",
5279             format("  SOME_MACRO  {int i;}  int i;"));
5280 }
5281 
5282 TEST_F(FormatTest, LayoutNestedBlocks) {
5283   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5284                "  struct s {\n"
5285                "    int i;\n"
5286                "  };\n"
5287                "  s kBitsToOs[] = {{10}};\n"
5288                "  for (int i = 0; i < 10; ++i)\n"
5289                "    return;\n"
5290                "}");
5291   verifyFormat("call(parameter, {\n"
5292                "  something();\n"
5293                "  // Comment using all columns.\n"
5294                "  somethingelse();\n"
5295                "});",
5296                getLLVMStyleWithColumns(40));
5297   verifyFormat("DEBUG( //\n"
5298                "    { f(); }, a);");
5299   verifyFormat("DEBUG( //\n"
5300                "    {\n"
5301                "      f(); //\n"
5302                "    },\n"
5303                "    a);");
5304 
5305   EXPECT_EQ("call(parameter, {\n"
5306             "  something();\n"
5307             "  // Comment too\n"
5308             "  // looooooooooong.\n"
5309             "  somethingElse();\n"
5310             "});",
5311             format("call(parameter, {\n"
5312                    "  something();\n"
5313                    "  // Comment too looooooooooong.\n"
5314                    "  somethingElse();\n"
5315                    "});",
5316                    getLLVMStyleWithColumns(29)));
5317   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5318   EXPECT_EQ("DEBUG({ // comment\n"
5319             "  int i;\n"
5320             "});",
5321             format("DEBUG({ // comment\n"
5322                    "int  i;\n"
5323                    "});"));
5324   EXPECT_EQ("DEBUG({\n"
5325             "  int i;\n"
5326             "\n"
5327             "  // comment\n"
5328             "  int j;\n"
5329             "});",
5330             format("DEBUG({\n"
5331                    "  int  i;\n"
5332                    "\n"
5333                    "  // comment\n"
5334                    "  int  j;\n"
5335                    "});"));
5336 
5337   verifyFormat("DEBUG({\n"
5338                "  if (a)\n"
5339                "    return;\n"
5340                "});");
5341   verifyGoogleFormat("DEBUG({\n"
5342                      "  if (a) return;\n"
5343                      "});");
5344   FormatStyle Style = getGoogleStyle();
5345   Style.ColumnLimit = 45;
5346   verifyFormat("Debug(\n"
5347                "    aaaaa,\n"
5348                "    {\n"
5349                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5350                "    },\n"
5351                "    a);",
5352                Style);
5353 
5354   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5355 
5356   verifyNoCrash("^{v^{a}}");
5357 }
5358 
5359 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5360   EXPECT_EQ("#define MACRO()                     \\\n"
5361             "  Debug(aaa, /* force line break */ \\\n"
5362             "        {                           \\\n"
5363             "          int i;                    \\\n"
5364             "          int j;                    \\\n"
5365             "        })",
5366             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5367                    "          {  int   i;  int  j;   })",
5368                    getGoogleStyle()));
5369 
5370   EXPECT_EQ("#define A                                       \\\n"
5371             "  [] {                                          \\\n"
5372             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5373             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5374             "  }",
5375             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5376                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5377                    getGoogleStyle()));
5378 }
5379 
5380 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5381   EXPECT_EQ("{}", format("{}"));
5382   verifyFormat("enum E {};");
5383   verifyFormat("enum E {}");
5384   FormatStyle Style = getLLVMStyle();
5385   Style.SpaceInEmptyBlock = true;
5386   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5387   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5388   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5389 }
5390 
5391 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5392   FormatStyle Style = getLLVMStyle();
5393   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5394   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5395   verifyFormat("FOO_BEGIN\n"
5396                "  FOO_ENTRY\n"
5397                "FOO_END",
5398                Style);
5399   verifyFormat("FOO_BEGIN\n"
5400                "  NESTED_FOO_BEGIN\n"
5401                "    NESTED_FOO_ENTRY\n"
5402                "  NESTED_FOO_END\n"
5403                "FOO_END",
5404                Style);
5405   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5406                "  int x;\n"
5407                "  x = 1;\n"
5408                "FOO_END(Baz)",
5409                Style);
5410 }
5411 
5412 //===----------------------------------------------------------------------===//
5413 // Line break tests.
5414 //===----------------------------------------------------------------------===//
5415 
5416 TEST_F(FormatTest, PreventConfusingIndents) {
5417   verifyFormat(
5418       "void f() {\n"
5419       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5420       "                         parameter, parameter, parameter)),\n"
5421       "                     SecondLongCall(parameter));\n"
5422       "}");
5423   verifyFormat(
5424       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5425       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5426       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5427       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5428   verifyFormat(
5429       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5430       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5431       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5432       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5433   verifyFormat(
5434       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5435       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5436       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5437       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5438   verifyFormat("int a = bbbb && ccc &&\n"
5439                "        fffff(\n"
5440                "#define A Just forcing a new line\n"
5441                "            ddd);");
5442 }
5443 
5444 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5445   verifyFormat(
5446       "bool aaaaaaa =\n"
5447       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5448       "    bbbbbbbb();");
5449   verifyFormat(
5450       "bool aaaaaaa =\n"
5451       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5452       "    bbbbbbbb();");
5453 
5454   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5455                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5456                "    ccccccccc == ddddddddddd;");
5457   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5458                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5459                "    ccccccccc == ddddddddddd;");
5460   verifyFormat(
5461       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5462       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5463       "    ccccccccc == ddddddddddd;");
5464 
5465   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5466                "                 aaaaaa) &&\n"
5467                "         bbbbbb && cccccc;");
5468   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5469                "                 aaaaaa) >>\n"
5470                "         bbbbbb;");
5471   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5472                "    SourceMgr.getSpellingColumnNumber(\n"
5473                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5474                "    1);");
5475 
5476   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5477                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5478                "    cccccc) {\n}");
5479   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5480                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5481                "              cccccc) {\n}");
5482   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5483                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5484                "              cccccc) {\n}");
5485   verifyFormat("b = a &&\n"
5486                "    // Comment\n"
5487                "    b.c && d;");
5488 
5489   // If the LHS of a comparison is not a binary expression itself, the
5490   // additional linebreak confuses many people.
5491   verifyFormat(
5492       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5493       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5494       "}");
5495   verifyFormat(
5496       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5497       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5498       "}");
5499   verifyFormat(
5500       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5501       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5502       "}");
5503   verifyFormat(
5504       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5505       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5506       "}");
5507   // Even explicit parentheses stress the precedence enough to make the
5508   // additional break unnecessary.
5509   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5510                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5511                "}");
5512   // This cases is borderline, but with the indentation it is still readable.
5513   verifyFormat(
5514       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5515       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5516       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5517       "}",
5518       getLLVMStyleWithColumns(75));
5519 
5520   // If the LHS is a binary expression, we should still use the additional break
5521   // as otherwise the formatting hides the operator precedence.
5522   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5523                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5524                "    5) {\n"
5525                "}");
5526   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5527                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5528                "    5) {\n"
5529                "}");
5530 
5531   FormatStyle OnePerLine = getLLVMStyle();
5532   OnePerLine.BinPackParameters = false;
5533   verifyFormat(
5534       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5535       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5536       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5537       OnePerLine);
5538 
5539   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5540                "                .aaa(aaaaaaaaaaaaa) *\n"
5541                "            aaaaaaa +\n"
5542                "        aaaaaaa;",
5543                getLLVMStyleWithColumns(40));
5544 }
5545 
5546 TEST_F(FormatTest, ExpressionIndentation) {
5547   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5548                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5549                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5550                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5551                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5552                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5553                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5554                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5555                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5556   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5557                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5558                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5559                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5560   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5561                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5562                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5563                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5564   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5565                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5566                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5567                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5568   verifyFormat("if () {\n"
5569                "} else if (aaaaa && bbbbb > // break\n"
5570                "                        ccccc) {\n"
5571                "}");
5572   verifyFormat("if () {\n"
5573                "} else if constexpr (aaaaa && bbbbb > // break\n"
5574                "                                  ccccc) {\n"
5575                "}");
5576   verifyFormat("if () {\n"
5577                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5578                "                                  ccccc) {\n"
5579                "}");
5580   verifyFormat("if () {\n"
5581                "} else if (aaaaa &&\n"
5582                "           bbbbb > // break\n"
5583                "               ccccc &&\n"
5584                "           ddddd) {\n"
5585                "}");
5586 
5587   // Presence of a trailing comment used to change indentation of b.
5588   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
5589                "       b;\n"
5590                "return aaaaaaaaaaaaaaaaaaa +\n"
5591                "       b; //",
5592                getLLVMStyleWithColumns(30));
5593 }
5594 
5595 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
5596   // Not sure what the best system is here. Like this, the LHS can be found
5597   // immediately above an operator (everything with the same or a higher
5598   // indent). The RHS is aligned right of the operator and so compasses
5599   // everything until something with the same indent as the operator is found.
5600   // FIXME: Is this a good system?
5601   FormatStyle Style = getLLVMStyle();
5602   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5603   verifyFormat(
5604       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5605       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5606       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5607       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5608       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5609       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5610       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5611       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5612       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
5613       Style);
5614   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5615                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5616                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5617                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5618                Style);
5619   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5620                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5621                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5622                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5623                Style);
5624   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5625                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5626                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5627                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5628                Style);
5629   verifyFormat("if () {\n"
5630                "} else if (aaaaa\n"
5631                "           && bbbbb // break\n"
5632                "                  > ccccc) {\n"
5633                "}",
5634                Style);
5635   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5636                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5637                Style);
5638   verifyFormat("return (a)\n"
5639                "       // comment\n"
5640                "       + b;",
5641                Style);
5642   verifyFormat(
5643       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5644       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5645       "             + cc;",
5646       Style);
5647 
5648   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5649                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5650                Style);
5651 
5652   // Forced by comments.
5653   verifyFormat(
5654       "unsigned ContentSize =\n"
5655       "    sizeof(int16_t)   // DWARF ARange version number\n"
5656       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5657       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5658       "    + sizeof(int8_t); // Segment Size (in bytes)");
5659 
5660   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5661                "       == boost::fusion::at_c<1>(iiii).second;",
5662                Style);
5663 
5664   Style.ColumnLimit = 60;
5665   verifyFormat("zzzzzzzzzz\n"
5666                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5667                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5668                Style);
5669 
5670   Style.ColumnLimit = 80;
5671   Style.IndentWidth = 4;
5672   Style.TabWidth = 4;
5673   Style.UseTab = FormatStyle::UT_Always;
5674   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5675   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5676   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
5677             "\t&& (someOtherLongishConditionPart1\n"
5678             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
5679             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
5680                    "(someOtherLongishConditionPart1 || "
5681                    "someOtherEvenLongerNestedConditionPart2);",
5682                    Style));
5683 }
5684 
5685 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
5686   FormatStyle Style = getLLVMStyle();
5687   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5688   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
5689 
5690   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5691                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5692                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5693                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5694                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5695                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5696                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5697                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5698                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
5699                Style);
5700   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5701                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5702                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5703                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5704                Style);
5705   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5706                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5707                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5708                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5709                Style);
5710   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5711                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5712                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5713                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5714                Style);
5715   verifyFormat("if () {\n"
5716                "} else if (aaaaa\n"
5717                "           && bbbbb // break\n"
5718                "                  > ccccc) {\n"
5719                "}",
5720                Style);
5721   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5722                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5723                Style);
5724   verifyFormat("return (a)\n"
5725                "     // comment\n"
5726                "     + b;",
5727                Style);
5728   verifyFormat(
5729       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5730       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5731       "           + cc;",
5732       Style);
5733   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
5734                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5735                "                        : 3333333333333333;",
5736                Style);
5737   verifyFormat(
5738       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
5739       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
5740       "                                             : eeeeeeeeeeeeeeeeee)\n"
5741       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5742       "                        : 3333333333333333;",
5743       Style);
5744   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5745                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5746                Style);
5747 
5748   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5749                "    == boost::fusion::at_c<1>(iiii).second;",
5750                Style);
5751 
5752   Style.ColumnLimit = 60;
5753   verifyFormat("zzzzzzzzzzzzz\n"
5754                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5755                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5756                Style);
5757 
5758   // Forced by comments.
5759   Style.ColumnLimit = 80;
5760   verifyFormat(
5761       "unsigned ContentSize\n"
5762       "    = sizeof(int16_t) // DWARF ARange version number\n"
5763       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5764       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5765       "    + sizeof(int8_t); // Segment Size (in bytes)",
5766       Style);
5767 
5768   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5769   verifyFormat(
5770       "unsigned ContentSize =\n"
5771       "    sizeof(int16_t)   // DWARF ARange version number\n"
5772       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5773       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5774       "    + sizeof(int8_t); // Segment Size (in bytes)",
5775       Style);
5776 
5777   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5778   verifyFormat(
5779       "unsigned ContentSize =\n"
5780       "    sizeof(int16_t)   // DWARF ARange version number\n"
5781       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5782       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5783       "    + sizeof(int8_t); // Segment Size (in bytes)",
5784       Style);
5785 }
5786 
5787 TEST_F(FormatTest, EnforcedOperatorWraps) {
5788   // Here we'd like to wrap after the || operators, but a comment is forcing an
5789   // earlier wrap.
5790   verifyFormat("bool x = aaaaa //\n"
5791                "         || bbbbb\n"
5792                "         //\n"
5793                "         || cccc;");
5794 }
5795 
5796 TEST_F(FormatTest, NoOperandAlignment) {
5797   FormatStyle Style = getLLVMStyle();
5798   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5799   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
5800                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5801                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5802                Style);
5803   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5804   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5805                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5806                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5807                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5808                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5809                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5810                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5811                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5812                "        > ccccccccccccccccccccccccccccccccccccccccc;",
5813                Style);
5814 
5815   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5816                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5817                "    + cc;",
5818                Style);
5819   verifyFormat("int a = aa\n"
5820                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5821                "        * cccccccccccccccccccccccccccccccccccc;\n",
5822                Style);
5823 
5824   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5825   verifyFormat("return (a > b\n"
5826                "    // comment1\n"
5827                "    // comment2\n"
5828                "    || c);",
5829                Style);
5830 }
5831 
5832 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5833   FormatStyle Style = getLLVMStyle();
5834   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5835   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5836                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5837                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5838                Style);
5839 }
5840 
5841 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
5842   FormatStyle Style = getLLVMStyle();
5843   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5844   Style.BinPackArguments = false;
5845   Style.ColumnLimit = 40;
5846   verifyFormat("void test() {\n"
5847                "  someFunction(\n"
5848                "      this + argument + is + quite\n"
5849                "      + long + so + it + gets + wrapped\n"
5850                "      + but + remains + bin - packed);\n"
5851                "}",
5852                Style);
5853   verifyFormat("void test() {\n"
5854                "  someFunction(arg1,\n"
5855                "               this + argument + is\n"
5856                "                   + quite + long + so\n"
5857                "                   + it + gets + wrapped\n"
5858                "                   + but + remains + bin\n"
5859                "                   - packed,\n"
5860                "               arg3);\n"
5861                "}",
5862                Style);
5863   verifyFormat("void test() {\n"
5864                "  someFunction(\n"
5865                "      arg1,\n"
5866                "      this + argument + has\n"
5867                "          + anotherFunc(nested,\n"
5868                "                        calls + whose\n"
5869                "                            + arguments\n"
5870                "                            + are + also\n"
5871                "                            + wrapped,\n"
5872                "                        in + addition)\n"
5873                "          + to + being + bin - packed,\n"
5874                "      arg3);\n"
5875                "}",
5876                Style);
5877 
5878   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5879   verifyFormat("void test() {\n"
5880                "  someFunction(\n"
5881                "      arg1,\n"
5882                "      this + argument + has +\n"
5883                "          anotherFunc(nested,\n"
5884                "                      calls + whose +\n"
5885                "                          arguments +\n"
5886                "                          are + also +\n"
5887                "                          wrapped,\n"
5888                "                      in + addition) +\n"
5889                "          to + being + bin - packed,\n"
5890                "      arg3);\n"
5891                "}",
5892                Style);
5893 }
5894 
5895 TEST_F(FormatTest, ConstructorInitializers) {
5896   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5897   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
5898                getLLVMStyleWithColumns(45));
5899   verifyFormat("Constructor()\n"
5900                "    : Inttializer(FitsOnTheLine) {}",
5901                getLLVMStyleWithColumns(44));
5902   verifyFormat("Constructor()\n"
5903                "    : Inttializer(FitsOnTheLine) {}",
5904                getLLVMStyleWithColumns(43));
5905 
5906   verifyFormat("template <typename T>\n"
5907                "Constructor() : Initializer(FitsOnTheLine) {}",
5908                getLLVMStyleWithColumns(45));
5909 
5910   verifyFormat(
5911       "SomeClass::Constructor()\n"
5912       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5913 
5914   verifyFormat(
5915       "SomeClass::Constructor()\n"
5916       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5917       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
5918   verifyFormat(
5919       "SomeClass::Constructor()\n"
5920       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5921       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5922   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5923                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5924                "    : aaaaaaaaaa(aaaaaa) {}");
5925 
5926   verifyFormat("Constructor()\n"
5927                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5928                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5929                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5930                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
5931 
5932   verifyFormat("Constructor()\n"
5933                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5934                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5935 
5936   verifyFormat("Constructor(int Parameter = 0)\n"
5937                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5938                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
5939   verifyFormat("Constructor()\n"
5940                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5941                "}",
5942                getLLVMStyleWithColumns(60));
5943   verifyFormat("Constructor()\n"
5944                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5945                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
5946 
5947   // Here a line could be saved by splitting the second initializer onto two
5948   // lines, but that is not desirable.
5949   verifyFormat("Constructor()\n"
5950                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5951                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
5952                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5953 
5954   FormatStyle OnePerLine = getLLVMStyle();
5955   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5956   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
5957   verifyFormat("SomeClass::Constructor()\n"
5958                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5959                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5960                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5961                OnePerLine);
5962   verifyFormat("SomeClass::Constructor()\n"
5963                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
5964                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5965                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5966                OnePerLine);
5967   verifyFormat("MyClass::MyClass(int var)\n"
5968                "    : some_var_(var),            // 4 space indent\n"
5969                "      some_other_var_(var + 1) { // lined up\n"
5970                "}",
5971                OnePerLine);
5972   verifyFormat("Constructor()\n"
5973                "    : aaaaa(aaaaaa),\n"
5974                "      aaaaa(aaaaaa),\n"
5975                "      aaaaa(aaaaaa),\n"
5976                "      aaaaa(aaaaaa),\n"
5977                "      aaaaa(aaaaaa) {}",
5978                OnePerLine);
5979   verifyFormat("Constructor()\n"
5980                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5981                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
5982                OnePerLine);
5983   OnePerLine.BinPackParameters = false;
5984   verifyFormat(
5985       "Constructor()\n"
5986       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5987       "          aaaaaaaaaaa().aaa(),\n"
5988       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5989       OnePerLine);
5990   OnePerLine.ColumnLimit = 60;
5991   verifyFormat("Constructor()\n"
5992                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5993                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
5994                OnePerLine);
5995 
5996   EXPECT_EQ("Constructor()\n"
5997             "    : // Comment forcing unwanted break.\n"
5998             "      aaaa(aaaa) {}",
5999             format("Constructor() :\n"
6000                    "    // Comment forcing unwanted break.\n"
6001                    "    aaaa(aaaa) {}"));
6002 }
6003 
6004 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6005   FormatStyle Style = getLLVMStyle();
6006   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6007   Style.ColumnLimit = 60;
6008   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6009   Style.AllowAllConstructorInitializersOnNextLine = true;
6010   Style.BinPackParameters = false;
6011 
6012   for (int i = 0; i < 4; ++i) {
6013     // Test all combinations of parameters that should not have an effect.
6014     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6015     Style.AllowAllArgumentsOnNextLine = i & 2;
6016 
6017     Style.AllowAllConstructorInitializersOnNextLine = true;
6018     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6019     verifyFormat("Constructor()\n"
6020                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6021                  Style);
6022     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6023 
6024     Style.AllowAllConstructorInitializersOnNextLine = false;
6025     verifyFormat("Constructor()\n"
6026                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6027                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6028                  Style);
6029     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6030 
6031     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6032     Style.AllowAllConstructorInitializersOnNextLine = true;
6033     verifyFormat("Constructor()\n"
6034                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6035                  Style);
6036 
6037     Style.AllowAllConstructorInitializersOnNextLine = false;
6038     verifyFormat("Constructor()\n"
6039                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6040                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6041                  Style);
6042 
6043     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6044     Style.AllowAllConstructorInitializersOnNextLine = true;
6045     verifyFormat("Constructor() :\n"
6046                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6047                  Style);
6048 
6049     Style.AllowAllConstructorInitializersOnNextLine = false;
6050     verifyFormat("Constructor() :\n"
6051                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6052                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6053                  Style);
6054   }
6055 
6056   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6057   // AllowAllConstructorInitializersOnNextLine in all
6058   // BreakConstructorInitializers modes
6059   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6060   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6061   Style.AllowAllConstructorInitializersOnNextLine = false;
6062   verifyFormat("SomeClassWithALongName::Constructor(\n"
6063                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6064                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6065                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6066                Style);
6067 
6068   Style.AllowAllConstructorInitializersOnNextLine = true;
6069   verifyFormat("SomeClassWithALongName::Constructor(\n"
6070                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6071                "    int bbbbbbbbbbbbb,\n"
6072                "    int cccccccccccccccc)\n"
6073                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6074                Style);
6075 
6076   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6077   Style.AllowAllConstructorInitializersOnNextLine = false;
6078   verifyFormat("SomeClassWithALongName::Constructor(\n"
6079                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6080                "    int bbbbbbbbbbbbb)\n"
6081                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6082                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6083                Style);
6084 
6085   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6086 
6087   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6088   verifyFormat("SomeClassWithALongName::Constructor(\n"
6089                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6090                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6091                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6092                Style);
6093 
6094   Style.AllowAllConstructorInitializersOnNextLine = true;
6095   verifyFormat("SomeClassWithALongName::Constructor(\n"
6096                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6097                "    int bbbbbbbbbbbbb,\n"
6098                "    int cccccccccccccccc)\n"
6099                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6100                Style);
6101 
6102   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6103   Style.AllowAllConstructorInitializersOnNextLine = false;
6104   verifyFormat("SomeClassWithALongName::Constructor(\n"
6105                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6106                "    int bbbbbbbbbbbbb)\n"
6107                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6108                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6109                Style);
6110 
6111   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6112   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6113   verifyFormat("SomeClassWithALongName::Constructor(\n"
6114                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6115                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6116                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6117                Style);
6118 
6119   Style.AllowAllConstructorInitializersOnNextLine = true;
6120   verifyFormat("SomeClassWithALongName::Constructor(\n"
6121                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6122                "    int bbbbbbbbbbbbb,\n"
6123                "    int cccccccccccccccc) :\n"
6124                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6125                Style);
6126 
6127   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6128   Style.AllowAllConstructorInitializersOnNextLine = false;
6129   verifyFormat("SomeClassWithALongName::Constructor(\n"
6130                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6131                "    int bbbbbbbbbbbbb) :\n"
6132                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6133                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6134                Style);
6135 }
6136 
6137 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6138   FormatStyle Style = getLLVMStyle();
6139   Style.ColumnLimit = 60;
6140   Style.BinPackArguments = false;
6141   for (int i = 0; i < 4; ++i) {
6142     // Test all combinations of parameters that should not have an effect.
6143     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6144     Style.AllowAllConstructorInitializersOnNextLine = i & 2;
6145 
6146     Style.AllowAllArgumentsOnNextLine = true;
6147     verifyFormat("void foo() {\n"
6148                  "  FunctionCallWithReallyLongName(\n"
6149                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6150                  "}",
6151                  Style);
6152     Style.AllowAllArgumentsOnNextLine = false;
6153     verifyFormat("void foo() {\n"
6154                  "  FunctionCallWithReallyLongName(\n"
6155                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6156                  "      bbbbbbbbbbbb);\n"
6157                  "}",
6158                  Style);
6159 
6160     Style.AllowAllArgumentsOnNextLine = true;
6161     verifyFormat("void foo() {\n"
6162                  "  auto VariableWithReallyLongName = {\n"
6163                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6164                  "}",
6165                  Style);
6166     Style.AllowAllArgumentsOnNextLine = false;
6167     verifyFormat("void foo() {\n"
6168                  "  auto VariableWithReallyLongName = {\n"
6169                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6170                  "      bbbbbbbbbbbb};\n"
6171                  "}",
6172                  Style);
6173   }
6174 
6175   // This parameter should not affect declarations.
6176   Style.BinPackParameters = false;
6177   Style.AllowAllArgumentsOnNextLine = false;
6178   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6179   verifyFormat("void FunctionCallWithReallyLongName(\n"
6180                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6181                Style);
6182   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6183   verifyFormat("void FunctionCallWithReallyLongName(\n"
6184                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6185                "    int bbbbbbbbbbbb);",
6186                Style);
6187 }
6188 
6189 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6190   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6191   // and BAS_Align.
6192   auto Style = getLLVMStyle();
6193   Style.ColumnLimit = 35;
6194   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6195                     "void functionDecl(int A, int B, int C);";
6196   Style.AllowAllArgumentsOnNextLine = false;
6197   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6198   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6199                       "    paramC);\n"
6200                       "void functionDecl(int A, int B,\n"
6201                       "    int C);"),
6202             format(Input, Style));
6203   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6204   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6205                       "             paramC);\n"
6206                       "void functionDecl(int A, int B,\n"
6207                       "                  int C);"),
6208             format(Input, Style));
6209   // However, BAS_AlwaysBreak should take precedence over
6210   // AllowAllArgumentsOnNextLine.
6211   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6212   EXPECT_EQ(StringRef("functionCall(\n"
6213                       "    paramA, paramB, paramC);\n"
6214                       "void functionDecl(\n"
6215                       "    int A, int B, int C);"),
6216             format(Input, Style));
6217 
6218   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6219   // first argument.
6220   Style.AllowAllArgumentsOnNextLine = true;
6221   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6222   EXPECT_EQ(StringRef("functionCall(\n"
6223                       "    paramA, paramB, paramC);\n"
6224                       "void functionDecl(\n"
6225                       "    int A, int B, int C);"),
6226             format(Input, Style));
6227   // It wouldn't fit on one line with aligned parameters so this setting
6228   // doesn't change anything for BAS_Align.
6229   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6230   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6231                       "             paramC);\n"
6232                       "void functionDecl(int A, int B,\n"
6233                       "                  int C);"),
6234             format(Input, Style));
6235   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6236   EXPECT_EQ(StringRef("functionCall(\n"
6237                       "    paramA, paramB, paramC);\n"
6238                       "void functionDecl(\n"
6239                       "    int A, int B, int C);"),
6240             format(Input, Style));
6241 }
6242 
6243 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6244   FormatStyle Style = getLLVMStyle();
6245   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6246 
6247   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6248   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6249                getStyleWithColumns(Style, 45));
6250   verifyFormat("Constructor() :\n"
6251                "    Initializer(FitsOnTheLine) {}",
6252                getStyleWithColumns(Style, 44));
6253   verifyFormat("Constructor() :\n"
6254                "    Initializer(FitsOnTheLine) {}",
6255                getStyleWithColumns(Style, 43));
6256 
6257   verifyFormat("template <typename T>\n"
6258                "Constructor() : Initializer(FitsOnTheLine) {}",
6259                getStyleWithColumns(Style, 50));
6260   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6261   verifyFormat(
6262       "SomeClass::Constructor() :\n"
6263       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6264       Style);
6265 
6266   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
6267   verifyFormat(
6268       "SomeClass::Constructor() :\n"
6269       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6270       Style);
6271 
6272   verifyFormat(
6273       "SomeClass::Constructor() :\n"
6274       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6275       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6276       Style);
6277   verifyFormat(
6278       "SomeClass::Constructor() :\n"
6279       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6280       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6281       Style);
6282   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6283                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6284                "    aaaaaaaaaa(aaaaaa) {}",
6285                Style);
6286 
6287   verifyFormat("Constructor() :\n"
6288                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6289                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6290                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6291                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6292                Style);
6293 
6294   verifyFormat("Constructor() :\n"
6295                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6296                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6297                Style);
6298 
6299   verifyFormat("Constructor(int Parameter = 0) :\n"
6300                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6301                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6302                Style);
6303   verifyFormat("Constructor() :\n"
6304                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6305                "}",
6306                getStyleWithColumns(Style, 60));
6307   verifyFormat("Constructor() :\n"
6308                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6309                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6310                Style);
6311 
6312   // Here a line could be saved by splitting the second initializer onto two
6313   // lines, but that is not desirable.
6314   verifyFormat("Constructor() :\n"
6315                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6316                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6317                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6318                Style);
6319 
6320   FormatStyle OnePerLine = Style;
6321   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6322   OnePerLine.AllowAllConstructorInitializersOnNextLine = false;
6323   verifyFormat("SomeClass::Constructor() :\n"
6324                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6325                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6326                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6327                OnePerLine);
6328   verifyFormat("SomeClass::Constructor() :\n"
6329                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6330                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6331                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6332                OnePerLine);
6333   verifyFormat("MyClass::MyClass(int var) :\n"
6334                "    some_var_(var),            // 4 space indent\n"
6335                "    some_other_var_(var + 1) { // lined up\n"
6336                "}",
6337                OnePerLine);
6338   verifyFormat("Constructor() :\n"
6339                "    aaaaa(aaaaaa),\n"
6340                "    aaaaa(aaaaaa),\n"
6341                "    aaaaa(aaaaaa),\n"
6342                "    aaaaa(aaaaaa),\n"
6343                "    aaaaa(aaaaaa) {}",
6344                OnePerLine);
6345   verifyFormat("Constructor() :\n"
6346                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6347                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6348                OnePerLine);
6349   OnePerLine.BinPackParameters = false;
6350   verifyFormat("Constructor() :\n"
6351                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6352                "        aaaaaaaaaaa().aaa(),\n"
6353                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6354                OnePerLine);
6355   OnePerLine.ColumnLimit = 60;
6356   verifyFormat("Constructor() :\n"
6357                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6358                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6359                OnePerLine);
6360 
6361   EXPECT_EQ("Constructor() :\n"
6362             "    // Comment forcing unwanted break.\n"
6363             "    aaaa(aaaa) {}",
6364             format("Constructor() :\n"
6365                    "    // Comment forcing unwanted break.\n"
6366                    "    aaaa(aaaa) {}",
6367                    Style));
6368 
6369   Style.ColumnLimit = 0;
6370   verifyFormat("SomeClass::Constructor() :\n"
6371                "    a(a) {}",
6372                Style);
6373   verifyFormat("SomeClass::Constructor() noexcept :\n"
6374                "    a(a) {}",
6375                Style);
6376   verifyFormat("SomeClass::Constructor() :\n"
6377                "    a(a), b(b), c(c) {}",
6378                Style);
6379   verifyFormat("SomeClass::Constructor() :\n"
6380                "    a(a) {\n"
6381                "  foo();\n"
6382                "  bar();\n"
6383                "}",
6384                Style);
6385 
6386   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6387   verifyFormat("SomeClass::Constructor() :\n"
6388                "    a(a), b(b), c(c) {\n"
6389                "}",
6390                Style);
6391   verifyFormat("SomeClass::Constructor() :\n"
6392                "    a(a) {\n"
6393                "}",
6394                Style);
6395 
6396   Style.ColumnLimit = 80;
6397   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6398   Style.ConstructorInitializerIndentWidth = 2;
6399   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6400   verifyFormat("SomeClass::Constructor() :\n"
6401                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6402                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6403                Style);
6404 
6405   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6406   // well
6407   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6408   verifyFormat(
6409       "class SomeClass\n"
6410       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6411       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6412       Style);
6413   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6414   verifyFormat(
6415       "class SomeClass\n"
6416       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6417       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6418       Style);
6419   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6420   verifyFormat(
6421       "class SomeClass :\n"
6422       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6423       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6424       Style);
6425   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6426   verifyFormat(
6427       "class SomeClass\n"
6428       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6429       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6430       Style);
6431 }
6432 
6433 #ifndef EXPENSIVE_CHECKS
6434 // Expensive checks enables libstdc++ checking which includes validating the
6435 // state of ranges used in std::priority_queue - this blows out the
6436 // runtime/scalability of the function and makes this test unacceptably slow.
6437 TEST_F(FormatTest, MemoizationTests) {
6438   // This breaks if the memoization lookup does not take \c Indent and
6439   // \c LastSpace into account.
6440   verifyFormat(
6441       "extern CFRunLoopTimerRef\n"
6442       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6443       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6444       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6445       "                     CFRunLoopTimerContext *context) {}");
6446 
6447   // Deep nesting somewhat works around our memoization.
6448   verifyFormat(
6449       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6450       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6451       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6452       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6453       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6454       getLLVMStyleWithColumns(65));
6455   verifyFormat(
6456       "aaaaa(\n"
6457       "    aaaaa,\n"
6458       "    aaaaa(\n"
6459       "        aaaaa,\n"
6460       "        aaaaa(\n"
6461       "            aaaaa,\n"
6462       "            aaaaa(\n"
6463       "                aaaaa,\n"
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))))))))))));",
6481       getLLVMStyleWithColumns(65));
6482   verifyFormat(
6483       "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"
6484       "                                  a),\n"
6485       "                                a),\n"
6486       "                              a),\n"
6487       "                            a),\n"
6488       "                          a),\n"
6489       "                        a),\n"
6490       "                      a),\n"
6491       "                    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)",
6501       getLLVMStyleWithColumns(65));
6502 
6503   // This test takes VERY long when memoization is broken.
6504   FormatStyle OnePerLine = getLLVMStyle();
6505   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6506   OnePerLine.BinPackParameters = false;
6507   std::string input = "Constructor()\n"
6508                       "    : aaaa(a,\n";
6509   for (unsigned i = 0, e = 80; i != e; ++i) {
6510     input += "           a,\n";
6511   }
6512   input += "           a) {}";
6513   verifyFormat(input, OnePerLine);
6514 }
6515 #endif
6516 
6517 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6518   verifyFormat(
6519       "void f() {\n"
6520       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6521       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6522       "    f();\n"
6523       "}");
6524   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6525                "    Intervals[i - 1].getRange().getLast()) {\n}");
6526 }
6527 
6528 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6529   // Principially, we break function declarations in a certain order:
6530   // 1) break amongst arguments.
6531   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6532                "                              Cccccccccccccc cccccccccccccc);");
6533   verifyFormat("template <class TemplateIt>\n"
6534                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6535                "                            TemplateIt *stop) {}");
6536 
6537   // 2) break after return type.
6538   verifyFormat(
6539       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6540       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6541       getGoogleStyle());
6542 
6543   // 3) break after (.
6544   verifyFormat(
6545       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6546       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6547       getGoogleStyle());
6548 
6549   // 4) break before after nested name specifiers.
6550   verifyFormat(
6551       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6552       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6553       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6554       getGoogleStyle());
6555 
6556   // However, there are exceptions, if a sufficient amount of lines can be
6557   // saved.
6558   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6559   // more adjusting.
6560   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6561                "                                  Cccccccccccccc cccccccccc,\n"
6562                "                                  Cccccccccccccc cccccccccc,\n"
6563                "                                  Cccccccccccccc cccccccccc,\n"
6564                "                                  Cccccccccccccc cccccccccc);");
6565   verifyFormat(
6566       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6567       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6568       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6569       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6570       getGoogleStyle());
6571   verifyFormat(
6572       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6573       "                                          Cccccccccccccc cccccccccc,\n"
6574       "                                          Cccccccccccccc cccccccccc,\n"
6575       "                                          Cccccccccccccc cccccccccc,\n"
6576       "                                          Cccccccccccccc cccccccccc,\n"
6577       "                                          Cccccccccccccc cccccccccc,\n"
6578       "                                          Cccccccccccccc cccccccccc);");
6579   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6580                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6581                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6582                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6583                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
6584 
6585   // Break after multi-line parameters.
6586   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6587                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6588                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6589                "    bbbb bbbb);");
6590   verifyFormat("void SomeLoooooooooooongFunction(\n"
6591                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6592                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6593                "    int bbbbbbbbbbbbb);");
6594 
6595   // Treat overloaded operators like other functions.
6596   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6597                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
6598   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6599                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
6600   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6601                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
6602   verifyGoogleFormat(
6603       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
6604       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6605   verifyGoogleFormat(
6606       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
6607       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6608   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6609                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6610   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
6611                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6612   verifyGoogleFormat(
6613       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
6614       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6615       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
6616   verifyGoogleFormat("template <typename T>\n"
6617                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6618                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
6619                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
6620 
6621   FormatStyle Style = getLLVMStyle();
6622   Style.PointerAlignment = FormatStyle::PAS_Left;
6623   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6624                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
6625                Style);
6626   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
6627                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6628                Style);
6629 }
6630 
6631 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
6632   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
6633   // Prefer keeping `::` followed by `operator` together.
6634   EXPECT_EQ("const aaaa::bbbbbbb &\n"
6635             "ccccccccc::operator++() {\n"
6636             "  stuff();\n"
6637             "}",
6638             format("const aaaa::bbbbbbb\n"
6639                    "&ccccccccc::operator++() { stuff(); }",
6640                    getLLVMStyleWithColumns(40)));
6641 }
6642 
6643 TEST_F(FormatTest, TrailingReturnType) {
6644   verifyFormat("auto foo() -> int;\n");
6645   // correct trailing return type spacing
6646   verifyFormat("auto operator->() -> int;\n");
6647   verifyFormat("auto operator++(int) -> int;\n");
6648 
6649   verifyFormat("struct S {\n"
6650                "  auto bar() const -> int;\n"
6651                "};");
6652   verifyFormat("template <size_t Order, typename T>\n"
6653                "auto load_img(const std::string &filename)\n"
6654                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
6655   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
6656                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
6657   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
6658   verifyFormat("template <typename T>\n"
6659                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
6660                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
6661 
6662   // Not trailing return types.
6663   verifyFormat("void f() { auto a = b->c(); }");
6664 }
6665 
6666 TEST_F(FormatTest, DeductionGuides) {
6667   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
6668   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
6669   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
6670   verifyFormat(
6671       "template <class... T>\n"
6672       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
6673   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
6674   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
6675   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
6676   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
6677   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
6678   verifyFormat("template <class T> x() -> x<1>;");
6679   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
6680 
6681   // Ensure not deduction guides.
6682   verifyFormat("c()->f<int>();");
6683   verifyFormat("x()->foo<1>;");
6684   verifyFormat("x = p->foo<3>();");
6685   verifyFormat("x()->x<1>();");
6686   verifyFormat("x()->x<1>;");
6687 }
6688 
6689 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
6690   // Avoid breaking before trailing 'const' or other trailing annotations, if
6691   // they are not function-like.
6692   FormatStyle Style = getGoogleStyle();
6693   Style.ColumnLimit = 47;
6694   verifyFormat("void someLongFunction(\n"
6695                "    int someLoooooooooooooongParameter) const {\n}",
6696                getLLVMStyleWithColumns(47));
6697   verifyFormat("LoooooongReturnType\n"
6698                "someLoooooooongFunction() const {}",
6699                getLLVMStyleWithColumns(47));
6700   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
6701                "    const {}",
6702                Style);
6703   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6704                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
6705   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6706                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
6707   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6708                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
6709   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
6710                "                   aaaaaaaaaaa aaaaa) const override;");
6711   verifyGoogleFormat(
6712       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6713       "    const override;");
6714 
6715   // Even if the first parameter has to be wrapped.
6716   verifyFormat("void someLongFunction(\n"
6717                "    int someLongParameter) const {}",
6718                getLLVMStyleWithColumns(46));
6719   verifyFormat("void someLongFunction(\n"
6720                "    int someLongParameter) const {}",
6721                Style);
6722   verifyFormat("void someLongFunction(\n"
6723                "    int someLongParameter) override {}",
6724                Style);
6725   verifyFormat("void someLongFunction(\n"
6726                "    int someLongParameter) OVERRIDE {}",
6727                Style);
6728   verifyFormat("void someLongFunction(\n"
6729                "    int someLongParameter) final {}",
6730                Style);
6731   verifyFormat("void someLongFunction(\n"
6732                "    int someLongParameter) FINAL {}",
6733                Style);
6734   verifyFormat("void someLongFunction(\n"
6735                "    int parameter) const override {}",
6736                Style);
6737 
6738   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
6739   verifyFormat("void someLongFunction(\n"
6740                "    int someLongParameter) const\n"
6741                "{\n"
6742                "}",
6743                Style);
6744 
6745   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
6746   verifyFormat("void someLongFunction(\n"
6747                "    int someLongParameter) const\n"
6748                "  {\n"
6749                "  }",
6750                Style);
6751 
6752   // Unless these are unknown annotations.
6753   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
6754                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6755                "    LONG_AND_UGLY_ANNOTATION;");
6756 
6757   // Breaking before function-like trailing annotations is fine to keep them
6758   // close to their arguments.
6759   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6760                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6761   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6762                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6763   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6764                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
6765   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
6766                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
6767   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
6768 
6769   verifyFormat(
6770       "void aaaaaaaaaaaaaaaaaa()\n"
6771       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
6772       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
6773   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6774                "    __attribute__((unused));");
6775   verifyGoogleFormat(
6776       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6777       "    GUARDED_BY(aaaaaaaaaaaa);");
6778   verifyGoogleFormat(
6779       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6780       "    GUARDED_BY(aaaaaaaaaaaa);");
6781   verifyGoogleFormat(
6782       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6783       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6784   verifyGoogleFormat(
6785       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6786       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
6787 }
6788 
6789 TEST_F(FormatTest, FunctionAnnotations) {
6790   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6791                "int OldFunction(const string &parameter) {}");
6792   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6793                "string OldFunction(const string &parameter) {}");
6794   verifyFormat("template <typename T>\n"
6795                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6796                "string OldFunction(const string &parameter) {}");
6797 
6798   // Not function annotations.
6799   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6800                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
6801   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
6802                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
6803   verifyFormat("MACRO(abc).function() // wrap\n"
6804                "    << abc;");
6805   verifyFormat("MACRO(abc)->function() // wrap\n"
6806                "    << abc;");
6807   verifyFormat("MACRO(abc)::function() // wrap\n"
6808                "    << abc;");
6809 }
6810 
6811 TEST_F(FormatTest, BreaksDesireably) {
6812   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6813                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6814                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6815   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6816                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6817                "}");
6818 
6819   verifyFormat(
6820       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6821       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6822 
6823   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6824                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6825                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6826 
6827   verifyFormat(
6828       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6829       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6830       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6831       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6832       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
6833 
6834   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6835                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6836 
6837   verifyFormat(
6838       "void f() {\n"
6839       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6840       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6841       "}");
6842   verifyFormat(
6843       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6844       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6845   verifyFormat(
6846       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6847       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6848   verifyFormat(
6849       "aaaaaa(aaa,\n"
6850       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6851       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6852       "       aaaa);");
6853   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6854                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6855                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6856 
6857   // Indent consistently independent of call expression and unary operator.
6858   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6859                "    dddddddddddddddddddddddddddddd));");
6860   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6861                "    dddddddddddddddddddddddddddddd));");
6862   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
6863                "    dddddddddddddddddddddddddddddd));");
6864 
6865   // This test case breaks on an incorrect memoization, i.e. an optimization not
6866   // taking into account the StopAt value.
6867   verifyFormat(
6868       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6869       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6870       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6871       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6872 
6873   verifyFormat("{\n  {\n    {\n"
6874                "      Annotation.SpaceRequiredBefore =\n"
6875                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
6876                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
6877                "    }\n  }\n}");
6878 
6879   // Break on an outer level if there was a break on an inner level.
6880   EXPECT_EQ("f(g(h(a, // comment\n"
6881             "      b, c),\n"
6882             "    d, e),\n"
6883             "  x, y);",
6884             format("f(g(h(a, // comment\n"
6885                    "    b, c), d, e), x, y);"));
6886 
6887   // Prefer breaking similar line breaks.
6888   verifyFormat(
6889       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
6890       "                             NSTrackingMouseEnteredAndExited |\n"
6891       "                             NSTrackingActiveAlways;");
6892 }
6893 
6894 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
6895   FormatStyle NoBinPacking = getGoogleStyle();
6896   NoBinPacking.BinPackParameters = false;
6897   NoBinPacking.BinPackArguments = true;
6898   verifyFormat("void f() {\n"
6899                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
6900                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6901                "}",
6902                NoBinPacking);
6903   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
6904                "       int aaaaaaaaaaaaaaaaaaaa,\n"
6905                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6906                NoBinPacking);
6907 
6908   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6909   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6910                "                        vector<int> bbbbbbbbbbbbbbb);",
6911                NoBinPacking);
6912   // FIXME: This behavior difference is probably not wanted. However, currently
6913   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
6914   // template arguments from BreakBeforeParameter being set because of the
6915   // one-per-line formatting.
6916   verifyFormat(
6917       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6918       "                                             aaaaaaaaaa> aaaaaaaaaa);",
6919       NoBinPacking);
6920   verifyFormat(
6921       "void fffffffffff(\n"
6922       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
6923       "        aaaaaaaaaa);");
6924 }
6925 
6926 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
6927   FormatStyle NoBinPacking = getGoogleStyle();
6928   NoBinPacking.BinPackParameters = false;
6929   NoBinPacking.BinPackArguments = false;
6930   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
6931                "  aaaaaaaaaaaaaaaaaaaa,\n"
6932                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
6933                NoBinPacking);
6934   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
6935                "        aaaaaaaaaaaaa,\n"
6936                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
6937                NoBinPacking);
6938   verifyFormat(
6939       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6940       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6941       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6942       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6943       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
6944       NoBinPacking);
6945   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6946                "    .aaaaaaaaaaaaaaaaaa();",
6947                NoBinPacking);
6948   verifyFormat("void f() {\n"
6949                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6950                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
6951                "}",
6952                NoBinPacking);
6953 
6954   verifyFormat(
6955       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6956       "             aaaaaaaaaaaa,\n"
6957       "             aaaaaaaaaaaa);",
6958       NoBinPacking);
6959   verifyFormat(
6960       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
6961       "                               ddddddddddddddddddddddddddddd),\n"
6962       "             test);",
6963       NoBinPacking);
6964 
6965   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6966                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
6967                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
6968                "    aaaaaaaaaaaaaaaaaa;",
6969                NoBinPacking);
6970   verifyFormat("a(\"a\"\n"
6971                "  \"a\",\n"
6972                "  a);");
6973 
6974   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6975   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
6976                "                aaaaaaaaa,\n"
6977                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6978                NoBinPacking);
6979   verifyFormat(
6980       "void f() {\n"
6981       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6982       "      .aaaaaaa();\n"
6983       "}",
6984       NoBinPacking);
6985   verifyFormat(
6986       "template <class SomeType, class SomeOtherType>\n"
6987       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
6988       NoBinPacking);
6989 }
6990 
6991 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
6992   FormatStyle Style = getLLVMStyleWithColumns(15);
6993   Style.ExperimentalAutoDetectBinPacking = true;
6994   EXPECT_EQ("aaa(aaaa,\n"
6995             "    aaaa,\n"
6996             "    aaaa);\n"
6997             "aaa(aaaa,\n"
6998             "    aaaa,\n"
6999             "    aaaa);",
7000             format("aaa(aaaa,\n" // one-per-line
7001                    "  aaaa,\n"
7002                    "    aaaa  );\n"
7003                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7004                    Style));
7005   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7006             "    aaaa);\n"
7007             "aaa(aaaa, aaaa,\n"
7008             "    aaaa);",
7009             format("aaa(aaaa,  aaaa,\n" // bin-packed
7010                    "    aaaa  );\n"
7011                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7012                    Style));
7013 }
7014 
7015 TEST_F(FormatTest, FormatsBuilderPattern) {
7016   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7017                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7018                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7019                "    .StartsWith(\".init\", ORDER_INIT)\n"
7020                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7021                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7022                "    .Default(ORDER_TEXT);\n");
7023 
7024   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7025                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7026   verifyFormat("aaaaaaa->aaaaaaa\n"
7027                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7028                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7029                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7030   verifyFormat(
7031       "aaaaaaa->aaaaaaa\n"
7032       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7033       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7034   verifyFormat(
7035       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7036       "    aaaaaaaaaaaaaa);");
7037   verifyFormat(
7038       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7039       "    aaaaaa->aaaaaaaaaaaa()\n"
7040       "        ->aaaaaaaaaaaaaaaa(\n"
7041       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7042       "        ->aaaaaaaaaaaaaaaaa();");
7043   verifyGoogleFormat(
7044       "void f() {\n"
7045       "  someo->Add((new util::filetools::Handler(dir))\n"
7046       "                 ->OnEvent1(NewPermanentCallback(\n"
7047       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7048       "                 ->OnEvent2(NewPermanentCallback(\n"
7049       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7050       "                 ->OnEvent3(NewPermanentCallback(\n"
7051       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7052       "                 ->OnEvent5(NewPermanentCallback(\n"
7053       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7054       "                 ->OnEvent6(NewPermanentCallback(\n"
7055       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7056       "}");
7057 
7058   verifyFormat(
7059       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7060   verifyFormat("aaaaaaaaaaaaaaa()\n"
7061                "    .aaaaaaaaaaaaaaa()\n"
7062                "    .aaaaaaaaaaaaaaa()\n"
7063                "    .aaaaaaaaaaaaaaa()\n"
7064                "    .aaaaaaaaaaaaaaa();");
7065   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7066                "    .aaaaaaaaaaaaaaa()\n"
7067                "    .aaaaaaaaaaaaaaa()\n"
7068                "    .aaaaaaaaaaaaaaa();");
7069   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7070                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7071                "    .aaaaaaaaaaaaaaa();");
7072   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7073                "    ->aaaaaaaaaaaaaae(0)\n"
7074                "    ->aaaaaaaaaaaaaaa();");
7075 
7076   // Don't linewrap after very short segments.
7077   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7078                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7079                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7080   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7081                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7082                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7083   verifyFormat("aaa()\n"
7084                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7085                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7086                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7087 
7088   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7089                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7090                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7091   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7092                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7093                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7094 
7095   // Prefer not to break after empty parentheses.
7096   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7097                "    First->LastNewlineOffset);");
7098 
7099   // Prefer not to create "hanging" indents.
7100   verifyFormat(
7101       "return !soooooooooooooome_map\n"
7102       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7103       "            .second;");
7104   verifyFormat(
7105       "return aaaaaaaaaaaaaaaa\n"
7106       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7107       "    .aaaa(aaaaaaaaaaaaaa);");
7108   // No hanging indent here.
7109   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7110                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7111   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7112                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7113   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7114                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7115                getLLVMStyleWithColumns(60));
7116   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7117                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7118                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7119                getLLVMStyleWithColumns(59));
7120   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7121                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7122                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7123 
7124   // Dont break if only closing statements before member call
7125   verifyFormat("test() {\n"
7126                "  ([]() -> {\n"
7127                "    int b = 32;\n"
7128                "    return 3;\n"
7129                "  }).foo();\n"
7130                "}");
7131   verifyFormat("test() {\n"
7132                "  (\n"
7133                "      []() -> {\n"
7134                "        int b = 32;\n"
7135                "        return 3;\n"
7136                "      },\n"
7137                "      foo, bar)\n"
7138                "      .foo();\n"
7139                "}");
7140   verifyFormat("test() {\n"
7141                "  ([]() -> {\n"
7142                "    int b = 32;\n"
7143                "    return 3;\n"
7144                "  })\n"
7145                "      .foo()\n"
7146                "      .bar();\n"
7147                "}");
7148   verifyFormat("test() {\n"
7149                "  ([]() -> {\n"
7150                "    int b = 32;\n"
7151                "    return 3;\n"
7152                "  })\n"
7153                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7154                "           \"bbbb\");\n"
7155                "}",
7156                getLLVMStyleWithColumns(30));
7157 }
7158 
7159 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7160   verifyFormat(
7161       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7162       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7163   verifyFormat(
7164       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7165       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7166 
7167   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7168                "    ccccccccccccccccccccccccc) {\n}");
7169   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7170                "    ccccccccccccccccccccccccc) {\n}");
7171 
7172   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7173                "    ccccccccccccccccccccccccc) {\n}");
7174   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7175                "    ccccccccccccccccccccccccc) {\n}");
7176 
7177   verifyFormat(
7178       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7179       "    ccccccccccccccccccccccccc) {\n}");
7180   verifyFormat(
7181       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7182       "    ccccccccccccccccccccccccc) {\n}");
7183 
7184   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7185                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7186                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7187                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7188   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7189                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7190                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7191                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7192 
7193   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7194                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7195                "    aaaaaaaaaaaaaaa != aa) {\n}");
7196   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7197                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7198                "    aaaaaaaaaaaaaaa != aa) {\n}");
7199 }
7200 
7201 TEST_F(FormatTest, BreaksAfterAssignments) {
7202   verifyFormat(
7203       "unsigned Cost =\n"
7204       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7205       "                        SI->getPointerAddressSpaceee());\n");
7206   verifyFormat(
7207       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7208       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7209 
7210   verifyFormat(
7211       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7212       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7213   verifyFormat("unsigned OriginalStartColumn =\n"
7214                "    SourceMgr.getSpellingColumnNumber(\n"
7215                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7216                "    1;");
7217 }
7218 
7219 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7220   FormatStyle Style = getLLVMStyle();
7221   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7222                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7223                Style);
7224 
7225   Style.PenaltyBreakAssignment = 20;
7226   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7227                "                                 cccccccccccccccccccccccccc;",
7228                Style);
7229 }
7230 
7231 TEST_F(FormatTest, AlignsAfterAssignments) {
7232   verifyFormat(
7233       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7234       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7235   verifyFormat(
7236       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7237       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7238   verifyFormat(
7239       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7240       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7241   verifyFormat(
7242       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7243       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7244   verifyFormat(
7245       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7246       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7247       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7248 }
7249 
7250 TEST_F(FormatTest, AlignsAfterReturn) {
7251   verifyFormat(
7252       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7253       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7254   verifyFormat(
7255       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7256       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7257   verifyFormat(
7258       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7259       "       aaaaaaaaaaaaaaaaaaaaaa();");
7260   verifyFormat(
7261       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7262       "        aaaaaaaaaaaaaaaaaaaaaa());");
7263   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7264                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7265   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7266                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7267                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7268   verifyFormat("return\n"
7269                "    // true if code is one of a or b.\n"
7270                "    code == a || code == b;");
7271 }
7272 
7273 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7274   verifyFormat(
7275       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7276       "                                                aaaaaaaaa aaaaaaa) {}");
7277   verifyFormat(
7278       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7279       "                                               aaaaaaaaaaa aaaaaaaaa);");
7280   verifyFormat(
7281       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7282       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7283   FormatStyle Style = getLLVMStyle();
7284   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7285   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7286                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7287                Style);
7288   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7289                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7290                Style);
7291   verifyFormat("SomeLongVariableName->someFunction(\n"
7292                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7293                Style);
7294   verifyFormat(
7295       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7296       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7297       Style);
7298   verifyFormat(
7299       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7300       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7301       Style);
7302   verifyFormat(
7303       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7304       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7305       Style);
7306 
7307   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7308                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7309                "        b));",
7310                Style);
7311 
7312   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7313   Style.BinPackArguments = false;
7314   Style.BinPackParameters = false;
7315   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7316                "    aaaaaaaaaaa aaaaaaaa,\n"
7317                "    aaaaaaaaa aaaaaaa,\n"
7318                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7319                Style);
7320   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7321                "    aaaaaaaaaaa aaaaaaaaa,\n"
7322                "    aaaaaaaaaaa aaaaaaaaa,\n"
7323                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7324                Style);
7325   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7326                "    aaaaaaaaaaaaaaa,\n"
7327                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7328                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7329                Style);
7330   verifyFormat(
7331       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7332       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7333       Style);
7334   verifyFormat(
7335       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7336       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7337       Style);
7338   verifyFormat(
7339       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7340       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7341       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7342       "    aaaaaaaaaaaaaaaa);",
7343       Style);
7344   verifyFormat(
7345       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7346       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7347       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7348       "    aaaaaaaaaaaaaaaa);",
7349       Style);
7350 }
7351 
7352 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7353   FormatStyle Style = getLLVMStyleWithColumns(40);
7354   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7355                "          bbbbbbbbbbbbbbbbbbbbbb);",
7356                Style);
7357   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7358   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7359   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7360                "          bbbbbbbbbbbbbbbbbbbbbb);",
7361                Style);
7362   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7363   Style.AlignOperands = FormatStyle::OAS_Align;
7364   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7365                "          bbbbbbbbbbbbbbbbbbbbbb);",
7366                Style);
7367   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7368   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7369   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7370                "    bbbbbbbbbbbbbbbbbbbbbb);",
7371                Style);
7372 }
7373 
7374 TEST_F(FormatTest, BreaksConditionalExpressions) {
7375   verifyFormat(
7376       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7377       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7378       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7379   verifyFormat(
7380       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7381       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7382       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7383   verifyFormat(
7384       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7385       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7386   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7387                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7388                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7389   verifyFormat(
7390       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7391       "                                                    : aaaaaaaaaaaaa);");
7392   verifyFormat(
7393       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7394       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7395       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7396       "                   aaaaaaaaaaaaa);");
7397   verifyFormat(
7398       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7399       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7400       "                   aaaaaaaaaaaaa);");
7401   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7402                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7403                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7404                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7405                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7406   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7407                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7408                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7409                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7410                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7411                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7412                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7413   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7414                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7415                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7416                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7417                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7418   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7419                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7420                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7421   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7422                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7423                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7424                "        : aaaaaaaaaaaaaaaa;");
7425   verifyFormat(
7426       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7427       "    ? aaaaaaaaaaaaaaa\n"
7428       "    : aaaaaaaaaaaaaaa;");
7429   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7430                "          aaaaaaaaa\n"
7431                "      ? b\n"
7432                "      : c);");
7433   verifyFormat("return aaaa == bbbb\n"
7434                "           // comment\n"
7435                "           ? aaaa\n"
7436                "           : bbbb;");
7437   verifyFormat("unsigned Indent =\n"
7438                "    format(TheLine.First,\n"
7439                "           IndentForLevel[TheLine.Level] >= 0\n"
7440                "               ? IndentForLevel[TheLine.Level]\n"
7441                "               : TheLine * 2,\n"
7442                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7443                getLLVMStyleWithColumns(60));
7444   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7445                "                  ? aaaaaaaaaaaaaaa\n"
7446                "                  : bbbbbbbbbbbbbbb //\n"
7447                "                        ? ccccccccccccccc\n"
7448                "                        : ddddddddddddddd;");
7449   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7450                "                  ? aaaaaaaaaaaaaaa\n"
7451                "                  : (bbbbbbbbbbbbbbb //\n"
7452                "                         ? ccccccccccccccc\n"
7453                "                         : ddddddddddddddd);");
7454   verifyFormat(
7455       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7456       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7457       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7458       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7459       "                                      : aaaaaaaaaa;");
7460   verifyFormat(
7461       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7462       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7463       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7464 
7465   FormatStyle NoBinPacking = getLLVMStyle();
7466   NoBinPacking.BinPackArguments = false;
7467   verifyFormat(
7468       "void f() {\n"
7469       "  g(aaa,\n"
7470       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7471       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7472       "        ? aaaaaaaaaaaaaaa\n"
7473       "        : aaaaaaaaaaaaaaa);\n"
7474       "}",
7475       NoBinPacking);
7476   verifyFormat(
7477       "void f() {\n"
7478       "  g(aaa,\n"
7479       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7480       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7481       "        ?: aaaaaaaaaaaaaaa);\n"
7482       "}",
7483       NoBinPacking);
7484 
7485   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7486                "             // comment.\n"
7487                "             ccccccccccccccccccccccccccccccccccccccc\n"
7488                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7489                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7490 
7491   // Assignments in conditional expressions. Apparently not uncommon :-(.
7492   verifyFormat("return a != b\n"
7493                "           // comment\n"
7494                "           ? a = b\n"
7495                "           : a = b;");
7496   verifyFormat("return a != b\n"
7497                "           // comment\n"
7498                "           ? a = a != b\n"
7499                "                     // comment\n"
7500                "                     ? a = b\n"
7501                "                     : a\n"
7502                "           : a;\n");
7503   verifyFormat("return a != b\n"
7504                "           // comment\n"
7505                "           ? a\n"
7506                "           : a = a != b\n"
7507                "                     // comment\n"
7508                "                     ? a = b\n"
7509                "                     : a;");
7510 
7511   // Chained conditionals
7512   FormatStyle Style = getLLVMStyle();
7513   Style.ColumnLimit = 70;
7514   Style.AlignOperands = FormatStyle::OAS_Align;
7515   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7516                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7517                "                        : 3333333333333333;",
7518                Style);
7519   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7520                "       : bbbbbbbbbb     ? 2222222222222222\n"
7521                "                        : 3333333333333333;",
7522                Style);
7523   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7524                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7525                "                          : 3333333333333333;",
7526                Style);
7527   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7528                "       : bbbbbbbbbbbbbb ? 222222\n"
7529                "                        : 333333;",
7530                Style);
7531   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7532                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7533                "       : cccccccccccccc ? 3333333333333333\n"
7534                "                        : 4444444444444444;",
7535                Style);
7536   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7537                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7538                "                        : 3333333333333333;",
7539                Style);
7540   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7541                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7542                "                        : (aaa ? bbb : ccc);",
7543                Style);
7544   verifyFormat(
7545       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7546       "                                             : cccccccccccccccccc)\n"
7547       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7548       "                        : 3333333333333333;",
7549       Style);
7550   verifyFormat(
7551       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7552       "                                             : cccccccccccccccccc)\n"
7553       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7554       "                        : 3333333333333333;",
7555       Style);
7556   verifyFormat(
7557       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7558       "                                             : dddddddddddddddddd)\n"
7559       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7560       "                        : 3333333333333333;",
7561       Style);
7562   verifyFormat(
7563       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7564       "                                             : dddddddddddddddddd)\n"
7565       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7566       "                        : 3333333333333333;",
7567       Style);
7568   verifyFormat(
7569       "return aaaaaaaaa        ? 1111111111111111\n"
7570       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7571       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7572       "                                             : dddddddddddddddddd)\n",
7573       Style);
7574   verifyFormat(
7575       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7576       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7577       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7578       "                                             : cccccccccccccccccc);",
7579       Style);
7580   verifyFormat(
7581       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7582       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7583       "                                             : eeeeeeeeeeeeeeeeee)\n"
7584       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7585       "                        : 3333333333333333;",
7586       Style);
7587   verifyFormat(
7588       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
7589       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7590       "                                             : eeeeeeeeeeeeeeeeee)\n"
7591       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7592       "                        : 3333333333333333;",
7593       Style);
7594   verifyFormat(
7595       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7596       "                           : cccccccccccc    ? dddddddddddddddddd\n"
7597       "                                             : eeeeeeeeeeeeeeeeee)\n"
7598       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7599       "                        : 3333333333333333;",
7600       Style);
7601   verifyFormat(
7602       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7603       "                                             : cccccccccccccccccc\n"
7604       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7605       "                        : 3333333333333333;",
7606       Style);
7607   verifyFormat(
7608       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7609       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
7610       "                                             : eeeeeeeeeeeeeeeeee\n"
7611       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7612       "                        : 3333333333333333;",
7613       Style);
7614   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
7615                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
7616                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
7617                "                                   : eeeeeeeeeeeeeeeeee)\n"
7618                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7619                "                             : 3333333333333333;",
7620                Style);
7621   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7622                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7623                "             : cccccccccccccccc ? dddddddddddddddddd\n"
7624                "                                : eeeeeeeeeeeeeeeeee\n"
7625                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7626                "                                 : 3333333333333333;",
7627                Style);
7628 
7629   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7630   Style.BreakBeforeTernaryOperators = false;
7631   // FIXME: Aligning the question marks is weird given DontAlign.
7632   // Consider disabling this alignment in this case. Also check whether this
7633   // will render the adjustment from https://reviews.llvm.org/D82199
7634   // unnecessary.
7635   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
7636                "    bbbb                ? cccccccccccccccccc :\n"
7637                "                          ddddd;\n",
7638                Style);
7639 
7640   EXPECT_EQ(
7641       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7642       "    /*\n"
7643       "     */\n"
7644       "    function() {\n"
7645       "      try {\n"
7646       "        return JJJJJJJJJJJJJJ(\n"
7647       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7648       "      }\n"
7649       "    } :\n"
7650       "    function() {};",
7651       format(
7652           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7653           "     /*\n"
7654           "      */\n"
7655           "     function() {\n"
7656           "      try {\n"
7657           "        return JJJJJJJJJJJJJJ(\n"
7658           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7659           "      }\n"
7660           "    } :\n"
7661           "    function() {};",
7662           getGoogleStyle(FormatStyle::LK_JavaScript)));
7663 }
7664 
7665 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
7666   FormatStyle Style = getLLVMStyle();
7667   Style.BreakBeforeTernaryOperators = false;
7668   Style.ColumnLimit = 70;
7669   verifyFormat(
7670       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7671       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7672       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7673       Style);
7674   verifyFormat(
7675       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7676       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7677       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7678       Style);
7679   verifyFormat(
7680       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7681       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7682       Style);
7683   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
7684                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7685                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7686                Style);
7687   verifyFormat(
7688       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
7689       "                                                      aaaaaaaaaaaaa);",
7690       Style);
7691   verifyFormat(
7692       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7693       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7694       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7695       "                   aaaaaaaaaaaaa);",
7696       Style);
7697   verifyFormat(
7698       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7699       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7700       "                   aaaaaaaaaaaaa);",
7701       Style);
7702   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7703                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7704                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7705                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7706                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7707                Style);
7708   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7709                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7710                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7711                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7712                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7713                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7714                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7715                Style);
7716   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7717                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
7718                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7719                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7720                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7721                Style);
7722   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7723                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7724                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7725                Style);
7726   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7727                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7728                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7729                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7730                Style);
7731   verifyFormat(
7732       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7733       "    aaaaaaaaaaaaaaa :\n"
7734       "    aaaaaaaaaaaaaaa;",
7735       Style);
7736   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7737                "          aaaaaaaaa ?\n"
7738                "      b :\n"
7739                "      c);",
7740                Style);
7741   verifyFormat("unsigned Indent =\n"
7742                "    format(TheLine.First,\n"
7743                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
7744                "               IndentForLevel[TheLine.Level] :\n"
7745                "               TheLine * 2,\n"
7746                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7747                Style);
7748   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7749                "                  aaaaaaaaaaaaaaa :\n"
7750                "                  bbbbbbbbbbbbbbb ? //\n"
7751                "                      ccccccccccccccc :\n"
7752                "                      ddddddddddddddd;",
7753                Style);
7754   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7755                "                  aaaaaaaaaaaaaaa :\n"
7756                "                  (bbbbbbbbbbbbbbb ? //\n"
7757                "                       ccccccccccccccc :\n"
7758                "                       ddddddddddddddd);",
7759                Style);
7760   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7761                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
7762                "            ccccccccccccccccccccccccccc;",
7763                Style);
7764   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7765                "           aaaaa :\n"
7766                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
7767                Style);
7768 
7769   // Chained conditionals
7770   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7771                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7772                "                          3333333333333333;",
7773                Style);
7774   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7775                "       bbbbbbbbbb       ? 2222222222222222 :\n"
7776                "                          3333333333333333;",
7777                Style);
7778   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
7779                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7780                "                          3333333333333333;",
7781                Style);
7782   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7783                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
7784                "                          333333;",
7785                Style);
7786   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7787                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7788                "       cccccccccccccccc ? 3333333333333333 :\n"
7789                "                          4444444444444444;",
7790                Style);
7791   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
7792                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7793                "                          3333333333333333;",
7794                Style);
7795   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7796                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7797                "                          (aaa ? bbb : ccc);",
7798                Style);
7799   verifyFormat(
7800       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7801       "                                               cccccccccccccccccc) :\n"
7802       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7803       "                          3333333333333333;",
7804       Style);
7805   verifyFormat(
7806       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7807       "                                               cccccccccccccccccc) :\n"
7808       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7809       "                          3333333333333333;",
7810       Style);
7811   verifyFormat(
7812       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7813       "                                               dddddddddddddddddd) :\n"
7814       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7815       "                          3333333333333333;",
7816       Style);
7817   verifyFormat(
7818       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7819       "                                               dddddddddddddddddd) :\n"
7820       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7821       "                          3333333333333333;",
7822       Style);
7823   verifyFormat(
7824       "return aaaaaaaaa        ? 1111111111111111 :\n"
7825       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7826       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7827       "                                               dddddddddddddddddd)\n",
7828       Style);
7829   verifyFormat(
7830       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7831       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7832       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7833       "                                               cccccccccccccccccc);",
7834       Style);
7835   verifyFormat(
7836       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7837       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7838       "                                               eeeeeeeeeeeeeeeeee) :\n"
7839       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7840       "                          3333333333333333;",
7841       Style);
7842   verifyFormat(
7843       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7844       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
7845       "                                               eeeeeeeeeeeeeeeeee) :\n"
7846       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7847       "                          3333333333333333;",
7848       Style);
7849   verifyFormat(
7850       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
7851       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7852       "                                               eeeeeeeeeeeeeeeeee) :\n"
7853       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7854       "                          3333333333333333;",
7855       Style);
7856   verifyFormat(
7857       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7858       "                                               cccccccccccccccccc :\n"
7859       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7860       "                          3333333333333333;",
7861       Style);
7862   verifyFormat(
7863       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7864       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
7865       "                                               eeeeeeeeeeeeeeeeee :\n"
7866       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7867       "                          3333333333333333;",
7868       Style);
7869   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7870                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7871                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
7872                "                                 eeeeeeeeeeeeeeeeee) :\n"
7873                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7874                "                               3333333333333333;",
7875                Style);
7876   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7877                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7878                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
7879                "                                  eeeeeeeeeeeeeeeeee :\n"
7880                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7881                "                               3333333333333333;",
7882                Style);
7883 }
7884 
7885 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
7886   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
7887                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
7888   verifyFormat("bool a = true, b = false;");
7889 
7890   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7891                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
7892                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
7893                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
7894   verifyFormat(
7895       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7896       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
7897       "     d = e && f;");
7898   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
7899                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
7900   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7901                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
7902   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
7903                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
7904 
7905   FormatStyle Style = getGoogleStyle();
7906   Style.PointerAlignment = FormatStyle::PAS_Left;
7907   Style.DerivePointerAlignment = false;
7908   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7909                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
7910                "    *b = bbbbbbbbbbbbbbbbbbb;",
7911                Style);
7912   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7913                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
7914                Style);
7915   verifyFormat("vector<int*> a, b;", Style);
7916   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
7917 }
7918 
7919 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
7920   verifyFormat("arr[foo ? bar : baz];");
7921   verifyFormat("f()[foo ? bar : baz];");
7922   verifyFormat("(a + b)[foo ? bar : baz];");
7923   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
7924 }
7925 
7926 TEST_F(FormatTest, AlignsStringLiterals) {
7927   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
7928                "                                      \"short literal\");");
7929   verifyFormat(
7930       "looooooooooooooooooooooooongFunction(\n"
7931       "    \"short literal\"\n"
7932       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
7933   verifyFormat("someFunction(\"Always break between multi-line\"\n"
7934                "             \" string literals\",\n"
7935                "             and, other, parameters);");
7936   EXPECT_EQ("fun + \"1243\" /* comment */\n"
7937             "      \"5678\";",
7938             format("fun + \"1243\" /* comment */\n"
7939                    "    \"5678\";",
7940                    getLLVMStyleWithColumns(28)));
7941   EXPECT_EQ(
7942       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7943       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
7944       "         \"aaaaaaaaaaaaaaaa\";",
7945       format("aaaaaa ="
7946              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7947              "aaaaaaaaaaaaaaaaaaaaa\" "
7948              "\"aaaaaaaaaaaaaaaa\";"));
7949   verifyFormat("a = a + \"a\"\n"
7950                "        \"a\"\n"
7951                "        \"a\";");
7952   verifyFormat("f(\"a\", \"b\"\n"
7953                "       \"c\");");
7954 
7955   verifyFormat(
7956       "#define LL_FORMAT \"ll\"\n"
7957       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
7958       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
7959 
7960   verifyFormat("#define A(X)          \\\n"
7961                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
7962                "  \"ccccc\"",
7963                getLLVMStyleWithColumns(23));
7964   verifyFormat("#define A \"def\"\n"
7965                "f(\"abc\" A \"ghi\"\n"
7966                "  \"jkl\");");
7967 
7968   verifyFormat("f(L\"a\"\n"
7969                "  L\"b\");");
7970   verifyFormat("#define A(X)            \\\n"
7971                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
7972                "  L\"ccccc\"",
7973                getLLVMStyleWithColumns(25));
7974 
7975   verifyFormat("f(@\"a\"\n"
7976                "  @\"b\");");
7977   verifyFormat("NSString s = @\"a\"\n"
7978                "             @\"b\"\n"
7979                "             @\"c\";");
7980   verifyFormat("NSString s = @\"a\"\n"
7981                "              \"b\"\n"
7982                "              \"c\";");
7983 }
7984 
7985 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
7986   FormatStyle Style = getLLVMStyle();
7987   // No declarations or definitions should be moved to own line.
7988   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
7989   verifyFormat("class A {\n"
7990                "  int f() { return 1; }\n"
7991                "  int g();\n"
7992                "};\n"
7993                "int f() { return 1; }\n"
7994                "int g();\n",
7995                Style);
7996 
7997   // All declarations and definitions should have the return type moved to its
7998   // own line.
7999   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8000   Style.TypenameMacros = {"LIST"};
8001   verifyFormat("SomeType\n"
8002                "funcdecl(LIST(uint64_t));",
8003                Style);
8004   verifyFormat("class E {\n"
8005                "  int\n"
8006                "  f() {\n"
8007                "    return 1;\n"
8008                "  }\n"
8009                "  int\n"
8010                "  g();\n"
8011                "};\n"
8012                "int\n"
8013                "f() {\n"
8014                "  return 1;\n"
8015                "}\n"
8016                "int\n"
8017                "g();\n",
8018                Style);
8019 
8020   // Top-level definitions, and no kinds of declarations should have the
8021   // return type moved to its own line.
8022   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8023   verifyFormat("class B {\n"
8024                "  int f() { return 1; }\n"
8025                "  int g();\n"
8026                "};\n"
8027                "int\n"
8028                "f() {\n"
8029                "  return 1;\n"
8030                "}\n"
8031                "int g();\n",
8032                Style);
8033 
8034   // Top-level definitions and declarations should have the return type moved
8035   // to its own line.
8036   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8037   verifyFormat("class C {\n"
8038                "  int f() { return 1; }\n"
8039                "  int g();\n"
8040                "};\n"
8041                "int\n"
8042                "f() {\n"
8043                "  return 1;\n"
8044                "}\n"
8045                "int\n"
8046                "g();\n",
8047                Style);
8048 
8049   // All definitions should have the return type moved to its own line, but no
8050   // kinds of declarations.
8051   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8052   verifyFormat("class D {\n"
8053                "  int\n"
8054                "  f() {\n"
8055                "    return 1;\n"
8056                "  }\n"
8057                "  int g();\n"
8058                "};\n"
8059                "int\n"
8060                "f() {\n"
8061                "  return 1;\n"
8062                "}\n"
8063                "int g();\n",
8064                Style);
8065   verifyFormat("const char *\n"
8066                "f(void) {\n" // Break here.
8067                "  return \"\";\n"
8068                "}\n"
8069                "const char *bar(void);\n", // No break here.
8070                Style);
8071   verifyFormat("template <class T>\n"
8072                "T *\n"
8073                "f(T &c) {\n" // Break here.
8074                "  return NULL;\n"
8075                "}\n"
8076                "template <class T> T *f(T &c);\n", // No break here.
8077                Style);
8078   verifyFormat("class C {\n"
8079                "  int\n"
8080                "  operator+() {\n"
8081                "    return 1;\n"
8082                "  }\n"
8083                "  int\n"
8084                "  operator()() {\n"
8085                "    return 1;\n"
8086                "  }\n"
8087                "};\n",
8088                Style);
8089   verifyFormat("void\n"
8090                "A::operator()() {}\n"
8091                "void\n"
8092                "A::operator>>() {}\n"
8093                "void\n"
8094                "A::operator+() {}\n"
8095                "void\n"
8096                "A::operator*() {}\n"
8097                "void\n"
8098                "A::operator->() {}\n"
8099                "void\n"
8100                "A::operator void *() {}\n"
8101                "void\n"
8102                "A::operator void &() {}\n"
8103                "void\n"
8104                "A::operator void &&() {}\n"
8105                "void\n"
8106                "A::operator char *() {}\n"
8107                "void\n"
8108                "A::operator[]() {}\n"
8109                "void\n"
8110                "A::operator!() {}\n"
8111                "void\n"
8112                "A::operator**() {}\n"
8113                "void\n"
8114                "A::operator<Foo> *() {}\n"
8115                "void\n"
8116                "A::operator<Foo> **() {}\n"
8117                "void\n"
8118                "A::operator<Foo> &() {}\n"
8119                "void\n"
8120                "A::operator void **() {}\n",
8121                Style);
8122   verifyFormat("constexpr auto\n"
8123                "operator()() const -> reference {}\n"
8124                "constexpr auto\n"
8125                "operator>>() const -> reference {}\n"
8126                "constexpr auto\n"
8127                "operator+() const -> reference {}\n"
8128                "constexpr auto\n"
8129                "operator*() const -> reference {}\n"
8130                "constexpr auto\n"
8131                "operator->() const -> reference {}\n"
8132                "constexpr auto\n"
8133                "operator++() const -> reference {}\n"
8134                "constexpr auto\n"
8135                "operator void *() const -> reference {}\n"
8136                "constexpr auto\n"
8137                "operator void **() const -> reference {}\n"
8138                "constexpr auto\n"
8139                "operator void *() const -> reference {}\n"
8140                "constexpr auto\n"
8141                "operator void &() const -> reference {}\n"
8142                "constexpr auto\n"
8143                "operator void &&() const -> reference {}\n"
8144                "constexpr auto\n"
8145                "operator char *() const -> reference {}\n"
8146                "constexpr auto\n"
8147                "operator!() const -> reference {}\n"
8148                "constexpr auto\n"
8149                "operator[]() const -> reference {}\n",
8150                Style);
8151   verifyFormat("void *operator new(std::size_t s);", // No break here.
8152                Style);
8153   verifyFormat("void *\n"
8154                "operator new(std::size_t s) {}",
8155                Style);
8156   verifyFormat("void *\n"
8157                "operator delete[](void *ptr) {}",
8158                Style);
8159   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8160   verifyFormat("const char *\n"
8161                "f(void)\n" // Break here.
8162                "{\n"
8163                "  return \"\";\n"
8164                "}\n"
8165                "const char *bar(void);\n", // No break here.
8166                Style);
8167   verifyFormat("template <class T>\n"
8168                "T *\n"     // Problem here: no line break
8169                "f(T &c)\n" // Break here.
8170                "{\n"
8171                "  return NULL;\n"
8172                "}\n"
8173                "template <class T> T *f(T &c);\n", // No break here.
8174                Style);
8175   verifyFormat("int\n"
8176                "foo(A<bool> a)\n"
8177                "{\n"
8178                "  return a;\n"
8179                "}\n",
8180                Style);
8181   verifyFormat("int\n"
8182                "foo(A<8> a)\n"
8183                "{\n"
8184                "  return a;\n"
8185                "}\n",
8186                Style);
8187   verifyFormat("int\n"
8188                "foo(A<B<bool>, 8> a)\n"
8189                "{\n"
8190                "  return a;\n"
8191                "}\n",
8192                Style);
8193   verifyFormat("int\n"
8194                "foo(A<B<8>, bool> a)\n"
8195                "{\n"
8196                "  return a;\n"
8197                "}\n",
8198                Style);
8199   verifyFormat("int\n"
8200                "foo(A<B<bool>, bool> a)\n"
8201                "{\n"
8202                "  return a;\n"
8203                "}\n",
8204                Style);
8205   verifyFormat("int\n"
8206                "foo(A<B<8>, 8> a)\n"
8207                "{\n"
8208                "  return a;\n"
8209                "}\n",
8210                Style);
8211 
8212   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8213   Style.BraceWrapping.AfterFunction = true;
8214   verifyFormat("int f(i);\n" // No break here.
8215                "int\n"       // Break here.
8216                "f(i)\n"
8217                "{\n"
8218                "  return i + 1;\n"
8219                "}",
8220                Style);
8221   verifyFormat("int f(a, b, c);\n" // No break here.
8222                "int\n"             // Break here.
8223                "f(a, b, c)\n"      // Break here.
8224                "short a, b;\n"
8225                "float c;\n"
8226                "{\n"
8227                "  return a + b < c;\n"
8228                "}",
8229                Style);
8230 
8231   Style = getGNUStyle();
8232 
8233   // Test for comments at the end of function declarations.
8234   verifyFormat("void\n"
8235                "foo (int a, /*abc*/ int b) // def\n"
8236                "{\n"
8237                "}\n",
8238                Style);
8239 
8240   verifyFormat("void\n"
8241                "foo (int a, /* abc */ int b) /* def */\n"
8242                "{\n"
8243                "}\n",
8244                Style);
8245 
8246   // Definitions that should not break after return type
8247   verifyFormat("void foo (int a, int b); // def\n", Style);
8248   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8249   verifyFormat("void foo (int a, int b);\n", Style);
8250 }
8251 
8252 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8253   FormatStyle NoBreak = getLLVMStyle();
8254   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8255   FormatStyle Break = getLLVMStyle();
8256   Break.AlwaysBreakBeforeMultilineStrings = true;
8257   verifyFormat("aaaa = \"bbbb\"\n"
8258                "       \"cccc\";",
8259                NoBreak);
8260   verifyFormat("aaaa =\n"
8261                "    \"bbbb\"\n"
8262                "    \"cccc\";",
8263                Break);
8264   verifyFormat("aaaa(\"bbbb\"\n"
8265                "     \"cccc\");",
8266                NoBreak);
8267   verifyFormat("aaaa(\n"
8268                "    \"bbbb\"\n"
8269                "    \"cccc\");",
8270                Break);
8271   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8272                "          \"cccc\");",
8273                NoBreak);
8274   verifyFormat("aaaa(qqq,\n"
8275                "     \"bbbb\"\n"
8276                "     \"cccc\");",
8277                Break);
8278   verifyFormat("aaaa(qqq,\n"
8279                "     L\"bbbb\"\n"
8280                "     L\"cccc\");",
8281                Break);
8282   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8283                "                      \"bbbb\"));",
8284                Break);
8285   verifyFormat("string s = someFunction(\n"
8286                "    \"abc\"\n"
8287                "    \"abc\");",
8288                Break);
8289 
8290   // As we break before unary operators, breaking right after them is bad.
8291   verifyFormat("string foo = abc ? \"x\"\n"
8292                "                   \"blah blah blah blah blah blah\"\n"
8293                "                 : \"y\";",
8294                Break);
8295 
8296   // Don't break if there is no column gain.
8297   verifyFormat("f(\"aaaa\"\n"
8298                "  \"bbbb\");",
8299                Break);
8300 
8301   // Treat literals with escaped newlines like multi-line string literals.
8302   EXPECT_EQ("x = \"a\\\n"
8303             "b\\\n"
8304             "c\";",
8305             format("x = \"a\\\n"
8306                    "b\\\n"
8307                    "c\";",
8308                    NoBreak));
8309   EXPECT_EQ("xxxx =\n"
8310             "    \"a\\\n"
8311             "b\\\n"
8312             "c\";",
8313             format("xxxx = \"a\\\n"
8314                    "b\\\n"
8315                    "c\";",
8316                    Break));
8317 
8318   EXPECT_EQ("NSString *const kString =\n"
8319             "    @\"aaaa\"\n"
8320             "    @\"bbbb\";",
8321             format("NSString *const kString = @\"aaaa\"\n"
8322                    "@\"bbbb\";",
8323                    Break));
8324 
8325   Break.ColumnLimit = 0;
8326   verifyFormat("const char *hello = \"hello llvm\";", Break);
8327 }
8328 
8329 TEST_F(FormatTest, AlignsPipes) {
8330   verifyFormat(
8331       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8332       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8333       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8334   verifyFormat(
8335       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8336       "                     << aaaaaaaaaaaaaaaaaaaa;");
8337   verifyFormat(
8338       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8339       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8340   verifyFormat(
8341       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8342       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8343   verifyFormat(
8344       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8345       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8346       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8347   verifyFormat(
8348       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8349       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8350       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8351   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8352                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8353                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8354                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8355   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8356                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8357   verifyFormat(
8358       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8359       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8360   verifyFormat(
8361       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8362       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8363 
8364   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8365                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8366   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8367                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8368                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8369                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8370   verifyFormat("LOG_IF(aaa == //\n"
8371                "       bbb)\n"
8372                "    << a << b;");
8373 
8374   // But sometimes, breaking before the first "<<" is desirable.
8375   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8376                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8377   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8378                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8379                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8380   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8381                "    << BEF << IsTemplate << Description << E->getType();");
8382   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8383                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8384                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8385   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8386                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8387                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8388                "    << aaa;");
8389 
8390   verifyFormat(
8391       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8392       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8393 
8394   // Incomplete string literal.
8395   EXPECT_EQ("llvm::errs() << \"\n"
8396             "             << a;",
8397             format("llvm::errs() << \"\n<<a;"));
8398 
8399   verifyFormat("void f() {\n"
8400                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8401                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8402                "}");
8403 
8404   // Handle 'endl'.
8405   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8406                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8407   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8408 
8409   // Handle '\n'.
8410   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8411                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8412   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8413                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8414   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8415                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8416   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8417 }
8418 
8419 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8420   verifyFormat("return out << \"somepacket = {\\n\"\n"
8421                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8422                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8423                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8424                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8425                "           << \"}\";");
8426 
8427   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8428                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8429                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8430   verifyFormat(
8431       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8432       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8433       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8434       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8435       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8436   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8437                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8438   verifyFormat(
8439       "void f() {\n"
8440       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8441       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8442       "}");
8443 
8444   // Breaking before the first "<<" is generally not desirable.
8445   verifyFormat(
8446       "llvm::errs()\n"
8447       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8448       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8449       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8450       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8451       getLLVMStyleWithColumns(70));
8452   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8453                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8454                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8455                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8456                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8457                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8458                getLLVMStyleWithColumns(70));
8459 
8460   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8461                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8462                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8463   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8464                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8465                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8466   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8467                "           (aaaa + aaaa);",
8468                getLLVMStyleWithColumns(40));
8469   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8470                "                  (aaaaaaa + aaaaa));",
8471                getLLVMStyleWithColumns(40));
8472   verifyFormat(
8473       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8474       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8475       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8476 }
8477 
8478 TEST_F(FormatTest, UnderstandsEquals) {
8479   verifyFormat(
8480       "aaaaaaaaaaaaaaaaa =\n"
8481       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8482   verifyFormat(
8483       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8484       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8485   verifyFormat(
8486       "if (a) {\n"
8487       "  f();\n"
8488       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8489       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8490       "}");
8491 
8492   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8493                "        100000000 + 10000000) {\n}");
8494 }
8495 
8496 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8497   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8498                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8499 
8500   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8501                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8502 
8503   verifyFormat(
8504       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8505       "                                                          Parameter2);");
8506 
8507   verifyFormat(
8508       "ShortObject->shortFunction(\n"
8509       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8510       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8511 
8512   verifyFormat("loooooooooooooongFunction(\n"
8513                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8514 
8515   verifyFormat(
8516       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8517       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8518 
8519   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8520                "    .WillRepeatedly(Return(SomeValue));");
8521   verifyFormat("void f() {\n"
8522                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8523                "      .Times(2)\n"
8524                "      .WillRepeatedly(Return(SomeValue));\n"
8525                "}");
8526   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
8527                "    ccccccccccccccccccccccc);");
8528   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8529                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8530                "          .aaaaa(aaaaa),\n"
8531                "      aaaaaaaaaaaaaaaaaaaaa);");
8532   verifyFormat("void f() {\n"
8533                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8534                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
8535                "}");
8536   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8537                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8538                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8539                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8540                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8541   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8542                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8543                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8544                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
8545                "}");
8546 
8547   // Here, it is not necessary to wrap at "." or "->".
8548   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
8549                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8550   verifyFormat(
8551       "aaaaaaaaaaa->aaaaaaaaa(\n"
8552       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8553       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
8554 
8555   verifyFormat(
8556       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8557       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
8558   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
8559                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8560   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
8561                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8562 
8563   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8564                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8565                "    .a();");
8566 
8567   FormatStyle NoBinPacking = getLLVMStyle();
8568   NoBinPacking.BinPackParameters = false;
8569   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8570                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8571                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
8572                "                         aaaaaaaaaaaaaaaaaaa,\n"
8573                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8574                NoBinPacking);
8575 
8576   // If there is a subsequent call, change to hanging indentation.
8577   verifyFormat(
8578       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8579       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
8580       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8581   verifyFormat(
8582       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8583       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
8584   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8585                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8586                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8587   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8588                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8589                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8590 }
8591 
8592 TEST_F(FormatTest, WrapsTemplateDeclarations) {
8593   verifyFormat("template <typename T>\n"
8594                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8595   verifyFormat("template <typename T>\n"
8596                "// T should be one of {A, B}.\n"
8597                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8598   verifyFormat(
8599       "template <typename T>\n"
8600       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
8601   verifyFormat("template <typename T>\n"
8602                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
8603                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
8604   verifyFormat(
8605       "template <typename T>\n"
8606       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
8607       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
8608   verifyFormat(
8609       "template <typename T>\n"
8610       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
8611       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
8612       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8613   verifyFormat("template <typename T>\n"
8614                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8615                "    int aaaaaaaaaaaaaaaaaaaaaa);");
8616   verifyFormat(
8617       "template <typename T1, typename T2 = char, typename T3 = char,\n"
8618       "          typename T4 = char>\n"
8619       "void f();");
8620   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
8621                "          template <typename> class cccccccccccccccccccccc,\n"
8622                "          typename ddddddddddddd>\n"
8623                "class C {};");
8624   verifyFormat(
8625       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
8626       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8627 
8628   verifyFormat("void f() {\n"
8629                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
8630                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
8631                "}");
8632 
8633   verifyFormat("template <typename T> class C {};");
8634   verifyFormat("template <typename T> void f();");
8635   verifyFormat("template <typename T> void f() {}");
8636   verifyFormat(
8637       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8638       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8639       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
8640       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8641       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8642       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
8643       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
8644       getLLVMStyleWithColumns(72));
8645   EXPECT_EQ("static_cast<A< //\n"
8646             "    B> *>(\n"
8647             "\n"
8648             ");",
8649             format("static_cast<A<//\n"
8650                    "    B>*>(\n"
8651                    "\n"
8652                    "    );"));
8653   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8654                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
8655 
8656   FormatStyle AlwaysBreak = getLLVMStyle();
8657   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8658   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
8659   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
8660   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
8661   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8662                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8663                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
8664   verifyFormat("template <template <typename> class Fooooooo,\n"
8665                "          template <typename> class Baaaaaaar>\n"
8666                "struct C {};",
8667                AlwaysBreak);
8668   verifyFormat("template <typename T> // T can be A, B or C.\n"
8669                "struct C {};",
8670                AlwaysBreak);
8671   verifyFormat("template <enum E> class A {\n"
8672                "public:\n"
8673                "  E *f();\n"
8674                "};");
8675 
8676   FormatStyle NeverBreak = getLLVMStyle();
8677   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
8678   verifyFormat("template <typename T> class C {};", NeverBreak);
8679   verifyFormat("template <typename T> void f();", NeverBreak);
8680   verifyFormat("template <typename T> void f() {}", NeverBreak);
8681   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8682                "bbbbbbbbbbbbbbbbbbbb) {}",
8683                NeverBreak);
8684   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8685                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8686                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
8687                NeverBreak);
8688   verifyFormat("template <template <typename> class Fooooooo,\n"
8689                "          template <typename> class Baaaaaaar>\n"
8690                "struct C {};",
8691                NeverBreak);
8692   verifyFormat("template <typename T> // T can be A, B or C.\n"
8693                "struct C {};",
8694                NeverBreak);
8695   verifyFormat("template <enum E> class A {\n"
8696                "public:\n"
8697                "  E *f();\n"
8698                "};",
8699                NeverBreak);
8700   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
8701   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8702                "bbbbbbbbbbbbbbbbbbbb) {}",
8703                NeverBreak);
8704 }
8705 
8706 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
8707   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
8708   Style.ColumnLimit = 60;
8709   EXPECT_EQ("// Baseline - no comments.\n"
8710             "template <\n"
8711             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8712             "void f() {}",
8713             format("// Baseline - no comments.\n"
8714                    "template <\n"
8715                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8716                    "void f() {}",
8717                    Style));
8718 
8719   EXPECT_EQ("template <\n"
8720             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8721             "void f() {}",
8722             format("template <\n"
8723                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8724                    "void f() {}",
8725                    Style));
8726 
8727   EXPECT_EQ(
8728       "template <\n"
8729       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
8730       "void f() {}",
8731       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
8732              "void f() {}",
8733              Style));
8734 
8735   EXPECT_EQ(
8736       "template <\n"
8737       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8738       "                                               // multiline\n"
8739       "void f() {}",
8740       format("template <\n"
8741              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8742              "                                              // multiline\n"
8743              "void f() {}",
8744              Style));
8745 
8746   EXPECT_EQ(
8747       "template <typename aaaaaaaaaa<\n"
8748       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
8749       "void f() {}",
8750       format(
8751           "template <\n"
8752           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
8753           "void f() {}",
8754           Style));
8755 }
8756 
8757 TEST_F(FormatTest, WrapsTemplateParameters) {
8758   FormatStyle Style = getLLVMStyle();
8759   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8760   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8761   verifyFormat(
8762       "template <typename... a> struct q {};\n"
8763       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8764       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8765       "    y;",
8766       Style);
8767   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8768   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8769   verifyFormat(
8770       "template <typename... a> struct r {};\n"
8771       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8772       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8773       "    y;",
8774       Style);
8775   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8776   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8777   verifyFormat("template <typename... a> struct s {};\n"
8778                "extern s<\n"
8779                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8780                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8781                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8782                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8783                "    y;",
8784                Style);
8785   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8786   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8787   verifyFormat("template <typename... a> struct t {};\n"
8788                "extern t<\n"
8789                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8790                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8791                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8792                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8793                "    y;",
8794                Style);
8795 }
8796 
8797 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
8798   verifyFormat(
8799       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8800       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8801   verifyFormat(
8802       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8803       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8804       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8805 
8806   // FIXME: Should we have the extra indent after the second break?
8807   verifyFormat(
8808       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8809       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8810       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8811 
8812   verifyFormat(
8813       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
8814       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
8815 
8816   // Breaking at nested name specifiers is generally not desirable.
8817   verifyFormat(
8818       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8819       "    aaaaaaaaaaaaaaaaaaaaaaa);");
8820 
8821   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
8822                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8823                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8824                "                   aaaaaaaaaaaaaaaaaaaaa);",
8825                getLLVMStyleWithColumns(74));
8826 
8827   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8828                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8829                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8830 }
8831 
8832 TEST_F(FormatTest, UnderstandsTemplateParameters) {
8833   verifyFormat("A<int> a;");
8834   verifyFormat("A<A<A<int>>> a;");
8835   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
8836   verifyFormat("bool x = a < 1 || 2 > a;");
8837   verifyFormat("bool x = 5 < f<int>();");
8838   verifyFormat("bool x = f<int>() > 5;");
8839   verifyFormat("bool x = 5 < a<int>::x;");
8840   verifyFormat("bool x = a < 4 ? a > 2 : false;");
8841   verifyFormat("bool x = f() ? a < 2 : a > 2;");
8842 
8843   verifyGoogleFormat("A<A<int>> a;");
8844   verifyGoogleFormat("A<A<A<int>>> a;");
8845   verifyGoogleFormat("A<A<A<A<int>>>> a;");
8846   verifyGoogleFormat("A<A<int> > a;");
8847   verifyGoogleFormat("A<A<A<int> > > a;");
8848   verifyGoogleFormat("A<A<A<A<int> > > > a;");
8849   verifyGoogleFormat("A<::A<int>> a;");
8850   verifyGoogleFormat("A<::A> a;");
8851   verifyGoogleFormat("A< ::A> a;");
8852   verifyGoogleFormat("A< ::A<int> > a;");
8853   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
8854   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
8855   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
8856   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
8857   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
8858             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
8859 
8860   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
8861 
8862   // template closer followed by a token that starts with > or =
8863   verifyFormat("bool b = a<1> > 1;");
8864   verifyFormat("bool b = a<1> >= 1;");
8865   verifyFormat("int i = a<1> >> 1;");
8866   FormatStyle Style = getLLVMStyle();
8867   Style.SpaceBeforeAssignmentOperators = false;
8868   verifyFormat("bool b= a<1> == 1;", Style);
8869   verifyFormat("a<int> = 1;", Style);
8870   verifyFormat("a<int> >>= 1;", Style);
8871 
8872   verifyFormat("test < a | b >> c;");
8873   verifyFormat("test<test<a | b>> c;");
8874   verifyFormat("test >> a >> b;");
8875   verifyFormat("test << a >> b;");
8876 
8877   verifyFormat("f<int>();");
8878   verifyFormat("template <typename T> void f() {}");
8879   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
8880   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
8881                "sizeof(char)>::type>;");
8882   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
8883   verifyFormat("f(a.operator()<A>());");
8884   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8885                "      .template operator()<A>());",
8886                getLLVMStyleWithColumns(35));
8887 
8888   // Not template parameters.
8889   verifyFormat("return a < b && c > d;");
8890   verifyFormat("void f() {\n"
8891                "  while (a < b && c > d) {\n"
8892                "  }\n"
8893                "}");
8894   verifyFormat("template <typename... Types>\n"
8895                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
8896 
8897   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8898                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
8899                getLLVMStyleWithColumns(60));
8900   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
8901   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
8902   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
8903   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
8904 }
8905 
8906 TEST_F(FormatTest, UnderstandsShiftOperators) {
8907   verifyFormat("if (i < x >> 1)");
8908   verifyFormat("while (i < x >> 1)");
8909   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
8910   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
8911   verifyFormat(
8912       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
8913   verifyFormat("Foo.call<Bar<Function>>()");
8914   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
8915   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
8916                "++i, v = v >> 1)");
8917   verifyFormat("if (w<u<v<x>>, 1>::t)");
8918 }
8919 
8920 TEST_F(FormatTest, BitshiftOperatorWidth) {
8921   EXPECT_EQ("int a = 1 << 2; /* foo\n"
8922             "                   bar */",
8923             format("int    a=1<<2;  /* foo\n"
8924                    "                   bar */"));
8925 
8926   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
8927             "                     bar */",
8928             format("int  b  =256>>1 ;  /* foo\n"
8929                    "                      bar */"));
8930 }
8931 
8932 TEST_F(FormatTest, UnderstandsBinaryOperators) {
8933   verifyFormat("COMPARE(a, ==, b);");
8934   verifyFormat("auto s = sizeof...(Ts) - 1;");
8935 }
8936 
8937 TEST_F(FormatTest, UnderstandsPointersToMembers) {
8938   verifyFormat("int A::*x;");
8939   verifyFormat("int (S::*func)(void *);");
8940   verifyFormat("void f() { int (S::*func)(void *); }");
8941   verifyFormat("typedef bool *(Class::*Member)() const;");
8942   verifyFormat("void f() {\n"
8943                "  (a->*f)();\n"
8944                "  a->*x;\n"
8945                "  (a.*f)();\n"
8946                "  ((*a).*f)();\n"
8947                "  a.*x;\n"
8948                "}");
8949   verifyFormat("void f() {\n"
8950                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
8951                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
8952                "}");
8953   verifyFormat(
8954       "(aaaaaaaaaa->*bbbbbbb)(\n"
8955       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8956   FormatStyle Style = getLLVMStyle();
8957   Style.PointerAlignment = FormatStyle::PAS_Left;
8958   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
8959 }
8960 
8961 TEST_F(FormatTest, UnderstandsUnaryOperators) {
8962   verifyFormat("int a = -2;");
8963   verifyFormat("f(-1, -2, -3);");
8964   verifyFormat("a[-1] = 5;");
8965   verifyFormat("int a = 5 + -2;");
8966   verifyFormat("if (i == -1) {\n}");
8967   verifyFormat("if (i != -1) {\n}");
8968   verifyFormat("if (i > -1) {\n}");
8969   verifyFormat("if (i < -1) {\n}");
8970   verifyFormat("++(a->f());");
8971   verifyFormat("--(a->f());");
8972   verifyFormat("(a->f())++;");
8973   verifyFormat("a[42]++;");
8974   verifyFormat("if (!(a->f())) {\n}");
8975   verifyFormat("if (!+i) {\n}");
8976   verifyFormat("~&a;");
8977 
8978   verifyFormat("a-- > b;");
8979   verifyFormat("b ? -a : c;");
8980   verifyFormat("n * sizeof char16;");
8981   verifyFormat("n * alignof char16;", getGoogleStyle());
8982   verifyFormat("sizeof(char);");
8983   verifyFormat("alignof(char);", getGoogleStyle());
8984 
8985   verifyFormat("return -1;");
8986   verifyFormat("throw -1;");
8987   verifyFormat("switch (a) {\n"
8988                "case -1:\n"
8989                "  break;\n"
8990                "}");
8991   verifyFormat("#define X -1");
8992   verifyFormat("#define X -kConstant");
8993 
8994   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
8995   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
8996 
8997   verifyFormat("int a = /* confusing comment */ -1;");
8998   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
8999   verifyFormat("int a = i /* confusing comment */++;");
9000 
9001   verifyFormat("co_yield -1;");
9002   verifyFormat("co_return -1;");
9003 
9004   // Check that * is not treated as a binary operator when we set
9005   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9006   FormatStyle PASLeftStyle = getLLVMStyle();
9007   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9008   verifyFormat("co_return *a;", PASLeftStyle);
9009   verifyFormat("co_await *a;", PASLeftStyle);
9010   verifyFormat("co_yield *a", PASLeftStyle);
9011   verifyFormat("return *a;", PASLeftStyle);
9012 }
9013 
9014 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9015   verifyFormat("if (!aaaaaaaaaa( // break\n"
9016                "        aaaaa)) {\n"
9017                "}");
9018   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9019                "    aaaaa));");
9020   verifyFormat("*aaa = aaaaaaa( // break\n"
9021                "    bbbbbb);");
9022 }
9023 
9024 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9025   verifyFormat("bool operator<();");
9026   verifyFormat("bool operator>();");
9027   verifyFormat("bool operator=();");
9028   verifyFormat("bool operator==();");
9029   verifyFormat("bool operator!=();");
9030   verifyFormat("int operator+();");
9031   verifyFormat("int operator++();");
9032   verifyFormat("int operator++(int) volatile noexcept;");
9033   verifyFormat("bool operator,();");
9034   verifyFormat("bool operator();");
9035   verifyFormat("bool operator()();");
9036   verifyFormat("bool operator[]();");
9037   verifyFormat("operator bool();");
9038   verifyFormat("operator int();");
9039   verifyFormat("operator void *();");
9040   verifyFormat("operator SomeType<int>();");
9041   verifyFormat("operator SomeType<int, int>();");
9042   verifyFormat("operator SomeType<SomeType<int>>();");
9043   verifyFormat("void *operator new(std::size_t size);");
9044   verifyFormat("void *operator new[](std::size_t size);");
9045   verifyFormat("void operator delete(void *ptr);");
9046   verifyFormat("void operator delete[](void *ptr);");
9047   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9048                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9049   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9050                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9051 
9052   verifyFormat(
9053       "ostream &operator<<(ostream &OutputStream,\n"
9054       "                    SomeReallyLongType WithSomeReallyLongValue);");
9055   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9056                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9057                "  return left.group < right.group;\n"
9058                "}");
9059   verifyFormat("SomeType &operator=(const SomeType &S);");
9060   verifyFormat("f.template operator()<int>();");
9061 
9062   verifyGoogleFormat("operator void*();");
9063   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9064   verifyGoogleFormat("operator ::A();");
9065 
9066   verifyFormat("using A::operator+;");
9067   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9068                "int i;");
9069 
9070   // Calling an operator as a member function.
9071   verifyFormat("void f() { a.operator*(); }");
9072   verifyFormat("void f() { a.operator*(b & b); }");
9073   verifyFormat("void f() { a->operator&(a * b); }");
9074   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9075   // TODO: Calling an operator as a non-member function is hard to distinguish.
9076   // https://llvm.org/PR50629
9077   // verifyFormat("void f() { operator*(a & a); }");
9078   // verifyFormat("void f() { operator&(a, b * b); }");
9079 }
9080 
9081 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9082   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9083   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9084   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9085   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9086   verifyFormat("Deleted &operator=(const Deleted &) &;");
9087   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9088   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9089   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9090   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9091   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9092   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9093   verifyFormat("void Fn(T const &) const &;");
9094   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9095   verifyFormat("template <typename T>\n"
9096                "void F(T) && = delete;",
9097                getGoogleStyle());
9098 
9099   FormatStyle AlignLeft = getLLVMStyle();
9100   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9101   verifyFormat("void A::b() && {}", AlignLeft);
9102   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9103   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9104                AlignLeft);
9105   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9106   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9107   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9108   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9109   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9110   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9111   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9112   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9113 
9114   FormatStyle Spaces = getLLVMStyle();
9115   Spaces.SpacesInCStyleCastParentheses = true;
9116   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9117   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9118   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9119   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9120 
9121   Spaces.SpacesInCStyleCastParentheses = false;
9122   Spaces.SpacesInParentheses = true;
9123   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9124   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9125                Spaces);
9126   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9127   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9128 
9129   FormatStyle BreakTemplate = getLLVMStyle();
9130   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9131 
9132   verifyFormat("struct f {\n"
9133                "  template <class T>\n"
9134                "  int &foo(const std::string &str) &noexcept {}\n"
9135                "};",
9136                BreakTemplate);
9137 
9138   verifyFormat("struct f {\n"
9139                "  template <class T>\n"
9140                "  int &foo(const std::string &str) &&noexcept {}\n"
9141                "};",
9142                BreakTemplate);
9143 
9144   verifyFormat("struct f {\n"
9145                "  template <class T>\n"
9146                "  int &foo(const std::string &str) const &noexcept {}\n"
9147                "};",
9148                BreakTemplate);
9149 
9150   verifyFormat("struct f {\n"
9151                "  template <class T>\n"
9152                "  int &foo(const std::string &str) const &noexcept {}\n"
9153                "};",
9154                BreakTemplate);
9155 
9156   verifyFormat("struct f {\n"
9157                "  template <class T>\n"
9158                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9159                "};",
9160                BreakTemplate);
9161 
9162   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9163   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9164       FormatStyle::BTDS_Yes;
9165   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9166 
9167   verifyFormat("struct f {\n"
9168                "  template <class T>\n"
9169                "  int& foo(const std::string& str) & noexcept {}\n"
9170                "};",
9171                AlignLeftBreakTemplate);
9172 
9173   verifyFormat("struct f {\n"
9174                "  template <class T>\n"
9175                "  int& foo(const std::string& str) && noexcept {}\n"
9176                "};",
9177                AlignLeftBreakTemplate);
9178 
9179   verifyFormat("struct f {\n"
9180                "  template <class T>\n"
9181                "  int& foo(const std::string& str) const& noexcept {}\n"
9182                "};",
9183                AlignLeftBreakTemplate);
9184 
9185   verifyFormat("struct f {\n"
9186                "  template <class T>\n"
9187                "  int& foo(const std::string& str) const&& noexcept {}\n"
9188                "};",
9189                AlignLeftBreakTemplate);
9190 
9191   verifyFormat("struct f {\n"
9192                "  template <class T>\n"
9193                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9194                "};",
9195                AlignLeftBreakTemplate);
9196 
9197   // The `&` in `Type&` should not be confused with a trailing `&` of
9198   // DEPRECATED(reason) member function.
9199   verifyFormat("struct f {\n"
9200                "  template <class T>\n"
9201                "  DEPRECATED(reason)\n"
9202                "  Type &foo(arguments) {}\n"
9203                "};",
9204                BreakTemplate);
9205 
9206   verifyFormat("struct f {\n"
9207                "  template <class T>\n"
9208                "  DEPRECATED(reason)\n"
9209                "  Type& foo(arguments) {}\n"
9210                "};",
9211                AlignLeftBreakTemplate);
9212 
9213   verifyFormat("void (*foopt)(int) = &func;");
9214 }
9215 
9216 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9217   verifyFormat("void f() {\n"
9218                "  A *a = new A;\n"
9219                "  A *a = new (placement) A;\n"
9220                "  delete a;\n"
9221                "  delete (A *)a;\n"
9222                "}");
9223   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9224                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9225   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9226                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9227                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9228   verifyFormat("delete[] h->p;");
9229 }
9230 
9231 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9232   verifyFormat("int *f(int *a) {}");
9233   verifyFormat("int main(int argc, char **argv) {}");
9234   verifyFormat("Test::Test(int b) : a(b * b) {}");
9235   verifyIndependentOfContext("f(a, *a);");
9236   verifyFormat("void g() { f(*a); }");
9237   verifyIndependentOfContext("int a = b * 10;");
9238   verifyIndependentOfContext("int a = 10 * b;");
9239   verifyIndependentOfContext("int a = b * c;");
9240   verifyIndependentOfContext("int a += b * c;");
9241   verifyIndependentOfContext("int a -= b * c;");
9242   verifyIndependentOfContext("int a *= b * c;");
9243   verifyIndependentOfContext("int a /= b * c;");
9244   verifyIndependentOfContext("int a = *b;");
9245   verifyIndependentOfContext("int a = *b * c;");
9246   verifyIndependentOfContext("int a = b * *c;");
9247   verifyIndependentOfContext("int a = b * (10);");
9248   verifyIndependentOfContext("S << b * (10);");
9249   verifyIndependentOfContext("return 10 * b;");
9250   verifyIndependentOfContext("return *b * *c;");
9251   verifyIndependentOfContext("return a & ~b;");
9252   verifyIndependentOfContext("f(b ? *c : *d);");
9253   verifyIndependentOfContext("int a = b ? *c : *d;");
9254   verifyIndependentOfContext("*b = a;");
9255   verifyIndependentOfContext("a * ~b;");
9256   verifyIndependentOfContext("a * !b;");
9257   verifyIndependentOfContext("a * +b;");
9258   verifyIndependentOfContext("a * -b;");
9259   verifyIndependentOfContext("a * ++b;");
9260   verifyIndependentOfContext("a * --b;");
9261   verifyIndependentOfContext("a[4] * b;");
9262   verifyIndependentOfContext("a[a * a] = 1;");
9263   verifyIndependentOfContext("f() * b;");
9264   verifyIndependentOfContext("a * [self dostuff];");
9265   verifyIndependentOfContext("int x = a * (a + b);");
9266   verifyIndependentOfContext("(a *)(a + b);");
9267   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9268   verifyIndependentOfContext("int *pa = (int *)&a;");
9269   verifyIndependentOfContext("return sizeof(int **);");
9270   verifyIndependentOfContext("return sizeof(int ******);");
9271   verifyIndependentOfContext("return (int **&)a;");
9272   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9273   verifyFormat("void f(Type (*parameter)[10]) {}");
9274   verifyFormat("void f(Type (&parameter)[10]) {}");
9275   verifyGoogleFormat("return sizeof(int**);");
9276   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9277   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9278   verifyFormat("auto a = [](int **&, int ***) {};");
9279   verifyFormat("auto PointerBinding = [](const char *S) {};");
9280   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9281   verifyFormat("[](const decltype(*a) &value) {}");
9282   verifyFormat("[](const typeof(*a) &value) {}");
9283   verifyFormat("[](const _Atomic(a *) &value) {}");
9284   verifyFormat("[](const __underlying_type(a) &value) {}");
9285   verifyFormat("decltype(a * b) F();");
9286   verifyFormat("typeof(a * b) F();");
9287   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9288   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9289   verifyIndependentOfContext("typedef void (*f)(int *a);");
9290   verifyIndependentOfContext("int i{a * b};");
9291   verifyIndependentOfContext("aaa && aaa->f();");
9292   verifyIndependentOfContext("int x = ~*p;");
9293   verifyFormat("Constructor() : a(a), area(width * height) {}");
9294   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9295   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9296   verifyFormat("void f() { f(a, c * d); }");
9297   verifyFormat("void f() { f(new a(), c * d); }");
9298   verifyFormat("void f(const MyOverride &override);");
9299   verifyFormat("void f(const MyFinal &final);");
9300   verifyIndependentOfContext("bool a = f() && override.f();");
9301   verifyIndependentOfContext("bool a = f() && final.f();");
9302 
9303   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9304 
9305   verifyIndependentOfContext("A<int *> a;");
9306   verifyIndependentOfContext("A<int **> a;");
9307   verifyIndependentOfContext("A<int *, int *> a;");
9308   verifyIndependentOfContext("A<int *[]> a;");
9309   verifyIndependentOfContext(
9310       "const char *const p = reinterpret_cast<const char *const>(q);");
9311   verifyIndependentOfContext("A<int **, int **> a;");
9312   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9313   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9314   verifyFormat("for (; a && b;) {\n}");
9315   verifyFormat("bool foo = true && [] { return false; }();");
9316 
9317   verifyFormat(
9318       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9319       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9320 
9321   verifyGoogleFormat("int const* a = &b;");
9322   verifyGoogleFormat("**outparam = 1;");
9323   verifyGoogleFormat("*outparam = a * b;");
9324   verifyGoogleFormat("int main(int argc, char** argv) {}");
9325   verifyGoogleFormat("A<int*> a;");
9326   verifyGoogleFormat("A<int**> a;");
9327   verifyGoogleFormat("A<int*, int*> a;");
9328   verifyGoogleFormat("A<int**, int**> a;");
9329   verifyGoogleFormat("f(b ? *c : *d);");
9330   verifyGoogleFormat("int a = b ? *c : *d;");
9331   verifyGoogleFormat("Type* t = **x;");
9332   verifyGoogleFormat("Type* t = *++*x;");
9333   verifyGoogleFormat("*++*x;");
9334   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9335   verifyGoogleFormat("Type* t = x++ * y;");
9336   verifyGoogleFormat(
9337       "const char* const p = reinterpret_cast<const char* const>(q);");
9338   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9339   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9340   verifyGoogleFormat("template <typename T>\n"
9341                      "void f(int i = 0, SomeType** temps = NULL);");
9342 
9343   FormatStyle Left = getLLVMStyle();
9344   Left.PointerAlignment = FormatStyle::PAS_Left;
9345   verifyFormat("x = *a(x) = *a(y);", Left);
9346   verifyFormat("for (;; *a = b) {\n}", Left);
9347   verifyFormat("return *this += 1;", Left);
9348   verifyFormat("throw *x;", Left);
9349   verifyFormat("delete *x;", Left);
9350   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9351   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9352   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9353   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9354   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9355   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9356   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9357   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9358   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9359 
9360   verifyIndependentOfContext("a = *(x + y);");
9361   verifyIndependentOfContext("a = &(x + y);");
9362   verifyIndependentOfContext("*(x + y).call();");
9363   verifyIndependentOfContext("&(x + y)->call();");
9364   verifyFormat("void f() { &(*I).first; }");
9365 
9366   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9367   verifyFormat(
9368       "int *MyValues = {\n"
9369       "    *A, // Operator detection might be confused by the '{'\n"
9370       "    *BB // Operator detection might be confused by previous comment\n"
9371       "};");
9372 
9373   verifyIndependentOfContext("if (int *a = &b)");
9374   verifyIndependentOfContext("if (int &a = *b)");
9375   verifyIndependentOfContext("if (a & b[i])");
9376   verifyIndependentOfContext("if constexpr (a & b[i])");
9377   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9378   verifyIndependentOfContext("if (a * (b * c))");
9379   verifyIndependentOfContext("if constexpr (a * (b * c))");
9380   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9381   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9382   verifyIndependentOfContext("if (*b[i])");
9383   verifyIndependentOfContext("if (int *a = (&b))");
9384   verifyIndependentOfContext("while (int *a = &b)");
9385   verifyIndependentOfContext("while (a * (b * c))");
9386   verifyIndependentOfContext("size = sizeof *a;");
9387   verifyIndependentOfContext("if (a && (b = c))");
9388   verifyFormat("void f() {\n"
9389                "  for (const int &v : Values) {\n"
9390                "  }\n"
9391                "}");
9392   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9393   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9394   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9395 
9396   verifyFormat("#define A (!a * b)");
9397   verifyFormat("#define MACRO     \\\n"
9398                "  int *i = a * b; \\\n"
9399                "  void f(a *b);",
9400                getLLVMStyleWithColumns(19));
9401 
9402   verifyIndependentOfContext("A = new SomeType *[Length];");
9403   verifyIndependentOfContext("A = new SomeType *[Length]();");
9404   verifyIndependentOfContext("T **t = new T *;");
9405   verifyIndependentOfContext("T **t = new T *();");
9406   verifyGoogleFormat("A = new SomeType*[Length]();");
9407   verifyGoogleFormat("A = new SomeType*[Length];");
9408   verifyGoogleFormat("T** t = new T*;");
9409   verifyGoogleFormat("T** t = new T*();");
9410 
9411   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9412   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9413   verifyFormat("template <bool a, bool b> "
9414                "typename t::if<x && y>::type f() {}");
9415   verifyFormat("template <int *y> f() {}");
9416   verifyFormat("vector<int *> v;");
9417   verifyFormat("vector<int *const> v;");
9418   verifyFormat("vector<int *const **const *> v;");
9419   verifyFormat("vector<int *volatile> v;");
9420   verifyFormat("vector<a *_Nonnull> v;");
9421   verifyFormat("vector<a *_Nullable> v;");
9422   verifyFormat("vector<a *_Null_unspecified> v;");
9423   verifyFormat("vector<a *__ptr32> v;");
9424   verifyFormat("vector<a *__ptr64> v;");
9425   verifyFormat("vector<a *__capability> v;");
9426   FormatStyle TypeMacros = getLLVMStyle();
9427   TypeMacros.TypenameMacros = {"LIST"};
9428   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9429   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9430   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9431   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9432   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9433 
9434   FormatStyle CustomQualifier = getLLVMStyle();
9435   // Add identifiers that should not be parsed as a qualifier by default.
9436   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9437   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9438   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9439   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9440   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9441   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9442   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9443   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9444   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9445   verifyFormat("vector<a * _NotAQualifier> v;");
9446   verifyFormat("vector<a * __not_a_qualifier> v;");
9447   verifyFormat("vector<a * b> v;");
9448   verifyFormat("foo<b && false>();");
9449   verifyFormat("foo<b & 1>();");
9450   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9451   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9452   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9453   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9454   verifyFormat(
9455       "template <class T, class = typename std::enable_if<\n"
9456       "                       std::is_integral<T>::value &&\n"
9457       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9458       "void F();",
9459       getLLVMStyleWithColumns(70));
9460   verifyFormat("template <class T,\n"
9461                "          class = typename std::enable_if<\n"
9462                "              std::is_integral<T>::value &&\n"
9463                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9464                "          class U>\n"
9465                "void F();",
9466                getLLVMStyleWithColumns(70));
9467   verifyFormat(
9468       "template <class T,\n"
9469       "          class = typename ::std::enable_if<\n"
9470       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9471       "void F();",
9472       getGoogleStyleWithColumns(68));
9473 
9474   verifyIndependentOfContext("MACRO(int *i);");
9475   verifyIndependentOfContext("MACRO(auto *a);");
9476   verifyIndependentOfContext("MACRO(const A *a);");
9477   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9478   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9479   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9480   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9481   verifyIndependentOfContext("MACRO(A *const a);");
9482   verifyIndependentOfContext("MACRO(A *restrict a);");
9483   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9484   verifyIndependentOfContext("MACRO(A *__restrict a);");
9485   verifyIndependentOfContext("MACRO(A *volatile a);");
9486   verifyIndependentOfContext("MACRO(A *__volatile a);");
9487   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9488   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9489   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9490   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9491   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9492   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9493   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9494   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9495   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9496   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9497   verifyIndependentOfContext("MACRO(A *__capability);");
9498   verifyIndependentOfContext("MACRO(A &__capability);");
9499   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9500   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9501   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9502   // a type declaration:
9503   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
9504   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
9505   // Also check that TypenameMacros prevents parsing it as multiplication:
9506   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
9507   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
9508 
9509   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
9510   verifyFormat("void f() { f(float{1}, a * a); }");
9511   verifyFormat("void f() { f(float(1), a * a); }");
9512 
9513   verifyFormat("f((void (*)(int))g);");
9514   verifyFormat("f((void (&)(int))g);");
9515   verifyFormat("f((void (^)(int))g);");
9516 
9517   // FIXME: Is there a way to make this work?
9518   // verifyIndependentOfContext("MACRO(A *a);");
9519   verifyFormat("MACRO(A &B);");
9520   verifyFormat("MACRO(A *B);");
9521   verifyFormat("void f() { MACRO(A * B); }");
9522   verifyFormat("void f() { MACRO(A & B); }");
9523 
9524   // This lambda was mis-formatted after D88956 (treating it as a binop):
9525   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
9526   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
9527   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
9528   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
9529 
9530   verifyFormat("DatumHandle const *operator->() const { return input_; }");
9531   verifyFormat("return options != nullptr && operator==(*options);");
9532 
9533   EXPECT_EQ("#define OP(x)                                    \\\n"
9534             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
9535             "    return s << a.DebugString();                 \\\n"
9536             "  }",
9537             format("#define OP(x) \\\n"
9538                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
9539                    "    return s << a.DebugString(); \\\n"
9540                    "  }",
9541                    getLLVMStyleWithColumns(50)));
9542 
9543   // FIXME: We cannot handle this case yet; we might be able to figure out that
9544   // foo<x> d > v; doesn't make sense.
9545   verifyFormat("foo<a<b && c> d> v;");
9546 
9547   FormatStyle PointerMiddle = getLLVMStyle();
9548   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9549   verifyFormat("delete *x;", PointerMiddle);
9550   verifyFormat("int * x;", PointerMiddle);
9551   verifyFormat("int *[] x;", PointerMiddle);
9552   verifyFormat("template <int * y> f() {}", PointerMiddle);
9553   verifyFormat("int * f(int * a) {}", PointerMiddle);
9554   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
9555   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
9556   verifyFormat("A<int *> a;", PointerMiddle);
9557   verifyFormat("A<int **> a;", PointerMiddle);
9558   verifyFormat("A<int *, int *> a;", PointerMiddle);
9559   verifyFormat("A<int *[]> a;", PointerMiddle);
9560   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
9561   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
9562   verifyFormat("T ** t = new T *;", PointerMiddle);
9563 
9564   // Member function reference qualifiers aren't binary operators.
9565   verifyFormat("string // break\n"
9566                "operator()() & {}");
9567   verifyFormat("string // break\n"
9568                "operator()() && {}");
9569   verifyGoogleFormat("template <typename T>\n"
9570                      "auto x() & -> int {}");
9571 
9572   // Should be binary operators when used as an argument expression (overloaded
9573   // operator invoked as a member function).
9574   verifyFormat("void f() { a.operator()(a * a); }");
9575   verifyFormat("void f() { a->operator()(a & a); }");
9576   verifyFormat("void f() { a.operator()(*a & *a); }");
9577   verifyFormat("void f() { a->operator()(*a * *a); }");
9578 }
9579 
9580 TEST_F(FormatTest, UnderstandsAttributes) {
9581   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
9582   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
9583                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9584   FormatStyle AfterType = getLLVMStyle();
9585   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9586   verifyFormat("__attribute__((nodebug)) void\n"
9587                "foo() {}\n",
9588                AfterType);
9589   verifyFormat("__unused void\n"
9590                "foo() {}",
9591                AfterType);
9592 
9593   FormatStyle CustomAttrs = getLLVMStyle();
9594   CustomAttrs.AttributeMacros.push_back("__unused");
9595   CustomAttrs.AttributeMacros.push_back("__attr1");
9596   CustomAttrs.AttributeMacros.push_back("__attr2");
9597   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
9598   verifyFormat("vector<SomeType *__attribute((foo))> v;");
9599   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
9600   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
9601   // Check that it is parsed as a multiplication without AttributeMacros and
9602   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
9603   verifyFormat("vector<SomeType * __attr1> v;");
9604   verifyFormat("vector<SomeType __attr1 *> v;");
9605   verifyFormat("vector<SomeType __attr1 *const> v;");
9606   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
9607   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
9608   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
9609   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
9610   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
9611   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
9612   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
9613   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
9614 
9615   // Check that these are not parsed as function declarations:
9616   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9617   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
9618   verifyFormat("SomeType s(InitValue);", CustomAttrs);
9619   verifyFormat("SomeType s{InitValue};", CustomAttrs);
9620   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
9621   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
9622   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
9623   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
9624   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
9625   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
9626 }
9627 
9628 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
9629   // Check that qualifiers on pointers don't break parsing of casts.
9630   verifyFormat("x = (foo *const)*v;");
9631   verifyFormat("x = (foo *volatile)*v;");
9632   verifyFormat("x = (foo *restrict)*v;");
9633   verifyFormat("x = (foo *__attribute__((foo)))*v;");
9634   verifyFormat("x = (foo *_Nonnull)*v;");
9635   verifyFormat("x = (foo *_Nullable)*v;");
9636   verifyFormat("x = (foo *_Null_unspecified)*v;");
9637   verifyFormat("x = (foo *_Nonnull)*v;");
9638   verifyFormat("x = (foo *[[clang::attr]])*v;");
9639   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
9640   verifyFormat("x = (foo *__ptr32)*v;");
9641   verifyFormat("x = (foo *__ptr64)*v;");
9642   verifyFormat("x = (foo *__capability)*v;");
9643 
9644   // Check that we handle multiple trailing qualifiers and skip them all to
9645   // determine that the expression is a cast to a pointer type.
9646   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
9647   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
9648   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
9649   StringRef AllQualifiers =
9650       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
9651       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
9652   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
9653   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
9654 
9655   // Also check that address-of is not parsed as a binary bitwise-and:
9656   verifyFormat("x = (foo *const)&v;");
9657   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
9658   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
9659 
9660   // Check custom qualifiers:
9661   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
9662   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9663   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
9664   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
9665   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
9666                CustomQualifier);
9667   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
9668                CustomQualifier);
9669 
9670   // Check that unknown identifiers result in binary operator parsing:
9671   verifyFormat("x = (foo * __unknown_qualifier) * v;");
9672   verifyFormat("x = (foo * __unknown_qualifier) & v;");
9673 }
9674 
9675 TEST_F(FormatTest, UnderstandsSquareAttributes) {
9676   verifyFormat("SomeType s [[unused]] (InitValue);");
9677   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
9678   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
9679   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
9680   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
9681   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9682                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9683   verifyFormat("[[nodiscard]] bool f() { return false; }");
9684   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
9685   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
9686   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
9687 
9688   // Make sure we do not mistake attributes for array subscripts.
9689   verifyFormat("int a() {}\n"
9690                "[[unused]] int b() {}\n");
9691   verifyFormat("NSArray *arr;\n"
9692                "arr[[Foo() bar]];");
9693 
9694   // On the other hand, we still need to correctly find array subscripts.
9695   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
9696 
9697   // Make sure that we do not mistake Objective-C method inside array literals
9698   // as attributes, even if those method names are also keywords.
9699   verifyFormat("@[ [foo bar] ];");
9700   verifyFormat("@[ [NSArray class] ];");
9701   verifyFormat("@[ [foo enum] ];");
9702 
9703   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
9704 
9705   // Make sure we do not parse attributes as lambda introducers.
9706   FormatStyle MultiLineFunctions = getLLVMStyle();
9707   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9708   verifyFormat("[[unused]] int b() {\n"
9709                "  return 42;\n"
9710                "}\n",
9711                MultiLineFunctions);
9712 }
9713 
9714 TEST_F(FormatTest, AttributeClass) {
9715   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
9716   verifyFormat("class S {\n"
9717                "  S(S&&) = default;\n"
9718                "};",
9719                Style);
9720   verifyFormat("class [[nodiscard]] S {\n"
9721                "  S(S&&) = default;\n"
9722                "};",
9723                Style);
9724   verifyFormat("class __attribute((maybeunused)) S {\n"
9725                "  S(S&&) = default;\n"
9726                "};",
9727                Style);
9728   verifyFormat("struct S {\n"
9729                "  S(S&&) = default;\n"
9730                "};",
9731                Style);
9732   verifyFormat("struct [[nodiscard]] S {\n"
9733                "  S(S&&) = default;\n"
9734                "};",
9735                Style);
9736 }
9737 
9738 TEST_F(FormatTest, AttributesAfterMacro) {
9739   FormatStyle Style = getLLVMStyle();
9740   verifyFormat("MACRO;\n"
9741                "__attribute__((maybe_unused)) int foo() {\n"
9742                "  //...\n"
9743                "}");
9744 
9745   verifyFormat("MACRO;\n"
9746                "[[nodiscard]] int foo() {\n"
9747                "  //...\n"
9748                "}");
9749 
9750   EXPECT_EQ("MACRO\n\n"
9751             "__attribute__((maybe_unused)) int foo() {\n"
9752             "  //...\n"
9753             "}",
9754             format("MACRO\n\n"
9755                    "__attribute__((maybe_unused)) int foo() {\n"
9756                    "  //...\n"
9757                    "}"));
9758 
9759   EXPECT_EQ("MACRO\n\n"
9760             "[[nodiscard]] int foo() {\n"
9761             "  //...\n"
9762             "}",
9763             format("MACRO\n\n"
9764                    "[[nodiscard]] int foo() {\n"
9765                    "  //...\n"
9766                    "}"));
9767 }
9768 
9769 TEST_F(FormatTest, AttributePenaltyBreaking) {
9770   FormatStyle Style = getLLVMStyle();
9771   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
9772                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9773                Style);
9774   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
9775                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9776                Style);
9777   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
9778                "shared_ptr<ALongTypeName> &C d) {\n}",
9779                Style);
9780 }
9781 
9782 TEST_F(FormatTest, UnderstandsEllipsis) {
9783   FormatStyle Style = getLLVMStyle();
9784   verifyFormat("int printf(const char *fmt, ...);");
9785   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
9786   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
9787 
9788   verifyFormat("template <int *...PP> a;", Style);
9789 
9790   Style.PointerAlignment = FormatStyle::PAS_Left;
9791   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
9792 
9793   verifyFormat("template <int*... PP> a;", Style);
9794 
9795   Style.PointerAlignment = FormatStyle::PAS_Middle;
9796   verifyFormat("template <int *... PP> a;", Style);
9797 }
9798 
9799 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
9800   EXPECT_EQ("int *a;\n"
9801             "int *a;\n"
9802             "int *a;",
9803             format("int *a;\n"
9804                    "int* a;\n"
9805                    "int *a;",
9806                    getGoogleStyle()));
9807   EXPECT_EQ("int* a;\n"
9808             "int* a;\n"
9809             "int* a;",
9810             format("int* a;\n"
9811                    "int* a;\n"
9812                    "int *a;",
9813                    getGoogleStyle()));
9814   EXPECT_EQ("int *a;\n"
9815             "int *a;\n"
9816             "int *a;",
9817             format("int *a;\n"
9818                    "int * a;\n"
9819                    "int *  a;",
9820                    getGoogleStyle()));
9821   EXPECT_EQ("auto x = [] {\n"
9822             "  int *a;\n"
9823             "  int *a;\n"
9824             "  int *a;\n"
9825             "};",
9826             format("auto x=[]{int *a;\n"
9827                    "int * a;\n"
9828                    "int *  a;};",
9829                    getGoogleStyle()));
9830 }
9831 
9832 TEST_F(FormatTest, UnderstandsRvalueReferences) {
9833   verifyFormat("int f(int &&a) {}");
9834   verifyFormat("int f(int a, char &&b) {}");
9835   verifyFormat("void f() { int &&a = b; }");
9836   verifyGoogleFormat("int f(int a, char&& b) {}");
9837   verifyGoogleFormat("void f() { int&& a = b; }");
9838 
9839   verifyIndependentOfContext("A<int &&> a;");
9840   verifyIndependentOfContext("A<int &&, int &&> a;");
9841   verifyGoogleFormat("A<int&&> a;");
9842   verifyGoogleFormat("A<int&&, int&&> a;");
9843 
9844   // Not rvalue references:
9845   verifyFormat("template <bool B, bool C> class A {\n"
9846                "  static_assert(B && C, \"Something is wrong\");\n"
9847                "};");
9848   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
9849   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
9850   verifyFormat("#define A(a, b) (a && b)");
9851 }
9852 
9853 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
9854   verifyFormat("void f() {\n"
9855                "  x[aaaaaaaaa -\n"
9856                "    b] = 23;\n"
9857                "}",
9858                getLLVMStyleWithColumns(15));
9859 }
9860 
9861 TEST_F(FormatTest, FormatsCasts) {
9862   verifyFormat("Type *A = static_cast<Type *>(P);");
9863   verifyFormat("Type *A = (Type *)P;");
9864   verifyFormat("Type *A = (vector<Type *, int *>)P;");
9865   verifyFormat("int a = (int)(2.0f);");
9866   verifyFormat("int a = (int)2.0f;");
9867   verifyFormat("x[(int32)y];");
9868   verifyFormat("x = (int32)y;");
9869   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
9870   verifyFormat("int a = (int)*b;");
9871   verifyFormat("int a = (int)2.0f;");
9872   verifyFormat("int a = (int)~0;");
9873   verifyFormat("int a = (int)++a;");
9874   verifyFormat("int a = (int)sizeof(int);");
9875   verifyFormat("int a = (int)+2;");
9876   verifyFormat("my_int a = (my_int)2.0f;");
9877   verifyFormat("my_int a = (my_int)sizeof(int);");
9878   verifyFormat("return (my_int)aaa;");
9879   verifyFormat("#define x ((int)-1)");
9880   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
9881   verifyFormat("#define p(q) ((int *)&q)");
9882   verifyFormat("fn(a)(b) + 1;");
9883 
9884   verifyFormat("void f() { my_int a = (my_int)*b; }");
9885   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
9886   verifyFormat("my_int a = (my_int)~0;");
9887   verifyFormat("my_int a = (my_int)++a;");
9888   verifyFormat("my_int a = (my_int)-2;");
9889   verifyFormat("my_int a = (my_int)1;");
9890   verifyFormat("my_int a = (my_int *)1;");
9891   verifyFormat("my_int a = (const my_int)-1;");
9892   verifyFormat("my_int a = (const my_int *)-1;");
9893   verifyFormat("my_int a = (my_int)(my_int)-1;");
9894   verifyFormat("my_int a = (ns::my_int)-2;");
9895   verifyFormat("case (my_int)ONE:");
9896   verifyFormat("auto x = (X)this;");
9897   // Casts in Obj-C style calls used to not be recognized as such.
9898   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
9899 
9900   // FIXME: single value wrapped with paren will be treated as cast.
9901   verifyFormat("void f(int i = (kValue)*kMask) {}");
9902 
9903   verifyFormat("{ (void)F; }");
9904 
9905   // Don't break after a cast's
9906   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9907                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
9908                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
9909 
9910   // These are not casts.
9911   verifyFormat("void f(int *) {}");
9912   verifyFormat("f(foo)->b;");
9913   verifyFormat("f(foo).b;");
9914   verifyFormat("f(foo)(b);");
9915   verifyFormat("f(foo)[b];");
9916   verifyFormat("[](foo) { return 4; }(bar);");
9917   verifyFormat("(*funptr)(foo)[4];");
9918   verifyFormat("funptrs[4](foo)[4];");
9919   verifyFormat("void f(int *);");
9920   verifyFormat("void f(int *) = 0;");
9921   verifyFormat("void f(SmallVector<int>) {}");
9922   verifyFormat("void f(SmallVector<int>);");
9923   verifyFormat("void f(SmallVector<int>) = 0;");
9924   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
9925   verifyFormat("int a = sizeof(int) * b;");
9926   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
9927   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
9928   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
9929   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
9930 
9931   // These are not casts, but at some point were confused with casts.
9932   verifyFormat("virtual void foo(int *) override;");
9933   verifyFormat("virtual void foo(char &) const;");
9934   verifyFormat("virtual void foo(int *a, char *) const;");
9935   verifyFormat("int a = sizeof(int *) + b;");
9936   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
9937   verifyFormat("bool b = f(g<int>) && c;");
9938   verifyFormat("typedef void (*f)(int i) func;");
9939   verifyFormat("void operator++(int) noexcept;");
9940   verifyFormat("void operator++(int &) noexcept;");
9941   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
9942                "&) noexcept;");
9943   verifyFormat(
9944       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
9945   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
9946   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
9947   verifyFormat("void operator delete(nothrow_t &) noexcept;");
9948   verifyFormat("void operator delete(foo &) noexcept;");
9949   verifyFormat("void operator delete(foo) noexcept;");
9950   verifyFormat("void operator delete(int) noexcept;");
9951   verifyFormat("void operator delete(int &) noexcept;");
9952   verifyFormat("void operator delete(int &) volatile noexcept;");
9953   verifyFormat("void operator delete(int &) const");
9954   verifyFormat("void operator delete(int &) = default");
9955   verifyFormat("void operator delete(int &) = delete");
9956   verifyFormat("void operator delete(int &) [[noreturn]]");
9957   verifyFormat("void operator delete(int &) throw();");
9958   verifyFormat("void operator delete(int &) throw(int);");
9959   verifyFormat("auto operator delete(int &) -> int;");
9960   verifyFormat("auto operator delete(int &) override");
9961   verifyFormat("auto operator delete(int &) final");
9962 
9963   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
9964                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
9965   // FIXME: The indentation here is not ideal.
9966   verifyFormat(
9967       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9968       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
9969       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
9970 }
9971 
9972 TEST_F(FormatTest, FormatsFunctionTypes) {
9973   verifyFormat("A<bool()> a;");
9974   verifyFormat("A<SomeType()> a;");
9975   verifyFormat("A<void (*)(int, std::string)> a;");
9976   verifyFormat("A<void *(int)>;");
9977   verifyFormat("void *(*a)(int *, SomeType *);");
9978   verifyFormat("int (*func)(void *);");
9979   verifyFormat("void f() { int (*func)(void *); }");
9980   verifyFormat("template <class CallbackClass>\n"
9981                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
9982 
9983   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
9984   verifyGoogleFormat("void* (*a)(int);");
9985   verifyGoogleFormat(
9986       "template <class CallbackClass>\n"
9987       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
9988 
9989   // Other constructs can look somewhat like function types:
9990   verifyFormat("A<sizeof(*x)> a;");
9991   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
9992   verifyFormat("some_var = function(*some_pointer_var)[0];");
9993   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
9994   verifyFormat("int x = f(&h)();");
9995   verifyFormat("returnsFunction(&param1, &param2)(param);");
9996   verifyFormat("std::function<\n"
9997                "    LooooooooooongTemplatedType<\n"
9998                "        SomeType>*(\n"
9999                "        LooooooooooooooooongType type)>\n"
10000                "    function;",
10001                getGoogleStyleWithColumns(40));
10002 }
10003 
10004 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10005   verifyFormat("A (*foo_)[6];");
10006   verifyFormat("vector<int> (*foo_)[6];");
10007 }
10008 
10009 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10010   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10011                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10012   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10013                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10014   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10015                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10016 
10017   // Different ways of ()-initializiation.
10018   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10019                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10020   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10021                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10022   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10023                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10024   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10025                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10026 
10027   // Lambdas should not confuse the variable declaration heuristic.
10028   verifyFormat("LooooooooooooooooongType\n"
10029                "    variable(nullptr, [](A *a) {});",
10030                getLLVMStyleWithColumns(40));
10031 }
10032 
10033 TEST_F(FormatTest, BreaksLongDeclarations) {
10034   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10035                "    AnotherNameForTheLongType;");
10036   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10037                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10038   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10039                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10040   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10041                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10042   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10043                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10044   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10045                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10046   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10047                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10048   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10049                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10050   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10051                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10052   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10053                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10054   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10055                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10056   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10057                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10058   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10059                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10060   FormatStyle Indented = getLLVMStyle();
10061   Indented.IndentWrappedFunctionNames = true;
10062   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10063                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10064                Indented);
10065   verifyFormat(
10066       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10067       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10068       Indented);
10069   verifyFormat(
10070       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10071       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10072       Indented);
10073   verifyFormat(
10074       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10075       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10076       Indented);
10077 
10078   // FIXME: Without the comment, this breaks after "(".
10079   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10080                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10081                getGoogleStyle());
10082 
10083   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10084                "                  int LoooooooooooooooooooongParam2) {}");
10085   verifyFormat(
10086       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10087       "                                   SourceLocation L, IdentifierIn *II,\n"
10088       "                                   Type *T) {}");
10089   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10090                "ReallyReaaallyLongFunctionName(\n"
10091                "    const std::string &SomeParameter,\n"
10092                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10093                "        &ReallyReallyLongParameterName,\n"
10094                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10095                "        &AnotherLongParameterName) {}");
10096   verifyFormat("template <typename A>\n"
10097                "SomeLoooooooooooooooooooooongType<\n"
10098                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10099                "Function() {}");
10100 
10101   verifyGoogleFormat(
10102       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10103       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10104   verifyGoogleFormat(
10105       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10106       "                                   SourceLocation L) {}");
10107   verifyGoogleFormat(
10108       "some_namespace::LongReturnType\n"
10109       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10110       "    int first_long_parameter, int second_parameter) {}");
10111 
10112   verifyGoogleFormat("template <typename T>\n"
10113                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10114                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10115   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10116                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10117 
10118   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10119                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10120                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10121   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10122                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10123                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10124   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10125                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10126                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10127                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10128 
10129   verifyFormat("template <typename T> // Templates on own line.\n"
10130                "static int            // Some comment.\n"
10131                "MyFunction(int a);",
10132                getLLVMStyle());
10133 }
10134 
10135 TEST_F(FormatTest, FormatsAccessModifiers) {
10136   FormatStyle Style = getLLVMStyle();
10137   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10138             FormatStyle::ELBAMS_LogicalBlock);
10139   verifyFormat("struct foo {\n"
10140                "private:\n"
10141                "  void f() {}\n"
10142                "\n"
10143                "private:\n"
10144                "  int i;\n"
10145                "\n"
10146                "protected:\n"
10147                "  int j;\n"
10148                "};\n",
10149                Style);
10150   verifyFormat("struct foo {\n"
10151                "private:\n"
10152                "  void f() {}\n"
10153                "\n"
10154                "private:\n"
10155                "  int i;\n"
10156                "\n"
10157                "protected:\n"
10158                "  int j;\n"
10159                "};\n",
10160                "struct foo {\n"
10161                "private:\n"
10162                "  void f() {}\n"
10163                "private:\n"
10164                "  int i;\n"
10165                "protected:\n"
10166                "  int j;\n"
10167                "};\n",
10168                Style);
10169   verifyFormat("struct foo { /* comment */\n"
10170                "private:\n"
10171                "  int i;\n"
10172                "  // comment\n"
10173                "private:\n"
10174                "  int j;\n"
10175                "};\n",
10176                Style);
10177   verifyFormat("struct foo {\n"
10178                "#ifdef FOO\n"
10179                "#endif\n"
10180                "private:\n"
10181                "  int i;\n"
10182                "#ifdef FOO\n"
10183                "private:\n"
10184                "#endif\n"
10185                "  int j;\n"
10186                "};\n",
10187                Style);
10188   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10189   verifyFormat("struct foo {\n"
10190                "private:\n"
10191                "  void f() {}\n"
10192                "private:\n"
10193                "  int i;\n"
10194                "protected:\n"
10195                "  int j;\n"
10196                "};\n",
10197                Style);
10198   verifyFormat("struct foo {\n"
10199                "private:\n"
10200                "  void f() {}\n"
10201                "private:\n"
10202                "  int i;\n"
10203                "protected:\n"
10204                "  int j;\n"
10205                "};\n",
10206                "struct foo {\n"
10207                "\n"
10208                "private:\n"
10209                "  void f() {}\n"
10210                "\n"
10211                "private:\n"
10212                "  int i;\n"
10213                "\n"
10214                "protected:\n"
10215                "  int j;\n"
10216                "};\n",
10217                Style);
10218   verifyFormat("struct foo { /* comment */\n"
10219                "private:\n"
10220                "  int i;\n"
10221                "  // comment\n"
10222                "private:\n"
10223                "  int j;\n"
10224                "};\n",
10225                "struct foo { /* comment */\n"
10226                "\n"
10227                "private:\n"
10228                "  int i;\n"
10229                "  // comment\n"
10230                "\n"
10231                "private:\n"
10232                "  int j;\n"
10233                "};\n",
10234                Style);
10235   verifyFormat("struct foo {\n"
10236                "#ifdef FOO\n"
10237                "#endif\n"
10238                "private:\n"
10239                "  int i;\n"
10240                "#ifdef FOO\n"
10241                "private:\n"
10242                "#endif\n"
10243                "  int j;\n"
10244                "};\n",
10245                "struct foo {\n"
10246                "#ifdef FOO\n"
10247                "#endif\n"
10248                "\n"
10249                "private:\n"
10250                "  int i;\n"
10251                "#ifdef FOO\n"
10252                "\n"
10253                "private:\n"
10254                "#endif\n"
10255                "  int j;\n"
10256                "};\n",
10257                Style);
10258   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10259   verifyFormat("struct foo {\n"
10260                "private:\n"
10261                "  void f() {}\n"
10262                "\n"
10263                "private:\n"
10264                "  int i;\n"
10265                "\n"
10266                "protected:\n"
10267                "  int j;\n"
10268                "};\n",
10269                Style);
10270   verifyFormat("struct foo {\n"
10271                "private:\n"
10272                "  void f() {}\n"
10273                "\n"
10274                "private:\n"
10275                "  int i;\n"
10276                "\n"
10277                "protected:\n"
10278                "  int j;\n"
10279                "};\n",
10280                "struct foo {\n"
10281                "private:\n"
10282                "  void f() {}\n"
10283                "private:\n"
10284                "  int i;\n"
10285                "protected:\n"
10286                "  int j;\n"
10287                "};\n",
10288                Style);
10289   verifyFormat("struct foo { /* comment */\n"
10290                "private:\n"
10291                "  int i;\n"
10292                "  // comment\n"
10293                "\n"
10294                "private:\n"
10295                "  int j;\n"
10296                "};\n",
10297                "struct foo { /* comment */\n"
10298                "private:\n"
10299                "  int i;\n"
10300                "  // comment\n"
10301                "\n"
10302                "private:\n"
10303                "  int j;\n"
10304                "};\n",
10305                Style);
10306   verifyFormat("struct foo {\n"
10307                "#ifdef FOO\n"
10308                "#endif\n"
10309                "\n"
10310                "private:\n"
10311                "  int i;\n"
10312                "#ifdef FOO\n"
10313                "\n"
10314                "private:\n"
10315                "#endif\n"
10316                "  int j;\n"
10317                "};\n",
10318                "struct foo {\n"
10319                "#ifdef FOO\n"
10320                "#endif\n"
10321                "private:\n"
10322                "  int i;\n"
10323                "#ifdef FOO\n"
10324                "private:\n"
10325                "#endif\n"
10326                "  int j;\n"
10327                "};\n",
10328                Style);
10329   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10330   EXPECT_EQ("struct foo {\n"
10331             "\n"
10332             "private:\n"
10333             "  void f() {}\n"
10334             "\n"
10335             "private:\n"
10336             "  int i;\n"
10337             "\n"
10338             "protected:\n"
10339             "  int j;\n"
10340             "};\n",
10341             format("struct foo {\n"
10342                    "\n"
10343                    "private:\n"
10344                    "  void f() {}\n"
10345                    "\n"
10346                    "private:\n"
10347                    "  int i;\n"
10348                    "\n"
10349                    "protected:\n"
10350                    "  int j;\n"
10351                    "};\n",
10352                    Style));
10353   verifyFormat("struct foo {\n"
10354                "private:\n"
10355                "  void f() {}\n"
10356                "private:\n"
10357                "  int i;\n"
10358                "protected:\n"
10359                "  int j;\n"
10360                "};\n",
10361                Style);
10362   EXPECT_EQ("struct foo { /* comment */\n"
10363             "\n"
10364             "private:\n"
10365             "  int i;\n"
10366             "  // comment\n"
10367             "\n"
10368             "private:\n"
10369             "  int j;\n"
10370             "};\n",
10371             format("struct foo { /* comment */\n"
10372                    "\n"
10373                    "private:\n"
10374                    "  int i;\n"
10375                    "  // comment\n"
10376                    "\n"
10377                    "private:\n"
10378                    "  int j;\n"
10379                    "};\n",
10380                    Style));
10381   verifyFormat("struct foo { /* comment */\n"
10382                "private:\n"
10383                "  int i;\n"
10384                "  // comment\n"
10385                "private:\n"
10386                "  int j;\n"
10387                "};\n",
10388                Style);
10389   EXPECT_EQ("struct foo {\n"
10390             "#ifdef FOO\n"
10391             "#endif\n"
10392             "\n"
10393             "private:\n"
10394             "  int i;\n"
10395             "#ifdef FOO\n"
10396             "\n"
10397             "private:\n"
10398             "#endif\n"
10399             "  int j;\n"
10400             "};\n",
10401             format("struct foo {\n"
10402                    "#ifdef FOO\n"
10403                    "#endif\n"
10404                    "\n"
10405                    "private:\n"
10406                    "  int i;\n"
10407                    "#ifdef FOO\n"
10408                    "\n"
10409                    "private:\n"
10410                    "#endif\n"
10411                    "  int j;\n"
10412                    "};\n",
10413                    Style));
10414   verifyFormat("struct foo {\n"
10415                "#ifdef FOO\n"
10416                "#endif\n"
10417                "private:\n"
10418                "  int i;\n"
10419                "#ifdef FOO\n"
10420                "private:\n"
10421                "#endif\n"
10422                "  int j;\n"
10423                "};\n",
10424                Style);
10425 
10426   FormatStyle NoEmptyLines = getLLVMStyle();
10427   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10428   verifyFormat("struct foo {\n"
10429                "private:\n"
10430                "  void f() {}\n"
10431                "\n"
10432                "private:\n"
10433                "  int i;\n"
10434                "\n"
10435                "public:\n"
10436                "protected:\n"
10437                "  int j;\n"
10438                "};\n",
10439                NoEmptyLines);
10440 
10441   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10442   verifyFormat("struct foo {\n"
10443                "private:\n"
10444                "  void f() {}\n"
10445                "private:\n"
10446                "  int i;\n"
10447                "public:\n"
10448                "protected:\n"
10449                "  int j;\n"
10450                "};\n",
10451                NoEmptyLines);
10452 
10453   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10454   verifyFormat("struct foo {\n"
10455                "private:\n"
10456                "  void f() {}\n"
10457                "\n"
10458                "private:\n"
10459                "  int i;\n"
10460                "\n"
10461                "public:\n"
10462                "\n"
10463                "protected:\n"
10464                "  int j;\n"
10465                "};\n",
10466                NoEmptyLines);
10467 }
10468 
10469 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10470 
10471   FormatStyle Style = getLLVMStyle();
10472   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10473   verifyFormat("struct foo {\n"
10474                "private:\n"
10475                "  void f() {}\n"
10476                "\n"
10477                "private:\n"
10478                "  int i;\n"
10479                "\n"
10480                "protected:\n"
10481                "  int j;\n"
10482                "};\n",
10483                Style);
10484 
10485   // Check if lines are removed.
10486   verifyFormat("struct foo {\n"
10487                "private:\n"
10488                "  void f() {}\n"
10489                "\n"
10490                "private:\n"
10491                "  int i;\n"
10492                "\n"
10493                "protected:\n"
10494                "  int j;\n"
10495                "};\n",
10496                "struct foo {\n"
10497                "private:\n"
10498                "\n"
10499                "  void f() {}\n"
10500                "\n"
10501                "private:\n"
10502                "\n"
10503                "  int i;\n"
10504                "\n"
10505                "protected:\n"
10506                "\n"
10507                "  int j;\n"
10508                "};\n",
10509                Style);
10510 
10511   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10512   verifyFormat("struct foo {\n"
10513                "private:\n"
10514                "\n"
10515                "  void f() {}\n"
10516                "\n"
10517                "private:\n"
10518                "\n"
10519                "  int i;\n"
10520                "\n"
10521                "protected:\n"
10522                "\n"
10523                "  int j;\n"
10524                "};\n",
10525                Style);
10526 
10527   // Check if lines are added.
10528   verifyFormat("struct foo {\n"
10529                "private:\n"
10530                "\n"
10531                "  void f() {}\n"
10532                "\n"
10533                "private:\n"
10534                "\n"
10535                "  int i;\n"
10536                "\n"
10537                "protected:\n"
10538                "\n"
10539                "  int j;\n"
10540                "};\n",
10541                "struct foo {\n"
10542                "private:\n"
10543                "  void f() {}\n"
10544                "\n"
10545                "private:\n"
10546                "  int i;\n"
10547                "\n"
10548                "protected:\n"
10549                "  int j;\n"
10550                "};\n",
10551                Style);
10552 
10553   // Leave tests rely on the code layout, test::messUp can not be used.
10554   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10555   Style.MaxEmptyLinesToKeep = 0u;
10556   verifyFormat("struct foo {\n"
10557                "private:\n"
10558                "  void f() {}\n"
10559                "\n"
10560                "private:\n"
10561                "  int i;\n"
10562                "\n"
10563                "protected:\n"
10564                "  int j;\n"
10565                "};\n",
10566                Style);
10567 
10568   // Check if MaxEmptyLinesToKeep is respected.
10569   EXPECT_EQ("struct foo {\n"
10570             "private:\n"
10571             "  void f() {}\n"
10572             "\n"
10573             "private:\n"
10574             "  int i;\n"
10575             "\n"
10576             "protected:\n"
10577             "  int j;\n"
10578             "};\n",
10579             format("struct foo {\n"
10580                    "private:\n"
10581                    "\n\n\n"
10582                    "  void f() {}\n"
10583                    "\n"
10584                    "private:\n"
10585                    "\n\n\n"
10586                    "  int i;\n"
10587                    "\n"
10588                    "protected:\n"
10589                    "\n\n\n"
10590                    "  int j;\n"
10591                    "};\n",
10592                    Style));
10593 
10594   Style.MaxEmptyLinesToKeep = 1u;
10595   EXPECT_EQ("struct foo {\n"
10596             "private:\n"
10597             "\n"
10598             "  void f() {}\n"
10599             "\n"
10600             "private:\n"
10601             "\n"
10602             "  int i;\n"
10603             "\n"
10604             "protected:\n"
10605             "\n"
10606             "  int j;\n"
10607             "};\n",
10608             format("struct foo {\n"
10609                    "private:\n"
10610                    "\n"
10611                    "  void f() {}\n"
10612                    "\n"
10613                    "private:\n"
10614                    "\n"
10615                    "  int i;\n"
10616                    "\n"
10617                    "protected:\n"
10618                    "\n"
10619                    "  int j;\n"
10620                    "};\n",
10621                    Style));
10622   // Check if no lines are kept.
10623   EXPECT_EQ("struct foo {\n"
10624             "private:\n"
10625             "  void f() {}\n"
10626             "\n"
10627             "private:\n"
10628             "  int i;\n"
10629             "\n"
10630             "protected:\n"
10631             "  int j;\n"
10632             "};\n",
10633             format("struct foo {\n"
10634                    "private:\n"
10635                    "  void f() {}\n"
10636                    "\n"
10637                    "private:\n"
10638                    "  int i;\n"
10639                    "\n"
10640                    "protected:\n"
10641                    "  int j;\n"
10642                    "};\n",
10643                    Style));
10644   // Check if MaxEmptyLinesToKeep is respected.
10645   EXPECT_EQ("struct foo {\n"
10646             "private:\n"
10647             "\n"
10648             "  void f() {}\n"
10649             "\n"
10650             "private:\n"
10651             "\n"
10652             "  int i;\n"
10653             "\n"
10654             "protected:\n"
10655             "\n"
10656             "  int j;\n"
10657             "};\n",
10658             format("struct foo {\n"
10659                    "private:\n"
10660                    "\n\n\n"
10661                    "  void f() {}\n"
10662                    "\n"
10663                    "private:\n"
10664                    "\n\n\n"
10665                    "  int i;\n"
10666                    "\n"
10667                    "protected:\n"
10668                    "\n\n\n"
10669                    "  int j;\n"
10670                    "};\n",
10671                    Style));
10672 
10673   Style.MaxEmptyLinesToKeep = 10u;
10674   EXPECT_EQ("struct foo {\n"
10675             "private:\n"
10676             "\n\n\n"
10677             "  void f() {}\n"
10678             "\n"
10679             "private:\n"
10680             "\n\n\n"
10681             "  int i;\n"
10682             "\n"
10683             "protected:\n"
10684             "\n\n\n"
10685             "  int j;\n"
10686             "};\n",
10687             format("struct foo {\n"
10688                    "private:\n"
10689                    "\n\n\n"
10690                    "  void f() {}\n"
10691                    "\n"
10692                    "private:\n"
10693                    "\n\n\n"
10694                    "  int i;\n"
10695                    "\n"
10696                    "protected:\n"
10697                    "\n\n\n"
10698                    "  int j;\n"
10699                    "};\n",
10700                    Style));
10701 
10702   // Test with comments.
10703   Style = getLLVMStyle();
10704   verifyFormat("struct foo {\n"
10705                "private:\n"
10706                "  // comment\n"
10707                "  void f() {}\n"
10708                "\n"
10709                "private: /* comment */\n"
10710                "  int i;\n"
10711                "};\n",
10712                Style);
10713   verifyFormat("struct foo {\n"
10714                "private:\n"
10715                "  // comment\n"
10716                "  void f() {}\n"
10717                "\n"
10718                "private: /* comment */\n"
10719                "  int i;\n"
10720                "};\n",
10721                "struct foo {\n"
10722                "private:\n"
10723                "\n"
10724                "  // comment\n"
10725                "  void f() {}\n"
10726                "\n"
10727                "private: /* comment */\n"
10728                "\n"
10729                "  int i;\n"
10730                "};\n",
10731                Style);
10732 
10733   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10734   verifyFormat("struct foo {\n"
10735                "private:\n"
10736                "\n"
10737                "  // comment\n"
10738                "  void f() {}\n"
10739                "\n"
10740                "private: /* comment */\n"
10741                "\n"
10742                "  int i;\n"
10743                "};\n",
10744                "struct foo {\n"
10745                "private:\n"
10746                "  // comment\n"
10747                "  void f() {}\n"
10748                "\n"
10749                "private: /* comment */\n"
10750                "  int i;\n"
10751                "};\n",
10752                Style);
10753   verifyFormat("struct foo {\n"
10754                "private:\n"
10755                "\n"
10756                "  // comment\n"
10757                "  void f() {}\n"
10758                "\n"
10759                "private: /* comment */\n"
10760                "\n"
10761                "  int i;\n"
10762                "};\n",
10763                Style);
10764 
10765   // Test with preprocessor defines.
10766   Style = getLLVMStyle();
10767   verifyFormat("struct foo {\n"
10768                "private:\n"
10769                "#ifdef FOO\n"
10770                "#endif\n"
10771                "  void f() {}\n"
10772                "};\n",
10773                Style);
10774   verifyFormat("struct foo {\n"
10775                "private:\n"
10776                "#ifdef FOO\n"
10777                "#endif\n"
10778                "  void f() {}\n"
10779                "};\n",
10780                "struct foo {\n"
10781                "private:\n"
10782                "\n"
10783                "#ifdef FOO\n"
10784                "#endif\n"
10785                "  void f() {}\n"
10786                "};\n",
10787                Style);
10788 
10789   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10790   verifyFormat("struct foo {\n"
10791                "private:\n"
10792                "\n"
10793                "#ifdef FOO\n"
10794                "#endif\n"
10795                "  void f() {}\n"
10796                "};\n",
10797                "struct foo {\n"
10798                "private:\n"
10799                "#ifdef FOO\n"
10800                "#endif\n"
10801                "  void f() {}\n"
10802                "};\n",
10803                Style);
10804   verifyFormat("struct foo {\n"
10805                "private:\n"
10806                "\n"
10807                "#ifdef FOO\n"
10808                "#endif\n"
10809                "  void f() {}\n"
10810                "};\n",
10811                Style);
10812 }
10813 
10814 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
10815   // Combined tests of EmptyLineAfterAccessModifier and
10816   // EmptyLineBeforeAccessModifier.
10817   FormatStyle Style = getLLVMStyle();
10818   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10819   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10820   verifyFormat("struct foo {\n"
10821                "private:\n"
10822                "\n"
10823                "protected:\n"
10824                "};\n",
10825                Style);
10826 
10827   Style.MaxEmptyLinesToKeep = 10u;
10828   // Both remove all new lines.
10829   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10830   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10831   verifyFormat("struct foo {\n"
10832                "private:\n"
10833                "protected:\n"
10834                "};\n",
10835                "struct foo {\n"
10836                "private:\n"
10837                "\n\n\n"
10838                "protected:\n"
10839                "};\n",
10840                Style);
10841 
10842   // Leave tests rely on the code layout, test::messUp can not be used.
10843   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10844   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10845   Style.MaxEmptyLinesToKeep = 10u;
10846   EXPECT_EQ("struct foo {\n"
10847             "private:\n"
10848             "\n\n\n"
10849             "protected:\n"
10850             "};\n",
10851             format("struct foo {\n"
10852                    "private:\n"
10853                    "\n\n\n"
10854                    "protected:\n"
10855                    "};\n",
10856                    Style));
10857   Style.MaxEmptyLinesToKeep = 3u;
10858   EXPECT_EQ("struct foo {\n"
10859             "private:\n"
10860             "\n\n\n"
10861             "protected:\n"
10862             "};\n",
10863             format("struct foo {\n"
10864                    "private:\n"
10865                    "\n\n\n"
10866                    "protected:\n"
10867                    "};\n",
10868                    Style));
10869   Style.MaxEmptyLinesToKeep = 1u;
10870   EXPECT_EQ("struct foo {\n"
10871             "private:\n"
10872             "\n\n\n"
10873             "protected:\n"
10874             "};\n",
10875             format("struct foo {\n"
10876                    "private:\n"
10877                    "\n\n\n"
10878                    "protected:\n"
10879                    "};\n",
10880                    Style)); // Based on new lines in original document and not
10881                             // on the setting.
10882 
10883   Style.MaxEmptyLinesToKeep = 10u;
10884   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10885   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10886   // Newlines are kept if they are greater than zero,
10887   // test::messUp removes all new lines which changes the logic
10888   EXPECT_EQ("struct foo {\n"
10889             "private:\n"
10890             "\n\n\n"
10891             "protected:\n"
10892             "};\n",
10893             format("struct foo {\n"
10894                    "private:\n"
10895                    "\n\n\n"
10896                    "protected:\n"
10897                    "};\n",
10898                    Style));
10899 
10900   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10901   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10902   // test::messUp removes all new lines which changes the logic
10903   EXPECT_EQ("struct foo {\n"
10904             "private:\n"
10905             "\n\n\n"
10906             "protected:\n"
10907             "};\n",
10908             format("struct foo {\n"
10909                    "private:\n"
10910                    "\n\n\n"
10911                    "protected:\n"
10912                    "};\n",
10913                    Style));
10914 
10915   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10916   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10917   EXPECT_EQ("struct foo {\n"
10918             "private:\n"
10919             "\n\n\n"
10920             "protected:\n"
10921             "};\n",
10922             format("struct foo {\n"
10923                    "private:\n"
10924                    "\n\n\n"
10925                    "protected:\n"
10926                    "};\n",
10927                    Style)); // test::messUp removes all new lines which changes
10928                             // the logic.
10929 
10930   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10931   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10932   verifyFormat("struct foo {\n"
10933                "private:\n"
10934                "protected:\n"
10935                "};\n",
10936                "struct foo {\n"
10937                "private:\n"
10938                "\n\n\n"
10939                "protected:\n"
10940                "};\n",
10941                Style);
10942 
10943   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10944   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10945   EXPECT_EQ("struct foo {\n"
10946             "private:\n"
10947             "\n\n\n"
10948             "protected:\n"
10949             "};\n",
10950             format("struct foo {\n"
10951                    "private:\n"
10952                    "\n\n\n"
10953                    "protected:\n"
10954                    "};\n",
10955                    Style)); // test::messUp removes all new lines which changes
10956                             // the logic.
10957 
10958   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10959   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10960   verifyFormat("struct foo {\n"
10961                "private:\n"
10962                "protected:\n"
10963                "};\n",
10964                "struct foo {\n"
10965                "private:\n"
10966                "\n\n\n"
10967                "protected:\n"
10968                "};\n",
10969                Style);
10970 
10971   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10972   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10973   verifyFormat("struct foo {\n"
10974                "private:\n"
10975                "protected:\n"
10976                "};\n",
10977                "struct foo {\n"
10978                "private:\n"
10979                "\n\n\n"
10980                "protected:\n"
10981                "};\n",
10982                Style);
10983 
10984   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10985   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10986   verifyFormat("struct foo {\n"
10987                "private:\n"
10988                "protected:\n"
10989                "};\n",
10990                "struct foo {\n"
10991                "private:\n"
10992                "\n\n\n"
10993                "protected:\n"
10994                "};\n",
10995                Style);
10996 
10997   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10998   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10999   verifyFormat("struct foo {\n"
11000                "private:\n"
11001                "protected:\n"
11002                "};\n",
11003                "struct foo {\n"
11004                "private:\n"
11005                "\n\n\n"
11006                "protected:\n"
11007                "};\n",
11008                Style);
11009 }
11010 
11011 TEST_F(FormatTest, FormatsArrays) {
11012   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11013                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11014   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11015                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11016   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11017                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11018   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11019                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11020   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11021                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11022   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11023                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11024                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11025   verifyFormat(
11026       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11027       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11028       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11029   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11030                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11031 
11032   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11033                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11034   verifyFormat(
11035       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11036       "                                  .aaaaaaa[0]\n"
11037       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11038   verifyFormat("a[::b::c];");
11039 
11040   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11041 
11042   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11043   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11044 }
11045 
11046 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11047   verifyFormat("(a)->b();");
11048   verifyFormat("--a;");
11049 }
11050 
11051 TEST_F(FormatTest, HandlesIncludeDirectives) {
11052   verifyFormat("#include <string>\n"
11053                "#include <a/b/c.h>\n"
11054                "#include \"a/b/string\"\n"
11055                "#include \"string.h\"\n"
11056                "#include \"string.h\"\n"
11057                "#include <a-a>\n"
11058                "#include < path with space >\n"
11059                "#include_next <test.h>"
11060                "#include \"abc.h\" // this is included for ABC\n"
11061                "#include \"some long include\" // with a comment\n"
11062                "#include \"some very long include path\"\n"
11063                "#include <some/very/long/include/path>\n",
11064                getLLVMStyleWithColumns(35));
11065   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11066   EXPECT_EQ("#include <a>", format("#include<a>"));
11067 
11068   verifyFormat("#import <string>");
11069   verifyFormat("#import <a/b/c.h>");
11070   verifyFormat("#import \"a/b/string\"");
11071   verifyFormat("#import \"string.h\"");
11072   verifyFormat("#import \"string.h\"");
11073   verifyFormat("#if __has_include(<strstream>)\n"
11074                "#include <strstream>\n"
11075                "#endif");
11076 
11077   verifyFormat("#define MY_IMPORT <a/b>");
11078 
11079   verifyFormat("#if __has_include(<a/b>)");
11080   verifyFormat("#if __has_include_next(<a/b>)");
11081   verifyFormat("#define F __has_include(<a/b>)");
11082   verifyFormat("#define F __has_include_next(<a/b>)");
11083 
11084   // Protocol buffer definition or missing "#".
11085   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11086                getLLVMStyleWithColumns(30));
11087 
11088   FormatStyle Style = getLLVMStyle();
11089   Style.AlwaysBreakBeforeMultilineStrings = true;
11090   Style.ColumnLimit = 0;
11091   verifyFormat("#import \"abc.h\"", Style);
11092 
11093   // But 'import' might also be a regular C++ namespace.
11094   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11095                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11096 }
11097 
11098 //===----------------------------------------------------------------------===//
11099 // Error recovery tests.
11100 //===----------------------------------------------------------------------===//
11101 
11102 TEST_F(FormatTest, IncompleteParameterLists) {
11103   FormatStyle NoBinPacking = getLLVMStyle();
11104   NoBinPacking.BinPackParameters = false;
11105   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11106                "                        double *min_x,\n"
11107                "                        double *max_x,\n"
11108                "                        double *min_y,\n"
11109                "                        double *max_y,\n"
11110                "                        double *min_z,\n"
11111                "                        double *max_z, ) {}",
11112                NoBinPacking);
11113 }
11114 
11115 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11116   verifyFormat("void f() { return; }\n42");
11117   verifyFormat("void f() {\n"
11118                "  if (0)\n"
11119                "    return;\n"
11120                "}\n"
11121                "42");
11122   verifyFormat("void f() { return }\n42");
11123   verifyFormat("void f() {\n"
11124                "  if (0)\n"
11125                "    return\n"
11126                "}\n"
11127                "42");
11128 }
11129 
11130 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11131   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11132   EXPECT_EQ("void f() {\n"
11133             "  if (a)\n"
11134             "    return\n"
11135             "}",
11136             format("void  f  (  )  {  if  ( a )  return  }"));
11137   EXPECT_EQ("namespace N {\n"
11138             "void f()\n"
11139             "}",
11140             format("namespace  N  {  void f()  }"));
11141   EXPECT_EQ("namespace N {\n"
11142             "void f() {}\n"
11143             "void g()\n"
11144             "} // namespace N",
11145             format("namespace N  { void f( ) { } void g( ) }"));
11146 }
11147 
11148 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11149   verifyFormat("int aaaaaaaa =\n"
11150                "    // Overlylongcomment\n"
11151                "    b;",
11152                getLLVMStyleWithColumns(20));
11153   verifyFormat("function(\n"
11154                "    ShortArgument,\n"
11155                "    LoooooooooooongArgument);\n",
11156                getLLVMStyleWithColumns(20));
11157 }
11158 
11159 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11160   verifyFormat("public:");
11161   verifyFormat("class A {\n"
11162                "public\n"
11163                "  void f() {}\n"
11164                "};");
11165   verifyFormat("public\n"
11166                "int qwerty;");
11167   verifyFormat("public\n"
11168                "B {}");
11169   verifyFormat("public\n"
11170                "{}");
11171   verifyFormat("public\n"
11172                "B { int x; }");
11173 }
11174 
11175 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11176   verifyFormat("{");
11177   verifyFormat("#})");
11178   verifyNoCrash("(/**/[:!] ?[).");
11179 }
11180 
11181 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11182   // Found by oss-fuzz:
11183   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11184   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11185   Style.ColumnLimit = 60;
11186   verifyNoCrash(
11187       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11188       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11189       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11190       Style);
11191 }
11192 
11193 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11194   verifyFormat("do {\n}");
11195   verifyFormat("do {\n}\n"
11196                "f();");
11197   verifyFormat("do {\n}\n"
11198                "wheeee(fun);");
11199   verifyFormat("do {\n"
11200                "  f();\n"
11201                "}");
11202 }
11203 
11204 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11205   verifyFormat("if {\n  foo;\n  foo();\n}");
11206   verifyFormat("switch {\n  foo;\n  foo();\n}");
11207   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11208   verifyFormat("while {\n  foo;\n  foo();\n}");
11209   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11210 }
11211 
11212 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11213   verifyIncompleteFormat("namespace {\n"
11214                          "class Foo { Foo (\n"
11215                          "};\n"
11216                          "} // namespace");
11217 }
11218 
11219 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11220   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11221   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11222   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11223   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11224 
11225   EXPECT_EQ("{\n"
11226             "  {\n"
11227             "    breakme(\n"
11228             "        qwe);\n"
11229             "  }\n",
11230             format("{\n"
11231                    "    {\n"
11232                    " breakme(qwe);\n"
11233                    "}\n",
11234                    getLLVMStyleWithColumns(10)));
11235 }
11236 
11237 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11238   verifyFormat("int x = {\n"
11239                "    avariable,\n"
11240                "    b(alongervariable)};",
11241                getLLVMStyleWithColumns(25));
11242 }
11243 
11244 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11245   verifyFormat("return (a)(b){1, 2, 3};");
11246 }
11247 
11248 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11249   verifyFormat("vector<int> x{1, 2, 3, 4};");
11250   verifyFormat("vector<int> x{\n"
11251                "    1,\n"
11252                "    2,\n"
11253                "    3,\n"
11254                "    4,\n"
11255                "};");
11256   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11257   verifyFormat("f({1, 2});");
11258   verifyFormat("auto v = Foo{-1};");
11259   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11260   verifyFormat("Class::Class : member{1, 2, 3} {}");
11261   verifyFormat("new vector<int>{1, 2, 3};");
11262   verifyFormat("new int[3]{1, 2, 3};");
11263   verifyFormat("new int{1};");
11264   verifyFormat("return {arg1, arg2};");
11265   verifyFormat("return {arg1, SomeType{parameter}};");
11266   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11267   verifyFormat("new T{arg1, arg2};");
11268   verifyFormat("f(MyMap[{composite, key}]);");
11269   verifyFormat("class Class {\n"
11270                "  T member = {arg1, arg2};\n"
11271                "};");
11272   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11273   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11274   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11275   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11276   verifyFormat("int a = std::is_integral<int>{} + 0;");
11277 
11278   verifyFormat("int foo(int i) { return fo1{}(i); }");
11279   verifyFormat("int foo(int i) { return fo1{}(i); }");
11280   verifyFormat("auto i = decltype(x){};");
11281   verifyFormat("auto i = typeof(x){};");
11282   verifyFormat("auto i = _Atomic(x){};");
11283   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11284   verifyFormat("Node n{1, Node{1000}, //\n"
11285                "       2};");
11286   verifyFormat("Aaaa aaaaaaa{\n"
11287                "    {\n"
11288                "        aaaa,\n"
11289                "    },\n"
11290                "};");
11291   verifyFormat("class C : public D {\n"
11292                "  SomeClass SC{2};\n"
11293                "};");
11294   verifyFormat("class C : public A {\n"
11295                "  class D : public B {\n"
11296                "    void f() { int i{2}; }\n"
11297                "  };\n"
11298                "};");
11299   verifyFormat("#define A {a, a},");
11300 
11301   // Avoid breaking between equal sign and opening brace
11302   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11303   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11304   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11305                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11306                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11307                "     {\"ccccccccccccccccccccc\", 2}};",
11308                AvoidBreakingFirstArgument);
11309 
11310   // Binpacking only if there is no trailing comma
11311   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11312                "                      cccccccccc, dddddddddd};",
11313                getLLVMStyleWithColumns(50));
11314   verifyFormat("const Aaaaaa aaaaa = {\n"
11315                "    aaaaaaaaaaa,\n"
11316                "    bbbbbbbbbbb,\n"
11317                "    ccccccccccc,\n"
11318                "    ddddddddddd,\n"
11319                "};",
11320                getLLVMStyleWithColumns(50));
11321 
11322   // Cases where distinguising braced lists and blocks is hard.
11323   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11324   verifyFormat("void f() {\n"
11325                "  return; // comment\n"
11326                "}\n"
11327                "SomeType t;");
11328   verifyFormat("void f() {\n"
11329                "  if (a) {\n"
11330                "    f();\n"
11331                "  }\n"
11332                "}\n"
11333                "SomeType t;");
11334 
11335   // In combination with BinPackArguments = false.
11336   FormatStyle NoBinPacking = getLLVMStyle();
11337   NoBinPacking.BinPackArguments = false;
11338   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11339                "                      bbbbb,\n"
11340                "                      ccccc,\n"
11341                "                      ddddd,\n"
11342                "                      eeeee,\n"
11343                "                      ffffff,\n"
11344                "                      ggggg,\n"
11345                "                      hhhhhh,\n"
11346                "                      iiiiii,\n"
11347                "                      jjjjjj,\n"
11348                "                      kkkkkk};",
11349                NoBinPacking);
11350   verifyFormat("const Aaaaaa aaaaa = {\n"
11351                "    aaaaa,\n"
11352                "    bbbbb,\n"
11353                "    ccccc,\n"
11354                "    ddddd,\n"
11355                "    eeeee,\n"
11356                "    ffffff,\n"
11357                "    ggggg,\n"
11358                "    hhhhhh,\n"
11359                "    iiiiii,\n"
11360                "    jjjjjj,\n"
11361                "    kkkkkk,\n"
11362                "};",
11363                NoBinPacking);
11364   verifyFormat(
11365       "const Aaaaaa aaaaa = {\n"
11366       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11367       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11368       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11369       "};",
11370       NoBinPacking);
11371 
11372   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11373   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11374             "    CDDDP83848_BMCR_REGISTER,\n"
11375             "    CDDDP83848_BMSR_REGISTER,\n"
11376             "    CDDDP83848_RBR_REGISTER};",
11377             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11378                    "                                CDDDP83848_BMSR_REGISTER,\n"
11379                    "                                CDDDP83848_RBR_REGISTER};",
11380                    NoBinPacking));
11381 
11382   // FIXME: The alignment of these trailing comments might be bad. Then again,
11383   // this might be utterly useless in real code.
11384   verifyFormat("Constructor::Constructor()\n"
11385                "    : some_value{         //\n"
11386                "                 aaaaaaa, //\n"
11387                "                 bbbbbbb} {}");
11388 
11389   // In braced lists, the first comment is always assumed to belong to the
11390   // first element. Thus, it can be moved to the next or previous line as
11391   // appropriate.
11392   EXPECT_EQ("function({// First element:\n"
11393             "          1,\n"
11394             "          // Second element:\n"
11395             "          2});",
11396             format("function({\n"
11397                    "    // First element:\n"
11398                    "    1,\n"
11399                    "    // Second element:\n"
11400                    "    2});"));
11401   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11402             "    // First element:\n"
11403             "    1,\n"
11404             "    // Second element:\n"
11405             "    2};",
11406             format("std::vector<int> MyNumbers{// First element:\n"
11407                    "                           1,\n"
11408                    "                           // Second element:\n"
11409                    "                           2};",
11410                    getLLVMStyleWithColumns(30)));
11411   // A trailing comma should still lead to an enforced line break and no
11412   // binpacking.
11413   EXPECT_EQ("vector<int> SomeVector = {\n"
11414             "    // aaa\n"
11415             "    1,\n"
11416             "    2,\n"
11417             "};",
11418             format("vector<int> SomeVector = { // aaa\n"
11419                    "    1, 2, };"));
11420 
11421   // C++11 brace initializer list l-braces should not be treated any differently
11422   // when breaking before lambda bodies is enabled
11423   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11424   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11425   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11426   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11427   verifyFormat(
11428       "std::runtime_error{\n"
11429       "    \"Long string which will force a break onto the next line...\"};",
11430       BreakBeforeLambdaBody);
11431 
11432   FormatStyle ExtraSpaces = getLLVMStyle();
11433   ExtraSpaces.Cpp11BracedListStyle = false;
11434   ExtraSpaces.ColumnLimit = 75;
11435   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11436   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11437   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11438   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11439   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11440   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11441   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11442   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11443   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11444   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11445   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11446   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11447   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11448   verifyFormat("class Class {\n"
11449                "  T member = { arg1, arg2 };\n"
11450                "};",
11451                ExtraSpaces);
11452   verifyFormat(
11453       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11454       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11455       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11456       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11457       ExtraSpaces);
11458   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11459   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11460                ExtraSpaces);
11461   verifyFormat(
11462       "someFunction(OtherParam,\n"
11463       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11464       "                         param1, param2,\n"
11465       "                         // comment 2\n"
11466       "                         param3, param4 });",
11467       ExtraSpaces);
11468   verifyFormat(
11469       "std::this_thread::sleep_for(\n"
11470       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11471       ExtraSpaces);
11472   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11473                "    aaaaaaa,\n"
11474                "    aaaaaaaaaa,\n"
11475                "    aaaaa,\n"
11476                "    aaaaaaaaaaaaaaa,\n"
11477                "    aaa,\n"
11478                "    aaaaaaaaaa,\n"
11479                "    a,\n"
11480                "    aaaaaaaaaaaaaaaaaaaaa,\n"
11481                "    aaaaaaaaaaaa,\n"
11482                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
11483                "    aaaaaaa,\n"
11484                "    a};");
11485   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
11486   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
11487   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
11488 
11489   // Avoid breaking between initializer/equal sign and opening brace
11490   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
11491   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
11492                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11493                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11494                "  { \"ccccccccccccccccccccc\", 2 }\n"
11495                "};",
11496                ExtraSpaces);
11497   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
11498                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11499                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11500                "  { \"ccccccccccccccccccccc\", 2 }\n"
11501                "};",
11502                ExtraSpaces);
11503 
11504   FormatStyle SpaceBeforeBrace = getLLVMStyle();
11505   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
11506   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
11507   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
11508 
11509   FormatStyle SpaceBetweenBraces = getLLVMStyle();
11510   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
11511   SpaceBetweenBraces.SpacesInParentheses = true;
11512   SpaceBetweenBraces.SpacesInSquareBrackets = true;
11513   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
11514   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
11515   verifyFormat("vector< int > x{ // comment 1\n"
11516                "                 1, 2, 3, 4 };",
11517                SpaceBetweenBraces);
11518   SpaceBetweenBraces.ColumnLimit = 20;
11519   EXPECT_EQ("vector< int > x{\n"
11520             "    1, 2, 3, 4 };",
11521             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11522   SpaceBetweenBraces.ColumnLimit = 24;
11523   EXPECT_EQ("vector< int > x{ 1, 2,\n"
11524             "                 3, 4 };",
11525             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11526   EXPECT_EQ("vector< int > x{\n"
11527             "    1,\n"
11528             "    2,\n"
11529             "    3,\n"
11530             "    4,\n"
11531             "};",
11532             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
11533   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
11534   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
11535   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
11536 }
11537 
11538 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
11539   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11540                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11541                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11542                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11543                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11544                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11545   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
11546                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11547                "                 1, 22, 333, 4444, 55555, //\n"
11548                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11549                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11550   verifyFormat(
11551       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11552       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11553       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
11554       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11555       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11556       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11557       "                 7777777};");
11558   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11559                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11560                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11561   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11562                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11563                "    // Separating comment.\n"
11564                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
11565   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11566                "    // Leading comment\n"
11567                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11568                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11569   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11570                "                 1, 1, 1, 1};",
11571                getLLVMStyleWithColumns(39));
11572   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11573                "                 1, 1, 1, 1};",
11574                getLLVMStyleWithColumns(38));
11575   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
11576                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
11577                getLLVMStyleWithColumns(43));
11578   verifyFormat(
11579       "static unsigned SomeValues[10][3] = {\n"
11580       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
11581       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
11582   verifyFormat("static auto fields = new vector<string>{\n"
11583                "    \"aaaaaaaaaaaaa\",\n"
11584                "    \"aaaaaaaaaaaaa\",\n"
11585                "    \"aaaaaaaaaaaa\",\n"
11586                "    \"aaaaaaaaaaaaaa\",\n"
11587                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11588                "    \"aaaaaaaaaaaa\",\n"
11589                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11590                "};");
11591   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
11592   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
11593                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
11594                "                 3, cccccccccccccccccccccc};",
11595                getLLVMStyleWithColumns(60));
11596 
11597   // Trailing commas.
11598   verifyFormat("vector<int> x = {\n"
11599                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
11600                "};",
11601                getLLVMStyleWithColumns(39));
11602   verifyFormat("vector<int> x = {\n"
11603                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
11604                "};",
11605                getLLVMStyleWithColumns(39));
11606   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11607                "                 1, 1, 1, 1,\n"
11608                "                 /**/ /**/};",
11609                getLLVMStyleWithColumns(39));
11610 
11611   // Trailing comment in the first line.
11612   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
11613                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
11614                "    111111111,  222222222,  3333333333,  444444444,  //\n"
11615                "    11111111,   22222222,   333333333,   44444444};");
11616   // Trailing comment in the last line.
11617   verifyFormat("int aaaaa[] = {\n"
11618                "    1, 2, 3, // comment\n"
11619                "    4, 5, 6  // comment\n"
11620                "};");
11621 
11622   // With nested lists, we should either format one item per line or all nested
11623   // lists one on line.
11624   // FIXME: For some nested lists, we can do better.
11625   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
11626                "        {aaaaaaaaaaaaaaaaaaa},\n"
11627                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
11628                "        {aaaaaaaaaaaaaaaaa}};",
11629                getLLVMStyleWithColumns(60));
11630   verifyFormat(
11631       "SomeStruct my_struct_array = {\n"
11632       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
11633       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
11634       "    {aaa, aaa},\n"
11635       "    {aaa, aaa},\n"
11636       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
11637       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
11638       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
11639 
11640   // No column layout should be used here.
11641   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
11642                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
11643 
11644   verifyNoCrash("a<,");
11645 
11646   // No braced initializer here.
11647   verifyFormat("void f() {\n"
11648                "  struct Dummy {};\n"
11649                "  f(v);\n"
11650                "}");
11651 
11652   // Long lists should be formatted in columns even if they are nested.
11653   verifyFormat(
11654       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11655       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11656       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11657       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11658       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11659       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
11660 
11661   // Allow "single-column" layout even if that violates the column limit. There
11662   // isn't going to be a better way.
11663   verifyFormat("std::vector<int> a = {\n"
11664                "    aaaaaaaa,\n"
11665                "    aaaaaaaa,\n"
11666                "    aaaaaaaa,\n"
11667                "    aaaaaaaa,\n"
11668                "    aaaaaaaaaa,\n"
11669                "    aaaaaaaa,\n"
11670                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
11671                getLLVMStyleWithColumns(30));
11672   verifyFormat("vector<int> aaaa = {\n"
11673                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11674                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11675                "    aaaaaa.aaaaaaa,\n"
11676                "    aaaaaa.aaaaaaa,\n"
11677                "    aaaaaa.aaaaaaa,\n"
11678                "    aaaaaa.aaaaaaa,\n"
11679                "};");
11680 
11681   // Don't create hanging lists.
11682   verifyFormat("someFunction(Param, {List1, List2,\n"
11683                "                     List3});",
11684                getLLVMStyleWithColumns(35));
11685   verifyFormat("someFunction(Param, Param,\n"
11686                "             {List1, List2,\n"
11687                "              List3});",
11688                getLLVMStyleWithColumns(35));
11689   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
11690                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
11691 }
11692 
11693 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
11694   FormatStyle DoNotMerge = getLLVMStyle();
11695   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11696 
11697   verifyFormat("void f() { return 42; }");
11698   verifyFormat("void f() {\n"
11699                "  return 42;\n"
11700                "}",
11701                DoNotMerge);
11702   verifyFormat("void f() {\n"
11703                "  // Comment\n"
11704                "}");
11705   verifyFormat("{\n"
11706                "#error {\n"
11707                "  int a;\n"
11708                "}");
11709   verifyFormat("{\n"
11710                "  int a;\n"
11711                "#error {\n"
11712                "}");
11713   verifyFormat("void f() {} // comment");
11714   verifyFormat("void f() { int a; } // comment");
11715   verifyFormat("void f() {\n"
11716                "} // comment",
11717                DoNotMerge);
11718   verifyFormat("void f() {\n"
11719                "  int a;\n"
11720                "} // comment",
11721                DoNotMerge);
11722   verifyFormat("void f() {\n"
11723                "} // comment",
11724                getLLVMStyleWithColumns(15));
11725 
11726   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
11727   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
11728 
11729   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
11730   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
11731   verifyFormat("class C {\n"
11732                "  C()\n"
11733                "      : iiiiiiii(nullptr),\n"
11734                "        kkkkkkk(nullptr),\n"
11735                "        mmmmmmm(nullptr),\n"
11736                "        nnnnnnn(nullptr) {}\n"
11737                "};",
11738                getGoogleStyle());
11739 
11740   FormatStyle NoColumnLimit = getLLVMStyle();
11741   NoColumnLimit.ColumnLimit = 0;
11742   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
11743   EXPECT_EQ("class C {\n"
11744             "  A() : b(0) {}\n"
11745             "};",
11746             format("class C{A():b(0){}};", NoColumnLimit));
11747   EXPECT_EQ("A()\n"
11748             "    : b(0) {\n"
11749             "}",
11750             format("A()\n:b(0)\n{\n}", NoColumnLimit));
11751 
11752   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
11753   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
11754       FormatStyle::SFS_None;
11755   EXPECT_EQ("A()\n"
11756             "    : b(0) {\n"
11757             "}",
11758             format("A():b(0){}", DoNotMergeNoColumnLimit));
11759   EXPECT_EQ("A()\n"
11760             "    : b(0) {\n"
11761             "}",
11762             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
11763 
11764   verifyFormat("#define A          \\\n"
11765                "  void f() {       \\\n"
11766                "    int i;         \\\n"
11767                "  }",
11768                getLLVMStyleWithColumns(20));
11769   verifyFormat("#define A           \\\n"
11770                "  void f() { int i; }",
11771                getLLVMStyleWithColumns(21));
11772   verifyFormat("#define A            \\\n"
11773                "  void f() {         \\\n"
11774                "    int i;           \\\n"
11775                "  }                  \\\n"
11776                "  int j;",
11777                getLLVMStyleWithColumns(22));
11778   verifyFormat("#define A             \\\n"
11779                "  void f() { int i; } \\\n"
11780                "  int j;",
11781                getLLVMStyleWithColumns(23));
11782 }
11783 
11784 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
11785   FormatStyle MergeEmptyOnly = getLLVMStyle();
11786   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11787   verifyFormat("class C {\n"
11788                "  int f() {}\n"
11789                "};",
11790                MergeEmptyOnly);
11791   verifyFormat("class C {\n"
11792                "  int f() {\n"
11793                "    return 42;\n"
11794                "  }\n"
11795                "};",
11796                MergeEmptyOnly);
11797   verifyFormat("int f() {}", MergeEmptyOnly);
11798   verifyFormat("int f() {\n"
11799                "  return 42;\n"
11800                "}",
11801                MergeEmptyOnly);
11802 
11803   // Also verify behavior when BraceWrapping.AfterFunction = true
11804   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11805   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
11806   verifyFormat("int f() {}", MergeEmptyOnly);
11807   verifyFormat("class C {\n"
11808                "  int f() {}\n"
11809                "};",
11810                MergeEmptyOnly);
11811 }
11812 
11813 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
11814   FormatStyle MergeInlineOnly = getLLVMStyle();
11815   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11816   verifyFormat("class C {\n"
11817                "  int f() { return 42; }\n"
11818                "};",
11819                MergeInlineOnly);
11820   verifyFormat("int f() {\n"
11821                "  return 42;\n"
11822                "}",
11823                MergeInlineOnly);
11824 
11825   // SFS_Inline implies SFS_Empty
11826   verifyFormat("class C {\n"
11827                "  int f() {}\n"
11828                "};",
11829                MergeInlineOnly);
11830   verifyFormat("int f() {}", MergeInlineOnly);
11831 
11832   // Also verify behavior when BraceWrapping.AfterFunction = true
11833   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11834   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11835   verifyFormat("class C {\n"
11836                "  int f() { return 42; }\n"
11837                "};",
11838                MergeInlineOnly);
11839   verifyFormat("int f()\n"
11840                "{\n"
11841                "  return 42;\n"
11842                "}",
11843                MergeInlineOnly);
11844 
11845   // SFS_Inline implies SFS_Empty
11846   verifyFormat("int f() {}", MergeInlineOnly);
11847   verifyFormat("class C {\n"
11848                "  int f() {}\n"
11849                "};",
11850                MergeInlineOnly);
11851 }
11852 
11853 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
11854   FormatStyle MergeInlineOnly = getLLVMStyle();
11855   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
11856       FormatStyle::SFS_InlineOnly;
11857   verifyFormat("class C {\n"
11858                "  int f() { return 42; }\n"
11859                "};",
11860                MergeInlineOnly);
11861   verifyFormat("int f() {\n"
11862                "  return 42;\n"
11863                "}",
11864                MergeInlineOnly);
11865 
11866   // SFS_InlineOnly does not imply SFS_Empty
11867   verifyFormat("class C {\n"
11868                "  int f() {}\n"
11869                "};",
11870                MergeInlineOnly);
11871   verifyFormat("int f() {\n"
11872                "}",
11873                MergeInlineOnly);
11874 
11875   // Also verify behavior when BraceWrapping.AfterFunction = true
11876   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11877   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11878   verifyFormat("class C {\n"
11879                "  int f() { return 42; }\n"
11880                "};",
11881                MergeInlineOnly);
11882   verifyFormat("int f()\n"
11883                "{\n"
11884                "  return 42;\n"
11885                "}",
11886                MergeInlineOnly);
11887 
11888   // SFS_InlineOnly does not imply SFS_Empty
11889   verifyFormat("int f()\n"
11890                "{\n"
11891                "}",
11892                MergeInlineOnly);
11893   verifyFormat("class C {\n"
11894                "  int f() {}\n"
11895                "};",
11896                MergeInlineOnly);
11897 }
11898 
11899 TEST_F(FormatTest, SplitEmptyFunction) {
11900   FormatStyle Style = getLLVMStyle();
11901   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11902   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11903   Style.BraceWrapping.AfterFunction = true;
11904   Style.BraceWrapping.SplitEmptyFunction = false;
11905   Style.ColumnLimit = 40;
11906 
11907   verifyFormat("int f()\n"
11908                "{}",
11909                Style);
11910   verifyFormat("int f()\n"
11911                "{\n"
11912                "  return 42;\n"
11913                "}",
11914                Style);
11915   verifyFormat("int f()\n"
11916                "{\n"
11917                "  // some comment\n"
11918                "}",
11919                Style);
11920 
11921   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11922   verifyFormat("int f() {}", Style);
11923   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11924                "{}",
11925                Style);
11926   verifyFormat("int f()\n"
11927                "{\n"
11928                "  return 0;\n"
11929                "}",
11930                Style);
11931 
11932   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11933   verifyFormat("class Foo {\n"
11934                "  int f() {}\n"
11935                "};\n",
11936                Style);
11937   verifyFormat("class Foo {\n"
11938                "  int f() { return 0; }\n"
11939                "};\n",
11940                Style);
11941   verifyFormat("class Foo {\n"
11942                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11943                "  {}\n"
11944                "};\n",
11945                Style);
11946   verifyFormat("class Foo {\n"
11947                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11948                "  {\n"
11949                "    return 0;\n"
11950                "  }\n"
11951                "};\n",
11952                Style);
11953 
11954   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11955   verifyFormat("int f() {}", Style);
11956   verifyFormat("int f() { return 0; }", Style);
11957   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11958                "{}",
11959                Style);
11960   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11961                "{\n"
11962                "  return 0;\n"
11963                "}",
11964                Style);
11965 }
11966 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
11967   FormatStyle Style = getLLVMStyle();
11968   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11969   verifyFormat("#ifdef A\n"
11970                "int f() {}\n"
11971                "#else\n"
11972                "int g() {}\n"
11973                "#endif",
11974                Style);
11975 }
11976 
11977 TEST_F(FormatTest, SplitEmptyClass) {
11978   FormatStyle Style = getLLVMStyle();
11979   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11980   Style.BraceWrapping.AfterClass = true;
11981   Style.BraceWrapping.SplitEmptyRecord = false;
11982 
11983   verifyFormat("class Foo\n"
11984                "{};",
11985                Style);
11986   verifyFormat("/* something */ class Foo\n"
11987                "{};",
11988                Style);
11989   verifyFormat("template <typename X> class Foo\n"
11990                "{};",
11991                Style);
11992   verifyFormat("class Foo\n"
11993                "{\n"
11994                "  Foo();\n"
11995                "};",
11996                Style);
11997   verifyFormat("typedef class Foo\n"
11998                "{\n"
11999                "} Foo_t;",
12000                Style);
12001 
12002   Style.BraceWrapping.SplitEmptyRecord = true;
12003   Style.BraceWrapping.AfterStruct = true;
12004   verifyFormat("class rep\n"
12005                "{\n"
12006                "};",
12007                Style);
12008   verifyFormat("struct rep\n"
12009                "{\n"
12010                "};",
12011                Style);
12012   verifyFormat("template <typename T> class rep\n"
12013                "{\n"
12014                "};",
12015                Style);
12016   verifyFormat("template <typename T> struct rep\n"
12017                "{\n"
12018                "};",
12019                Style);
12020   verifyFormat("class rep\n"
12021                "{\n"
12022                "  int x;\n"
12023                "};",
12024                Style);
12025   verifyFormat("struct rep\n"
12026                "{\n"
12027                "  int x;\n"
12028                "};",
12029                Style);
12030   verifyFormat("template <typename T> class rep\n"
12031                "{\n"
12032                "  int x;\n"
12033                "};",
12034                Style);
12035   verifyFormat("template <typename T> struct rep\n"
12036                "{\n"
12037                "  int x;\n"
12038                "};",
12039                Style);
12040   verifyFormat("template <typename T> class rep // Foo\n"
12041                "{\n"
12042                "  int x;\n"
12043                "};",
12044                Style);
12045   verifyFormat("template <typename T> struct rep // Bar\n"
12046                "{\n"
12047                "  int x;\n"
12048                "};",
12049                Style);
12050 
12051   verifyFormat("template <typename T> class rep<T>\n"
12052                "{\n"
12053                "  int x;\n"
12054                "};",
12055                Style);
12056 
12057   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12058                "{\n"
12059                "  int x;\n"
12060                "};",
12061                Style);
12062   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12063                "{\n"
12064                "};",
12065                Style);
12066 
12067   verifyFormat("#include \"stdint.h\"\n"
12068                "namespace rep {}",
12069                Style);
12070   verifyFormat("#include <stdint.h>\n"
12071                "namespace rep {}",
12072                Style);
12073   verifyFormat("#include <stdint.h>\n"
12074                "namespace rep {}",
12075                "#include <stdint.h>\n"
12076                "namespace rep {\n"
12077                "\n"
12078                "\n"
12079                "}",
12080                Style);
12081 }
12082 
12083 TEST_F(FormatTest, SplitEmptyStruct) {
12084   FormatStyle Style = getLLVMStyle();
12085   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12086   Style.BraceWrapping.AfterStruct = true;
12087   Style.BraceWrapping.SplitEmptyRecord = false;
12088 
12089   verifyFormat("struct Foo\n"
12090                "{};",
12091                Style);
12092   verifyFormat("/* something */ struct Foo\n"
12093                "{};",
12094                Style);
12095   verifyFormat("template <typename X> struct Foo\n"
12096                "{};",
12097                Style);
12098   verifyFormat("struct Foo\n"
12099                "{\n"
12100                "  Foo();\n"
12101                "};",
12102                Style);
12103   verifyFormat("typedef struct Foo\n"
12104                "{\n"
12105                "} Foo_t;",
12106                Style);
12107   // typedef struct Bar {} Bar_t;
12108 }
12109 
12110 TEST_F(FormatTest, SplitEmptyUnion) {
12111   FormatStyle Style = getLLVMStyle();
12112   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12113   Style.BraceWrapping.AfterUnion = true;
12114   Style.BraceWrapping.SplitEmptyRecord = false;
12115 
12116   verifyFormat("union Foo\n"
12117                "{};",
12118                Style);
12119   verifyFormat("/* something */ union Foo\n"
12120                "{};",
12121                Style);
12122   verifyFormat("union Foo\n"
12123                "{\n"
12124                "  A,\n"
12125                "};",
12126                Style);
12127   verifyFormat("typedef union Foo\n"
12128                "{\n"
12129                "} Foo_t;",
12130                Style);
12131 }
12132 
12133 TEST_F(FormatTest, SplitEmptyNamespace) {
12134   FormatStyle Style = getLLVMStyle();
12135   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12136   Style.BraceWrapping.AfterNamespace = true;
12137   Style.BraceWrapping.SplitEmptyNamespace = false;
12138 
12139   verifyFormat("namespace Foo\n"
12140                "{};",
12141                Style);
12142   verifyFormat("/* something */ namespace Foo\n"
12143                "{};",
12144                Style);
12145   verifyFormat("inline namespace Foo\n"
12146                "{};",
12147                Style);
12148   verifyFormat("/* something */ inline namespace Foo\n"
12149                "{};",
12150                Style);
12151   verifyFormat("export namespace Foo\n"
12152                "{};",
12153                Style);
12154   verifyFormat("namespace Foo\n"
12155                "{\n"
12156                "void Bar();\n"
12157                "};",
12158                Style);
12159 }
12160 
12161 TEST_F(FormatTest, NeverMergeShortRecords) {
12162   FormatStyle Style = getLLVMStyle();
12163 
12164   verifyFormat("class Foo {\n"
12165                "  Foo();\n"
12166                "};",
12167                Style);
12168   verifyFormat("typedef class Foo {\n"
12169                "  Foo();\n"
12170                "} Foo_t;",
12171                Style);
12172   verifyFormat("struct Foo {\n"
12173                "  Foo();\n"
12174                "};",
12175                Style);
12176   verifyFormat("typedef struct Foo {\n"
12177                "  Foo();\n"
12178                "} Foo_t;",
12179                Style);
12180   verifyFormat("union Foo {\n"
12181                "  A,\n"
12182                "};",
12183                Style);
12184   verifyFormat("typedef union Foo {\n"
12185                "  A,\n"
12186                "} Foo_t;",
12187                Style);
12188   verifyFormat("namespace Foo {\n"
12189                "void Bar();\n"
12190                "};",
12191                Style);
12192 
12193   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12194   Style.BraceWrapping.AfterClass = true;
12195   Style.BraceWrapping.AfterStruct = true;
12196   Style.BraceWrapping.AfterUnion = true;
12197   Style.BraceWrapping.AfterNamespace = true;
12198   verifyFormat("class Foo\n"
12199                "{\n"
12200                "  Foo();\n"
12201                "};",
12202                Style);
12203   verifyFormat("typedef class Foo\n"
12204                "{\n"
12205                "  Foo();\n"
12206                "} Foo_t;",
12207                Style);
12208   verifyFormat("struct Foo\n"
12209                "{\n"
12210                "  Foo();\n"
12211                "};",
12212                Style);
12213   verifyFormat("typedef struct Foo\n"
12214                "{\n"
12215                "  Foo();\n"
12216                "} Foo_t;",
12217                Style);
12218   verifyFormat("union Foo\n"
12219                "{\n"
12220                "  A,\n"
12221                "};",
12222                Style);
12223   verifyFormat("typedef union Foo\n"
12224                "{\n"
12225                "  A,\n"
12226                "} Foo_t;",
12227                Style);
12228   verifyFormat("namespace Foo\n"
12229                "{\n"
12230                "void Bar();\n"
12231                "};",
12232                Style);
12233 }
12234 
12235 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12236   // Elaborate type variable declarations.
12237   verifyFormat("struct foo a = {bar};\nint n;");
12238   verifyFormat("class foo a = {bar};\nint n;");
12239   verifyFormat("union foo a = {bar};\nint n;");
12240 
12241   // Elaborate types inside function definitions.
12242   verifyFormat("struct foo f() {}\nint n;");
12243   verifyFormat("class foo f() {}\nint n;");
12244   verifyFormat("union foo f() {}\nint n;");
12245 
12246   // Templates.
12247   verifyFormat("template <class X> void f() {}\nint n;");
12248   verifyFormat("template <struct X> void f() {}\nint n;");
12249   verifyFormat("template <union X> void f() {}\nint n;");
12250 
12251   // Actual definitions...
12252   verifyFormat("struct {\n} n;");
12253   verifyFormat(
12254       "template <template <class T, class Y>, class Z> class X {\n} n;");
12255   verifyFormat("union Z {\n  int n;\n} x;");
12256   verifyFormat("class MACRO Z {\n} n;");
12257   verifyFormat("class MACRO(X) Z {\n} n;");
12258   verifyFormat("class __attribute__(X) Z {\n} n;");
12259   verifyFormat("class __declspec(X) Z {\n} n;");
12260   verifyFormat("class A##B##C {\n} n;");
12261   verifyFormat("class alignas(16) Z {\n} n;");
12262   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12263   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12264 
12265   // Redefinition from nested context:
12266   verifyFormat("class A::B::C {\n} n;");
12267 
12268   // Template definitions.
12269   verifyFormat(
12270       "template <typename F>\n"
12271       "Matcher(const Matcher<F> &Other,\n"
12272       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12273       "                             !is_same<F, T>::value>::type * = 0)\n"
12274       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12275 
12276   // FIXME: This is still incorrectly handled at the formatter side.
12277   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12278   verifyFormat("int i = SomeFunction(a<b, a> b);");
12279 
12280   // FIXME:
12281   // This now gets parsed incorrectly as class definition.
12282   // verifyFormat("class A<int> f() {\n}\nint n;");
12283 
12284   // Elaborate types where incorrectly parsing the structural element would
12285   // break the indent.
12286   verifyFormat("if (true)\n"
12287                "  class X x;\n"
12288                "else\n"
12289                "  f();\n");
12290 
12291   // This is simply incomplete. Formatting is not important, but must not crash.
12292   verifyFormat("class A:");
12293 }
12294 
12295 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12296   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12297             format("#error Leave     all         white!!!!! space* alone!\n"));
12298   EXPECT_EQ(
12299       "#warning Leave     all         white!!!!! space* alone!\n",
12300       format("#warning Leave     all         white!!!!! space* alone!\n"));
12301   EXPECT_EQ("#error 1", format("  #  error   1"));
12302   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12303 }
12304 
12305 TEST_F(FormatTest, FormatHashIfExpressions) {
12306   verifyFormat("#if AAAA && BBBB");
12307   verifyFormat("#if (AAAA && BBBB)");
12308   verifyFormat("#elif (AAAA && BBBB)");
12309   // FIXME: Come up with a better indentation for #elif.
12310   verifyFormat(
12311       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12312       "    defined(BBBBBBBB)\n"
12313       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12314       "    defined(BBBBBBBB)\n"
12315       "#endif",
12316       getLLVMStyleWithColumns(65));
12317 }
12318 
12319 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12320   FormatStyle AllowsMergedIf = getGoogleStyle();
12321   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12322       FormatStyle::SIS_WithoutElse;
12323   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12324   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12325   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12326   EXPECT_EQ("if (true) return 42;",
12327             format("if (true)\nreturn 42;", AllowsMergedIf));
12328   FormatStyle ShortMergedIf = AllowsMergedIf;
12329   ShortMergedIf.ColumnLimit = 25;
12330   verifyFormat("#define A \\\n"
12331                "  if (true) return 42;",
12332                ShortMergedIf);
12333   verifyFormat("#define A \\\n"
12334                "  f();    \\\n"
12335                "  if (true)\n"
12336                "#define B",
12337                ShortMergedIf);
12338   verifyFormat("#define A \\\n"
12339                "  f();    \\\n"
12340                "  if (true)\n"
12341                "g();",
12342                ShortMergedIf);
12343   verifyFormat("{\n"
12344                "#ifdef A\n"
12345                "  // Comment\n"
12346                "  if (true) continue;\n"
12347                "#endif\n"
12348                "  // Comment\n"
12349                "  if (true) continue;\n"
12350                "}",
12351                ShortMergedIf);
12352   ShortMergedIf.ColumnLimit = 33;
12353   verifyFormat("#define A \\\n"
12354                "  if constexpr (true) return 42;",
12355                ShortMergedIf);
12356   verifyFormat("#define A \\\n"
12357                "  if CONSTEXPR (true) return 42;",
12358                ShortMergedIf);
12359   ShortMergedIf.ColumnLimit = 29;
12360   verifyFormat("#define A                   \\\n"
12361                "  if (aaaaaaaaaa) return 1; \\\n"
12362                "  return 2;",
12363                ShortMergedIf);
12364   ShortMergedIf.ColumnLimit = 28;
12365   verifyFormat("#define A         \\\n"
12366                "  if (aaaaaaaaaa) \\\n"
12367                "    return 1;     \\\n"
12368                "  return 2;",
12369                ShortMergedIf);
12370   verifyFormat("#define A                \\\n"
12371                "  if constexpr (aaaaaaa) \\\n"
12372                "    return 1;            \\\n"
12373                "  return 2;",
12374                ShortMergedIf);
12375   verifyFormat("#define A                \\\n"
12376                "  if CONSTEXPR (aaaaaaa) \\\n"
12377                "    return 1;            \\\n"
12378                "  return 2;",
12379                ShortMergedIf);
12380 }
12381 
12382 TEST_F(FormatTest, FormatStarDependingOnContext) {
12383   verifyFormat("void f(int *a);");
12384   verifyFormat("void f() { f(fint * b); }");
12385   verifyFormat("class A {\n  void f(int *a);\n};");
12386   verifyFormat("class A {\n  int *a;\n};");
12387   verifyFormat("namespace a {\n"
12388                "namespace b {\n"
12389                "class A {\n"
12390                "  void f() {}\n"
12391                "  int *a;\n"
12392                "};\n"
12393                "} // namespace b\n"
12394                "} // namespace a");
12395 }
12396 
12397 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12398   verifyFormat("while");
12399   verifyFormat("operator");
12400 }
12401 
12402 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12403   // This code would be painfully slow to format if we didn't skip it.
12404   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
12405                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12406                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12407                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12408                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12409                    "A(1, 1)\n"
12410                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12411                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12412                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12413                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12414                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12415                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12416                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12417                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12418                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12419                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
12420   // Deeply nested part is untouched, rest is formatted.
12421   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
12422             format(std::string("int    i;\n") + Code + "int    j;\n",
12423                    getLLVMStyle(), SC_ExpectIncomplete));
12424 }
12425 
12426 //===----------------------------------------------------------------------===//
12427 // Objective-C tests.
12428 //===----------------------------------------------------------------------===//
12429 
12430 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
12431   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
12432   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
12433             format("-(NSUInteger)indexOfObject:(id)anObject;"));
12434   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
12435   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
12436   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
12437             format("-(NSInteger)Method3:(id)anObject;"));
12438   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
12439             format("-(NSInteger)Method4:(id)anObject;"));
12440   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
12441             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
12442   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
12443             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
12444   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12445             "forAllCells:(BOOL)flag;",
12446             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12447                    "forAllCells:(BOOL)flag;"));
12448 
12449   // Very long objectiveC method declaration.
12450   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
12451                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
12452   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
12453                "                    inRange:(NSRange)range\n"
12454                "                   outRange:(NSRange)out_range\n"
12455                "                  outRange1:(NSRange)out_range1\n"
12456                "                  outRange2:(NSRange)out_range2\n"
12457                "                  outRange3:(NSRange)out_range3\n"
12458                "                  outRange4:(NSRange)out_range4\n"
12459                "                  outRange5:(NSRange)out_range5\n"
12460                "                  outRange6:(NSRange)out_range6\n"
12461                "                  outRange7:(NSRange)out_range7\n"
12462                "                  outRange8:(NSRange)out_range8\n"
12463                "                  outRange9:(NSRange)out_range9;");
12464 
12465   // When the function name has to be wrapped.
12466   FormatStyle Style = getLLVMStyle();
12467   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
12468   // and always indents instead.
12469   Style.IndentWrappedFunctionNames = false;
12470   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12471                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
12472                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
12473                "}",
12474                Style);
12475   Style.IndentWrappedFunctionNames = true;
12476   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12477                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
12478                "               anotherName:(NSString)dddddddddddddd {\n"
12479                "}",
12480                Style);
12481 
12482   verifyFormat("- (int)sum:(vector<int>)numbers;");
12483   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
12484   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
12485   // protocol lists (but not for template classes):
12486   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
12487 
12488   verifyFormat("- (int (*)())foo:(int (*)())f;");
12489   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
12490 
12491   // If there's no return type (very rare in practice!), LLVM and Google style
12492   // agree.
12493   verifyFormat("- foo;");
12494   verifyFormat("- foo:(int)f;");
12495   verifyGoogleFormat("- foo:(int)foo;");
12496 }
12497 
12498 TEST_F(FormatTest, BreaksStringLiterals) {
12499   EXPECT_EQ("\"some text \"\n"
12500             "\"other\";",
12501             format("\"some text other\";", getLLVMStyleWithColumns(12)));
12502   EXPECT_EQ("\"some text \"\n"
12503             "\"other\";",
12504             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
12505   EXPECT_EQ(
12506       "#define A  \\\n"
12507       "  \"some \"  \\\n"
12508       "  \"text \"  \\\n"
12509       "  \"other\";",
12510       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
12511   EXPECT_EQ(
12512       "#define A  \\\n"
12513       "  \"so \"    \\\n"
12514       "  \"text \"  \\\n"
12515       "  \"other\";",
12516       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
12517 
12518   EXPECT_EQ("\"some text\"",
12519             format("\"some text\"", getLLVMStyleWithColumns(1)));
12520   EXPECT_EQ("\"some text\"",
12521             format("\"some text\"", getLLVMStyleWithColumns(11)));
12522   EXPECT_EQ("\"some \"\n"
12523             "\"text\"",
12524             format("\"some text\"", getLLVMStyleWithColumns(10)));
12525   EXPECT_EQ("\"some \"\n"
12526             "\"text\"",
12527             format("\"some text\"", getLLVMStyleWithColumns(7)));
12528   EXPECT_EQ("\"some\"\n"
12529             "\" tex\"\n"
12530             "\"t\"",
12531             format("\"some text\"", getLLVMStyleWithColumns(6)));
12532   EXPECT_EQ("\"some\"\n"
12533             "\" tex\"\n"
12534             "\" and\"",
12535             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
12536   EXPECT_EQ("\"some\"\n"
12537             "\"/tex\"\n"
12538             "\"/and\"",
12539             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
12540 
12541   EXPECT_EQ("variable =\n"
12542             "    \"long string \"\n"
12543             "    \"literal\";",
12544             format("variable = \"long string literal\";",
12545                    getLLVMStyleWithColumns(20)));
12546 
12547   EXPECT_EQ("variable = f(\n"
12548             "    \"long string \"\n"
12549             "    \"literal\",\n"
12550             "    short,\n"
12551             "    loooooooooooooooooooong);",
12552             format("variable = f(\"long string literal\", short, "
12553                    "loooooooooooooooooooong);",
12554                    getLLVMStyleWithColumns(20)));
12555 
12556   EXPECT_EQ(
12557       "f(g(\"long string \"\n"
12558       "    \"literal\"),\n"
12559       "  b);",
12560       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
12561   EXPECT_EQ("f(g(\"long string \"\n"
12562             "    \"literal\",\n"
12563             "    a),\n"
12564             "  b);",
12565             format("f(g(\"long string literal\", a), b);",
12566                    getLLVMStyleWithColumns(20)));
12567   EXPECT_EQ(
12568       "f(\"one two\".split(\n"
12569       "    variable));",
12570       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
12571   EXPECT_EQ("f(\"one two three four five six \"\n"
12572             "  \"seven\".split(\n"
12573             "      really_looooong_variable));",
12574             format("f(\"one two three four five six seven\"."
12575                    "split(really_looooong_variable));",
12576                    getLLVMStyleWithColumns(33)));
12577 
12578   EXPECT_EQ("f(\"some \"\n"
12579             "  \"text\",\n"
12580             "  other);",
12581             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
12582 
12583   // Only break as a last resort.
12584   verifyFormat(
12585       "aaaaaaaaaaaaaaaaaaaa(\n"
12586       "    aaaaaaaaaaaaaaaaaaaa,\n"
12587       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
12588 
12589   EXPECT_EQ("\"splitmea\"\n"
12590             "\"trandomp\"\n"
12591             "\"oint\"",
12592             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
12593 
12594   EXPECT_EQ("\"split/\"\n"
12595             "\"pathat/\"\n"
12596             "\"slashes\"",
12597             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12598 
12599   EXPECT_EQ("\"split/\"\n"
12600             "\"pathat/\"\n"
12601             "\"slashes\"",
12602             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12603   EXPECT_EQ("\"split at \"\n"
12604             "\"spaces/at/\"\n"
12605             "\"slashes.at.any$\"\n"
12606             "\"non-alphanumeric%\"\n"
12607             "\"1111111111characte\"\n"
12608             "\"rs\"",
12609             format("\"split at "
12610                    "spaces/at/"
12611                    "slashes.at."
12612                    "any$non-"
12613                    "alphanumeric%"
12614                    "1111111111characte"
12615                    "rs\"",
12616                    getLLVMStyleWithColumns(20)));
12617 
12618   // Verify that splitting the strings understands
12619   // Style::AlwaysBreakBeforeMultilineStrings.
12620   EXPECT_EQ("aaaaaaaaaaaa(\n"
12621             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
12622             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
12623             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
12624                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12625                    "aaaaaaaaaaaaaaaaaaaaaa\");",
12626                    getGoogleStyle()));
12627   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12628             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
12629             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
12630                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12631                    "aaaaaaaaaaaaaaaaaaaaaa\";",
12632                    getGoogleStyle()));
12633   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12634             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12635             format("llvm::outs() << "
12636                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
12637                    "aaaaaaaaaaaaaaaaaaa\";"));
12638   EXPECT_EQ("ffff(\n"
12639             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12640             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12641             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
12642                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12643                    getGoogleStyle()));
12644 
12645   FormatStyle Style = getLLVMStyleWithColumns(12);
12646   Style.BreakStringLiterals = false;
12647   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
12648 
12649   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
12650   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12651   EXPECT_EQ("#define A \\\n"
12652             "  \"some \" \\\n"
12653             "  \"text \" \\\n"
12654             "  \"other\";",
12655             format("#define A \"some text other\";", AlignLeft));
12656 }
12657 
12658 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
12659   EXPECT_EQ("C a = \"some more \"\n"
12660             "      \"text\";",
12661             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
12662 }
12663 
12664 TEST_F(FormatTest, FullyRemoveEmptyLines) {
12665   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
12666   NoEmptyLines.MaxEmptyLinesToKeep = 0;
12667   EXPECT_EQ("int i = a(b());",
12668             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
12669 }
12670 
12671 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
12672   EXPECT_EQ(
12673       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12674       "(\n"
12675       "    \"x\t\");",
12676       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12677              "aaaaaaa("
12678              "\"x\t\");"));
12679 }
12680 
12681 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
12682   EXPECT_EQ(
12683       "u8\"utf8 string \"\n"
12684       "u8\"literal\";",
12685       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
12686   EXPECT_EQ(
12687       "u\"utf16 string \"\n"
12688       "u\"literal\";",
12689       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
12690   EXPECT_EQ(
12691       "U\"utf32 string \"\n"
12692       "U\"literal\";",
12693       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
12694   EXPECT_EQ("L\"wide string \"\n"
12695             "L\"literal\";",
12696             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
12697   EXPECT_EQ("@\"NSString \"\n"
12698             "@\"literal\";",
12699             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
12700   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
12701 
12702   // This input makes clang-format try to split the incomplete unicode escape
12703   // sequence, which used to lead to a crasher.
12704   verifyNoCrash(
12705       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12706       getLLVMStyleWithColumns(60));
12707 }
12708 
12709 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
12710   FormatStyle Style = getGoogleStyleWithColumns(15);
12711   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
12712   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
12713   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
12714   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
12715   EXPECT_EQ("u8R\"x(raw literal)x\";",
12716             format("u8R\"x(raw literal)x\";", Style));
12717 }
12718 
12719 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
12720   FormatStyle Style = getLLVMStyleWithColumns(20);
12721   EXPECT_EQ(
12722       "_T(\"aaaaaaaaaaaaaa\")\n"
12723       "_T(\"aaaaaaaaaaaaaa\")\n"
12724       "_T(\"aaaaaaaaaaaa\")",
12725       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
12726   EXPECT_EQ("f(x,\n"
12727             "  _T(\"aaaaaaaaaaaa\")\n"
12728             "  _T(\"aaa\"),\n"
12729             "  z);",
12730             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
12731 
12732   // FIXME: Handle embedded spaces in one iteration.
12733   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
12734   //            "_T(\"aaaaaaaaaaaaa\")\n"
12735   //            "_T(\"aaaaaaaaaaaaa\")\n"
12736   //            "_T(\"a\")",
12737   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12738   //                   getLLVMStyleWithColumns(20)));
12739   EXPECT_EQ(
12740       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12741       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
12742   EXPECT_EQ("f(\n"
12743             "#if !TEST\n"
12744             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12745             "#endif\n"
12746             ");",
12747             format("f(\n"
12748                    "#if !TEST\n"
12749                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12750                    "#endif\n"
12751                    ");"));
12752   EXPECT_EQ("f(\n"
12753             "\n"
12754             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
12755             format("f(\n"
12756                    "\n"
12757                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
12758 }
12759 
12760 TEST_F(FormatTest, BreaksStringLiteralOperands) {
12761   // In a function call with two operands, the second can be broken with no line
12762   // break before it.
12763   EXPECT_EQ(
12764       "func(a, \"long long \"\n"
12765       "        \"long long\");",
12766       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
12767   // In a function call with three operands, the second must be broken with a
12768   // line break before it.
12769   EXPECT_EQ("func(a,\n"
12770             "     \"long long long \"\n"
12771             "     \"long\",\n"
12772             "     c);",
12773             format("func(a, \"long long long long\", c);",
12774                    getLLVMStyleWithColumns(24)));
12775   // In a function call with three operands, the third must be broken with a
12776   // line break before it.
12777   EXPECT_EQ("func(a, b,\n"
12778             "     \"long long long \"\n"
12779             "     \"long\");",
12780             format("func(a, b, \"long long long long\");",
12781                    getLLVMStyleWithColumns(24)));
12782   // In a function call with three operands, both the second and the third must
12783   // be broken with a line break before them.
12784   EXPECT_EQ("func(a,\n"
12785             "     \"long long long \"\n"
12786             "     \"long\",\n"
12787             "     \"long long long \"\n"
12788             "     \"long\");",
12789             format("func(a, \"long long long long\", \"long long long long\");",
12790                    getLLVMStyleWithColumns(24)));
12791   // In a chain of << with two operands, the second can be broken with no line
12792   // break before it.
12793   EXPECT_EQ("a << \"line line \"\n"
12794             "     \"line\";",
12795             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
12796   // In a chain of << with three operands, the second can be broken with no line
12797   // break before it.
12798   EXPECT_EQ(
12799       "abcde << \"line \"\n"
12800       "         \"line line\"\n"
12801       "      << c;",
12802       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
12803   // In a chain of << with three operands, the third must be broken with a line
12804   // break before it.
12805   EXPECT_EQ(
12806       "a << b\n"
12807       "  << \"line line \"\n"
12808       "     \"line\";",
12809       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
12810   // In a chain of << with three operands, the second can be broken with no line
12811   // break before it and the third must be broken with a line break before it.
12812   EXPECT_EQ("abcd << \"line line \"\n"
12813             "        \"line\"\n"
12814             "     << \"line line \"\n"
12815             "        \"line\";",
12816             format("abcd << \"line line line\" << \"line line line\";",
12817                    getLLVMStyleWithColumns(20)));
12818   // In a chain of binary operators with two operands, the second can be broken
12819   // with no line break before it.
12820   EXPECT_EQ(
12821       "abcd + \"line line \"\n"
12822       "       \"line line\";",
12823       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
12824   // In a chain of binary operators with three operands, the second must be
12825   // broken with a line break before it.
12826   EXPECT_EQ("abcd +\n"
12827             "    \"line line \"\n"
12828             "    \"line line\" +\n"
12829             "    e;",
12830             format("abcd + \"line line line line\" + e;",
12831                    getLLVMStyleWithColumns(20)));
12832   // In a function call with two operands, with AlignAfterOpenBracket enabled,
12833   // the first must be broken with a line break before it.
12834   FormatStyle Style = getLLVMStyleWithColumns(25);
12835   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12836   EXPECT_EQ("someFunction(\n"
12837             "    \"long long long \"\n"
12838             "    \"long\",\n"
12839             "    a);",
12840             format("someFunction(\"long long long long\", a);", Style));
12841 }
12842 
12843 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
12844   EXPECT_EQ(
12845       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12846       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12847       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12848       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12849              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12850              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
12851 }
12852 
12853 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
12854   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
12855             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
12856   EXPECT_EQ("fffffffffff(g(R\"x(\n"
12857             "multiline raw string literal xxxxxxxxxxxxxx\n"
12858             ")x\",\n"
12859             "              a),\n"
12860             "            b);",
12861             format("fffffffffff(g(R\"x(\n"
12862                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12863                    ")x\", a), b);",
12864                    getGoogleStyleWithColumns(20)));
12865   EXPECT_EQ("fffffffffff(\n"
12866             "    g(R\"x(qqq\n"
12867             "multiline raw string literal xxxxxxxxxxxxxx\n"
12868             ")x\",\n"
12869             "      a),\n"
12870             "    b);",
12871             format("fffffffffff(g(R\"x(qqq\n"
12872                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12873                    ")x\", a), b);",
12874                    getGoogleStyleWithColumns(20)));
12875 
12876   EXPECT_EQ("fffffffffff(R\"x(\n"
12877             "multiline raw string literal xxxxxxxxxxxxxx\n"
12878             ")x\");",
12879             format("fffffffffff(R\"x(\n"
12880                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12881                    ")x\");",
12882                    getGoogleStyleWithColumns(20)));
12883   EXPECT_EQ("fffffffffff(R\"x(\n"
12884             "multiline raw string literal xxxxxxxxxxxxxx\n"
12885             ")x\" + bbbbbb);",
12886             format("fffffffffff(R\"x(\n"
12887                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12888                    ")x\" +   bbbbbb);",
12889                    getGoogleStyleWithColumns(20)));
12890   EXPECT_EQ("fffffffffff(\n"
12891             "    R\"x(\n"
12892             "multiline raw string literal xxxxxxxxxxxxxx\n"
12893             ")x\" +\n"
12894             "    bbbbbb);",
12895             format("fffffffffff(\n"
12896                    " R\"x(\n"
12897                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12898                    ")x\" + bbbbbb);",
12899                    getGoogleStyleWithColumns(20)));
12900   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
12901             format("fffffffffff(\n"
12902                    " R\"(single line raw string)\" + bbbbbb);"));
12903 }
12904 
12905 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
12906   verifyFormat("string a = \"unterminated;");
12907   EXPECT_EQ("function(\"unterminated,\n"
12908             "         OtherParameter);",
12909             format("function(  \"unterminated,\n"
12910                    "    OtherParameter);"));
12911 }
12912 
12913 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
12914   FormatStyle Style = getLLVMStyle();
12915   Style.Standard = FormatStyle::LS_Cpp03;
12916   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
12917             format("#define x(_a) printf(\"foo\"_a);", Style));
12918 }
12919 
12920 TEST_F(FormatTest, CppLexVersion) {
12921   FormatStyle Style = getLLVMStyle();
12922   // Formatting of x * y differs if x is a type.
12923   verifyFormat("void foo() { MACRO(a * b); }", Style);
12924   verifyFormat("void foo() { MACRO(int *b); }", Style);
12925 
12926   // LLVM style uses latest lexer.
12927   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
12928   Style.Standard = FormatStyle::LS_Cpp17;
12929   // But in c++17, char8_t isn't a keyword.
12930   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
12931 }
12932 
12933 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
12934 
12935 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
12936   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
12937             "             \"ddeeefff\");",
12938             format("someFunction(\"aaabbbcccdddeeefff\");",
12939                    getLLVMStyleWithColumns(25)));
12940   EXPECT_EQ("someFunction1234567890(\n"
12941             "    \"aaabbbcccdddeeefff\");",
12942             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12943                    getLLVMStyleWithColumns(26)));
12944   EXPECT_EQ("someFunction1234567890(\n"
12945             "    \"aaabbbcccdddeeeff\"\n"
12946             "    \"f\");",
12947             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12948                    getLLVMStyleWithColumns(25)));
12949   EXPECT_EQ("someFunction1234567890(\n"
12950             "    \"aaabbbcccdddeeeff\"\n"
12951             "    \"f\");",
12952             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12953                    getLLVMStyleWithColumns(24)));
12954   EXPECT_EQ("someFunction(\n"
12955             "    \"aaabbbcc ddde \"\n"
12956             "    \"efff\");",
12957             format("someFunction(\"aaabbbcc ddde efff\");",
12958                    getLLVMStyleWithColumns(25)));
12959   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
12960             "             \"ddeeefff\");",
12961             format("someFunction(\"aaabbbccc ddeeefff\");",
12962                    getLLVMStyleWithColumns(25)));
12963   EXPECT_EQ("someFunction1234567890(\n"
12964             "    \"aaabb \"\n"
12965             "    \"cccdddeeefff\");",
12966             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
12967                    getLLVMStyleWithColumns(25)));
12968   EXPECT_EQ("#define A          \\\n"
12969             "  string s =       \\\n"
12970             "      \"123456789\"  \\\n"
12971             "      \"0\";         \\\n"
12972             "  int i;",
12973             format("#define A string s = \"1234567890\"; int i;",
12974                    getLLVMStyleWithColumns(20)));
12975   EXPECT_EQ("someFunction(\n"
12976             "    \"aaabbbcc \"\n"
12977             "    \"dddeeefff\");",
12978             format("someFunction(\"aaabbbcc dddeeefff\");",
12979                    getLLVMStyleWithColumns(25)));
12980 }
12981 
12982 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
12983   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
12984   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
12985   EXPECT_EQ("\"test\"\n"
12986             "\"\\n\"",
12987             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
12988   EXPECT_EQ("\"tes\\\\\"\n"
12989             "\"n\"",
12990             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
12991   EXPECT_EQ("\"\\\\\\\\\"\n"
12992             "\"\\n\"",
12993             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
12994   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
12995   EXPECT_EQ("\"\\uff01\"\n"
12996             "\"test\"",
12997             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
12998   EXPECT_EQ("\"\\Uff01ff02\"",
12999             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13000   EXPECT_EQ("\"\\x000000000001\"\n"
13001             "\"next\"",
13002             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13003   EXPECT_EQ("\"\\x000000000001next\"",
13004             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13005   EXPECT_EQ("\"\\x000000000001\"",
13006             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13007   EXPECT_EQ("\"test\"\n"
13008             "\"\\000000\"\n"
13009             "\"000001\"",
13010             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13011   EXPECT_EQ("\"test\\000\"\n"
13012             "\"00000000\"\n"
13013             "\"1\"",
13014             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13015 }
13016 
13017 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13018   verifyFormat("void f() {\n"
13019                "  return g() {}\n"
13020                "  void h() {}");
13021   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13022                "g();\n"
13023                "}");
13024 }
13025 
13026 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13027   verifyFormat(
13028       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13029 }
13030 
13031 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13032   verifyFormat("class X {\n"
13033                "  void f() {\n"
13034                "  }\n"
13035                "};",
13036                getLLVMStyleWithColumns(12));
13037 }
13038 
13039 TEST_F(FormatTest, ConfigurableIndentWidth) {
13040   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13041   EightIndent.IndentWidth = 8;
13042   EightIndent.ContinuationIndentWidth = 8;
13043   verifyFormat("void f() {\n"
13044                "        someFunction();\n"
13045                "        if (true) {\n"
13046                "                f();\n"
13047                "        }\n"
13048                "}",
13049                EightIndent);
13050   verifyFormat("class X {\n"
13051                "        void f() {\n"
13052                "        }\n"
13053                "};",
13054                EightIndent);
13055   verifyFormat("int x[] = {\n"
13056                "        call(),\n"
13057                "        call()};",
13058                EightIndent);
13059 }
13060 
13061 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13062   verifyFormat("double\n"
13063                "f();",
13064                getLLVMStyleWithColumns(8));
13065 }
13066 
13067 TEST_F(FormatTest, ConfigurableUseOfTab) {
13068   FormatStyle Tab = getLLVMStyleWithColumns(42);
13069   Tab.IndentWidth = 8;
13070   Tab.UseTab = FormatStyle::UT_Always;
13071   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13072 
13073   EXPECT_EQ("if (aaaaaaaa && // q\n"
13074             "    bb)\t\t// w\n"
13075             "\t;",
13076             format("if (aaaaaaaa &&// q\n"
13077                    "bb)// w\n"
13078                    ";",
13079                    Tab));
13080   EXPECT_EQ("if (aaa && bbb) // w\n"
13081             "\t;",
13082             format("if(aaa&&bbb)// w\n"
13083                    ";",
13084                    Tab));
13085 
13086   verifyFormat("class X {\n"
13087                "\tvoid f() {\n"
13088                "\t\tsomeFunction(parameter1,\n"
13089                "\t\t\t     parameter2);\n"
13090                "\t}\n"
13091                "};",
13092                Tab);
13093   verifyFormat("#define A                        \\\n"
13094                "\tvoid f() {               \\\n"
13095                "\t\tsomeFunction(    \\\n"
13096                "\t\t    parameter1,  \\\n"
13097                "\t\t    parameter2); \\\n"
13098                "\t}",
13099                Tab);
13100   verifyFormat("int a;\t      // x\n"
13101                "int bbbbbbbb; // x\n",
13102                Tab);
13103 
13104   Tab.TabWidth = 4;
13105   Tab.IndentWidth = 8;
13106   verifyFormat("class TabWidth4Indent8 {\n"
13107                "\t\tvoid f() {\n"
13108                "\t\t\t\tsomeFunction(parameter1,\n"
13109                "\t\t\t\t\t\t\t parameter2);\n"
13110                "\t\t}\n"
13111                "};",
13112                Tab);
13113 
13114   Tab.TabWidth = 4;
13115   Tab.IndentWidth = 4;
13116   verifyFormat("class TabWidth4Indent4 {\n"
13117                "\tvoid f() {\n"
13118                "\t\tsomeFunction(parameter1,\n"
13119                "\t\t\t\t\t parameter2);\n"
13120                "\t}\n"
13121                "};",
13122                Tab);
13123 
13124   Tab.TabWidth = 8;
13125   Tab.IndentWidth = 4;
13126   verifyFormat("class TabWidth8Indent4 {\n"
13127                "    void f() {\n"
13128                "\tsomeFunction(parameter1,\n"
13129                "\t\t     parameter2);\n"
13130                "    }\n"
13131                "};",
13132                Tab);
13133 
13134   Tab.TabWidth = 8;
13135   Tab.IndentWidth = 8;
13136   EXPECT_EQ("/*\n"
13137             "\t      a\t\tcomment\n"
13138             "\t      in multiple lines\n"
13139             "       */",
13140             format("   /*\t \t \n"
13141                    " \t \t a\t\tcomment\t \t\n"
13142                    " \t \t in multiple lines\t\n"
13143                    " \t  */",
13144                    Tab));
13145 
13146   Tab.UseTab = FormatStyle::UT_ForIndentation;
13147   verifyFormat("{\n"
13148                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13149                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13150                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13151                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13152                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13153                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13154                "};",
13155                Tab);
13156   verifyFormat("enum AA {\n"
13157                "\ta1, // Force multiple lines\n"
13158                "\ta2,\n"
13159                "\ta3\n"
13160                "};",
13161                Tab);
13162   EXPECT_EQ("if (aaaaaaaa && // q\n"
13163             "    bb)         // w\n"
13164             "\t;",
13165             format("if (aaaaaaaa &&// q\n"
13166                    "bb)// w\n"
13167                    ";",
13168                    Tab));
13169   verifyFormat("class X {\n"
13170                "\tvoid f() {\n"
13171                "\t\tsomeFunction(parameter1,\n"
13172                "\t\t             parameter2);\n"
13173                "\t}\n"
13174                "};",
13175                Tab);
13176   verifyFormat("{\n"
13177                "\tQ(\n"
13178                "\t    {\n"
13179                "\t\t    int a;\n"
13180                "\t\t    someFunction(aaaaaaaa,\n"
13181                "\t\t                 bbbbbbb);\n"
13182                "\t    },\n"
13183                "\t    p);\n"
13184                "}",
13185                Tab);
13186   EXPECT_EQ("{\n"
13187             "\t/* aaaa\n"
13188             "\t   bbbb */\n"
13189             "}",
13190             format("{\n"
13191                    "/* aaaa\n"
13192                    "   bbbb */\n"
13193                    "}",
13194                    Tab));
13195   EXPECT_EQ("{\n"
13196             "\t/*\n"
13197             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13198             "\t  bbbbbbbbbbbbb\n"
13199             "\t*/\n"
13200             "}",
13201             format("{\n"
13202                    "/*\n"
13203                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13204                    "*/\n"
13205                    "}",
13206                    Tab));
13207   EXPECT_EQ("{\n"
13208             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13209             "\t// bbbbbbbbbbbbb\n"
13210             "}",
13211             format("{\n"
13212                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13213                    "}",
13214                    Tab));
13215   EXPECT_EQ("{\n"
13216             "\t/*\n"
13217             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13218             "\t  bbbbbbbbbbbbb\n"
13219             "\t*/\n"
13220             "}",
13221             format("{\n"
13222                    "\t/*\n"
13223                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13224                    "\t*/\n"
13225                    "}",
13226                    Tab));
13227   EXPECT_EQ("{\n"
13228             "\t/*\n"
13229             "\n"
13230             "\t*/\n"
13231             "}",
13232             format("{\n"
13233                    "\t/*\n"
13234                    "\n"
13235                    "\t*/\n"
13236                    "}",
13237                    Tab));
13238   EXPECT_EQ("{\n"
13239             "\t/*\n"
13240             " asdf\n"
13241             "\t*/\n"
13242             "}",
13243             format("{\n"
13244                    "\t/*\n"
13245                    " asdf\n"
13246                    "\t*/\n"
13247                    "}",
13248                    Tab));
13249 
13250   Tab.UseTab = FormatStyle::UT_Never;
13251   EXPECT_EQ("/*\n"
13252             "              a\t\tcomment\n"
13253             "              in multiple lines\n"
13254             "       */",
13255             format("   /*\t \t \n"
13256                    " \t \t a\t\tcomment\t \t\n"
13257                    " \t \t in multiple lines\t\n"
13258                    " \t  */",
13259                    Tab));
13260   EXPECT_EQ("/* some\n"
13261             "   comment */",
13262             format(" \t \t /* some\n"
13263                    " \t \t    comment */",
13264                    Tab));
13265   EXPECT_EQ("int a; /* some\n"
13266             "   comment */",
13267             format(" \t \t int a; /* some\n"
13268                    " \t \t    comment */",
13269                    Tab));
13270 
13271   EXPECT_EQ("int a; /* some\n"
13272             "comment */",
13273             format(" \t \t int\ta; /* some\n"
13274                    " \t \t    comment */",
13275                    Tab));
13276   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13277             "    comment */",
13278             format(" \t \t f(\"\t\t\"); /* some\n"
13279                    " \t \t    comment */",
13280                    Tab));
13281   EXPECT_EQ("{\n"
13282             "        /*\n"
13283             "         * Comment\n"
13284             "         */\n"
13285             "        int i;\n"
13286             "}",
13287             format("{\n"
13288                    "\t/*\n"
13289                    "\t * Comment\n"
13290                    "\t */\n"
13291                    "\t int i;\n"
13292                    "}",
13293                    Tab));
13294 
13295   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13296   Tab.TabWidth = 8;
13297   Tab.IndentWidth = 8;
13298   EXPECT_EQ("if (aaaaaaaa && // q\n"
13299             "    bb)         // w\n"
13300             "\t;",
13301             format("if (aaaaaaaa &&// q\n"
13302                    "bb)// w\n"
13303                    ";",
13304                    Tab));
13305   EXPECT_EQ("if (aaa && bbb) // w\n"
13306             "\t;",
13307             format("if(aaa&&bbb)// w\n"
13308                    ";",
13309                    Tab));
13310   verifyFormat("class X {\n"
13311                "\tvoid f() {\n"
13312                "\t\tsomeFunction(parameter1,\n"
13313                "\t\t\t     parameter2);\n"
13314                "\t}\n"
13315                "};",
13316                Tab);
13317   verifyFormat("#define A                        \\\n"
13318                "\tvoid f() {               \\\n"
13319                "\t\tsomeFunction(    \\\n"
13320                "\t\t    parameter1,  \\\n"
13321                "\t\t    parameter2); \\\n"
13322                "\t}",
13323                Tab);
13324   Tab.TabWidth = 4;
13325   Tab.IndentWidth = 8;
13326   verifyFormat("class TabWidth4Indent8 {\n"
13327                "\t\tvoid f() {\n"
13328                "\t\t\t\tsomeFunction(parameter1,\n"
13329                "\t\t\t\t\t\t\t parameter2);\n"
13330                "\t\t}\n"
13331                "};",
13332                Tab);
13333   Tab.TabWidth = 4;
13334   Tab.IndentWidth = 4;
13335   verifyFormat("class TabWidth4Indent4 {\n"
13336                "\tvoid f() {\n"
13337                "\t\tsomeFunction(parameter1,\n"
13338                "\t\t\t\t\t parameter2);\n"
13339                "\t}\n"
13340                "};",
13341                Tab);
13342   Tab.TabWidth = 8;
13343   Tab.IndentWidth = 4;
13344   verifyFormat("class TabWidth8Indent4 {\n"
13345                "    void f() {\n"
13346                "\tsomeFunction(parameter1,\n"
13347                "\t\t     parameter2);\n"
13348                "    }\n"
13349                "};",
13350                Tab);
13351   Tab.TabWidth = 8;
13352   Tab.IndentWidth = 8;
13353   EXPECT_EQ("/*\n"
13354             "\t      a\t\tcomment\n"
13355             "\t      in multiple lines\n"
13356             "       */",
13357             format("   /*\t \t \n"
13358                    " \t \t a\t\tcomment\t \t\n"
13359                    " \t \t in multiple lines\t\n"
13360                    " \t  */",
13361                    Tab));
13362   verifyFormat("{\n"
13363                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13364                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13365                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13366                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13367                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13368                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13369                "};",
13370                Tab);
13371   verifyFormat("enum AA {\n"
13372                "\ta1, // Force multiple lines\n"
13373                "\ta2,\n"
13374                "\ta3\n"
13375                "};",
13376                Tab);
13377   EXPECT_EQ("if (aaaaaaaa && // q\n"
13378             "    bb)         // w\n"
13379             "\t;",
13380             format("if (aaaaaaaa &&// q\n"
13381                    "bb)// w\n"
13382                    ";",
13383                    Tab));
13384   verifyFormat("class X {\n"
13385                "\tvoid f() {\n"
13386                "\t\tsomeFunction(parameter1,\n"
13387                "\t\t\t     parameter2);\n"
13388                "\t}\n"
13389                "};",
13390                Tab);
13391   verifyFormat("{\n"
13392                "\tQ(\n"
13393                "\t    {\n"
13394                "\t\t    int a;\n"
13395                "\t\t    someFunction(aaaaaaaa,\n"
13396                "\t\t\t\t bbbbbbb);\n"
13397                "\t    },\n"
13398                "\t    p);\n"
13399                "}",
13400                Tab);
13401   EXPECT_EQ("{\n"
13402             "\t/* aaaa\n"
13403             "\t   bbbb */\n"
13404             "}",
13405             format("{\n"
13406                    "/* aaaa\n"
13407                    "   bbbb */\n"
13408                    "}",
13409                    Tab));
13410   EXPECT_EQ("{\n"
13411             "\t/*\n"
13412             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13413             "\t  bbbbbbbbbbbbb\n"
13414             "\t*/\n"
13415             "}",
13416             format("{\n"
13417                    "/*\n"
13418                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13419                    "*/\n"
13420                    "}",
13421                    Tab));
13422   EXPECT_EQ("{\n"
13423             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13424             "\t// bbbbbbbbbbbbb\n"
13425             "}",
13426             format("{\n"
13427                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13428                    "}",
13429                    Tab));
13430   EXPECT_EQ("{\n"
13431             "\t/*\n"
13432             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13433             "\t  bbbbbbbbbbbbb\n"
13434             "\t*/\n"
13435             "}",
13436             format("{\n"
13437                    "\t/*\n"
13438                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13439                    "\t*/\n"
13440                    "}",
13441                    Tab));
13442   EXPECT_EQ("{\n"
13443             "\t/*\n"
13444             "\n"
13445             "\t*/\n"
13446             "}",
13447             format("{\n"
13448                    "\t/*\n"
13449                    "\n"
13450                    "\t*/\n"
13451                    "}",
13452                    Tab));
13453   EXPECT_EQ("{\n"
13454             "\t/*\n"
13455             " asdf\n"
13456             "\t*/\n"
13457             "}",
13458             format("{\n"
13459                    "\t/*\n"
13460                    " asdf\n"
13461                    "\t*/\n"
13462                    "}",
13463                    Tab));
13464   EXPECT_EQ("/* some\n"
13465             "   comment */",
13466             format(" \t \t /* some\n"
13467                    " \t \t    comment */",
13468                    Tab));
13469   EXPECT_EQ("int a; /* some\n"
13470             "   comment */",
13471             format(" \t \t int a; /* some\n"
13472                    " \t \t    comment */",
13473                    Tab));
13474   EXPECT_EQ("int a; /* some\n"
13475             "comment */",
13476             format(" \t \t int\ta; /* some\n"
13477                    " \t \t    comment */",
13478                    Tab));
13479   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13480             "    comment */",
13481             format(" \t \t f(\"\t\t\"); /* some\n"
13482                    " \t \t    comment */",
13483                    Tab));
13484   EXPECT_EQ("{\n"
13485             "\t/*\n"
13486             "\t * Comment\n"
13487             "\t */\n"
13488             "\tint i;\n"
13489             "}",
13490             format("{\n"
13491                    "\t/*\n"
13492                    "\t * Comment\n"
13493                    "\t */\n"
13494                    "\t int i;\n"
13495                    "}",
13496                    Tab));
13497   Tab.TabWidth = 2;
13498   Tab.IndentWidth = 2;
13499   EXPECT_EQ("{\n"
13500             "\t/* aaaa\n"
13501             "\t\t bbbb */\n"
13502             "}",
13503             format("{\n"
13504                    "/* aaaa\n"
13505                    "\t bbbb */\n"
13506                    "}",
13507                    Tab));
13508   EXPECT_EQ("{\n"
13509             "\t/*\n"
13510             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13511             "\t\tbbbbbbbbbbbbb\n"
13512             "\t*/\n"
13513             "}",
13514             format("{\n"
13515                    "/*\n"
13516                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13517                    "*/\n"
13518                    "}",
13519                    Tab));
13520   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13521   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13522   Tab.TabWidth = 4;
13523   Tab.IndentWidth = 4;
13524   verifyFormat("class Assign {\n"
13525                "\tvoid f() {\n"
13526                "\t\tint         x      = 123;\n"
13527                "\t\tint         random = 4;\n"
13528                "\t\tstd::string alphabet =\n"
13529                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13530                "\t}\n"
13531                "};",
13532                Tab);
13533 
13534   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13535   Tab.TabWidth = 8;
13536   Tab.IndentWidth = 8;
13537   EXPECT_EQ("if (aaaaaaaa && // q\n"
13538             "    bb)         // w\n"
13539             "\t;",
13540             format("if (aaaaaaaa &&// q\n"
13541                    "bb)// w\n"
13542                    ";",
13543                    Tab));
13544   EXPECT_EQ("if (aaa && bbb) // w\n"
13545             "\t;",
13546             format("if(aaa&&bbb)// w\n"
13547                    ";",
13548                    Tab));
13549   verifyFormat("class X {\n"
13550                "\tvoid f() {\n"
13551                "\t\tsomeFunction(parameter1,\n"
13552                "\t\t             parameter2);\n"
13553                "\t}\n"
13554                "};",
13555                Tab);
13556   verifyFormat("#define A                        \\\n"
13557                "\tvoid f() {               \\\n"
13558                "\t\tsomeFunction(    \\\n"
13559                "\t\t    parameter1,  \\\n"
13560                "\t\t    parameter2); \\\n"
13561                "\t}",
13562                Tab);
13563   Tab.TabWidth = 4;
13564   Tab.IndentWidth = 8;
13565   verifyFormat("class TabWidth4Indent8 {\n"
13566                "\t\tvoid f() {\n"
13567                "\t\t\t\tsomeFunction(parameter1,\n"
13568                "\t\t\t\t             parameter2);\n"
13569                "\t\t}\n"
13570                "};",
13571                Tab);
13572   Tab.TabWidth = 4;
13573   Tab.IndentWidth = 4;
13574   verifyFormat("class TabWidth4Indent4 {\n"
13575                "\tvoid f() {\n"
13576                "\t\tsomeFunction(parameter1,\n"
13577                "\t\t             parameter2);\n"
13578                "\t}\n"
13579                "};",
13580                Tab);
13581   Tab.TabWidth = 8;
13582   Tab.IndentWidth = 4;
13583   verifyFormat("class TabWidth8Indent4 {\n"
13584                "    void f() {\n"
13585                "\tsomeFunction(parameter1,\n"
13586                "\t             parameter2);\n"
13587                "    }\n"
13588                "};",
13589                Tab);
13590   Tab.TabWidth = 8;
13591   Tab.IndentWidth = 8;
13592   EXPECT_EQ("/*\n"
13593             "              a\t\tcomment\n"
13594             "              in multiple lines\n"
13595             "       */",
13596             format("   /*\t \t \n"
13597                    " \t \t a\t\tcomment\t \t\n"
13598                    " \t \t in multiple lines\t\n"
13599                    " \t  */",
13600                    Tab));
13601   verifyFormat("{\n"
13602                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13603                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13604                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13605                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13606                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13607                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13608                "};",
13609                Tab);
13610   verifyFormat("enum AA {\n"
13611                "\ta1, // Force multiple lines\n"
13612                "\ta2,\n"
13613                "\ta3\n"
13614                "};",
13615                Tab);
13616   EXPECT_EQ("if (aaaaaaaa && // q\n"
13617             "    bb)         // w\n"
13618             "\t;",
13619             format("if (aaaaaaaa &&// q\n"
13620                    "bb)// w\n"
13621                    ";",
13622                    Tab));
13623   verifyFormat("class X {\n"
13624                "\tvoid f() {\n"
13625                "\t\tsomeFunction(parameter1,\n"
13626                "\t\t             parameter2);\n"
13627                "\t}\n"
13628                "};",
13629                Tab);
13630   verifyFormat("{\n"
13631                "\tQ(\n"
13632                "\t    {\n"
13633                "\t\t    int a;\n"
13634                "\t\t    someFunction(aaaaaaaa,\n"
13635                "\t\t                 bbbbbbb);\n"
13636                "\t    },\n"
13637                "\t    p);\n"
13638                "}",
13639                Tab);
13640   EXPECT_EQ("{\n"
13641             "\t/* aaaa\n"
13642             "\t   bbbb */\n"
13643             "}",
13644             format("{\n"
13645                    "/* aaaa\n"
13646                    "   bbbb */\n"
13647                    "}",
13648                    Tab));
13649   EXPECT_EQ("{\n"
13650             "\t/*\n"
13651             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13652             "\t  bbbbbbbbbbbbb\n"
13653             "\t*/\n"
13654             "}",
13655             format("{\n"
13656                    "/*\n"
13657                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13658                    "*/\n"
13659                    "}",
13660                    Tab));
13661   EXPECT_EQ("{\n"
13662             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13663             "\t// bbbbbbbbbbbbb\n"
13664             "}",
13665             format("{\n"
13666                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13667                    "}",
13668                    Tab));
13669   EXPECT_EQ("{\n"
13670             "\t/*\n"
13671             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13672             "\t  bbbbbbbbbbbbb\n"
13673             "\t*/\n"
13674             "}",
13675             format("{\n"
13676                    "\t/*\n"
13677                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13678                    "\t*/\n"
13679                    "}",
13680                    Tab));
13681   EXPECT_EQ("{\n"
13682             "\t/*\n"
13683             "\n"
13684             "\t*/\n"
13685             "}",
13686             format("{\n"
13687                    "\t/*\n"
13688                    "\n"
13689                    "\t*/\n"
13690                    "}",
13691                    Tab));
13692   EXPECT_EQ("{\n"
13693             "\t/*\n"
13694             " asdf\n"
13695             "\t*/\n"
13696             "}",
13697             format("{\n"
13698                    "\t/*\n"
13699                    " asdf\n"
13700                    "\t*/\n"
13701                    "}",
13702                    Tab));
13703   EXPECT_EQ("/* some\n"
13704             "   comment */",
13705             format(" \t \t /* some\n"
13706                    " \t \t    comment */",
13707                    Tab));
13708   EXPECT_EQ("int a; /* some\n"
13709             "   comment */",
13710             format(" \t \t int a; /* some\n"
13711                    " \t \t    comment */",
13712                    Tab));
13713   EXPECT_EQ("int a; /* some\n"
13714             "comment */",
13715             format(" \t \t int\ta; /* some\n"
13716                    " \t \t    comment */",
13717                    Tab));
13718   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13719             "    comment */",
13720             format(" \t \t f(\"\t\t\"); /* some\n"
13721                    " \t \t    comment */",
13722                    Tab));
13723   EXPECT_EQ("{\n"
13724             "\t/*\n"
13725             "\t * Comment\n"
13726             "\t */\n"
13727             "\tint i;\n"
13728             "}",
13729             format("{\n"
13730                    "\t/*\n"
13731                    "\t * Comment\n"
13732                    "\t */\n"
13733                    "\t int i;\n"
13734                    "}",
13735                    Tab));
13736   Tab.TabWidth = 2;
13737   Tab.IndentWidth = 2;
13738   EXPECT_EQ("{\n"
13739             "\t/* aaaa\n"
13740             "\t   bbbb */\n"
13741             "}",
13742             format("{\n"
13743                    "/* aaaa\n"
13744                    "   bbbb */\n"
13745                    "}",
13746                    Tab));
13747   EXPECT_EQ("{\n"
13748             "\t/*\n"
13749             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13750             "\t  bbbbbbbbbbbbb\n"
13751             "\t*/\n"
13752             "}",
13753             format("{\n"
13754                    "/*\n"
13755                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13756                    "*/\n"
13757                    "}",
13758                    Tab));
13759   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13760   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13761   Tab.TabWidth = 4;
13762   Tab.IndentWidth = 4;
13763   verifyFormat("class Assign {\n"
13764                "\tvoid f() {\n"
13765                "\t\tint         x      = 123;\n"
13766                "\t\tint         random = 4;\n"
13767                "\t\tstd::string alphabet =\n"
13768                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13769                "\t}\n"
13770                "};",
13771                Tab);
13772   Tab.AlignOperands = FormatStyle::OAS_Align;
13773   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
13774                "                 cccccccccccccccccccc;",
13775                Tab);
13776   // no alignment
13777   verifyFormat("int aaaaaaaaaa =\n"
13778                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
13779                Tab);
13780   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
13781                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
13782                "                        : 333333333333333;",
13783                Tab);
13784   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
13785   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
13786   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
13787                "               + cccccccccccccccccccc;",
13788                Tab);
13789 }
13790 
13791 TEST_F(FormatTest, ZeroTabWidth) {
13792   FormatStyle Tab = getLLVMStyleWithColumns(42);
13793   Tab.IndentWidth = 8;
13794   Tab.UseTab = FormatStyle::UT_Never;
13795   Tab.TabWidth = 0;
13796   EXPECT_EQ("void a(){\n"
13797             "    // line starts with '\t'\n"
13798             "};",
13799             format("void a(){\n"
13800                    "\t// line starts with '\t'\n"
13801                    "};",
13802                    Tab));
13803 
13804   EXPECT_EQ("void a(){\n"
13805             "    // line starts with '\t'\n"
13806             "};",
13807             format("void a(){\n"
13808                    "\t\t// line starts with '\t'\n"
13809                    "};",
13810                    Tab));
13811 
13812   Tab.UseTab = FormatStyle::UT_ForIndentation;
13813   EXPECT_EQ("void a(){\n"
13814             "    // line starts with '\t'\n"
13815             "};",
13816             format("void a(){\n"
13817                    "\t// line starts with '\t'\n"
13818                    "};",
13819                    Tab));
13820 
13821   EXPECT_EQ("void a(){\n"
13822             "    // line starts with '\t'\n"
13823             "};",
13824             format("void a(){\n"
13825                    "\t\t// line starts with '\t'\n"
13826                    "};",
13827                    Tab));
13828 
13829   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13830   EXPECT_EQ("void a(){\n"
13831             "    // line starts with '\t'\n"
13832             "};",
13833             format("void a(){\n"
13834                    "\t// line starts with '\t'\n"
13835                    "};",
13836                    Tab));
13837 
13838   EXPECT_EQ("void a(){\n"
13839             "    // line starts with '\t'\n"
13840             "};",
13841             format("void a(){\n"
13842                    "\t\t// line starts with '\t'\n"
13843                    "};",
13844                    Tab));
13845 
13846   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13847   EXPECT_EQ("void a(){\n"
13848             "    // line starts with '\t'\n"
13849             "};",
13850             format("void a(){\n"
13851                    "\t// line starts with '\t'\n"
13852                    "};",
13853                    Tab));
13854 
13855   EXPECT_EQ("void a(){\n"
13856             "    // line starts with '\t'\n"
13857             "};",
13858             format("void a(){\n"
13859                    "\t\t// line starts with '\t'\n"
13860                    "};",
13861                    Tab));
13862 
13863   Tab.UseTab = FormatStyle::UT_Always;
13864   EXPECT_EQ("void a(){\n"
13865             "// line starts with '\t'\n"
13866             "};",
13867             format("void a(){\n"
13868                    "\t// line starts with '\t'\n"
13869                    "};",
13870                    Tab));
13871 
13872   EXPECT_EQ("void a(){\n"
13873             "// line starts with '\t'\n"
13874             "};",
13875             format("void a(){\n"
13876                    "\t\t// line starts with '\t'\n"
13877                    "};",
13878                    Tab));
13879 }
13880 
13881 TEST_F(FormatTest, CalculatesOriginalColumn) {
13882   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13883             "q\"; /* some\n"
13884             "       comment */",
13885             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13886                    "q\"; /* some\n"
13887                    "       comment */",
13888                    getLLVMStyle()));
13889   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13890             "/* some\n"
13891             "   comment */",
13892             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13893                    " /* some\n"
13894                    "    comment */",
13895                    getLLVMStyle()));
13896   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13897             "qqq\n"
13898             "/* some\n"
13899             "   comment */",
13900             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13901                    "qqq\n"
13902                    " /* some\n"
13903                    "    comment */",
13904                    getLLVMStyle()));
13905   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13906             "wwww; /* some\n"
13907             "         comment */",
13908             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13909                    "wwww; /* some\n"
13910                    "         comment */",
13911                    getLLVMStyle()));
13912 }
13913 
13914 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
13915   FormatStyle NoSpace = getLLVMStyle();
13916   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
13917 
13918   verifyFormat("while(true)\n"
13919                "  continue;",
13920                NoSpace);
13921   verifyFormat("for(;;)\n"
13922                "  continue;",
13923                NoSpace);
13924   verifyFormat("if(true)\n"
13925                "  f();\n"
13926                "else if(true)\n"
13927                "  f();",
13928                NoSpace);
13929   verifyFormat("do {\n"
13930                "  do_something();\n"
13931                "} while(something());",
13932                NoSpace);
13933   verifyFormat("switch(x) {\n"
13934                "default:\n"
13935                "  break;\n"
13936                "}",
13937                NoSpace);
13938   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
13939   verifyFormat("size_t x = sizeof(x);", NoSpace);
13940   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
13941   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
13942   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
13943   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
13944   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
13945   verifyFormat("alignas(128) char a[128];", NoSpace);
13946   verifyFormat("size_t x = alignof(MyType);", NoSpace);
13947   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
13948   verifyFormat("int f() throw(Deprecated);", NoSpace);
13949   verifyFormat("typedef void (*cb)(int);", NoSpace);
13950   verifyFormat("T A::operator()();", NoSpace);
13951   verifyFormat("X A::operator++(T);", NoSpace);
13952   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
13953 
13954   FormatStyle Space = getLLVMStyle();
13955   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
13956 
13957   verifyFormat("int f ();", Space);
13958   verifyFormat("void f (int a, T b) {\n"
13959                "  while (true)\n"
13960                "    continue;\n"
13961                "}",
13962                Space);
13963   verifyFormat("if (true)\n"
13964                "  f ();\n"
13965                "else if (true)\n"
13966                "  f ();",
13967                Space);
13968   verifyFormat("do {\n"
13969                "  do_something ();\n"
13970                "} while (something ());",
13971                Space);
13972   verifyFormat("switch (x) {\n"
13973                "default:\n"
13974                "  break;\n"
13975                "}",
13976                Space);
13977   verifyFormat("A::A () : a (1) {}", Space);
13978   verifyFormat("void f () __attribute__ ((asdf));", Space);
13979   verifyFormat("*(&a + 1);\n"
13980                "&((&a)[1]);\n"
13981                "a[(b + c) * d];\n"
13982                "(((a + 1) * 2) + 3) * 4;",
13983                Space);
13984   verifyFormat("#define A(x) x", Space);
13985   verifyFormat("#define A (x) x", Space);
13986   verifyFormat("#if defined(x)\n"
13987                "#endif",
13988                Space);
13989   verifyFormat("auto i = std::make_unique<int> (5);", Space);
13990   verifyFormat("size_t x = sizeof (x);", Space);
13991   verifyFormat("auto f (int x) -> decltype (x);", Space);
13992   verifyFormat("auto f (int x) -> typeof (x);", Space);
13993   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
13994   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
13995   verifyFormat("int f (T x) noexcept (x.create ());", Space);
13996   verifyFormat("alignas (128) char a[128];", Space);
13997   verifyFormat("size_t x = alignof (MyType);", Space);
13998   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
13999   verifyFormat("int f () throw (Deprecated);", Space);
14000   verifyFormat("typedef void (*cb) (int);", Space);
14001   verifyFormat("T A::operator() ();", Space);
14002   verifyFormat("X A::operator++ (T);", Space);
14003   verifyFormat("auto lambda = [] () { return 0; };", Space);
14004   verifyFormat("int x = int (y);", Space);
14005 
14006   FormatStyle SomeSpace = getLLVMStyle();
14007   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14008 
14009   verifyFormat("[]() -> float {}", SomeSpace);
14010   verifyFormat("[] (auto foo) {}", SomeSpace);
14011   verifyFormat("[foo]() -> int {}", SomeSpace);
14012   verifyFormat("int f();", SomeSpace);
14013   verifyFormat("void f (int a, T b) {\n"
14014                "  while (true)\n"
14015                "    continue;\n"
14016                "}",
14017                SomeSpace);
14018   verifyFormat("if (true)\n"
14019                "  f();\n"
14020                "else if (true)\n"
14021                "  f();",
14022                SomeSpace);
14023   verifyFormat("do {\n"
14024                "  do_something();\n"
14025                "} while (something());",
14026                SomeSpace);
14027   verifyFormat("switch (x) {\n"
14028                "default:\n"
14029                "  break;\n"
14030                "}",
14031                SomeSpace);
14032   verifyFormat("A::A() : a (1) {}", SomeSpace);
14033   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14034   verifyFormat("*(&a + 1);\n"
14035                "&((&a)[1]);\n"
14036                "a[(b + c) * d];\n"
14037                "(((a + 1) * 2) + 3) * 4;",
14038                SomeSpace);
14039   verifyFormat("#define A(x) x", SomeSpace);
14040   verifyFormat("#define A (x) x", SomeSpace);
14041   verifyFormat("#if defined(x)\n"
14042                "#endif",
14043                SomeSpace);
14044   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14045   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14046   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14047   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14048   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14049   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14050   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14051   verifyFormat("alignas (128) char a[128];", SomeSpace);
14052   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14053   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14054                SomeSpace);
14055   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14056   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14057   verifyFormat("T A::operator()();", SomeSpace);
14058   verifyFormat("X A::operator++ (T);", SomeSpace);
14059   verifyFormat("int x = int (y);", SomeSpace);
14060   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14061 }
14062 
14063 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14064   FormatStyle Spaces = getLLVMStyle();
14065   Spaces.SpaceAfterLogicalNot = true;
14066 
14067   verifyFormat("bool x = ! y", Spaces);
14068   verifyFormat("if (! isFailure())", Spaces);
14069   verifyFormat("if (! (a && b))", Spaces);
14070   verifyFormat("\"Error!\"", Spaces);
14071   verifyFormat("! ! x", Spaces);
14072 }
14073 
14074 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14075   FormatStyle Spaces = getLLVMStyle();
14076 
14077   Spaces.SpacesInParentheses = true;
14078   verifyFormat("do_something( ::globalVar );", Spaces);
14079   verifyFormat("call( x, y, z );", Spaces);
14080   verifyFormat("call();", Spaces);
14081   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14082   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14083                Spaces);
14084   verifyFormat("while ( (bool)1 )\n"
14085                "  continue;",
14086                Spaces);
14087   verifyFormat("for ( ;; )\n"
14088                "  continue;",
14089                Spaces);
14090   verifyFormat("if ( true )\n"
14091                "  f();\n"
14092                "else if ( true )\n"
14093                "  f();",
14094                Spaces);
14095   verifyFormat("do {\n"
14096                "  do_something( (int)i );\n"
14097                "} while ( something() );",
14098                Spaces);
14099   verifyFormat("switch ( x ) {\n"
14100                "default:\n"
14101                "  break;\n"
14102                "}",
14103                Spaces);
14104 
14105   Spaces.SpacesInParentheses = false;
14106   Spaces.SpacesInCStyleCastParentheses = true;
14107   verifyFormat("Type *A = ( Type * )P;", Spaces);
14108   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14109   verifyFormat("x = ( int32 )y;", Spaces);
14110   verifyFormat("int a = ( int )(2.0f);", Spaces);
14111   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14112   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14113   verifyFormat("#define x (( int )-1)", Spaces);
14114 
14115   // Run the first set of tests again with:
14116   Spaces.SpacesInParentheses = false;
14117   Spaces.SpaceInEmptyParentheses = true;
14118   Spaces.SpacesInCStyleCastParentheses = true;
14119   verifyFormat("call(x, y, z);", Spaces);
14120   verifyFormat("call( );", Spaces);
14121   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14122   verifyFormat("while (( bool )1)\n"
14123                "  continue;",
14124                Spaces);
14125   verifyFormat("for (;;)\n"
14126                "  continue;",
14127                Spaces);
14128   verifyFormat("if (true)\n"
14129                "  f( );\n"
14130                "else if (true)\n"
14131                "  f( );",
14132                Spaces);
14133   verifyFormat("do {\n"
14134                "  do_something(( int )i);\n"
14135                "} while (something( ));",
14136                Spaces);
14137   verifyFormat("switch (x) {\n"
14138                "default:\n"
14139                "  break;\n"
14140                "}",
14141                Spaces);
14142 
14143   // Run the first set of tests again with:
14144   Spaces.SpaceAfterCStyleCast = true;
14145   verifyFormat("call(x, y, z);", Spaces);
14146   verifyFormat("call( );", Spaces);
14147   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14148   verifyFormat("while (( bool ) 1)\n"
14149                "  continue;",
14150                Spaces);
14151   verifyFormat("for (;;)\n"
14152                "  continue;",
14153                Spaces);
14154   verifyFormat("if (true)\n"
14155                "  f( );\n"
14156                "else if (true)\n"
14157                "  f( );",
14158                Spaces);
14159   verifyFormat("do {\n"
14160                "  do_something(( int ) i);\n"
14161                "} while (something( ));",
14162                Spaces);
14163   verifyFormat("switch (x) {\n"
14164                "default:\n"
14165                "  break;\n"
14166                "}",
14167                Spaces);
14168 
14169   // Run subset of tests again with:
14170   Spaces.SpacesInCStyleCastParentheses = false;
14171   Spaces.SpaceAfterCStyleCast = true;
14172   verifyFormat("while ((bool) 1)\n"
14173                "  continue;",
14174                Spaces);
14175   verifyFormat("do {\n"
14176                "  do_something((int) i);\n"
14177                "} while (something( ));",
14178                Spaces);
14179 
14180   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14181   verifyFormat("size_t idx = (size_t) a;", Spaces);
14182   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14183   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14184   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14185   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14186   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14187   Spaces.ColumnLimit = 80;
14188   Spaces.IndentWidth = 4;
14189   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14190   verifyFormat("void foo( ) {\n"
14191                "    size_t foo = (*(function))(\n"
14192                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14193                "BarrrrrrrrrrrrLong,\n"
14194                "        FoooooooooLooooong);\n"
14195                "}",
14196                Spaces);
14197   Spaces.SpaceAfterCStyleCast = false;
14198   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
14199   verifyFormat("size_t idx = (size_t)a;", Spaces);
14200   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
14201   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14202   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14203   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14204   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14205 
14206   verifyFormat("void foo( ) {\n"
14207                "    size_t foo = (*(function))(\n"
14208                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14209                "BarrrrrrrrrrrrLong,\n"
14210                "        FoooooooooLooooong);\n"
14211                "}",
14212                Spaces);
14213 }
14214 
14215 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
14216   verifyFormat("int a[5];");
14217   verifyFormat("a[3] += 42;");
14218 
14219   FormatStyle Spaces = getLLVMStyle();
14220   Spaces.SpacesInSquareBrackets = true;
14221   // Not lambdas.
14222   verifyFormat("int a[ 5 ];", Spaces);
14223   verifyFormat("a[ 3 ] += 42;", Spaces);
14224   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
14225   verifyFormat("double &operator[](int i) { return 0; }\n"
14226                "int i;",
14227                Spaces);
14228   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
14229   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
14230   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
14231   // Lambdas.
14232   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
14233   verifyFormat("return [ i, args... ] {};", Spaces);
14234   verifyFormat("int foo = [ &bar ]() {};", Spaces);
14235   verifyFormat("int foo = [ = ]() {};", Spaces);
14236   verifyFormat("int foo = [ & ]() {};", Spaces);
14237   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
14238   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
14239 }
14240 
14241 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
14242   FormatStyle NoSpaceStyle = getLLVMStyle();
14243   verifyFormat("int a[5];", NoSpaceStyle);
14244   verifyFormat("a[3] += 42;", NoSpaceStyle);
14245 
14246   verifyFormat("int a[1];", NoSpaceStyle);
14247   verifyFormat("int 1 [a];", NoSpaceStyle);
14248   verifyFormat("int a[1][2];", NoSpaceStyle);
14249   verifyFormat("a[7] = 5;", NoSpaceStyle);
14250   verifyFormat("int a = (f())[23];", NoSpaceStyle);
14251   verifyFormat("f([] {})", NoSpaceStyle);
14252 
14253   FormatStyle Space = getLLVMStyle();
14254   Space.SpaceBeforeSquareBrackets = true;
14255   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
14256   verifyFormat("return [i, args...] {};", Space);
14257 
14258   verifyFormat("int a [5];", Space);
14259   verifyFormat("a [3] += 42;", Space);
14260   verifyFormat("constexpr char hello []{\"hello\"};", Space);
14261   verifyFormat("double &operator[](int i) { return 0; }\n"
14262                "int i;",
14263                Space);
14264   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
14265   verifyFormat("int i = a [a][a]->f();", Space);
14266   verifyFormat("int i = (*b) [a]->f();", Space);
14267 
14268   verifyFormat("int a [1];", Space);
14269   verifyFormat("int 1 [a];", Space);
14270   verifyFormat("int a [1][2];", Space);
14271   verifyFormat("a [7] = 5;", Space);
14272   verifyFormat("int a = (f()) [23];", Space);
14273   verifyFormat("f([] {})", Space);
14274 }
14275 
14276 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
14277   verifyFormat("int a = 5;");
14278   verifyFormat("a += 42;");
14279   verifyFormat("a or_eq 8;");
14280 
14281   FormatStyle Spaces = getLLVMStyle();
14282   Spaces.SpaceBeforeAssignmentOperators = false;
14283   verifyFormat("int a= 5;", Spaces);
14284   verifyFormat("a+= 42;", Spaces);
14285   verifyFormat("a or_eq 8;", Spaces);
14286 }
14287 
14288 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
14289   verifyFormat("class Foo : public Bar {};");
14290   verifyFormat("Foo::Foo() : foo(1) {}");
14291   verifyFormat("for (auto a : b) {\n}");
14292   verifyFormat("int x = a ? b : c;");
14293   verifyFormat("{\n"
14294                "label0:\n"
14295                "  int x = 0;\n"
14296                "}");
14297   verifyFormat("switch (x) {\n"
14298                "case 1:\n"
14299                "default:\n"
14300                "}");
14301   verifyFormat("switch (allBraces) {\n"
14302                "case 1: {\n"
14303                "  break;\n"
14304                "}\n"
14305                "case 2: {\n"
14306                "  [[fallthrough]];\n"
14307                "}\n"
14308                "default: {\n"
14309                "  break;\n"
14310                "}\n"
14311                "}");
14312 
14313   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
14314   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
14315   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
14316   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
14317   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
14318   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
14319   verifyFormat("{\n"
14320                "label1:\n"
14321                "  int x = 0;\n"
14322                "}",
14323                CtorInitializerStyle);
14324   verifyFormat("switch (x) {\n"
14325                "case 1:\n"
14326                "default:\n"
14327                "}",
14328                CtorInitializerStyle);
14329   verifyFormat("switch (allBraces) {\n"
14330                "case 1: {\n"
14331                "  break;\n"
14332                "}\n"
14333                "case 2: {\n"
14334                "  [[fallthrough]];\n"
14335                "}\n"
14336                "default: {\n"
14337                "  break;\n"
14338                "}\n"
14339                "}",
14340                CtorInitializerStyle);
14341   CtorInitializerStyle.BreakConstructorInitializers =
14342       FormatStyle::BCIS_AfterColon;
14343   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
14344                "    aaaaaaaaaaaaaaaa(1),\n"
14345                "    bbbbbbbbbbbbbbbb(2) {}",
14346                CtorInitializerStyle);
14347   CtorInitializerStyle.BreakConstructorInitializers =
14348       FormatStyle::BCIS_BeforeComma;
14349   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14350                "    : aaaaaaaaaaaaaaaa(1)\n"
14351                "    , bbbbbbbbbbbbbbbb(2) {}",
14352                CtorInitializerStyle);
14353   CtorInitializerStyle.BreakConstructorInitializers =
14354       FormatStyle::BCIS_BeforeColon;
14355   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14356                "    : aaaaaaaaaaaaaaaa(1),\n"
14357                "      bbbbbbbbbbbbbbbb(2) {}",
14358                CtorInitializerStyle);
14359   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
14360   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14361                ": aaaaaaaaaaaaaaaa(1),\n"
14362                "  bbbbbbbbbbbbbbbb(2) {}",
14363                CtorInitializerStyle);
14364 
14365   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
14366   InheritanceStyle.SpaceBeforeInheritanceColon = false;
14367   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
14368   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
14369   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
14370   verifyFormat("int x = a ? b : c;", InheritanceStyle);
14371   verifyFormat("{\n"
14372                "label2:\n"
14373                "  int x = 0;\n"
14374                "}",
14375                InheritanceStyle);
14376   verifyFormat("switch (x) {\n"
14377                "case 1:\n"
14378                "default:\n"
14379                "}",
14380                InheritanceStyle);
14381   verifyFormat("switch (allBraces) {\n"
14382                "case 1: {\n"
14383                "  break;\n"
14384                "}\n"
14385                "case 2: {\n"
14386                "  [[fallthrough]];\n"
14387                "}\n"
14388                "default: {\n"
14389                "  break;\n"
14390                "}\n"
14391                "}",
14392                InheritanceStyle);
14393   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
14394   verifyFormat("class Foooooooooooooooooooooo\n"
14395                "    : public aaaaaaaaaaaaaaaaaa,\n"
14396                "      public bbbbbbbbbbbbbbbbbb {\n"
14397                "}",
14398                InheritanceStyle);
14399   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
14400   verifyFormat("class Foooooooooooooooooooooo:\n"
14401                "    public aaaaaaaaaaaaaaaaaa,\n"
14402                "    public bbbbbbbbbbbbbbbbbb {\n"
14403                "}",
14404                InheritanceStyle);
14405   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
14406   verifyFormat("class Foooooooooooooooooooooo\n"
14407                "    : public aaaaaaaaaaaaaaaaaa\n"
14408                "    , public bbbbbbbbbbbbbbbbbb {\n"
14409                "}",
14410                InheritanceStyle);
14411   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
14412   verifyFormat("class Foooooooooooooooooooooo\n"
14413                "    : public aaaaaaaaaaaaaaaaaa,\n"
14414                "      public bbbbbbbbbbbbbbbbbb {\n"
14415                "}",
14416                InheritanceStyle);
14417   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
14418   verifyFormat("class Foooooooooooooooooooooo\n"
14419                ": public aaaaaaaaaaaaaaaaaa,\n"
14420                "  public bbbbbbbbbbbbbbbbbb {}",
14421                InheritanceStyle);
14422 
14423   FormatStyle ForLoopStyle = getLLVMStyle();
14424   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
14425   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
14426   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
14427   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
14428   verifyFormat("int x = a ? b : c;", ForLoopStyle);
14429   verifyFormat("{\n"
14430                "label2:\n"
14431                "  int x = 0;\n"
14432                "}",
14433                ForLoopStyle);
14434   verifyFormat("switch (x) {\n"
14435                "case 1:\n"
14436                "default:\n"
14437                "}",
14438                ForLoopStyle);
14439   verifyFormat("switch (allBraces) {\n"
14440                "case 1: {\n"
14441                "  break;\n"
14442                "}\n"
14443                "case 2: {\n"
14444                "  [[fallthrough]];\n"
14445                "}\n"
14446                "default: {\n"
14447                "  break;\n"
14448                "}\n"
14449                "}",
14450                ForLoopStyle);
14451 
14452   FormatStyle CaseStyle = getLLVMStyle();
14453   CaseStyle.SpaceBeforeCaseColon = true;
14454   verifyFormat("class Foo : public Bar {};", CaseStyle);
14455   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
14456   verifyFormat("for (auto a : b) {\n}", CaseStyle);
14457   verifyFormat("int x = a ? b : c;", CaseStyle);
14458   verifyFormat("switch (x) {\n"
14459                "case 1 :\n"
14460                "default :\n"
14461                "}",
14462                CaseStyle);
14463   verifyFormat("switch (allBraces) {\n"
14464                "case 1 : {\n"
14465                "  break;\n"
14466                "}\n"
14467                "case 2 : {\n"
14468                "  [[fallthrough]];\n"
14469                "}\n"
14470                "default : {\n"
14471                "  break;\n"
14472                "}\n"
14473                "}",
14474                CaseStyle);
14475 
14476   FormatStyle NoSpaceStyle = getLLVMStyle();
14477   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
14478   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14479   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
14480   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14481   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
14482   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
14483   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
14484   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
14485   verifyFormat("{\n"
14486                "label3:\n"
14487                "  int x = 0;\n"
14488                "}",
14489                NoSpaceStyle);
14490   verifyFormat("switch (x) {\n"
14491                "case 1:\n"
14492                "default:\n"
14493                "}",
14494                NoSpaceStyle);
14495   verifyFormat("switch (allBraces) {\n"
14496                "case 1: {\n"
14497                "  break;\n"
14498                "}\n"
14499                "case 2: {\n"
14500                "  [[fallthrough]];\n"
14501                "}\n"
14502                "default: {\n"
14503                "  break;\n"
14504                "}\n"
14505                "}",
14506                NoSpaceStyle);
14507 
14508   FormatStyle InvertedSpaceStyle = getLLVMStyle();
14509   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
14510   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14511   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
14512   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14513   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
14514   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
14515   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
14516   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
14517   verifyFormat("{\n"
14518                "label3:\n"
14519                "  int x = 0;\n"
14520                "}",
14521                InvertedSpaceStyle);
14522   verifyFormat("switch (x) {\n"
14523                "case 1 :\n"
14524                "case 2 : {\n"
14525                "  break;\n"
14526                "}\n"
14527                "default :\n"
14528                "  break;\n"
14529                "}",
14530                InvertedSpaceStyle);
14531   verifyFormat("switch (allBraces) {\n"
14532                "case 1 : {\n"
14533                "  break;\n"
14534                "}\n"
14535                "case 2 : {\n"
14536                "  [[fallthrough]];\n"
14537                "}\n"
14538                "default : {\n"
14539                "  break;\n"
14540                "}\n"
14541                "}",
14542                InvertedSpaceStyle);
14543 }
14544 
14545 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
14546   FormatStyle Style = getLLVMStyle();
14547 
14548   Style.PointerAlignment = FormatStyle::PAS_Left;
14549   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14550   verifyFormat("void* const* x = NULL;", Style);
14551 
14552 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
14553   do {                                                                         \
14554     Style.PointerAlignment = FormatStyle::Pointers;                            \
14555     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
14556     verifyFormat(Code, Style);                                                 \
14557   } while (false)
14558 
14559   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
14560   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
14561   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
14562 
14563   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
14564   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
14565   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
14566 
14567   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
14568   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
14569   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
14570 
14571   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
14572   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
14573   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
14574 
14575   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
14576   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14577                         SAPQ_Default);
14578   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14579                         SAPQ_Default);
14580 
14581   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
14582   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14583                         SAPQ_Before);
14584   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14585                         SAPQ_Before);
14586 
14587   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
14588   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
14589   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14590                         SAPQ_After);
14591 
14592   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
14593   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
14594   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
14595 
14596 #undef verifyQualifierSpaces
14597 
14598   FormatStyle Spaces = getLLVMStyle();
14599   Spaces.AttributeMacros.push_back("qualified");
14600   Spaces.PointerAlignment = FormatStyle::PAS_Right;
14601   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14602   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
14603   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
14604   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
14605   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
14606   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14607   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14608   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
14609   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
14610   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14611   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14612   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14613 
14614   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
14615   Spaces.PointerAlignment = FormatStyle::PAS_Left;
14616   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14617   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
14618   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
14619   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
14620   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
14621   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14622   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
14623   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14624   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
14625   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
14626   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
14627   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
14628   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14629 
14630   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
14631   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
14632   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14633   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
14634   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
14635   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14636   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14637   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14638 }
14639 
14640 TEST_F(FormatTest, AlignConsecutiveMacros) {
14641   FormatStyle Style = getLLVMStyle();
14642   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14643   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14644   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14645 
14646   verifyFormat("#define a 3\n"
14647                "#define bbbb 4\n"
14648                "#define ccc (5)",
14649                Style);
14650 
14651   verifyFormat("#define f(x) (x * x)\n"
14652                "#define fff(x, y, z) (x * y + z)\n"
14653                "#define ffff(x, y) (x - y)",
14654                Style);
14655 
14656   verifyFormat("#define foo(x, y) (x + y)\n"
14657                "#define bar (5, 6)(2 + 2)",
14658                Style);
14659 
14660   verifyFormat("#define a 3\n"
14661                "#define bbbb 4\n"
14662                "#define ccc (5)\n"
14663                "#define f(x) (x * x)\n"
14664                "#define fff(x, y, z) (x * y + z)\n"
14665                "#define ffff(x, y) (x - y)",
14666                Style);
14667 
14668   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14669   verifyFormat("#define a    3\n"
14670                "#define bbbb 4\n"
14671                "#define ccc  (5)",
14672                Style);
14673 
14674   verifyFormat("#define f(x)         (x * x)\n"
14675                "#define fff(x, y, z) (x * y + z)\n"
14676                "#define ffff(x, y)   (x - y)",
14677                Style);
14678 
14679   verifyFormat("#define foo(x, y) (x + y)\n"
14680                "#define bar       (5, 6)(2 + 2)",
14681                Style);
14682 
14683   verifyFormat("#define a            3\n"
14684                "#define bbbb         4\n"
14685                "#define ccc          (5)\n"
14686                "#define f(x)         (x * x)\n"
14687                "#define fff(x, y, z) (x * y + z)\n"
14688                "#define ffff(x, y)   (x - y)",
14689                Style);
14690 
14691   verifyFormat("#define a         5\n"
14692                "#define foo(x, y) (x + y)\n"
14693                "#define CCC       (6)\n"
14694                "auto lambda = []() {\n"
14695                "  auto  ii = 0;\n"
14696                "  float j  = 0;\n"
14697                "  return 0;\n"
14698                "};\n"
14699                "int   i  = 0;\n"
14700                "float i2 = 0;\n"
14701                "auto  v  = type{\n"
14702                "    i = 1,   //\n"
14703                "    (i = 2), //\n"
14704                "    i = 3    //\n"
14705                "};",
14706                Style);
14707 
14708   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14709   Style.ColumnLimit = 20;
14710 
14711   verifyFormat("#define a          \\\n"
14712                "  \"aabbbbbbbbbbbb\"\n"
14713                "#define D          \\\n"
14714                "  \"aabbbbbbbbbbbb\" \\\n"
14715                "  \"ccddeeeeeeeee\"\n"
14716                "#define B          \\\n"
14717                "  \"QQQQQQQQQQQQQ\"  \\\n"
14718                "  \"FFFFFFFFFFFFF\"  \\\n"
14719                "  \"LLLLLLLL\"\n",
14720                Style);
14721 
14722   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14723   verifyFormat("#define a          \\\n"
14724                "  \"aabbbbbbbbbbbb\"\n"
14725                "#define D          \\\n"
14726                "  \"aabbbbbbbbbbbb\" \\\n"
14727                "  \"ccddeeeeeeeee\"\n"
14728                "#define B          \\\n"
14729                "  \"QQQQQQQQQQQQQ\"  \\\n"
14730                "  \"FFFFFFFFFFFFF\"  \\\n"
14731                "  \"LLLLLLLL\"\n",
14732                Style);
14733 
14734   // Test across comments
14735   Style.MaxEmptyLinesToKeep = 10;
14736   Style.ReflowComments = false;
14737   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
14738   EXPECT_EQ("#define a    3\n"
14739             "// line comment\n"
14740             "#define bbbb 4\n"
14741             "#define ccc  (5)",
14742             format("#define a 3\n"
14743                    "// line comment\n"
14744                    "#define bbbb 4\n"
14745                    "#define ccc (5)",
14746                    Style));
14747 
14748   EXPECT_EQ("#define a    3\n"
14749             "/* block comment */\n"
14750             "#define bbbb 4\n"
14751             "#define ccc  (5)",
14752             format("#define a  3\n"
14753                    "/* block comment */\n"
14754                    "#define bbbb 4\n"
14755                    "#define ccc (5)",
14756                    Style));
14757 
14758   EXPECT_EQ("#define a    3\n"
14759             "/* multi-line *\n"
14760             " * block comment */\n"
14761             "#define bbbb 4\n"
14762             "#define ccc  (5)",
14763             format("#define a 3\n"
14764                    "/* multi-line *\n"
14765                    " * block comment */\n"
14766                    "#define bbbb 4\n"
14767                    "#define ccc (5)",
14768                    Style));
14769 
14770   EXPECT_EQ("#define a    3\n"
14771             "// multi-line line comment\n"
14772             "//\n"
14773             "#define bbbb 4\n"
14774             "#define ccc  (5)",
14775             format("#define a  3\n"
14776                    "// multi-line line comment\n"
14777                    "//\n"
14778                    "#define bbbb 4\n"
14779                    "#define ccc (5)",
14780                    Style));
14781 
14782   EXPECT_EQ("#define a 3\n"
14783             "// empty lines still break.\n"
14784             "\n"
14785             "#define bbbb 4\n"
14786             "#define ccc  (5)",
14787             format("#define a     3\n"
14788                    "// empty lines still break.\n"
14789                    "\n"
14790                    "#define bbbb     4\n"
14791                    "#define ccc  (5)",
14792                    Style));
14793 
14794   // Test across empty lines
14795   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
14796   EXPECT_EQ("#define a    3\n"
14797             "\n"
14798             "#define bbbb 4\n"
14799             "#define ccc  (5)",
14800             format("#define a 3\n"
14801                    "\n"
14802                    "#define bbbb 4\n"
14803                    "#define ccc (5)",
14804                    Style));
14805 
14806   EXPECT_EQ("#define a    3\n"
14807             "\n"
14808             "\n"
14809             "\n"
14810             "#define bbbb 4\n"
14811             "#define ccc  (5)",
14812             format("#define a        3\n"
14813                    "\n"
14814                    "\n"
14815                    "\n"
14816                    "#define bbbb 4\n"
14817                    "#define ccc (5)",
14818                    Style));
14819 
14820   EXPECT_EQ("#define a 3\n"
14821             "// comments should break alignment\n"
14822             "//\n"
14823             "#define bbbb 4\n"
14824             "#define ccc  (5)",
14825             format("#define a        3\n"
14826                    "// comments should break alignment\n"
14827                    "//\n"
14828                    "#define bbbb 4\n"
14829                    "#define ccc (5)",
14830                    Style));
14831 
14832   // Test across empty lines and comments
14833   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
14834   verifyFormat("#define a    3\n"
14835                "\n"
14836                "// line comment\n"
14837                "#define bbbb 4\n"
14838                "#define ccc  (5)",
14839                Style);
14840 
14841   EXPECT_EQ("#define a    3\n"
14842             "\n"
14843             "\n"
14844             "/* multi-line *\n"
14845             " * block comment */\n"
14846             "\n"
14847             "\n"
14848             "#define bbbb 4\n"
14849             "#define ccc  (5)",
14850             format("#define a 3\n"
14851                    "\n"
14852                    "\n"
14853                    "/* multi-line *\n"
14854                    " * block comment */\n"
14855                    "\n"
14856                    "\n"
14857                    "#define bbbb 4\n"
14858                    "#define ccc (5)",
14859                    Style));
14860 
14861   EXPECT_EQ("#define a    3\n"
14862             "\n"
14863             "\n"
14864             "/* multi-line *\n"
14865             " * block comment */\n"
14866             "\n"
14867             "\n"
14868             "#define bbbb 4\n"
14869             "#define ccc  (5)",
14870             format("#define a 3\n"
14871                    "\n"
14872                    "\n"
14873                    "/* multi-line *\n"
14874                    " * block comment */\n"
14875                    "\n"
14876                    "\n"
14877                    "#define bbbb 4\n"
14878                    "#define ccc       (5)",
14879                    Style));
14880 }
14881 
14882 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
14883   FormatStyle Alignment = getLLVMStyle();
14884   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14885   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
14886 
14887   Alignment.MaxEmptyLinesToKeep = 10;
14888   /* Test alignment across empty lines */
14889   EXPECT_EQ("int a           = 5;\n"
14890             "\n"
14891             "int oneTwoThree = 123;",
14892             format("int a       = 5;\n"
14893                    "\n"
14894                    "int oneTwoThree= 123;",
14895                    Alignment));
14896   EXPECT_EQ("int a           = 5;\n"
14897             "int one         = 1;\n"
14898             "\n"
14899             "int oneTwoThree = 123;",
14900             format("int a = 5;\n"
14901                    "int one = 1;\n"
14902                    "\n"
14903                    "int oneTwoThree = 123;",
14904                    Alignment));
14905   EXPECT_EQ("int a           = 5;\n"
14906             "int one         = 1;\n"
14907             "\n"
14908             "int oneTwoThree = 123;\n"
14909             "int oneTwo      = 12;",
14910             format("int a = 5;\n"
14911                    "int one = 1;\n"
14912                    "\n"
14913                    "int oneTwoThree = 123;\n"
14914                    "int oneTwo = 12;",
14915                    Alignment));
14916 
14917   /* Test across comments */
14918   EXPECT_EQ("int a = 5;\n"
14919             "/* block comment */\n"
14920             "int oneTwoThree = 123;",
14921             format("int a = 5;\n"
14922                    "/* block comment */\n"
14923                    "int oneTwoThree=123;",
14924                    Alignment));
14925 
14926   EXPECT_EQ("int a = 5;\n"
14927             "// line comment\n"
14928             "int oneTwoThree = 123;",
14929             format("int a = 5;\n"
14930                    "// line comment\n"
14931                    "int oneTwoThree=123;",
14932                    Alignment));
14933 
14934   /* Test across comments and newlines */
14935   EXPECT_EQ("int a = 5;\n"
14936             "\n"
14937             "/* block comment */\n"
14938             "int oneTwoThree = 123;",
14939             format("int a = 5;\n"
14940                    "\n"
14941                    "/* block comment */\n"
14942                    "int oneTwoThree=123;",
14943                    Alignment));
14944 
14945   EXPECT_EQ("int a = 5;\n"
14946             "\n"
14947             "// line comment\n"
14948             "int oneTwoThree = 123;",
14949             format("int a = 5;\n"
14950                    "\n"
14951                    "// line comment\n"
14952                    "int oneTwoThree=123;",
14953                    Alignment));
14954 }
14955 
14956 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
14957   FormatStyle Alignment = getLLVMStyle();
14958   Alignment.AlignConsecutiveDeclarations =
14959       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14960   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
14961 
14962   Alignment.MaxEmptyLinesToKeep = 10;
14963   /* Test alignment across empty lines */
14964   EXPECT_EQ("int         a = 5;\n"
14965             "\n"
14966             "float const oneTwoThree = 123;",
14967             format("int a = 5;\n"
14968                    "\n"
14969                    "float const oneTwoThree = 123;",
14970                    Alignment));
14971   EXPECT_EQ("int         a = 5;\n"
14972             "float const one = 1;\n"
14973             "\n"
14974             "int         oneTwoThree = 123;",
14975             format("int a = 5;\n"
14976                    "float const one = 1;\n"
14977                    "\n"
14978                    "int oneTwoThree = 123;",
14979                    Alignment));
14980 
14981   /* Test across comments */
14982   EXPECT_EQ("float const a = 5;\n"
14983             "/* block comment */\n"
14984             "int         oneTwoThree = 123;",
14985             format("float const a = 5;\n"
14986                    "/* block comment */\n"
14987                    "int oneTwoThree=123;",
14988                    Alignment));
14989 
14990   EXPECT_EQ("float const a = 5;\n"
14991             "// line comment\n"
14992             "int         oneTwoThree = 123;",
14993             format("float const a = 5;\n"
14994                    "// line comment\n"
14995                    "int oneTwoThree=123;",
14996                    Alignment));
14997 
14998   /* Test across comments and newlines */
14999   EXPECT_EQ("float const a = 5;\n"
15000             "\n"
15001             "/* block comment */\n"
15002             "int         oneTwoThree = 123;",
15003             format("float const a = 5;\n"
15004                    "\n"
15005                    "/* block comment */\n"
15006                    "int         oneTwoThree=123;",
15007                    Alignment));
15008 
15009   EXPECT_EQ("float const a = 5;\n"
15010             "\n"
15011             "// line comment\n"
15012             "int         oneTwoThree = 123;",
15013             format("float const a = 5;\n"
15014                    "\n"
15015                    "// line comment\n"
15016                    "int oneTwoThree=123;",
15017                    Alignment));
15018 }
15019 
15020 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15021   FormatStyle Alignment = getLLVMStyle();
15022   Alignment.AlignConsecutiveBitFields =
15023       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15024 
15025   Alignment.MaxEmptyLinesToKeep = 10;
15026   /* Test alignment across empty lines */
15027   EXPECT_EQ("int a            : 5;\n"
15028             "\n"
15029             "int longbitfield : 6;",
15030             format("int a : 5;\n"
15031                    "\n"
15032                    "int longbitfield : 6;",
15033                    Alignment));
15034   EXPECT_EQ("int a            : 5;\n"
15035             "int one          : 1;\n"
15036             "\n"
15037             "int longbitfield : 6;",
15038             format("int a : 5;\n"
15039                    "int one : 1;\n"
15040                    "\n"
15041                    "int longbitfield : 6;",
15042                    Alignment));
15043 
15044   /* Test across comments */
15045   EXPECT_EQ("int a            : 5;\n"
15046             "/* block comment */\n"
15047             "int longbitfield : 6;",
15048             format("int a : 5;\n"
15049                    "/* block comment */\n"
15050                    "int longbitfield : 6;",
15051                    Alignment));
15052   EXPECT_EQ("int a            : 5;\n"
15053             "int one          : 1;\n"
15054             "// line comment\n"
15055             "int longbitfield : 6;",
15056             format("int a : 5;\n"
15057                    "int one : 1;\n"
15058                    "// line comment\n"
15059                    "int longbitfield : 6;",
15060                    Alignment));
15061 
15062   /* Test across comments and newlines */
15063   EXPECT_EQ("int a            : 5;\n"
15064             "/* block comment */\n"
15065             "\n"
15066             "int longbitfield : 6;",
15067             format("int a : 5;\n"
15068                    "/* block comment */\n"
15069                    "\n"
15070                    "int longbitfield : 6;",
15071                    Alignment));
15072   EXPECT_EQ("int a            : 5;\n"
15073             "int one          : 1;\n"
15074             "\n"
15075             "// line comment\n"
15076             "\n"
15077             "int longbitfield : 6;",
15078             format("int a : 5;\n"
15079                    "int one : 1;\n"
15080                    "\n"
15081                    "// line comment \n"
15082                    "\n"
15083                    "int longbitfield : 6;",
15084                    Alignment));
15085 }
15086 
15087 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15088   FormatStyle Alignment = getLLVMStyle();
15089   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15090   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15091 
15092   Alignment.MaxEmptyLinesToKeep = 10;
15093   /* Test alignment across empty lines */
15094   EXPECT_EQ("int a = 5;\n"
15095             "\n"
15096             "int oneTwoThree = 123;",
15097             format("int a       = 5;\n"
15098                    "\n"
15099                    "int oneTwoThree= 123;",
15100                    Alignment));
15101   EXPECT_EQ("int a   = 5;\n"
15102             "int one = 1;\n"
15103             "\n"
15104             "int oneTwoThree = 123;",
15105             format("int a = 5;\n"
15106                    "int one = 1;\n"
15107                    "\n"
15108                    "int oneTwoThree = 123;",
15109                    Alignment));
15110 
15111   /* Test across comments */
15112   EXPECT_EQ("int a           = 5;\n"
15113             "/* block comment */\n"
15114             "int oneTwoThree = 123;",
15115             format("int a = 5;\n"
15116                    "/* block comment */\n"
15117                    "int oneTwoThree=123;",
15118                    Alignment));
15119 
15120   EXPECT_EQ("int a           = 5;\n"
15121             "// line comment\n"
15122             "int oneTwoThree = 123;",
15123             format("int a = 5;\n"
15124                    "// line comment\n"
15125                    "int oneTwoThree=123;",
15126                    Alignment));
15127 
15128   EXPECT_EQ("int a           = 5;\n"
15129             "/*\n"
15130             " * multi-line block comment\n"
15131             " */\n"
15132             "int oneTwoThree = 123;",
15133             format("int a = 5;\n"
15134                    "/*\n"
15135                    " * multi-line block comment\n"
15136                    " */\n"
15137                    "int oneTwoThree=123;",
15138                    Alignment));
15139 
15140   EXPECT_EQ("int a           = 5;\n"
15141             "//\n"
15142             "// multi-line line comment\n"
15143             "//\n"
15144             "int oneTwoThree = 123;",
15145             format("int a = 5;\n"
15146                    "//\n"
15147                    "// multi-line line comment\n"
15148                    "//\n"
15149                    "int oneTwoThree=123;",
15150                    Alignment));
15151 
15152   /* Test across comments and newlines */
15153   EXPECT_EQ("int a = 5;\n"
15154             "\n"
15155             "/* block comment */\n"
15156             "int oneTwoThree = 123;",
15157             format("int a = 5;\n"
15158                    "\n"
15159                    "/* block comment */\n"
15160                    "int oneTwoThree=123;",
15161                    Alignment));
15162 
15163   EXPECT_EQ("int a = 5;\n"
15164             "\n"
15165             "// line comment\n"
15166             "int oneTwoThree = 123;",
15167             format("int a = 5;\n"
15168                    "\n"
15169                    "// line comment\n"
15170                    "int oneTwoThree=123;",
15171                    Alignment));
15172 }
15173 
15174 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15175   FormatStyle Alignment = getLLVMStyle();
15176   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15177   Alignment.AlignConsecutiveAssignments =
15178       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15179   verifyFormat("int a           = 5;\n"
15180                "int oneTwoThree = 123;",
15181                Alignment);
15182   verifyFormat("int a           = method();\n"
15183                "int oneTwoThree = 133;",
15184                Alignment);
15185   verifyFormat("a &= 5;\n"
15186                "bcd *= 5;\n"
15187                "ghtyf += 5;\n"
15188                "dvfvdb -= 5;\n"
15189                "a /= 5;\n"
15190                "vdsvsv %= 5;\n"
15191                "sfdbddfbdfbb ^= 5;\n"
15192                "dvsdsv |= 5;\n"
15193                "int dsvvdvsdvvv = 123;",
15194                Alignment);
15195   verifyFormat("int i = 1, j = 10;\n"
15196                "something = 2000;",
15197                Alignment);
15198   verifyFormat("something = 2000;\n"
15199                "int i = 1, j = 10;\n",
15200                Alignment);
15201   verifyFormat("something = 2000;\n"
15202                "another   = 911;\n"
15203                "int i = 1, j = 10;\n"
15204                "oneMore = 1;\n"
15205                "i       = 2;",
15206                Alignment);
15207   verifyFormat("int a   = 5;\n"
15208                "int one = 1;\n"
15209                "method();\n"
15210                "int oneTwoThree = 123;\n"
15211                "int oneTwo      = 12;",
15212                Alignment);
15213   verifyFormat("int oneTwoThree = 123;\n"
15214                "int oneTwo      = 12;\n"
15215                "method();\n",
15216                Alignment);
15217   verifyFormat("int oneTwoThree = 123; // comment\n"
15218                "int oneTwo      = 12;  // comment",
15219                Alignment);
15220 
15221   // Bug 25167
15222   /* Uncomment when fixed
15223     verifyFormat("#if A\n"
15224                  "#else\n"
15225                  "int aaaaaaaa = 12;\n"
15226                  "#endif\n"
15227                  "#if B\n"
15228                  "#else\n"
15229                  "int a = 12;\n"
15230                  "#endif\n",
15231                  Alignment);
15232     verifyFormat("enum foo {\n"
15233                  "#if A\n"
15234                  "#else\n"
15235                  "  aaaaaaaa = 12;\n"
15236                  "#endif\n"
15237                  "#if B\n"
15238                  "#else\n"
15239                  "  a = 12;\n"
15240                  "#endif\n"
15241                  "};\n",
15242                  Alignment);
15243   */
15244 
15245   Alignment.MaxEmptyLinesToKeep = 10;
15246   /* Test alignment across empty lines */
15247   EXPECT_EQ("int a           = 5;\n"
15248             "\n"
15249             "int oneTwoThree = 123;",
15250             format("int a       = 5;\n"
15251                    "\n"
15252                    "int oneTwoThree= 123;",
15253                    Alignment));
15254   EXPECT_EQ("int a           = 5;\n"
15255             "int one         = 1;\n"
15256             "\n"
15257             "int oneTwoThree = 123;",
15258             format("int a = 5;\n"
15259                    "int one = 1;\n"
15260                    "\n"
15261                    "int oneTwoThree = 123;",
15262                    Alignment));
15263   EXPECT_EQ("int a           = 5;\n"
15264             "int one         = 1;\n"
15265             "\n"
15266             "int oneTwoThree = 123;\n"
15267             "int oneTwo      = 12;",
15268             format("int a = 5;\n"
15269                    "int one = 1;\n"
15270                    "\n"
15271                    "int oneTwoThree = 123;\n"
15272                    "int oneTwo = 12;",
15273                    Alignment));
15274 
15275   /* Test across comments */
15276   EXPECT_EQ("int a           = 5;\n"
15277             "/* block comment */\n"
15278             "int oneTwoThree = 123;",
15279             format("int a = 5;\n"
15280                    "/* block comment */\n"
15281                    "int oneTwoThree=123;",
15282                    Alignment));
15283 
15284   EXPECT_EQ("int a           = 5;\n"
15285             "// line comment\n"
15286             "int oneTwoThree = 123;",
15287             format("int a = 5;\n"
15288                    "// line comment\n"
15289                    "int oneTwoThree=123;",
15290                    Alignment));
15291 
15292   /* Test across comments and newlines */
15293   EXPECT_EQ("int a           = 5;\n"
15294             "\n"
15295             "/* block comment */\n"
15296             "int oneTwoThree = 123;",
15297             format("int a = 5;\n"
15298                    "\n"
15299                    "/* block comment */\n"
15300                    "int oneTwoThree=123;",
15301                    Alignment));
15302 
15303   EXPECT_EQ("int a           = 5;\n"
15304             "\n"
15305             "// line comment\n"
15306             "int oneTwoThree = 123;",
15307             format("int a = 5;\n"
15308                    "\n"
15309                    "// line comment\n"
15310                    "int oneTwoThree=123;",
15311                    Alignment));
15312 
15313   EXPECT_EQ("int a           = 5;\n"
15314             "//\n"
15315             "// multi-line line comment\n"
15316             "//\n"
15317             "int oneTwoThree = 123;",
15318             format("int a = 5;\n"
15319                    "//\n"
15320                    "// multi-line line comment\n"
15321                    "//\n"
15322                    "int oneTwoThree=123;",
15323                    Alignment));
15324 
15325   EXPECT_EQ("int a           = 5;\n"
15326             "/*\n"
15327             " *  multi-line block comment\n"
15328             " */\n"
15329             "int oneTwoThree = 123;",
15330             format("int a = 5;\n"
15331                    "/*\n"
15332                    " *  multi-line block comment\n"
15333                    " */\n"
15334                    "int oneTwoThree=123;",
15335                    Alignment));
15336 
15337   EXPECT_EQ("int a           = 5;\n"
15338             "\n"
15339             "/* block comment */\n"
15340             "\n"
15341             "\n"
15342             "\n"
15343             "int oneTwoThree = 123;",
15344             format("int a = 5;\n"
15345                    "\n"
15346                    "/* block comment */\n"
15347                    "\n"
15348                    "\n"
15349                    "\n"
15350                    "int oneTwoThree=123;",
15351                    Alignment));
15352 
15353   EXPECT_EQ("int a           = 5;\n"
15354             "\n"
15355             "// line comment\n"
15356             "\n"
15357             "\n"
15358             "\n"
15359             "int oneTwoThree = 123;",
15360             format("int a = 5;\n"
15361                    "\n"
15362                    "// line comment\n"
15363                    "\n"
15364                    "\n"
15365                    "\n"
15366                    "int oneTwoThree=123;",
15367                    Alignment));
15368 
15369   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15370   verifyFormat("#define A \\\n"
15371                "  int aaaa       = 12; \\\n"
15372                "  int b          = 23; \\\n"
15373                "  int ccc        = 234; \\\n"
15374                "  int dddddddddd = 2345;",
15375                Alignment);
15376   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15377   verifyFormat("#define A               \\\n"
15378                "  int aaaa       = 12;  \\\n"
15379                "  int b          = 23;  \\\n"
15380                "  int ccc        = 234; \\\n"
15381                "  int dddddddddd = 2345;",
15382                Alignment);
15383   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15384   verifyFormat("#define A                                                      "
15385                "                \\\n"
15386                "  int aaaa       = 12;                                         "
15387                "                \\\n"
15388                "  int b          = 23;                                         "
15389                "                \\\n"
15390                "  int ccc        = 234;                                        "
15391                "                \\\n"
15392                "  int dddddddddd = 2345;",
15393                Alignment);
15394   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15395                "k = 4, int l = 5,\n"
15396                "                  int m = 6) {\n"
15397                "  int j      = 10;\n"
15398                "  otherThing = 1;\n"
15399                "}",
15400                Alignment);
15401   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15402                "  int i   = 1;\n"
15403                "  int j   = 2;\n"
15404                "  int big = 10000;\n"
15405                "}",
15406                Alignment);
15407   verifyFormat("class C {\n"
15408                "public:\n"
15409                "  int i            = 1;\n"
15410                "  virtual void f() = 0;\n"
15411                "};",
15412                Alignment);
15413   verifyFormat("int i = 1;\n"
15414                "if (SomeType t = getSomething()) {\n"
15415                "}\n"
15416                "int j   = 2;\n"
15417                "int big = 10000;",
15418                Alignment);
15419   verifyFormat("int j = 7;\n"
15420                "for (int k = 0; k < N; ++k) {\n"
15421                "}\n"
15422                "int j   = 2;\n"
15423                "int big = 10000;\n"
15424                "}",
15425                Alignment);
15426   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15427   verifyFormat("int i = 1;\n"
15428                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15429                "    = someLooooooooooooooooongFunction();\n"
15430                "int j = 2;",
15431                Alignment);
15432   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15433   verifyFormat("int i = 1;\n"
15434                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15435                "    someLooooooooooooooooongFunction();\n"
15436                "int j = 2;",
15437                Alignment);
15438 
15439   verifyFormat("auto lambda = []() {\n"
15440                "  auto i = 0;\n"
15441                "  return 0;\n"
15442                "};\n"
15443                "int i  = 0;\n"
15444                "auto v = type{\n"
15445                "    i = 1,   //\n"
15446                "    (i = 2), //\n"
15447                "    i = 3    //\n"
15448                "};",
15449                Alignment);
15450 
15451   verifyFormat(
15452       "int i      = 1;\n"
15453       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15454       "                          loooooooooooooooooooooongParameterB);\n"
15455       "int j      = 2;",
15456       Alignment);
15457 
15458   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15459                "          typename B   = very_long_type_name_1,\n"
15460                "          typename T_2 = very_long_type_name_2>\n"
15461                "auto foo() {}\n",
15462                Alignment);
15463   verifyFormat("int a, b = 1;\n"
15464                "int c  = 2;\n"
15465                "int dd = 3;\n",
15466                Alignment);
15467   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15468                "float b[1][] = {{3.f}};\n",
15469                Alignment);
15470   verifyFormat("for (int i = 0; i < 1; i++)\n"
15471                "  int x = 1;\n",
15472                Alignment);
15473   verifyFormat("for (i = 0; i < 1; i++)\n"
15474                "  x = 1;\n"
15475                "y = 1;\n",
15476                Alignment);
15477 
15478   Alignment.ReflowComments = true;
15479   Alignment.ColumnLimit = 50;
15480   EXPECT_EQ("int x   = 0;\n"
15481             "int yy  = 1; /// specificlennospace\n"
15482             "int zzz = 2;\n",
15483             format("int x   = 0;\n"
15484                    "int yy  = 1; ///specificlennospace\n"
15485                    "int zzz = 2;\n",
15486                    Alignment));
15487 }
15488 
15489 TEST_F(FormatTest, AlignConsecutiveAssignments) {
15490   FormatStyle Alignment = getLLVMStyle();
15491   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15492   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15493   verifyFormat("int a = 5;\n"
15494                "int oneTwoThree = 123;",
15495                Alignment);
15496   verifyFormat("int a = 5;\n"
15497                "int oneTwoThree = 123;",
15498                Alignment);
15499 
15500   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15501   verifyFormat("int a           = 5;\n"
15502                "int oneTwoThree = 123;",
15503                Alignment);
15504   verifyFormat("int a           = method();\n"
15505                "int oneTwoThree = 133;",
15506                Alignment);
15507   verifyFormat("a &= 5;\n"
15508                "bcd *= 5;\n"
15509                "ghtyf += 5;\n"
15510                "dvfvdb -= 5;\n"
15511                "a /= 5;\n"
15512                "vdsvsv %= 5;\n"
15513                "sfdbddfbdfbb ^= 5;\n"
15514                "dvsdsv |= 5;\n"
15515                "int dsvvdvsdvvv = 123;",
15516                Alignment);
15517   verifyFormat("int i = 1, j = 10;\n"
15518                "something = 2000;",
15519                Alignment);
15520   verifyFormat("something = 2000;\n"
15521                "int i = 1, j = 10;\n",
15522                Alignment);
15523   verifyFormat("something = 2000;\n"
15524                "another   = 911;\n"
15525                "int i = 1, j = 10;\n"
15526                "oneMore = 1;\n"
15527                "i       = 2;",
15528                Alignment);
15529   verifyFormat("int a   = 5;\n"
15530                "int one = 1;\n"
15531                "method();\n"
15532                "int oneTwoThree = 123;\n"
15533                "int oneTwo      = 12;",
15534                Alignment);
15535   verifyFormat("int oneTwoThree = 123;\n"
15536                "int oneTwo      = 12;\n"
15537                "method();\n",
15538                Alignment);
15539   verifyFormat("int oneTwoThree = 123; // comment\n"
15540                "int oneTwo      = 12;  // comment",
15541                Alignment);
15542 
15543   // Bug 25167
15544   /* Uncomment when fixed
15545     verifyFormat("#if A\n"
15546                  "#else\n"
15547                  "int aaaaaaaa = 12;\n"
15548                  "#endif\n"
15549                  "#if B\n"
15550                  "#else\n"
15551                  "int a = 12;\n"
15552                  "#endif\n",
15553                  Alignment);
15554     verifyFormat("enum foo {\n"
15555                  "#if A\n"
15556                  "#else\n"
15557                  "  aaaaaaaa = 12;\n"
15558                  "#endif\n"
15559                  "#if B\n"
15560                  "#else\n"
15561                  "  a = 12;\n"
15562                  "#endif\n"
15563                  "};\n",
15564                  Alignment);
15565   */
15566 
15567   EXPECT_EQ("int a = 5;\n"
15568             "\n"
15569             "int oneTwoThree = 123;",
15570             format("int a       = 5;\n"
15571                    "\n"
15572                    "int oneTwoThree= 123;",
15573                    Alignment));
15574   EXPECT_EQ("int a   = 5;\n"
15575             "int one = 1;\n"
15576             "\n"
15577             "int oneTwoThree = 123;",
15578             format("int a = 5;\n"
15579                    "int one = 1;\n"
15580                    "\n"
15581                    "int oneTwoThree = 123;",
15582                    Alignment));
15583   EXPECT_EQ("int a   = 5;\n"
15584             "int one = 1;\n"
15585             "\n"
15586             "int oneTwoThree = 123;\n"
15587             "int oneTwo      = 12;",
15588             format("int a = 5;\n"
15589                    "int one = 1;\n"
15590                    "\n"
15591                    "int oneTwoThree = 123;\n"
15592                    "int oneTwo = 12;",
15593                    Alignment));
15594   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15595   verifyFormat("#define A \\\n"
15596                "  int aaaa       = 12; \\\n"
15597                "  int b          = 23; \\\n"
15598                "  int ccc        = 234; \\\n"
15599                "  int dddddddddd = 2345;",
15600                Alignment);
15601   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15602   verifyFormat("#define A               \\\n"
15603                "  int aaaa       = 12;  \\\n"
15604                "  int b          = 23;  \\\n"
15605                "  int ccc        = 234; \\\n"
15606                "  int dddddddddd = 2345;",
15607                Alignment);
15608   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15609   verifyFormat("#define A                                                      "
15610                "                \\\n"
15611                "  int aaaa       = 12;                                         "
15612                "                \\\n"
15613                "  int b          = 23;                                         "
15614                "                \\\n"
15615                "  int ccc        = 234;                                        "
15616                "                \\\n"
15617                "  int dddddddddd = 2345;",
15618                Alignment);
15619   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15620                "k = 4, int l = 5,\n"
15621                "                  int m = 6) {\n"
15622                "  int j      = 10;\n"
15623                "  otherThing = 1;\n"
15624                "}",
15625                Alignment);
15626   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15627                "  int i   = 1;\n"
15628                "  int j   = 2;\n"
15629                "  int big = 10000;\n"
15630                "}",
15631                Alignment);
15632   verifyFormat("class C {\n"
15633                "public:\n"
15634                "  int i            = 1;\n"
15635                "  virtual void f() = 0;\n"
15636                "};",
15637                Alignment);
15638   verifyFormat("int i = 1;\n"
15639                "if (SomeType t = getSomething()) {\n"
15640                "}\n"
15641                "int j   = 2;\n"
15642                "int big = 10000;",
15643                Alignment);
15644   verifyFormat("int j = 7;\n"
15645                "for (int k = 0; k < N; ++k) {\n"
15646                "}\n"
15647                "int j   = 2;\n"
15648                "int big = 10000;\n"
15649                "}",
15650                Alignment);
15651   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15652   verifyFormat("int i = 1;\n"
15653                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15654                "    = someLooooooooooooooooongFunction();\n"
15655                "int j = 2;",
15656                Alignment);
15657   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15658   verifyFormat("int i = 1;\n"
15659                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15660                "    someLooooooooooooooooongFunction();\n"
15661                "int j = 2;",
15662                Alignment);
15663 
15664   verifyFormat("auto lambda = []() {\n"
15665                "  auto i = 0;\n"
15666                "  return 0;\n"
15667                "};\n"
15668                "int i  = 0;\n"
15669                "auto v = type{\n"
15670                "    i = 1,   //\n"
15671                "    (i = 2), //\n"
15672                "    i = 3    //\n"
15673                "};",
15674                Alignment);
15675 
15676   verifyFormat(
15677       "int i      = 1;\n"
15678       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15679       "                          loooooooooooooooooooooongParameterB);\n"
15680       "int j      = 2;",
15681       Alignment);
15682 
15683   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15684                "          typename B   = very_long_type_name_1,\n"
15685                "          typename T_2 = very_long_type_name_2>\n"
15686                "auto foo() {}\n",
15687                Alignment);
15688   verifyFormat("int a, b = 1;\n"
15689                "int c  = 2;\n"
15690                "int dd = 3;\n",
15691                Alignment);
15692   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15693                "float b[1][] = {{3.f}};\n",
15694                Alignment);
15695   verifyFormat("for (int i = 0; i < 1; i++)\n"
15696                "  int x = 1;\n",
15697                Alignment);
15698   verifyFormat("for (i = 0; i < 1; i++)\n"
15699                "  x = 1;\n"
15700                "y = 1;\n",
15701                Alignment);
15702 
15703   Alignment.ReflowComments = true;
15704   Alignment.ColumnLimit = 50;
15705   EXPECT_EQ("int x   = 0;\n"
15706             "int yy  = 1; /// specificlennospace\n"
15707             "int zzz = 2;\n",
15708             format("int x   = 0;\n"
15709                    "int yy  = 1; ///specificlennospace\n"
15710                    "int zzz = 2;\n",
15711                    Alignment));
15712 }
15713 
15714 TEST_F(FormatTest, AlignConsecutiveBitFields) {
15715   FormatStyle Alignment = getLLVMStyle();
15716   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
15717   verifyFormat("int const a     : 5;\n"
15718                "int oneTwoThree : 23;",
15719                Alignment);
15720 
15721   // Initializers are allowed starting with c++2a
15722   verifyFormat("int const a     : 5 = 1;\n"
15723                "int oneTwoThree : 23 = 0;",
15724                Alignment);
15725 
15726   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15727   verifyFormat("int const a           : 5;\n"
15728                "int       oneTwoThree : 23;",
15729                Alignment);
15730 
15731   verifyFormat("int const a           : 5;  // comment\n"
15732                "int       oneTwoThree : 23; // comment",
15733                Alignment);
15734 
15735   verifyFormat("int const a           : 5 = 1;\n"
15736                "int       oneTwoThree : 23 = 0;",
15737                Alignment);
15738 
15739   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15740   verifyFormat("int const a           : 5  = 1;\n"
15741                "int       oneTwoThree : 23 = 0;",
15742                Alignment);
15743   verifyFormat("int const a           : 5  = {1};\n"
15744                "int       oneTwoThree : 23 = 0;",
15745                Alignment);
15746 
15747   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
15748   verifyFormat("int const a          :5;\n"
15749                "int       oneTwoThree:23;",
15750                Alignment);
15751 
15752   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
15753   verifyFormat("int const a           :5;\n"
15754                "int       oneTwoThree :23;",
15755                Alignment);
15756 
15757   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
15758   verifyFormat("int const a          : 5;\n"
15759                "int       oneTwoThree: 23;",
15760                Alignment);
15761 
15762   // Known limitations: ':' is only recognized as a bitfield colon when
15763   // followed by a number.
15764   /*
15765   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
15766                "int a           : 5;",
15767                Alignment);
15768   */
15769 }
15770 
15771 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
15772   FormatStyle Alignment = getLLVMStyle();
15773   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15774   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
15775   Alignment.PointerAlignment = FormatStyle::PAS_Right;
15776   verifyFormat("float const a = 5;\n"
15777                "int oneTwoThree = 123;",
15778                Alignment);
15779   verifyFormat("int a = 5;\n"
15780                "float const oneTwoThree = 123;",
15781                Alignment);
15782 
15783   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15784   verifyFormat("float const a = 5;\n"
15785                "int         oneTwoThree = 123;",
15786                Alignment);
15787   verifyFormat("int         a = method();\n"
15788                "float const oneTwoThree = 133;",
15789                Alignment);
15790   verifyFormat("int i = 1, j = 10;\n"
15791                "something = 2000;",
15792                Alignment);
15793   verifyFormat("something = 2000;\n"
15794                "int i = 1, j = 10;\n",
15795                Alignment);
15796   verifyFormat("float      something = 2000;\n"
15797                "double     another = 911;\n"
15798                "int        i = 1, j = 10;\n"
15799                "const int *oneMore = 1;\n"
15800                "unsigned   i = 2;",
15801                Alignment);
15802   verifyFormat("float a = 5;\n"
15803                "int   one = 1;\n"
15804                "method();\n"
15805                "const double       oneTwoThree = 123;\n"
15806                "const unsigned int oneTwo = 12;",
15807                Alignment);
15808   verifyFormat("int      oneTwoThree{0}; // comment\n"
15809                "unsigned oneTwo;         // comment",
15810                Alignment);
15811   verifyFormat("unsigned int       *a;\n"
15812                "int                *b;\n"
15813                "unsigned int Const *c;\n"
15814                "unsigned int const *d;\n"
15815                "unsigned int Const &e;\n"
15816                "unsigned int const &f;",
15817                Alignment);
15818   verifyFormat("Const unsigned int *c;\n"
15819                "const unsigned int *d;\n"
15820                "Const unsigned int &e;\n"
15821                "const unsigned int &f;\n"
15822                "const unsigned      g;\n"
15823                "Const unsigned      h;",
15824                Alignment);
15825   EXPECT_EQ("float const a = 5;\n"
15826             "\n"
15827             "int oneTwoThree = 123;",
15828             format("float const   a = 5;\n"
15829                    "\n"
15830                    "int           oneTwoThree= 123;",
15831                    Alignment));
15832   EXPECT_EQ("float a = 5;\n"
15833             "int   one = 1;\n"
15834             "\n"
15835             "unsigned oneTwoThree = 123;",
15836             format("float    a = 5;\n"
15837                    "int      one = 1;\n"
15838                    "\n"
15839                    "unsigned oneTwoThree = 123;",
15840                    Alignment));
15841   EXPECT_EQ("float a = 5;\n"
15842             "int   one = 1;\n"
15843             "\n"
15844             "unsigned oneTwoThree = 123;\n"
15845             "int      oneTwo = 12;",
15846             format("float    a = 5;\n"
15847                    "int one = 1;\n"
15848                    "\n"
15849                    "unsigned oneTwoThree = 123;\n"
15850                    "int oneTwo = 12;",
15851                    Alignment));
15852   // Function prototype alignment
15853   verifyFormat("int    a();\n"
15854                "double b();",
15855                Alignment);
15856   verifyFormat("int    a(int x);\n"
15857                "double b();",
15858                Alignment);
15859   unsigned OldColumnLimit = Alignment.ColumnLimit;
15860   // We need to set ColumnLimit to zero, in order to stress nested alignments,
15861   // otherwise the function parameters will be re-flowed onto a single line.
15862   Alignment.ColumnLimit = 0;
15863   EXPECT_EQ("int    a(int   x,\n"
15864             "         float y);\n"
15865             "double b(int    x,\n"
15866             "         double y);",
15867             format("int a(int x,\n"
15868                    " float y);\n"
15869                    "double b(int x,\n"
15870                    " double y);",
15871                    Alignment));
15872   // This ensures that function parameters of function declarations are
15873   // correctly indented when their owning functions are indented.
15874   // The failure case here is for 'double y' to not be indented enough.
15875   EXPECT_EQ("double a(int x);\n"
15876             "int    b(int    y,\n"
15877             "         double z);",
15878             format("double a(int x);\n"
15879                    "int b(int y,\n"
15880                    " double z);",
15881                    Alignment));
15882   // Set ColumnLimit low so that we induce wrapping immediately after
15883   // the function name and opening paren.
15884   Alignment.ColumnLimit = 13;
15885   verifyFormat("int function(\n"
15886                "    int  x,\n"
15887                "    bool y);",
15888                Alignment);
15889   Alignment.ColumnLimit = OldColumnLimit;
15890   // Ensure function pointers don't screw up recursive alignment
15891   verifyFormat("int    a(int x, void (*fp)(int y));\n"
15892                "double b();",
15893                Alignment);
15894   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15895   // Ensure recursive alignment is broken by function braces, so that the
15896   // "a = 1" does not align with subsequent assignments inside the function
15897   // body.
15898   verifyFormat("int func(int a = 1) {\n"
15899                "  int b  = 2;\n"
15900                "  int cc = 3;\n"
15901                "}",
15902                Alignment);
15903   verifyFormat("float      something = 2000;\n"
15904                "double     another   = 911;\n"
15905                "int        i = 1, j = 10;\n"
15906                "const int *oneMore = 1;\n"
15907                "unsigned   i       = 2;",
15908                Alignment);
15909   verifyFormat("int      oneTwoThree = {0}; // comment\n"
15910                "unsigned oneTwo      = 0;   // comment",
15911                Alignment);
15912   // Make sure that scope is correctly tracked, in the absence of braces
15913   verifyFormat("for (int i = 0; i < n; i++)\n"
15914                "  j = i;\n"
15915                "double x = 1;\n",
15916                Alignment);
15917   verifyFormat("if (int i = 0)\n"
15918                "  j = i;\n"
15919                "double x = 1;\n",
15920                Alignment);
15921   // Ensure operator[] and operator() are comprehended
15922   verifyFormat("struct test {\n"
15923                "  long long int foo();\n"
15924                "  int           operator[](int a);\n"
15925                "  double        bar();\n"
15926                "};\n",
15927                Alignment);
15928   verifyFormat("struct test {\n"
15929                "  long long int foo();\n"
15930                "  int           operator()(int a);\n"
15931                "  double        bar();\n"
15932                "};\n",
15933                Alignment);
15934 
15935   // PAS_Right
15936   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15937             "  int const i   = 1;\n"
15938             "  int      *j   = 2;\n"
15939             "  int       big = 10000;\n"
15940             "\n"
15941             "  unsigned oneTwoThree = 123;\n"
15942             "  int      oneTwo      = 12;\n"
15943             "  method();\n"
15944             "  float k  = 2;\n"
15945             "  int   ll = 10000;\n"
15946             "}",
15947             format("void SomeFunction(int parameter= 0) {\n"
15948                    " int const  i= 1;\n"
15949                    "  int *j=2;\n"
15950                    " int big  =  10000;\n"
15951                    "\n"
15952                    "unsigned oneTwoThree  =123;\n"
15953                    "int oneTwo = 12;\n"
15954                    "  method();\n"
15955                    "float k= 2;\n"
15956                    "int ll=10000;\n"
15957                    "}",
15958                    Alignment));
15959   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15960             "  int const i   = 1;\n"
15961             "  int     **j   = 2, ***k;\n"
15962             "  int      &k   = i;\n"
15963             "  int     &&l   = i + j;\n"
15964             "  int       big = 10000;\n"
15965             "\n"
15966             "  unsigned oneTwoThree = 123;\n"
15967             "  int      oneTwo      = 12;\n"
15968             "  method();\n"
15969             "  float k  = 2;\n"
15970             "  int   ll = 10000;\n"
15971             "}",
15972             format("void SomeFunction(int parameter= 0) {\n"
15973                    " int const  i= 1;\n"
15974                    "  int **j=2,***k;\n"
15975                    "int &k=i;\n"
15976                    "int &&l=i+j;\n"
15977                    " int big  =  10000;\n"
15978                    "\n"
15979                    "unsigned oneTwoThree  =123;\n"
15980                    "int oneTwo = 12;\n"
15981                    "  method();\n"
15982                    "float k= 2;\n"
15983                    "int ll=10000;\n"
15984                    "}",
15985                    Alignment));
15986   // variables are aligned at their name, pointers are at the right most
15987   // position
15988   verifyFormat("int   *a;\n"
15989                "int  **b;\n"
15990                "int ***c;\n"
15991                "int    foobar;\n",
15992                Alignment);
15993 
15994   // PAS_Left
15995   FormatStyle AlignmentLeft = Alignment;
15996   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
15997   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15998             "  int const i   = 1;\n"
15999             "  int*      j   = 2;\n"
16000             "  int       big = 10000;\n"
16001             "\n"
16002             "  unsigned oneTwoThree = 123;\n"
16003             "  int      oneTwo      = 12;\n"
16004             "  method();\n"
16005             "  float k  = 2;\n"
16006             "  int   ll = 10000;\n"
16007             "}",
16008             format("void SomeFunction(int parameter= 0) {\n"
16009                    " int const  i= 1;\n"
16010                    "  int *j=2;\n"
16011                    " int big  =  10000;\n"
16012                    "\n"
16013                    "unsigned oneTwoThree  =123;\n"
16014                    "int oneTwo = 12;\n"
16015                    "  method();\n"
16016                    "float k= 2;\n"
16017                    "int ll=10000;\n"
16018                    "}",
16019                    AlignmentLeft));
16020   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16021             "  int const i   = 1;\n"
16022             "  int**     j   = 2;\n"
16023             "  int&      k   = i;\n"
16024             "  int&&     l   = i + j;\n"
16025             "  int       big = 10000;\n"
16026             "\n"
16027             "  unsigned oneTwoThree = 123;\n"
16028             "  int      oneTwo      = 12;\n"
16029             "  method();\n"
16030             "  float k  = 2;\n"
16031             "  int   ll = 10000;\n"
16032             "}",
16033             format("void SomeFunction(int parameter= 0) {\n"
16034                    " int const  i= 1;\n"
16035                    "  int **j=2;\n"
16036                    "int &k=i;\n"
16037                    "int &&l=i+j;\n"
16038                    " int big  =  10000;\n"
16039                    "\n"
16040                    "unsigned oneTwoThree  =123;\n"
16041                    "int oneTwo = 12;\n"
16042                    "  method();\n"
16043                    "float k= 2;\n"
16044                    "int ll=10000;\n"
16045                    "}",
16046                    AlignmentLeft));
16047   // variables are aligned at their name, pointers are at the left most position
16048   verifyFormat("int*   a;\n"
16049                "int**  b;\n"
16050                "int*** c;\n"
16051                "int    foobar;\n",
16052                AlignmentLeft);
16053 
16054   // PAS_Middle
16055   FormatStyle AlignmentMiddle = Alignment;
16056   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16057   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16058             "  int const i   = 1;\n"
16059             "  int *     j   = 2;\n"
16060             "  int       big = 10000;\n"
16061             "\n"
16062             "  unsigned oneTwoThree = 123;\n"
16063             "  int      oneTwo      = 12;\n"
16064             "  method();\n"
16065             "  float k  = 2;\n"
16066             "  int   ll = 10000;\n"
16067             "}",
16068             format("void SomeFunction(int parameter= 0) {\n"
16069                    " int const  i= 1;\n"
16070                    "  int *j=2;\n"
16071                    " int big  =  10000;\n"
16072                    "\n"
16073                    "unsigned oneTwoThree  =123;\n"
16074                    "int oneTwo = 12;\n"
16075                    "  method();\n"
16076                    "float k= 2;\n"
16077                    "int ll=10000;\n"
16078                    "}",
16079                    AlignmentMiddle));
16080   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16081             "  int const i   = 1;\n"
16082             "  int **    j   = 2, ***k;\n"
16083             "  int &     k   = i;\n"
16084             "  int &&    l   = i + j;\n"
16085             "  int       big = 10000;\n"
16086             "\n"
16087             "  unsigned oneTwoThree = 123;\n"
16088             "  int      oneTwo      = 12;\n"
16089             "  method();\n"
16090             "  float k  = 2;\n"
16091             "  int   ll = 10000;\n"
16092             "}",
16093             format("void SomeFunction(int parameter= 0) {\n"
16094                    " int const  i= 1;\n"
16095                    "  int **j=2,***k;\n"
16096                    "int &k=i;\n"
16097                    "int &&l=i+j;\n"
16098                    " int big  =  10000;\n"
16099                    "\n"
16100                    "unsigned oneTwoThree  =123;\n"
16101                    "int oneTwo = 12;\n"
16102                    "  method();\n"
16103                    "float k= 2;\n"
16104                    "int ll=10000;\n"
16105                    "}",
16106                    AlignmentMiddle));
16107   // variables are aligned at their name, pointers are in the middle
16108   verifyFormat("int *   a;\n"
16109                "int *   b;\n"
16110                "int *** c;\n"
16111                "int     foobar;\n",
16112                AlignmentMiddle);
16113 
16114   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16115   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16116   verifyFormat("#define A \\\n"
16117                "  int       aaaa = 12; \\\n"
16118                "  float     b = 23; \\\n"
16119                "  const int ccc = 234; \\\n"
16120                "  unsigned  dddddddddd = 2345;",
16121                Alignment);
16122   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16123   verifyFormat("#define A              \\\n"
16124                "  int       aaaa = 12; \\\n"
16125                "  float     b = 23;    \\\n"
16126                "  const int ccc = 234; \\\n"
16127                "  unsigned  dddddddddd = 2345;",
16128                Alignment);
16129   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16130   Alignment.ColumnLimit = 30;
16131   verifyFormat("#define A                    \\\n"
16132                "  int       aaaa = 12;       \\\n"
16133                "  float     b = 23;          \\\n"
16134                "  const int ccc = 234;       \\\n"
16135                "  int       dddddddddd = 2345;",
16136                Alignment);
16137   Alignment.ColumnLimit = 80;
16138   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16139                "k = 4, int l = 5,\n"
16140                "                  int m = 6) {\n"
16141                "  const int j = 10;\n"
16142                "  otherThing = 1;\n"
16143                "}",
16144                Alignment);
16145   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16146                "  int const i = 1;\n"
16147                "  int      *j = 2;\n"
16148                "  int       big = 10000;\n"
16149                "}",
16150                Alignment);
16151   verifyFormat("class C {\n"
16152                "public:\n"
16153                "  int          i = 1;\n"
16154                "  virtual void f() = 0;\n"
16155                "};",
16156                Alignment);
16157   verifyFormat("float i = 1;\n"
16158                "if (SomeType t = getSomething()) {\n"
16159                "}\n"
16160                "const unsigned j = 2;\n"
16161                "int            big = 10000;",
16162                Alignment);
16163   verifyFormat("float j = 7;\n"
16164                "for (int k = 0; k < N; ++k) {\n"
16165                "}\n"
16166                "unsigned j = 2;\n"
16167                "int      big = 10000;\n"
16168                "}",
16169                Alignment);
16170   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16171   verifyFormat("float              i = 1;\n"
16172                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16173                "    = someLooooooooooooooooongFunction();\n"
16174                "int j = 2;",
16175                Alignment);
16176   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16177   verifyFormat("int                i = 1;\n"
16178                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16179                "    someLooooooooooooooooongFunction();\n"
16180                "int j = 2;",
16181                Alignment);
16182 
16183   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16184   verifyFormat("auto lambda = []() {\n"
16185                "  auto  ii = 0;\n"
16186                "  float j  = 0;\n"
16187                "  return 0;\n"
16188                "};\n"
16189                "int   i  = 0;\n"
16190                "float i2 = 0;\n"
16191                "auto  v  = type{\n"
16192                "    i = 1,   //\n"
16193                "    (i = 2), //\n"
16194                "    i = 3    //\n"
16195                "};",
16196                Alignment);
16197   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16198 
16199   verifyFormat(
16200       "int      i = 1;\n"
16201       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16202       "                          loooooooooooooooooooooongParameterB);\n"
16203       "int      j = 2;",
16204       Alignment);
16205 
16206   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
16207   // We expect declarations and assignments to align, as long as it doesn't
16208   // exceed the column limit, starting a new alignment sequence whenever it
16209   // happens.
16210   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16211   Alignment.ColumnLimit = 30;
16212   verifyFormat("float    ii              = 1;\n"
16213                "unsigned j               = 2;\n"
16214                "int someVerylongVariable = 1;\n"
16215                "AnotherLongType  ll = 123456;\n"
16216                "VeryVeryLongType k  = 2;\n"
16217                "int              myvar = 1;",
16218                Alignment);
16219   Alignment.ColumnLimit = 80;
16220   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16221 
16222   verifyFormat(
16223       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
16224       "          typename LongType, typename B>\n"
16225       "auto foo() {}\n",
16226       Alignment);
16227   verifyFormat("float a, b = 1;\n"
16228                "int   c = 2;\n"
16229                "int   dd = 3;\n",
16230                Alignment);
16231   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
16232                "float b[1][] = {{3.f}};\n",
16233                Alignment);
16234   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16235   verifyFormat("float a, b = 1;\n"
16236                "int   c  = 2;\n"
16237                "int   dd = 3;\n",
16238                Alignment);
16239   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
16240                "float b[1][] = {{3.f}};\n",
16241                Alignment);
16242   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16243 
16244   Alignment.ColumnLimit = 30;
16245   Alignment.BinPackParameters = false;
16246   verifyFormat("void foo(float     a,\n"
16247                "         float     b,\n"
16248                "         int       c,\n"
16249                "         uint32_t *d) {\n"
16250                "  int   *e = 0;\n"
16251                "  float  f = 0;\n"
16252                "  double g = 0;\n"
16253                "}\n"
16254                "void bar(ino_t     a,\n"
16255                "         int       b,\n"
16256                "         uint32_t *c,\n"
16257                "         bool      d) {}\n",
16258                Alignment);
16259   Alignment.BinPackParameters = true;
16260   Alignment.ColumnLimit = 80;
16261 
16262   // Bug 33507
16263   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16264   verifyFormat(
16265       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
16266       "  static const Version verVs2017;\n"
16267       "  return true;\n"
16268       "});\n",
16269       Alignment);
16270   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16271 
16272   // See llvm.org/PR35641
16273   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16274   verifyFormat("int func() { //\n"
16275                "  int      b;\n"
16276                "  unsigned c;\n"
16277                "}",
16278                Alignment);
16279 
16280   // See PR37175
16281   FormatStyle Style = getMozillaStyle();
16282   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16283   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
16284             "foo(int a);",
16285             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
16286 
16287   Alignment.PointerAlignment = FormatStyle::PAS_Left;
16288   verifyFormat("unsigned int*       a;\n"
16289                "int*                b;\n"
16290                "unsigned int Const* c;\n"
16291                "unsigned int const* d;\n"
16292                "unsigned int Const& e;\n"
16293                "unsigned int const& f;",
16294                Alignment);
16295   verifyFormat("Const unsigned int* c;\n"
16296                "const unsigned int* d;\n"
16297                "Const unsigned int& e;\n"
16298                "const unsigned int& f;\n"
16299                "const unsigned      g;\n"
16300                "Const unsigned      h;",
16301                Alignment);
16302 
16303   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16304   verifyFormat("unsigned int *       a;\n"
16305                "int *                b;\n"
16306                "unsigned int Const * c;\n"
16307                "unsigned int const * d;\n"
16308                "unsigned int Const & e;\n"
16309                "unsigned int const & f;",
16310                Alignment);
16311   verifyFormat("Const unsigned int * c;\n"
16312                "const unsigned int * d;\n"
16313                "Const unsigned int & e;\n"
16314                "const unsigned int & f;\n"
16315                "const unsigned       g;\n"
16316                "Const unsigned       h;",
16317                Alignment);
16318 }
16319 
16320 TEST_F(FormatTest, AlignWithLineBreaks) {
16321   auto Style = getLLVMStyleWithColumns(120);
16322 
16323   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
16324   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16325   verifyFormat("void foo() {\n"
16326                "  int myVar = 5;\n"
16327                "  double x = 3.14;\n"
16328                "  auto str = \"Hello \"\n"
16329                "             \"World\";\n"
16330                "  auto s = \"Hello \"\n"
16331                "           \"Again\";\n"
16332                "}",
16333                Style);
16334 
16335   // clang-format off
16336   verifyFormat("void foo() {\n"
16337                "  const int capacityBefore = Entries.capacity();\n"
16338                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16339                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16340                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16341                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16342                "}",
16343                Style);
16344   // clang-format on
16345 
16346   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16347   verifyFormat("void foo() {\n"
16348                "  int myVar = 5;\n"
16349                "  double x  = 3.14;\n"
16350                "  auto str  = \"Hello \"\n"
16351                "              \"World\";\n"
16352                "  auto s    = \"Hello \"\n"
16353                "              \"Again\";\n"
16354                "}",
16355                Style);
16356 
16357   // clang-format off
16358   verifyFormat("void foo() {\n"
16359                "  const int capacityBefore = Entries.capacity();\n"
16360                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16361                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16362                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16363                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16364                "}",
16365                Style);
16366   // clang-format on
16367 
16368   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16369   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16370   verifyFormat("void foo() {\n"
16371                "  int    myVar = 5;\n"
16372                "  double x = 3.14;\n"
16373                "  auto   str = \"Hello \"\n"
16374                "               \"World\";\n"
16375                "  auto   s = \"Hello \"\n"
16376                "             \"Again\";\n"
16377                "}",
16378                Style);
16379 
16380   // clang-format off
16381   verifyFormat("void foo() {\n"
16382                "  const int  capacityBefore = Entries.capacity();\n"
16383                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16384                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16385                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16386                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16387                "}",
16388                Style);
16389   // clang-format on
16390 
16391   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16392   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16393 
16394   verifyFormat("void foo() {\n"
16395                "  int    myVar = 5;\n"
16396                "  double x     = 3.14;\n"
16397                "  auto   str   = \"Hello \"\n"
16398                "                 \"World\";\n"
16399                "  auto   s     = \"Hello \"\n"
16400                "                 \"Again\";\n"
16401                "}",
16402                Style);
16403 
16404   // clang-format off
16405   verifyFormat("void foo() {\n"
16406                "  const int  capacityBefore = Entries.capacity();\n"
16407                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16408                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16409                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16410                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16411                "}",
16412                Style);
16413   // clang-format on
16414 }
16415 
16416 TEST_F(FormatTest, AlignWithInitializerPeriods) {
16417   auto Style = getLLVMStyleWithColumns(60);
16418 
16419   verifyFormat("void foo1(void) {\n"
16420                "  BYTE p[1] = 1;\n"
16421                "  A B = {.one_foooooooooooooooo = 2,\n"
16422                "         .two_fooooooooooooo = 3,\n"
16423                "         .three_fooooooooooooo = 4};\n"
16424                "  BYTE payload = 2;\n"
16425                "}",
16426                Style);
16427 
16428   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16429   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16430   verifyFormat("void foo2(void) {\n"
16431                "  BYTE p[1]    = 1;\n"
16432                "  A B          = {.one_foooooooooooooooo = 2,\n"
16433                "                  .two_fooooooooooooo    = 3,\n"
16434                "                  .three_fooooooooooooo  = 4};\n"
16435                "  BYTE payload = 2;\n"
16436                "}",
16437                Style);
16438 
16439   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16440   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16441   verifyFormat("void foo3(void) {\n"
16442                "  BYTE p[1] = 1;\n"
16443                "  A    B = {.one_foooooooooooooooo = 2,\n"
16444                "            .two_fooooooooooooo = 3,\n"
16445                "            .three_fooooooooooooo = 4};\n"
16446                "  BYTE payload = 2;\n"
16447                "}",
16448                Style);
16449 
16450   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16451   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16452   verifyFormat("void foo4(void) {\n"
16453                "  BYTE p[1]    = 1;\n"
16454                "  A    B       = {.one_foooooooooooooooo = 2,\n"
16455                "                  .two_fooooooooooooo    = 3,\n"
16456                "                  .three_fooooooooooooo  = 4};\n"
16457                "  BYTE payload = 2;\n"
16458                "}",
16459                Style);
16460 }
16461 
16462 TEST_F(FormatTest, LinuxBraceBreaking) {
16463   FormatStyle LinuxBraceStyle = getLLVMStyle();
16464   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
16465   verifyFormat("namespace a\n"
16466                "{\n"
16467                "class A\n"
16468                "{\n"
16469                "  void f()\n"
16470                "  {\n"
16471                "    if (true) {\n"
16472                "      a();\n"
16473                "      b();\n"
16474                "    } else {\n"
16475                "      a();\n"
16476                "    }\n"
16477                "  }\n"
16478                "  void g() { return; }\n"
16479                "};\n"
16480                "struct B {\n"
16481                "  int x;\n"
16482                "};\n"
16483                "} // namespace a\n",
16484                LinuxBraceStyle);
16485   verifyFormat("enum X {\n"
16486                "  Y = 0,\n"
16487                "}\n",
16488                LinuxBraceStyle);
16489   verifyFormat("struct S {\n"
16490                "  int Type;\n"
16491                "  union {\n"
16492                "    int x;\n"
16493                "    double y;\n"
16494                "  } Value;\n"
16495                "  class C\n"
16496                "  {\n"
16497                "    MyFavoriteType Value;\n"
16498                "  } Class;\n"
16499                "}\n",
16500                LinuxBraceStyle);
16501 }
16502 
16503 TEST_F(FormatTest, MozillaBraceBreaking) {
16504   FormatStyle MozillaBraceStyle = getLLVMStyle();
16505   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
16506   MozillaBraceStyle.FixNamespaceComments = false;
16507   verifyFormat("namespace a {\n"
16508                "class A\n"
16509                "{\n"
16510                "  void f()\n"
16511                "  {\n"
16512                "    if (true) {\n"
16513                "      a();\n"
16514                "      b();\n"
16515                "    }\n"
16516                "  }\n"
16517                "  void g() { return; }\n"
16518                "};\n"
16519                "enum E\n"
16520                "{\n"
16521                "  A,\n"
16522                "  // foo\n"
16523                "  B,\n"
16524                "  C\n"
16525                "};\n"
16526                "struct B\n"
16527                "{\n"
16528                "  int x;\n"
16529                "};\n"
16530                "}\n",
16531                MozillaBraceStyle);
16532   verifyFormat("struct S\n"
16533                "{\n"
16534                "  int Type;\n"
16535                "  union\n"
16536                "  {\n"
16537                "    int x;\n"
16538                "    double y;\n"
16539                "  } Value;\n"
16540                "  class C\n"
16541                "  {\n"
16542                "    MyFavoriteType Value;\n"
16543                "  } Class;\n"
16544                "}\n",
16545                MozillaBraceStyle);
16546 }
16547 
16548 TEST_F(FormatTest, StroustrupBraceBreaking) {
16549   FormatStyle StroustrupBraceStyle = getLLVMStyle();
16550   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
16551   verifyFormat("namespace a {\n"
16552                "class A {\n"
16553                "  void f()\n"
16554                "  {\n"
16555                "    if (true) {\n"
16556                "      a();\n"
16557                "      b();\n"
16558                "    }\n"
16559                "  }\n"
16560                "  void g() { return; }\n"
16561                "};\n"
16562                "struct B {\n"
16563                "  int x;\n"
16564                "};\n"
16565                "} // namespace a\n",
16566                StroustrupBraceStyle);
16567 
16568   verifyFormat("void foo()\n"
16569                "{\n"
16570                "  if (a) {\n"
16571                "    a();\n"
16572                "  }\n"
16573                "  else {\n"
16574                "    b();\n"
16575                "  }\n"
16576                "}\n",
16577                StroustrupBraceStyle);
16578 
16579   verifyFormat("#ifdef _DEBUG\n"
16580                "int foo(int i = 0)\n"
16581                "#else\n"
16582                "int foo(int i = 5)\n"
16583                "#endif\n"
16584                "{\n"
16585                "  return i;\n"
16586                "}",
16587                StroustrupBraceStyle);
16588 
16589   verifyFormat("void foo() {}\n"
16590                "void bar()\n"
16591                "#ifdef _DEBUG\n"
16592                "{\n"
16593                "  foo();\n"
16594                "}\n"
16595                "#else\n"
16596                "{\n"
16597                "}\n"
16598                "#endif",
16599                StroustrupBraceStyle);
16600 
16601   verifyFormat("void foobar() { int i = 5; }\n"
16602                "#ifdef _DEBUG\n"
16603                "void bar() {}\n"
16604                "#else\n"
16605                "void bar() { foobar(); }\n"
16606                "#endif",
16607                StroustrupBraceStyle);
16608 }
16609 
16610 TEST_F(FormatTest, AllmanBraceBreaking) {
16611   FormatStyle AllmanBraceStyle = getLLVMStyle();
16612   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
16613 
16614   EXPECT_EQ("namespace a\n"
16615             "{\n"
16616             "void f();\n"
16617             "void g();\n"
16618             "} // namespace a\n",
16619             format("namespace a\n"
16620                    "{\n"
16621                    "void f();\n"
16622                    "void g();\n"
16623                    "}\n",
16624                    AllmanBraceStyle));
16625 
16626   verifyFormat("namespace a\n"
16627                "{\n"
16628                "class A\n"
16629                "{\n"
16630                "  void f()\n"
16631                "  {\n"
16632                "    if (true)\n"
16633                "    {\n"
16634                "      a();\n"
16635                "      b();\n"
16636                "    }\n"
16637                "  }\n"
16638                "  void g() { return; }\n"
16639                "};\n"
16640                "struct B\n"
16641                "{\n"
16642                "  int x;\n"
16643                "};\n"
16644                "union C\n"
16645                "{\n"
16646                "};\n"
16647                "} // namespace a",
16648                AllmanBraceStyle);
16649 
16650   verifyFormat("void f()\n"
16651                "{\n"
16652                "  if (true)\n"
16653                "  {\n"
16654                "    a();\n"
16655                "  }\n"
16656                "  else if (false)\n"
16657                "  {\n"
16658                "    b();\n"
16659                "  }\n"
16660                "  else\n"
16661                "  {\n"
16662                "    c();\n"
16663                "  }\n"
16664                "}\n",
16665                AllmanBraceStyle);
16666 
16667   verifyFormat("void f()\n"
16668                "{\n"
16669                "  for (int i = 0; i < 10; ++i)\n"
16670                "  {\n"
16671                "    a();\n"
16672                "  }\n"
16673                "  while (false)\n"
16674                "  {\n"
16675                "    b();\n"
16676                "  }\n"
16677                "  do\n"
16678                "  {\n"
16679                "    c();\n"
16680                "  } while (false)\n"
16681                "}\n",
16682                AllmanBraceStyle);
16683 
16684   verifyFormat("void f(int a)\n"
16685                "{\n"
16686                "  switch (a)\n"
16687                "  {\n"
16688                "  case 0:\n"
16689                "    break;\n"
16690                "  case 1:\n"
16691                "  {\n"
16692                "    break;\n"
16693                "  }\n"
16694                "  case 2:\n"
16695                "  {\n"
16696                "  }\n"
16697                "  break;\n"
16698                "  default:\n"
16699                "    break;\n"
16700                "  }\n"
16701                "}\n",
16702                AllmanBraceStyle);
16703 
16704   verifyFormat("enum X\n"
16705                "{\n"
16706                "  Y = 0,\n"
16707                "}\n",
16708                AllmanBraceStyle);
16709   verifyFormat("enum X\n"
16710                "{\n"
16711                "  Y = 0\n"
16712                "}\n",
16713                AllmanBraceStyle);
16714 
16715   verifyFormat("@interface BSApplicationController ()\n"
16716                "{\n"
16717                "@private\n"
16718                "  id _extraIvar;\n"
16719                "}\n"
16720                "@end\n",
16721                AllmanBraceStyle);
16722 
16723   verifyFormat("#ifdef _DEBUG\n"
16724                "int foo(int i = 0)\n"
16725                "#else\n"
16726                "int foo(int i = 5)\n"
16727                "#endif\n"
16728                "{\n"
16729                "  return i;\n"
16730                "}",
16731                AllmanBraceStyle);
16732 
16733   verifyFormat("void foo() {}\n"
16734                "void bar()\n"
16735                "#ifdef _DEBUG\n"
16736                "{\n"
16737                "  foo();\n"
16738                "}\n"
16739                "#else\n"
16740                "{\n"
16741                "}\n"
16742                "#endif",
16743                AllmanBraceStyle);
16744 
16745   verifyFormat("void foobar() { int i = 5; }\n"
16746                "#ifdef _DEBUG\n"
16747                "void bar() {}\n"
16748                "#else\n"
16749                "void bar() { foobar(); }\n"
16750                "#endif",
16751                AllmanBraceStyle);
16752 
16753   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
16754             FormatStyle::SLS_All);
16755 
16756   verifyFormat("[](int i) { return i + 2; };\n"
16757                "[](int i, int j)\n"
16758                "{\n"
16759                "  auto x = i + j;\n"
16760                "  auto y = i * j;\n"
16761                "  return x ^ y;\n"
16762                "};\n"
16763                "void foo()\n"
16764                "{\n"
16765                "  auto shortLambda = [](int i) { return i + 2; };\n"
16766                "  auto longLambda = [](int i, int j)\n"
16767                "  {\n"
16768                "    auto x = i + j;\n"
16769                "    auto y = i * j;\n"
16770                "    return x ^ y;\n"
16771                "  };\n"
16772                "}",
16773                AllmanBraceStyle);
16774 
16775   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
16776 
16777   verifyFormat("[](int i)\n"
16778                "{\n"
16779                "  return i + 2;\n"
16780                "};\n"
16781                "[](int i, int j)\n"
16782                "{\n"
16783                "  auto x = i + j;\n"
16784                "  auto y = i * j;\n"
16785                "  return x ^ y;\n"
16786                "};\n"
16787                "void foo()\n"
16788                "{\n"
16789                "  auto shortLambda = [](int i)\n"
16790                "  {\n"
16791                "    return i + 2;\n"
16792                "  };\n"
16793                "  auto longLambda = [](int i, int j)\n"
16794                "  {\n"
16795                "    auto x = i + j;\n"
16796                "    auto y = i * j;\n"
16797                "    return x ^ y;\n"
16798                "  };\n"
16799                "}",
16800                AllmanBraceStyle);
16801 
16802   // Reset
16803   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
16804 
16805   // This shouldn't affect ObjC blocks..
16806   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
16807                "  // ...\n"
16808                "  int i;\n"
16809                "}];",
16810                AllmanBraceStyle);
16811   verifyFormat("void (^block)(void) = ^{\n"
16812                "  // ...\n"
16813                "  int i;\n"
16814                "};",
16815                AllmanBraceStyle);
16816   // .. or dict literals.
16817   verifyFormat("void f()\n"
16818                "{\n"
16819                "  // ...\n"
16820                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
16821                "}",
16822                AllmanBraceStyle);
16823   verifyFormat("void f()\n"
16824                "{\n"
16825                "  // ...\n"
16826                "  [object someMethod:@{a : @\"b\"}];\n"
16827                "}",
16828                AllmanBraceStyle);
16829   verifyFormat("int f()\n"
16830                "{ // comment\n"
16831                "  return 42;\n"
16832                "}",
16833                AllmanBraceStyle);
16834 
16835   AllmanBraceStyle.ColumnLimit = 19;
16836   verifyFormat("void f() { int i; }", AllmanBraceStyle);
16837   AllmanBraceStyle.ColumnLimit = 18;
16838   verifyFormat("void f()\n"
16839                "{\n"
16840                "  int i;\n"
16841                "}",
16842                AllmanBraceStyle);
16843   AllmanBraceStyle.ColumnLimit = 80;
16844 
16845   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
16846   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
16847       FormatStyle::SIS_WithoutElse;
16848   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
16849   verifyFormat("void f(bool b)\n"
16850                "{\n"
16851                "  if (b)\n"
16852                "  {\n"
16853                "    return;\n"
16854                "  }\n"
16855                "}\n",
16856                BreakBeforeBraceShortIfs);
16857   verifyFormat("void f(bool b)\n"
16858                "{\n"
16859                "  if constexpr (b)\n"
16860                "  {\n"
16861                "    return;\n"
16862                "  }\n"
16863                "}\n",
16864                BreakBeforeBraceShortIfs);
16865   verifyFormat("void f(bool b)\n"
16866                "{\n"
16867                "  if CONSTEXPR (b)\n"
16868                "  {\n"
16869                "    return;\n"
16870                "  }\n"
16871                "}\n",
16872                BreakBeforeBraceShortIfs);
16873   verifyFormat("void f(bool b)\n"
16874                "{\n"
16875                "  if (b) return;\n"
16876                "}\n",
16877                BreakBeforeBraceShortIfs);
16878   verifyFormat("void f(bool b)\n"
16879                "{\n"
16880                "  if constexpr (b) return;\n"
16881                "}\n",
16882                BreakBeforeBraceShortIfs);
16883   verifyFormat("void f(bool b)\n"
16884                "{\n"
16885                "  if CONSTEXPR (b) return;\n"
16886                "}\n",
16887                BreakBeforeBraceShortIfs);
16888   verifyFormat("void f(bool b)\n"
16889                "{\n"
16890                "  while (b)\n"
16891                "  {\n"
16892                "    return;\n"
16893                "  }\n"
16894                "}\n",
16895                BreakBeforeBraceShortIfs);
16896 }
16897 
16898 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
16899   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
16900   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
16901 
16902   // Make a few changes to the style for testing purposes
16903   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
16904       FormatStyle::SFS_Empty;
16905   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
16906   WhitesmithsBraceStyle.ColumnLimit = 0;
16907 
16908   // FIXME: this test case can't decide whether there should be a blank line
16909   // after the ~D() line or not. It adds one if one doesn't exist in the test
16910   // and it removes the line if one exists.
16911   /*
16912   verifyFormat("class A;\n"
16913                "namespace B\n"
16914                "  {\n"
16915                "class C;\n"
16916                "// Comment\n"
16917                "class D\n"
16918                "  {\n"
16919                "public:\n"
16920                "  D();\n"
16921                "  ~D() {}\n"
16922                "private:\n"
16923                "  enum E\n"
16924                "    {\n"
16925                "    F\n"
16926                "    }\n"
16927                "  };\n"
16928                "  } // namespace B\n",
16929                WhitesmithsBraceStyle);
16930   */
16931 
16932   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
16933   verifyFormat("namespace a\n"
16934                "  {\n"
16935                "class A\n"
16936                "  {\n"
16937                "  void f()\n"
16938                "    {\n"
16939                "    if (true)\n"
16940                "      {\n"
16941                "      a();\n"
16942                "      b();\n"
16943                "      }\n"
16944                "    }\n"
16945                "  void g()\n"
16946                "    {\n"
16947                "    return;\n"
16948                "    }\n"
16949                "  };\n"
16950                "struct B\n"
16951                "  {\n"
16952                "  int x;\n"
16953                "  };\n"
16954                "  } // namespace a",
16955                WhitesmithsBraceStyle);
16956 
16957   verifyFormat("namespace a\n"
16958                "  {\n"
16959                "namespace b\n"
16960                "  {\n"
16961                "class A\n"
16962                "  {\n"
16963                "  void f()\n"
16964                "    {\n"
16965                "    if (true)\n"
16966                "      {\n"
16967                "      a();\n"
16968                "      b();\n"
16969                "      }\n"
16970                "    }\n"
16971                "  void g()\n"
16972                "    {\n"
16973                "    return;\n"
16974                "    }\n"
16975                "  };\n"
16976                "struct B\n"
16977                "  {\n"
16978                "  int x;\n"
16979                "  };\n"
16980                "  } // namespace b\n"
16981                "  } // namespace a",
16982                WhitesmithsBraceStyle);
16983 
16984   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
16985   verifyFormat("namespace a\n"
16986                "  {\n"
16987                "namespace b\n"
16988                "  {\n"
16989                "  class A\n"
16990                "    {\n"
16991                "    void f()\n"
16992                "      {\n"
16993                "      if (true)\n"
16994                "        {\n"
16995                "        a();\n"
16996                "        b();\n"
16997                "        }\n"
16998                "      }\n"
16999                "    void g()\n"
17000                "      {\n"
17001                "      return;\n"
17002                "      }\n"
17003                "    };\n"
17004                "  struct B\n"
17005                "    {\n"
17006                "    int x;\n"
17007                "    };\n"
17008                "  } // namespace b\n"
17009                "  } // namespace a",
17010                WhitesmithsBraceStyle);
17011 
17012   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17013   verifyFormat("namespace a\n"
17014                "  {\n"
17015                "  namespace b\n"
17016                "    {\n"
17017                "    class A\n"
17018                "      {\n"
17019                "      void f()\n"
17020                "        {\n"
17021                "        if (true)\n"
17022                "          {\n"
17023                "          a();\n"
17024                "          b();\n"
17025                "          }\n"
17026                "        }\n"
17027                "      void g()\n"
17028                "        {\n"
17029                "        return;\n"
17030                "        }\n"
17031                "      };\n"
17032                "    struct B\n"
17033                "      {\n"
17034                "      int x;\n"
17035                "      };\n"
17036                "    } // namespace b\n"
17037                "  }   // namespace a",
17038                WhitesmithsBraceStyle);
17039 
17040   verifyFormat("void f()\n"
17041                "  {\n"
17042                "  if (true)\n"
17043                "    {\n"
17044                "    a();\n"
17045                "    }\n"
17046                "  else if (false)\n"
17047                "    {\n"
17048                "    b();\n"
17049                "    }\n"
17050                "  else\n"
17051                "    {\n"
17052                "    c();\n"
17053                "    }\n"
17054                "  }\n",
17055                WhitesmithsBraceStyle);
17056 
17057   verifyFormat("void f()\n"
17058                "  {\n"
17059                "  for (int i = 0; i < 10; ++i)\n"
17060                "    {\n"
17061                "    a();\n"
17062                "    }\n"
17063                "  while (false)\n"
17064                "    {\n"
17065                "    b();\n"
17066                "    }\n"
17067                "  do\n"
17068                "    {\n"
17069                "    c();\n"
17070                "    } while (false)\n"
17071                "  }\n",
17072                WhitesmithsBraceStyle);
17073 
17074   WhitesmithsBraceStyle.IndentCaseLabels = true;
17075   verifyFormat("void switchTest1(int a)\n"
17076                "  {\n"
17077                "  switch (a)\n"
17078                "    {\n"
17079                "    case 2:\n"
17080                "      {\n"
17081                "      }\n"
17082                "      break;\n"
17083                "    }\n"
17084                "  }\n",
17085                WhitesmithsBraceStyle);
17086 
17087   verifyFormat("void switchTest2(int a)\n"
17088                "  {\n"
17089                "  switch (a)\n"
17090                "    {\n"
17091                "    case 0:\n"
17092                "      break;\n"
17093                "    case 1:\n"
17094                "      {\n"
17095                "      break;\n"
17096                "      }\n"
17097                "    case 2:\n"
17098                "      {\n"
17099                "      }\n"
17100                "      break;\n"
17101                "    default:\n"
17102                "      break;\n"
17103                "    }\n"
17104                "  }\n",
17105                WhitesmithsBraceStyle);
17106 
17107   verifyFormat("void switchTest3(int a)\n"
17108                "  {\n"
17109                "  switch (a)\n"
17110                "    {\n"
17111                "    case 0:\n"
17112                "      {\n"
17113                "      foo(x);\n"
17114                "      }\n"
17115                "      break;\n"
17116                "    default:\n"
17117                "      {\n"
17118                "      foo(1);\n"
17119                "      }\n"
17120                "      break;\n"
17121                "    }\n"
17122                "  }\n",
17123                WhitesmithsBraceStyle);
17124 
17125   WhitesmithsBraceStyle.IndentCaseLabels = false;
17126 
17127   verifyFormat("void switchTest4(int a)\n"
17128                "  {\n"
17129                "  switch (a)\n"
17130                "    {\n"
17131                "  case 2:\n"
17132                "    {\n"
17133                "    }\n"
17134                "    break;\n"
17135                "    }\n"
17136                "  }\n",
17137                WhitesmithsBraceStyle);
17138 
17139   verifyFormat("void switchTest5(int a)\n"
17140                "  {\n"
17141                "  switch (a)\n"
17142                "    {\n"
17143                "  case 0:\n"
17144                "    break;\n"
17145                "  case 1:\n"
17146                "    {\n"
17147                "    foo();\n"
17148                "    break;\n"
17149                "    }\n"
17150                "  case 2:\n"
17151                "    {\n"
17152                "    }\n"
17153                "    break;\n"
17154                "  default:\n"
17155                "    break;\n"
17156                "    }\n"
17157                "  }\n",
17158                WhitesmithsBraceStyle);
17159 
17160   verifyFormat("void switchTest6(int a)\n"
17161                "  {\n"
17162                "  switch (a)\n"
17163                "    {\n"
17164                "  case 0:\n"
17165                "    {\n"
17166                "    foo(x);\n"
17167                "    }\n"
17168                "    break;\n"
17169                "  default:\n"
17170                "    {\n"
17171                "    foo(1);\n"
17172                "    }\n"
17173                "    break;\n"
17174                "    }\n"
17175                "  }\n",
17176                WhitesmithsBraceStyle);
17177 
17178   verifyFormat("enum X\n"
17179                "  {\n"
17180                "  Y = 0, // testing\n"
17181                "  }\n",
17182                WhitesmithsBraceStyle);
17183 
17184   verifyFormat("enum X\n"
17185                "  {\n"
17186                "  Y = 0\n"
17187                "  }\n",
17188                WhitesmithsBraceStyle);
17189   verifyFormat("enum X\n"
17190                "  {\n"
17191                "  Y = 0,\n"
17192                "  Z = 1\n"
17193                "  };\n",
17194                WhitesmithsBraceStyle);
17195 
17196   verifyFormat("@interface BSApplicationController ()\n"
17197                "  {\n"
17198                "@private\n"
17199                "  id _extraIvar;\n"
17200                "  }\n"
17201                "@end\n",
17202                WhitesmithsBraceStyle);
17203 
17204   verifyFormat("#ifdef _DEBUG\n"
17205                "int foo(int i = 0)\n"
17206                "#else\n"
17207                "int foo(int i = 5)\n"
17208                "#endif\n"
17209                "  {\n"
17210                "  return i;\n"
17211                "  }",
17212                WhitesmithsBraceStyle);
17213 
17214   verifyFormat("void foo() {}\n"
17215                "void bar()\n"
17216                "#ifdef _DEBUG\n"
17217                "  {\n"
17218                "  foo();\n"
17219                "  }\n"
17220                "#else\n"
17221                "  {\n"
17222                "  }\n"
17223                "#endif",
17224                WhitesmithsBraceStyle);
17225 
17226   verifyFormat("void foobar()\n"
17227                "  {\n"
17228                "  int i = 5;\n"
17229                "  }\n"
17230                "#ifdef _DEBUG\n"
17231                "void bar()\n"
17232                "  {\n"
17233                "  }\n"
17234                "#else\n"
17235                "void bar()\n"
17236                "  {\n"
17237                "  foobar();\n"
17238                "  }\n"
17239                "#endif",
17240                WhitesmithsBraceStyle);
17241 
17242   // This shouldn't affect ObjC blocks..
17243   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17244                "  // ...\n"
17245                "  int i;\n"
17246                "}];",
17247                WhitesmithsBraceStyle);
17248   verifyFormat("void (^block)(void) = ^{\n"
17249                "  // ...\n"
17250                "  int i;\n"
17251                "};",
17252                WhitesmithsBraceStyle);
17253   // .. or dict literals.
17254   verifyFormat("void f()\n"
17255                "  {\n"
17256                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17257                "  }",
17258                WhitesmithsBraceStyle);
17259 
17260   verifyFormat("int f()\n"
17261                "  { // comment\n"
17262                "  return 42;\n"
17263                "  }",
17264                WhitesmithsBraceStyle);
17265 
17266   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
17267   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17268       FormatStyle::SIS_OnlyFirstIf;
17269   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17270   verifyFormat("void f(bool b)\n"
17271                "  {\n"
17272                "  if (b)\n"
17273                "    {\n"
17274                "    return;\n"
17275                "    }\n"
17276                "  }\n",
17277                BreakBeforeBraceShortIfs);
17278   verifyFormat("void f(bool b)\n"
17279                "  {\n"
17280                "  if (b) return;\n"
17281                "  }\n",
17282                BreakBeforeBraceShortIfs);
17283   verifyFormat("void f(bool b)\n"
17284                "  {\n"
17285                "  while (b)\n"
17286                "    {\n"
17287                "    return;\n"
17288                "    }\n"
17289                "  }\n",
17290                BreakBeforeBraceShortIfs);
17291 }
17292 
17293 TEST_F(FormatTest, GNUBraceBreaking) {
17294   FormatStyle GNUBraceStyle = getLLVMStyle();
17295   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
17296   verifyFormat("namespace a\n"
17297                "{\n"
17298                "class A\n"
17299                "{\n"
17300                "  void f()\n"
17301                "  {\n"
17302                "    int a;\n"
17303                "    {\n"
17304                "      int b;\n"
17305                "    }\n"
17306                "    if (true)\n"
17307                "      {\n"
17308                "        a();\n"
17309                "        b();\n"
17310                "      }\n"
17311                "  }\n"
17312                "  void g() { return; }\n"
17313                "}\n"
17314                "} // namespace a",
17315                GNUBraceStyle);
17316 
17317   verifyFormat("void f()\n"
17318                "{\n"
17319                "  if (true)\n"
17320                "    {\n"
17321                "      a();\n"
17322                "    }\n"
17323                "  else if (false)\n"
17324                "    {\n"
17325                "      b();\n"
17326                "    }\n"
17327                "  else\n"
17328                "    {\n"
17329                "      c();\n"
17330                "    }\n"
17331                "}\n",
17332                GNUBraceStyle);
17333 
17334   verifyFormat("void f()\n"
17335                "{\n"
17336                "  for (int i = 0; i < 10; ++i)\n"
17337                "    {\n"
17338                "      a();\n"
17339                "    }\n"
17340                "  while (false)\n"
17341                "    {\n"
17342                "      b();\n"
17343                "    }\n"
17344                "  do\n"
17345                "    {\n"
17346                "      c();\n"
17347                "    }\n"
17348                "  while (false);\n"
17349                "}\n",
17350                GNUBraceStyle);
17351 
17352   verifyFormat("void f(int a)\n"
17353                "{\n"
17354                "  switch (a)\n"
17355                "    {\n"
17356                "    case 0:\n"
17357                "      break;\n"
17358                "    case 1:\n"
17359                "      {\n"
17360                "        break;\n"
17361                "      }\n"
17362                "    case 2:\n"
17363                "      {\n"
17364                "      }\n"
17365                "      break;\n"
17366                "    default:\n"
17367                "      break;\n"
17368                "    }\n"
17369                "}\n",
17370                GNUBraceStyle);
17371 
17372   verifyFormat("enum X\n"
17373                "{\n"
17374                "  Y = 0,\n"
17375                "}\n",
17376                GNUBraceStyle);
17377 
17378   verifyFormat("@interface BSApplicationController ()\n"
17379                "{\n"
17380                "@private\n"
17381                "  id _extraIvar;\n"
17382                "}\n"
17383                "@end\n",
17384                GNUBraceStyle);
17385 
17386   verifyFormat("#ifdef _DEBUG\n"
17387                "int foo(int i = 0)\n"
17388                "#else\n"
17389                "int foo(int i = 5)\n"
17390                "#endif\n"
17391                "{\n"
17392                "  return i;\n"
17393                "}",
17394                GNUBraceStyle);
17395 
17396   verifyFormat("void foo() {}\n"
17397                "void bar()\n"
17398                "#ifdef _DEBUG\n"
17399                "{\n"
17400                "  foo();\n"
17401                "}\n"
17402                "#else\n"
17403                "{\n"
17404                "}\n"
17405                "#endif",
17406                GNUBraceStyle);
17407 
17408   verifyFormat("void foobar() { int i = 5; }\n"
17409                "#ifdef _DEBUG\n"
17410                "void bar() {}\n"
17411                "#else\n"
17412                "void bar() { foobar(); }\n"
17413                "#endif",
17414                GNUBraceStyle);
17415 }
17416 
17417 TEST_F(FormatTest, WebKitBraceBreaking) {
17418   FormatStyle WebKitBraceStyle = getLLVMStyle();
17419   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
17420   WebKitBraceStyle.FixNamespaceComments = false;
17421   verifyFormat("namespace a {\n"
17422                "class A {\n"
17423                "  void f()\n"
17424                "  {\n"
17425                "    if (true) {\n"
17426                "      a();\n"
17427                "      b();\n"
17428                "    }\n"
17429                "  }\n"
17430                "  void g() { return; }\n"
17431                "};\n"
17432                "enum E {\n"
17433                "  A,\n"
17434                "  // foo\n"
17435                "  B,\n"
17436                "  C\n"
17437                "};\n"
17438                "struct B {\n"
17439                "  int x;\n"
17440                "};\n"
17441                "}\n",
17442                WebKitBraceStyle);
17443   verifyFormat("struct S {\n"
17444                "  int Type;\n"
17445                "  union {\n"
17446                "    int x;\n"
17447                "    double y;\n"
17448                "  } Value;\n"
17449                "  class C {\n"
17450                "    MyFavoriteType Value;\n"
17451                "  } Class;\n"
17452                "};\n",
17453                WebKitBraceStyle);
17454 }
17455 
17456 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
17457   verifyFormat("void f() {\n"
17458                "  try {\n"
17459                "  } catch (const Exception &e) {\n"
17460                "  }\n"
17461                "}\n",
17462                getLLVMStyle());
17463 }
17464 
17465 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
17466   auto Style = getLLVMStyle();
17467   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17468   Style.AlignConsecutiveAssignments =
17469       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17470   Style.AlignConsecutiveDeclarations =
17471       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17472   verifyFormat("struct test demo[] = {\n"
17473                "    {56,    23, \"hello\"},\n"
17474                "    {-1, 93463, \"world\"},\n"
17475                "    { 7,     5,    \"!!\"}\n"
17476                "};\n",
17477                Style);
17478 
17479   verifyFormat("struct test demo[] = {\n"
17480                "    {56,    23, \"hello\"}, // first line\n"
17481                "    {-1, 93463, \"world\"}, // second line\n"
17482                "    { 7,     5,    \"!!\"}  // third line\n"
17483                "};\n",
17484                Style);
17485 
17486   verifyFormat("struct test demo[4] = {\n"
17487                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17488                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17489                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17490                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17491                "};\n",
17492                Style);
17493 
17494   verifyFormat("struct test demo[3] = {\n"
17495                "    {56,    23, \"hello\"},\n"
17496                "    {-1, 93463, \"world\"},\n"
17497                "    { 7,     5,    \"!!\"}\n"
17498                "};\n",
17499                Style);
17500 
17501   verifyFormat("struct test demo[3] = {\n"
17502                "    {int{56},    23, \"hello\"},\n"
17503                "    {int{-1}, 93463, \"world\"},\n"
17504                "    { int{7},     5,    \"!!\"}\n"
17505                "};\n",
17506                Style);
17507 
17508   verifyFormat("struct test demo[] = {\n"
17509                "    {56,    23, \"hello\"},\n"
17510                "    {-1, 93463, \"world\"},\n"
17511                "    { 7,     5,    \"!!\"},\n"
17512                "};\n",
17513                Style);
17514 
17515   verifyFormat("test demo[] = {\n"
17516                "    {56,    23, \"hello\"},\n"
17517                "    {-1, 93463, \"world\"},\n"
17518                "    { 7,     5,    \"!!\"},\n"
17519                "};\n",
17520                Style);
17521 
17522   verifyFormat("demo = std::array<struct test, 3>{\n"
17523                "    test{56,    23, \"hello\"},\n"
17524                "    test{-1, 93463, \"world\"},\n"
17525                "    test{ 7,     5,    \"!!\"},\n"
17526                "};\n",
17527                Style);
17528 
17529   verifyFormat("test demo[] = {\n"
17530                "    {56,    23, \"hello\"},\n"
17531                "#if X\n"
17532                "    {-1, 93463, \"world\"},\n"
17533                "#endif\n"
17534                "    { 7,     5,    \"!!\"}\n"
17535                "};\n",
17536                Style);
17537 
17538   verifyFormat(
17539       "test demo[] = {\n"
17540       "    { 7,    23,\n"
17541       "     \"hello world i am a very long line that really, in any\"\n"
17542       "     \"just world, ought to be split over multiple lines\"},\n"
17543       "    {-1, 93463,                                  \"world\"},\n"
17544       "    {56,     5,                                     \"!!\"}\n"
17545       "};\n",
17546       Style);
17547 
17548   verifyFormat("return GradForUnaryCwise(g, {\n"
17549                "                                {{\"sign\"}, \"Sign\",  "
17550                "  {\"x\", \"dy\"}},\n"
17551                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
17552                ", \"sign\"}},\n"
17553                "});\n",
17554                Style);
17555 
17556   Style.ColumnLimit = 0;
17557   EXPECT_EQ(
17558       "test demo[] = {\n"
17559       "    {56,    23, \"hello world i am a very long line that really, "
17560       "in any just world, ought to be split over multiple lines\"},\n"
17561       "    {-1, 93463,                                                  "
17562       "                                                 \"world\"},\n"
17563       "    { 7,     5,                                                  "
17564       "                                                    \"!!\"},\n"
17565       "};",
17566       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17567              "that really, in any just world, ought to be split over multiple "
17568              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17569              Style));
17570 
17571   Style.ColumnLimit = 80;
17572   verifyFormat("test demo[] = {\n"
17573                "    {56,    23, /* a comment */ \"hello\"},\n"
17574                "    {-1, 93463,                 \"world\"},\n"
17575                "    { 7,     5,                    \"!!\"}\n"
17576                "};\n",
17577                Style);
17578 
17579   verifyFormat("test demo[] = {\n"
17580                "    {56,    23,                    \"hello\"},\n"
17581                "    {-1, 93463, \"world\" /* comment here */},\n"
17582                "    { 7,     5,                       \"!!\"}\n"
17583                "};\n",
17584                Style);
17585 
17586   verifyFormat("test demo[] = {\n"
17587                "    {56, /* a comment */ 23, \"hello\"},\n"
17588                "    {-1,              93463, \"world\"},\n"
17589                "    { 7,                  5,    \"!!\"}\n"
17590                "};\n",
17591                Style);
17592 
17593   Style.ColumnLimit = 20;
17594   EXPECT_EQ(
17595       "demo = std::array<\n"
17596       "    struct test, 3>{\n"
17597       "    test{\n"
17598       "         56,    23,\n"
17599       "         \"hello \"\n"
17600       "         \"world i \"\n"
17601       "         \"am a very \"\n"
17602       "         \"long line \"\n"
17603       "         \"that \"\n"
17604       "         \"really, \"\n"
17605       "         \"in any \"\n"
17606       "         \"just \"\n"
17607       "         \"world, \"\n"
17608       "         \"ought to \"\n"
17609       "         \"be split \"\n"
17610       "         \"over \"\n"
17611       "         \"multiple \"\n"
17612       "         \"lines\"},\n"
17613       "    test{-1, 93463,\n"
17614       "         \"world\"},\n"
17615       "    test{ 7,     5,\n"
17616       "         \"!!\"   },\n"
17617       "};",
17618       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17619              "i am a very long line that really, in any just world, ought "
17620              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17621              "test{7, 5, \"!!\"},};",
17622              Style));
17623   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17624   Style = getLLVMStyleWithColumns(50);
17625   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17626   verifyFormat("static A x = {\n"
17627                "    {{init1, init2, init3, init4},\n"
17628                "     {init1, init2, init3, init4}}\n"
17629                "};",
17630                Style);
17631   Style.ColumnLimit = 100;
17632   EXPECT_EQ(
17633       "test demo[] = {\n"
17634       "    {56,    23,\n"
17635       "     \"hello world i am a very long line that really, in any just world"
17636       ", ought to be split over \"\n"
17637       "     \"multiple lines\"  },\n"
17638       "    {-1, 93463, \"world\"},\n"
17639       "    { 7,     5,    \"!!\"},\n"
17640       "};",
17641       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17642              "that really, in any just world, ought to be split over multiple "
17643              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17644              Style));
17645 
17646   Style = getLLVMStyleWithColumns(50);
17647   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17648   Style.AlignConsecutiveAssignments =
17649       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17650   Style.AlignConsecutiveDeclarations =
17651       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17652   verifyFormat("struct test demo[] = {\n"
17653                "    {56,    23, \"hello\"},\n"
17654                "    {-1, 93463, \"world\"},\n"
17655                "    { 7,     5,    \"!!\"}\n"
17656                "};\n"
17657                "static A x = {\n"
17658                "    {{init1, init2, init3, init4},\n"
17659                "     {init1, init2, init3, init4}}\n"
17660                "};",
17661                Style);
17662   Style.ColumnLimit = 100;
17663   Style.AlignConsecutiveAssignments =
17664       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17665   Style.AlignConsecutiveDeclarations =
17666       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17667   verifyFormat("struct test demo[] = {\n"
17668                "    {56,    23, \"hello\"},\n"
17669                "    {-1, 93463, \"world\"},\n"
17670                "    { 7,     5,    \"!!\"}\n"
17671                "};\n"
17672                "struct test demo[4] = {\n"
17673                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17674                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17675                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17676                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17677                "};\n",
17678                Style);
17679   EXPECT_EQ(
17680       "test demo[] = {\n"
17681       "    {56,\n"
17682       "     \"hello world i am a very long line that really, in any just world"
17683       ", ought to be split over \"\n"
17684       "     \"multiple lines\",    23},\n"
17685       "    {-1,      \"world\", 93463},\n"
17686       "    { 7,         \"!!\",     5},\n"
17687       "};",
17688       format("test demo[] = {{56, \"hello world i am a very long line "
17689              "that really, in any just world, ought to be split over multiple "
17690              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
17691              Style));
17692 }
17693 
17694 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
17695   auto Style = getLLVMStyle();
17696   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
17697   verifyFormat("struct test demo[] = {\n"
17698                "    {56, 23,    \"hello\"},\n"
17699                "    {-1, 93463, \"world\"},\n"
17700                "    {7,  5,     \"!!\"   }\n"
17701                "};\n",
17702                Style);
17703 
17704   verifyFormat("struct test demo[] = {\n"
17705                "    {56, 23,    \"hello\"}, // first line\n"
17706                "    {-1, 93463, \"world\"}, // second line\n"
17707                "    {7,  5,     \"!!\"   }  // third line\n"
17708                "};\n",
17709                Style);
17710   verifyFormat("struct test demo[4] = {\n"
17711                "    {56,  23,    21, \"oh\"      }, // first line\n"
17712                "    {-1,  93463, 22, \"my\"      }, // second line\n"
17713                "    {7,   5,     1,  \"goodness\"}  // third line\n"
17714                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
17715                "};\n",
17716                Style);
17717   verifyFormat("struct test demo[3] = {\n"
17718                "    {56, 23,    \"hello\"},\n"
17719                "    {-1, 93463, \"world\"},\n"
17720                "    {7,  5,     \"!!\"   }\n"
17721                "};\n",
17722                Style);
17723 
17724   verifyFormat("struct test demo[3] = {\n"
17725                "    {int{56}, 23,    \"hello\"},\n"
17726                "    {int{-1}, 93463, \"world\"},\n"
17727                "    {int{7},  5,     \"!!\"   }\n"
17728                "};\n",
17729                Style);
17730   verifyFormat("struct test demo[] = {\n"
17731                "    {56, 23,    \"hello\"},\n"
17732                "    {-1, 93463, \"world\"},\n"
17733                "    {7,  5,     \"!!\"   },\n"
17734                "};\n",
17735                Style);
17736   verifyFormat("test demo[] = {\n"
17737                "    {56, 23,    \"hello\"},\n"
17738                "    {-1, 93463, \"world\"},\n"
17739                "    {7,  5,     \"!!\"   },\n"
17740                "};\n",
17741                Style);
17742   verifyFormat("demo = std::array<struct test, 3>{\n"
17743                "    test{56, 23,    \"hello\"},\n"
17744                "    test{-1, 93463, \"world\"},\n"
17745                "    test{7,  5,     \"!!\"   },\n"
17746                "};\n",
17747                Style);
17748   verifyFormat("test demo[] = {\n"
17749                "    {56, 23,    \"hello\"},\n"
17750                "#if X\n"
17751                "    {-1, 93463, \"world\"},\n"
17752                "#endif\n"
17753                "    {7,  5,     \"!!\"   }\n"
17754                "};\n",
17755                Style);
17756   verifyFormat(
17757       "test demo[] = {\n"
17758       "    {7,  23,\n"
17759       "     \"hello world i am a very long line that really, in any\"\n"
17760       "     \"just world, ought to be split over multiple lines\"},\n"
17761       "    {-1, 93463, \"world\"                                 },\n"
17762       "    {56, 5,     \"!!\"                                    }\n"
17763       "};\n",
17764       Style);
17765 
17766   verifyFormat("return GradForUnaryCwise(g, {\n"
17767                "                                {{\"sign\"}, \"Sign\", {\"x\", "
17768                "\"dy\"}   },\n"
17769                "                                {{\"dx\"},   \"Mul\",  "
17770                "{\"dy\", \"sign\"}},\n"
17771                "});\n",
17772                Style);
17773 
17774   Style.ColumnLimit = 0;
17775   EXPECT_EQ(
17776       "test demo[] = {\n"
17777       "    {56, 23,    \"hello world i am a very long line that really, in any "
17778       "just world, ought to be split over multiple lines\"},\n"
17779       "    {-1, 93463, \"world\"                                               "
17780       "                                                   },\n"
17781       "    {7,  5,     \"!!\"                                                  "
17782       "                                                   },\n"
17783       "};",
17784       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17785              "that really, in any just world, ought to be split over multiple "
17786              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17787              Style));
17788 
17789   Style.ColumnLimit = 80;
17790   verifyFormat("test demo[] = {\n"
17791                "    {56, 23,    /* a comment */ \"hello\"},\n"
17792                "    {-1, 93463, \"world\"                },\n"
17793                "    {7,  5,     \"!!\"                   }\n"
17794                "};\n",
17795                Style);
17796 
17797   verifyFormat("test demo[] = {\n"
17798                "    {56, 23,    \"hello\"                   },\n"
17799                "    {-1, 93463, \"world\" /* comment here */},\n"
17800                "    {7,  5,     \"!!\"                      }\n"
17801                "};\n",
17802                Style);
17803 
17804   verifyFormat("test demo[] = {\n"
17805                "    {56, /* a comment */ 23, \"hello\"},\n"
17806                "    {-1, 93463,              \"world\"},\n"
17807                "    {7,  5,                  \"!!\"   }\n"
17808                "};\n",
17809                Style);
17810 
17811   Style.ColumnLimit = 20;
17812   EXPECT_EQ(
17813       "demo = std::array<\n"
17814       "    struct test, 3>{\n"
17815       "    test{\n"
17816       "         56, 23,\n"
17817       "         \"hello \"\n"
17818       "         \"world i \"\n"
17819       "         \"am a very \"\n"
17820       "         \"long line \"\n"
17821       "         \"that \"\n"
17822       "         \"really, \"\n"
17823       "         \"in any \"\n"
17824       "         \"just \"\n"
17825       "         \"world, \"\n"
17826       "         \"ought to \"\n"
17827       "         \"be split \"\n"
17828       "         \"over \"\n"
17829       "         \"multiple \"\n"
17830       "         \"lines\"},\n"
17831       "    test{-1, 93463,\n"
17832       "         \"world\"},\n"
17833       "    test{7,  5,\n"
17834       "         \"!!\"   },\n"
17835       "};",
17836       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17837              "i am a very long line that really, in any just world, ought "
17838              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17839              "test{7, 5, \"!!\"},};",
17840              Style));
17841 
17842   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17843   Style = getLLVMStyleWithColumns(50);
17844   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
17845   verifyFormat("static A x = {\n"
17846                "    {{init1, init2, init3, init4},\n"
17847                "     {init1, init2, init3, init4}}\n"
17848                "};",
17849                Style);
17850   Style.ColumnLimit = 100;
17851   EXPECT_EQ(
17852       "test demo[] = {\n"
17853       "    {56, 23,\n"
17854       "     \"hello world i am a very long line that really, in any just world"
17855       ", ought to be split over \"\n"
17856       "     \"multiple lines\"  },\n"
17857       "    {-1, 93463, \"world\"},\n"
17858       "    {7,  5,     \"!!\"   },\n"
17859       "};",
17860       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17861              "that really, in any just world, ought to be split over multiple "
17862              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17863              Style));
17864 }
17865 
17866 TEST_F(FormatTest, UnderstandsPragmas) {
17867   verifyFormat("#pragma omp reduction(| : var)");
17868   verifyFormat("#pragma omp reduction(+ : var)");
17869 
17870   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
17871             "(including parentheses).",
17872             format("#pragma    mark   Any non-hyphenated or hyphenated string "
17873                    "(including parentheses)."));
17874 }
17875 
17876 TEST_F(FormatTest, UnderstandPragmaOption) {
17877   verifyFormat("#pragma option -C -A");
17878 
17879   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
17880 }
17881 
17882 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
17883   FormatStyle Style = getLLVMStyle();
17884   Style.ColumnLimit = 20;
17885 
17886   // See PR41213
17887   EXPECT_EQ("/*\n"
17888             " *\t9012345\n"
17889             " * /8901\n"
17890             " */",
17891             format("/*\n"
17892                    " *\t9012345 /8901\n"
17893                    " */",
17894                    Style));
17895   EXPECT_EQ("/*\n"
17896             " *345678\n"
17897             " *\t/8901\n"
17898             " */",
17899             format("/*\n"
17900                    " *345678\t/8901\n"
17901                    " */",
17902                    Style));
17903 
17904   verifyFormat("int a; // the\n"
17905                "       // comment",
17906                Style);
17907   EXPECT_EQ("int a; /* first line\n"
17908             "        * second\n"
17909             "        * line third\n"
17910             "        * line\n"
17911             "        */",
17912             format("int a; /* first line\n"
17913                    "        * second\n"
17914                    "        * line third\n"
17915                    "        * line\n"
17916                    "        */",
17917                    Style));
17918   EXPECT_EQ("int a; // first line\n"
17919             "       // second\n"
17920             "       // line third\n"
17921             "       // line",
17922             format("int a; // first line\n"
17923                    "       // second line\n"
17924                    "       // third line",
17925                    Style));
17926 
17927   Style.PenaltyExcessCharacter = 90;
17928   verifyFormat("int a; // the comment", Style);
17929   EXPECT_EQ("int a; // the comment\n"
17930             "       // aaa",
17931             format("int a; // the comment aaa", Style));
17932   EXPECT_EQ("int a; /* first line\n"
17933             "        * second line\n"
17934             "        * third line\n"
17935             "        */",
17936             format("int a; /* first line\n"
17937                    "        * second line\n"
17938                    "        * third line\n"
17939                    "        */",
17940                    Style));
17941   EXPECT_EQ("int a; // first line\n"
17942             "       // second line\n"
17943             "       // third line",
17944             format("int a; // first line\n"
17945                    "       // second line\n"
17946                    "       // third line",
17947                    Style));
17948   // FIXME: Investigate why this is not getting the same layout as the test
17949   // above.
17950   EXPECT_EQ("int a; /* first line\n"
17951             "        * second line\n"
17952             "        * third line\n"
17953             "        */",
17954             format("int a; /* first line second line third line"
17955                    "\n*/",
17956                    Style));
17957 
17958   EXPECT_EQ("// foo bar baz bazfoo\n"
17959             "// foo bar foo bar\n",
17960             format("// foo bar baz bazfoo\n"
17961                    "// foo bar foo           bar\n",
17962                    Style));
17963   EXPECT_EQ("// foo bar baz bazfoo\n"
17964             "// foo bar foo bar\n",
17965             format("// foo bar baz      bazfoo\n"
17966                    "// foo            bar foo bar\n",
17967                    Style));
17968 
17969   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
17970   // next one.
17971   EXPECT_EQ("// foo bar baz bazfoo\n"
17972             "// bar foo bar\n",
17973             format("// foo bar baz      bazfoo bar\n"
17974                    "// foo            bar\n",
17975                    Style));
17976 
17977   EXPECT_EQ("// foo bar baz bazfoo\n"
17978             "// foo bar baz bazfoo\n"
17979             "// bar foo bar\n",
17980             format("// foo bar baz      bazfoo\n"
17981                    "// foo bar baz      bazfoo bar\n"
17982                    "// foo bar\n",
17983                    Style));
17984 
17985   EXPECT_EQ("// foo bar baz bazfoo\n"
17986             "// foo bar baz bazfoo\n"
17987             "// bar foo bar\n",
17988             format("// foo bar baz      bazfoo\n"
17989                    "// foo bar baz      bazfoo bar\n"
17990                    "// foo           bar\n",
17991                    Style));
17992 
17993   // Make sure we do not keep protruding characters if strict mode reflow is
17994   // cheaper than keeping protruding characters.
17995   Style.ColumnLimit = 21;
17996   EXPECT_EQ(
17997       "// foo foo foo foo\n"
17998       "// foo foo foo foo\n"
17999       "// foo foo foo foo\n",
18000       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18001 
18002   EXPECT_EQ("int a = /* long block\n"
18003             "           comment */\n"
18004             "    42;",
18005             format("int a = /* long block comment */ 42;", Style));
18006 }
18007 
18008 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
18009   for (size_t i = 1; i < Styles.size(); ++i)                                   \
18010   EXPECT_EQ(Styles[0], Styles[i])                                              \
18011       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
18012 
18013 TEST_F(FormatTest, GetsPredefinedStyleByName) {
18014   SmallVector<FormatStyle, 3> Styles;
18015   Styles.resize(3);
18016 
18017   Styles[0] = getLLVMStyle();
18018   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
18019   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
18020   EXPECT_ALL_STYLES_EQUAL(Styles);
18021 
18022   Styles[0] = getGoogleStyle();
18023   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
18024   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
18025   EXPECT_ALL_STYLES_EQUAL(Styles);
18026 
18027   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18028   EXPECT_TRUE(
18029       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
18030   EXPECT_TRUE(
18031       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
18032   EXPECT_ALL_STYLES_EQUAL(Styles);
18033 
18034   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
18035   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
18036   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
18037   EXPECT_ALL_STYLES_EQUAL(Styles);
18038 
18039   Styles[0] = getMozillaStyle();
18040   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
18041   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
18042   EXPECT_ALL_STYLES_EQUAL(Styles);
18043 
18044   Styles[0] = getWebKitStyle();
18045   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
18046   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
18047   EXPECT_ALL_STYLES_EQUAL(Styles);
18048 
18049   Styles[0] = getGNUStyle();
18050   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
18051   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
18052   EXPECT_ALL_STYLES_EQUAL(Styles);
18053 
18054   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
18055 }
18056 
18057 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
18058   SmallVector<FormatStyle, 8> Styles;
18059   Styles.resize(2);
18060 
18061   Styles[0] = getGoogleStyle();
18062   Styles[1] = getLLVMStyle();
18063   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18064   EXPECT_ALL_STYLES_EQUAL(Styles);
18065 
18066   Styles.resize(5);
18067   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18068   Styles[1] = getLLVMStyle();
18069   Styles[1].Language = FormatStyle::LK_JavaScript;
18070   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18071 
18072   Styles[2] = getLLVMStyle();
18073   Styles[2].Language = FormatStyle::LK_JavaScript;
18074   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
18075                                   "BasedOnStyle: Google",
18076                                   &Styles[2])
18077                    .value());
18078 
18079   Styles[3] = getLLVMStyle();
18080   Styles[3].Language = FormatStyle::LK_JavaScript;
18081   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
18082                                   "Language: JavaScript",
18083                                   &Styles[3])
18084                    .value());
18085 
18086   Styles[4] = getLLVMStyle();
18087   Styles[4].Language = FormatStyle::LK_JavaScript;
18088   EXPECT_EQ(0, parseConfiguration("---\n"
18089                                   "BasedOnStyle: LLVM\n"
18090                                   "IndentWidth: 123\n"
18091                                   "---\n"
18092                                   "BasedOnStyle: Google\n"
18093                                   "Language: JavaScript",
18094                                   &Styles[4])
18095                    .value());
18096   EXPECT_ALL_STYLES_EQUAL(Styles);
18097 }
18098 
18099 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
18100   Style.FIELD = false;                                                         \
18101   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
18102   EXPECT_TRUE(Style.FIELD);                                                    \
18103   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
18104   EXPECT_FALSE(Style.FIELD);
18105 
18106 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
18107 
18108 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
18109   Style.STRUCT.FIELD = false;                                                  \
18110   EXPECT_EQ(0,                                                                 \
18111             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
18112                 .value());                                                     \
18113   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
18114   EXPECT_EQ(0,                                                                 \
18115             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
18116                 .value());                                                     \
18117   EXPECT_FALSE(Style.STRUCT.FIELD);
18118 
18119 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
18120   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
18121 
18122 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
18123   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
18124   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
18125   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
18126 
18127 TEST_F(FormatTest, ParsesConfigurationBools) {
18128   FormatStyle Style = {};
18129   Style.Language = FormatStyle::LK_Cpp;
18130   CHECK_PARSE_BOOL(AlignTrailingComments);
18131   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
18132   CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
18133   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
18134   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
18135   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
18136   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
18137   CHECK_PARSE_BOOL(BinPackArguments);
18138   CHECK_PARSE_BOOL(BinPackParameters);
18139   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
18140   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
18141   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
18142   CHECK_PARSE_BOOL(BreakStringLiterals);
18143   CHECK_PARSE_BOOL(CompactNamespaces);
18144   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
18145   CHECK_PARSE_BOOL(DeriveLineEnding);
18146   CHECK_PARSE_BOOL(DerivePointerAlignment);
18147   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
18148   CHECK_PARSE_BOOL(DisableFormat);
18149   CHECK_PARSE_BOOL(IndentAccessModifiers);
18150   CHECK_PARSE_BOOL(IndentCaseLabels);
18151   CHECK_PARSE_BOOL(IndentCaseBlocks);
18152   CHECK_PARSE_BOOL(IndentGotoLabels);
18153   CHECK_PARSE_BOOL(IndentRequires);
18154   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
18155   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
18156   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
18157   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
18158   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
18159   CHECK_PARSE_BOOL(ReflowComments);
18160   CHECK_PARSE_BOOL(SortUsingDeclarations);
18161   CHECK_PARSE_BOOL(SpacesInParentheses);
18162   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
18163   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
18164   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
18165   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
18166   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
18167   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
18168   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
18169   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
18170   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
18171   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
18172   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
18173   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
18174   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
18175   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
18176   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
18177   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
18178   CHECK_PARSE_BOOL(UseCRLF);
18179 
18180   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
18181   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
18182   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
18183   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
18184   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
18185   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
18186   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
18187   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
18188   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
18189   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
18190   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
18191   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
18192   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
18193   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
18194   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
18195   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
18196   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
18197 }
18198 
18199 #undef CHECK_PARSE_BOOL
18200 
18201 TEST_F(FormatTest, ParsesConfiguration) {
18202   FormatStyle Style = {};
18203   Style.Language = FormatStyle::LK_Cpp;
18204   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
18205   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
18206               ConstructorInitializerIndentWidth, 1234u);
18207   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
18208   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
18209   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
18210   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
18211   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
18212               PenaltyBreakBeforeFirstCallParameter, 1234u);
18213   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
18214               PenaltyBreakTemplateDeclaration, 1234u);
18215   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
18216   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
18217               PenaltyReturnTypeOnItsOwnLine, 1234u);
18218   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
18219               SpacesBeforeTrailingComments, 1234u);
18220   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
18221   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
18222   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
18223 
18224   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
18225   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
18226               FormatStyle::ACS_None);
18227   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
18228               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
18229   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
18230               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
18231   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
18232               AlignConsecutiveAssignments,
18233               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18234   // For backwards compability, false / true should still parse
18235   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
18236               FormatStyle::ACS_None);
18237   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
18238               FormatStyle::ACS_Consecutive);
18239 
18240   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
18241   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
18242               FormatStyle::ACS_None);
18243   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
18244               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
18245   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
18246               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
18247   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
18248               AlignConsecutiveBitFields,
18249               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18250   // For backwards compability, false / true should still parse
18251   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
18252               FormatStyle::ACS_None);
18253   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
18254               FormatStyle::ACS_Consecutive);
18255 
18256   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
18257   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
18258               FormatStyle::ACS_None);
18259   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
18260               FormatStyle::ACS_Consecutive);
18261   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
18262               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
18263   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
18264               AlignConsecutiveMacros,
18265               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18266   // For backwards compability, false / true should still parse
18267   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
18268               FormatStyle::ACS_None);
18269   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
18270               FormatStyle::ACS_Consecutive);
18271 
18272   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
18273   CHECK_PARSE("AlignConsecutiveDeclarations: None",
18274               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18275   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
18276               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18277   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
18278               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
18279   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
18280               AlignConsecutiveDeclarations,
18281               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18282   // For backwards compability, false / true should still parse
18283   CHECK_PARSE("AlignConsecutiveDeclarations: false",
18284               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18285   CHECK_PARSE("AlignConsecutiveDeclarations: true",
18286               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18287 
18288   Style.PointerAlignment = FormatStyle::PAS_Middle;
18289   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
18290               FormatStyle::PAS_Left);
18291   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
18292               FormatStyle::PAS_Right);
18293   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
18294               FormatStyle::PAS_Middle);
18295   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
18296   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
18297               FormatStyle::RAS_Pointer);
18298   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
18299               FormatStyle::RAS_Left);
18300   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
18301               FormatStyle::RAS_Right);
18302   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
18303               FormatStyle::RAS_Middle);
18304   // For backward compatibility:
18305   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
18306               FormatStyle::PAS_Left);
18307   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
18308               FormatStyle::PAS_Right);
18309   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
18310               FormatStyle::PAS_Middle);
18311 
18312   Style.Standard = FormatStyle::LS_Auto;
18313   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
18314   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
18315   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
18316   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
18317   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
18318   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
18319   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
18320   // Legacy aliases:
18321   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
18322   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
18323   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
18324   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
18325 
18326   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18327   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
18328               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
18329   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
18330               FormatStyle::BOS_None);
18331   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
18332               FormatStyle::BOS_All);
18333   // For backward compatibility:
18334   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
18335               FormatStyle::BOS_None);
18336   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
18337               FormatStyle::BOS_All);
18338 
18339   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
18340   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
18341               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18342   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
18343               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
18344   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
18345               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
18346   // For backward compatibility:
18347   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
18348               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18349 
18350   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18351   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
18352               FormatStyle::BILS_AfterComma);
18353   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
18354               FormatStyle::BILS_BeforeComma);
18355   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
18356               FormatStyle::BILS_AfterColon);
18357   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
18358               FormatStyle::BILS_BeforeColon);
18359   // For backward compatibility:
18360   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
18361               FormatStyle::BILS_BeforeComma);
18362 
18363   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
18364   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
18365               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
18366   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
18367               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
18368   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
18369               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
18370   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
18371               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
18372 
18373   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18374   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
18375               FormatStyle::BAS_Align);
18376   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
18377               FormatStyle::BAS_DontAlign);
18378   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
18379               FormatStyle::BAS_AlwaysBreak);
18380   // For backward compatibility:
18381   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
18382               FormatStyle::BAS_DontAlign);
18383   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
18384               FormatStyle::BAS_Align);
18385 
18386   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18387   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
18388               FormatStyle::ENAS_DontAlign);
18389   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
18390               FormatStyle::ENAS_Left);
18391   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
18392               FormatStyle::ENAS_Right);
18393   // For backward compatibility:
18394   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
18395               FormatStyle::ENAS_Left);
18396   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
18397               FormatStyle::ENAS_Right);
18398 
18399   Style.AlignOperands = FormatStyle::OAS_Align;
18400   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
18401               FormatStyle::OAS_DontAlign);
18402   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
18403   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
18404               FormatStyle::OAS_AlignAfterOperator);
18405   // For backward compatibility:
18406   CHECK_PARSE("AlignOperands: false", AlignOperands,
18407               FormatStyle::OAS_DontAlign);
18408   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
18409 
18410   Style.UseTab = FormatStyle::UT_ForIndentation;
18411   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
18412   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
18413   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
18414   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
18415               FormatStyle::UT_ForContinuationAndIndentation);
18416   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
18417               FormatStyle::UT_AlignWithSpaces);
18418   // For backward compatibility:
18419   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
18420   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
18421 
18422   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
18423   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
18424               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18425   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
18426               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
18427   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
18428               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18429   // For backward compatibility:
18430   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
18431               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18432   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
18433               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18434 
18435   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18436   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
18437               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18438   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
18439               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
18440   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
18441               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
18442   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
18443               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18444   // For backward compatibility:
18445   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
18446               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18447   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
18448               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18449 
18450   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
18451   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
18452               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
18453   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
18454               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
18455   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
18456               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
18457   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
18458               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
18459 
18460   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
18461   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
18462               FormatStyle::SBPO_Never);
18463   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
18464               FormatStyle::SBPO_Always);
18465   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
18466               FormatStyle::SBPO_ControlStatements);
18467   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
18468               SpaceBeforeParens,
18469               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18470   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
18471               FormatStyle::SBPO_NonEmptyParentheses);
18472   // For backward compatibility:
18473   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
18474               FormatStyle::SBPO_Never);
18475   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
18476               FormatStyle::SBPO_ControlStatements);
18477   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
18478               SpaceBeforeParens,
18479               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18480 
18481   Style.ColumnLimit = 123;
18482   FormatStyle BaseStyle = getLLVMStyle();
18483   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
18484   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
18485 
18486   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
18487   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
18488               FormatStyle::BS_Attach);
18489   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
18490               FormatStyle::BS_Linux);
18491   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
18492               FormatStyle::BS_Mozilla);
18493   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
18494               FormatStyle::BS_Stroustrup);
18495   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
18496               FormatStyle::BS_Allman);
18497   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
18498               FormatStyle::BS_Whitesmiths);
18499   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
18500   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
18501               FormatStyle::BS_WebKit);
18502   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
18503               FormatStyle::BS_Custom);
18504 
18505   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
18506   CHECK_PARSE("BraceWrapping:\n"
18507               "  AfterControlStatement: MultiLine",
18508               BraceWrapping.AfterControlStatement,
18509               FormatStyle::BWACS_MultiLine);
18510   CHECK_PARSE("BraceWrapping:\n"
18511               "  AfterControlStatement: Always",
18512               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18513   CHECK_PARSE("BraceWrapping:\n"
18514               "  AfterControlStatement: Never",
18515               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18516   // For backward compatibility:
18517   CHECK_PARSE("BraceWrapping:\n"
18518               "  AfterControlStatement: true",
18519               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18520   CHECK_PARSE("BraceWrapping:\n"
18521               "  AfterControlStatement: false",
18522               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18523 
18524   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
18525   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
18526               FormatStyle::RTBS_None);
18527   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
18528               FormatStyle::RTBS_All);
18529   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
18530               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
18531   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
18532               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
18533   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
18534               AlwaysBreakAfterReturnType,
18535               FormatStyle::RTBS_TopLevelDefinitions);
18536 
18537   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
18538   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
18539               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
18540   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
18541               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18542   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
18543               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18544   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
18545               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18546   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
18547               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18548 
18549   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
18550   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
18551               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
18552   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
18553               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
18554   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
18555               AlwaysBreakAfterDefinitionReturnType,
18556               FormatStyle::DRTBS_TopLevel);
18557 
18558   Style.NamespaceIndentation = FormatStyle::NI_All;
18559   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
18560               FormatStyle::NI_None);
18561   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
18562               FormatStyle::NI_Inner);
18563   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
18564               FormatStyle::NI_All);
18565 
18566   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
18567   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
18568               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18569   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
18570               AllowShortIfStatementsOnASingleLine,
18571               FormatStyle::SIS_WithoutElse);
18572   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
18573               AllowShortIfStatementsOnASingleLine,
18574               FormatStyle::SIS_OnlyFirstIf);
18575   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
18576               AllowShortIfStatementsOnASingleLine,
18577               FormatStyle::SIS_AllIfsAndElse);
18578   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
18579               AllowShortIfStatementsOnASingleLine,
18580               FormatStyle::SIS_OnlyFirstIf);
18581   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
18582               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18583   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
18584               AllowShortIfStatementsOnASingleLine,
18585               FormatStyle::SIS_WithoutElse);
18586 
18587   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
18588   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
18589               FormatStyle::IEBS_AfterExternBlock);
18590   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
18591               FormatStyle::IEBS_Indent);
18592   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
18593               FormatStyle::IEBS_NoIndent);
18594   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
18595               FormatStyle::IEBS_Indent);
18596   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
18597               FormatStyle::IEBS_NoIndent);
18598 
18599   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
18600   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
18601               FormatStyle::BFCS_Both);
18602   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
18603               FormatStyle::BFCS_None);
18604   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
18605               FormatStyle::BFCS_Before);
18606   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
18607               FormatStyle::BFCS_After);
18608 
18609   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
18610   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
18611               FormatStyle::SJSIO_After);
18612   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
18613               FormatStyle::SJSIO_Before);
18614 
18615   // FIXME: This is required because parsing a configuration simply overwrites
18616   // the first N elements of the list instead of resetting it.
18617   Style.ForEachMacros.clear();
18618   std::vector<std::string> BoostForeach;
18619   BoostForeach.push_back("BOOST_FOREACH");
18620   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
18621   std::vector<std::string> BoostAndQForeach;
18622   BoostAndQForeach.push_back("BOOST_FOREACH");
18623   BoostAndQForeach.push_back("Q_FOREACH");
18624   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
18625               BoostAndQForeach);
18626 
18627   Style.IfMacros.clear();
18628   std::vector<std::string> CustomIfs;
18629   CustomIfs.push_back("MYIF");
18630   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
18631 
18632   Style.AttributeMacros.clear();
18633   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
18634               std::vector<std::string>{"__capability"});
18635   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
18636               std::vector<std::string>({"attr1", "attr2"}));
18637 
18638   Style.StatementAttributeLikeMacros.clear();
18639   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
18640               StatementAttributeLikeMacros,
18641               std::vector<std::string>({"emit", "Q_EMIT"}));
18642 
18643   Style.StatementMacros.clear();
18644   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
18645               std::vector<std::string>{"QUNUSED"});
18646   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
18647               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
18648 
18649   Style.NamespaceMacros.clear();
18650   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
18651               std::vector<std::string>{"TESTSUITE"});
18652   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
18653               std::vector<std::string>({"TESTSUITE", "SUITE"}));
18654 
18655   Style.WhitespaceSensitiveMacros.clear();
18656   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
18657               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
18658   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
18659               WhitespaceSensitiveMacros,
18660               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
18661   Style.WhitespaceSensitiveMacros.clear();
18662   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
18663               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
18664   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
18665               WhitespaceSensitiveMacros,
18666               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
18667 
18668   Style.IncludeStyle.IncludeCategories.clear();
18669   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
18670       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
18671   CHECK_PARSE("IncludeCategories:\n"
18672               "  - Regex: abc/.*\n"
18673               "    Priority: 2\n"
18674               "  - Regex: .*\n"
18675               "    Priority: 1\n"
18676               "    CaseSensitive: true\n",
18677               IncludeStyle.IncludeCategories, ExpectedCategories);
18678   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
18679               "abc$");
18680   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
18681               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
18682 
18683   Style.SortIncludes = FormatStyle::SI_Never;
18684   CHECK_PARSE("SortIncludes: true", SortIncludes,
18685               FormatStyle::SI_CaseSensitive);
18686   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
18687   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
18688               FormatStyle::SI_CaseInsensitive);
18689   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
18690               FormatStyle::SI_CaseSensitive);
18691   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
18692 
18693   Style.RawStringFormats.clear();
18694   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
18695       {
18696           FormatStyle::LK_TextProto,
18697           {"pb", "proto"},
18698           {"PARSE_TEXT_PROTO"},
18699           /*CanonicalDelimiter=*/"",
18700           "llvm",
18701       },
18702       {
18703           FormatStyle::LK_Cpp,
18704           {"cc", "cpp"},
18705           {"C_CODEBLOCK", "CPPEVAL"},
18706           /*CanonicalDelimiter=*/"cc",
18707           /*BasedOnStyle=*/"",
18708       },
18709   };
18710 
18711   CHECK_PARSE("RawStringFormats:\n"
18712               "  - Language: TextProto\n"
18713               "    Delimiters:\n"
18714               "      - 'pb'\n"
18715               "      - 'proto'\n"
18716               "    EnclosingFunctions:\n"
18717               "      - 'PARSE_TEXT_PROTO'\n"
18718               "    BasedOnStyle: llvm\n"
18719               "  - Language: Cpp\n"
18720               "    Delimiters:\n"
18721               "      - 'cc'\n"
18722               "      - 'cpp'\n"
18723               "    EnclosingFunctions:\n"
18724               "      - 'C_CODEBLOCK'\n"
18725               "      - 'CPPEVAL'\n"
18726               "    CanonicalDelimiter: 'cc'",
18727               RawStringFormats, ExpectedRawStringFormats);
18728 
18729   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18730               "  Minimum: 0\n"
18731               "  Maximum: 0",
18732               SpacesInLineCommentPrefix.Minimum, 0u);
18733   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
18734   Style.SpacesInLineCommentPrefix.Minimum = 1;
18735   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18736               "  Minimum: 2",
18737               SpacesInLineCommentPrefix.Minimum, 0u);
18738   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18739               "  Maximum: -1",
18740               SpacesInLineCommentPrefix.Maximum, -1u);
18741   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18742               "  Minimum: 2",
18743               SpacesInLineCommentPrefix.Minimum, 2u);
18744   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18745               "  Maximum: 1",
18746               SpacesInLineCommentPrefix.Maximum, 1u);
18747   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
18748 
18749   Style.SpacesInAngles = FormatStyle::SIAS_Always;
18750   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
18751   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
18752               FormatStyle::SIAS_Always);
18753   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
18754   // For backward compatibility:
18755   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
18756   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
18757 }
18758 
18759 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
18760   FormatStyle Style = {};
18761   Style.Language = FormatStyle::LK_Cpp;
18762   CHECK_PARSE("Language: Cpp\n"
18763               "IndentWidth: 12",
18764               IndentWidth, 12u);
18765   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
18766                                "IndentWidth: 34",
18767                                &Style),
18768             ParseError::Unsuitable);
18769   FormatStyle BinPackedTCS = {};
18770   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
18771   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
18772                                "InsertTrailingCommas: Wrapped",
18773                                &BinPackedTCS),
18774             ParseError::BinPackTrailingCommaConflict);
18775   EXPECT_EQ(12u, Style.IndentWidth);
18776   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
18777   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
18778 
18779   Style.Language = FormatStyle::LK_JavaScript;
18780   CHECK_PARSE("Language: JavaScript\n"
18781               "IndentWidth: 12",
18782               IndentWidth, 12u);
18783   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
18784   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
18785                                "IndentWidth: 34",
18786                                &Style),
18787             ParseError::Unsuitable);
18788   EXPECT_EQ(23u, Style.IndentWidth);
18789   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
18790   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
18791 
18792   CHECK_PARSE("BasedOnStyle: LLVM\n"
18793               "IndentWidth: 67",
18794               IndentWidth, 67u);
18795 
18796   CHECK_PARSE("---\n"
18797               "Language: JavaScript\n"
18798               "IndentWidth: 12\n"
18799               "---\n"
18800               "Language: Cpp\n"
18801               "IndentWidth: 34\n"
18802               "...\n",
18803               IndentWidth, 12u);
18804 
18805   Style.Language = FormatStyle::LK_Cpp;
18806   CHECK_PARSE("---\n"
18807               "Language: JavaScript\n"
18808               "IndentWidth: 12\n"
18809               "---\n"
18810               "Language: Cpp\n"
18811               "IndentWidth: 34\n"
18812               "...\n",
18813               IndentWidth, 34u);
18814   CHECK_PARSE("---\n"
18815               "IndentWidth: 78\n"
18816               "---\n"
18817               "Language: JavaScript\n"
18818               "IndentWidth: 56\n"
18819               "...\n",
18820               IndentWidth, 78u);
18821 
18822   Style.ColumnLimit = 123;
18823   Style.IndentWidth = 234;
18824   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
18825   Style.TabWidth = 345;
18826   EXPECT_FALSE(parseConfiguration("---\n"
18827                                   "IndentWidth: 456\n"
18828                                   "BreakBeforeBraces: Allman\n"
18829                                   "---\n"
18830                                   "Language: JavaScript\n"
18831                                   "IndentWidth: 111\n"
18832                                   "TabWidth: 111\n"
18833                                   "---\n"
18834                                   "Language: Cpp\n"
18835                                   "BreakBeforeBraces: Stroustrup\n"
18836                                   "TabWidth: 789\n"
18837                                   "...\n",
18838                                   &Style));
18839   EXPECT_EQ(123u, Style.ColumnLimit);
18840   EXPECT_EQ(456u, Style.IndentWidth);
18841   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
18842   EXPECT_EQ(789u, Style.TabWidth);
18843 
18844   EXPECT_EQ(parseConfiguration("---\n"
18845                                "Language: JavaScript\n"
18846                                "IndentWidth: 56\n"
18847                                "---\n"
18848                                "IndentWidth: 78\n"
18849                                "...\n",
18850                                &Style),
18851             ParseError::Error);
18852   EXPECT_EQ(parseConfiguration("---\n"
18853                                "Language: JavaScript\n"
18854                                "IndentWidth: 56\n"
18855                                "---\n"
18856                                "Language: JavaScript\n"
18857                                "IndentWidth: 78\n"
18858                                "...\n",
18859                                &Style),
18860             ParseError::Error);
18861 
18862   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
18863 }
18864 
18865 #undef CHECK_PARSE
18866 
18867 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
18868   FormatStyle Style = {};
18869   Style.Language = FormatStyle::LK_JavaScript;
18870   Style.BreakBeforeTernaryOperators = true;
18871   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
18872   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
18873 
18874   Style.BreakBeforeTernaryOperators = true;
18875   EXPECT_EQ(0, parseConfiguration("---\n"
18876                                   "BasedOnStyle: Google\n"
18877                                   "---\n"
18878                                   "Language: JavaScript\n"
18879                                   "IndentWidth: 76\n"
18880                                   "...\n",
18881                                   &Style)
18882                    .value());
18883   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
18884   EXPECT_EQ(76u, Style.IndentWidth);
18885   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
18886 }
18887 
18888 TEST_F(FormatTest, ConfigurationRoundTripTest) {
18889   FormatStyle Style = getLLVMStyle();
18890   std::string YAML = configurationAsText(Style);
18891   FormatStyle ParsedStyle = {};
18892   ParsedStyle.Language = FormatStyle::LK_Cpp;
18893   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
18894   EXPECT_EQ(Style, ParsedStyle);
18895 }
18896 
18897 TEST_F(FormatTest, WorksFor8bitEncodings) {
18898   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
18899             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
18900             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
18901             "\"\xef\xee\xf0\xf3...\"",
18902             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
18903                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
18904                    "\xef\xee\xf0\xf3...\"",
18905                    getLLVMStyleWithColumns(12)));
18906 }
18907 
18908 TEST_F(FormatTest, HandlesUTF8BOM) {
18909   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
18910   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
18911             format("\xef\xbb\xbf#include <iostream>"));
18912   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
18913             format("\xef\xbb\xbf\n#include <iostream>"));
18914 }
18915 
18916 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
18917 #if !defined(_MSC_VER)
18918 
18919 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
18920   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
18921                getLLVMStyleWithColumns(35));
18922   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
18923                getLLVMStyleWithColumns(31));
18924   verifyFormat("// Однажды в студёную зимнюю пору...",
18925                getLLVMStyleWithColumns(36));
18926   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
18927   verifyFormat("/* Однажды в студёную зимнюю пору... */",
18928                getLLVMStyleWithColumns(39));
18929   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
18930                getLLVMStyleWithColumns(35));
18931 }
18932 
18933 TEST_F(FormatTest, SplitsUTF8Strings) {
18934   // Non-printable characters' width is currently considered to be the length in
18935   // bytes in UTF8. The characters can be displayed in very different manner
18936   // (zero-width, single width with a substitution glyph, expanded to their code
18937   // (e.g. "<8d>"), so there's no single correct way to handle them.
18938   EXPECT_EQ("\"aaaaÄ\"\n"
18939             "\"\xc2\x8d\";",
18940             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
18941   EXPECT_EQ("\"aaaaaaaÄ\"\n"
18942             "\"\xc2\x8d\";",
18943             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
18944   EXPECT_EQ("\"Однажды, в \"\n"
18945             "\"студёную \"\n"
18946             "\"зимнюю \"\n"
18947             "\"пору,\"",
18948             format("\"Однажды, в студёную зимнюю пору,\"",
18949                    getLLVMStyleWithColumns(13)));
18950   EXPECT_EQ(
18951       "\"一 二 三 \"\n"
18952       "\"四 五六 \"\n"
18953       "\"七 八 九 \"\n"
18954       "\"十\"",
18955       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
18956   EXPECT_EQ("\"一\t\"\n"
18957             "\"二 \t\"\n"
18958             "\"三 四 \"\n"
18959             "\"五\t\"\n"
18960             "\"六 \t\"\n"
18961             "\"七 \"\n"
18962             "\"八九十\tqq\"",
18963             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
18964                    getLLVMStyleWithColumns(11)));
18965 
18966   // UTF8 character in an escape sequence.
18967   EXPECT_EQ("\"aaaaaa\"\n"
18968             "\"\\\xC2\x8D\"",
18969             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
18970 }
18971 
18972 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
18973   EXPECT_EQ("const char *sssss =\n"
18974             "    \"一二三四五六七八\\\n"
18975             " 九 十\";",
18976             format("const char *sssss = \"一二三四五六七八\\\n"
18977                    " 九 十\";",
18978                    getLLVMStyleWithColumns(30)));
18979 }
18980 
18981 TEST_F(FormatTest, SplitsUTF8LineComments) {
18982   EXPECT_EQ("// aaaaÄ\xc2\x8d",
18983             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
18984   EXPECT_EQ("// Я из лесу\n"
18985             "// вышел; был\n"
18986             "// сильный\n"
18987             "// мороз.",
18988             format("// Я из лесу вышел; был сильный мороз.",
18989                    getLLVMStyleWithColumns(13)));
18990   EXPECT_EQ("// 一二三\n"
18991             "// 四五六七\n"
18992             "// 八  九\n"
18993             "// 十",
18994             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
18995 }
18996 
18997 TEST_F(FormatTest, SplitsUTF8BlockComments) {
18998   EXPECT_EQ("/* Гляжу,\n"
18999             " * поднимается\n"
19000             " * медленно в\n"
19001             " * гору\n"
19002             " * Лошадка,\n"
19003             " * везущая\n"
19004             " * хворосту\n"
19005             " * воз. */",
19006             format("/* Гляжу, поднимается медленно в гору\n"
19007                    " * Лошадка, везущая хворосту воз. */",
19008                    getLLVMStyleWithColumns(13)));
19009   EXPECT_EQ(
19010       "/* 一二三\n"
19011       " * 四五六七\n"
19012       " * 八  九\n"
19013       " * 十  */",
19014       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
19015   EXPECT_EQ("/* �������� ��������\n"
19016             " * ��������\n"
19017             " * ������-�� */",
19018             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
19019 }
19020 
19021 #endif // _MSC_VER
19022 
19023 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
19024   FormatStyle Style = getLLVMStyle();
19025 
19026   Style.ConstructorInitializerIndentWidth = 4;
19027   verifyFormat(
19028       "SomeClass::Constructor()\n"
19029       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19030       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19031       Style);
19032 
19033   Style.ConstructorInitializerIndentWidth = 2;
19034   verifyFormat(
19035       "SomeClass::Constructor()\n"
19036       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19037       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19038       Style);
19039 
19040   Style.ConstructorInitializerIndentWidth = 0;
19041   verifyFormat(
19042       "SomeClass::Constructor()\n"
19043       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19044       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19045       Style);
19046   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19047   verifyFormat(
19048       "SomeLongTemplateVariableName<\n"
19049       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
19050       Style);
19051   verifyFormat("bool smaller = 1 < "
19052                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
19053                "                       "
19054                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
19055                Style);
19056 
19057   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
19058   verifyFormat("SomeClass::Constructor() :\n"
19059                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
19060                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
19061                Style);
19062 }
19063 
19064 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
19065   FormatStyle Style = getLLVMStyle();
19066   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19067   Style.ConstructorInitializerIndentWidth = 4;
19068   verifyFormat("SomeClass::Constructor()\n"
19069                "    : a(a)\n"
19070                "    , b(b)\n"
19071                "    , c(c) {}",
19072                Style);
19073   verifyFormat("SomeClass::Constructor()\n"
19074                "    : a(a) {}",
19075                Style);
19076 
19077   Style.ColumnLimit = 0;
19078   verifyFormat("SomeClass::Constructor()\n"
19079                "    : a(a) {}",
19080                Style);
19081   verifyFormat("SomeClass::Constructor() noexcept\n"
19082                "    : a(a) {}",
19083                Style);
19084   verifyFormat("SomeClass::Constructor()\n"
19085                "    : a(a)\n"
19086                "    , b(b)\n"
19087                "    , c(c) {}",
19088                Style);
19089   verifyFormat("SomeClass::Constructor()\n"
19090                "    : a(a) {\n"
19091                "  foo();\n"
19092                "  bar();\n"
19093                "}",
19094                Style);
19095 
19096   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
19097   verifyFormat("SomeClass::Constructor()\n"
19098                "    : a(a)\n"
19099                "    , b(b)\n"
19100                "    , c(c) {\n}",
19101                Style);
19102   verifyFormat("SomeClass::Constructor()\n"
19103                "    : a(a) {\n}",
19104                Style);
19105 
19106   Style.ColumnLimit = 80;
19107   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
19108   Style.ConstructorInitializerIndentWidth = 2;
19109   verifyFormat("SomeClass::Constructor()\n"
19110                "  : a(a)\n"
19111                "  , b(b)\n"
19112                "  , c(c) {}",
19113                Style);
19114 
19115   Style.ConstructorInitializerIndentWidth = 0;
19116   verifyFormat("SomeClass::Constructor()\n"
19117                ": a(a)\n"
19118                ", b(b)\n"
19119                ", c(c) {}",
19120                Style);
19121 
19122   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
19123   Style.ConstructorInitializerIndentWidth = 4;
19124   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
19125   verifyFormat(
19126       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
19127       Style);
19128   verifyFormat(
19129       "SomeClass::Constructor()\n"
19130       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
19131       Style);
19132   Style.ConstructorInitializerIndentWidth = 4;
19133   Style.ColumnLimit = 60;
19134   verifyFormat("SomeClass::Constructor()\n"
19135                "    : aaaaaaaa(aaaaaaaa)\n"
19136                "    , aaaaaaaa(aaaaaaaa)\n"
19137                "    , aaaaaaaa(aaaaaaaa) {}",
19138                Style);
19139 }
19140 
19141 TEST_F(FormatTest, Destructors) {
19142   verifyFormat("void F(int &i) { i.~int(); }");
19143   verifyFormat("void F(int &i) { i->~int(); }");
19144 }
19145 
19146 TEST_F(FormatTest, FormatsWithWebKitStyle) {
19147   FormatStyle Style = getWebKitStyle();
19148 
19149   // Don't indent in outer namespaces.
19150   verifyFormat("namespace outer {\n"
19151                "int i;\n"
19152                "namespace inner {\n"
19153                "    int i;\n"
19154                "} // namespace inner\n"
19155                "} // namespace outer\n"
19156                "namespace other_outer {\n"
19157                "int i;\n"
19158                "}",
19159                Style);
19160 
19161   // Don't indent case labels.
19162   verifyFormat("switch (variable) {\n"
19163                "case 1:\n"
19164                "case 2:\n"
19165                "    doSomething();\n"
19166                "    break;\n"
19167                "default:\n"
19168                "    ++variable;\n"
19169                "}",
19170                Style);
19171 
19172   // Wrap before binary operators.
19173   EXPECT_EQ("void f()\n"
19174             "{\n"
19175             "    if (aaaaaaaaaaaaaaaa\n"
19176             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
19177             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19178             "        return;\n"
19179             "}",
19180             format("void f() {\n"
19181                    "if (aaaaaaaaaaaaaaaa\n"
19182                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
19183                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19184                    "return;\n"
19185                    "}",
19186                    Style));
19187 
19188   // Allow functions on a single line.
19189   verifyFormat("void f() { return; }", Style);
19190 
19191   // Allow empty blocks on a single line and insert a space in empty blocks.
19192   EXPECT_EQ("void f() { }", format("void f() {}", Style));
19193   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
19194   // However, don't merge non-empty short loops.
19195   EXPECT_EQ("while (true) {\n"
19196             "    continue;\n"
19197             "}",
19198             format("while (true) { continue; }", Style));
19199 
19200   // Constructor initializers are formatted one per line with the "," on the
19201   // new line.
19202   verifyFormat("Constructor()\n"
19203                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
19204                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
19205                "          aaaaaaaaaaaaaa)\n"
19206                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
19207                "{\n"
19208                "}",
19209                Style);
19210   verifyFormat("SomeClass::Constructor()\n"
19211                "    : a(a)\n"
19212                "{\n"
19213                "}",
19214                Style);
19215   EXPECT_EQ("SomeClass::Constructor()\n"
19216             "    : a(a)\n"
19217             "{\n"
19218             "}",
19219             format("SomeClass::Constructor():a(a){}", Style));
19220   verifyFormat("SomeClass::Constructor()\n"
19221                "    : a(a)\n"
19222                "    , b(b)\n"
19223                "    , c(c)\n"
19224                "{\n"
19225                "}",
19226                Style);
19227   verifyFormat("SomeClass::Constructor()\n"
19228                "    : a(a)\n"
19229                "{\n"
19230                "    foo();\n"
19231                "    bar();\n"
19232                "}",
19233                Style);
19234 
19235   // Access specifiers should be aligned left.
19236   verifyFormat("class C {\n"
19237                "public:\n"
19238                "    int i;\n"
19239                "};",
19240                Style);
19241 
19242   // Do not align comments.
19243   verifyFormat("int a; // Do not\n"
19244                "double b; // align comments.",
19245                Style);
19246 
19247   // Do not align operands.
19248   EXPECT_EQ("ASSERT(aaaa\n"
19249             "    || bbbb);",
19250             format("ASSERT ( aaaa\n||bbbb);", Style));
19251 
19252   // Accept input's line breaks.
19253   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
19254             "    || bbbbbbbbbbbbbbb) {\n"
19255             "    i++;\n"
19256             "}",
19257             format("if (aaaaaaaaaaaaaaa\n"
19258                    "|| bbbbbbbbbbbbbbb) { i++; }",
19259                    Style));
19260   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
19261             "    i++;\n"
19262             "}",
19263             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
19264 
19265   // Don't automatically break all macro definitions (llvm.org/PR17842).
19266   verifyFormat("#define aNumber 10", Style);
19267   // However, generally keep the line breaks that the user authored.
19268   EXPECT_EQ("#define aNumber \\\n"
19269             "    10",
19270             format("#define aNumber \\\n"
19271                    " 10",
19272                    Style));
19273 
19274   // Keep empty and one-element array literals on a single line.
19275   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
19276             "                                  copyItems:YES];",
19277             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
19278                    "copyItems:YES];",
19279                    Style));
19280   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
19281             "                                  copyItems:YES];",
19282             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
19283                    "             copyItems:YES];",
19284                    Style));
19285   // FIXME: This does not seem right, there should be more indentation before
19286   // the array literal's entries. Nested blocks have the same problem.
19287   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19288             "    @\"a\",\n"
19289             "    @\"a\"\n"
19290             "]\n"
19291             "                                  copyItems:YES];",
19292             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19293                    "     @\"a\",\n"
19294                    "     @\"a\"\n"
19295                    "     ]\n"
19296                    "       copyItems:YES];",
19297                    Style));
19298   EXPECT_EQ(
19299       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19300       "                                  copyItems:YES];",
19301       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19302              "   copyItems:YES];",
19303              Style));
19304 
19305   verifyFormat("[self.a b:c c:d];", Style);
19306   EXPECT_EQ("[self.a b:c\n"
19307             "        c:d];",
19308             format("[self.a b:c\n"
19309                    "c:d];",
19310                    Style));
19311 }
19312 
19313 TEST_F(FormatTest, FormatsLambdas) {
19314   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
19315   verifyFormat(
19316       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
19317   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
19318   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
19319   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
19320   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
19321   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
19322   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
19323   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
19324   verifyFormat("int x = f(*+[] {});");
19325   verifyFormat("void f() {\n"
19326                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
19327                "}\n");
19328   verifyFormat("void f() {\n"
19329                "  other(x.begin(), //\n"
19330                "        x.end(),   //\n"
19331                "        [&](int, int) { return 1; });\n"
19332                "}\n");
19333   verifyFormat("void f() {\n"
19334                "  other.other.other.other.other(\n"
19335                "      x.begin(), x.end(),\n"
19336                "      [something, rather](int, int, int, int, int, int, int) { "
19337                "return 1; });\n"
19338                "}\n");
19339   verifyFormat(
19340       "void f() {\n"
19341       "  other.other.other.other.other(\n"
19342       "      x.begin(), x.end(),\n"
19343       "      [something, rather](int, int, int, int, int, int, int) {\n"
19344       "        //\n"
19345       "      });\n"
19346       "}\n");
19347   verifyFormat("SomeFunction([]() { // A cool function...\n"
19348                "  return 43;\n"
19349                "});");
19350   EXPECT_EQ("SomeFunction([]() {\n"
19351             "#define A a\n"
19352             "  return 43;\n"
19353             "});",
19354             format("SomeFunction([](){\n"
19355                    "#define A a\n"
19356                    "return 43;\n"
19357                    "});"));
19358   verifyFormat("void f() {\n"
19359                "  SomeFunction([](decltype(x), A *a) {});\n"
19360                "  SomeFunction([](typeof(x), A *a) {});\n"
19361                "  SomeFunction([](_Atomic(x), A *a) {});\n"
19362                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
19363                "}");
19364   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19365                "    [](const aaaaaaaaaa &a) { return a; });");
19366   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
19367                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
19368                "});");
19369   verifyFormat("Constructor()\n"
19370                "    : Field([] { // comment\n"
19371                "        int i;\n"
19372                "      }) {}");
19373   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
19374                "  return some_parameter.size();\n"
19375                "};");
19376   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
19377                "    [](const string &s) { return s; };");
19378   verifyFormat("int i = aaaaaa ? 1 //\n"
19379                "               : [] {\n"
19380                "                   return 2; //\n"
19381                "                 }();");
19382   verifyFormat("llvm::errs() << \"number of twos is \"\n"
19383                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
19384                "                  return x == 2; // force break\n"
19385                "                });");
19386   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19387                "    [=](int iiiiiiiiiiii) {\n"
19388                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
19389                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
19390                "    });",
19391                getLLVMStyleWithColumns(60));
19392 
19393   verifyFormat("SomeFunction({[&] {\n"
19394                "                // comment\n"
19395                "              },\n"
19396                "              [&] {\n"
19397                "                // comment\n"
19398                "              }});");
19399   verifyFormat("SomeFunction({[&] {\n"
19400                "  // comment\n"
19401                "}});");
19402   verifyFormat(
19403       "virtual aaaaaaaaaaaaaaaa(\n"
19404       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
19405       "    aaaaa aaaaaaaaa);");
19406 
19407   // Lambdas with return types.
19408   verifyFormat("int c = []() -> int { return 2; }();\n");
19409   verifyFormat("int c = []() -> int * { return 2; }();\n");
19410   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
19411   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
19412   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
19413   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
19414   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
19415   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
19416   verifyFormat("[a, a]() -> a<1> {};");
19417   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
19418   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
19419   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
19420   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
19421   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
19422   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
19423   verifyFormat("[]() -> foo<!5> { return {}; };");
19424   verifyFormat("[]() -> foo<~5> { return {}; };");
19425   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
19426   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
19427   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
19428   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
19429   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
19430   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
19431   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
19432   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
19433   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
19434   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
19435   verifyFormat("namespace bar {\n"
19436                "// broken:\n"
19437                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
19438                "} // namespace bar");
19439   verifyFormat("namespace bar {\n"
19440                "// broken:\n"
19441                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
19442                "} // namespace bar");
19443   verifyFormat("namespace bar {\n"
19444                "// broken:\n"
19445                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
19446                "} // namespace bar");
19447   verifyFormat("namespace bar {\n"
19448                "// broken:\n"
19449                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
19450                "} // namespace bar");
19451   verifyFormat("namespace bar {\n"
19452                "// broken:\n"
19453                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
19454                "} // namespace bar");
19455   verifyFormat("namespace bar {\n"
19456                "// broken:\n"
19457                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
19458                "} // namespace bar");
19459   verifyFormat("namespace bar {\n"
19460                "// broken:\n"
19461                "auto foo{[]() -> foo<!5> { return {}; }};\n"
19462                "} // namespace bar");
19463   verifyFormat("namespace bar {\n"
19464                "// broken:\n"
19465                "auto foo{[]() -> foo<~5> { return {}; }};\n"
19466                "} // namespace bar");
19467   verifyFormat("namespace bar {\n"
19468                "// broken:\n"
19469                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
19470                "} // namespace bar");
19471   verifyFormat("namespace bar {\n"
19472                "// broken:\n"
19473                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
19474                "} // namespace bar");
19475   verifyFormat("namespace bar {\n"
19476                "// broken:\n"
19477                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
19478                "} // namespace bar");
19479   verifyFormat("namespace bar {\n"
19480                "// broken:\n"
19481                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
19482                "} // namespace bar");
19483   verifyFormat("namespace bar {\n"
19484                "// broken:\n"
19485                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
19486                "} // namespace bar");
19487   verifyFormat("namespace bar {\n"
19488                "// broken:\n"
19489                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
19490                "} // namespace bar");
19491   verifyFormat("namespace bar {\n"
19492                "// broken:\n"
19493                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
19494                "} // namespace bar");
19495   verifyFormat("namespace bar {\n"
19496                "// broken:\n"
19497                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
19498                "} // namespace bar");
19499   verifyFormat("namespace bar {\n"
19500                "// broken:\n"
19501                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
19502                "} // namespace bar");
19503   verifyFormat("namespace bar {\n"
19504                "// broken:\n"
19505                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
19506                "} // namespace bar");
19507   verifyFormat("[]() -> a<1> {};");
19508   verifyFormat("[]() -> a<1> { ; };");
19509   verifyFormat("[]() -> a<1> { ; }();");
19510   verifyFormat("[a, a]() -> a<true> {};");
19511   verifyFormat("[]() -> a<true> {};");
19512   verifyFormat("[]() -> a<true> { ; };");
19513   verifyFormat("[]() -> a<true> { ; }();");
19514   verifyFormat("[a, a]() -> a<false> {};");
19515   verifyFormat("[]() -> a<false> {};");
19516   verifyFormat("[]() -> a<false> { ; };");
19517   verifyFormat("[]() -> a<false> { ; }();");
19518   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
19519   verifyFormat("namespace bar {\n"
19520                "auto foo{[]() -> foo<false> { ; }};\n"
19521                "} // namespace bar");
19522   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
19523                "                   int j) -> int {\n"
19524                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
19525                "};");
19526   verifyFormat(
19527       "aaaaaaaaaaaaaaaaaaaaaa(\n"
19528       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
19529       "      return aaaaaaaaaaaaaaaaa;\n"
19530       "    });",
19531       getLLVMStyleWithColumns(70));
19532   verifyFormat("[]() //\n"
19533                "    -> int {\n"
19534                "  return 1; //\n"
19535                "};");
19536   verifyFormat("[]() -> Void<T...> {};");
19537   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
19538 
19539   // Lambdas with explicit template argument lists.
19540   verifyFormat(
19541       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
19542 
19543   // Multiple lambdas in the same parentheses change indentation rules. These
19544   // lambdas are forced to start on new lines.
19545   verifyFormat("SomeFunction(\n"
19546                "    []() {\n"
19547                "      //\n"
19548                "    },\n"
19549                "    []() {\n"
19550                "      //\n"
19551                "    });");
19552 
19553   // A lambda passed as arg0 is always pushed to the next line.
19554   verifyFormat("SomeFunction(\n"
19555                "    [this] {\n"
19556                "      //\n"
19557                "    },\n"
19558                "    1);\n");
19559 
19560   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
19561   // the arg0 case above.
19562   auto Style = getGoogleStyle();
19563   Style.BinPackArguments = false;
19564   verifyFormat("SomeFunction(\n"
19565                "    a,\n"
19566                "    [this] {\n"
19567                "      //\n"
19568                "    },\n"
19569                "    b);\n",
19570                Style);
19571   verifyFormat("SomeFunction(\n"
19572                "    a,\n"
19573                "    [this] {\n"
19574                "      //\n"
19575                "    },\n"
19576                "    b);\n");
19577 
19578   // A lambda with a very long line forces arg0 to be pushed out irrespective of
19579   // the BinPackArguments value (as long as the code is wide enough).
19580   verifyFormat(
19581       "something->SomeFunction(\n"
19582       "    a,\n"
19583       "    [this] {\n"
19584       "      "
19585       "D0000000000000000000000000000000000000000000000000000000000001();\n"
19586       "    },\n"
19587       "    b);\n");
19588 
19589   // A multi-line lambda is pulled up as long as the introducer fits on the
19590   // previous line and there are no further args.
19591   verifyFormat("function(1, [this, that] {\n"
19592                "  //\n"
19593                "});\n");
19594   verifyFormat("function([this, that] {\n"
19595                "  //\n"
19596                "});\n");
19597   // FIXME: this format is not ideal and we should consider forcing the first
19598   // arg onto its own line.
19599   verifyFormat("function(a, b, c, //\n"
19600                "         d, [this, that] {\n"
19601                "           //\n"
19602                "         });\n");
19603 
19604   // Multiple lambdas are treated correctly even when there is a short arg0.
19605   verifyFormat("SomeFunction(\n"
19606                "    1,\n"
19607                "    [this] {\n"
19608                "      //\n"
19609                "    },\n"
19610                "    [this] {\n"
19611                "      //\n"
19612                "    },\n"
19613                "    1);\n");
19614 
19615   // More complex introducers.
19616   verifyFormat("return [i, args...] {};");
19617 
19618   // Not lambdas.
19619   verifyFormat("constexpr char hello[]{\"hello\"};");
19620   verifyFormat("double &operator[](int i) { return 0; }\n"
19621                "int i;");
19622   verifyFormat("std::unique_ptr<int[]> foo() {}");
19623   verifyFormat("int i = a[a][a]->f();");
19624   verifyFormat("int i = (*b)[a]->f();");
19625 
19626   // Other corner cases.
19627   verifyFormat("void f() {\n"
19628                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
19629                "  );\n"
19630                "}");
19631 
19632   // Lambdas created through weird macros.
19633   verifyFormat("void f() {\n"
19634                "  MACRO((const AA &a) { return 1; });\n"
19635                "  MACRO((AA &a) { return 1; });\n"
19636                "}");
19637 
19638   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
19639                "      doo_dah();\n"
19640                "      doo_dah();\n"
19641                "    })) {\n"
19642                "}");
19643   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
19644                "                doo_dah();\n"
19645                "                doo_dah();\n"
19646                "              })) {\n"
19647                "}");
19648   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
19649                "                doo_dah();\n"
19650                "                doo_dah();\n"
19651                "              })) {\n"
19652                "}");
19653   verifyFormat("auto lambda = []() {\n"
19654                "  int a = 2\n"
19655                "#if A\n"
19656                "          + 2\n"
19657                "#endif\n"
19658                "      ;\n"
19659                "};");
19660 
19661   // Lambdas with complex multiline introducers.
19662   verifyFormat(
19663       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19664       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
19665       "        -> ::std::unordered_set<\n"
19666       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
19667       "      //\n"
19668       "    });");
19669 
19670   FormatStyle DoNotMerge = getLLVMStyle();
19671   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
19672   verifyFormat("auto c = []() {\n"
19673                "  return b;\n"
19674                "};",
19675                "auto c = []() { return b; };", DoNotMerge);
19676   verifyFormat("auto c = []() {\n"
19677                "};",
19678                " auto c = []() {};", DoNotMerge);
19679 
19680   FormatStyle MergeEmptyOnly = getLLVMStyle();
19681   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
19682   verifyFormat("auto c = []() {\n"
19683                "  return b;\n"
19684                "};",
19685                "auto c = []() {\n"
19686                "  return b;\n"
19687                " };",
19688                MergeEmptyOnly);
19689   verifyFormat("auto c = []() {};",
19690                "auto c = []() {\n"
19691                "};",
19692                MergeEmptyOnly);
19693 
19694   FormatStyle MergeInline = getLLVMStyle();
19695   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
19696   verifyFormat("auto c = []() {\n"
19697                "  return b;\n"
19698                "};",
19699                "auto c = []() { return b; };", MergeInline);
19700   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
19701                MergeInline);
19702   verifyFormat("function([]() { return b; }, a)",
19703                "function([]() { return b; }, a)", MergeInline);
19704   verifyFormat("function(a, []() { return b; })",
19705                "function(a, []() { return b; })", MergeInline);
19706 
19707   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
19708   // AllowShortLambdasOnASingleLine
19709   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
19710   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
19711   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
19712   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19713       FormatStyle::ShortLambdaStyle::SLS_None;
19714   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
19715                "    []()\n"
19716                "    {\n"
19717                "      return 17;\n"
19718                "    });",
19719                LLVMWithBeforeLambdaBody);
19720   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
19721                "    []()\n"
19722                "    {\n"
19723                "    });",
19724                LLVMWithBeforeLambdaBody);
19725   verifyFormat("auto fct_SLS_None = []()\n"
19726                "{\n"
19727                "  return 17;\n"
19728                "};",
19729                LLVMWithBeforeLambdaBody);
19730   verifyFormat("TwoNestedLambdas_SLS_None(\n"
19731                "    []()\n"
19732                "    {\n"
19733                "      return Call(\n"
19734                "          []()\n"
19735                "          {\n"
19736                "            return 17;\n"
19737                "          });\n"
19738                "    });",
19739                LLVMWithBeforeLambdaBody);
19740   verifyFormat("void Fct() {\n"
19741                "  return {[]()\n"
19742                "          {\n"
19743                "            return 17;\n"
19744                "          }};\n"
19745                "}",
19746                LLVMWithBeforeLambdaBody);
19747 
19748   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19749       FormatStyle::ShortLambdaStyle::SLS_Empty;
19750   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
19751                "    []()\n"
19752                "    {\n"
19753                "      return 17;\n"
19754                "    });",
19755                LLVMWithBeforeLambdaBody);
19756   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
19757                LLVMWithBeforeLambdaBody);
19758   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
19759                "ongFunctionName_SLS_Empty(\n"
19760                "    []() {});",
19761                LLVMWithBeforeLambdaBody);
19762   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
19763                "                                []()\n"
19764                "                                {\n"
19765                "                                  return 17;\n"
19766                "                                });",
19767                LLVMWithBeforeLambdaBody);
19768   verifyFormat("auto fct_SLS_Empty = []()\n"
19769                "{\n"
19770                "  return 17;\n"
19771                "};",
19772                LLVMWithBeforeLambdaBody);
19773   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
19774                "    []()\n"
19775                "    {\n"
19776                "      return Call([]() {});\n"
19777                "    });",
19778                LLVMWithBeforeLambdaBody);
19779   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
19780                "                           []()\n"
19781                "                           {\n"
19782                "                             return Call([]() {});\n"
19783                "                           });",
19784                LLVMWithBeforeLambdaBody);
19785   verifyFormat(
19786       "FctWithLongLineInLambda_SLS_Empty(\n"
19787       "    []()\n"
19788       "    {\n"
19789       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19790       "                               AndShouldNotBeConsiderAsInline,\n"
19791       "                               LambdaBodyMustBeBreak);\n"
19792       "    });",
19793       LLVMWithBeforeLambdaBody);
19794 
19795   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19796       FormatStyle::ShortLambdaStyle::SLS_Inline;
19797   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
19798                LLVMWithBeforeLambdaBody);
19799   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
19800                LLVMWithBeforeLambdaBody);
19801   verifyFormat("auto fct_SLS_Inline = []()\n"
19802                "{\n"
19803                "  return 17;\n"
19804                "};",
19805                LLVMWithBeforeLambdaBody);
19806   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
19807                "17; }); });",
19808                LLVMWithBeforeLambdaBody);
19809   verifyFormat(
19810       "FctWithLongLineInLambda_SLS_Inline(\n"
19811       "    []()\n"
19812       "    {\n"
19813       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19814       "                               AndShouldNotBeConsiderAsInline,\n"
19815       "                               LambdaBodyMustBeBreak);\n"
19816       "    });",
19817       LLVMWithBeforeLambdaBody);
19818   verifyFormat("FctWithMultipleParams_SLS_Inline("
19819                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
19820                "                                 []() { return 17; });",
19821                LLVMWithBeforeLambdaBody);
19822   verifyFormat(
19823       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
19824       LLVMWithBeforeLambdaBody);
19825 
19826   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19827       FormatStyle::ShortLambdaStyle::SLS_All;
19828   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
19829                LLVMWithBeforeLambdaBody);
19830   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
19831                LLVMWithBeforeLambdaBody);
19832   verifyFormat("auto fct_SLS_All = []() { return 17; };",
19833                LLVMWithBeforeLambdaBody);
19834   verifyFormat("FctWithOneParam_SLS_All(\n"
19835                "    []()\n"
19836                "    {\n"
19837                "      // A cool function...\n"
19838                "      return 43;\n"
19839                "    });",
19840                LLVMWithBeforeLambdaBody);
19841   verifyFormat("FctWithMultipleParams_SLS_All("
19842                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
19843                "                              []() { return 17; });",
19844                LLVMWithBeforeLambdaBody);
19845   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
19846                LLVMWithBeforeLambdaBody);
19847   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
19848                LLVMWithBeforeLambdaBody);
19849   verifyFormat(
19850       "FctWithLongLineInLambda_SLS_All(\n"
19851       "    []()\n"
19852       "    {\n"
19853       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19854       "                               AndShouldNotBeConsiderAsInline,\n"
19855       "                               LambdaBodyMustBeBreak);\n"
19856       "    });",
19857       LLVMWithBeforeLambdaBody);
19858   verifyFormat(
19859       "auto fct_SLS_All = []()\n"
19860       "{\n"
19861       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19862       "                           AndShouldNotBeConsiderAsInline,\n"
19863       "                           LambdaBodyMustBeBreak);\n"
19864       "};",
19865       LLVMWithBeforeLambdaBody);
19866   LLVMWithBeforeLambdaBody.BinPackParameters = false;
19867   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
19868                LLVMWithBeforeLambdaBody);
19869   verifyFormat(
19870       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
19871       "                                FirstParam,\n"
19872       "                                SecondParam,\n"
19873       "                                ThirdParam,\n"
19874       "                                FourthParam);",
19875       LLVMWithBeforeLambdaBody);
19876   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19877                "    []() { return "
19878                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
19879                "    FirstParam,\n"
19880                "    SecondParam,\n"
19881                "    ThirdParam,\n"
19882                "    FourthParam);",
19883                LLVMWithBeforeLambdaBody);
19884   verifyFormat(
19885       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
19886       "                                SecondParam,\n"
19887       "                                ThirdParam,\n"
19888       "                                FourthParam,\n"
19889       "                                []() { return SomeValueNotSoLong; });",
19890       LLVMWithBeforeLambdaBody);
19891   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19892                "    []()\n"
19893                "    {\n"
19894                "      return "
19895                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
19896                "eConsiderAsInline;\n"
19897                "    });",
19898                LLVMWithBeforeLambdaBody);
19899   verifyFormat(
19900       "FctWithLongLineInLambda_SLS_All(\n"
19901       "    []()\n"
19902       "    {\n"
19903       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19904       "                               AndShouldNotBeConsiderAsInline,\n"
19905       "                               LambdaBodyMustBeBreak);\n"
19906       "    });",
19907       LLVMWithBeforeLambdaBody);
19908   verifyFormat("FctWithTwoParams_SLS_All(\n"
19909                "    []()\n"
19910                "    {\n"
19911                "      // A cool function...\n"
19912                "      return 43;\n"
19913                "    },\n"
19914                "    87);",
19915                LLVMWithBeforeLambdaBody);
19916   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
19917                LLVMWithBeforeLambdaBody);
19918   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
19919                LLVMWithBeforeLambdaBody);
19920   verifyFormat(
19921       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
19922       LLVMWithBeforeLambdaBody);
19923   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
19924                "}); }, x);",
19925                LLVMWithBeforeLambdaBody);
19926   verifyFormat("TwoNestedLambdas_SLS_All(\n"
19927                "    []()\n"
19928                "    {\n"
19929                "      // A cool function...\n"
19930                "      return Call([]() { return 17; });\n"
19931                "    });",
19932                LLVMWithBeforeLambdaBody);
19933   verifyFormat("TwoNestedLambdas_SLS_All(\n"
19934                "    []()\n"
19935                "    {\n"
19936                "      return Call(\n"
19937                "          []()\n"
19938                "          {\n"
19939                "            // A cool function...\n"
19940                "            return 17;\n"
19941                "          });\n"
19942                "    });",
19943                LLVMWithBeforeLambdaBody);
19944 
19945   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19946       FormatStyle::ShortLambdaStyle::SLS_None;
19947 
19948   verifyFormat("auto select = [this]() -> const Library::Object *\n"
19949                "{\n"
19950                "  return MyAssignment::SelectFromList(this);\n"
19951                "};\n",
19952                LLVMWithBeforeLambdaBody);
19953 
19954   verifyFormat("auto select = [this]() -> const Library::Object &\n"
19955                "{\n"
19956                "  return MyAssignment::SelectFromList(this);\n"
19957                "};\n",
19958                LLVMWithBeforeLambdaBody);
19959 
19960   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
19961                "{\n"
19962                "  return MyAssignment::SelectFromList(this);\n"
19963                "};\n",
19964                LLVMWithBeforeLambdaBody);
19965 
19966   verifyFormat("namespace test {\n"
19967                "class Test {\n"
19968                "public:\n"
19969                "  Test() = default;\n"
19970                "};\n"
19971                "} // namespace test",
19972                LLVMWithBeforeLambdaBody);
19973 
19974   // Lambdas with different indentation styles.
19975   Style = getLLVMStyleWithColumns(100);
19976   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
19977             "  return promise.then(\n"
19978             "      [this, &someVariable, someObject = "
19979             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
19980             "        return someObject.startAsyncAction().then(\n"
19981             "            [this, &someVariable](AsyncActionResult result) "
19982             "mutable { result.processMore(); });\n"
19983             "      });\n"
19984             "}\n",
19985             format("SomeResult doSomething(SomeObject promise) {\n"
19986                    "  return promise.then([this, &someVariable, someObject = "
19987                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
19988                    "    return someObject.startAsyncAction().then([this, "
19989                    "&someVariable](AsyncActionResult result) mutable {\n"
19990                    "      result.processMore();\n"
19991                    "    });\n"
19992                    "  });\n"
19993                    "}\n",
19994                    Style));
19995   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
19996   verifyFormat("test() {\n"
19997                "  ([]() -> {\n"
19998                "    int b = 32;\n"
19999                "    return 3;\n"
20000                "  }).foo();\n"
20001                "}",
20002                Style);
20003   verifyFormat("test() {\n"
20004                "  []() -> {\n"
20005                "    int b = 32;\n"
20006                "    return 3;\n"
20007                "  }\n"
20008                "}",
20009                Style);
20010   verifyFormat("std::sort(v.begin(), v.end(),\n"
20011                "          [](const auto &someLongArgumentName, const auto "
20012                "&someOtherLongArgumentName) {\n"
20013                "  return someLongArgumentName.someMemberVariable < "
20014                "someOtherLongArgumentName.someMemberVariable;\n"
20015                "});",
20016                Style);
20017   verifyFormat("test() {\n"
20018                "  (\n"
20019                "      []() -> {\n"
20020                "        int b = 32;\n"
20021                "        return 3;\n"
20022                "      },\n"
20023                "      foo, bar)\n"
20024                "      .foo();\n"
20025                "}",
20026                Style);
20027   verifyFormat("test() {\n"
20028                "  ([]() -> {\n"
20029                "    int b = 32;\n"
20030                "    return 3;\n"
20031                "  })\n"
20032                "      .foo()\n"
20033                "      .bar();\n"
20034                "}",
20035                Style);
20036   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20037             "  return promise.then(\n"
20038             "      [this, &someVariable, someObject = "
20039             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20040             "    return someObject.startAsyncAction().then(\n"
20041             "        [this, &someVariable](AsyncActionResult result) mutable { "
20042             "result.processMore(); });\n"
20043             "  });\n"
20044             "}\n",
20045             format("SomeResult doSomething(SomeObject promise) {\n"
20046                    "  return promise.then([this, &someVariable, someObject = "
20047                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20048                    "    return someObject.startAsyncAction().then([this, "
20049                    "&someVariable](AsyncActionResult result) mutable {\n"
20050                    "      result.processMore();\n"
20051                    "    });\n"
20052                    "  });\n"
20053                    "}\n",
20054                    Style));
20055   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20056             "  return promise.then([this, &someVariable] {\n"
20057             "    return someObject.startAsyncAction().then(\n"
20058             "        [this, &someVariable](AsyncActionResult result) mutable { "
20059             "result.processMore(); });\n"
20060             "  });\n"
20061             "}\n",
20062             format("SomeResult doSomething(SomeObject promise) {\n"
20063                    "  return promise.then([this, &someVariable] {\n"
20064                    "    return someObject.startAsyncAction().then([this, "
20065                    "&someVariable](AsyncActionResult result) mutable {\n"
20066                    "      result.processMore();\n"
20067                    "    });\n"
20068                    "  });\n"
20069                    "}\n",
20070                    Style));
20071   Style = getGoogleStyle();
20072   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20073   EXPECT_EQ("#define A                                       \\\n"
20074             "  [] {                                          \\\n"
20075             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
20076             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
20077             "      }",
20078             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
20079                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
20080                    Style));
20081   // TODO: The current formatting has a minor issue that's not worth fixing
20082   // right now whereby the closing brace is indented relative to the signature
20083   // instead of being aligned. This only happens with macros.
20084 }
20085 
20086 TEST_F(FormatTest, LambdaWithLineComments) {
20087   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20088   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20089   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20090   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20091       FormatStyle::ShortLambdaStyle::SLS_All;
20092 
20093   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
20094   verifyFormat("auto k = []() // comment\n"
20095                "{ return; }",
20096                LLVMWithBeforeLambdaBody);
20097   verifyFormat("auto k = []() /* comment */ { return; }",
20098                LLVMWithBeforeLambdaBody);
20099   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
20100                LLVMWithBeforeLambdaBody);
20101   verifyFormat("auto k = []() // X\n"
20102                "{ return; }",
20103                LLVMWithBeforeLambdaBody);
20104   verifyFormat(
20105       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
20106       "{ return; }",
20107       LLVMWithBeforeLambdaBody);
20108 }
20109 
20110 TEST_F(FormatTest, EmptyLinesInLambdas) {
20111   verifyFormat("auto lambda = []() {\n"
20112                "  x(); //\n"
20113                "};",
20114                "auto lambda = []() {\n"
20115                "\n"
20116                "  x(); //\n"
20117                "\n"
20118                "};");
20119 }
20120 
20121 TEST_F(FormatTest, FormatsBlocks) {
20122   FormatStyle ShortBlocks = getLLVMStyle();
20123   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20124   verifyFormat("int (^Block)(int, int);", ShortBlocks);
20125   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
20126   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
20127   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
20128   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
20129   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
20130 
20131   verifyFormat("foo(^{ bar(); });", ShortBlocks);
20132   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
20133   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
20134 
20135   verifyFormat("[operation setCompletionBlock:^{\n"
20136                "  [self onOperationDone];\n"
20137                "}];");
20138   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
20139                "  [self onOperationDone];\n"
20140                "}]};");
20141   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
20142                "  f();\n"
20143                "}];");
20144   verifyFormat("int a = [operation block:^int(int *i) {\n"
20145                "  return 1;\n"
20146                "}];");
20147   verifyFormat("[myObject doSomethingWith:arg1\n"
20148                "                      aaa:^int(int *a) {\n"
20149                "                        return 1;\n"
20150                "                      }\n"
20151                "                      bbb:f(a * bbbbbbbb)];");
20152 
20153   verifyFormat("[operation setCompletionBlock:^{\n"
20154                "  [self.delegate newDataAvailable];\n"
20155                "}];",
20156                getLLVMStyleWithColumns(60));
20157   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
20158                "  NSString *path = [self sessionFilePath];\n"
20159                "  if (path) {\n"
20160                "    // ...\n"
20161                "  }\n"
20162                "});");
20163   verifyFormat("[[SessionService sharedService]\n"
20164                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20165                "      if (window) {\n"
20166                "        [self windowDidLoad:window];\n"
20167                "      } else {\n"
20168                "        [self errorLoadingWindow];\n"
20169                "      }\n"
20170                "    }];");
20171   verifyFormat("void (^largeBlock)(void) = ^{\n"
20172                "  // ...\n"
20173                "};\n",
20174                getLLVMStyleWithColumns(40));
20175   verifyFormat("[[SessionService sharedService]\n"
20176                "    loadWindowWithCompletionBlock: //\n"
20177                "        ^(SessionWindow *window) {\n"
20178                "          if (window) {\n"
20179                "            [self windowDidLoad:window];\n"
20180                "          } else {\n"
20181                "            [self errorLoadingWindow];\n"
20182                "          }\n"
20183                "        }];",
20184                getLLVMStyleWithColumns(60));
20185   verifyFormat("[myObject doSomethingWith:arg1\n"
20186                "    firstBlock:^(Foo *a) {\n"
20187                "      // ...\n"
20188                "      int i;\n"
20189                "    }\n"
20190                "    secondBlock:^(Bar *b) {\n"
20191                "      // ...\n"
20192                "      int i;\n"
20193                "    }\n"
20194                "    thirdBlock:^Foo(Bar *b) {\n"
20195                "      // ...\n"
20196                "      int i;\n"
20197                "    }];");
20198   verifyFormat("[myObject doSomethingWith:arg1\n"
20199                "               firstBlock:-1\n"
20200                "              secondBlock:^(Bar *b) {\n"
20201                "                // ...\n"
20202                "                int i;\n"
20203                "              }];");
20204 
20205   verifyFormat("f(^{\n"
20206                "  @autoreleasepool {\n"
20207                "    if (a) {\n"
20208                "      g();\n"
20209                "    }\n"
20210                "  }\n"
20211                "});");
20212   verifyFormat("Block b = ^int *(A *a, B *b) {}");
20213   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
20214                "};");
20215 
20216   FormatStyle FourIndent = getLLVMStyle();
20217   FourIndent.ObjCBlockIndentWidth = 4;
20218   verifyFormat("[operation setCompletionBlock:^{\n"
20219                "    [self onOperationDone];\n"
20220                "}];",
20221                FourIndent);
20222 }
20223 
20224 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
20225   FormatStyle ZeroColumn = getLLVMStyle();
20226   ZeroColumn.ColumnLimit = 0;
20227 
20228   verifyFormat("[[SessionService sharedService] "
20229                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20230                "  if (window) {\n"
20231                "    [self windowDidLoad:window];\n"
20232                "  } else {\n"
20233                "    [self errorLoadingWindow];\n"
20234                "  }\n"
20235                "}];",
20236                ZeroColumn);
20237   EXPECT_EQ("[[SessionService sharedService]\n"
20238             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20239             "      if (window) {\n"
20240             "        [self windowDidLoad:window];\n"
20241             "      } else {\n"
20242             "        [self errorLoadingWindow];\n"
20243             "      }\n"
20244             "    }];",
20245             format("[[SessionService sharedService]\n"
20246                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20247                    "                if (window) {\n"
20248                    "    [self windowDidLoad:window];\n"
20249                    "  } else {\n"
20250                    "    [self errorLoadingWindow];\n"
20251                    "  }\n"
20252                    "}];",
20253                    ZeroColumn));
20254   verifyFormat("[myObject doSomethingWith:arg1\n"
20255                "    firstBlock:^(Foo *a) {\n"
20256                "      // ...\n"
20257                "      int i;\n"
20258                "    }\n"
20259                "    secondBlock:^(Bar *b) {\n"
20260                "      // ...\n"
20261                "      int i;\n"
20262                "    }\n"
20263                "    thirdBlock:^Foo(Bar *b) {\n"
20264                "      // ...\n"
20265                "      int i;\n"
20266                "    }];",
20267                ZeroColumn);
20268   verifyFormat("f(^{\n"
20269                "  @autoreleasepool {\n"
20270                "    if (a) {\n"
20271                "      g();\n"
20272                "    }\n"
20273                "  }\n"
20274                "});",
20275                ZeroColumn);
20276   verifyFormat("void (^largeBlock)(void) = ^{\n"
20277                "  // ...\n"
20278                "};",
20279                ZeroColumn);
20280 
20281   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20282   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
20283             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20284   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
20285   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
20286             "  int i;\n"
20287             "};",
20288             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20289 }
20290 
20291 TEST_F(FormatTest, SupportsCRLF) {
20292   EXPECT_EQ("int a;\r\n"
20293             "int b;\r\n"
20294             "int c;\r\n",
20295             format("int a;\r\n"
20296                    "  int b;\r\n"
20297                    "    int c;\r\n",
20298                    getLLVMStyle()));
20299   EXPECT_EQ("int a;\r\n"
20300             "int b;\r\n"
20301             "int c;\r\n",
20302             format("int a;\r\n"
20303                    "  int b;\n"
20304                    "    int c;\r\n",
20305                    getLLVMStyle()));
20306   EXPECT_EQ("int a;\n"
20307             "int b;\n"
20308             "int c;\n",
20309             format("int a;\r\n"
20310                    "  int b;\n"
20311                    "    int c;\n",
20312                    getLLVMStyle()));
20313   EXPECT_EQ("\"aaaaaaa \"\r\n"
20314             "\"bbbbbbb\";\r\n",
20315             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
20316   EXPECT_EQ("#define A \\\r\n"
20317             "  b;      \\\r\n"
20318             "  c;      \\\r\n"
20319             "  d;\r\n",
20320             format("#define A \\\r\n"
20321                    "  b; \\\r\n"
20322                    "  c; d; \r\n",
20323                    getGoogleStyle()));
20324 
20325   EXPECT_EQ("/*\r\n"
20326             "multi line block comments\r\n"
20327             "should not introduce\r\n"
20328             "an extra carriage return\r\n"
20329             "*/\r\n",
20330             format("/*\r\n"
20331                    "multi line block comments\r\n"
20332                    "should not introduce\r\n"
20333                    "an extra carriage return\r\n"
20334                    "*/\r\n"));
20335   EXPECT_EQ("/*\r\n"
20336             "\r\n"
20337             "*/",
20338             format("/*\r\n"
20339                    "    \r\r\r\n"
20340                    "*/"));
20341 
20342   FormatStyle style = getLLVMStyle();
20343 
20344   style.DeriveLineEnding = true;
20345   style.UseCRLF = false;
20346   EXPECT_EQ("union FooBarBazQux {\n"
20347             "  int foo;\n"
20348             "  int bar;\n"
20349             "  int baz;\n"
20350             "};",
20351             format("union FooBarBazQux {\r\n"
20352                    "  int foo;\n"
20353                    "  int bar;\r\n"
20354                    "  int baz;\n"
20355                    "};",
20356                    style));
20357   style.UseCRLF = true;
20358   EXPECT_EQ("union FooBarBazQux {\r\n"
20359             "  int foo;\r\n"
20360             "  int bar;\r\n"
20361             "  int baz;\r\n"
20362             "};",
20363             format("union FooBarBazQux {\r\n"
20364                    "  int foo;\n"
20365                    "  int bar;\r\n"
20366                    "  int baz;\n"
20367                    "};",
20368                    style));
20369 
20370   style.DeriveLineEnding = false;
20371   style.UseCRLF = false;
20372   EXPECT_EQ("union FooBarBazQux {\n"
20373             "  int foo;\n"
20374             "  int bar;\n"
20375             "  int baz;\n"
20376             "  int qux;\n"
20377             "};",
20378             format("union FooBarBazQux {\r\n"
20379                    "  int foo;\n"
20380                    "  int bar;\r\n"
20381                    "  int baz;\n"
20382                    "  int qux;\r\n"
20383                    "};",
20384                    style));
20385   style.UseCRLF = true;
20386   EXPECT_EQ("union FooBarBazQux {\r\n"
20387             "  int foo;\r\n"
20388             "  int bar;\r\n"
20389             "  int baz;\r\n"
20390             "  int qux;\r\n"
20391             "};",
20392             format("union FooBarBazQux {\r\n"
20393                    "  int foo;\n"
20394                    "  int bar;\r\n"
20395                    "  int baz;\n"
20396                    "  int qux;\n"
20397                    "};",
20398                    style));
20399 
20400   style.DeriveLineEnding = true;
20401   style.UseCRLF = false;
20402   EXPECT_EQ("union FooBarBazQux {\r\n"
20403             "  int foo;\r\n"
20404             "  int bar;\r\n"
20405             "  int baz;\r\n"
20406             "  int qux;\r\n"
20407             "};",
20408             format("union FooBarBazQux {\r\n"
20409                    "  int foo;\n"
20410                    "  int bar;\r\n"
20411                    "  int baz;\n"
20412                    "  int qux;\r\n"
20413                    "};",
20414                    style));
20415   style.UseCRLF = true;
20416   EXPECT_EQ("union FooBarBazQux {\n"
20417             "  int foo;\n"
20418             "  int bar;\n"
20419             "  int baz;\n"
20420             "  int qux;\n"
20421             "};",
20422             format("union FooBarBazQux {\r\n"
20423                    "  int foo;\n"
20424                    "  int bar;\r\n"
20425                    "  int baz;\n"
20426                    "  int qux;\n"
20427                    "};",
20428                    style));
20429 }
20430 
20431 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
20432   verifyFormat("MY_CLASS(C) {\n"
20433                "  int i;\n"
20434                "  int j;\n"
20435                "};");
20436 }
20437 
20438 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
20439   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
20440   TwoIndent.ContinuationIndentWidth = 2;
20441 
20442   EXPECT_EQ("int i =\n"
20443             "  longFunction(\n"
20444             "    arg);",
20445             format("int i = longFunction(arg);", TwoIndent));
20446 
20447   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
20448   SixIndent.ContinuationIndentWidth = 6;
20449 
20450   EXPECT_EQ("int i =\n"
20451             "      longFunction(\n"
20452             "            arg);",
20453             format("int i = longFunction(arg);", SixIndent));
20454 }
20455 
20456 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
20457   FormatStyle Style = getLLVMStyle();
20458   verifyFormat("int Foo::getter(\n"
20459                "    //\n"
20460                ") const {\n"
20461                "  return foo;\n"
20462                "}",
20463                Style);
20464   verifyFormat("void Foo::setter(\n"
20465                "    //\n"
20466                ") {\n"
20467                "  foo = 1;\n"
20468                "}",
20469                Style);
20470 }
20471 
20472 TEST_F(FormatTest, SpacesInAngles) {
20473   FormatStyle Spaces = getLLVMStyle();
20474   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20475 
20476   verifyFormat("vector< ::std::string > x1;", Spaces);
20477   verifyFormat("Foo< int, Bar > x2;", Spaces);
20478   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
20479 
20480   verifyFormat("static_cast< int >(arg);", Spaces);
20481   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
20482   verifyFormat("f< int, float >();", Spaces);
20483   verifyFormat("template <> g() {}", Spaces);
20484   verifyFormat("template < std::vector< int > > f() {}", Spaces);
20485   verifyFormat("std::function< void(int, int) > fct;", Spaces);
20486   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
20487                Spaces);
20488 
20489   Spaces.Standard = FormatStyle::LS_Cpp03;
20490   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20491   verifyFormat("A< A< int > >();", Spaces);
20492 
20493   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20494   verifyFormat("A<A<int> >();", Spaces);
20495 
20496   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20497   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
20498                Spaces);
20499   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
20500                Spaces);
20501 
20502   verifyFormat("A<A<int> >();", Spaces);
20503   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
20504   verifyFormat("A< A< int > >();", Spaces);
20505 
20506   Spaces.Standard = FormatStyle::LS_Cpp11;
20507   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20508   verifyFormat("A< A< int > >();", Spaces);
20509 
20510   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20511   verifyFormat("vector<::std::string> x4;", Spaces);
20512   verifyFormat("vector<int> x5;", Spaces);
20513   verifyFormat("Foo<int, Bar> x6;", Spaces);
20514   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20515 
20516   verifyFormat("A<A<int>>();", Spaces);
20517 
20518   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20519   verifyFormat("vector<::std::string> x4;", Spaces);
20520   verifyFormat("vector< ::std::string > x4;", Spaces);
20521   verifyFormat("vector<int> x5;", Spaces);
20522   verifyFormat("vector< int > x5;", Spaces);
20523   verifyFormat("Foo<int, Bar> x6;", Spaces);
20524   verifyFormat("Foo< int, Bar > x6;", Spaces);
20525   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20526   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
20527 
20528   verifyFormat("A<A<int>>();", Spaces);
20529   verifyFormat("A< A< int > >();", Spaces);
20530   verifyFormat("A<A<int > >();", Spaces);
20531   verifyFormat("A< A< int>>();", Spaces);
20532 }
20533 
20534 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
20535   FormatStyle Style = getLLVMStyle();
20536   Style.SpaceAfterTemplateKeyword = false;
20537   verifyFormat("template<int> void foo();", Style);
20538 }
20539 
20540 TEST_F(FormatTest, TripleAngleBrackets) {
20541   verifyFormat("f<<<1, 1>>>();");
20542   verifyFormat("f<<<1, 1, 1, s>>>();");
20543   verifyFormat("f<<<a, b, c, d>>>();");
20544   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
20545   verifyFormat("f<param><<<1, 1>>>();");
20546   verifyFormat("f<1><<<1, 1>>>();");
20547   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
20548   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20549                "aaaaaaaaaaa<<<\n    1, 1>>>();");
20550   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
20551                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
20552 }
20553 
20554 TEST_F(FormatTest, MergeLessLessAtEnd) {
20555   verifyFormat("<<");
20556   EXPECT_EQ("< < <", format("\\\n<<<"));
20557   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20558                "aaallvm::outs() <<");
20559   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20560                "aaaallvm::outs()\n    <<");
20561 }
20562 
20563 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
20564   std::string code = "#if A\n"
20565                      "#if B\n"
20566                      "a.\n"
20567                      "#endif\n"
20568                      "    a = 1;\n"
20569                      "#else\n"
20570                      "#endif\n"
20571                      "#if C\n"
20572                      "#else\n"
20573                      "#endif\n";
20574   EXPECT_EQ(code, format(code));
20575 }
20576 
20577 TEST_F(FormatTest, HandleConflictMarkers) {
20578   // Git/SVN conflict markers.
20579   EXPECT_EQ("int a;\n"
20580             "void f() {\n"
20581             "  callme(some(parameter1,\n"
20582             "<<<<<<< text by the vcs\n"
20583             "              parameter2),\n"
20584             "||||||| text by the vcs\n"
20585             "              parameter2),\n"
20586             "         parameter3,\n"
20587             "======= text by the vcs\n"
20588             "              parameter2, parameter3),\n"
20589             ">>>>>>> text by the vcs\n"
20590             "         otherparameter);\n",
20591             format("int a;\n"
20592                    "void f() {\n"
20593                    "  callme(some(parameter1,\n"
20594                    "<<<<<<< text by the vcs\n"
20595                    "  parameter2),\n"
20596                    "||||||| text by the vcs\n"
20597                    "  parameter2),\n"
20598                    "  parameter3,\n"
20599                    "======= text by the vcs\n"
20600                    "  parameter2,\n"
20601                    "  parameter3),\n"
20602                    ">>>>>>> text by the vcs\n"
20603                    "  otherparameter);\n"));
20604 
20605   // Perforce markers.
20606   EXPECT_EQ("void f() {\n"
20607             "  function(\n"
20608             ">>>> text by the vcs\n"
20609             "      parameter,\n"
20610             "==== text by the vcs\n"
20611             "      parameter,\n"
20612             "==== text by the vcs\n"
20613             "      parameter,\n"
20614             "<<<< text by the vcs\n"
20615             "      parameter);\n",
20616             format("void f() {\n"
20617                    "  function(\n"
20618                    ">>>> text by the vcs\n"
20619                    "  parameter,\n"
20620                    "==== text by the vcs\n"
20621                    "  parameter,\n"
20622                    "==== text by the vcs\n"
20623                    "  parameter,\n"
20624                    "<<<< text by the vcs\n"
20625                    "  parameter);\n"));
20626 
20627   EXPECT_EQ("<<<<<<<\n"
20628             "|||||||\n"
20629             "=======\n"
20630             ">>>>>>>",
20631             format("<<<<<<<\n"
20632                    "|||||||\n"
20633                    "=======\n"
20634                    ">>>>>>>"));
20635 
20636   EXPECT_EQ("<<<<<<<\n"
20637             "|||||||\n"
20638             "int i;\n"
20639             "=======\n"
20640             ">>>>>>>",
20641             format("<<<<<<<\n"
20642                    "|||||||\n"
20643                    "int i;\n"
20644                    "=======\n"
20645                    ">>>>>>>"));
20646 
20647   // FIXME: Handle parsing of macros around conflict markers correctly:
20648   EXPECT_EQ("#define Macro \\\n"
20649             "<<<<<<<\n"
20650             "Something \\\n"
20651             "|||||||\n"
20652             "Else \\\n"
20653             "=======\n"
20654             "Other \\\n"
20655             ">>>>>>>\n"
20656             "    End int i;\n",
20657             format("#define Macro \\\n"
20658                    "<<<<<<<\n"
20659                    "  Something \\\n"
20660                    "|||||||\n"
20661                    "  Else \\\n"
20662                    "=======\n"
20663                    "  Other \\\n"
20664                    ">>>>>>>\n"
20665                    "  End\n"
20666                    "int i;\n"));
20667 }
20668 
20669 TEST_F(FormatTest, DisableRegions) {
20670   EXPECT_EQ("int i;\n"
20671             "// clang-format off\n"
20672             "  int j;\n"
20673             "// clang-format on\n"
20674             "int k;",
20675             format(" int  i;\n"
20676                    "   // clang-format off\n"
20677                    "  int j;\n"
20678                    " // clang-format on\n"
20679                    "   int   k;"));
20680   EXPECT_EQ("int i;\n"
20681             "/* clang-format off */\n"
20682             "  int j;\n"
20683             "/* clang-format on */\n"
20684             "int k;",
20685             format(" int  i;\n"
20686                    "   /* clang-format off */\n"
20687                    "  int j;\n"
20688                    " /* clang-format on */\n"
20689                    "   int   k;"));
20690 
20691   // Don't reflow comments within disabled regions.
20692   EXPECT_EQ("// clang-format off\n"
20693             "// long long long long long long line\n"
20694             "/* clang-format on */\n"
20695             "/* long long long\n"
20696             " * long long long\n"
20697             " * line */\n"
20698             "int i;\n"
20699             "/* clang-format off */\n"
20700             "/* long long long long long long line */\n",
20701             format("// clang-format off\n"
20702                    "// long long long long long long line\n"
20703                    "/* clang-format on */\n"
20704                    "/* long long long long long long line */\n"
20705                    "int i;\n"
20706                    "/* clang-format off */\n"
20707                    "/* long long long long long long line */\n",
20708                    getLLVMStyleWithColumns(20)));
20709 }
20710 
20711 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
20712   format("? ) =");
20713   verifyNoCrash("#define a\\\n /**/}");
20714 }
20715 
20716 TEST_F(FormatTest, FormatsTableGenCode) {
20717   FormatStyle Style = getLLVMStyle();
20718   Style.Language = FormatStyle::LK_TableGen;
20719   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
20720 }
20721 
20722 TEST_F(FormatTest, ArrayOfTemplates) {
20723   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
20724             format("auto a = new unique_ptr<int > [ 10];"));
20725 
20726   FormatStyle Spaces = getLLVMStyle();
20727   Spaces.SpacesInSquareBrackets = true;
20728   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
20729             format("auto a = new unique_ptr<int > [10];", Spaces));
20730 }
20731 
20732 TEST_F(FormatTest, ArrayAsTemplateType) {
20733   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
20734             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
20735 
20736   FormatStyle Spaces = getLLVMStyle();
20737   Spaces.SpacesInSquareBrackets = true;
20738   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
20739             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
20740 }
20741 
20742 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
20743 
20744 TEST(FormatStyle, GetStyleWithEmptyFileName) {
20745   llvm::vfs::InMemoryFileSystem FS;
20746   auto Style1 = getStyle("file", "", "Google", "", &FS);
20747   ASSERT_TRUE((bool)Style1);
20748   ASSERT_EQ(*Style1, getGoogleStyle());
20749 }
20750 
20751 TEST(FormatStyle, GetStyleOfFile) {
20752   llvm::vfs::InMemoryFileSystem FS;
20753   // Test 1: format file in the same directory.
20754   ASSERT_TRUE(
20755       FS.addFile("/a/.clang-format", 0,
20756                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
20757   ASSERT_TRUE(
20758       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20759   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
20760   ASSERT_TRUE((bool)Style1);
20761   ASSERT_EQ(*Style1, getLLVMStyle());
20762 
20763   // Test 2.1: fallback to default.
20764   ASSERT_TRUE(
20765       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20766   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
20767   ASSERT_TRUE((bool)Style2);
20768   ASSERT_EQ(*Style2, getMozillaStyle());
20769 
20770   // Test 2.2: no format on 'none' fallback style.
20771   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
20772   ASSERT_TRUE((bool)Style2);
20773   ASSERT_EQ(*Style2, getNoStyle());
20774 
20775   // Test 2.3: format if config is found with no based style while fallback is
20776   // 'none'.
20777   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
20778                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
20779   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
20780   ASSERT_TRUE((bool)Style2);
20781   ASSERT_EQ(*Style2, getLLVMStyle());
20782 
20783   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
20784   Style2 = getStyle("{}", "a.h", "none", "", &FS);
20785   ASSERT_TRUE((bool)Style2);
20786   ASSERT_EQ(*Style2, getLLVMStyle());
20787 
20788   // Test 3: format file in parent directory.
20789   ASSERT_TRUE(
20790       FS.addFile("/c/.clang-format", 0,
20791                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
20792   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
20793                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20794   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
20795   ASSERT_TRUE((bool)Style3);
20796   ASSERT_EQ(*Style3, getGoogleStyle());
20797 
20798   // Test 4: error on invalid fallback style
20799   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
20800   ASSERT_FALSE((bool)Style4);
20801   llvm::consumeError(Style4.takeError());
20802 
20803   // Test 5: error on invalid yaml on command line
20804   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
20805   ASSERT_FALSE((bool)Style5);
20806   llvm::consumeError(Style5.takeError());
20807 
20808   // Test 6: error on invalid style
20809   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
20810   ASSERT_FALSE((bool)Style6);
20811   llvm::consumeError(Style6.takeError());
20812 
20813   // Test 7: found config file, error on parsing it
20814   ASSERT_TRUE(
20815       FS.addFile("/d/.clang-format", 0,
20816                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
20817                                                   "InvalidKey: InvalidValue")));
20818   ASSERT_TRUE(
20819       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20820   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
20821   ASSERT_FALSE((bool)Style7a);
20822   llvm::consumeError(Style7a.takeError());
20823 
20824   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
20825   ASSERT_TRUE((bool)Style7b);
20826 
20827   // Test 8: inferred per-language defaults apply.
20828   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
20829   ASSERT_TRUE((bool)StyleTd);
20830   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
20831 
20832   // Test 9.1: overwriting a file style, when parent no file exists with no
20833   // fallback style
20834   ASSERT_TRUE(FS.addFile(
20835       "/e/sub/.clang-format", 0,
20836       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
20837                                        "ColumnLimit: 20")));
20838   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
20839                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20840   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
20841   ASSERT_TRUE(static_cast<bool>(Style9));
20842   ASSERT_EQ(*Style9, [] {
20843     auto Style = getNoStyle();
20844     Style.ColumnLimit = 20;
20845     return Style;
20846   }());
20847 
20848   // Test 9.2: with LLVM fallback style
20849   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
20850   ASSERT_TRUE(static_cast<bool>(Style9));
20851   ASSERT_EQ(*Style9, [] {
20852     auto Style = getLLVMStyle();
20853     Style.ColumnLimit = 20;
20854     return Style;
20855   }());
20856 
20857   // Test 9.3: with a parent file
20858   ASSERT_TRUE(
20859       FS.addFile("/e/.clang-format", 0,
20860                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
20861                                                   "UseTab: Always")));
20862   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
20863   ASSERT_TRUE(static_cast<bool>(Style9));
20864   ASSERT_EQ(*Style9, [] {
20865     auto Style = getGoogleStyle();
20866     Style.ColumnLimit = 20;
20867     Style.UseTab = FormatStyle::UT_Always;
20868     return Style;
20869   }());
20870 
20871   // Test 9.4: propagate more than one level
20872   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
20873                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20874   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
20875                          llvm::MemoryBuffer::getMemBuffer(
20876                              "BasedOnStyle: InheritParentConfig\n"
20877                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
20878   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
20879 
20880   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
20881     auto Style = getGoogleStyle();
20882     Style.ColumnLimit = 20;
20883     Style.UseTab = FormatStyle::UT_Always;
20884     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
20885     return Style;
20886   }();
20887 
20888   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
20889   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
20890   ASSERT_TRUE(static_cast<bool>(Style9));
20891   ASSERT_EQ(*Style9, SubSubStyle);
20892 
20893   // Test 9.5: use InheritParentConfig as style name
20894   Style9 =
20895       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
20896   ASSERT_TRUE(static_cast<bool>(Style9));
20897   ASSERT_EQ(*Style9, SubSubStyle);
20898 
20899   // Test 9.6: use command line style with inheritance
20900   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
20901                     "none", "", &FS);
20902   ASSERT_TRUE(static_cast<bool>(Style9));
20903   ASSERT_EQ(*Style9, SubSubStyle);
20904 
20905   // Test 9.7: use command line style with inheritance and own config
20906   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
20907                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
20908                     "/e/sub/code.cpp", "none", "", &FS);
20909   ASSERT_TRUE(static_cast<bool>(Style9));
20910   ASSERT_EQ(*Style9, SubSubStyle);
20911 
20912   // Test 9.8: use inheritance from a file without BasedOnStyle
20913   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
20914                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
20915   ASSERT_TRUE(
20916       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
20917                  llvm::MemoryBuffer::getMemBuffer(
20918                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
20919   // Make sure we do not use the fallback style
20920   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
20921   ASSERT_TRUE(static_cast<bool>(Style9));
20922   ASSERT_EQ(*Style9, [] {
20923     auto Style = getLLVMStyle();
20924     Style.ColumnLimit = 123;
20925     return Style;
20926   }());
20927 
20928   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
20929   ASSERT_TRUE(static_cast<bool>(Style9));
20930   ASSERT_EQ(*Style9, [] {
20931     auto Style = getLLVMStyle();
20932     Style.ColumnLimit = 123;
20933     Style.IndentWidth = 7;
20934     return Style;
20935   }());
20936 }
20937 
20938 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
20939   // Column limit is 20.
20940   std::string Code = "Type *a =\n"
20941                      "    new Type();\n"
20942                      "g(iiiii, 0, jjjjj,\n"
20943                      "  0, kkkkk, 0, mm);\n"
20944                      "int  bad     = format   ;";
20945   std::string Expected = "auto a = new Type();\n"
20946                          "g(iiiii, nullptr,\n"
20947                          "  jjjjj, nullptr,\n"
20948                          "  kkkkk, nullptr,\n"
20949                          "  mm);\n"
20950                          "int  bad     = format   ;";
20951   FileID ID = Context.createInMemoryFile("format.cpp", Code);
20952   tooling::Replacements Replaces = toReplacements(
20953       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
20954                             "auto "),
20955        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
20956                             "nullptr"),
20957        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
20958                             "nullptr"),
20959        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
20960                             "nullptr")});
20961 
20962   format::FormatStyle Style = format::getLLVMStyle();
20963   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
20964   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
20965   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
20966       << llvm::toString(FormattedReplaces.takeError()) << "\n";
20967   auto Result = applyAllReplacements(Code, *FormattedReplaces);
20968   EXPECT_TRUE(static_cast<bool>(Result));
20969   EXPECT_EQ(Expected, *Result);
20970 }
20971 
20972 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
20973   std::string Code = "#include \"a.h\"\n"
20974                      "#include \"c.h\"\n"
20975                      "\n"
20976                      "int main() {\n"
20977                      "  return 0;\n"
20978                      "}";
20979   std::string Expected = "#include \"a.h\"\n"
20980                          "#include \"b.h\"\n"
20981                          "#include \"c.h\"\n"
20982                          "\n"
20983                          "int main() {\n"
20984                          "  return 0;\n"
20985                          "}";
20986   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
20987   tooling::Replacements Replaces = toReplacements(
20988       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
20989                             "#include \"b.h\"\n")});
20990 
20991   format::FormatStyle Style = format::getLLVMStyle();
20992   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
20993   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
20994   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
20995       << llvm::toString(FormattedReplaces.takeError()) << "\n";
20996   auto Result = applyAllReplacements(Code, *FormattedReplaces);
20997   EXPECT_TRUE(static_cast<bool>(Result));
20998   EXPECT_EQ(Expected, *Result);
20999 }
21000 
21001 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
21002   EXPECT_EQ("using std::cin;\n"
21003             "using std::cout;",
21004             format("using std::cout;\n"
21005                    "using std::cin;",
21006                    getGoogleStyle()));
21007 }
21008 
21009 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
21010   format::FormatStyle Style = format::getLLVMStyle();
21011   Style.Standard = FormatStyle::LS_Cpp03;
21012   // cpp03 recognize this string as identifier u8 and literal character 'a'
21013   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
21014 }
21015 
21016 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
21017   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
21018   // all modes, including C++11, C++14 and C++17
21019   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
21020 }
21021 
21022 TEST_F(FormatTest, DoNotFormatLikelyXml) {
21023   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
21024   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
21025 }
21026 
21027 TEST_F(FormatTest, StructuredBindings) {
21028   // Structured bindings is a C++17 feature.
21029   // all modes, including C++11, C++14 and C++17
21030   verifyFormat("auto [a, b] = f();");
21031   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
21032   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
21033   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
21034   EXPECT_EQ("auto const volatile [a, b] = f();",
21035             format("auto  const   volatile[a, b] = f();"));
21036   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
21037   EXPECT_EQ("auto &[a, b, c] = f();",
21038             format("auto   &[  a  ,  b,c   ] = f();"));
21039   EXPECT_EQ("auto &&[a, b, c] = f();",
21040             format("auto   &&[  a  ,  b,c   ] = f();"));
21041   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
21042   EXPECT_EQ("auto const volatile &&[a, b] = f();",
21043             format("auto  const  volatile  &&[a, b] = f();"));
21044   EXPECT_EQ("auto const &&[a, b] = f();",
21045             format("auto  const   &&  [a, b] = f();"));
21046   EXPECT_EQ("const auto &[a, b] = f();",
21047             format("const  auto  &  [a, b] = f();"));
21048   EXPECT_EQ("const auto volatile &&[a, b] = f();",
21049             format("const  auto   volatile  &&[a, b] = f();"));
21050   EXPECT_EQ("volatile const auto &&[a, b] = f();",
21051             format("volatile  const  auto   &&[a, b] = f();"));
21052   EXPECT_EQ("const auto &&[a, b] = f();",
21053             format("const  auto  &&  [a, b] = f();"));
21054 
21055   // Make sure we don't mistake structured bindings for lambdas.
21056   FormatStyle PointerMiddle = getLLVMStyle();
21057   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
21058   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
21059   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
21060   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
21061   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
21062   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
21063   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
21064   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
21065   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
21066   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
21067   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
21068   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
21069   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
21070 
21071   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
21072             format("for (const auto   &&   [a, b] : some_range) {\n}"));
21073   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
21074             format("for (const auto   &   [a, b] : some_range) {\n}"));
21075   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
21076             format("for (const auto[a, b] : some_range) {\n}"));
21077   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
21078   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
21079   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
21080   EXPECT_EQ("auto const &[x, y](expr);",
21081             format("auto  const  &  [x,y]  (expr);"));
21082   EXPECT_EQ("auto const &&[x, y](expr);",
21083             format("auto  const  &&  [x,y]  (expr);"));
21084   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
21085   EXPECT_EQ("auto const &[x, y]{expr};",
21086             format("auto  const  &  [x,y]  {expr};"));
21087   EXPECT_EQ("auto const &&[x, y]{expr};",
21088             format("auto  const  &&  [x,y]  {expr};"));
21089 
21090   format::FormatStyle Spaces = format::getLLVMStyle();
21091   Spaces.SpacesInSquareBrackets = true;
21092   verifyFormat("auto [ a, b ] = f();", Spaces);
21093   verifyFormat("auto &&[ a, b ] = f();", Spaces);
21094   verifyFormat("auto &[ a, b ] = f();", Spaces);
21095   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
21096   verifyFormat("auto const &[ a, b ] = f();", Spaces);
21097 }
21098 
21099 TEST_F(FormatTest, FileAndCode) {
21100   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
21101   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
21102   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
21103   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
21104   EXPECT_EQ(FormatStyle::LK_ObjC,
21105             guessLanguage("foo.h", "@interface Foo\n@end\n"));
21106   EXPECT_EQ(
21107       FormatStyle::LK_ObjC,
21108       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
21109   EXPECT_EQ(FormatStyle::LK_ObjC,
21110             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
21111   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
21112   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
21113   EXPECT_EQ(FormatStyle::LK_ObjC,
21114             guessLanguage("foo", "@interface Foo\n@end\n"));
21115   EXPECT_EQ(FormatStyle::LK_ObjC,
21116             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
21117   EXPECT_EQ(
21118       FormatStyle::LK_ObjC,
21119       guessLanguage("foo.h",
21120                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
21121   EXPECT_EQ(
21122       FormatStyle::LK_Cpp,
21123       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
21124 }
21125 
21126 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
21127   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
21128   EXPECT_EQ(FormatStyle::LK_ObjC,
21129             guessLanguage("foo.h", "array[[calculator getIndex]];"));
21130   EXPECT_EQ(FormatStyle::LK_Cpp,
21131             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
21132   EXPECT_EQ(
21133       FormatStyle::LK_Cpp,
21134       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
21135   EXPECT_EQ(FormatStyle::LK_ObjC,
21136             guessLanguage("foo.h", "[[noreturn foo] bar];"));
21137   EXPECT_EQ(FormatStyle::LK_Cpp,
21138             guessLanguage("foo.h", "[[clang::fallthrough]];"));
21139   EXPECT_EQ(FormatStyle::LK_ObjC,
21140             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
21141   EXPECT_EQ(FormatStyle::LK_Cpp,
21142             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
21143   EXPECT_EQ(FormatStyle::LK_Cpp,
21144             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
21145   EXPECT_EQ(FormatStyle::LK_ObjC,
21146             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
21147   EXPECT_EQ(FormatStyle::LK_Cpp,
21148             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
21149   EXPECT_EQ(
21150       FormatStyle::LK_Cpp,
21151       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
21152   EXPECT_EQ(
21153       FormatStyle::LK_Cpp,
21154       guessLanguage("foo.h",
21155                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
21156   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
21157 }
21158 
21159 TEST_F(FormatTest, GuessLanguageWithCaret) {
21160   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
21161   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
21162   EXPECT_EQ(FormatStyle::LK_ObjC,
21163             guessLanguage("foo.h", "int(^)(char, float);"));
21164   EXPECT_EQ(FormatStyle::LK_ObjC,
21165             guessLanguage("foo.h", "int(^foo)(char, float);"));
21166   EXPECT_EQ(FormatStyle::LK_ObjC,
21167             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
21168   EXPECT_EQ(FormatStyle::LK_ObjC,
21169             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
21170   EXPECT_EQ(
21171       FormatStyle::LK_ObjC,
21172       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
21173 }
21174 
21175 TEST_F(FormatTest, GuessLanguageWithPragmas) {
21176   EXPECT_EQ(FormatStyle::LK_Cpp,
21177             guessLanguage("foo.h", "__pragma(warning(disable:))"));
21178   EXPECT_EQ(FormatStyle::LK_Cpp,
21179             guessLanguage("foo.h", "#pragma(warning(disable:))"));
21180   EXPECT_EQ(FormatStyle::LK_Cpp,
21181             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
21182 }
21183 
21184 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
21185   // ASM symbolic names are identifiers that must be surrounded by [] without
21186   // space in between:
21187   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
21188 
21189   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
21190   verifyFormat(R"(//
21191 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
21192 )");
21193 
21194   // A list of several ASM symbolic names.
21195   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
21196 
21197   // ASM symbolic names in inline ASM with inputs and outputs.
21198   verifyFormat(R"(//
21199 asm("cmoveq %1, %2, %[result]"
21200     : [result] "=r"(result)
21201     : "r"(test), "r"(new), "[result]"(old));
21202 )");
21203 
21204   // ASM symbolic names in inline ASM with no outputs.
21205   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
21206 }
21207 
21208 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
21209   EXPECT_EQ(FormatStyle::LK_Cpp,
21210             guessLanguage("foo.h", "void f() {\n"
21211                                    "  asm (\"mov %[e], %[d]\"\n"
21212                                    "     : [d] \"=rm\" (d)\n"
21213                                    "       [e] \"rm\" (*e));\n"
21214                                    "}"));
21215   EXPECT_EQ(FormatStyle::LK_Cpp,
21216             guessLanguage("foo.h", "void f() {\n"
21217                                    "  _asm (\"mov %[e], %[d]\"\n"
21218                                    "     : [d] \"=rm\" (d)\n"
21219                                    "       [e] \"rm\" (*e));\n"
21220                                    "}"));
21221   EXPECT_EQ(FormatStyle::LK_Cpp,
21222             guessLanguage("foo.h", "void f() {\n"
21223                                    "  __asm (\"mov %[e], %[d]\"\n"
21224                                    "     : [d] \"=rm\" (d)\n"
21225                                    "       [e] \"rm\" (*e));\n"
21226                                    "}"));
21227   EXPECT_EQ(FormatStyle::LK_Cpp,
21228             guessLanguage("foo.h", "void f() {\n"
21229                                    "  __asm__ (\"mov %[e], %[d]\"\n"
21230                                    "     : [d] \"=rm\" (d)\n"
21231                                    "       [e] \"rm\" (*e));\n"
21232                                    "}"));
21233   EXPECT_EQ(FormatStyle::LK_Cpp,
21234             guessLanguage("foo.h", "void f() {\n"
21235                                    "  asm (\"mov %[e], %[d]\"\n"
21236                                    "     : [d] \"=rm\" (d),\n"
21237                                    "       [e] \"rm\" (*e));\n"
21238                                    "}"));
21239   EXPECT_EQ(FormatStyle::LK_Cpp,
21240             guessLanguage("foo.h", "void f() {\n"
21241                                    "  asm volatile (\"mov %[e], %[d]\"\n"
21242                                    "     : [d] \"=rm\" (d)\n"
21243                                    "       [e] \"rm\" (*e));\n"
21244                                    "}"));
21245 }
21246 
21247 TEST_F(FormatTest, GuessLanguageWithChildLines) {
21248   EXPECT_EQ(FormatStyle::LK_Cpp,
21249             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
21250   EXPECT_EQ(FormatStyle::LK_ObjC,
21251             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
21252   EXPECT_EQ(
21253       FormatStyle::LK_Cpp,
21254       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
21255   EXPECT_EQ(
21256       FormatStyle::LK_ObjC,
21257       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
21258 }
21259 
21260 TEST_F(FormatTest, TypenameMacros) {
21261   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
21262 
21263   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
21264   FormatStyle Google = getGoogleStyleWithColumns(0);
21265   Google.TypenameMacros = TypenameMacros;
21266   verifyFormat("struct foo {\n"
21267                "  int bar;\n"
21268                "  TAILQ_ENTRY(a) bleh;\n"
21269                "};",
21270                Google);
21271 
21272   FormatStyle Macros = getLLVMStyle();
21273   Macros.TypenameMacros = TypenameMacros;
21274 
21275   verifyFormat("STACK_OF(int) a;", Macros);
21276   verifyFormat("STACK_OF(int) *a;", Macros);
21277   verifyFormat("STACK_OF(int const *) *a;", Macros);
21278   verifyFormat("STACK_OF(int *const) *a;", Macros);
21279   verifyFormat("STACK_OF(int, string) a;", Macros);
21280   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
21281   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
21282   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
21283   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
21284   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
21285   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
21286 
21287   Macros.PointerAlignment = FormatStyle::PAS_Left;
21288   verifyFormat("STACK_OF(int)* a;", Macros);
21289   verifyFormat("STACK_OF(int*)* a;", Macros);
21290   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
21291   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
21292   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
21293 }
21294 
21295 TEST_F(FormatTest, AtomicQualifier) {
21296   // Check that we treate _Atomic as a type and not a function call
21297   FormatStyle Google = getGoogleStyleWithColumns(0);
21298   verifyFormat("struct foo {\n"
21299                "  int a1;\n"
21300                "  _Atomic(a) a2;\n"
21301                "  _Atomic(_Atomic(int) *const) a3;\n"
21302                "};",
21303                Google);
21304   verifyFormat("_Atomic(uint64_t) a;");
21305   verifyFormat("_Atomic(uint64_t) *a;");
21306   verifyFormat("_Atomic(uint64_t const *) *a;");
21307   verifyFormat("_Atomic(uint64_t *const) *a;");
21308   verifyFormat("_Atomic(const uint64_t *) *a;");
21309   verifyFormat("_Atomic(uint64_t) a;");
21310   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
21311   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
21312   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
21313   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
21314 
21315   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
21316   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
21317   FormatStyle Style = getLLVMStyle();
21318   Style.PointerAlignment = FormatStyle::PAS_Left;
21319   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
21320   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
21321   verifyFormat("_Atomic(int)* a;", Style);
21322   verifyFormat("_Atomic(int*)* a;", Style);
21323   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
21324 
21325   Style.SpacesInCStyleCastParentheses = true;
21326   Style.SpacesInParentheses = false;
21327   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
21328   Style.SpacesInCStyleCastParentheses = false;
21329   Style.SpacesInParentheses = true;
21330   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
21331   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
21332 }
21333 
21334 TEST_F(FormatTest, AmbersandInLamda) {
21335   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
21336   FormatStyle AlignStyle = getLLVMStyle();
21337   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
21338   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21339   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
21340   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21341 }
21342 
21343 TEST_F(FormatTest, SpacesInConditionalStatement) {
21344   FormatStyle Spaces = getLLVMStyle();
21345   Spaces.IfMacros.clear();
21346   Spaces.IfMacros.push_back("MYIF");
21347   Spaces.SpacesInConditionalStatement = true;
21348   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
21349   verifyFormat("if ( !a )\n  return;", Spaces);
21350   verifyFormat("if ( a )\n  return;", Spaces);
21351   verifyFormat("if constexpr ( a )\n  return;", Spaces);
21352   verifyFormat("MYIF ( a )\n  return;", Spaces);
21353   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
21354   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
21355   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
21356   verifyFormat("while ( a )\n  return;", Spaces);
21357   verifyFormat("while ( (a && b) )\n  return;", Spaces);
21358   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
21359   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
21360   // Check that space on the left of "::" is inserted as expected at beginning
21361   // of condition.
21362   verifyFormat("while ( ::func() )\n  return;", Spaces);
21363 
21364   // Check impact of ControlStatementsExceptControlMacros is honored.
21365   Spaces.SpaceBeforeParens =
21366       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
21367   verifyFormat("MYIF( a )\n  return;", Spaces);
21368   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
21369   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
21370 }
21371 
21372 TEST_F(FormatTest, AlternativeOperators) {
21373   // Test case for ensuring alternate operators are not
21374   // combined with their right most neighbour.
21375   verifyFormat("int a and b;");
21376   verifyFormat("int a and_eq b;");
21377   verifyFormat("int a bitand b;");
21378   verifyFormat("int a bitor b;");
21379   verifyFormat("int a compl b;");
21380   verifyFormat("int a not b;");
21381   verifyFormat("int a not_eq b;");
21382   verifyFormat("int a or b;");
21383   verifyFormat("int a xor b;");
21384   verifyFormat("int a xor_eq b;");
21385   verifyFormat("return this not_eq bitand other;");
21386   verifyFormat("bool operator not_eq(const X bitand other)");
21387 
21388   verifyFormat("int a and 5;");
21389   verifyFormat("int a and_eq 5;");
21390   verifyFormat("int a bitand 5;");
21391   verifyFormat("int a bitor 5;");
21392   verifyFormat("int a compl 5;");
21393   verifyFormat("int a not 5;");
21394   verifyFormat("int a not_eq 5;");
21395   verifyFormat("int a or 5;");
21396   verifyFormat("int a xor 5;");
21397   verifyFormat("int a xor_eq 5;");
21398 
21399   verifyFormat("int a compl(5);");
21400   verifyFormat("int a not(5);");
21401 
21402   /* FIXME handle alternate tokens
21403    * https://en.cppreference.com/w/cpp/language/operator_alternative
21404   // alternative tokens
21405   verifyFormat("compl foo();");     //  ~foo();
21406   verifyFormat("foo() <%%>;");      // foo();
21407   verifyFormat("void foo() <%%>;"); // void foo(){}
21408   verifyFormat("int a <:1:>;");     // int a[1];[
21409   verifyFormat("%:define ABC abc"); // #define ABC abc
21410   verifyFormat("%:%:");             // ##
21411   */
21412 }
21413 
21414 TEST_F(FormatTest, STLWhileNotDefineChed) {
21415   verifyFormat("#if defined(while)\n"
21416                "#define while EMIT WARNING C4005\n"
21417                "#endif // while");
21418 }
21419 
21420 TEST_F(FormatTest, OperatorSpacing) {
21421   FormatStyle Style = getLLVMStyle();
21422   Style.PointerAlignment = FormatStyle::PAS_Right;
21423   verifyFormat("Foo::operator*();", Style);
21424   verifyFormat("Foo::operator void *();", Style);
21425   verifyFormat("Foo::operator void **();", Style);
21426   verifyFormat("Foo::operator void *&();", Style);
21427   verifyFormat("Foo::operator void *&&();", Style);
21428   verifyFormat("Foo::operator void const *();", Style);
21429   verifyFormat("Foo::operator void const **();", Style);
21430   verifyFormat("Foo::operator void const *&();", Style);
21431   verifyFormat("Foo::operator void const *&&();", Style);
21432   verifyFormat("Foo::operator()(void *);", Style);
21433   verifyFormat("Foo::operator*(void *);", Style);
21434   verifyFormat("Foo::operator*();", Style);
21435   verifyFormat("Foo::operator**();", Style);
21436   verifyFormat("Foo::operator&();", Style);
21437   verifyFormat("Foo::operator<int> *();", Style);
21438   verifyFormat("Foo::operator<Foo> *();", Style);
21439   verifyFormat("Foo::operator<int> **();", Style);
21440   verifyFormat("Foo::operator<Foo> **();", Style);
21441   verifyFormat("Foo::operator<int> &();", Style);
21442   verifyFormat("Foo::operator<Foo> &();", Style);
21443   verifyFormat("Foo::operator<int> &&();", Style);
21444   verifyFormat("Foo::operator<Foo> &&();", Style);
21445   verifyFormat("Foo::operator<int> *&();", Style);
21446   verifyFormat("Foo::operator<Foo> *&();", Style);
21447   verifyFormat("Foo::operator<int> *&&();", Style);
21448   verifyFormat("Foo::operator<Foo> *&&();", Style);
21449   verifyFormat("operator*(int (*)(), class Foo);", Style);
21450 
21451   verifyFormat("Foo::operator&();", Style);
21452   verifyFormat("Foo::operator void &();", Style);
21453   verifyFormat("Foo::operator void const &();", Style);
21454   verifyFormat("Foo::operator()(void &);", Style);
21455   verifyFormat("Foo::operator&(void &);", Style);
21456   verifyFormat("Foo::operator&();", Style);
21457   verifyFormat("operator&(int (&)(), class Foo);", Style);
21458 
21459   verifyFormat("Foo::operator&&();", Style);
21460   verifyFormat("Foo::operator**();", Style);
21461   verifyFormat("Foo::operator void &&();", Style);
21462   verifyFormat("Foo::operator void const &&();", Style);
21463   verifyFormat("Foo::operator()(void &&);", Style);
21464   verifyFormat("Foo::operator&&(void &&);", Style);
21465   verifyFormat("Foo::operator&&();", Style);
21466   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21467   verifyFormat("operator const nsTArrayRight<E> &()", Style);
21468   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
21469                Style);
21470   verifyFormat("operator void **()", Style);
21471   verifyFormat("operator const FooRight<Object> &()", Style);
21472   verifyFormat("operator const FooRight<Object> *()", Style);
21473   verifyFormat("operator const FooRight<Object> **()", Style);
21474   verifyFormat("operator const FooRight<Object> *&()", Style);
21475   verifyFormat("operator const FooRight<Object> *&&()", Style);
21476 
21477   Style.PointerAlignment = FormatStyle::PAS_Left;
21478   verifyFormat("Foo::operator*();", Style);
21479   verifyFormat("Foo::operator**();", Style);
21480   verifyFormat("Foo::operator void*();", Style);
21481   verifyFormat("Foo::operator void**();", Style);
21482   verifyFormat("Foo::operator void*&();", Style);
21483   verifyFormat("Foo::operator void*&&();", Style);
21484   verifyFormat("Foo::operator void const*();", Style);
21485   verifyFormat("Foo::operator void const**();", Style);
21486   verifyFormat("Foo::operator void const*&();", Style);
21487   verifyFormat("Foo::operator void const*&&();", Style);
21488   verifyFormat("Foo::operator/*comment*/ void*();", Style);
21489   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
21490   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
21491   verifyFormat("Foo::operator()(void*);", Style);
21492   verifyFormat("Foo::operator*(void*);", Style);
21493   verifyFormat("Foo::operator*();", Style);
21494   verifyFormat("Foo::operator<int>*();", Style);
21495   verifyFormat("Foo::operator<Foo>*();", Style);
21496   verifyFormat("Foo::operator<int>**();", Style);
21497   verifyFormat("Foo::operator<Foo>**();", Style);
21498   verifyFormat("Foo::operator<Foo>*&();", Style);
21499   verifyFormat("Foo::operator<int>&();", Style);
21500   verifyFormat("Foo::operator<Foo>&();", Style);
21501   verifyFormat("Foo::operator<int>&&();", Style);
21502   verifyFormat("Foo::operator<Foo>&&();", Style);
21503   verifyFormat("Foo::operator<int>*&();", Style);
21504   verifyFormat("Foo::operator<Foo>*&();", Style);
21505   verifyFormat("operator*(int (*)(), class Foo);", Style);
21506 
21507   verifyFormat("Foo::operator&();", Style);
21508   verifyFormat("Foo::operator void&();", Style);
21509   verifyFormat("Foo::operator void const&();", Style);
21510   verifyFormat("Foo::operator/*comment*/ void&();", Style);
21511   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
21512   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
21513   verifyFormat("Foo::operator()(void&);", Style);
21514   verifyFormat("Foo::operator&(void&);", Style);
21515   verifyFormat("Foo::operator&();", Style);
21516   verifyFormat("operator&(int (&)(), class Foo);", Style);
21517 
21518   verifyFormat("Foo::operator&&();", Style);
21519   verifyFormat("Foo::operator void&&();", Style);
21520   verifyFormat("Foo::operator void const&&();", Style);
21521   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
21522   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
21523   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
21524   verifyFormat("Foo::operator()(void&&);", Style);
21525   verifyFormat("Foo::operator&&(void&&);", Style);
21526   verifyFormat("Foo::operator&&();", Style);
21527   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21528   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
21529   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
21530                Style);
21531   verifyFormat("operator void**()", Style);
21532   verifyFormat("operator const FooLeft<Object>&()", Style);
21533   verifyFormat("operator const FooLeft<Object>*()", Style);
21534   verifyFormat("operator const FooLeft<Object>**()", Style);
21535   verifyFormat("operator const FooLeft<Object>*&()", Style);
21536   verifyFormat("operator const FooLeft<Object>*&&()", Style);
21537 
21538   // PR45107
21539   verifyFormat("operator Vector<String>&();", Style);
21540   verifyFormat("operator const Vector<String>&();", Style);
21541   verifyFormat("operator foo::Bar*();", Style);
21542   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
21543   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
21544                Style);
21545 
21546   Style.PointerAlignment = FormatStyle::PAS_Middle;
21547   verifyFormat("Foo::operator*();", Style);
21548   verifyFormat("Foo::operator void *();", Style);
21549   verifyFormat("Foo::operator()(void *);", Style);
21550   verifyFormat("Foo::operator*(void *);", Style);
21551   verifyFormat("Foo::operator*();", Style);
21552   verifyFormat("operator*(int (*)(), class Foo);", Style);
21553 
21554   verifyFormat("Foo::operator&();", Style);
21555   verifyFormat("Foo::operator void &();", Style);
21556   verifyFormat("Foo::operator void const &();", Style);
21557   verifyFormat("Foo::operator()(void &);", Style);
21558   verifyFormat("Foo::operator&(void &);", Style);
21559   verifyFormat("Foo::operator&();", Style);
21560   verifyFormat("operator&(int (&)(), class Foo);", Style);
21561 
21562   verifyFormat("Foo::operator&&();", Style);
21563   verifyFormat("Foo::operator void &&();", Style);
21564   verifyFormat("Foo::operator void const &&();", Style);
21565   verifyFormat("Foo::operator()(void &&);", Style);
21566   verifyFormat("Foo::operator&&(void &&);", Style);
21567   verifyFormat("Foo::operator&&();", Style);
21568   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21569 }
21570 
21571 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
21572   FormatStyle Style = getLLVMStyle();
21573   // PR46157
21574   verifyFormat("foo(operator+, -42);", Style);
21575   verifyFormat("foo(operator++, -42);", Style);
21576   verifyFormat("foo(operator--, -42);", Style);
21577   verifyFormat("foo(-42, operator--);", Style);
21578   verifyFormat("foo(-42, operator, );", Style);
21579   verifyFormat("foo(operator, , -42);", Style);
21580 }
21581 
21582 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
21583   FormatStyle Style = getLLVMStyle();
21584   Style.WhitespaceSensitiveMacros.push_back("FOO");
21585 
21586   // Don't use the helpers here, since 'mess up' will change the whitespace
21587   // and these are all whitespace sensitive by definition
21588   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
21589             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
21590   EXPECT_EQ(
21591       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
21592       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
21593   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
21594             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
21595   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
21596             "       Still=Intentional);",
21597             format("FOO(String-ized&Messy+But,: :\n"
21598                    "       Still=Intentional);",
21599                    Style));
21600   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
21601   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
21602             "       Still=Intentional);",
21603             format("FOO(String-ized=&Messy+But,: :\n"
21604                    "       Still=Intentional);",
21605                    Style));
21606 
21607   Style.ColumnLimit = 21;
21608   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
21609             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
21610 }
21611 
21612 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
21613   // These tests are not in NamespaceFixer because that doesn't
21614   // test its interaction with line wrapping
21615   FormatStyle Style = getLLVMStyle();
21616   Style.ColumnLimit = 80;
21617   verifyFormat("namespace {\n"
21618                "int i;\n"
21619                "int j;\n"
21620                "} // namespace",
21621                Style);
21622 
21623   verifyFormat("namespace AAA {\n"
21624                "int i;\n"
21625                "int j;\n"
21626                "} // namespace AAA",
21627                Style);
21628 
21629   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
21630             "int i;\n"
21631             "int j;\n"
21632             "} // namespace Averyveryveryverylongnamespace",
21633             format("namespace Averyveryveryverylongnamespace {\n"
21634                    "int i;\n"
21635                    "int j;\n"
21636                    "}",
21637                    Style));
21638 
21639   EXPECT_EQ(
21640       "namespace "
21641       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
21642       "    went::mad::now {\n"
21643       "int i;\n"
21644       "int j;\n"
21645       "} // namespace\n"
21646       "  // "
21647       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
21648       "went::mad::now",
21649       format("namespace "
21650              "would::it::save::you::a::lot::of::time::if_::i::"
21651              "just::gave::up::and_::went::mad::now {\n"
21652              "int i;\n"
21653              "int j;\n"
21654              "}",
21655              Style));
21656 
21657   // This used to duplicate the comment again and again on subsequent runs
21658   EXPECT_EQ(
21659       "namespace "
21660       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
21661       "    went::mad::now {\n"
21662       "int i;\n"
21663       "int j;\n"
21664       "} // namespace\n"
21665       "  // "
21666       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
21667       "went::mad::now",
21668       format("namespace "
21669              "would::it::save::you::a::lot::of::time::if_::i::"
21670              "just::gave::up::and_::went::mad::now {\n"
21671              "int i;\n"
21672              "int j;\n"
21673              "} // namespace\n"
21674              "  // "
21675              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
21676              "and_::went::mad::now",
21677              Style));
21678 }
21679 
21680 TEST_F(FormatTest, LikelyUnlikely) {
21681   FormatStyle Style = getLLVMStyle();
21682 
21683   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21684                "  return 29;\n"
21685                "}",
21686                Style);
21687 
21688   verifyFormat("if (argc > 5) [[likely]] {\n"
21689                "  return 29;\n"
21690                "}",
21691                Style);
21692 
21693   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21694                "  return 29;\n"
21695                "} else [[likely]] {\n"
21696                "  return 42;\n"
21697                "}\n",
21698                Style);
21699 
21700   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21701                "  return 29;\n"
21702                "} else if (argc > 10) [[likely]] {\n"
21703                "  return 99;\n"
21704                "} else {\n"
21705                "  return 42;\n"
21706                "}\n",
21707                Style);
21708 
21709   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
21710                "  return 29;\n"
21711                "}",
21712                Style);
21713 }
21714 
21715 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
21716   verifyFormat("Constructor()\n"
21717                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21718                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
21719                "aaaaaaaaaaaaaaaaaat))");
21720   verifyFormat("Constructor()\n"
21721                "    : aaaaaaaaaaaaa(aaaaaa), "
21722                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
21723 
21724   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
21725   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
21726   verifyFormat("Constructor()\n"
21727                "    : aaaaaa(aaaaaa),\n"
21728                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21729                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
21730                StyleWithWhitespacePenalty);
21731   verifyFormat("Constructor()\n"
21732                "    : aaaaaaaaaaaaa(aaaaaa), "
21733                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
21734                StyleWithWhitespacePenalty);
21735 }
21736 
21737 TEST_F(FormatTest, LLVMDefaultStyle) {
21738   FormatStyle Style = getLLVMStyle();
21739   verifyFormat("extern \"C\" {\n"
21740                "int foo();\n"
21741                "}",
21742                Style);
21743 }
21744 TEST_F(FormatTest, GNUDefaultStyle) {
21745   FormatStyle Style = getGNUStyle();
21746   verifyFormat("extern \"C\"\n"
21747                "{\n"
21748                "  int foo ();\n"
21749                "}",
21750                Style);
21751 }
21752 TEST_F(FormatTest, MozillaDefaultStyle) {
21753   FormatStyle Style = getMozillaStyle();
21754   verifyFormat("extern \"C\"\n"
21755                "{\n"
21756                "  int foo();\n"
21757                "}",
21758                Style);
21759 }
21760 TEST_F(FormatTest, GoogleDefaultStyle) {
21761   FormatStyle Style = getGoogleStyle();
21762   verifyFormat("extern \"C\" {\n"
21763                "int foo();\n"
21764                "}",
21765                Style);
21766 }
21767 TEST_F(FormatTest, ChromiumDefaultStyle) {
21768   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
21769   verifyFormat("extern \"C\" {\n"
21770                "int foo();\n"
21771                "}",
21772                Style);
21773 }
21774 TEST_F(FormatTest, MicrosoftDefaultStyle) {
21775   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
21776   verifyFormat("extern \"C\"\n"
21777                "{\n"
21778                "    int foo();\n"
21779                "}",
21780                Style);
21781 }
21782 TEST_F(FormatTest, WebKitDefaultStyle) {
21783   FormatStyle Style = getWebKitStyle();
21784   verifyFormat("extern \"C\" {\n"
21785                "int foo();\n"
21786                "}",
21787                Style);
21788 }
21789 
21790 TEST_F(FormatTest, ConceptsAndRequires) {
21791   FormatStyle Style = getLLVMStyle();
21792   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
21793 
21794   verifyFormat("template <typename T>\n"
21795                "concept Hashable = requires(T a) {\n"
21796                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
21797                "};",
21798                Style);
21799   verifyFormat("template <typename T>\n"
21800                "concept EqualityComparable = requires(T a, T b) {\n"
21801                "  { a == b } -> bool;\n"
21802                "};",
21803                Style);
21804   verifyFormat("template <typename T>\n"
21805                "concept EqualityComparable = requires(T a, T b) {\n"
21806                "  { a == b } -> bool;\n"
21807                "  { a != b } -> bool;\n"
21808                "};",
21809                Style);
21810   verifyFormat("template <typename T>\n"
21811                "concept EqualityComparable = requires(T a, T b) {\n"
21812                "  { a == b } -> bool;\n"
21813                "  { a != b } -> bool;\n"
21814                "};",
21815                Style);
21816 
21817   verifyFormat("template <typename It>\n"
21818                "requires Iterator<It>\n"
21819                "void sort(It begin, It end) {\n"
21820                "  //....\n"
21821                "}",
21822                Style);
21823 
21824   verifyFormat("template <typename T>\n"
21825                "concept Large = sizeof(T) > 10;",
21826                Style);
21827 
21828   verifyFormat("template <typename T, typename U>\n"
21829                "concept FooableWith = requires(T t, U u) {\n"
21830                "  typename T::foo_type;\n"
21831                "  { t.foo(u) } -> typename T::foo_type;\n"
21832                "  t++;\n"
21833                "};\n"
21834                "void doFoo(FooableWith<int> auto t) {\n"
21835                "  t.foo(3);\n"
21836                "}",
21837                Style);
21838   verifyFormat("template <typename T>\n"
21839                "concept Context = sizeof(T) == 1;",
21840                Style);
21841   verifyFormat("template <typename T>\n"
21842                "concept Context = is_specialization_of_v<context, T>;",
21843                Style);
21844   verifyFormat("template <typename T>\n"
21845                "concept Node = std::is_object_v<T>;",
21846                Style);
21847   verifyFormat("template <typename T>\n"
21848                "concept Tree = true;",
21849                Style);
21850 
21851   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
21852                "  //...\n"
21853                "}",
21854                Style);
21855 
21856   verifyFormat(
21857       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
21858       "  //...\n"
21859       "}",
21860       Style);
21861 
21862   verifyFormat(
21863       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
21864       "  //...\n"
21865       "}",
21866       Style);
21867 
21868   verifyFormat("template <typename T>\n"
21869                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
21870                "Concept2<I> {\n"
21871                "  //...\n"
21872                "}",
21873                Style);
21874 
21875   verifyFormat("template <typename T>\n"
21876                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
21877                "Concept2<I> {\n"
21878                "  //...\n"
21879                "}",
21880                Style);
21881 
21882   verifyFormat(
21883       "template <typename T>\n"
21884       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
21885       "  //...\n"
21886       "}",
21887       Style);
21888 
21889   verifyFormat(
21890       "template <typename T>\n"
21891       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
21892       "  //...\n"
21893       "}",
21894       Style);
21895 
21896   verifyFormat("template <typename It>\n"
21897                "requires Foo<It>() && Bar<It> {\n"
21898                "  //....\n"
21899                "}",
21900                Style);
21901 
21902   verifyFormat("template <typename It>\n"
21903                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
21904                "  //....\n"
21905                "}",
21906                Style);
21907 
21908   verifyFormat("template <typename It>\n"
21909                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
21910                "  //....\n"
21911                "}",
21912                Style);
21913 
21914   verifyFormat(
21915       "template <typename It>\n"
21916       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
21917       "  //....\n"
21918       "}",
21919       Style);
21920 
21921   Style.IndentRequires = true;
21922   verifyFormat("template <typename It>\n"
21923                "  requires Iterator<It>\n"
21924                "void sort(It begin, It end) {\n"
21925                "  //....\n"
21926                "}",
21927                Style);
21928   verifyFormat("template <std::size index_>\n"
21929                "  requires(index_ < sizeof...(Children_))\n"
21930                "Tree auto &child() {\n"
21931                "  // ...\n"
21932                "}",
21933                Style);
21934 
21935   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
21936   verifyFormat("template <typename T>\n"
21937                "concept Hashable = requires (T a) {\n"
21938                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
21939                "};",
21940                Style);
21941 
21942   verifyFormat("template <class T = void>\n"
21943                "  requires EqualityComparable<T> || Same<T, void>\n"
21944                "struct equal_to;",
21945                Style);
21946 
21947   verifyFormat("template <class T>\n"
21948                "  requires requires {\n"
21949                "    T{};\n"
21950                "    T (int);\n"
21951                "  }\n",
21952                Style);
21953 
21954   Style.ColumnLimit = 78;
21955   verifyFormat("template <typename T>\n"
21956                "concept Context = Traits<typename T::traits_type> and\n"
21957                "    Interface<typename T::interface_type> and\n"
21958                "    Request<typename T::request_type> and\n"
21959                "    Response<typename T::response_type> and\n"
21960                "    ContextExtension<typename T::extension_type> and\n"
21961                "    ::std::is_copy_constructable<T> and "
21962                "::std::is_move_constructable<T> and\n"
21963                "    requires (T c) {\n"
21964                "  { c.response; } -> Response;\n"
21965                "} and requires (T c) {\n"
21966                "  { c.request; } -> Request;\n"
21967                "}\n",
21968                Style);
21969 
21970   verifyFormat("template <typename T>\n"
21971                "concept Context = Traits<typename T::traits_type> or\n"
21972                "    Interface<typename T::interface_type> or\n"
21973                "    Request<typename T::request_type> or\n"
21974                "    Response<typename T::response_type> or\n"
21975                "    ContextExtension<typename T::extension_type> or\n"
21976                "    ::std::is_copy_constructable<T> or "
21977                "::std::is_move_constructable<T> or\n"
21978                "    requires (T c) {\n"
21979                "  { c.response; } -> Response;\n"
21980                "} or requires (T c) {\n"
21981                "  { c.request; } -> Request;\n"
21982                "}\n",
21983                Style);
21984 
21985   verifyFormat("template <typename T>\n"
21986                "concept Context = Traits<typename T::traits_type> &&\n"
21987                "    Interface<typename T::interface_type> &&\n"
21988                "    Request<typename T::request_type> &&\n"
21989                "    Response<typename T::response_type> &&\n"
21990                "    ContextExtension<typename T::extension_type> &&\n"
21991                "    ::std::is_copy_constructable<T> && "
21992                "::std::is_move_constructable<T> &&\n"
21993                "    requires (T c) {\n"
21994                "  { c.response; } -> Response;\n"
21995                "} && requires (T c) {\n"
21996                "  { c.request; } -> Request;\n"
21997                "}\n",
21998                Style);
21999 
22000   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
22001                "Constraint2<T>;");
22002 
22003   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
22004   Style.BraceWrapping.AfterFunction = true;
22005   Style.BraceWrapping.AfterClass = true;
22006   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
22007   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
22008   verifyFormat("void Foo () requires (std::copyable<T>)\n"
22009                "{\n"
22010                "  return\n"
22011                "}\n",
22012                Style);
22013 
22014   verifyFormat("void Foo () requires std::copyable<T>\n"
22015                "{\n"
22016                "  return\n"
22017                "}\n",
22018                Style);
22019 
22020   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22021                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
22022                "struct constant;",
22023                Style);
22024 
22025   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22026                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
22027                "struct constant;",
22028                Style);
22029 
22030   verifyFormat("template <class T>\n"
22031                "class plane_with_very_very_very_long_name\n"
22032                "{\n"
22033                "  constexpr plane_with_very_very_very_long_name () requires "
22034                "std::copyable<T>\n"
22035                "      : plane_with_very_very_very_long_name (1)\n"
22036                "  {\n"
22037                "  }\n"
22038                "}\n",
22039                Style);
22040 
22041   verifyFormat("template <class T>\n"
22042                "class plane_with_long_name\n"
22043                "{\n"
22044                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
22045                "      : plane_with_long_name (1)\n"
22046                "  {\n"
22047                "  }\n"
22048                "}\n",
22049                Style);
22050 
22051   Style.BreakBeforeConceptDeclarations = false;
22052   verifyFormat("template <typename T> concept Tree = true;", Style);
22053 
22054   Style.IndentRequires = false;
22055   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22056                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
22057                "struct constant;",
22058                Style);
22059 }
22060 
22061 TEST_F(FormatTest, StatementAttributeLikeMacros) {
22062   FormatStyle Style = getLLVMStyle();
22063   StringRef Source = "void Foo::slot() {\n"
22064                      "  unsigned char MyChar = 'x';\n"
22065                      "  emit signal(MyChar);\n"
22066                      "  Q_EMIT signal(MyChar);\n"
22067                      "}";
22068 
22069   EXPECT_EQ(Source, format(Source, Style));
22070 
22071   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
22072   EXPECT_EQ("void Foo::slot() {\n"
22073             "  unsigned char MyChar = 'x';\n"
22074             "  emit          signal(MyChar);\n"
22075             "  Q_EMIT signal(MyChar);\n"
22076             "}",
22077             format(Source, Style));
22078 
22079   Style.StatementAttributeLikeMacros.push_back("emit");
22080   EXPECT_EQ(Source, format(Source, Style));
22081 
22082   Style.StatementAttributeLikeMacros = {};
22083   EXPECT_EQ("void Foo::slot() {\n"
22084             "  unsigned char MyChar = 'x';\n"
22085             "  emit          signal(MyChar);\n"
22086             "  Q_EMIT        signal(MyChar);\n"
22087             "}",
22088             format(Source, Style));
22089 }
22090 
22091 TEST_F(FormatTest, IndentAccessModifiers) {
22092   FormatStyle Style = getLLVMStyle();
22093   Style.IndentAccessModifiers = true;
22094   // Members are *two* levels below the record;
22095   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
22096   verifyFormat("class C {\n"
22097                "    int i;\n"
22098                "};\n",
22099                Style);
22100   verifyFormat("union C {\n"
22101                "    int i;\n"
22102                "    unsigned u;\n"
22103                "};\n",
22104                Style);
22105   // Access modifiers should be indented one level below the record.
22106   verifyFormat("class C {\n"
22107                "  public:\n"
22108                "    int i;\n"
22109                "};\n",
22110                Style);
22111   verifyFormat("struct S {\n"
22112                "  private:\n"
22113                "    class C {\n"
22114                "        int j;\n"
22115                "\n"
22116                "      public:\n"
22117                "        C();\n"
22118                "    };\n"
22119                "\n"
22120                "  public:\n"
22121                "    int i;\n"
22122                "};\n",
22123                Style);
22124   // Enumerations are not records and should be unaffected.
22125   Style.AllowShortEnumsOnASingleLine = false;
22126   verifyFormat("enum class E\n"
22127                "{\n"
22128                "  A,\n"
22129                "  B\n"
22130                "};\n",
22131                Style);
22132   // Test with a different indentation width;
22133   // also proves that the result is Style.AccessModifierOffset agnostic.
22134   Style.IndentWidth = 3;
22135   verifyFormat("class C {\n"
22136                "   public:\n"
22137                "      int i;\n"
22138                "};\n",
22139                Style);
22140 }
22141 
22142 TEST_F(FormatTest, LimitlessStringsAndComments) {
22143   auto Style = getLLVMStyleWithColumns(0);
22144   constexpr StringRef Code =
22145       "/**\n"
22146       " * This is a multiline comment with quite some long lines, at least for "
22147       "the LLVM Style.\n"
22148       " * We will redo this with strings and line comments. Just to  check if "
22149       "everything is working.\n"
22150       " */\n"
22151       "bool foo() {\n"
22152       "  /* Single line multi line comment. */\n"
22153       "  const std::string String = \"This is a multiline string with quite "
22154       "some long lines, at least for the LLVM Style.\"\n"
22155       "                             \"We already did it with multi line "
22156       "comments, and we will do it with line comments. Just to check if "
22157       "everything is working.\";\n"
22158       "  // This is a line comment (block) with quite some long lines, at "
22159       "least for the LLVM Style.\n"
22160       "  // We already did this with multi line comments and strings. Just to "
22161       "check if everything is working.\n"
22162       "  const std::string SmallString = \"Hello World\";\n"
22163       "  // Small line comment\n"
22164       "  return String.size() > SmallString.size();\n"
22165       "}";
22166   EXPECT_EQ(Code, format(Code, Style));
22167 }
22168 } // namespace
22169 } // namespace format
22170 } // namespace clang
22171