xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision ee3b2c47ce41aeabede85d96e43bee33be73aa2f)
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   // ...but do keep inlining and removing empty lines for non-block extern "C"
266   // functions.
267   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
268   EXPECT_EQ("extern \"C\" int f() {\n"
269             "  int i = 42;\n"
270             "  return i;\n"
271             "}",
272             format("extern \"C\" int f() {\n"
273                    "\n"
274                    "  int i = 42;\n"
275                    "  return i;\n"
276                    "}",
277                    getGoogleStyle()));
278 
279   // Remove empty lines at the beginning and end of blocks.
280   EXPECT_EQ("void f() {\n"
281             "\n"
282             "  if (a) {\n"
283             "\n"
284             "    f();\n"
285             "  }\n"
286             "}",
287             format("void f() {\n"
288                    "\n"
289                    "  if (a) {\n"
290                    "\n"
291                    "    f();\n"
292                    "\n"
293                    "  }\n"
294                    "\n"
295                    "}",
296                    getLLVMStyle()));
297   EXPECT_EQ("void f() {\n"
298             "  if (a) {\n"
299             "    f();\n"
300             "  }\n"
301             "}",
302             format("void f() {\n"
303                    "\n"
304                    "  if (a) {\n"
305                    "\n"
306                    "    f();\n"
307                    "\n"
308                    "  }\n"
309                    "\n"
310                    "}",
311                    getGoogleStyle()));
312 
313   // Don't remove empty lines in more complex control statements.
314   EXPECT_EQ("void f() {\n"
315             "  if (a) {\n"
316             "    f();\n"
317             "\n"
318             "  } else if (b) {\n"
319             "    f();\n"
320             "  }\n"
321             "}",
322             format("void f() {\n"
323                    "  if (a) {\n"
324                    "    f();\n"
325                    "\n"
326                    "  } else if (b) {\n"
327                    "    f();\n"
328                    "\n"
329                    "  }\n"
330                    "\n"
331                    "}"));
332 
333   // Don't remove empty lines before namespace endings.
334   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
335   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
336   EXPECT_EQ("namespace {\n"
337             "int i;\n"
338             "\n"
339             "}",
340             format("namespace {\n"
341                    "int i;\n"
342                    "\n"
343                    "}",
344                    LLVMWithNoNamespaceFix));
345   EXPECT_EQ("namespace {\n"
346             "int i;\n"
347             "}",
348             format("namespace {\n"
349                    "int i;\n"
350                    "}",
351                    LLVMWithNoNamespaceFix));
352   EXPECT_EQ("namespace {\n"
353             "int i;\n"
354             "\n"
355             "};",
356             format("namespace {\n"
357                    "int i;\n"
358                    "\n"
359                    "};",
360                    LLVMWithNoNamespaceFix));
361   EXPECT_EQ("namespace {\n"
362             "int i;\n"
363             "};",
364             format("namespace {\n"
365                    "int i;\n"
366                    "};",
367                    LLVMWithNoNamespaceFix));
368   EXPECT_EQ("namespace {\n"
369             "int i;\n"
370             "\n"
371             "}",
372             format("namespace {\n"
373                    "int i;\n"
374                    "\n"
375                    "}"));
376   EXPECT_EQ("namespace {\n"
377             "int i;\n"
378             "\n"
379             "} // namespace",
380             format("namespace {\n"
381                    "int i;\n"
382                    "\n"
383                    "}  // namespace"));
384 
385   FormatStyle Style = getLLVMStyle();
386   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
387   Style.MaxEmptyLinesToKeep = 2;
388   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
389   Style.BraceWrapping.AfterClass = true;
390   Style.BraceWrapping.AfterFunction = true;
391   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
392 
393   EXPECT_EQ("class Foo\n"
394             "{\n"
395             "  Foo() {}\n"
396             "\n"
397             "  void funk() {}\n"
398             "};",
399             format("class Foo\n"
400                    "{\n"
401                    "  Foo()\n"
402                    "  {\n"
403                    "  }\n"
404                    "\n"
405                    "  void funk() {}\n"
406                    "};",
407                    Style));
408 }
409 
410 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
411   verifyFormat("x = (a) and (b);");
412   verifyFormat("x = (a) or (b);");
413   verifyFormat("x = (a) bitand (b);");
414   verifyFormat("x = (a) bitor (b);");
415   verifyFormat("x = (a) not_eq (b);");
416   verifyFormat("x = (a) and_eq (b);");
417   verifyFormat("x = (a) or_eq (b);");
418   verifyFormat("x = (a) xor (b);");
419 }
420 
421 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
422   verifyFormat("x = compl(a);");
423   verifyFormat("x = not(a);");
424   verifyFormat("x = bitand(a);");
425   // Unary operator must not be merged with the next identifier
426   verifyFormat("x = compl a;");
427   verifyFormat("x = not a;");
428   verifyFormat("x = bitand a;");
429 }
430 
431 //===----------------------------------------------------------------------===//
432 // Tests for control statements.
433 //===----------------------------------------------------------------------===//
434 
435 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
436   verifyFormat("if (true)\n  f();\ng();");
437   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
438   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
439   verifyFormat("if constexpr (true)\n"
440                "  f();\ng();");
441   verifyFormat("if CONSTEXPR (true)\n"
442                "  f();\ng();");
443   verifyFormat("if constexpr (a)\n"
444                "  if constexpr (b)\n"
445                "    if constexpr (c)\n"
446                "      g();\n"
447                "h();");
448   verifyFormat("if CONSTEXPR (a)\n"
449                "  if CONSTEXPR (b)\n"
450                "    if CONSTEXPR (c)\n"
451                "      g();\n"
452                "h();");
453   verifyFormat("if constexpr (a)\n"
454                "  if constexpr (b) {\n"
455                "    f();\n"
456                "  }\n"
457                "g();");
458   verifyFormat("if CONSTEXPR (a)\n"
459                "  if CONSTEXPR (b) {\n"
460                "    f();\n"
461                "  }\n"
462                "g();");
463 
464   verifyFormat("if (a)\n"
465                "  g();");
466   verifyFormat("if (a) {\n"
467                "  g()\n"
468                "};");
469   verifyFormat("if (a)\n"
470                "  g();\n"
471                "else\n"
472                "  g();");
473   verifyFormat("if (a) {\n"
474                "  g();\n"
475                "} else\n"
476                "  g();");
477   verifyFormat("if (a)\n"
478                "  g();\n"
479                "else {\n"
480                "  g();\n"
481                "}");
482   verifyFormat("if (a) {\n"
483                "  g();\n"
484                "} else {\n"
485                "  g();\n"
486                "}");
487   verifyFormat("if (a)\n"
488                "  g();\n"
489                "else if (b)\n"
490                "  g();\n"
491                "else\n"
492                "  g();");
493   verifyFormat("if (a) {\n"
494                "  g();\n"
495                "} else if (b)\n"
496                "  g();\n"
497                "else\n"
498                "  g();");
499   verifyFormat("if (a)\n"
500                "  g();\n"
501                "else if (b) {\n"
502                "  g();\n"
503                "} else\n"
504                "  g();");
505   verifyFormat("if (a)\n"
506                "  g();\n"
507                "else if (b)\n"
508                "  g();\n"
509                "else {\n"
510                "  g();\n"
511                "}");
512   verifyFormat("if (a)\n"
513                "  g();\n"
514                "else if (b) {\n"
515                "  g();\n"
516                "} else {\n"
517                "  g();\n"
518                "}");
519   verifyFormat("if (a) {\n"
520                "  g();\n"
521                "} else if (b) {\n"
522                "  g();\n"
523                "} else {\n"
524                "  g();\n"
525                "}");
526 
527   FormatStyle AllowsMergedIf = getLLVMStyle();
528   AllowsMergedIf.IfMacros.push_back("MYIF");
529   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
530   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
531       FormatStyle::SIS_WithoutElse;
532   verifyFormat("if (a)\n"
533                "  // comment\n"
534                "  f();",
535                AllowsMergedIf);
536   verifyFormat("{\n"
537                "  if (a)\n"
538                "  label:\n"
539                "    f();\n"
540                "}",
541                AllowsMergedIf);
542   verifyFormat("#define A \\\n"
543                "  if (a)  \\\n"
544                "  label:  \\\n"
545                "    f()",
546                AllowsMergedIf);
547   verifyFormat("if (a)\n"
548                "  ;",
549                AllowsMergedIf);
550   verifyFormat("if (a)\n"
551                "  if (b) return;",
552                AllowsMergedIf);
553 
554   verifyFormat("if (a) // Can't merge this\n"
555                "  f();\n",
556                AllowsMergedIf);
557   verifyFormat("if (a) /* still don't merge */\n"
558                "  f();",
559                AllowsMergedIf);
560   verifyFormat("if (a) { // Never merge this\n"
561                "  f();\n"
562                "}",
563                AllowsMergedIf);
564   verifyFormat("if (a) { /* Never merge this */\n"
565                "  f();\n"
566                "}",
567                AllowsMergedIf);
568   verifyFormat("MYIF (a)\n"
569                "  // comment\n"
570                "  f();",
571                AllowsMergedIf);
572   verifyFormat("{\n"
573                "  MYIF (a)\n"
574                "  label:\n"
575                "    f();\n"
576                "}",
577                AllowsMergedIf);
578   verifyFormat("#define A  \\\n"
579                "  MYIF (a) \\\n"
580                "  label:   \\\n"
581                "    f()",
582                AllowsMergedIf);
583   verifyFormat("MYIF (a)\n"
584                "  ;",
585                AllowsMergedIf);
586   verifyFormat("MYIF (a)\n"
587                "  MYIF (b) return;",
588                AllowsMergedIf);
589 
590   verifyFormat("MYIF (a) // Can't merge this\n"
591                "  f();\n",
592                AllowsMergedIf);
593   verifyFormat("MYIF (a) /* still don't merge */\n"
594                "  f();",
595                AllowsMergedIf);
596   verifyFormat("MYIF (a) { // Never merge this\n"
597                "  f();\n"
598                "}",
599                AllowsMergedIf);
600   verifyFormat("MYIF (a) { /* Never merge this */\n"
601                "  f();\n"
602                "}",
603                AllowsMergedIf);
604 
605   AllowsMergedIf.ColumnLimit = 14;
606   // Where line-lengths matter, a 2-letter synonym that maintains line length.
607   // Not IF to avoid any confusion that IF is somehow special.
608   AllowsMergedIf.IfMacros.push_back("FI");
609   verifyFormat("if (a) return;", AllowsMergedIf);
610   verifyFormat("if (aaaaaaaaa)\n"
611                "  return;",
612                AllowsMergedIf);
613   verifyFormat("FI (a) return;", AllowsMergedIf);
614   verifyFormat("FI (aaaaaaaaa)\n"
615                "  return;",
616                AllowsMergedIf);
617 
618   AllowsMergedIf.ColumnLimit = 13;
619   verifyFormat("if (a)\n  return;", AllowsMergedIf);
620   verifyFormat("FI (a)\n  return;", AllowsMergedIf);
621 
622   FormatStyle AllowsMergedIfElse = getLLVMStyle();
623   AllowsMergedIfElse.IfMacros.push_back("MYIF");
624   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
625       FormatStyle::SIS_AllIfsAndElse;
626   verifyFormat("if (a)\n"
627                "  // comment\n"
628                "  f();\n"
629                "else\n"
630                "  // comment\n"
631                "  f();",
632                AllowsMergedIfElse);
633   verifyFormat("{\n"
634                "  if (a)\n"
635                "  label:\n"
636                "    f();\n"
637                "  else\n"
638                "  label:\n"
639                "    f();\n"
640                "}",
641                AllowsMergedIfElse);
642   verifyFormat("if (a)\n"
643                "  ;\n"
644                "else\n"
645                "  ;",
646                AllowsMergedIfElse);
647   verifyFormat("if (a) {\n"
648                "} else {\n"
649                "}",
650                AllowsMergedIfElse);
651   verifyFormat("if (a) return;\n"
652                "else if (b) return;\n"
653                "else return;",
654                AllowsMergedIfElse);
655   verifyFormat("if (a) {\n"
656                "} else return;",
657                AllowsMergedIfElse);
658   verifyFormat("if (a) {\n"
659                "} else if (b) return;\n"
660                "else return;",
661                AllowsMergedIfElse);
662   verifyFormat("if (a) return;\n"
663                "else if (b) {\n"
664                "} else return;",
665                AllowsMergedIfElse);
666   verifyFormat("if (a)\n"
667                "  if (b) return;\n"
668                "  else return;",
669                AllowsMergedIfElse);
670   verifyFormat("if constexpr (a)\n"
671                "  if constexpr (b) return;\n"
672                "  else if constexpr (c) return;\n"
673                "  else return;",
674                AllowsMergedIfElse);
675   verifyFormat("MYIF (a)\n"
676                "  // comment\n"
677                "  f();\n"
678                "else\n"
679                "  // comment\n"
680                "  f();",
681                AllowsMergedIfElse);
682   verifyFormat("{\n"
683                "  MYIF (a)\n"
684                "  label:\n"
685                "    f();\n"
686                "  else\n"
687                "  label:\n"
688                "    f();\n"
689                "}",
690                AllowsMergedIfElse);
691   verifyFormat("MYIF (a)\n"
692                "  ;\n"
693                "else\n"
694                "  ;",
695                AllowsMergedIfElse);
696   verifyFormat("MYIF (a) {\n"
697                "} else {\n"
698                "}",
699                AllowsMergedIfElse);
700   verifyFormat("MYIF (a) return;\n"
701                "else MYIF (b) return;\n"
702                "else return;",
703                AllowsMergedIfElse);
704   verifyFormat("MYIF (a) {\n"
705                "} else return;",
706                AllowsMergedIfElse);
707   verifyFormat("MYIF (a) {\n"
708                "} else MYIF (b) return;\n"
709                "else return;",
710                AllowsMergedIfElse);
711   verifyFormat("MYIF (a) return;\n"
712                "else MYIF (b) {\n"
713                "} else return;",
714                AllowsMergedIfElse);
715   verifyFormat("MYIF (a)\n"
716                "  MYIF (b) return;\n"
717                "  else return;",
718                AllowsMergedIfElse);
719   verifyFormat("MYIF constexpr (a)\n"
720                "  MYIF constexpr (b) return;\n"
721                "  else MYIF constexpr (c) return;\n"
722                "  else return;",
723                AllowsMergedIfElse);
724 }
725 
726 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
727   FormatStyle AllowsMergedIf = getLLVMStyle();
728   AllowsMergedIf.IfMacros.push_back("MYIF");
729   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
730   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
731       FormatStyle::SIS_WithoutElse;
732   verifyFormat("if (a)\n"
733                "  f();\n"
734                "else {\n"
735                "  g();\n"
736                "}",
737                AllowsMergedIf);
738   verifyFormat("if (a)\n"
739                "  f();\n"
740                "else\n"
741                "  g();\n",
742                AllowsMergedIf);
743 
744   verifyFormat("if (a) g();", AllowsMergedIf);
745   verifyFormat("if (a) {\n"
746                "  g()\n"
747                "};",
748                AllowsMergedIf);
749   verifyFormat("if (a)\n"
750                "  g();\n"
751                "else\n"
752                "  g();",
753                AllowsMergedIf);
754   verifyFormat("if (a) {\n"
755                "  g();\n"
756                "} else\n"
757                "  g();",
758                AllowsMergedIf);
759   verifyFormat("if (a)\n"
760                "  g();\n"
761                "else {\n"
762                "  g();\n"
763                "}",
764                AllowsMergedIf);
765   verifyFormat("if (a) {\n"
766                "  g();\n"
767                "} else {\n"
768                "  g();\n"
769                "}",
770                AllowsMergedIf);
771   verifyFormat("if (a)\n"
772                "  g();\n"
773                "else if (b)\n"
774                "  g();\n"
775                "else\n"
776                "  g();",
777                AllowsMergedIf);
778   verifyFormat("if (a) {\n"
779                "  g();\n"
780                "} else if (b)\n"
781                "  g();\n"
782                "else\n"
783                "  g();",
784                AllowsMergedIf);
785   verifyFormat("if (a)\n"
786                "  g();\n"
787                "else if (b) {\n"
788                "  g();\n"
789                "} else\n"
790                "  g();",
791                AllowsMergedIf);
792   verifyFormat("if (a)\n"
793                "  g();\n"
794                "else if (b)\n"
795                "  g();\n"
796                "else {\n"
797                "  g();\n"
798                "}",
799                AllowsMergedIf);
800   verifyFormat("if (a)\n"
801                "  g();\n"
802                "else if (b) {\n"
803                "  g();\n"
804                "} else {\n"
805                "  g();\n"
806                "}",
807                AllowsMergedIf);
808   verifyFormat("if (a) {\n"
809                "  g();\n"
810                "} else if (b) {\n"
811                "  g();\n"
812                "} else {\n"
813                "  g();\n"
814                "}",
815                AllowsMergedIf);
816   verifyFormat("MYIF (a)\n"
817                "  f();\n"
818                "else {\n"
819                "  g();\n"
820                "}",
821                AllowsMergedIf);
822   verifyFormat("MYIF (a)\n"
823                "  f();\n"
824                "else\n"
825                "  g();\n",
826                AllowsMergedIf);
827 
828   verifyFormat("MYIF (a) g();", AllowsMergedIf);
829   verifyFormat("MYIF (a) {\n"
830                "  g()\n"
831                "};",
832                AllowsMergedIf);
833   verifyFormat("MYIF (a)\n"
834                "  g();\n"
835                "else\n"
836                "  g();",
837                AllowsMergedIf);
838   verifyFormat("MYIF (a) {\n"
839                "  g();\n"
840                "} else\n"
841                "  g();",
842                AllowsMergedIf);
843   verifyFormat("MYIF (a)\n"
844                "  g();\n"
845                "else {\n"
846                "  g();\n"
847                "}",
848                AllowsMergedIf);
849   verifyFormat("MYIF (a) {\n"
850                "  g();\n"
851                "} else {\n"
852                "  g();\n"
853                "}",
854                AllowsMergedIf);
855   verifyFormat("MYIF (a)\n"
856                "  g();\n"
857                "else MYIF (b)\n"
858                "  g();\n"
859                "else\n"
860                "  g();",
861                AllowsMergedIf);
862   verifyFormat("MYIF (a)\n"
863                "  g();\n"
864                "else if (b)\n"
865                "  g();\n"
866                "else\n"
867                "  g();",
868                AllowsMergedIf);
869   verifyFormat("MYIF (a) {\n"
870                "  g();\n"
871                "} else MYIF (b)\n"
872                "  g();\n"
873                "else\n"
874                "  g();",
875                AllowsMergedIf);
876   verifyFormat("MYIF (a) {\n"
877                "  g();\n"
878                "} else if (b)\n"
879                "  g();\n"
880                "else\n"
881                "  g();",
882                AllowsMergedIf);
883   verifyFormat("MYIF (a)\n"
884                "  g();\n"
885                "else MYIF (b) {\n"
886                "  g();\n"
887                "} else\n"
888                "  g();",
889                AllowsMergedIf);
890   verifyFormat("MYIF (a)\n"
891                "  g();\n"
892                "else if (b) {\n"
893                "  g();\n"
894                "} else\n"
895                "  g();",
896                AllowsMergedIf);
897   verifyFormat("MYIF (a)\n"
898                "  g();\n"
899                "else MYIF (b)\n"
900                "  g();\n"
901                "else {\n"
902                "  g();\n"
903                "}",
904                AllowsMergedIf);
905   verifyFormat("MYIF (a)\n"
906                "  g();\n"
907                "else if (b)\n"
908                "  g();\n"
909                "else {\n"
910                "  g();\n"
911                "}",
912                AllowsMergedIf);
913   verifyFormat("MYIF (a)\n"
914                "  g();\n"
915                "else MYIF (b) {\n"
916                "  g();\n"
917                "} else {\n"
918                "  g();\n"
919                "}",
920                AllowsMergedIf);
921   verifyFormat("MYIF (a)\n"
922                "  g();\n"
923                "else if (b) {\n"
924                "  g();\n"
925                "} else {\n"
926                "  g();\n"
927                "}",
928                AllowsMergedIf);
929   verifyFormat("MYIF (a) {\n"
930                "  g();\n"
931                "} else MYIF (b) {\n"
932                "  g();\n"
933                "} else {\n"
934                "  g();\n"
935                "}",
936                AllowsMergedIf);
937   verifyFormat("MYIF (a) {\n"
938                "  g();\n"
939                "} else if (b) {\n"
940                "  g();\n"
941                "} else {\n"
942                "  g();\n"
943                "}",
944                AllowsMergedIf);
945 
946   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
947       FormatStyle::SIS_OnlyFirstIf;
948 
949   verifyFormat("if (a) f();\n"
950                "else {\n"
951                "  g();\n"
952                "}",
953                AllowsMergedIf);
954   verifyFormat("if (a) f();\n"
955                "else {\n"
956                "  if (a) f();\n"
957                "  else {\n"
958                "    g();\n"
959                "  }\n"
960                "  g();\n"
961                "}",
962                AllowsMergedIf);
963 
964   verifyFormat("if (a) g();", AllowsMergedIf);
965   verifyFormat("if (a) {\n"
966                "  g()\n"
967                "};",
968                AllowsMergedIf);
969   verifyFormat("if (a) g();\n"
970                "else\n"
971                "  g();",
972                AllowsMergedIf);
973   verifyFormat("if (a) {\n"
974                "  g();\n"
975                "} else\n"
976                "  g();",
977                AllowsMergedIf);
978   verifyFormat("if (a) g();\n"
979                "else {\n"
980                "  g();\n"
981                "}",
982                AllowsMergedIf);
983   verifyFormat("if (a) {\n"
984                "  g();\n"
985                "} else {\n"
986                "  g();\n"
987                "}",
988                AllowsMergedIf);
989   verifyFormat("if (a) g();\n"
990                "else if (b)\n"
991                "  g();\n"
992                "else\n"
993                "  g();",
994                AllowsMergedIf);
995   verifyFormat("if (a) {\n"
996                "  g();\n"
997                "} else if (b)\n"
998                "  g();\n"
999                "else\n"
1000                "  g();",
1001                AllowsMergedIf);
1002   verifyFormat("if (a) g();\n"
1003                "else if (b) {\n"
1004                "  g();\n"
1005                "} else\n"
1006                "  g();",
1007                AllowsMergedIf);
1008   verifyFormat("if (a) g();\n"
1009                "else if (b)\n"
1010                "  g();\n"
1011                "else {\n"
1012                "  g();\n"
1013                "}",
1014                AllowsMergedIf);
1015   verifyFormat("if (a) g();\n"
1016                "else if (b) {\n"
1017                "  g();\n"
1018                "} else {\n"
1019                "  g();\n"
1020                "}",
1021                AllowsMergedIf);
1022   verifyFormat("if (a) {\n"
1023                "  g();\n"
1024                "} else if (b) {\n"
1025                "  g();\n"
1026                "} else {\n"
1027                "  g();\n"
1028                "}",
1029                AllowsMergedIf);
1030   verifyFormat("MYIF (a) f();\n"
1031                "else {\n"
1032                "  g();\n"
1033                "}",
1034                AllowsMergedIf);
1035   verifyFormat("MYIF (a) f();\n"
1036                "else {\n"
1037                "  if (a) f();\n"
1038                "  else {\n"
1039                "    g();\n"
1040                "  }\n"
1041                "  g();\n"
1042                "}",
1043                AllowsMergedIf);
1044 
1045   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1046   verifyFormat("MYIF (a) {\n"
1047                "  g()\n"
1048                "};",
1049                AllowsMergedIf);
1050   verifyFormat("MYIF (a) g();\n"
1051                "else\n"
1052                "  g();",
1053                AllowsMergedIf);
1054   verifyFormat("MYIF (a) {\n"
1055                "  g();\n"
1056                "} else\n"
1057                "  g();",
1058                AllowsMergedIf);
1059   verifyFormat("MYIF (a) g();\n"
1060                "else {\n"
1061                "  g();\n"
1062                "}",
1063                AllowsMergedIf);
1064   verifyFormat("MYIF (a) {\n"
1065                "  g();\n"
1066                "} else {\n"
1067                "  g();\n"
1068                "}",
1069                AllowsMergedIf);
1070   verifyFormat("MYIF (a) g();\n"
1071                "else MYIF (b)\n"
1072                "  g();\n"
1073                "else\n"
1074                "  g();",
1075                AllowsMergedIf);
1076   verifyFormat("MYIF (a) g();\n"
1077                "else if (b)\n"
1078                "  g();\n"
1079                "else\n"
1080                "  g();",
1081                AllowsMergedIf);
1082   verifyFormat("MYIF (a) {\n"
1083                "  g();\n"
1084                "} else MYIF (b)\n"
1085                "  g();\n"
1086                "else\n"
1087                "  g();",
1088                AllowsMergedIf);
1089   verifyFormat("MYIF (a) {\n"
1090                "  g();\n"
1091                "} else if (b)\n"
1092                "  g();\n"
1093                "else\n"
1094                "  g();",
1095                AllowsMergedIf);
1096   verifyFormat("MYIF (a) g();\n"
1097                "else MYIF (b) {\n"
1098                "  g();\n"
1099                "} else\n"
1100                "  g();",
1101                AllowsMergedIf);
1102   verifyFormat("MYIF (a) g();\n"
1103                "else if (b) {\n"
1104                "  g();\n"
1105                "} else\n"
1106                "  g();",
1107                AllowsMergedIf);
1108   verifyFormat("MYIF (a) g();\n"
1109                "else MYIF (b)\n"
1110                "  g();\n"
1111                "else {\n"
1112                "  g();\n"
1113                "}",
1114                AllowsMergedIf);
1115   verifyFormat("MYIF (a) g();\n"
1116                "else if (b)\n"
1117                "  g();\n"
1118                "else {\n"
1119                "  g();\n"
1120                "}",
1121                AllowsMergedIf);
1122   verifyFormat("MYIF (a) g();\n"
1123                "else MYIF (b) {\n"
1124                "  g();\n"
1125                "} else {\n"
1126                "  g();\n"
1127                "}",
1128                AllowsMergedIf);
1129   verifyFormat("MYIF (a) g();\n"
1130                "else if (b) {\n"
1131                "  g();\n"
1132                "} else {\n"
1133                "  g();\n"
1134                "}",
1135                AllowsMergedIf);
1136   verifyFormat("MYIF (a) {\n"
1137                "  g();\n"
1138                "} else MYIF (b) {\n"
1139                "  g();\n"
1140                "} else {\n"
1141                "  g();\n"
1142                "}",
1143                AllowsMergedIf);
1144   verifyFormat("MYIF (a) {\n"
1145                "  g();\n"
1146                "} else if (b) {\n"
1147                "  g();\n"
1148                "} else {\n"
1149                "  g();\n"
1150                "}",
1151                AllowsMergedIf);
1152 
1153   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1154       FormatStyle::SIS_AllIfsAndElse;
1155 
1156   verifyFormat("if (a) f();\n"
1157                "else {\n"
1158                "  g();\n"
1159                "}",
1160                AllowsMergedIf);
1161   verifyFormat("if (a) f();\n"
1162                "else {\n"
1163                "  if (a) f();\n"
1164                "  else {\n"
1165                "    g();\n"
1166                "  }\n"
1167                "  g();\n"
1168                "}",
1169                AllowsMergedIf);
1170 
1171   verifyFormat("if (a) g();", AllowsMergedIf);
1172   verifyFormat("if (a) {\n"
1173                "  g()\n"
1174                "};",
1175                AllowsMergedIf);
1176   verifyFormat("if (a) g();\n"
1177                "else g();",
1178                AllowsMergedIf);
1179   verifyFormat("if (a) {\n"
1180                "  g();\n"
1181                "} else g();",
1182                AllowsMergedIf);
1183   verifyFormat("if (a) g();\n"
1184                "else {\n"
1185                "  g();\n"
1186                "}",
1187                AllowsMergedIf);
1188   verifyFormat("if (a) {\n"
1189                "  g();\n"
1190                "} else {\n"
1191                "  g();\n"
1192                "}",
1193                AllowsMergedIf);
1194   verifyFormat("if (a) g();\n"
1195                "else if (b) g();\n"
1196                "else g();",
1197                AllowsMergedIf);
1198   verifyFormat("if (a) {\n"
1199                "  g();\n"
1200                "} else if (b) g();\n"
1201                "else g();",
1202                AllowsMergedIf);
1203   verifyFormat("if (a) g();\n"
1204                "else if (b) {\n"
1205                "  g();\n"
1206                "} else g();",
1207                AllowsMergedIf);
1208   verifyFormat("if (a) g();\n"
1209                "else if (b) g();\n"
1210                "else {\n"
1211                "  g();\n"
1212                "}",
1213                AllowsMergedIf);
1214   verifyFormat("if (a) g();\n"
1215                "else if (b) {\n"
1216                "  g();\n"
1217                "} else {\n"
1218                "  g();\n"
1219                "}",
1220                AllowsMergedIf);
1221   verifyFormat("if (a) {\n"
1222                "  g();\n"
1223                "} else if (b) {\n"
1224                "  g();\n"
1225                "} else {\n"
1226                "  g();\n"
1227                "}",
1228                AllowsMergedIf);
1229   verifyFormat("MYIF (a) f();\n"
1230                "else {\n"
1231                "  g();\n"
1232                "}",
1233                AllowsMergedIf);
1234   verifyFormat("MYIF (a) f();\n"
1235                "else {\n"
1236                "  if (a) f();\n"
1237                "  else {\n"
1238                "    g();\n"
1239                "  }\n"
1240                "  g();\n"
1241                "}",
1242                AllowsMergedIf);
1243 
1244   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1245   verifyFormat("MYIF (a) {\n"
1246                "  g()\n"
1247                "};",
1248                AllowsMergedIf);
1249   verifyFormat("MYIF (a) g();\n"
1250                "else g();",
1251                AllowsMergedIf);
1252   verifyFormat("MYIF (a) {\n"
1253                "  g();\n"
1254                "} else g();",
1255                AllowsMergedIf);
1256   verifyFormat("MYIF (a) g();\n"
1257                "else {\n"
1258                "  g();\n"
1259                "}",
1260                AllowsMergedIf);
1261   verifyFormat("MYIF (a) {\n"
1262                "  g();\n"
1263                "} else {\n"
1264                "  g();\n"
1265                "}",
1266                AllowsMergedIf);
1267   verifyFormat("MYIF (a) g();\n"
1268                "else MYIF (b) g();\n"
1269                "else g();",
1270                AllowsMergedIf);
1271   verifyFormat("MYIF (a) g();\n"
1272                "else if (b) g();\n"
1273                "else g();",
1274                AllowsMergedIf);
1275   verifyFormat("MYIF (a) {\n"
1276                "  g();\n"
1277                "} else MYIF (b) g();\n"
1278                "else g();",
1279                AllowsMergedIf);
1280   verifyFormat("MYIF (a) {\n"
1281                "  g();\n"
1282                "} else if (b) g();\n"
1283                "else g();",
1284                AllowsMergedIf);
1285   verifyFormat("MYIF (a) g();\n"
1286                "else MYIF (b) {\n"
1287                "  g();\n"
1288                "} else g();",
1289                AllowsMergedIf);
1290   verifyFormat("MYIF (a) g();\n"
1291                "else if (b) {\n"
1292                "  g();\n"
1293                "} else g();",
1294                AllowsMergedIf);
1295   verifyFormat("MYIF (a) g();\n"
1296                "else MYIF (b) g();\n"
1297                "else {\n"
1298                "  g();\n"
1299                "}",
1300                AllowsMergedIf);
1301   verifyFormat("MYIF (a) g();\n"
1302                "else if (b) g();\n"
1303                "else {\n"
1304                "  g();\n"
1305                "}",
1306                AllowsMergedIf);
1307   verifyFormat("MYIF (a) g();\n"
1308                "else MYIF (b) {\n"
1309                "  g();\n"
1310                "} else {\n"
1311                "  g();\n"
1312                "}",
1313                AllowsMergedIf);
1314   verifyFormat("MYIF (a) g();\n"
1315                "else if (b) {\n"
1316                "  g();\n"
1317                "} else {\n"
1318                "  g();\n"
1319                "}",
1320                AllowsMergedIf);
1321   verifyFormat("MYIF (a) {\n"
1322                "  g();\n"
1323                "} else MYIF (b) {\n"
1324                "  g();\n"
1325                "} else {\n"
1326                "  g();\n"
1327                "}",
1328                AllowsMergedIf);
1329   verifyFormat("MYIF (a) {\n"
1330                "  g();\n"
1331                "} else if (b) {\n"
1332                "  g();\n"
1333                "} else {\n"
1334                "  g();\n"
1335                "}",
1336                AllowsMergedIf);
1337 }
1338 
1339 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1340   FormatStyle AllowsMergedLoops = getLLVMStyle();
1341   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1342   verifyFormat("while (true) continue;", AllowsMergedLoops);
1343   verifyFormat("for (;;) continue;", AllowsMergedLoops);
1344   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1345   verifyFormat("while (true)\n"
1346                "  ;",
1347                AllowsMergedLoops);
1348   verifyFormat("for (;;)\n"
1349                "  ;",
1350                AllowsMergedLoops);
1351   verifyFormat("for (;;)\n"
1352                "  for (;;) continue;",
1353                AllowsMergedLoops);
1354   verifyFormat("for (;;) // Can't merge this\n"
1355                "  continue;",
1356                AllowsMergedLoops);
1357   verifyFormat("for (;;) /* still don't merge */\n"
1358                "  continue;",
1359                AllowsMergedLoops);
1360   verifyFormat("do a++;\n"
1361                "while (true);",
1362                AllowsMergedLoops);
1363   verifyFormat("do /* Don't merge */\n"
1364                "  a++;\n"
1365                "while (true);",
1366                AllowsMergedLoops);
1367   verifyFormat("do // Don't merge\n"
1368                "  a++;\n"
1369                "while (true);",
1370                AllowsMergedLoops);
1371   verifyFormat("do\n"
1372                "  // Don't merge\n"
1373                "  a++;\n"
1374                "while (true);",
1375                AllowsMergedLoops);
1376   // Without braces labels are interpreted differently.
1377   verifyFormat("{\n"
1378                "  do\n"
1379                "  label:\n"
1380                "    a++;\n"
1381                "  while (true);\n"
1382                "}",
1383                AllowsMergedLoops);
1384 }
1385 
1386 TEST_F(FormatTest, FormatShortBracedStatements) {
1387   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1388   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1389   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1390   // Not IF to avoid any confusion that IF is somehow special.
1391   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1392   AllowSimpleBracedStatements.ColumnLimit = 40;
1393   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1394       FormatStyle::SBS_Always;
1395 
1396   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1397       FormatStyle::SIS_WithoutElse;
1398   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1399 
1400   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1401   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1402   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1403 
1404   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1405   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1406   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1407   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1408   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1409   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1410   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1411   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1412   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1413   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1414   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1415   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1416   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1417   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1418   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1419   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1420   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1421                AllowSimpleBracedStatements);
1422   verifyFormat("if (true) {\n"
1423                "  ffffffffffffffffffffffff();\n"
1424                "}",
1425                AllowSimpleBracedStatements);
1426   verifyFormat("if (true) {\n"
1427                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1428                "}",
1429                AllowSimpleBracedStatements);
1430   verifyFormat("if (true) { //\n"
1431                "  f();\n"
1432                "}",
1433                AllowSimpleBracedStatements);
1434   verifyFormat("if (true) {\n"
1435                "  f();\n"
1436                "  f();\n"
1437                "}",
1438                AllowSimpleBracedStatements);
1439   verifyFormat("if (true) {\n"
1440                "  f();\n"
1441                "} else {\n"
1442                "  f();\n"
1443                "}",
1444                AllowSimpleBracedStatements);
1445   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1446                AllowSimpleBracedStatements);
1447   verifyFormat("MYIF (true) {\n"
1448                "  ffffffffffffffffffffffff();\n"
1449                "}",
1450                AllowSimpleBracedStatements);
1451   verifyFormat("MYIF (true) {\n"
1452                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1453                "}",
1454                AllowSimpleBracedStatements);
1455   verifyFormat("MYIF (true) { //\n"
1456                "  f();\n"
1457                "}",
1458                AllowSimpleBracedStatements);
1459   verifyFormat("MYIF (true) {\n"
1460                "  f();\n"
1461                "  f();\n"
1462                "}",
1463                AllowSimpleBracedStatements);
1464   verifyFormat("MYIF (true) {\n"
1465                "  f();\n"
1466                "} else {\n"
1467                "  f();\n"
1468                "}",
1469                AllowSimpleBracedStatements);
1470 
1471   verifyFormat("struct A2 {\n"
1472                "  int X;\n"
1473                "};",
1474                AllowSimpleBracedStatements);
1475   verifyFormat("typedef struct A2 {\n"
1476                "  int X;\n"
1477                "} A2_t;",
1478                AllowSimpleBracedStatements);
1479   verifyFormat("template <int> struct A2 {\n"
1480                "  struct B {};\n"
1481                "};",
1482                AllowSimpleBracedStatements);
1483 
1484   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1485       FormatStyle::SIS_Never;
1486   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1487   verifyFormat("if (true) {\n"
1488                "  f();\n"
1489                "}",
1490                AllowSimpleBracedStatements);
1491   verifyFormat("if (true) {\n"
1492                "  f();\n"
1493                "} else {\n"
1494                "  f();\n"
1495                "}",
1496                AllowSimpleBracedStatements);
1497   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1498   verifyFormat("MYIF (true) {\n"
1499                "  f();\n"
1500                "}",
1501                AllowSimpleBracedStatements);
1502   verifyFormat("MYIF (true) {\n"
1503                "  f();\n"
1504                "} else {\n"
1505                "  f();\n"
1506                "}",
1507                AllowSimpleBracedStatements);
1508 
1509   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1510   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1511   verifyFormat("while (true) {\n"
1512                "  f();\n"
1513                "}",
1514                AllowSimpleBracedStatements);
1515   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1516   verifyFormat("for (;;) {\n"
1517                "  f();\n"
1518                "}",
1519                AllowSimpleBracedStatements);
1520 
1521   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1522       FormatStyle::SIS_WithoutElse;
1523   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1524   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1525       FormatStyle::BWACS_Always;
1526 
1527   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1528   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1529   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1530   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1531   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1532   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1533   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1534   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1535   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1536   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1537   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1538   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1539   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1540   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1541   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1542   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1543   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1544                AllowSimpleBracedStatements);
1545   verifyFormat("if (true)\n"
1546                "{\n"
1547                "  ffffffffffffffffffffffff();\n"
1548                "}",
1549                AllowSimpleBracedStatements);
1550   verifyFormat("if (true)\n"
1551                "{\n"
1552                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1553                "}",
1554                AllowSimpleBracedStatements);
1555   verifyFormat("if (true)\n"
1556                "{ //\n"
1557                "  f();\n"
1558                "}",
1559                AllowSimpleBracedStatements);
1560   verifyFormat("if (true)\n"
1561                "{\n"
1562                "  f();\n"
1563                "  f();\n"
1564                "}",
1565                AllowSimpleBracedStatements);
1566   verifyFormat("if (true)\n"
1567                "{\n"
1568                "  f();\n"
1569                "} else\n"
1570                "{\n"
1571                "  f();\n"
1572                "}",
1573                AllowSimpleBracedStatements);
1574   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1575                AllowSimpleBracedStatements);
1576   verifyFormat("MYIF (true)\n"
1577                "{\n"
1578                "  ffffffffffffffffffffffff();\n"
1579                "}",
1580                AllowSimpleBracedStatements);
1581   verifyFormat("MYIF (true)\n"
1582                "{\n"
1583                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1584                "}",
1585                AllowSimpleBracedStatements);
1586   verifyFormat("MYIF (true)\n"
1587                "{ //\n"
1588                "  f();\n"
1589                "}",
1590                AllowSimpleBracedStatements);
1591   verifyFormat("MYIF (true)\n"
1592                "{\n"
1593                "  f();\n"
1594                "  f();\n"
1595                "}",
1596                AllowSimpleBracedStatements);
1597   verifyFormat("MYIF (true)\n"
1598                "{\n"
1599                "  f();\n"
1600                "} else\n"
1601                "{\n"
1602                "  f();\n"
1603                "}",
1604                AllowSimpleBracedStatements);
1605 
1606   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1607       FormatStyle::SIS_Never;
1608   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1609   verifyFormat("if (true)\n"
1610                "{\n"
1611                "  f();\n"
1612                "}",
1613                AllowSimpleBracedStatements);
1614   verifyFormat("if (true)\n"
1615                "{\n"
1616                "  f();\n"
1617                "} else\n"
1618                "{\n"
1619                "  f();\n"
1620                "}",
1621                AllowSimpleBracedStatements);
1622   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1623   verifyFormat("MYIF (true)\n"
1624                "{\n"
1625                "  f();\n"
1626                "}",
1627                AllowSimpleBracedStatements);
1628   verifyFormat("MYIF (true)\n"
1629                "{\n"
1630                "  f();\n"
1631                "} else\n"
1632                "{\n"
1633                "  f();\n"
1634                "}",
1635                AllowSimpleBracedStatements);
1636 
1637   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1638   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1639   verifyFormat("while (true)\n"
1640                "{\n"
1641                "  f();\n"
1642                "}",
1643                AllowSimpleBracedStatements);
1644   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1645   verifyFormat("for (;;)\n"
1646                "{\n"
1647                "  f();\n"
1648                "}",
1649                AllowSimpleBracedStatements);
1650 }
1651 
1652 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1653   FormatStyle Style = getLLVMStyleWithColumns(60);
1654   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1655   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1656   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1657   EXPECT_EQ("#define A                                                  \\\n"
1658             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1659             "  {                                                        \\\n"
1660             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1661             "  }\n"
1662             "X;",
1663             format("#define A \\\n"
1664                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1665                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1666                    "   }\n"
1667                    "X;",
1668                    Style));
1669 }
1670 
1671 TEST_F(FormatTest, ParseIfElse) {
1672   verifyFormat("if (true)\n"
1673                "  if (true)\n"
1674                "    if (true)\n"
1675                "      f();\n"
1676                "    else\n"
1677                "      g();\n"
1678                "  else\n"
1679                "    h();\n"
1680                "else\n"
1681                "  i();");
1682   verifyFormat("if (true)\n"
1683                "  if (true)\n"
1684                "    if (true) {\n"
1685                "      if (true)\n"
1686                "        f();\n"
1687                "    } else {\n"
1688                "      g();\n"
1689                "    }\n"
1690                "  else\n"
1691                "    h();\n"
1692                "else {\n"
1693                "  i();\n"
1694                "}");
1695   verifyFormat("if (true)\n"
1696                "  if constexpr (true)\n"
1697                "    if (true) {\n"
1698                "      if constexpr (true)\n"
1699                "        f();\n"
1700                "    } else {\n"
1701                "      g();\n"
1702                "    }\n"
1703                "  else\n"
1704                "    h();\n"
1705                "else {\n"
1706                "  i();\n"
1707                "}");
1708   verifyFormat("if (true)\n"
1709                "  if CONSTEXPR (true)\n"
1710                "    if (true) {\n"
1711                "      if CONSTEXPR (true)\n"
1712                "        f();\n"
1713                "    } else {\n"
1714                "      g();\n"
1715                "    }\n"
1716                "  else\n"
1717                "    h();\n"
1718                "else {\n"
1719                "  i();\n"
1720                "}");
1721   verifyFormat("void f() {\n"
1722                "  if (a) {\n"
1723                "  } else {\n"
1724                "  }\n"
1725                "}");
1726 }
1727 
1728 TEST_F(FormatTest, ElseIf) {
1729   verifyFormat("if (a) {\n} else if (b) {\n}");
1730   verifyFormat("if (a)\n"
1731                "  f();\n"
1732                "else if (b)\n"
1733                "  g();\n"
1734                "else\n"
1735                "  h();");
1736   verifyFormat("if (a)\n"
1737                "  f();\n"
1738                "else // comment\n"
1739                "  if (b) {\n"
1740                "    g();\n"
1741                "    h();\n"
1742                "  }");
1743   verifyFormat("if constexpr (a)\n"
1744                "  f();\n"
1745                "else if constexpr (b)\n"
1746                "  g();\n"
1747                "else\n"
1748                "  h();");
1749   verifyFormat("if CONSTEXPR (a)\n"
1750                "  f();\n"
1751                "else if CONSTEXPR (b)\n"
1752                "  g();\n"
1753                "else\n"
1754                "  h();");
1755   verifyFormat("if (a) {\n"
1756                "  f();\n"
1757                "}\n"
1758                "// or else ..\n"
1759                "else {\n"
1760                "  g()\n"
1761                "}");
1762 
1763   verifyFormat("if (a) {\n"
1764                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1765                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1766                "}");
1767   verifyFormat("if (a) {\n"
1768                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1769                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1770                "}");
1771   verifyFormat("if (a) {\n"
1772                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1773                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1774                "}");
1775   verifyFormat("if (a) {\n"
1776                "} else if (\n"
1777                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1778                "}",
1779                getLLVMStyleWithColumns(62));
1780   verifyFormat("if (a) {\n"
1781                "} else if constexpr (\n"
1782                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1783                "}",
1784                getLLVMStyleWithColumns(62));
1785   verifyFormat("if (a) {\n"
1786                "} else if CONSTEXPR (\n"
1787                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1788                "}",
1789                getLLVMStyleWithColumns(62));
1790 }
1791 
1792 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1793   FormatStyle Style = getLLVMStyle();
1794   // Check first the default LLVM style
1795   // Style.PointerAlignment = FormatStyle::PAS_Right;
1796   // Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1797   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1798   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
1799   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
1800   verifyFormat("int *f1(int &a) const &;", Style);
1801   verifyFormat("int *f1(int &a) const & = 0;", Style);
1802   verifyFormat("int *a = f1();", Style);
1803   verifyFormat("int &b = f2();", Style);
1804   verifyFormat("int &&c = f3();", Style);
1805 
1806   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1807   verifyFormat("Const unsigned int *c;\n"
1808                "const unsigned int *d;\n"
1809                "Const unsigned int &e;\n"
1810                "const unsigned int &f;\n"
1811                "const unsigned    &&g;\n"
1812                "Const unsigned      h;",
1813                Style);
1814 
1815   Style.PointerAlignment = FormatStyle::PAS_Left;
1816   Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1817   verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
1818   verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
1819   verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
1820   verifyFormat("int* f1(int& a) const& = 0;", Style);
1821   verifyFormat("int* a = f1();", Style);
1822   verifyFormat("int& b = f2();", Style);
1823   verifyFormat("int&& c = f3();", Style);
1824 
1825   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1826   verifyFormat("Const unsigned int* c;\n"
1827                "const unsigned int* d;\n"
1828                "Const unsigned int& e;\n"
1829                "const unsigned int& f;\n"
1830                "const unsigned&&    g;\n"
1831                "Const unsigned      h;",
1832                Style);
1833 
1834   Style.PointerAlignment = FormatStyle::PAS_Right;
1835   Style.ReferenceAlignment = FormatStyle::RAS_Left;
1836   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
1837   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
1838   verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
1839   verifyFormat("int *a = f1();", Style);
1840   verifyFormat("int& b = f2();", Style);
1841   verifyFormat("int&& c = f3();", Style);
1842 
1843   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1844   verifyFormat("Const unsigned int *c;\n"
1845                "const unsigned int *d;\n"
1846                "Const unsigned int& e;\n"
1847                "const unsigned int& f;\n"
1848                "const unsigned      g;\n"
1849                "Const unsigned      h;",
1850                Style);
1851 
1852   Style.PointerAlignment = FormatStyle::PAS_Left;
1853   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
1854   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
1855   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
1856   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
1857   verifyFormat("int* a = f1();", Style);
1858   verifyFormat("int & b = f2();", Style);
1859   verifyFormat("int && c = f3();", Style);
1860 
1861   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1862   verifyFormat("Const unsigned int*  c;\n"
1863                "const unsigned int*  d;\n"
1864                "Const unsigned int & e;\n"
1865                "const unsigned int & f;\n"
1866                "const unsigned &&    g;\n"
1867                "Const unsigned       h;",
1868                Style);
1869 
1870   Style.PointerAlignment = FormatStyle::PAS_Middle;
1871   Style.ReferenceAlignment = FormatStyle::RAS_Right;
1872   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
1873   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
1874   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
1875   verifyFormat("int * a = f1();", Style);
1876   verifyFormat("int &b = f2();", Style);
1877   verifyFormat("int &&c = f3();", Style);
1878 
1879   // FIXME: we don't handle this yet, so output may be arbitrary until it's
1880   // specifically handled
1881   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
1882 }
1883 
1884 TEST_F(FormatTest, FormatsForLoop) {
1885   verifyFormat(
1886       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
1887       "     ++VeryVeryLongLoopVariable)\n"
1888       "  ;");
1889   verifyFormat("for (;;)\n"
1890                "  f();");
1891   verifyFormat("for (;;) {\n}");
1892   verifyFormat("for (;;) {\n"
1893                "  f();\n"
1894                "}");
1895   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
1896 
1897   verifyFormat(
1898       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
1899       "                                          E = UnwrappedLines.end();\n"
1900       "     I != E; ++I) {\n}");
1901 
1902   verifyFormat(
1903       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
1904       "     ++IIIII) {\n}");
1905   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
1906                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
1907                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
1908   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
1909                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
1910                "         E = FD->getDeclsInPrototypeScope().end();\n"
1911                "     I != E; ++I) {\n}");
1912   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
1913                "         I = Container.begin(),\n"
1914                "         E = Container.end();\n"
1915                "     I != E; ++I) {\n}",
1916                getLLVMStyleWithColumns(76));
1917 
1918   verifyFormat(
1919       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
1920       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
1921       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1922       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
1923       "     ++aaaaaaaaaaa) {\n}");
1924   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1925                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
1926                "     ++i) {\n}");
1927   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
1928                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
1929                "}");
1930   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
1931                "         aaaaaaaaaa);\n"
1932                "     iter; ++iter) {\n"
1933                "}");
1934   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1935                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
1936                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
1937                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
1938 
1939   // These should not be formatted as Objective-C for-in loops.
1940   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
1941   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
1942   verifyFormat("Foo *x;\nfor (x in y) {\n}");
1943   verifyFormat(
1944       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
1945 
1946   FormatStyle NoBinPacking = getLLVMStyle();
1947   NoBinPacking.BinPackParameters = false;
1948   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
1949                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
1950                "                                           aaaaaaaaaaaaaaaa,\n"
1951                "                                           aaaaaaaaaaaaaaaa,\n"
1952                "                                           aaaaaaaaaaaaaaaa);\n"
1953                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
1954                "}",
1955                NoBinPacking);
1956   verifyFormat(
1957       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
1958       "                                          E = UnwrappedLines.end();\n"
1959       "     I != E;\n"
1960       "     ++I) {\n}",
1961       NoBinPacking);
1962 
1963   FormatStyle AlignLeft = getLLVMStyle();
1964   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
1965   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
1966 }
1967 
1968 TEST_F(FormatTest, RangeBasedForLoops) {
1969   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1970                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1971   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
1972                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
1973   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
1974                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1975   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
1976                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
1977 }
1978 
1979 TEST_F(FormatTest, ForEachLoops) {
1980   verifyFormat("void f() {\n"
1981                "  foreach (Item *item, itemlist) {}\n"
1982                "  Q_FOREACH (Item *item, itemlist) {}\n"
1983                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
1984                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
1985                "}");
1986 
1987   FormatStyle Style = getLLVMStyle();
1988   Style.SpaceBeforeParens =
1989       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
1990   verifyFormat("void f() {\n"
1991                "  foreach(Item *item, itemlist) {}\n"
1992                "  Q_FOREACH(Item *item, itemlist) {}\n"
1993                "  BOOST_FOREACH(Item *item, itemlist) {}\n"
1994                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
1995                "}",
1996                Style);
1997 
1998   // As function-like macros.
1999   verifyFormat("#define foreach(x, y)\n"
2000                "#define Q_FOREACH(x, y)\n"
2001                "#define BOOST_FOREACH(x, y)\n"
2002                "#define UNKNOWN_FOREACH(x, y)\n");
2003 
2004   // Not as function-like macros.
2005   verifyFormat("#define foreach (x, y)\n"
2006                "#define Q_FOREACH (x, y)\n"
2007                "#define BOOST_FOREACH (x, y)\n"
2008                "#define UNKNOWN_FOREACH (x, y)\n");
2009 
2010   // handle microsoft non standard extension
2011   verifyFormat("for each (char c in x->MyStringProperty)");
2012 }
2013 
2014 TEST_F(FormatTest, FormatsWhileLoop) {
2015   verifyFormat("while (true) {\n}");
2016   verifyFormat("while (true)\n"
2017                "  f();");
2018   verifyFormat("while () {\n}");
2019   verifyFormat("while () {\n"
2020                "  f();\n"
2021                "}");
2022 }
2023 
2024 TEST_F(FormatTest, FormatsDoWhile) {
2025   verifyFormat("do {\n"
2026                "  do_something();\n"
2027                "} while (something());");
2028   verifyFormat("do\n"
2029                "  do_something();\n"
2030                "while (something());");
2031 }
2032 
2033 TEST_F(FormatTest, FormatsSwitchStatement) {
2034   verifyFormat("switch (x) {\n"
2035                "case 1:\n"
2036                "  f();\n"
2037                "  break;\n"
2038                "case kFoo:\n"
2039                "case ns::kBar:\n"
2040                "case kBaz:\n"
2041                "  break;\n"
2042                "default:\n"
2043                "  g();\n"
2044                "  break;\n"
2045                "}");
2046   verifyFormat("switch (x) {\n"
2047                "case 1: {\n"
2048                "  f();\n"
2049                "  break;\n"
2050                "}\n"
2051                "case 2: {\n"
2052                "  break;\n"
2053                "}\n"
2054                "}");
2055   verifyFormat("switch (x) {\n"
2056                "case 1: {\n"
2057                "  f();\n"
2058                "  {\n"
2059                "    g();\n"
2060                "    h();\n"
2061                "  }\n"
2062                "  break;\n"
2063                "}\n"
2064                "}");
2065   verifyFormat("switch (x) {\n"
2066                "case 1: {\n"
2067                "  f();\n"
2068                "  if (foo) {\n"
2069                "    g();\n"
2070                "    h();\n"
2071                "  }\n"
2072                "  break;\n"
2073                "}\n"
2074                "}");
2075   verifyFormat("switch (x) {\n"
2076                "case 1: {\n"
2077                "  f();\n"
2078                "  g();\n"
2079                "} break;\n"
2080                "}");
2081   verifyFormat("switch (test)\n"
2082                "  ;");
2083   verifyFormat("switch (x) {\n"
2084                "default: {\n"
2085                "  // Do nothing.\n"
2086                "}\n"
2087                "}");
2088   verifyFormat("switch (x) {\n"
2089                "// comment\n"
2090                "// if 1, do f()\n"
2091                "case 1:\n"
2092                "  f();\n"
2093                "}");
2094   verifyFormat("switch (x) {\n"
2095                "case 1:\n"
2096                "  // Do amazing stuff\n"
2097                "  {\n"
2098                "    f();\n"
2099                "    g();\n"
2100                "  }\n"
2101                "  break;\n"
2102                "}");
2103   verifyFormat("#define A          \\\n"
2104                "  switch (x) {     \\\n"
2105                "  case a:          \\\n"
2106                "    foo = b;       \\\n"
2107                "  }",
2108                getLLVMStyleWithColumns(20));
2109   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2110                "  case OP_name:                        \\\n"
2111                "    return operations::Operation##name\n",
2112                getLLVMStyleWithColumns(40));
2113   verifyFormat("switch (x) {\n"
2114                "case 1:;\n"
2115                "default:;\n"
2116                "  int i;\n"
2117                "}");
2118 
2119   verifyGoogleFormat("switch (x) {\n"
2120                      "  case 1:\n"
2121                      "    f();\n"
2122                      "    break;\n"
2123                      "  case kFoo:\n"
2124                      "  case ns::kBar:\n"
2125                      "  case kBaz:\n"
2126                      "    break;\n"
2127                      "  default:\n"
2128                      "    g();\n"
2129                      "    break;\n"
2130                      "}");
2131   verifyGoogleFormat("switch (x) {\n"
2132                      "  case 1: {\n"
2133                      "    f();\n"
2134                      "    break;\n"
2135                      "  }\n"
2136                      "}");
2137   verifyGoogleFormat("switch (test)\n"
2138                      "  ;");
2139 
2140   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2141                      "  case OP_name:              \\\n"
2142                      "    return operations::Operation##name\n");
2143   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2144                      "  // Get the correction operation class.\n"
2145                      "  switch (OpCode) {\n"
2146                      "    CASE(Add);\n"
2147                      "    CASE(Subtract);\n"
2148                      "    default:\n"
2149                      "      return operations::Unknown;\n"
2150                      "  }\n"
2151                      "#undef OPERATION_CASE\n"
2152                      "}");
2153   verifyFormat("DEBUG({\n"
2154                "  switch (x) {\n"
2155                "  case A:\n"
2156                "    f();\n"
2157                "    break;\n"
2158                "    // fallthrough\n"
2159                "  case B:\n"
2160                "    g();\n"
2161                "    break;\n"
2162                "  }\n"
2163                "});");
2164   EXPECT_EQ("DEBUG({\n"
2165             "  switch (x) {\n"
2166             "  case A:\n"
2167             "    f();\n"
2168             "    break;\n"
2169             "  // On B:\n"
2170             "  case B:\n"
2171             "    g();\n"
2172             "    break;\n"
2173             "  }\n"
2174             "});",
2175             format("DEBUG({\n"
2176                    "  switch (x) {\n"
2177                    "  case A:\n"
2178                    "    f();\n"
2179                    "    break;\n"
2180                    "  // On B:\n"
2181                    "  case B:\n"
2182                    "    g();\n"
2183                    "    break;\n"
2184                    "  }\n"
2185                    "});",
2186                    getLLVMStyle()));
2187   EXPECT_EQ("switch (n) {\n"
2188             "case 0: {\n"
2189             "  return false;\n"
2190             "}\n"
2191             "default: {\n"
2192             "  return true;\n"
2193             "}\n"
2194             "}",
2195             format("switch (n)\n"
2196                    "{\n"
2197                    "case 0: {\n"
2198                    "  return false;\n"
2199                    "}\n"
2200                    "default: {\n"
2201                    "  return true;\n"
2202                    "}\n"
2203                    "}",
2204                    getLLVMStyle()));
2205   verifyFormat("switch (a) {\n"
2206                "case (b):\n"
2207                "  return;\n"
2208                "}");
2209 
2210   verifyFormat("switch (a) {\n"
2211                "case some_namespace::\n"
2212                "    some_constant:\n"
2213                "  return;\n"
2214                "}",
2215                getLLVMStyleWithColumns(34));
2216 
2217   FormatStyle Style = getLLVMStyle();
2218   Style.IndentCaseLabels = true;
2219   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2220   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2221   Style.BraceWrapping.AfterCaseLabel = true;
2222   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2223   EXPECT_EQ("switch (n)\n"
2224             "{\n"
2225             "  case 0:\n"
2226             "  {\n"
2227             "    return false;\n"
2228             "  }\n"
2229             "  default:\n"
2230             "  {\n"
2231             "    return true;\n"
2232             "  }\n"
2233             "}",
2234             format("switch (n) {\n"
2235                    "  case 0: {\n"
2236                    "    return false;\n"
2237                    "  }\n"
2238                    "  default: {\n"
2239                    "    return true;\n"
2240                    "  }\n"
2241                    "}",
2242                    Style));
2243   Style.BraceWrapping.AfterCaseLabel = false;
2244   EXPECT_EQ("switch (n)\n"
2245             "{\n"
2246             "  case 0: {\n"
2247             "    return false;\n"
2248             "  }\n"
2249             "  default: {\n"
2250             "    return true;\n"
2251             "  }\n"
2252             "}",
2253             format("switch (n) {\n"
2254                    "  case 0:\n"
2255                    "  {\n"
2256                    "    return false;\n"
2257                    "  }\n"
2258                    "  default:\n"
2259                    "  {\n"
2260                    "    return true;\n"
2261                    "  }\n"
2262                    "}",
2263                    Style));
2264   Style.IndentCaseLabels = false;
2265   Style.IndentCaseBlocks = true;
2266   EXPECT_EQ("switch (n)\n"
2267             "{\n"
2268             "case 0:\n"
2269             "  {\n"
2270             "    return false;\n"
2271             "  }\n"
2272             "case 1:\n"
2273             "  break;\n"
2274             "default:\n"
2275             "  {\n"
2276             "    return true;\n"
2277             "  }\n"
2278             "}",
2279             format("switch (n) {\n"
2280                    "case 0: {\n"
2281                    "  return false;\n"
2282                    "}\n"
2283                    "case 1:\n"
2284                    "  break;\n"
2285                    "default: {\n"
2286                    "  return true;\n"
2287                    "}\n"
2288                    "}",
2289                    Style));
2290   Style.IndentCaseLabels = true;
2291   Style.IndentCaseBlocks = true;
2292   EXPECT_EQ("switch (n)\n"
2293             "{\n"
2294             "  case 0:\n"
2295             "    {\n"
2296             "      return false;\n"
2297             "    }\n"
2298             "  case 1:\n"
2299             "    break;\n"
2300             "  default:\n"
2301             "    {\n"
2302             "      return true;\n"
2303             "    }\n"
2304             "}",
2305             format("switch (n) {\n"
2306                    "case 0: {\n"
2307                    "  return false;\n"
2308                    "}\n"
2309                    "case 1:\n"
2310                    "  break;\n"
2311                    "default: {\n"
2312                    "  return true;\n"
2313                    "}\n"
2314                    "}",
2315                    Style));
2316 }
2317 
2318 TEST_F(FormatTest, CaseRanges) {
2319   verifyFormat("switch (x) {\n"
2320                "case 'A' ... 'Z':\n"
2321                "case 1 ... 5:\n"
2322                "case a ... b:\n"
2323                "  break;\n"
2324                "}");
2325 }
2326 
2327 TEST_F(FormatTest, ShortEnums) {
2328   FormatStyle Style = getLLVMStyle();
2329   Style.AllowShortEnumsOnASingleLine = true;
2330   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2331   Style.AllowShortEnumsOnASingleLine = false;
2332   verifyFormat("enum\n"
2333                "{\n"
2334                "  A,\n"
2335                "  B,\n"
2336                "  C\n"
2337                "} ShortEnum1, ShortEnum2;",
2338                Style);
2339 }
2340 
2341 TEST_F(FormatTest, ShortCaseLabels) {
2342   FormatStyle Style = getLLVMStyle();
2343   Style.AllowShortCaseLabelsOnASingleLine = true;
2344   verifyFormat("switch (a) {\n"
2345                "case 1: x = 1; break;\n"
2346                "case 2: return;\n"
2347                "case 3:\n"
2348                "case 4:\n"
2349                "case 5: return;\n"
2350                "case 6: // comment\n"
2351                "  return;\n"
2352                "case 7:\n"
2353                "  // comment\n"
2354                "  return;\n"
2355                "case 8:\n"
2356                "  x = 8; // comment\n"
2357                "  break;\n"
2358                "default: y = 1; break;\n"
2359                "}",
2360                Style);
2361   verifyFormat("switch (a) {\n"
2362                "case 0: return; // comment\n"
2363                "case 1: break;  // comment\n"
2364                "case 2: return;\n"
2365                "// comment\n"
2366                "case 3: return;\n"
2367                "// comment 1\n"
2368                "// comment 2\n"
2369                "// comment 3\n"
2370                "case 4: break; /* comment */\n"
2371                "case 5:\n"
2372                "  // comment\n"
2373                "  break;\n"
2374                "case 6: /* comment */ x = 1; break;\n"
2375                "case 7: x = /* comment */ 1; break;\n"
2376                "case 8:\n"
2377                "  x = 1; /* comment */\n"
2378                "  break;\n"
2379                "case 9:\n"
2380                "  break; // comment line 1\n"
2381                "         // comment line 2\n"
2382                "}",
2383                Style);
2384   EXPECT_EQ("switch (a) {\n"
2385             "case 1:\n"
2386             "  x = 8;\n"
2387             "  // fall through\n"
2388             "case 2: x = 8;\n"
2389             "// comment\n"
2390             "case 3:\n"
2391             "  return; /* comment line 1\n"
2392             "           * comment line 2 */\n"
2393             "case 4: i = 8;\n"
2394             "// something else\n"
2395             "#if FOO\n"
2396             "case 5: break;\n"
2397             "#endif\n"
2398             "}",
2399             format("switch (a) {\n"
2400                    "case 1: x = 8;\n"
2401                    "  // fall through\n"
2402                    "case 2:\n"
2403                    "  x = 8;\n"
2404                    "// comment\n"
2405                    "case 3:\n"
2406                    "  return; /* comment line 1\n"
2407                    "           * comment line 2 */\n"
2408                    "case 4:\n"
2409                    "  i = 8;\n"
2410                    "// something else\n"
2411                    "#if FOO\n"
2412                    "case 5: break;\n"
2413                    "#endif\n"
2414                    "}",
2415                    Style));
2416   EXPECT_EQ("switch (a) {\n"
2417             "case 0:\n"
2418             "  return; // long long long long long long long long long long "
2419             "long long comment\n"
2420             "          // line\n"
2421             "}",
2422             format("switch (a) {\n"
2423                    "case 0: return; // long long long long long long long long "
2424                    "long long long long comment line\n"
2425                    "}",
2426                    Style));
2427   EXPECT_EQ("switch (a) {\n"
2428             "case 0:\n"
2429             "  return; /* long long long long long long long long long long "
2430             "long long comment\n"
2431             "             line */\n"
2432             "}",
2433             format("switch (a) {\n"
2434                    "case 0: return; /* long long long long long long long long "
2435                    "long long long long comment line */\n"
2436                    "}",
2437                    Style));
2438   verifyFormat("switch (a) {\n"
2439                "#if FOO\n"
2440                "case 0: return 0;\n"
2441                "#endif\n"
2442                "}",
2443                Style);
2444   verifyFormat("switch (a) {\n"
2445                "case 1: {\n"
2446                "}\n"
2447                "case 2: {\n"
2448                "  return;\n"
2449                "}\n"
2450                "case 3: {\n"
2451                "  x = 1;\n"
2452                "  return;\n"
2453                "}\n"
2454                "case 4:\n"
2455                "  if (x)\n"
2456                "    return;\n"
2457                "}",
2458                Style);
2459   Style.ColumnLimit = 21;
2460   verifyFormat("switch (a) {\n"
2461                "case 1: x = 1; break;\n"
2462                "case 2: return;\n"
2463                "case 3:\n"
2464                "case 4:\n"
2465                "case 5: return;\n"
2466                "default:\n"
2467                "  y = 1;\n"
2468                "  break;\n"
2469                "}",
2470                Style);
2471   Style.ColumnLimit = 80;
2472   Style.AllowShortCaseLabelsOnASingleLine = false;
2473   Style.IndentCaseLabels = true;
2474   EXPECT_EQ("switch (n) {\n"
2475             "  default /*comments*/:\n"
2476             "    return true;\n"
2477             "  case 0:\n"
2478             "    return false;\n"
2479             "}",
2480             format("switch (n) {\n"
2481                    "default/*comments*/:\n"
2482                    "  return true;\n"
2483                    "case 0:\n"
2484                    "  return false;\n"
2485                    "}",
2486                    Style));
2487   Style.AllowShortCaseLabelsOnASingleLine = true;
2488   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2489   Style.BraceWrapping.AfterCaseLabel = true;
2490   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2491   EXPECT_EQ("switch (n)\n"
2492             "{\n"
2493             "  case 0:\n"
2494             "  {\n"
2495             "    return false;\n"
2496             "  }\n"
2497             "  default:\n"
2498             "  {\n"
2499             "    return true;\n"
2500             "  }\n"
2501             "}",
2502             format("switch (n) {\n"
2503                    "  case 0: {\n"
2504                    "    return false;\n"
2505                    "  }\n"
2506                    "  default:\n"
2507                    "  {\n"
2508                    "    return true;\n"
2509                    "  }\n"
2510                    "}",
2511                    Style));
2512 }
2513 
2514 TEST_F(FormatTest, FormatsLabels) {
2515   verifyFormat("void f() {\n"
2516                "  some_code();\n"
2517                "test_label:\n"
2518                "  some_other_code();\n"
2519                "  {\n"
2520                "    some_more_code();\n"
2521                "  another_label:\n"
2522                "    some_more_code();\n"
2523                "  }\n"
2524                "}");
2525   verifyFormat("{\n"
2526                "  some_code();\n"
2527                "test_label:\n"
2528                "  some_other_code();\n"
2529                "}");
2530   verifyFormat("{\n"
2531                "  some_code();\n"
2532                "test_label:;\n"
2533                "  int i = 0;\n"
2534                "}");
2535   FormatStyle Style = getLLVMStyle();
2536   Style.IndentGotoLabels = false;
2537   verifyFormat("void f() {\n"
2538                "  some_code();\n"
2539                "test_label:\n"
2540                "  some_other_code();\n"
2541                "  {\n"
2542                "    some_more_code();\n"
2543                "another_label:\n"
2544                "    some_more_code();\n"
2545                "  }\n"
2546                "}",
2547                Style);
2548   verifyFormat("{\n"
2549                "  some_code();\n"
2550                "test_label:\n"
2551                "  some_other_code();\n"
2552                "}",
2553                Style);
2554   verifyFormat("{\n"
2555                "  some_code();\n"
2556                "test_label:;\n"
2557                "  int i = 0;\n"
2558                "}");
2559 }
2560 
2561 TEST_F(FormatTest, MultiLineControlStatements) {
2562   FormatStyle Style = getLLVMStyle();
2563   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2564   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2565   Style.ColumnLimit = 20;
2566   // Short lines should keep opening brace on same line.
2567   EXPECT_EQ("if (foo) {\n"
2568             "  bar();\n"
2569             "}",
2570             format("if(foo){bar();}", Style));
2571   EXPECT_EQ("if (foo) {\n"
2572             "  bar();\n"
2573             "} else {\n"
2574             "  baz();\n"
2575             "}",
2576             format("if(foo){bar();}else{baz();}", Style));
2577   EXPECT_EQ("if (foo && bar) {\n"
2578             "  baz();\n"
2579             "}",
2580             format("if(foo&&bar){baz();}", Style));
2581   EXPECT_EQ("if (foo) {\n"
2582             "  bar();\n"
2583             "} else if (baz) {\n"
2584             "  quux();\n"
2585             "}",
2586             format("if(foo){bar();}else if(baz){quux();}", Style));
2587   EXPECT_EQ(
2588       "if (foo) {\n"
2589       "  bar();\n"
2590       "} else if (baz) {\n"
2591       "  quux();\n"
2592       "} else {\n"
2593       "  foobar();\n"
2594       "}",
2595       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2596   EXPECT_EQ("for (;;) {\n"
2597             "  foo();\n"
2598             "}",
2599             format("for(;;){foo();}"));
2600   EXPECT_EQ("while (1) {\n"
2601             "  foo();\n"
2602             "}",
2603             format("while(1){foo();}", Style));
2604   EXPECT_EQ("switch (foo) {\n"
2605             "case bar:\n"
2606             "  return;\n"
2607             "}",
2608             format("switch(foo){case bar:return;}", Style));
2609   EXPECT_EQ("try {\n"
2610             "  foo();\n"
2611             "} catch (...) {\n"
2612             "  bar();\n"
2613             "}",
2614             format("try{foo();}catch(...){bar();}", Style));
2615   EXPECT_EQ("do {\n"
2616             "  foo();\n"
2617             "} while (bar &&\n"
2618             "         baz);",
2619             format("do{foo();}while(bar&&baz);", Style));
2620   // Long lines should put opening brace on new line.
2621   EXPECT_EQ("if (foo && bar &&\n"
2622             "    baz)\n"
2623             "{\n"
2624             "  quux();\n"
2625             "}",
2626             format("if(foo&&bar&&baz){quux();}", Style));
2627   EXPECT_EQ("if (foo && bar &&\n"
2628             "    baz)\n"
2629             "{\n"
2630             "  quux();\n"
2631             "}",
2632             format("if (foo && bar &&\n"
2633                    "    baz) {\n"
2634                    "  quux();\n"
2635                    "}",
2636                    Style));
2637   EXPECT_EQ("if (foo) {\n"
2638             "  bar();\n"
2639             "} else if (baz ||\n"
2640             "           quux)\n"
2641             "{\n"
2642             "  foobar();\n"
2643             "}",
2644             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2645   EXPECT_EQ(
2646       "if (foo) {\n"
2647       "  bar();\n"
2648       "} else if (baz ||\n"
2649       "           quux)\n"
2650       "{\n"
2651       "  foobar();\n"
2652       "} else {\n"
2653       "  barbaz();\n"
2654       "}",
2655       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2656              Style));
2657   EXPECT_EQ("for (int i = 0;\n"
2658             "     i < 10; ++i)\n"
2659             "{\n"
2660             "  foo();\n"
2661             "}",
2662             format("for(int i=0;i<10;++i){foo();}", Style));
2663   EXPECT_EQ("foreach (int i,\n"
2664             "         list)\n"
2665             "{\n"
2666             "  foo();\n"
2667             "}",
2668             format("foreach(int i, list){foo();}", Style));
2669   Style.ColumnLimit =
2670       40; // to concentrate at brace wrapping, not line wrap due to column limit
2671   EXPECT_EQ("foreach (int i, list) {\n"
2672             "  foo();\n"
2673             "}",
2674             format("foreach(int i, list){foo();}", Style));
2675   Style.ColumnLimit =
2676       20; // to concentrate at brace wrapping, not line wrap due to column limit
2677   EXPECT_EQ("while (foo || bar ||\n"
2678             "       baz)\n"
2679             "{\n"
2680             "  quux();\n"
2681             "}",
2682             format("while(foo||bar||baz){quux();}", Style));
2683   EXPECT_EQ("switch (\n"
2684             "    foo = barbaz)\n"
2685             "{\n"
2686             "case quux:\n"
2687             "  return;\n"
2688             "}",
2689             format("switch(foo=barbaz){case quux:return;}", Style));
2690   EXPECT_EQ("try {\n"
2691             "  foo();\n"
2692             "} catch (\n"
2693             "    Exception &bar)\n"
2694             "{\n"
2695             "  baz();\n"
2696             "}",
2697             format("try{foo();}catch(Exception&bar){baz();}", Style));
2698   Style.ColumnLimit =
2699       40; // to concentrate at brace wrapping, not line wrap due to column limit
2700   EXPECT_EQ("try {\n"
2701             "  foo();\n"
2702             "} catch (Exception &bar) {\n"
2703             "  baz();\n"
2704             "}",
2705             format("try{foo();}catch(Exception&bar){baz();}", Style));
2706   Style.ColumnLimit =
2707       20; // to concentrate at brace wrapping, not line wrap due to column limit
2708 
2709   Style.BraceWrapping.BeforeElse = true;
2710   EXPECT_EQ(
2711       "if (foo) {\n"
2712       "  bar();\n"
2713       "}\n"
2714       "else if (baz ||\n"
2715       "         quux)\n"
2716       "{\n"
2717       "  foobar();\n"
2718       "}\n"
2719       "else {\n"
2720       "  barbaz();\n"
2721       "}",
2722       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2723              Style));
2724 
2725   Style.BraceWrapping.BeforeCatch = true;
2726   EXPECT_EQ("try {\n"
2727             "  foo();\n"
2728             "}\n"
2729             "catch (...) {\n"
2730             "  baz();\n"
2731             "}",
2732             format("try{foo();}catch(...){baz();}", Style));
2733 }
2734 
2735 TEST_F(FormatTest, BeforeWhile) {
2736   FormatStyle Style = getLLVMStyle();
2737   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2738 
2739   verifyFormat("do {\n"
2740                "  foo();\n"
2741                "} while (1);",
2742                Style);
2743   Style.BraceWrapping.BeforeWhile = true;
2744   verifyFormat("do {\n"
2745                "  foo();\n"
2746                "}\n"
2747                "while (1);",
2748                Style);
2749 }
2750 
2751 //===----------------------------------------------------------------------===//
2752 // Tests for classes, namespaces, etc.
2753 //===----------------------------------------------------------------------===//
2754 
2755 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
2756   verifyFormat("class A {};");
2757 }
2758 
2759 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
2760   verifyFormat("class A {\n"
2761                "public:\n"
2762                "public: // comment\n"
2763                "protected:\n"
2764                "private:\n"
2765                "  void f() {}\n"
2766                "};");
2767   verifyFormat("export class A {\n"
2768                "public:\n"
2769                "public: // comment\n"
2770                "protected:\n"
2771                "private:\n"
2772                "  void f() {}\n"
2773                "};");
2774   verifyGoogleFormat("class A {\n"
2775                      " public:\n"
2776                      " protected:\n"
2777                      " private:\n"
2778                      "  void f() {}\n"
2779                      "};");
2780   verifyGoogleFormat("export class A {\n"
2781                      " public:\n"
2782                      " protected:\n"
2783                      " private:\n"
2784                      "  void f() {}\n"
2785                      "};");
2786   verifyFormat("class A {\n"
2787                "public slots:\n"
2788                "  void f1() {}\n"
2789                "public Q_SLOTS:\n"
2790                "  void f2() {}\n"
2791                "protected slots:\n"
2792                "  void f3() {}\n"
2793                "protected Q_SLOTS:\n"
2794                "  void f4() {}\n"
2795                "private slots:\n"
2796                "  void f5() {}\n"
2797                "private Q_SLOTS:\n"
2798                "  void f6() {}\n"
2799                "signals:\n"
2800                "  void g1();\n"
2801                "Q_SIGNALS:\n"
2802                "  void g2();\n"
2803                "};");
2804 
2805   // Don't interpret 'signals' the wrong way.
2806   verifyFormat("signals.set();");
2807   verifyFormat("for (Signals signals : f()) {\n}");
2808   verifyFormat("{\n"
2809                "  signals.set(); // This needs indentation.\n"
2810                "}");
2811   verifyFormat("void f() {\n"
2812                "label:\n"
2813                "  signals.baz();\n"
2814                "}");
2815 }
2816 
2817 TEST_F(FormatTest, SeparatesLogicalBlocks) {
2818   EXPECT_EQ("class A {\n"
2819             "public:\n"
2820             "  void f();\n"
2821             "\n"
2822             "private:\n"
2823             "  void g() {}\n"
2824             "  // test\n"
2825             "protected:\n"
2826             "  int h;\n"
2827             "};",
2828             format("class A {\n"
2829                    "public:\n"
2830                    "void f();\n"
2831                    "private:\n"
2832                    "void g() {}\n"
2833                    "// test\n"
2834                    "protected:\n"
2835                    "int h;\n"
2836                    "};"));
2837   EXPECT_EQ("class A {\n"
2838             "protected:\n"
2839             "public:\n"
2840             "  void f();\n"
2841             "};",
2842             format("class A {\n"
2843                    "protected:\n"
2844                    "\n"
2845                    "public:\n"
2846                    "\n"
2847                    "  void f();\n"
2848                    "};"));
2849 
2850   // Even ensure proper spacing inside macros.
2851   EXPECT_EQ("#define B     \\\n"
2852             "  class A {   \\\n"
2853             "   protected: \\\n"
2854             "   public:    \\\n"
2855             "    void f(); \\\n"
2856             "  };",
2857             format("#define B     \\\n"
2858                    "  class A {   \\\n"
2859                    "   protected: \\\n"
2860                    "              \\\n"
2861                    "   public:    \\\n"
2862                    "              \\\n"
2863                    "    void f(); \\\n"
2864                    "  };",
2865                    getGoogleStyle()));
2866   // But don't remove empty lines after macros ending in access specifiers.
2867   EXPECT_EQ("#define A private:\n"
2868             "\n"
2869             "int i;",
2870             format("#define A         private:\n"
2871                    "\n"
2872                    "int              i;"));
2873 }
2874 
2875 TEST_F(FormatTest, FormatsClasses) {
2876   verifyFormat("class A : public B {};");
2877   verifyFormat("class A : public ::B {};");
2878 
2879   verifyFormat(
2880       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2881       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2882   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
2883                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2884                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2885   verifyFormat(
2886       "class A : public B, public C, public D, public E, public F {};");
2887   verifyFormat("class AAAAAAAAAAAA : public B,\n"
2888                "                     public C,\n"
2889                "                     public D,\n"
2890                "                     public E,\n"
2891                "                     public F,\n"
2892                "                     public G {};");
2893 
2894   verifyFormat("class\n"
2895                "    ReallyReallyLongClassName {\n"
2896                "  int i;\n"
2897                "};",
2898                getLLVMStyleWithColumns(32));
2899   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
2900                "                           aaaaaaaaaaaaaaaa> {};");
2901   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
2902                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
2903                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
2904   verifyFormat("template <class R, class C>\n"
2905                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
2906                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
2907   verifyFormat("class ::A::B {};");
2908 }
2909 
2910 TEST_F(FormatTest, BreakInheritanceStyle) {
2911   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
2912   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
2913       FormatStyle::BILS_BeforeComma;
2914   verifyFormat("class MyClass : public X {};",
2915                StyleWithInheritanceBreakBeforeComma);
2916   verifyFormat("class MyClass\n"
2917                "    : public X\n"
2918                "    , public Y {};",
2919                StyleWithInheritanceBreakBeforeComma);
2920   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
2921                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
2922                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
2923                StyleWithInheritanceBreakBeforeComma);
2924   verifyFormat("struct aaaaaaaaaaaaa\n"
2925                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
2926                "          aaaaaaaaaaaaaaaa> {};",
2927                StyleWithInheritanceBreakBeforeComma);
2928 
2929   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
2930   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
2931       FormatStyle::BILS_AfterColon;
2932   verifyFormat("class MyClass : public X {};",
2933                StyleWithInheritanceBreakAfterColon);
2934   verifyFormat("class MyClass : public X, public Y {};",
2935                StyleWithInheritanceBreakAfterColon);
2936   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
2937                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2938                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
2939                StyleWithInheritanceBreakAfterColon);
2940   verifyFormat("struct aaaaaaaaaaaaa :\n"
2941                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
2942                "        aaaaaaaaaaaaaaaa> {};",
2943                StyleWithInheritanceBreakAfterColon);
2944 
2945   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
2946   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
2947       FormatStyle::BILS_AfterComma;
2948   verifyFormat("class MyClass : public X {};",
2949                StyleWithInheritanceBreakAfterComma);
2950   verifyFormat("class MyClass : public X,\n"
2951                "                public Y {};",
2952                StyleWithInheritanceBreakAfterComma);
2953   verifyFormat(
2954       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2955       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
2956       "{};",
2957       StyleWithInheritanceBreakAfterComma);
2958   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
2959                "                           aaaaaaaaaaaaaaaa> {};",
2960                StyleWithInheritanceBreakAfterComma);
2961   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
2962                "    : public OnceBreak,\n"
2963                "      public AlwaysBreak,\n"
2964                "      EvenBasesFitInOneLine {};",
2965                StyleWithInheritanceBreakAfterComma);
2966 }
2967 
2968 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
2969   verifyFormat("class A {\n} a, b;");
2970   verifyFormat("struct A {\n} a, b;");
2971   verifyFormat("union A {\n} a;");
2972 }
2973 
2974 TEST_F(FormatTest, FormatsEnum) {
2975   verifyFormat("enum {\n"
2976                "  Zero,\n"
2977                "  One = 1,\n"
2978                "  Two = One + 1,\n"
2979                "  Three = (One + Two),\n"
2980                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2981                "  Five = (One, Two, Three, Four, 5)\n"
2982                "};");
2983   verifyGoogleFormat("enum {\n"
2984                      "  Zero,\n"
2985                      "  One = 1,\n"
2986                      "  Two = One + 1,\n"
2987                      "  Three = (One + Two),\n"
2988                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2989                      "  Five = (One, Two, Three, Four, 5)\n"
2990                      "};");
2991   verifyFormat("enum Enum {};");
2992   verifyFormat("enum {};");
2993   verifyFormat("enum X E {} d;");
2994   verifyFormat("enum __attribute__((...)) E {} d;");
2995   verifyFormat("enum __declspec__((...)) E {} d;");
2996   verifyFormat("enum {\n"
2997                "  Bar = Foo<int, int>::value\n"
2998                "};",
2999                getLLVMStyleWithColumns(30));
3000 
3001   verifyFormat("enum ShortEnum { A, B, C };");
3002   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3003 
3004   EXPECT_EQ("enum KeepEmptyLines {\n"
3005             "  ONE,\n"
3006             "\n"
3007             "  TWO,\n"
3008             "\n"
3009             "  THREE\n"
3010             "}",
3011             format("enum KeepEmptyLines {\n"
3012                    "  ONE,\n"
3013                    "\n"
3014                    "  TWO,\n"
3015                    "\n"
3016                    "\n"
3017                    "  THREE\n"
3018                    "}"));
3019   verifyFormat("enum E { // comment\n"
3020                "  ONE,\n"
3021                "  TWO\n"
3022                "};\n"
3023                "int i;");
3024 
3025   FormatStyle EightIndent = getLLVMStyle();
3026   EightIndent.IndentWidth = 8;
3027   verifyFormat("enum {\n"
3028                "        VOID,\n"
3029                "        CHAR,\n"
3030                "        SHORT,\n"
3031                "        INT,\n"
3032                "        LONG,\n"
3033                "        SIGNED,\n"
3034                "        UNSIGNED,\n"
3035                "        BOOL,\n"
3036                "        FLOAT,\n"
3037                "        DOUBLE,\n"
3038                "        COMPLEX\n"
3039                "};",
3040                EightIndent);
3041 
3042   // Not enums.
3043   verifyFormat("enum X f() {\n"
3044                "  a();\n"
3045                "  return 42;\n"
3046                "}");
3047   verifyFormat("enum X Type::f() {\n"
3048                "  a();\n"
3049                "  return 42;\n"
3050                "}");
3051   verifyFormat("enum ::X f() {\n"
3052                "  a();\n"
3053                "  return 42;\n"
3054                "}");
3055   verifyFormat("enum ns::X f() {\n"
3056                "  a();\n"
3057                "  return 42;\n"
3058                "}");
3059 }
3060 
3061 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3062   verifyFormat("enum Type {\n"
3063                "  One = 0; // These semicolons should be commas.\n"
3064                "  Two = 1;\n"
3065                "};");
3066   verifyFormat("namespace n {\n"
3067                "enum Type {\n"
3068                "  One,\n"
3069                "  Two, // missing };\n"
3070                "  int i;\n"
3071                "}\n"
3072                "void g() {}");
3073 }
3074 
3075 TEST_F(FormatTest, FormatsEnumStruct) {
3076   verifyFormat("enum struct {\n"
3077                "  Zero,\n"
3078                "  One = 1,\n"
3079                "  Two = One + 1,\n"
3080                "  Three = (One + Two),\n"
3081                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3082                "  Five = (One, Two, Three, Four, 5)\n"
3083                "};");
3084   verifyFormat("enum struct Enum {};");
3085   verifyFormat("enum struct {};");
3086   verifyFormat("enum struct X E {} d;");
3087   verifyFormat("enum struct __attribute__((...)) E {} d;");
3088   verifyFormat("enum struct __declspec__((...)) E {} d;");
3089   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3090 }
3091 
3092 TEST_F(FormatTest, FormatsEnumClass) {
3093   verifyFormat("enum class {\n"
3094                "  Zero,\n"
3095                "  One = 1,\n"
3096                "  Two = One + 1,\n"
3097                "  Three = (One + Two),\n"
3098                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3099                "  Five = (One, Two, Three, Four, 5)\n"
3100                "};");
3101   verifyFormat("enum class Enum {};");
3102   verifyFormat("enum class {};");
3103   verifyFormat("enum class X E {} d;");
3104   verifyFormat("enum class __attribute__((...)) E {} d;");
3105   verifyFormat("enum class __declspec__((...)) E {} d;");
3106   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3107 }
3108 
3109 TEST_F(FormatTest, FormatsEnumTypes) {
3110   verifyFormat("enum X : int {\n"
3111                "  A, // Force multiple lines.\n"
3112                "  B\n"
3113                "};");
3114   verifyFormat("enum X : int { A, B };");
3115   verifyFormat("enum X : std::uint32_t { A, B };");
3116 }
3117 
3118 TEST_F(FormatTest, FormatsTypedefEnum) {
3119   FormatStyle Style = getLLVMStyle();
3120   Style.ColumnLimit = 40;
3121   verifyFormat("typedef enum {} EmptyEnum;");
3122   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3123   verifyFormat("typedef enum {\n"
3124                "  ZERO = 0,\n"
3125                "  ONE = 1,\n"
3126                "  TWO = 2,\n"
3127                "  THREE = 3\n"
3128                "} LongEnum;",
3129                Style);
3130   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3131   Style.BraceWrapping.AfterEnum = true;
3132   verifyFormat("typedef enum {} EmptyEnum;");
3133   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3134   verifyFormat("typedef enum\n"
3135                "{\n"
3136                "  ZERO = 0,\n"
3137                "  ONE = 1,\n"
3138                "  TWO = 2,\n"
3139                "  THREE = 3\n"
3140                "} LongEnum;",
3141                Style);
3142 }
3143 
3144 TEST_F(FormatTest, FormatsNSEnums) {
3145   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3146   verifyGoogleFormat(
3147       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3148   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3149                      "  // Information about someDecentlyLongValue.\n"
3150                      "  someDecentlyLongValue,\n"
3151                      "  // Information about anotherDecentlyLongValue.\n"
3152                      "  anotherDecentlyLongValue,\n"
3153                      "  // Information about aThirdDecentlyLongValue.\n"
3154                      "  aThirdDecentlyLongValue\n"
3155                      "};");
3156   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3157                      "  // Information about someDecentlyLongValue.\n"
3158                      "  someDecentlyLongValue,\n"
3159                      "  // Information about anotherDecentlyLongValue.\n"
3160                      "  anotherDecentlyLongValue,\n"
3161                      "  // Information about aThirdDecentlyLongValue.\n"
3162                      "  aThirdDecentlyLongValue\n"
3163                      "};");
3164   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3165                      "  a = 1,\n"
3166                      "  b = 2,\n"
3167                      "  c = 3,\n"
3168                      "};");
3169   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3170                      "  a = 1,\n"
3171                      "  b = 2,\n"
3172                      "  c = 3,\n"
3173                      "};");
3174   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3175                      "  a = 1,\n"
3176                      "  b = 2,\n"
3177                      "  c = 3,\n"
3178                      "};");
3179   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3180                      "  a = 1,\n"
3181                      "  b = 2,\n"
3182                      "  c = 3,\n"
3183                      "};");
3184 }
3185 
3186 TEST_F(FormatTest, FormatsBitfields) {
3187   verifyFormat("struct Bitfields {\n"
3188                "  unsigned sClass : 8;\n"
3189                "  unsigned ValueKind : 2;\n"
3190                "};");
3191   verifyFormat("struct A {\n"
3192                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3193                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3194                "};");
3195   verifyFormat("struct MyStruct {\n"
3196                "  uchar data;\n"
3197                "  uchar : 8;\n"
3198                "  uchar : 8;\n"
3199                "  uchar other;\n"
3200                "};");
3201   FormatStyle Style = getLLVMStyle();
3202   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3203   verifyFormat("struct Bitfields {\n"
3204                "  unsigned sClass:8;\n"
3205                "  unsigned ValueKind:2;\n"
3206                "  uchar other;\n"
3207                "};",
3208                Style);
3209   verifyFormat("struct A {\n"
3210                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3211                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3212                "};",
3213                Style);
3214   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3215   verifyFormat("struct Bitfields {\n"
3216                "  unsigned sClass :8;\n"
3217                "  unsigned ValueKind :2;\n"
3218                "  uchar other;\n"
3219                "};",
3220                Style);
3221   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3222   verifyFormat("struct Bitfields {\n"
3223                "  unsigned sClass: 8;\n"
3224                "  unsigned ValueKind: 2;\n"
3225                "  uchar other;\n"
3226                "};",
3227                Style);
3228 }
3229 
3230 TEST_F(FormatTest, FormatsNamespaces) {
3231   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3232   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3233 
3234   verifyFormat("namespace some_namespace {\n"
3235                "class A {};\n"
3236                "void f() { f(); }\n"
3237                "}",
3238                LLVMWithNoNamespaceFix);
3239   verifyFormat("namespace N::inline D {\n"
3240                "class A {};\n"
3241                "void f() { f(); }\n"
3242                "}",
3243                LLVMWithNoNamespaceFix);
3244   verifyFormat("namespace N::inline D::E {\n"
3245                "class A {};\n"
3246                "void f() { f(); }\n"
3247                "}",
3248                LLVMWithNoNamespaceFix);
3249   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3250                "class A {};\n"
3251                "void f() { f(); }\n"
3252                "}",
3253                LLVMWithNoNamespaceFix);
3254   verifyFormat("/* something */ namespace some_namespace {\n"
3255                "class A {};\n"
3256                "void f() { f(); }\n"
3257                "}",
3258                LLVMWithNoNamespaceFix);
3259   verifyFormat("namespace {\n"
3260                "class A {};\n"
3261                "void f() { f(); }\n"
3262                "}",
3263                LLVMWithNoNamespaceFix);
3264   verifyFormat("/* something */ namespace {\n"
3265                "class A {};\n"
3266                "void f() { f(); }\n"
3267                "}",
3268                LLVMWithNoNamespaceFix);
3269   verifyFormat("inline namespace X {\n"
3270                "class A {};\n"
3271                "void f() { f(); }\n"
3272                "}",
3273                LLVMWithNoNamespaceFix);
3274   verifyFormat("/* something */ inline namespace X {\n"
3275                "class A {};\n"
3276                "void f() { f(); }\n"
3277                "}",
3278                LLVMWithNoNamespaceFix);
3279   verifyFormat("export namespace X {\n"
3280                "class A {};\n"
3281                "void f() { f(); }\n"
3282                "}",
3283                LLVMWithNoNamespaceFix);
3284   verifyFormat("using namespace some_namespace;\n"
3285                "class A {};\n"
3286                "void f() { f(); }",
3287                LLVMWithNoNamespaceFix);
3288 
3289   // This code is more common than we thought; if we
3290   // layout this correctly the semicolon will go into
3291   // its own line, which is undesirable.
3292   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3293   verifyFormat("namespace {\n"
3294                "class A {};\n"
3295                "};",
3296                LLVMWithNoNamespaceFix);
3297 
3298   verifyFormat("namespace {\n"
3299                "int SomeVariable = 0; // comment\n"
3300                "} // namespace",
3301                LLVMWithNoNamespaceFix);
3302   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3303             "#define HEADER_GUARD\n"
3304             "namespace my_namespace {\n"
3305             "int i;\n"
3306             "} // my_namespace\n"
3307             "#endif // HEADER_GUARD",
3308             format("#ifndef HEADER_GUARD\n"
3309                    " #define HEADER_GUARD\n"
3310                    "   namespace my_namespace {\n"
3311                    "int i;\n"
3312                    "}    // my_namespace\n"
3313                    "#endif    // HEADER_GUARD",
3314                    LLVMWithNoNamespaceFix));
3315 
3316   EXPECT_EQ("namespace A::B {\n"
3317             "class C {};\n"
3318             "}",
3319             format("namespace A::B {\n"
3320                    "class C {};\n"
3321                    "}",
3322                    LLVMWithNoNamespaceFix));
3323 
3324   FormatStyle Style = getLLVMStyle();
3325   Style.NamespaceIndentation = FormatStyle::NI_All;
3326   EXPECT_EQ("namespace out {\n"
3327             "  int i;\n"
3328             "  namespace in {\n"
3329             "    int i;\n"
3330             "  } // namespace in\n"
3331             "} // namespace out",
3332             format("namespace out {\n"
3333                    "int i;\n"
3334                    "namespace in {\n"
3335                    "int i;\n"
3336                    "} // namespace in\n"
3337                    "} // namespace out",
3338                    Style));
3339 
3340   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3341   EXPECT_EQ("namespace out {\n"
3342             "int i;\n"
3343             "namespace in {\n"
3344             "  int i;\n"
3345             "} // namespace in\n"
3346             "} // namespace out",
3347             format("namespace out {\n"
3348                    "int i;\n"
3349                    "namespace in {\n"
3350                    "int i;\n"
3351                    "} // namespace in\n"
3352                    "} // namespace out",
3353                    Style));
3354 }
3355 
3356 TEST_F(FormatTest, NamespaceMacros) {
3357   FormatStyle Style = getLLVMStyle();
3358   Style.NamespaceMacros.push_back("TESTSUITE");
3359 
3360   verifyFormat("TESTSUITE(A) {\n"
3361                "int foo();\n"
3362                "} // TESTSUITE(A)",
3363                Style);
3364 
3365   verifyFormat("TESTSUITE(A, B) {\n"
3366                "int foo();\n"
3367                "} // TESTSUITE(A)",
3368                Style);
3369 
3370   // Properly indent according to NamespaceIndentation style
3371   Style.NamespaceIndentation = FormatStyle::NI_All;
3372   verifyFormat("TESTSUITE(A) {\n"
3373                "  int foo();\n"
3374                "} // TESTSUITE(A)",
3375                Style);
3376   verifyFormat("TESTSUITE(A) {\n"
3377                "  namespace B {\n"
3378                "    int foo();\n"
3379                "  } // namespace B\n"
3380                "} // TESTSUITE(A)",
3381                Style);
3382   verifyFormat("namespace A {\n"
3383                "  TESTSUITE(B) {\n"
3384                "    int foo();\n"
3385                "  } // TESTSUITE(B)\n"
3386                "} // namespace A",
3387                Style);
3388 
3389   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3390   verifyFormat("TESTSUITE(A) {\n"
3391                "TESTSUITE(B) {\n"
3392                "  int foo();\n"
3393                "} // TESTSUITE(B)\n"
3394                "} // TESTSUITE(A)",
3395                Style);
3396   verifyFormat("TESTSUITE(A) {\n"
3397                "namespace B {\n"
3398                "  int foo();\n"
3399                "} // namespace B\n"
3400                "} // TESTSUITE(A)",
3401                Style);
3402   verifyFormat("namespace A {\n"
3403                "TESTSUITE(B) {\n"
3404                "  int foo();\n"
3405                "} // TESTSUITE(B)\n"
3406                "} // namespace A",
3407                Style);
3408 
3409   // Properly merge namespace-macros blocks in CompactNamespaces mode
3410   Style.NamespaceIndentation = FormatStyle::NI_None;
3411   Style.CompactNamespaces = true;
3412   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3413                "}} // TESTSUITE(A::B)",
3414                Style);
3415 
3416   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3417             "}} // TESTSUITE(out::in)",
3418             format("TESTSUITE(out) {\n"
3419                    "TESTSUITE(in) {\n"
3420                    "} // TESTSUITE(in)\n"
3421                    "} // TESTSUITE(out)",
3422                    Style));
3423 
3424   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3425             "}} // TESTSUITE(out::in)",
3426             format("TESTSUITE(out) {\n"
3427                    "TESTSUITE(in) {\n"
3428                    "} // TESTSUITE(in)\n"
3429                    "} // TESTSUITE(out)",
3430                    Style));
3431 
3432   // Do not merge different namespaces/macros
3433   EXPECT_EQ("namespace out {\n"
3434             "TESTSUITE(in) {\n"
3435             "} // TESTSUITE(in)\n"
3436             "} // namespace out",
3437             format("namespace out {\n"
3438                    "TESTSUITE(in) {\n"
3439                    "} // TESTSUITE(in)\n"
3440                    "} // namespace out",
3441                    Style));
3442   EXPECT_EQ("TESTSUITE(out) {\n"
3443             "namespace in {\n"
3444             "} // namespace in\n"
3445             "} // TESTSUITE(out)",
3446             format("TESTSUITE(out) {\n"
3447                    "namespace in {\n"
3448                    "} // namespace in\n"
3449                    "} // TESTSUITE(out)",
3450                    Style));
3451   Style.NamespaceMacros.push_back("FOOBAR");
3452   EXPECT_EQ("TESTSUITE(out) {\n"
3453             "FOOBAR(in) {\n"
3454             "} // FOOBAR(in)\n"
3455             "} // TESTSUITE(out)",
3456             format("TESTSUITE(out) {\n"
3457                    "FOOBAR(in) {\n"
3458                    "} // FOOBAR(in)\n"
3459                    "} // TESTSUITE(out)",
3460                    Style));
3461 }
3462 
3463 TEST_F(FormatTest, FormatsCompactNamespaces) {
3464   FormatStyle Style = getLLVMStyle();
3465   Style.CompactNamespaces = true;
3466   Style.NamespaceMacros.push_back("TESTSUITE");
3467 
3468   verifyFormat("namespace A { namespace B {\n"
3469                "}} // namespace A::B",
3470                Style);
3471 
3472   EXPECT_EQ("namespace out { namespace in {\n"
3473             "}} // namespace out::in",
3474             format("namespace out {\n"
3475                    "namespace in {\n"
3476                    "} // namespace in\n"
3477                    "} // namespace out",
3478                    Style));
3479 
3480   // Only namespaces which have both consecutive opening and end get compacted
3481   EXPECT_EQ("namespace out {\n"
3482             "namespace in1 {\n"
3483             "} // namespace in1\n"
3484             "namespace in2 {\n"
3485             "} // namespace in2\n"
3486             "} // namespace out",
3487             format("namespace out {\n"
3488                    "namespace in1 {\n"
3489                    "} // namespace in1\n"
3490                    "namespace in2 {\n"
3491                    "} // namespace in2\n"
3492                    "} // namespace out",
3493                    Style));
3494 
3495   EXPECT_EQ("namespace out {\n"
3496             "int i;\n"
3497             "namespace in {\n"
3498             "int j;\n"
3499             "} // namespace in\n"
3500             "int k;\n"
3501             "} // namespace out",
3502             format("namespace out { int i;\n"
3503                    "namespace in { int j; } // namespace in\n"
3504                    "int k; } // namespace out",
3505                    Style));
3506 
3507   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3508             "}}} // namespace A::B::C\n",
3509             format("namespace A { namespace B {\n"
3510                    "namespace C {\n"
3511                    "}} // namespace B::C\n"
3512                    "} // namespace A\n",
3513                    Style));
3514 
3515   Style.ColumnLimit = 40;
3516   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3517             "namespace bbbbbbbbbb {\n"
3518             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3519             format("namespace aaaaaaaaaa {\n"
3520                    "namespace bbbbbbbbbb {\n"
3521                    "} // namespace bbbbbbbbbb\n"
3522                    "} // namespace aaaaaaaaaa",
3523                    Style));
3524 
3525   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3526             "namespace cccccc {\n"
3527             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3528             format("namespace aaaaaa {\n"
3529                    "namespace bbbbbb {\n"
3530                    "namespace cccccc {\n"
3531                    "} // namespace cccccc\n"
3532                    "} // namespace bbbbbb\n"
3533                    "} // namespace aaaaaa",
3534                    Style));
3535   Style.ColumnLimit = 80;
3536 
3537   // Extra semicolon after 'inner' closing brace prevents merging
3538   EXPECT_EQ("namespace out { namespace in {\n"
3539             "}; } // namespace out::in",
3540             format("namespace out {\n"
3541                    "namespace in {\n"
3542                    "}; // namespace in\n"
3543                    "} // namespace out",
3544                    Style));
3545 
3546   // Extra semicolon after 'outer' closing brace is conserved
3547   EXPECT_EQ("namespace out { namespace in {\n"
3548             "}}; // namespace out::in",
3549             format("namespace out {\n"
3550                    "namespace in {\n"
3551                    "} // namespace in\n"
3552                    "}; // namespace out",
3553                    Style));
3554 
3555   Style.NamespaceIndentation = FormatStyle::NI_All;
3556   EXPECT_EQ("namespace out { namespace in {\n"
3557             "  int i;\n"
3558             "}} // namespace out::in",
3559             format("namespace out {\n"
3560                    "namespace in {\n"
3561                    "int i;\n"
3562                    "} // namespace in\n"
3563                    "} // namespace out",
3564                    Style));
3565   EXPECT_EQ("namespace out { namespace mid {\n"
3566             "  namespace in {\n"
3567             "    int j;\n"
3568             "  } // namespace in\n"
3569             "  int k;\n"
3570             "}} // namespace out::mid",
3571             format("namespace out { namespace mid {\n"
3572                    "namespace in { int j; } // namespace in\n"
3573                    "int k; }} // namespace out::mid",
3574                    Style));
3575 
3576   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3577   EXPECT_EQ("namespace out { namespace in {\n"
3578             "  int i;\n"
3579             "}} // namespace out::in",
3580             format("namespace out {\n"
3581                    "namespace in {\n"
3582                    "int i;\n"
3583                    "} // namespace in\n"
3584                    "} // namespace out",
3585                    Style));
3586   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
3587             "  int i;\n"
3588             "}}} // namespace out::mid::in",
3589             format("namespace out {\n"
3590                    "namespace mid {\n"
3591                    "namespace in {\n"
3592                    "int i;\n"
3593                    "} // namespace in\n"
3594                    "} // namespace mid\n"
3595                    "} // namespace out",
3596                    Style));
3597 }
3598 
3599 TEST_F(FormatTest, FormatsExternC) {
3600   verifyFormat("extern \"C\" {\nint a;");
3601   verifyFormat("extern \"C\" {}");
3602   verifyFormat("extern \"C\" {\n"
3603                "int foo();\n"
3604                "}");
3605   verifyFormat("extern \"C\" int foo() {}");
3606   verifyFormat("extern \"C\" int foo();");
3607   verifyFormat("extern \"C\" int foo() {\n"
3608                "  int i = 42;\n"
3609                "  return i;\n"
3610                "}");
3611 
3612   FormatStyle Style = getLLVMStyle();
3613   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3614   Style.BraceWrapping.AfterFunction = true;
3615   verifyFormat("extern \"C\" int foo() {}", Style);
3616   verifyFormat("extern \"C\" int foo();", Style);
3617   verifyFormat("extern \"C\" int foo()\n"
3618                "{\n"
3619                "  int i = 42;\n"
3620                "  return i;\n"
3621                "}",
3622                Style);
3623 
3624   Style.BraceWrapping.AfterExternBlock = true;
3625   Style.BraceWrapping.SplitEmptyRecord = false;
3626   verifyFormat("extern \"C\"\n"
3627                "{}",
3628                Style);
3629   verifyFormat("extern \"C\"\n"
3630                "{\n"
3631                "  int foo();\n"
3632                "}",
3633                Style);
3634 }
3635 
3636 TEST_F(FormatTest, IndentExternBlockStyle) {
3637   FormatStyle Style = getLLVMStyle();
3638   Style.IndentWidth = 2;
3639 
3640   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3641   verifyFormat("extern \"C\" { /*9*/\n}", Style);
3642   verifyFormat("extern \"C\" {\n"
3643                "  int foo10();\n"
3644                "}",
3645                Style);
3646 
3647   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
3648   verifyFormat("extern \"C\" { /*11*/\n}", Style);
3649   verifyFormat("extern \"C\" {\n"
3650                "int foo12();\n"
3651                "}",
3652                Style);
3653 
3654   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3655   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3656   Style.BraceWrapping.AfterExternBlock = true;
3657   verifyFormat("extern \"C\"\n{ /*13*/\n}", Style);
3658   verifyFormat("extern \"C\"\n{\n"
3659                "  int foo14();\n"
3660                "}",
3661                Style);
3662 
3663   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3664   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3665   Style.BraceWrapping.AfterExternBlock = false;
3666   verifyFormat("extern \"C\" { /*15*/\n}", Style);
3667   verifyFormat("extern \"C\" {\n"
3668                "int foo16();\n"
3669                "}",
3670                Style);
3671 }
3672 
3673 TEST_F(FormatTest, FormatsInlineASM) {
3674   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
3675   verifyFormat("asm(\"nop\" ::: \"memory\");");
3676   verifyFormat(
3677       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
3678       "    \"cpuid\\n\\t\"\n"
3679       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
3680       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
3681       "    : \"a\"(value));");
3682   EXPECT_EQ(
3683       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
3684       "  __asm {\n"
3685       "        mov     edx,[that] // vtable in edx\n"
3686       "        mov     eax,methodIndex\n"
3687       "        call    [edx][eax*4] // stdcall\n"
3688       "  }\n"
3689       "}",
3690       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
3691              "    __asm {\n"
3692              "        mov     edx,[that] // vtable in edx\n"
3693              "        mov     eax,methodIndex\n"
3694              "        call    [edx][eax*4] // stdcall\n"
3695              "    }\n"
3696              "}"));
3697   EXPECT_EQ("_asm {\n"
3698             "  xor eax, eax;\n"
3699             "  cpuid;\n"
3700             "}",
3701             format("_asm {\n"
3702                    "  xor eax, eax;\n"
3703                    "  cpuid;\n"
3704                    "}"));
3705   verifyFormat("void function() {\n"
3706                "  // comment\n"
3707                "  asm(\"\");\n"
3708                "}");
3709   EXPECT_EQ("__asm {\n"
3710             "}\n"
3711             "int i;",
3712             format("__asm   {\n"
3713                    "}\n"
3714                    "int   i;"));
3715 }
3716 
3717 TEST_F(FormatTest, FormatTryCatch) {
3718   verifyFormat("try {\n"
3719                "  throw a * b;\n"
3720                "} catch (int a) {\n"
3721                "  // Do nothing.\n"
3722                "} catch (...) {\n"
3723                "  exit(42);\n"
3724                "}");
3725 
3726   // Function-level try statements.
3727   verifyFormat("int f() try { return 4; } catch (...) {\n"
3728                "  return 5;\n"
3729                "}");
3730   verifyFormat("class A {\n"
3731                "  int a;\n"
3732                "  A() try : a(0) {\n"
3733                "  } catch (...) {\n"
3734                "    throw;\n"
3735                "  }\n"
3736                "};\n");
3737   verifyFormat("class A {\n"
3738                "  int a;\n"
3739                "  A() try : a(0), b{1} {\n"
3740                "  } catch (...) {\n"
3741                "    throw;\n"
3742                "  }\n"
3743                "};\n");
3744   verifyFormat("class A {\n"
3745                "  int a;\n"
3746                "  A() try : a(0), b{1}, c{2} {\n"
3747                "  } catch (...) {\n"
3748                "    throw;\n"
3749                "  }\n"
3750                "};\n");
3751   verifyFormat("class A {\n"
3752                "  int a;\n"
3753                "  A() try : a(0), b{1}, c{2} {\n"
3754                "    { // New scope.\n"
3755                "    }\n"
3756                "  } catch (...) {\n"
3757                "    throw;\n"
3758                "  }\n"
3759                "};\n");
3760 
3761   // Incomplete try-catch blocks.
3762   verifyIncompleteFormat("try {} catch (");
3763 }
3764 
3765 TEST_F(FormatTest, FormatTryAsAVariable) {
3766   verifyFormat("int try;");
3767   verifyFormat("int try, size;");
3768   verifyFormat("try = foo();");
3769   verifyFormat("if (try < size) {\n  return true;\n}");
3770 
3771   verifyFormat("int catch;");
3772   verifyFormat("int catch, size;");
3773   verifyFormat("catch = foo();");
3774   verifyFormat("if (catch < size) {\n  return true;\n}");
3775 
3776   FormatStyle Style = getLLVMStyle();
3777   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3778   Style.BraceWrapping.AfterFunction = true;
3779   Style.BraceWrapping.BeforeCatch = true;
3780   verifyFormat("try {\n"
3781                "  int bar = 1;\n"
3782                "}\n"
3783                "catch (...) {\n"
3784                "  int bar = 1;\n"
3785                "}",
3786                Style);
3787   verifyFormat("#if NO_EX\n"
3788                "try\n"
3789                "#endif\n"
3790                "{\n"
3791                "}\n"
3792                "#if NO_EX\n"
3793                "catch (...) {\n"
3794                "}",
3795                Style);
3796   verifyFormat("try /* abc */ {\n"
3797                "  int bar = 1;\n"
3798                "}\n"
3799                "catch (...) {\n"
3800                "  int bar = 1;\n"
3801                "}",
3802                Style);
3803   verifyFormat("try\n"
3804                "// abc\n"
3805                "{\n"
3806                "  int bar = 1;\n"
3807                "}\n"
3808                "catch (...) {\n"
3809                "  int bar = 1;\n"
3810                "}",
3811                Style);
3812 }
3813 
3814 TEST_F(FormatTest, FormatSEHTryCatch) {
3815   verifyFormat("__try {\n"
3816                "  int a = b * c;\n"
3817                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
3818                "  // Do nothing.\n"
3819                "}");
3820 
3821   verifyFormat("__try {\n"
3822                "  int a = b * c;\n"
3823                "} __finally {\n"
3824                "  // Do nothing.\n"
3825                "}");
3826 
3827   verifyFormat("DEBUG({\n"
3828                "  __try {\n"
3829                "  } __finally {\n"
3830                "  }\n"
3831                "});\n");
3832 }
3833 
3834 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
3835   verifyFormat("try {\n"
3836                "  f();\n"
3837                "} catch {\n"
3838                "  g();\n"
3839                "}");
3840   verifyFormat("try {\n"
3841                "  f();\n"
3842                "} catch (A a) MACRO(x) {\n"
3843                "  g();\n"
3844                "} catch (B b) MACRO(x) {\n"
3845                "  g();\n"
3846                "}");
3847 }
3848 
3849 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
3850   FormatStyle Style = getLLVMStyle();
3851   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
3852                           FormatStyle::BS_WebKit}) {
3853     Style.BreakBeforeBraces = BraceStyle;
3854     verifyFormat("try {\n"
3855                  "  // something\n"
3856                  "} catch (...) {\n"
3857                  "  // something\n"
3858                  "}",
3859                  Style);
3860   }
3861   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
3862   verifyFormat("try {\n"
3863                "  // something\n"
3864                "}\n"
3865                "catch (...) {\n"
3866                "  // something\n"
3867                "}",
3868                Style);
3869   verifyFormat("__try {\n"
3870                "  // something\n"
3871                "}\n"
3872                "__finally {\n"
3873                "  // something\n"
3874                "}",
3875                Style);
3876   verifyFormat("@try {\n"
3877                "  // something\n"
3878                "}\n"
3879                "@finally {\n"
3880                "  // something\n"
3881                "}",
3882                Style);
3883   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3884   verifyFormat("try\n"
3885                "{\n"
3886                "  // something\n"
3887                "}\n"
3888                "catch (...)\n"
3889                "{\n"
3890                "  // something\n"
3891                "}",
3892                Style);
3893   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
3894   verifyFormat("try\n"
3895                "  {\n"
3896                "  // something white\n"
3897                "  }\n"
3898                "catch (...)\n"
3899                "  {\n"
3900                "  // something white\n"
3901                "  }",
3902                Style);
3903   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
3904   verifyFormat("try\n"
3905                "  {\n"
3906                "    // something\n"
3907                "  }\n"
3908                "catch (...)\n"
3909                "  {\n"
3910                "    // something\n"
3911                "  }",
3912                Style);
3913   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3914   Style.BraceWrapping.BeforeCatch = true;
3915   verifyFormat("try {\n"
3916                "  // something\n"
3917                "}\n"
3918                "catch (...) {\n"
3919                "  // something\n"
3920                "}",
3921                Style);
3922 }
3923 
3924 TEST_F(FormatTest, StaticInitializers) {
3925   verifyFormat("static SomeClass SC = {1, 'a'};");
3926 
3927   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
3928                "    100000000, "
3929                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
3930 
3931   // Here, everything other than the "}" would fit on a line.
3932   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
3933                "    10000000000000000000000000};");
3934   EXPECT_EQ("S s = {a,\n"
3935             "\n"
3936             "       b};",
3937             format("S s = {\n"
3938                    "  a,\n"
3939                    "\n"
3940                    "  b\n"
3941                    "};"));
3942 
3943   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
3944   // line. However, the formatting looks a bit off and this probably doesn't
3945   // happen often in practice.
3946   verifyFormat("static int Variable[1] = {\n"
3947                "    {1000000000000000000000000000000000000}};",
3948                getLLVMStyleWithColumns(40));
3949 }
3950 
3951 TEST_F(FormatTest, DesignatedInitializers) {
3952   verifyFormat("const struct A a = {.a = 1, .b = 2};");
3953   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
3954                "                    .bbbbbbbbbb = 2,\n"
3955                "                    .cccccccccc = 3,\n"
3956                "                    .dddddddddd = 4,\n"
3957                "                    .eeeeeeeeee = 5};");
3958   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
3959                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
3960                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
3961                "    .ccccccccccccccccccccccccccc = 3,\n"
3962                "    .ddddddddddddddddddddddddddd = 4,\n"
3963                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
3964 
3965   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
3966 
3967   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
3968   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
3969                "                    [2] = bbbbbbbbbb,\n"
3970                "                    [3] = cccccccccc,\n"
3971                "                    [4] = dddddddddd,\n"
3972                "                    [5] = eeeeeeeeee};");
3973   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
3974                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3975                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3976                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
3977                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
3978                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
3979 }
3980 
3981 TEST_F(FormatTest, NestedStaticInitializers) {
3982   verifyFormat("static A x = {{{}}};\n");
3983   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
3984                "               {init1, init2, init3, init4}}};",
3985                getLLVMStyleWithColumns(50));
3986 
3987   verifyFormat("somes Status::global_reps[3] = {\n"
3988                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
3989                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
3990                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
3991                getLLVMStyleWithColumns(60));
3992   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
3993                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
3994                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
3995                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
3996   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
3997                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
3998                "rect.fTop}};");
3999 
4000   verifyFormat(
4001       "SomeArrayOfSomeType a = {\n"
4002       "    {{1, 2, 3},\n"
4003       "     {1, 2, 3},\n"
4004       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4005       "      333333333333333333333333333333},\n"
4006       "     {1, 2, 3},\n"
4007       "     {1, 2, 3}}};");
4008   verifyFormat(
4009       "SomeArrayOfSomeType a = {\n"
4010       "    {{1, 2, 3}},\n"
4011       "    {{1, 2, 3}},\n"
4012       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4013       "      333333333333333333333333333333}},\n"
4014       "    {{1, 2, 3}},\n"
4015       "    {{1, 2, 3}}};");
4016 
4017   verifyFormat("struct {\n"
4018                "  unsigned bit;\n"
4019                "  const char *const name;\n"
4020                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4021                "                 {kOsWin, \"Windows\"},\n"
4022                "                 {kOsLinux, \"Linux\"},\n"
4023                "                 {kOsCrOS, \"Chrome OS\"}};");
4024   verifyFormat("struct {\n"
4025                "  unsigned bit;\n"
4026                "  const char *const name;\n"
4027                "} kBitsToOs[] = {\n"
4028                "    {kOsMac, \"Mac\"},\n"
4029                "    {kOsWin, \"Windows\"},\n"
4030                "    {kOsLinux, \"Linux\"},\n"
4031                "    {kOsCrOS, \"Chrome OS\"},\n"
4032                "};");
4033 }
4034 
4035 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4036   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4037                "                      \\\n"
4038                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4039 }
4040 
4041 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4042   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4043                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4044 
4045   // Do break defaulted and deleted functions.
4046   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4047                "    default;",
4048                getLLVMStyleWithColumns(40));
4049   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4050                "    delete;",
4051                getLLVMStyleWithColumns(40));
4052 }
4053 
4054 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4055   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4056                getLLVMStyleWithColumns(40));
4057   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4058                getLLVMStyleWithColumns(40));
4059   EXPECT_EQ("#define Q                              \\\n"
4060             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4061             "  \"aaaaaaaa.cpp\"",
4062             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4063                    getLLVMStyleWithColumns(40)));
4064 }
4065 
4066 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4067   EXPECT_EQ("# 123 \"A string literal\"",
4068             format("   #     123    \"A string literal\""));
4069 }
4070 
4071 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4072   EXPECT_EQ("#;", format("#;"));
4073   verifyFormat("#\n;\n;\n;");
4074 }
4075 
4076 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4077   EXPECT_EQ("#line 42 \"test\"\n",
4078             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4079   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4080                                     getLLVMStyleWithColumns(12)));
4081 }
4082 
4083 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4084   EXPECT_EQ("#line 42 \"test\"",
4085             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4086   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4087 }
4088 
4089 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4090   verifyFormat("#define A \\x20");
4091   verifyFormat("#define A \\ x20");
4092   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4093   verifyFormat("#define A ''");
4094   verifyFormat("#define A ''qqq");
4095   verifyFormat("#define A `qqq");
4096   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4097   EXPECT_EQ("const char *c = STRINGIFY(\n"
4098             "\\na : b);",
4099             format("const char * c = STRINGIFY(\n"
4100                    "\\na : b);"));
4101 
4102   verifyFormat("a\r\\");
4103   verifyFormat("a\v\\");
4104   verifyFormat("a\f\\");
4105 }
4106 
4107 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4108   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4109   style.IndentWidth = 4;
4110   style.PPIndentWidth = 1;
4111 
4112   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4113   verifyFormat("#ifdef __linux__\n"
4114                "void foo() {\n"
4115                "    int x = 0;\n"
4116                "}\n"
4117                "#define FOO\n"
4118                "#endif\n"
4119                "void bar() {\n"
4120                "    int y = 0;\n"
4121                "}\n",
4122                style);
4123 
4124   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4125   verifyFormat("#ifdef __linux__\n"
4126                "void foo() {\n"
4127                "    int x = 0;\n"
4128                "}\n"
4129                "# define FOO foo\n"
4130                "#endif\n"
4131                "void bar() {\n"
4132                "    int y = 0;\n"
4133                "}\n",
4134                style);
4135 
4136   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4137   verifyFormat("#ifdef __linux__\n"
4138                "void foo() {\n"
4139                "    int x = 0;\n"
4140                "}\n"
4141                " #define FOO foo\n"
4142                "#endif\n"
4143                "void bar() {\n"
4144                "    int y = 0;\n"
4145                "}\n",
4146                style);
4147 }
4148 
4149 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4150   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4151   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4152   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4153   // FIXME: We never break before the macro name.
4154   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4155 
4156   verifyFormat("#define A A\n#define A A");
4157   verifyFormat("#define A(X) A\n#define A A");
4158 
4159   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4160   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4161 }
4162 
4163 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4164   EXPECT_EQ("// somecomment\n"
4165             "#include \"a.h\"\n"
4166             "#define A(  \\\n"
4167             "    A, B)\n"
4168             "#include \"b.h\"\n"
4169             "// somecomment\n",
4170             format("  // somecomment\n"
4171                    "  #include \"a.h\"\n"
4172                    "#define A(A,\\\n"
4173                    "    B)\n"
4174                    "    #include \"b.h\"\n"
4175                    " // somecomment\n",
4176                    getLLVMStyleWithColumns(13)));
4177 }
4178 
4179 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4180 
4181 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4182   EXPECT_EQ("#define A    \\\n"
4183             "  c;         \\\n"
4184             "  e;\n"
4185             "f;",
4186             format("#define A c; e;\n"
4187                    "f;",
4188                    getLLVMStyleWithColumns(14)));
4189 }
4190 
4191 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4192 
4193 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4194   EXPECT_EQ("int x,\n"
4195             "#define A\n"
4196             "    y;",
4197             format("int x,\n#define A\ny;"));
4198 }
4199 
4200 TEST_F(FormatTest, HashInMacroDefinition) {
4201   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4202   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4203   verifyFormat("#define A  \\\n"
4204                "  {        \\\n"
4205                "    f(#c); \\\n"
4206                "  }",
4207                getLLVMStyleWithColumns(11));
4208 
4209   verifyFormat("#define A(X)         \\\n"
4210                "  void function##X()",
4211                getLLVMStyleWithColumns(22));
4212 
4213   verifyFormat("#define A(a, b, c)   \\\n"
4214                "  void a##b##c()",
4215                getLLVMStyleWithColumns(22));
4216 
4217   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4218 }
4219 
4220 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4221   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4222   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4223 
4224   FormatStyle Style = getLLVMStyle();
4225   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4226   verifyFormat("#define true ((foo)1)", Style);
4227   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4228   verifyFormat("#define false((foo)0)", Style);
4229 }
4230 
4231 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4232   EXPECT_EQ("#define A b;", format("#define A \\\n"
4233                                    "          \\\n"
4234                                    "  b;",
4235                                    getLLVMStyleWithColumns(25)));
4236   EXPECT_EQ("#define A \\\n"
4237             "          \\\n"
4238             "  a;      \\\n"
4239             "  b;",
4240             format("#define A \\\n"
4241                    "          \\\n"
4242                    "  a;      \\\n"
4243                    "  b;",
4244                    getLLVMStyleWithColumns(11)));
4245   EXPECT_EQ("#define A \\\n"
4246             "  a;      \\\n"
4247             "          \\\n"
4248             "  b;",
4249             format("#define A \\\n"
4250                    "  a;      \\\n"
4251                    "          \\\n"
4252                    "  b;",
4253                    getLLVMStyleWithColumns(11)));
4254 }
4255 
4256 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4257   verifyIncompleteFormat("#define A :");
4258   verifyFormat("#define SOMECASES  \\\n"
4259                "  case 1:          \\\n"
4260                "  case 2\n",
4261                getLLVMStyleWithColumns(20));
4262   verifyFormat("#define MACRO(a) \\\n"
4263                "  if (a)         \\\n"
4264                "    f();         \\\n"
4265                "  else           \\\n"
4266                "    g()",
4267                getLLVMStyleWithColumns(18));
4268   verifyFormat("#define A template <typename T>");
4269   verifyIncompleteFormat("#define STR(x) #x\n"
4270                          "f(STR(this_is_a_string_literal{));");
4271   verifyFormat("#pragma omp threadprivate( \\\n"
4272                "    y)), // expected-warning",
4273                getLLVMStyleWithColumns(28));
4274   verifyFormat("#d, = };");
4275   verifyFormat("#if \"a");
4276   verifyIncompleteFormat("({\n"
4277                          "#define b     \\\n"
4278                          "  }           \\\n"
4279                          "  a\n"
4280                          "a",
4281                          getLLVMStyleWithColumns(15));
4282   verifyFormat("#define A     \\\n"
4283                "  {           \\\n"
4284                "    {\n"
4285                "#define B     \\\n"
4286                "  }           \\\n"
4287                "  }",
4288                getLLVMStyleWithColumns(15));
4289   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4290   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4291   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4292   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4293 }
4294 
4295 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4296   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4297   EXPECT_EQ("class A : public QObject {\n"
4298             "  Q_OBJECT\n"
4299             "\n"
4300             "  A() {}\n"
4301             "};",
4302             format("class A  :  public QObject {\n"
4303                    "     Q_OBJECT\n"
4304                    "\n"
4305                    "  A() {\n}\n"
4306                    "}  ;"));
4307   EXPECT_EQ("MACRO\n"
4308             "/*static*/ int i;",
4309             format("MACRO\n"
4310                    " /*static*/ int   i;"));
4311   EXPECT_EQ("SOME_MACRO\n"
4312             "namespace {\n"
4313             "void f();\n"
4314             "} // namespace",
4315             format("SOME_MACRO\n"
4316                    "  namespace    {\n"
4317                    "void   f(  );\n"
4318                    "} // namespace"));
4319   // Only if the identifier contains at least 5 characters.
4320   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4321   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4322   // Only if everything is upper case.
4323   EXPECT_EQ("class A : public QObject {\n"
4324             "  Q_Object A() {}\n"
4325             "};",
4326             format("class A  :  public QObject {\n"
4327                    "     Q_Object\n"
4328                    "  A() {\n}\n"
4329                    "}  ;"));
4330 
4331   // Only if the next line can actually start an unwrapped line.
4332   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4333             format("SOME_WEIRD_LOG_MACRO\n"
4334                    "<< SomeThing;"));
4335 
4336   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4337                "(n, buffers))\n",
4338                getChromiumStyle(FormatStyle::LK_Cpp));
4339 
4340   // See PR41483
4341   EXPECT_EQ("/**/ FOO(a)\n"
4342             "FOO(b)",
4343             format("/**/ FOO(a)\n"
4344                    "FOO(b)"));
4345 }
4346 
4347 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4348   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4349             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4350             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4351             "class X {};\n"
4352             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4353             "int *createScopDetectionPass() { return 0; }",
4354             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4355                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4356                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4357                    "  class X {};\n"
4358                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4359                    "  int *createScopDetectionPass() { return 0; }"));
4360   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4361   // braces, so that inner block is indented one level more.
4362   EXPECT_EQ("int q() {\n"
4363             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4364             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4365             "  IPC_END_MESSAGE_MAP()\n"
4366             "}",
4367             format("int q() {\n"
4368                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4369                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4370                    "  IPC_END_MESSAGE_MAP()\n"
4371                    "}"));
4372 
4373   // Same inside macros.
4374   EXPECT_EQ("#define LIST(L) \\\n"
4375             "  L(A)          \\\n"
4376             "  L(B)          \\\n"
4377             "  L(C)",
4378             format("#define LIST(L) \\\n"
4379                    "  L(A) \\\n"
4380                    "  L(B) \\\n"
4381                    "  L(C)",
4382                    getGoogleStyle()));
4383 
4384   // These must not be recognized as macros.
4385   EXPECT_EQ("int q() {\n"
4386             "  f(x);\n"
4387             "  f(x) {}\n"
4388             "  f(x)->g();\n"
4389             "  f(x)->*g();\n"
4390             "  f(x).g();\n"
4391             "  f(x) = x;\n"
4392             "  f(x) += x;\n"
4393             "  f(x) -= x;\n"
4394             "  f(x) *= x;\n"
4395             "  f(x) /= x;\n"
4396             "  f(x) %= x;\n"
4397             "  f(x) &= x;\n"
4398             "  f(x) |= x;\n"
4399             "  f(x) ^= x;\n"
4400             "  f(x) >>= x;\n"
4401             "  f(x) <<= x;\n"
4402             "  f(x)[y].z();\n"
4403             "  LOG(INFO) << x;\n"
4404             "  ifstream(x) >> x;\n"
4405             "}\n",
4406             format("int q() {\n"
4407                    "  f(x)\n;\n"
4408                    "  f(x)\n {}\n"
4409                    "  f(x)\n->g();\n"
4410                    "  f(x)\n->*g();\n"
4411                    "  f(x)\n.g();\n"
4412                    "  f(x)\n = x;\n"
4413                    "  f(x)\n += x;\n"
4414                    "  f(x)\n -= x;\n"
4415                    "  f(x)\n *= x;\n"
4416                    "  f(x)\n /= x;\n"
4417                    "  f(x)\n %= x;\n"
4418                    "  f(x)\n &= x;\n"
4419                    "  f(x)\n |= x;\n"
4420                    "  f(x)\n ^= x;\n"
4421                    "  f(x)\n >>= x;\n"
4422                    "  f(x)\n <<= x;\n"
4423                    "  f(x)\n[y].z();\n"
4424                    "  LOG(INFO)\n << x;\n"
4425                    "  ifstream(x)\n >> x;\n"
4426                    "}\n"));
4427   EXPECT_EQ("int q() {\n"
4428             "  F(x)\n"
4429             "  if (1) {\n"
4430             "  }\n"
4431             "  F(x)\n"
4432             "  while (1) {\n"
4433             "  }\n"
4434             "  F(x)\n"
4435             "  G(x);\n"
4436             "  F(x)\n"
4437             "  try {\n"
4438             "    Q();\n"
4439             "  } catch (...) {\n"
4440             "  }\n"
4441             "}\n",
4442             format("int q() {\n"
4443                    "F(x)\n"
4444                    "if (1) {}\n"
4445                    "F(x)\n"
4446                    "while (1) {}\n"
4447                    "F(x)\n"
4448                    "G(x);\n"
4449                    "F(x)\n"
4450                    "try { Q(); } catch (...) {}\n"
4451                    "}\n"));
4452   EXPECT_EQ("class A {\n"
4453             "  A() : t(0) {}\n"
4454             "  A(int i) noexcept() : {}\n"
4455             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4456             "  try : t(0) {\n"
4457             "  } catch (...) {\n"
4458             "  }\n"
4459             "};",
4460             format("class A {\n"
4461                    "  A()\n : t(0) {}\n"
4462                    "  A(int i)\n noexcept() : {}\n"
4463                    "  A(X x)\n"
4464                    "  try : t(0) {} catch (...) {}\n"
4465                    "};"));
4466   FormatStyle Style = getLLVMStyle();
4467   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4468   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4469   Style.BraceWrapping.AfterFunction = true;
4470   EXPECT_EQ("void f()\n"
4471             "try\n"
4472             "{\n"
4473             "}",
4474             format("void f() try {\n"
4475                    "}",
4476                    Style));
4477   EXPECT_EQ("class SomeClass {\n"
4478             "public:\n"
4479             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4480             "};",
4481             format("class SomeClass {\n"
4482                    "public:\n"
4483                    "  SomeClass()\n"
4484                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4485                    "};"));
4486   EXPECT_EQ("class SomeClass {\n"
4487             "public:\n"
4488             "  SomeClass()\n"
4489             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4490             "};",
4491             format("class SomeClass {\n"
4492                    "public:\n"
4493                    "  SomeClass()\n"
4494                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4495                    "};",
4496                    getLLVMStyleWithColumns(40)));
4497 
4498   verifyFormat("MACRO(>)");
4499 
4500   // Some macros contain an implicit semicolon.
4501   Style = getLLVMStyle();
4502   Style.StatementMacros.push_back("FOO");
4503   verifyFormat("FOO(a) int b = 0;");
4504   verifyFormat("FOO(a)\n"
4505                "int b = 0;",
4506                Style);
4507   verifyFormat("FOO(a);\n"
4508                "int b = 0;",
4509                Style);
4510   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
4511                "int b = 0;",
4512                Style);
4513   verifyFormat("FOO()\n"
4514                "int b = 0;",
4515                Style);
4516   verifyFormat("FOO\n"
4517                "int b = 0;",
4518                Style);
4519   verifyFormat("void f() {\n"
4520                "  FOO(a)\n"
4521                "  return a;\n"
4522                "}",
4523                Style);
4524   verifyFormat("FOO(a)\n"
4525                "FOO(b)",
4526                Style);
4527   verifyFormat("int a = 0;\n"
4528                "FOO(b)\n"
4529                "int c = 0;",
4530                Style);
4531   verifyFormat("int a = 0;\n"
4532                "int x = FOO(a)\n"
4533                "int b = 0;",
4534                Style);
4535   verifyFormat("void foo(int a) { FOO(a) }\n"
4536                "uint32_t bar() {}",
4537                Style);
4538 }
4539 
4540 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
4541   verifyFormat("#define A \\\n"
4542                "  f({     \\\n"
4543                "    g();  \\\n"
4544                "  });",
4545                getLLVMStyleWithColumns(11));
4546 }
4547 
4548 TEST_F(FormatTest, IndentPreprocessorDirectives) {
4549   FormatStyle Style = getLLVMStyle();
4550   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
4551   Style.ColumnLimit = 40;
4552   verifyFormat("#ifdef _WIN32\n"
4553                "#define A 0\n"
4554                "#ifdef VAR2\n"
4555                "#define B 1\n"
4556                "#include <someheader.h>\n"
4557                "#define MACRO                          \\\n"
4558                "  some_very_long_func_aaaaaaaaaa();\n"
4559                "#endif\n"
4560                "#else\n"
4561                "#define A 1\n"
4562                "#endif",
4563                Style);
4564   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4565   verifyFormat("#ifdef _WIN32\n"
4566                "#  define A 0\n"
4567                "#  ifdef VAR2\n"
4568                "#    define B 1\n"
4569                "#    include <someheader.h>\n"
4570                "#    define MACRO                      \\\n"
4571                "      some_very_long_func_aaaaaaaaaa();\n"
4572                "#  endif\n"
4573                "#else\n"
4574                "#  define A 1\n"
4575                "#endif",
4576                Style);
4577   verifyFormat("#if A\n"
4578                "#  define MACRO                        \\\n"
4579                "    void a(int x) {                    \\\n"
4580                "      b();                             \\\n"
4581                "      c();                             \\\n"
4582                "      d();                             \\\n"
4583                "      e();                             \\\n"
4584                "      f();                             \\\n"
4585                "    }\n"
4586                "#endif",
4587                Style);
4588   // Comments before include guard.
4589   verifyFormat("// file comment\n"
4590                "// file comment\n"
4591                "#ifndef HEADER_H\n"
4592                "#define HEADER_H\n"
4593                "code();\n"
4594                "#endif",
4595                Style);
4596   // Test with include guards.
4597   verifyFormat("#ifndef HEADER_H\n"
4598                "#define HEADER_H\n"
4599                "code();\n"
4600                "#endif",
4601                Style);
4602   // Include guards must have a #define with the same variable immediately
4603   // after #ifndef.
4604   verifyFormat("#ifndef NOT_GUARD\n"
4605                "#  define FOO\n"
4606                "code();\n"
4607                "#endif",
4608                Style);
4609 
4610   // Include guards must cover the entire file.
4611   verifyFormat("code();\n"
4612                "code();\n"
4613                "#ifndef NOT_GUARD\n"
4614                "#  define NOT_GUARD\n"
4615                "code();\n"
4616                "#endif",
4617                Style);
4618   verifyFormat("#ifndef NOT_GUARD\n"
4619                "#  define NOT_GUARD\n"
4620                "code();\n"
4621                "#endif\n"
4622                "code();",
4623                Style);
4624   // Test with trailing blank lines.
4625   verifyFormat("#ifndef HEADER_H\n"
4626                "#define HEADER_H\n"
4627                "code();\n"
4628                "#endif\n",
4629                Style);
4630   // Include guards don't have #else.
4631   verifyFormat("#ifndef NOT_GUARD\n"
4632                "#  define NOT_GUARD\n"
4633                "code();\n"
4634                "#else\n"
4635                "#endif",
4636                Style);
4637   verifyFormat("#ifndef NOT_GUARD\n"
4638                "#  define NOT_GUARD\n"
4639                "code();\n"
4640                "#elif FOO\n"
4641                "#endif",
4642                Style);
4643   // Non-identifier #define after potential include guard.
4644   verifyFormat("#ifndef FOO\n"
4645                "#  define 1\n"
4646                "#endif\n",
4647                Style);
4648   // #if closes past last non-preprocessor line.
4649   verifyFormat("#ifndef FOO\n"
4650                "#define FOO\n"
4651                "#if 1\n"
4652                "int i;\n"
4653                "#  define A 0\n"
4654                "#endif\n"
4655                "#endif\n",
4656                Style);
4657   // Don't crash if there is an #elif directive without a condition.
4658   verifyFormat("#if 1\n"
4659                "int x;\n"
4660                "#elif\n"
4661                "int y;\n"
4662                "#else\n"
4663                "int z;\n"
4664                "#endif",
4665                Style);
4666   // FIXME: This doesn't handle the case where there's code between the
4667   // #ifndef and #define but all other conditions hold. This is because when
4668   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
4669   // previous code line yet, so we can't detect it.
4670   EXPECT_EQ("#ifndef NOT_GUARD\n"
4671             "code();\n"
4672             "#define NOT_GUARD\n"
4673             "code();\n"
4674             "#endif",
4675             format("#ifndef NOT_GUARD\n"
4676                    "code();\n"
4677                    "#  define NOT_GUARD\n"
4678                    "code();\n"
4679                    "#endif",
4680                    Style));
4681   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
4682   // be outside an include guard. Examples are #pragma once and
4683   // #pragma GCC diagnostic, or anything else that does not change the meaning
4684   // of the file if it's included multiple times.
4685   EXPECT_EQ("#ifdef WIN32\n"
4686             "#  pragma once\n"
4687             "#endif\n"
4688             "#ifndef HEADER_H\n"
4689             "#  define HEADER_H\n"
4690             "code();\n"
4691             "#endif",
4692             format("#ifdef WIN32\n"
4693                    "#  pragma once\n"
4694                    "#endif\n"
4695                    "#ifndef HEADER_H\n"
4696                    "#define HEADER_H\n"
4697                    "code();\n"
4698                    "#endif",
4699                    Style));
4700   // FIXME: This does not detect when there is a single non-preprocessor line
4701   // in front of an include-guard-like structure where other conditions hold
4702   // because ScopedLineState hides the line.
4703   EXPECT_EQ("code();\n"
4704             "#ifndef HEADER_H\n"
4705             "#define HEADER_H\n"
4706             "code();\n"
4707             "#endif",
4708             format("code();\n"
4709                    "#ifndef HEADER_H\n"
4710                    "#  define HEADER_H\n"
4711                    "code();\n"
4712                    "#endif",
4713                    Style));
4714   // Keep comments aligned with #, otherwise indent comments normally. These
4715   // tests cannot use verifyFormat because messUp manipulates leading
4716   // whitespace.
4717   {
4718     const char *Expected = ""
4719                            "void f() {\n"
4720                            "#if 1\n"
4721                            "// Preprocessor aligned.\n"
4722                            "#  define A 0\n"
4723                            "  // Code. Separated by blank line.\n"
4724                            "\n"
4725                            "#  define B 0\n"
4726                            "  // Code. Not aligned with #\n"
4727                            "#  define C 0\n"
4728                            "#endif";
4729     const char *ToFormat = ""
4730                            "void f() {\n"
4731                            "#if 1\n"
4732                            "// Preprocessor aligned.\n"
4733                            "#  define A 0\n"
4734                            "// Code. Separated by blank line.\n"
4735                            "\n"
4736                            "#  define B 0\n"
4737                            "   // Code. Not aligned with #\n"
4738                            "#  define C 0\n"
4739                            "#endif";
4740     EXPECT_EQ(Expected, format(ToFormat, Style));
4741     EXPECT_EQ(Expected, format(Expected, Style));
4742   }
4743   // Keep block quotes aligned.
4744   {
4745     const char *Expected = ""
4746                            "void f() {\n"
4747                            "#if 1\n"
4748                            "/* Preprocessor aligned. */\n"
4749                            "#  define A 0\n"
4750                            "  /* Code. Separated by blank line. */\n"
4751                            "\n"
4752                            "#  define B 0\n"
4753                            "  /* Code. Not aligned with # */\n"
4754                            "#  define C 0\n"
4755                            "#endif";
4756     const char *ToFormat = ""
4757                            "void f() {\n"
4758                            "#if 1\n"
4759                            "/* Preprocessor aligned. */\n"
4760                            "#  define A 0\n"
4761                            "/* Code. Separated by blank line. */\n"
4762                            "\n"
4763                            "#  define B 0\n"
4764                            "   /* Code. Not aligned with # */\n"
4765                            "#  define C 0\n"
4766                            "#endif";
4767     EXPECT_EQ(Expected, format(ToFormat, Style));
4768     EXPECT_EQ(Expected, format(Expected, Style));
4769   }
4770   // Keep comments aligned with un-indented directives.
4771   {
4772     const char *Expected = ""
4773                            "void f() {\n"
4774                            "// Preprocessor aligned.\n"
4775                            "#define A 0\n"
4776                            "  // Code. Separated by blank line.\n"
4777                            "\n"
4778                            "#define B 0\n"
4779                            "  // Code. Not aligned with #\n"
4780                            "#define C 0\n";
4781     const char *ToFormat = ""
4782                            "void f() {\n"
4783                            "// Preprocessor aligned.\n"
4784                            "#define A 0\n"
4785                            "// Code. Separated by blank line.\n"
4786                            "\n"
4787                            "#define B 0\n"
4788                            "   // Code. Not aligned with #\n"
4789                            "#define C 0\n";
4790     EXPECT_EQ(Expected, format(ToFormat, Style));
4791     EXPECT_EQ(Expected, format(Expected, Style));
4792   }
4793   // Test AfterHash with tabs.
4794   {
4795     FormatStyle Tabbed = Style;
4796     Tabbed.UseTab = FormatStyle::UT_Always;
4797     Tabbed.IndentWidth = 8;
4798     Tabbed.TabWidth = 8;
4799     verifyFormat("#ifdef _WIN32\n"
4800                  "#\tdefine A 0\n"
4801                  "#\tifdef VAR2\n"
4802                  "#\t\tdefine B 1\n"
4803                  "#\t\tinclude <someheader.h>\n"
4804                  "#\t\tdefine MACRO          \\\n"
4805                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
4806                  "#\tendif\n"
4807                  "#else\n"
4808                  "#\tdefine A 1\n"
4809                  "#endif",
4810                  Tabbed);
4811   }
4812 
4813   // Regression test: Multiline-macro inside include guards.
4814   verifyFormat("#ifndef HEADER_H\n"
4815                "#define HEADER_H\n"
4816                "#define A()        \\\n"
4817                "  int i;           \\\n"
4818                "  int j;\n"
4819                "#endif // HEADER_H",
4820                getLLVMStyleWithColumns(20));
4821 
4822   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4823   // Basic before hash indent tests
4824   verifyFormat("#ifdef _WIN32\n"
4825                "  #define A 0\n"
4826                "  #ifdef VAR2\n"
4827                "    #define B 1\n"
4828                "    #include <someheader.h>\n"
4829                "    #define MACRO                      \\\n"
4830                "      some_very_long_func_aaaaaaaaaa();\n"
4831                "  #endif\n"
4832                "#else\n"
4833                "  #define A 1\n"
4834                "#endif",
4835                Style);
4836   verifyFormat("#if A\n"
4837                "  #define MACRO                        \\\n"
4838                "    void a(int x) {                    \\\n"
4839                "      b();                             \\\n"
4840                "      c();                             \\\n"
4841                "      d();                             \\\n"
4842                "      e();                             \\\n"
4843                "      f();                             \\\n"
4844                "    }\n"
4845                "#endif",
4846                Style);
4847   // Keep comments aligned with indented directives. These
4848   // tests cannot use verifyFormat because messUp manipulates leading
4849   // whitespace.
4850   {
4851     const char *Expected = "void f() {\n"
4852                            "// Aligned to preprocessor.\n"
4853                            "#if 1\n"
4854                            "  // Aligned to code.\n"
4855                            "  int a;\n"
4856                            "  #if 1\n"
4857                            "    // Aligned to preprocessor.\n"
4858                            "    #define A 0\n"
4859                            "  // Aligned to code.\n"
4860                            "  int b;\n"
4861                            "  #endif\n"
4862                            "#endif\n"
4863                            "}";
4864     const char *ToFormat = "void f() {\n"
4865                            "// Aligned to preprocessor.\n"
4866                            "#if 1\n"
4867                            "// Aligned to code.\n"
4868                            "int a;\n"
4869                            "#if 1\n"
4870                            "// Aligned to preprocessor.\n"
4871                            "#define A 0\n"
4872                            "// Aligned to code.\n"
4873                            "int b;\n"
4874                            "#endif\n"
4875                            "#endif\n"
4876                            "}";
4877     EXPECT_EQ(Expected, format(ToFormat, Style));
4878     EXPECT_EQ(Expected, format(Expected, Style));
4879   }
4880   {
4881     const char *Expected = "void f() {\n"
4882                            "/* Aligned to preprocessor. */\n"
4883                            "#if 1\n"
4884                            "  /* Aligned to code. */\n"
4885                            "  int a;\n"
4886                            "  #if 1\n"
4887                            "    /* Aligned to preprocessor. */\n"
4888                            "    #define A 0\n"
4889                            "  /* Aligned to code. */\n"
4890                            "  int b;\n"
4891                            "  #endif\n"
4892                            "#endif\n"
4893                            "}";
4894     const char *ToFormat = "void f() {\n"
4895                            "/* Aligned to preprocessor. */\n"
4896                            "#if 1\n"
4897                            "/* Aligned to code. */\n"
4898                            "int a;\n"
4899                            "#if 1\n"
4900                            "/* Aligned to preprocessor. */\n"
4901                            "#define A 0\n"
4902                            "/* Aligned to code. */\n"
4903                            "int b;\n"
4904                            "#endif\n"
4905                            "#endif\n"
4906                            "}";
4907     EXPECT_EQ(Expected, format(ToFormat, Style));
4908     EXPECT_EQ(Expected, format(Expected, Style));
4909   }
4910 
4911   // Test single comment before preprocessor
4912   verifyFormat("// Comment\n"
4913                "\n"
4914                "#if 1\n"
4915                "#endif",
4916                Style);
4917 }
4918 
4919 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
4920   verifyFormat("{\n  { a #c; }\n}");
4921 }
4922 
4923 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
4924   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
4925             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
4926   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
4927             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
4928 }
4929 
4930 TEST_F(FormatTest, EscapedNewlines) {
4931   FormatStyle Narrow = getLLVMStyleWithColumns(11);
4932   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
4933             format("#define A \\\nint i;\\\n  int j;", Narrow));
4934   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
4935   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
4936   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
4937   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
4938 
4939   FormatStyle AlignLeft = getLLVMStyle();
4940   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
4941   EXPECT_EQ("#define MACRO(x) \\\n"
4942             "private:         \\\n"
4943             "  int x(int a);\n",
4944             format("#define MACRO(x) \\\n"
4945                    "private:         \\\n"
4946                    "  int x(int a);\n",
4947                    AlignLeft));
4948 
4949   // CRLF line endings
4950   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
4951             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
4952   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
4953   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
4954   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
4955   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
4956   EXPECT_EQ("#define MACRO(x) \\\r\n"
4957             "private:         \\\r\n"
4958             "  int x(int a);\r\n",
4959             format("#define MACRO(x) \\\r\n"
4960                    "private:         \\\r\n"
4961                    "  int x(int a);\r\n",
4962                    AlignLeft));
4963 
4964   FormatStyle DontAlign = getLLVMStyle();
4965   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
4966   DontAlign.MaxEmptyLinesToKeep = 3;
4967   // FIXME: can't use verifyFormat here because the newline before
4968   // "public:" is not inserted the first time it's reformatted
4969   EXPECT_EQ("#define A \\\n"
4970             "  class Foo { \\\n"
4971             "    void bar(); \\\n"
4972             "\\\n"
4973             "\\\n"
4974             "\\\n"
4975             "  public: \\\n"
4976             "    void baz(); \\\n"
4977             "  };",
4978             format("#define A \\\n"
4979                    "  class Foo { \\\n"
4980                    "    void bar(); \\\n"
4981                    "\\\n"
4982                    "\\\n"
4983                    "\\\n"
4984                    "  public: \\\n"
4985                    "    void baz(); \\\n"
4986                    "  };",
4987                    DontAlign));
4988 }
4989 
4990 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
4991   verifyFormat("#define A \\\n"
4992                "  int v(  \\\n"
4993                "      a); \\\n"
4994                "  int i;",
4995                getLLVMStyleWithColumns(11));
4996 }
4997 
4998 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
4999   EXPECT_EQ(
5000       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5001       "                      \\\n"
5002       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5003       "\n"
5004       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5005       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5006       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5007              "\\\n"
5008              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5009              "  \n"
5010              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5011              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5012 }
5013 
5014 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5015   EXPECT_EQ("int\n"
5016             "#define A\n"
5017             "    a;",
5018             format("int\n#define A\na;"));
5019   verifyFormat("functionCallTo(\n"
5020                "    someOtherFunction(\n"
5021                "        withSomeParameters, whichInSequence,\n"
5022                "        areLongerThanALine(andAnotherCall,\n"
5023                "#define A B\n"
5024                "                           withMoreParamters,\n"
5025                "                           whichStronglyInfluenceTheLayout),\n"
5026                "        andMoreParameters),\n"
5027                "    trailing);",
5028                getLLVMStyleWithColumns(69));
5029   verifyFormat("Foo::Foo()\n"
5030                "#ifdef BAR\n"
5031                "    : baz(0)\n"
5032                "#endif\n"
5033                "{\n"
5034                "}");
5035   verifyFormat("void f() {\n"
5036                "  if (true)\n"
5037                "#ifdef A\n"
5038                "    f(42);\n"
5039                "  x();\n"
5040                "#else\n"
5041                "    g();\n"
5042                "  x();\n"
5043                "#endif\n"
5044                "}");
5045   verifyFormat("void f(param1, param2,\n"
5046                "       param3,\n"
5047                "#ifdef A\n"
5048                "       param4(param5,\n"
5049                "#ifdef A1\n"
5050                "              param6,\n"
5051                "#ifdef A2\n"
5052                "              param7),\n"
5053                "#else\n"
5054                "              param8),\n"
5055                "       param9,\n"
5056                "#endif\n"
5057                "       param10,\n"
5058                "#endif\n"
5059                "       param11)\n"
5060                "#else\n"
5061                "       param12)\n"
5062                "#endif\n"
5063                "{\n"
5064                "  x();\n"
5065                "}",
5066                getLLVMStyleWithColumns(28));
5067   verifyFormat("#if 1\n"
5068                "int i;");
5069   verifyFormat("#if 1\n"
5070                "#endif\n"
5071                "#if 1\n"
5072                "#else\n"
5073                "#endif\n");
5074   verifyFormat("DEBUG({\n"
5075                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5076                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5077                "});\n"
5078                "#if a\n"
5079                "#else\n"
5080                "#endif");
5081 
5082   verifyIncompleteFormat("void f(\n"
5083                          "#if A\n"
5084                          ");\n"
5085                          "#else\n"
5086                          "#endif");
5087 }
5088 
5089 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5090   verifyFormat("#endif\n"
5091                "#if B");
5092 }
5093 
5094 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5095   FormatStyle SingleLine = getLLVMStyle();
5096   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5097   verifyFormat("#if 0\n"
5098                "#elif 1\n"
5099                "#endif\n"
5100                "void foo() {\n"
5101                "  if (test) foo2();\n"
5102                "}",
5103                SingleLine);
5104 }
5105 
5106 TEST_F(FormatTest, LayoutBlockInsideParens) {
5107   verifyFormat("functionCall({ int i; });");
5108   verifyFormat("functionCall({\n"
5109                "  int i;\n"
5110                "  int j;\n"
5111                "});");
5112   verifyFormat("functionCall(\n"
5113                "    {\n"
5114                "      int i;\n"
5115                "      int j;\n"
5116                "    },\n"
5117                "    aaaa, bbbb, cccc);");
5118   verifyFormat("functionA(functionB({\n"
5119                "            int i;\n"
5120                "            int j;\n"
5121                "          }),\n"
5122                "          aaaa, bbbb, cccc);");
5123   verifyFormat("functionCall(\n"
5124                "    {\n"
5125                "      int i;\n"
5126                "      int j;\n"
5127                "    },\n"
5128                "    aaaa, bbbb, // comment\n"
5129                "    cccc);");
5130   verifyFormat("functionA(functionB({\n"
5131                "            int i;\n"
5132                "            int j;\n"
5133                "          }),\n"
5134                "          aaaa, bbbb, // comment\n"
5135                "          cccc);");
5136   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5137   verifyFormat("functionCall(aaaa, bbbb, {\n"
5138                "  int i;\n"
5139                "  int j;\n"
5140                "});");
5141   verifyFormat(
5142       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5143       "    {\n"
5144       "      int i; // break\n"
5145       "    },\n"
5146       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5147       "                                     ccccccccccccccccc));");
5148   verifyFormat("DEBUG({\n"
5149                "  if (a)\n"
5150                "    f();\n"
5151                "});");
5152 }
5153 
5154 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5155   EXPECT_EQ("SOME_MACRO { int i; }\n"
5156             "int i;",
5157             format("  SOME_MACRO  {int i;}  int i;"));
5158 }
5159 
5160 TEST_F(FormatTest, LayoutNestedBlocks) {
5161   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5162                "  struct s {\n"
5163                "    int i;\n"
5164                "  };\n"
5165                "  s kBitsToOs[] = {{10}};\n"
5166                "  for (int i = 0; i < 10; ++i)\n"
5167                "    return;\n"
5168                "}");
5169   verifyFormat("call(parameter, {\n"
5170                "  something();\n"
5171                "  // Comment using all columns.\n"
5172                "  somethingelse();\n"
5173                "});",
5174                getLLVMStyleWithColumns(40));
5175   verifyFormat("DEBUG( //\n"
5176                "    { f(); }, a);");
5177   verifyFormat("DEBUG( //\n"
5178                "    {\n"
5179                "      f(); //\n"
5180                "    },\n"
5181                "    a);");
5182 
5183   EXPECT_EQ("call(parameter, {\n"
5184             "  something();\n"
5185             "  // Comment too\n"
5186             "  // looooooooooong.\n"
5187             "  somethingElse();\n"
5188             "});",
5189             format("call(parameter, {\n"
5190                    "  something();\n"
5191                    "  // Comment too looooooooooong.\n"
5192                    "  somethingElse();\n"
5193                    "});",
5194                    getLLVMStyleWithColumns(29)));
5195   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5196   EXPECT_EQ("DEBUG({ // comment\n"
5197             "  int i;\n"
5198             "});",
5199             format("DEBUG({ // comment\n"
5200                    "int  i;\n"
5201                    "});"));
5202   EXPECT_EQ("DEBUG({\n"
5203             "  int i;\n"
5204             "\n"
5205             "  // comment\n"
5206             "  int j;\n"
5207             "});",
5208             format("DEBUG({\n"
5209                    "  int  i;\n"
5210                    "\n"
5211                    "  // comment\n"
5212                    "  int  j;\n"
5213                    "});"));
5214 
5215   verifyFormat("DEBUG({\n"
5216                "  if (a)\n"
5217                "    return;\n"
5218                "});");
5219   verifyGoogleFormat("DEBUG({\n"
5220                      "  if (a) return;\n"
5221                      "});");
5222   FormatStyle Style = getGoogleStyle();
5223   Style.ColumnLimit = 45;
5224   verifyFormat("Debug(\n"
5225                "    aaaaa,\n"
5226                "    {\n"
5227                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5228                "    },\n"
5229                "    a);",
5230                Style);
5231 
5232   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5233 
5234   verifyNoCrash("^{v^{a}}");
5235 }
5236 
5237 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5238   EXPECT_EQ("#define MACRO()                     \\\n"
5239             "  Debug(aaa, /* force line break */ \\\n"
5240             "        {                           \\\n"
5241             "          int i;                    \\\n"
5242             "          int j;                    \\\n"
5243             "        })",
5244             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5245                    "          {  int   i;  int  j;   })",
5246                    getGoogleStyle()));
5247 
5248   EXPECT_EQ("#define A                                       \\\n"
5249             "  [] {                                          \\\n"
5250             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5251             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5252             "  }",
5253             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5254                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5255                    getGoogleStyle()));
5256 }
5257 
5258 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5259   EXPECT_EQ("{}", format("{}"));
5260   verifyFormat("enum E {};");
5261   verifyFormat("enum E {}");
5262   FormatStyle Style = getLLVMStyle();
5263   Style.SpaceInEmptyBlock = true;
5264   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5265   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5266   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5267 }
5268 
5269 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5270   FormatStyle Style = getLLVMStyle();
5271   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5272   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5273   verifyFormat("FOO_BEGIN\n"
5274                "  FOO_ENTRY\n"
5275                "FOO_END",
5276                Style);
5277   verifyFormat("FOO_BEGIN\n"
5278                "  NESTED_FOO_BEGIN\n"
5279                "    NESTED_FOO_ENTRY\n"
5280                "  NESTED_FOO_END\n"
5281                "FOO_END",
5282                Style);
5283   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5284                "  int x;\n"
5285                "  x = 1;\n"
5286                "FOO_END(Baz)",
5287                Style);
5288 }
5289 
5290 //===----------------------------------------------------------------------===//
5291 // Line break tests.
5292 //===----------------------------------------------------------------------===//
5293 
5294 TEST_F(FormatTest, PreventConfusingIndents) {
5295   verifyFormat(
5296       "void f() {\n"
5297       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5298       "                         parameter, parameter, parameter)),\n"
5299       "                     SecondLongCall(parameter));\n"
5300       "}");
5301   verifyFormat(
5302       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5303       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5304       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5305       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5306   verifyFormat(
5307       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5308       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5309       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5310       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5311   verifyFormat(
5312       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5313       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5314       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5315       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5316   verifyFormat("int a = bbbb && ccc &&\n"
5317                "        fffff(\n"
5318                "#define A Just forcing a new line\n"
5319                "            ddd);");
5320 }
5321 
5322 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5323   verifyFormat(
5324       "bool aaaaaaa =\n"
5325       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5326       "    bbbbbbbb();");
5327   verifyFormat(
5328       "bool aaaaaaa =\n"
5329       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5330       "    bbbbbbbb();");
5331 
5332   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5333                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5334                "    ccccccccc == ddddddddddd;");
5335   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5336                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5337                "    ccccccccc == ddddddddddd;");
5338   verifyFormat(
5339       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5340       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5341       "    ccccccccc == ddddddddddd;");
5342 
5343   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5344                "                 aaaaaa) &&\n"
5345                "         bbbbbb && cccccc;");
5346   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5347                "                 aaaaaa) >>\n"
5348                "         bbbbbb;");
5349   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5350                "    SourceMgr.getSpellingColumnNumber(\n"
5351                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5352                "    1);");
5353 
5354   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5355                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5356                "    cccccc) {\n}");
5357   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5358                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5359                "              cccccc) {\n}");
5360   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5361                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5362                "              cccccc) {\n}");
5363   verifyFormat("b = a &&\n"
5364                "    // Comment\n"
5365                "    b.c && d;");
5366 
5367   // If the LHS of a comparison is not a binary expression itself, the
5368   // additional linebreak confuses many people.
5369   verifyFormat(
5370       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5371       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5372       "}");
5373   verifyFormat(
5374       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5375       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5376       "}");
5377   verifyFormat(
5378       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5379       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5380       "}");
5381   verifyFormat(
5382       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5383       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5384       "}");
5385   // Even explicit parentheses stress the precedence enough to make the
5386   // additional break unnecessary.
5387   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5388                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5389                "}");
5390   // This cases is borderline, but with the indentation it is still readable.
5391   verifyFormat(
5392       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5393       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5394       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5395       "}",
5396       getLLVMStyleWithColumns(75));
5397 
5398   // If the LHS is a binary expression, we should still use the additional break
5399   // as otherwise the formatting hides the operator precedence.
5400   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5401                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5402                "    5) {\n"
5403                "}");
5404   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5405                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5406                "    5) {\n"
5407                "}");
5408 
5409   FormatStyle OnePerLine = getLLVMStyle();
5410   OnePerLine.BinPackParameters = false;
5411   verifyFormat(
5412       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5413       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5414       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5415       OnePerLine);
5416 
5417   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5418                "                .aaa(aaaaaaaaaaaaa) *\n"
5419                "            aaaaaaa +\n"
5420                "        aaaaaaa;",
5421                getLLVMStyleWithColumns(40));
5422 }
5423 
5424 TEST_F(FormatTest, ExpressionIndentation) {
5425   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5426                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5427                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5428                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5429                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5430                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5431                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5432                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5433                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5434   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5435                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5436                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5437                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5438   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5439                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5440                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5441                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5442   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5443                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5444                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5445                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5446   verifyFormat("if () {\n"
5447                "} else if (aaaaa && bbbbb > // break\n"
5448                "                        ccccc) {\n"
5449                "}");
5450   verifyFormat("if () {\n"
5451                "} else if constexpr (aaaaa && bbbbb > // break\n"
5452                "                                  ccccc) {\n"
5453                "}");
5454   verifyFormat("if () {\n"
5455                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5456                "                                  ccccc) {\n"
5457                "}");
5458   verifyFormat("if () {\n"
5459                "} else if (aaaaa &&\n"
5460                "           bbbbb > // break\n"
5461                "               ccccc &&\n"
5462                "           ddddd) {\n"
5463                "}");
5464 
5465   // Presence of a trailing comment used to change indentation of b.
5466   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
5467                "       b;\n"
5468                "return aaaaaaaaaaaaaaaaaaa +\n"
5469                "       b; //",
5470                getLLVMStyleWithColumns(30));
5471 }
5472 
5473 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
5474   // Not sure what the best system is here. Like this, the LHS can be found
5475   // immediately above an operator (everything with the same or a higher
5476   // indent). The RHS is aligned right of the operator and so compasses
5477   // everything until something with the same indent as the operator is found.
5478   // FIXME: Is this a good system?
5479   FormatStyle Style = getLLVMStyle();
5480   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5481   verifyFormat(
5482       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5483       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5484       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5485       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5486       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5487       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5488       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5489       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5490       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
5491       Style);
5492   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5493                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5494                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5495                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5496                Style);
5497   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5498                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5499                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5500                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5501                Style);
5502   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5503                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5504                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5505                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5506                Style);
5507   verifyFormat("if () {\n"
5508                "} else if (aaaaa\n"
5509                "           && bbbbb // break\n"
5510                "                  > ccccc) {\n"
5511                "}",
5512                Style);
5513   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5514                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5515                Style);
5516   verifyFormat("return (a)\n"
5517                "       // comment\n"
5518                "       + b;",
5519                Style);
5520   verifyFormat(
5521       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5522       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5523       "             + cc;",
5524       Style);
5525 
5526   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5527                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5528                Style);
5529 
5530   // Forced by comments.
5531   verifyFormat(
5532       "unsigned ContentSize =\n"
5533       "    sizeof(int16_t)   // DWARF ARange version number\n"
5534       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5535       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5536       "    + sizeof(int8_t); // Segment Size (in bytes)");
5537 
5538   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5539                "       == boost::fusion::at_c<1>(iiii).second;",
5540                Style);
5541 
5542   Style.ColumnLimit = 60;
5543   verifyFormat("zzzzzzzzzz\n"
5544                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5545                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5546                Style);
5547 
5548   Style.ColumnLimit = 80;
5549   Style.IndentWidth = 4;
5550   Style.TabWidth = 4;
5551   Style.UseTab = FormatStyle::UT_Always;
5552   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5553   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5554   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
5555             "\t&& (someOtherLongishConditionPart1\n"
5556             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
5557             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
5558                    "(someOtherLongishConditionPart1 || "
5559                    "someOtherEvenLongerNestedConditionPart2);",
5560                    Style));
5561 }
5562 
5563 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
5564   FormatStyle Style = getLLVMStyle();
5565   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5566   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
5567 
5568   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5569                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5570                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5571                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5572                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5573                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5574                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5575                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5576                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
5577                Style);
5578   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5579                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5580                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5581                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5582                Style);
5583   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5584                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5585                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5586                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5587                Style);
5588   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5589                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5590                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5591                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5592                Style);
5593   verifyFormat("if () {\n"
5594                "} else if (aaaaa\n"
5595                "           && bbbbb // break\n"
5596                "                  > ccccc) {\n"
5597                "}",
5598                Style);
5599   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5600                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5601                Style);
5602   verifyFormat("return (a)\n"
5603                "     // comment\n"
5604                "     + b;",
5605                Style);
5606   verifyFormat(
5607       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5608       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5609       "           + cc;",
5610       Style);
5611   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
5612                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5613                "                        : 3333333333333333;",
5614                Style);
5615   verifyFormat(
5616       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
5617       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
5618       "                                             : eeeeeeeeeeeeeeeeee)\n"
5619       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5620       "                        : 3333333333333333;",
5621       Style);
5622   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5623                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5624                Style);
5625 
5626   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5627                "    == boost::fusion::at_c<1>(iiii).second;",
5628                Style);
5629 
5630   Style.ColumnLimit = 60;
5631   verifyFormat("zzzzzzzzzzzzz\n"
5632                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5633                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5634                Style);
5635 
5636   // Forced by comments.
5637   Style.ColumnLimit = 80;
5638   verifyFormat(
5639       "unsigned ContentSize\n"
5640       "    = sizeof(int16_t) // DWARF ARange version number\n"
5641       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5642       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5643       "    + sizeof(int8_t); // Segment Size (in bytes)",
5644       Style);
5645 
5646   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5647   verifyFormat(
5648       "unsigned ContentSize =\n"
5649       "    sizeof(int16_t)   // DWARF ARange version number\n"
5650       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5651       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5652       "    + sizeof(int8_t); // Segment Size (in bytes)",
5653       Style);
5654 
5655   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5656   verifyFormat(
5657       "unsigned ContentSize =\n"
5658       "    sizeof(int16_t)   // DWARF ARange version number\n"
5659       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5660       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5661       "    + sizeof(int8_t); // Segment Size (in bytes)",
5662       Style);
5663 }
5664 
5665 TEST_F(FormatTest, EnforcedOperatorWraps) {
5666   // Here we'd like to wrap after the || operators, but a comment is forcing an
5667   // earlier wrap.
5668   verifyFormat("bool x = aaaaa //\n"
5669                "         || bbbbb\n"
5670                "         //\n"
5671                "         || cccc;");
5672 }
5673 
5674 TEST_F(FormatTest, NoOperandAlignment) {
5675   FormatStyle Style = getLLVMStyle();
5676   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5677   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
5678                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5679                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5680                Style);
5681   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5682   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5683                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5684                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5685                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5686                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5687                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5688                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5689                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5690                "        > ccccccccccccccccccccccccccccccccccccccccc;",
5691                Style);
5692 
5693   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5694                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5695                "    + cc;",
5696                Style);
5697   verifyFormat("int a = aa\n"
5698                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5699                "        * cccccccccccccccccccccccccccccccccccc;\n",
5700                Style);
5701 
5702   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5703   verifyFormat("return (a > b\n"
5704                "    // comment1\n"
5705                "    // comment2\n"
5706                "    || c);",
5707                Style);
5708 }
5709 
5710 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5711   FormatStyle Style = getLLVMStyle();
5712   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5713   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5714                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5715                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5716                Style);
5717 }
5718 
5719 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
5720   FormatStyle Style = getLLVMStyle();
5721   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5722   Style.BinPackArguments = false;
5723   Style.ColumnLimit = 40;
5724   verifyFormat("void test() {\n"
5725                "  someFunction(\n"
5726                "      this + argument + is + quite\n"
5727                "      + long + so + it + gets + wrapped\n"
5728                "      + but + remains + bin - packed);\n"
5729                "}",
5730                Style);
5731   verifyFormat("void test() {\n"
5732                "  someFunction(arg1,\n"
5733                "               this + argument + is\n"
5734                "                   + quite + long + so\n"
5735                "                   + it + gets + wrapped\n"
5736                "                   + but + remains + bin\n"
5737                "                   - packed,\n"
5738                "               arg3);\n"
5739                "}",
5740                Style);
5741   verifyFormat("void test() {\n"
5742                "  someFunction(\n"
5743                "      arg1,\n"
5744                "      this + argument + has\n"
5745                "          + anotherFunc(nested,\n"
5746                "                        calls + whose\n"
5747                "                            + arguments\n"
5748                "                            + are + also\n"
5749                "                            + wrapped,\n"
5750                "                        in + addition)\n"
5751                "          + to + being + bin - packed,\n"
5752                "      arg3);\n"
5753                "}",
5754                Style);
5755 
5756   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5757   verifyFormat("void test() {\n"
5758                "  someFunction(\n"
5759                "      arg1,\n"
5760                "      this + argument + has +\n"
5761                "          anotherFunc(nested,\n"
5762                "                      calls + whose +\n"
5763                "                          arguments +\n"
5764                "                          are + also +\n"
5765                "                          wrapped,\n"
5766                "                      in + addition) +\n"
5767                "          to + being + bin - packed,\n"
5768                "      arg3);\n"
5769                "}",
5770                Style);
5771 }
5772 
5773 TEST_F(FormatTest, ConstructorInitializers) {
5774   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5775   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
5776                getLLVMStyleWithColumns(45));
5777   verifyFormat("Constructor()\n"
5778                "    : Inttializer(FitsOnTheLine) {}",
5779                getLLVMStyleWithColumns(44));
5780   verifyFormat("Constructor()\n"
5781                "    : Inttializer(FitsOnTheLine) {}",
5782                getLLVMStyleWithColumns(43));
5783 
5784   verifyFormat("template <typename T>\n"
5785                "Constructor() : Initializer(FitsOnTheLine) {}",
5786                getLLVMStyleWithColumns(45));
5787 
5788   verifyFormat(
5789       "SomeClass::Constructor()\n"
5790       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5791 
5792   verifyFormat(
5793       "SomeClass::Constructor()\n"
5794       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5795       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
5796   verifyFormat(
5797       "SomeClass::Constructor()\n"
5798       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5799       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5800   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5801                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5802                "    : aaaaaaaaaa(aaaaaa) {}");
5803 
5804   verifyFormat("Constructor()\n"
5805                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5806                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5807                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5808                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
5809 
5810   verifyFormat("Constructor()\n"
5811                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5812                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5813 
5814   verifyFormat("Constructor(int Parameter = 0)\n"
5815                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5816                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
5817   verifyFormat("Constructor()\n"
5818                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5819                "}",
5820                getLLVMStyleWithColumns(60));
5821   verifyFormat("Constructor()\n"
5822                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5823                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
5824 
5825   // Here a line could be saved by splitting the second initializer onto two
5826   // lines, but that is not desirable.
5827   verifyFormat("Constructor()\n"
5828                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5829                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
5830                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5831 
5832   FormatStyle OnePerLine = getLLVMStyle();
5833   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5834   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
5835   verifyFormat("SomeClass::Constructor()\n"
5836                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5837                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5838                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5839                OnePerLine);
5840   verifyFormat("SomeClass::Constructor()\n"
5841                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
5842                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5843                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5844                OnePerLine);
5845   verifyFormat("MyClass::MyClass(int var)\n"
5846                "    : some_var_(var),            // 4 space indent\n"
5847                "      some_other_var_(var + 1) { // lined up\n"
5848                "}",
5849                OnePerLine);
5850   verifyFormat("Constructor()\n"
5851                "    : aaaaa(aaaaaa),\n"
5852                "      aaaaa(aaaaaa),\n"
5853                "      aaaaa(aaaaaa),\n"
5854                "      aaaaa(aaaaaa),\n"
5855                "      aaaaa(aaaaaa) {}",
5856                OnePerLine);
5857   verifyFormat("Constructor()\n"
5858                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5859                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
5860                OnePerLine);
5861   OnePerLine.BinPackParameters = false;
5862   verifyFormat(
5863       "Constructor()\n"
5864       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5865       "          aaaaaaaaaaa().aaa(),\n"
5866       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5867       OnePerLine);
5868   OnePerLine.ColumnLimit = 60;
5869   verifyFormat("Constructor()\n"
5870                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5871                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
5872                OnePerLine);
5873 
5874   EXPECT_EQ("Constructor()\n"
5875             "    : // Comment forcing unwanted break.\n"
5876             "      aaaa(aaaa) {}",
5877             format("Constructor() :\n"
5878                    "    // Comment forcing unwanted break.\n"
5879                    "    aaaa(aaaa) {}"));
5880 }
5881 
5882 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
5883   FormatStyle Style = getLLVMStyle();
5884   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
5885   Style.ColumnLimit = 60;
5886   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5887   Style.AllowAllConstructorInitializersOnNextLine = true;
5888   Style.BinPackParameters = false;
5889 
5890   for (int i = 0; i < 4; ++i) {
5891     // Test all combinations of parameters that should not have an effect.
5892     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
5893     Style.AllowAllArgumentsOnNextLine = i & 2;
5894 
5895     Style.AllowAllConstructorInitializersOnNextLine = true;
5896     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
5897     verifyFormat("Constructor()\n"
5898                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5899                  Style);
5900     verifyFormat("Constructor() : a(a), b(b) {}", Style);
5901 
5902     Style.AllowAllConstructorInitializersOnNextLine = false;
5903     verifyFormat("Constructor()\n"
5904                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
5905                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
5906                  Style);
5907     verifyFormat("Constructor() : a(a), b(b) {}", Style);
5908 
5909     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
5910     Style.AllowAllConstructorInitializersOnNextLine = true;
5911     verifyFormat("Constructor()\n"
5912                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5913                  Style);
5914 
5915     Style.AllowAllConstructorInitializersOnNextLine = false;
5916     verifyFormat("Constructor()\n"
5917                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5918                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
5919                  Style);
5920 
5921     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
5922     Style.AllowAllConstructorInitializersOnNextLine = true;
5923     verifyFormat("Constructor() :\n"
5924                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5925                  Style);
5926 
5927     Style.AllowAllConstructorInitializersOnNextLine = false;
5928     verifyFormat("Constructor() :\n"
5929                  "    aaaaaaaaaaaaaaaaaa(a),\n"
5930                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
5931                  Style);
5932   }
5933 
5934   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
5935   // AllowAllConstructorInitializersOnNextLine in all
5936   // BreakConstructorInitializers modes
5937   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
5938   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5939   Style.AllowAllConstructorInitializersOnNextLine = false;
5940   verifyFormat("SomeClassWithALongName::Constructor(\n"
5941                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
5942                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
5943                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
5944                Style);
5945 
5946   Style.AllowAllConstructorInitializersOnNextLine = true;
5947   verifyFormat("SomeClassWithALongName::Constructor(\n"
5948                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5949                "    int bbbbbbbbbbbbb,\n"
5950                "    int cccccccccccccccc)\n"
5951                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5952                Style);
5953 
5954   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5955   Style.AllowAllConstructorInitializersOnNextLine = false;
5956   verifyFormat("SomeClassWithALongName::Constructor(\n"
5957                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5958                "    int bbbbbbbbbbbbb)\n"
5959                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
5960                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
5961                Style);
5962 
5963   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
5964 
5965   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5966   verifyFormat("SomeClassWithALongName::Constructor(\n"
5967                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
5968                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5969                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
5970                Style);
5971 
5972   Style.AllowAllConstructorInitializersOnNextLine = true;
5973   verifyFormat("SomeClassWithALongName::Constructor(\n"
5974                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5975                "    int bbbbbbbbbbbbb,\n"
5976                "    int cccccccccccccccc)\n"
5977                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5978                Style);
5979 
5980   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5981   Style.AllowAllConstructorInitializersOnNextLine = false;
5982   verifyFormat("SomeClassWithALongName::Constructor(\n"
5983                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5984                "    int bbbbbbbbbbbbb)\n"
5985                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5986                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
5987                Style);
5988 
5989   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
5990   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5991   verifyFormat("SomeClassWithALongName::Constructor(\n"
5992                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
5993                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
5994                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
5995                Style);
5996 
5997   Style.AllowAllConstructorInitializersOnNextLine = true;
5998   verifyFormat("SomeClassWithALongName::Constructor(\n"
5999                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6000                "    int bbbbbbbbbbbbb,\n"
6001                "    int cccccccccccccccc) :\n"
6002                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6003                Style);
6004 
6005   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6006   Style.AllowAllConstructorInitializersOnNextLine = false;
6007   verifyFormat("SomeClassWithALongName::Constructor(\n"
6008                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6009                "    int bbbbbbbbbbbbb) :\n"
6010                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6011                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6012                Style);
6013 }
6014 
6015 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6016   FormatStyle Style = getLLVMStyle();
6017   Style.ColumnLimit = 60;
6018   Style.BinPackArguments = false;
6019   for (int i = 0; i < 4; ++i) {
6020     // Test all combinations of parameters that should not have an effect.
6021     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6022     Style.AllowAllConstructorInitializersOnNextLine = i & 2;
6023 
6024     Style.AllowAllArgumentsOnNextLine = true;
6025     verifyFormat("void foo() {\n"
6026                  "  FunctionCallWithReallyLongName(\n"
6027                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6028                  "}",
6029                  Style);
6030     Style.AllowAllArgumentsOnNextLine = false;
6031     verifyFormat("void foo() {\n"
6032                  "  FunctionCallWithReallyLongName(\n"
6033                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6034                  "      bbbbbbbbbbbb);\n"
6035                  "}",
6036                  Style);
6037 
6038     Style.AllowAllArgumentsOnNextLine = true;
6039     verifyFormat("void foo() {\n"
6040                  "  auto VariableWithReallyLongName = {\n"
6041                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6042                  "}",
6043                  Style);
6044     Style.AllowAllArgumentsOnNextLine = false;
6045     verifyFormat("void foo() {\n"
6046                  "  auto VariableWithReallyLongName = {\n"
6047                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6048                  "      bbbbbbbbbbbb};\n"
6049                  "}",
6050                  Style);
6051   }
6052 
6053   // This parameter should not affect declarations.
6054   Style.BinPackParameters = false;
6055   Style.AllowAllArgumentsOnNextLine = false;
6056   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6057   verifyFormat("void FunctionCallWithReallyLongName(\n"
6058                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6059                Style);
6060   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6061   verifyFormat("void FunctionCallWithReallyLongName(\n"
6062                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6063                "    int bbbbbbbbbbbb);",
6064                Style);
6065 }
6066 
6067 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6068   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6069   // and BAS_Align.
6070   auto Style = getLLVMStyle();
6071   Style.ColumnLimit = 35;
6072   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6073                     "void functionDecl(int A, int B, int C);";
6074   Style.AllowAllArgumentsOnNextLine = false;
6075   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6076   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6077                       "    paramC);\n"
6078                       "void functionDecl(int A, int B,\n"
6079                       "    int C);"),
6080             format(Input, Style));
6081   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6082   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6083                       "             paramC);\n"
6084                       "void functionDecl(int A, int B,\n"
6085                       "                  int C);"),
6086             format(Input, Style));
6087   // However, BAS_AlwaysBreak should take precedence over
6088   // AllowAllArgumentsOnNextLine.
6089   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6090   EXPECT_EQ(StringRef("functionCall(\n"
6091                       "    paramA, paramB, paramC);\n"
6092                       "void functionDecl(\n"
6093                       "    int A, int B, int C);"),
6094             format(Input, Style));
6095 
6096   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6097   // first argument.
6098   Style.AllowAllArgumentsOnNextLine = true;
6099   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6100   EXPECT_EQ(StringRef("functionCall(\n"
6101                       "    paramA, paramB, paramC);\n"
6102                       "void functionDecl(\n"
6103                       "    int A, int B, int C);"),
6104             format(Input, Style));
6105   // It wouldn't fit on one line with aligned parameters so this setting
6106   // doesn't change anything for BAS_Align.
6107   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6108   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6109                       "             paramC);\n"
6110                       "void functionDecl(int A, int B,\n"
6111                       "                  int C);"),
6112             format(Input, Style));
6113   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6114   EXPECT_EQ(StringRef("functionCall(\n"
6115                       "    paramA, paramB, paramC);\n"
6116                       "void functionDecl(\n"
6117                       "    int A, int B, int C);"),
6118             format(Input, Style));
6119 }
6120 
6121 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6122   FormatStyle Style = getLLVMStyle();
6123   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6124 
6125   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6126   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6127                getStyleWithColumns(Style, 45));
6128   verifyFormat("Constructor() :\n"
6129                "    Initializer(FitsOnTheLine) {}",
6130                getStyleWithColumns(Style, 44));
6131   verifyFormat("Constructor() :\n"
6132                "    Initializer(FitsOnTheLine) {}",
6133                getStyleWithColumns(Style, 43));
6134 
6135   verifyFormat("template <typename T>\n"
6136                "Constructor() : Initializer(FitsOnTheLine) {}",
6137                getStyleWithColumns(Style, 50));
6138   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6139   verifyFormat(
6140       "SomeClass::Constructor() :\n"
6141       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6142       Style);
6143 
6144   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
6145   verifyFormat(
6146       "SomeClass::Constructor() :\n"
6147       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6148       Style);
6149 
6150   verifyFormat(
6151       "SomeClass::Constructor() :\n"
6152       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6153       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6154       Style);
6155   verifyFormat(
6156       "SomeClass::Constructor() :\n"
6157       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6158       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6159       Style);
6160   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6161                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6162                "    aaaaaaaaaa(aaaaaa) {}",
6163                Style);
6164 
6165   verifyFormat("Constructor() :\n"
6166                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6167                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6168                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6169                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6170                Style);
6171 
6172   verifyFormat("Constructor() :\n"
6173                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6174                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6175                Style);
6176 
6177   verifyFormat("Constructor(int Parameter = 0) :\n"
6178                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6179                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6180                Style);
6181   verifyFormat("Constructor() :\n"
6182                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6183                "}",
6184                getStyleWithColumns(Style, 60));
6185   verifyFormat("Constructor() :\n"
6186                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6187                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6188                Style);
6189 
6190   // Here a line could be saved by splitting the second initializer onto two
6191   // lines, but that is not desirable.
6192   verifyFormat("Constructor() :\n"
6193                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6194                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6195                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6196                Style);
6197 
6198   FormatStyle OnePerLine = Style;
6199   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6200   OnePerLine.AllowAllConstructorInitializersOnNextLine = false;
6201   verifyFormat("SomeClass::Constructor() :\n"
6202                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6203                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6204                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6205                OnePerLine);
6206   verifyFormat("SomeClass::Constructor() :\n"
6207                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6208                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6209                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6210                OnePerLine);
6211   verifyFormat("MyClass::MyClass(int var) :\n"
6212                "    some_var_(var),            // 4 space indent\n"
6213                "    some_other_var_(var + 1) { // lined up\n"
6214                "}",
6215                OnePerLine);
6216   verifyFormat("Constructor() :\n"
6217                "    aaaaa(aaaaaa),\n"
6218                "    aaaaa(aaaaaa),\n"
6219                "    aaaaa(aaaaaa),\n"
6220                "    aaaaa(aaaaaa),\n"
6221                "    aaaaa(aaaaaa) {}",
6222                OnePerLine);
6223   verifyFormat("Constructor() :\n"
6224                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6225                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6226                OnePerLine);
6227   OnePerLine.BinPackParameters = false;
6228   verifyFormat("Constructor() :\n"
6229                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6230                "        aaaaaaaaaaa().aaa(),\n"
6231                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6232                OnePerLine);
6233   OnePerLine.ColumnLimit = 60;
6234   verifyFormat("Constructor() :\n"
6235                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6236                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6237                OnePerLine);
6238 
6239   EXPECT_EQ("Constructor() :\n"
6240             "    // Comment forcing unwanted break.\n"
6241             "    aaaa(aaaa) {}",
6242             format("Constructor() :\n"
6243                    "    // Comment forcing unwanted break.\n"
6244                    "    aaaa(aaaa) {}",
6245                    Style));
6246 
6247   Style.ColumnLimit = 0;
6248   verifyFormat("SomeClass::Constructor() :\n"
6249                "    a(a) {}",
6250                Style);
6251   verifyFormat("SomeClass::Constructor() noexcept :\n"
6252                "    a(a) {}",
6253                Style);
6254   verifyFormat("SomeClass::Constructor() :\n"
6255                "    a(a), b(b), c(c) {}",
6256                Style);
6257   verifyFormat("SomeClass::Constructor() :\n"
6258                "    a(a) {\n"
6259                "  foo();\n"
6260                "  bar();\n"
6261                "}",
6262                Style);
6263 
6264   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6265   verifyFormat("SomeClass::Constructor() :\n"
6266                "    a(a), b(b), c(c) {\n"
6267                "}",
6268                Style);
6269   verifyFormat("SomeClass::Constructor() :\n"
6270                "    a(a) {\n"
6271                "}",
6272                Style);
6273 
6274   Style.ColumnLimit = 80;
6275   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6276   Style.ConstructorInitializerIndentWidth = 2;
6277   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6278   verifyFormat("SomeClass::Constructor() :\n"
6279                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6280                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6281                Style);
6282 
6283   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6284   // well
6285   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6286   verifyFormat(
6287       "class SomeClass\n"
6288       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6289       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6290       Style);
6291   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6292   verifyFormat(
6293       "class SomeClass\n"
6294       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6295       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6296       Style);
6297   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6298   verifyFormat(
6299       "class SomeClass :\n"
6300       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6301       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6302       Style);
6303   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6304   verifyFormat(
6305       "class SomeClass\n"
6306       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6307       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6308       Style);
6309 }
6310 
6311 #ifndef EXPENSIVE_CHECKS
6312 // Expensive checks enables libstdc++ checking which includes validating the
6313 // state of ranges used in std::priority_queue - this blows out the
6314 // runtime/scalability of the function and makes this test unacceptably slow.
6315 TEST_F(FormatTest, MemoizationTests) {
6316   // This breaks if the memoization lookup does not take \c Indent and
6317   // \c LastSpace into account.
6318   verifyFormat(
6319       "extern CFRunLoopTimerRef\n"
6320       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6321       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6322       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6323       "                     CFRunLoopTimerContext *context) {}");
6324 
6325   // Deep nesting somewhat works around our memoization.
6326   verifyFormat(
6327       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6328       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6329       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6330       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6331       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6332       getLLVMStyleWithColumns(65));
6333   verifyFormat(
6334       "aaaaa(\n"
6335       "    aaaaa,\n"
6336       "    aaaaa(\n"
6337       "        aaaaa,\n"
6338       "        aaaaa(\n"
6339       "            aaaaa,\n"
6340       "            aaaaa(\n"
6341       "                aaaaa,\n"
6342       "                aaaaa(\n"
6343       "                    aaaaa,\n"
6344       "                    aaaaa(\n"
6345       "                        aaaaa,\n"
6346       "                        aaaaa(\n"
6347       "                            aaaaa,\n"
6348       "                            aaaaa(\n"
6349       "                                aaaaa,\n"
6350       "                                aaaaa(\n"
6351       "                                    aaaaa,\n"
6352       "                                    aaaaa(\n"
6353       "                                        aaaaa,\n"
6354       "                                        aaaaa(\n"
6355       "                                            aaaaa,\n"
6356       "                                            aaaaa(\n"
6357       "                                                aaaaa,\n"
6358       "                                                aaaaa))))))))))));",
6359       getLLVMStyleWithColumns(65));
6360   verifyFormat(
6361       "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"
6362       "                                  a),\n"
6363       "                                a),\n"
6364       "                              a),\n"
6365       "                            a),\n"
6366       "                          a),\n"
6367       "                        a),\n"
6368       "                      a),\n"
6369       "                    a),\n"
6370       "                  a),\n"
6371       "                a),\n"
6372       "              a),\n"
6373       "            a),\n"
6374       "          a),\n"
6375       "        a),\n"
6376       "      a),\n"
6377       "    a),\n"
6378       "  a)",
6379       getLLVMStyleWithColumns(65));
6380 
6381   // This test takes VERY long when memoization is broken.
6382   FormatStyle OnePerLine = getLLVMStyle();
6383   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6384   OnePerLine.BinPackParameters = false;
6385   std::string input = "Constructor()\n"
6386                       "    : aaaa(a,\n";
6387   for (unsigned i = 0, e = 80; i != e; ++i) {
6388     input += "           a,\n";
6389   }
6390   input += "           a) {}";
6391   verifyFormat(input, OnePerLine);
6392 }
6393 #endif
6394 
6395 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6396   verifyFormat(
6397       "void f() {\n"
6398       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6399       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6400       "    f();\n"
6401       "}");
6402   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6403                "    Intervals[i - 1].getRange().getLast()) {\n}");
6404 }
6405 
6406 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6407   // Principially, we break function declarations in a certain order:
6408   // 1) break amongst arguments.
6409   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6410                "                              Cccccccccccccc cccccccccccccc);");
6411   verifyFormat("template <class TemplateIt>\n"
6412                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6413                "                            TemplateIt *stop) {}");
6414 
6415   // 2) break after return type.
6416   verifyFormat(
6417       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6418       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6419       getGoogleStyle());
6420 
6421   // 3) break after (.
6422   verifyFormat(
6423       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6424       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6425       getGoogleStyle());
6426 
6427   // 4) break before after nested name specifiers.
6428   verifyFormat(
6429       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6430       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6431       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6432       getGoogleStyle());
6433 
6434   // However, there are exceptions, if a sufficient amount of lines can be
6435   // saved.
6436   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6437   // more adjusting.
6438   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6439                "                                  Cccccccccccccc cccccccccc,\n"
6440                "                                  Cccccccccccccc cccccccccc,\n"
6441                "                                  Cccccccccccccc cccccccccc,\n"
6442                "                                  Cccccccccccccc cccccccccc);");
6443   verifyFormat(
6444       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6445       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6446       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6447       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6448       getGoogleStyle());
6449   verifyFormat(
6450       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6451       "                                          Cccccccccccccc cccccccccc,\n"
6452       "                                          Cccccccccccccc cccccccccc,\n"
6453       "                                          Cccccccccccccc cccccccccc,\n"
6454       "                                          Cccccccccccccc cccccccccc,\n"
6455       "                                          Cccccccccccccc cccccccccc,\n"
6456       "                                          Cccccccccccccc cccccccccc);");
6457   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6458                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6459                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6460                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6461                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
6462 
6463   // Break after multi-line parameters.
6464   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6465                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6466                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6467                "    bbbb bbbb);");
6468   verifyFormat("void SomeLoooooooooooongFunction(\n"
6469                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6470                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6471                "    int bbbbbbbbbbbbb);");
6472 
6473   // Treat overloaded operators like other functions.
6474   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6475                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
6476   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6477                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
6478   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6479                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
6480   verifyGoogleFormat(
6481       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
6482       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6483   verifyGoogleFormat(
6484       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
6485       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6486   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6487                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6488   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
6489                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6490   verifyGoogleFormat(
6491       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
6492       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6493       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
6494   verifyGoogleFormat("template <typename T>\n"
6495                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6496                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
6497                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
6498 
6499   FormatStyle Style = getLLVMStyle();
6500   Style.PointerAlignment = FormatStyle::PAS_Left;
6501   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6502                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
6503                Style);
6504   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
6505                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6506                Style);
6507 }
6508 
6509 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
6510   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
6511   // Prefer keeping `::` followed by `operator` together.
6512   EXPECT_EQ("const aaaa::bbbbbbb &\n"
6513             "ccccccccc::operator++() {\n"
6514             "  stuff();\n"
6515             "}",
6516             format("const aaaa::bbbbbbb\n"
6517                    "&ccccccccc::operator++() { stuff(); }",
6518                    getLLVMStyleWithColumns(40)));
6519 }
6520 
6521 TEST_F(FormatTest, TrailingReturnType) {
6522   verifyFormat("auto foo() -> int;\n");
6523   // correct trailing return type spacing
6524   verifyFormat("auto operator->() -> int;\n");
6525   verifyFormat("auto operator++(int) -> int;\n");
6526 
6527   verifyFormat("struct S {\n"
6528                "  auto bar() const -> int;\n"
6529                "};");
6530   verifyFormat("template <size_t Order, typename T>\n"
6531                "auto load_img(const std::string &filename)\n"
6532                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
6533   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
6534                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
6535   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
6536   verifyFormat("template <typename T>\n"
6537                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
6538                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
6539 
6540   // Not trailing return types.
6541   verifyFormat("void f() { auto a = b->c(); }");
6542 }
6543 
6544 TEST_F(FormatTest, DeductionGuides) {
6545   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
6546   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
6547   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
6548   verifyFormat(
6549       "template <class... T>\n"
6550       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
6551   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
6552   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
6553   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
6554   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
6555   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
6556   verifyFormat("template <class T> x() -> x<1>;");
6557   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
6558 
6559   // Ensure not deduction guides.
6560   verifyFormat("c()->f<int>();");
6561   verifyFormat("x()->foo<1>;");
6562   verifyFormat("x = p->foo<3>();");
6563   verifyFormat("x()->x<1>();");
6564   verifyFormat("x()->x<1>;");
6565 }
6566 
6567 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
6568   // Avoid breaking before trailing 'const' or other trailing annotations, if
6569   // they are not function-like.
6570   FormatStyle Style = getGoogleStyle();
6571   Style.ColumnLimit = 47;
6572   verifyFormat("void someLongFunction(\n"
6573                "    int someLoooooooooooooongParameter) const {\n}",
6574                getLLVMStyleWithColumns(47));
6575   verifyFormat("LoooooongReturnType\n"
6576                "someLoooooooongFunction() const {}",
6577                getLLVMStyleWithColumns(47));
6578   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
6579                "    const {}",
6580                Style);
6581   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6582                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
6583   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6584                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
6585   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6586                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
6587   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
6588                "                   aaaaaaaaaaa aaaaa) const override;");
6589   verifyGoogleFormat(
6590       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6591       "    const override;");
6592 
6593   // Even if the first parameter has to be wrapped.
6594   verifyFormat("void someLongFunction(\n"
6595                "    int someLongParameter) const {}",
6596                getLLVMStyleWithColumns(46));
6597   verifyFormat("void someLongFunction(\n"
6598                "    int someLongParameter) const {}",
6599                Style);
6600   verifyFormat("void someLongFunction(\n"
6601                "    int someLongParameter) override {}",
6602                Style);
6603   verifyFormat("void someLongFunction(\n"
6604                "    int someLongParameter) OVERRIDE {}",
6605                Style);
6606   verifyFormat("void someLongFunction(\n"
6607                "    int someLongParameter) final {}",
6608                Style);
6609   verifyFormat("void someLongFunction(\n"
6610                "    int someLongParameter) FINAL {}",
6611                Style);
6612   verifyFormat("void someLongFunction(\n"
6613                "    int parameter) const override {}",
6614                Style);
6615 
6616   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
6617   verifyFormat("void someLongFunction(\n"
6618                "    int someLongParameter) const\n"
6619                "{\n"
6620                "}",
6621                Style);
6622 
6623   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
6624   verifyFormat("void someLongFunction(\n"
6625                "    int someLongParameter) const\n"
6626                "  {\n"
6627                "  }",
6628                Style);
6629 
6630   // Unless these are unknown annotations.
6631   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
6632                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6633                "    LONG_AND_UGLY_ANNOTATION;");
6634 
6635   // Breaking before function-like trailing annotations is fine to keep them
6636   // close to their arguments.
6637   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6638                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6639   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6640                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6641   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6642                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
6643   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
6644                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
6645   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
6646 
6647   verifyFormat(
6648       "void aaaaaaaaaaaaaaaaaa()\n"
6649       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
6650       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
6651   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6652                "    __attribute__((unused));");
6653   verifyGoogleFormat(
6654       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6655       "    GUARDED_BY(aaaaaaaaaaaa);");
6656   verifyGoogleFormat(
6657       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6658       "    GUARDED_BY(aaaaaaaaaaaa);");
6659   verifyGoogleFormat(
6660       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6661       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6662   verifyGoogleFormat(
6663       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6664       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
6665 }
6666 
6667 TEST_F(FormatTest, FunctionAnnotations) {
6668   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6669                "int OldFunction(const string &parameter) {}");
6670   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6671                "string OldFunction(const string &parameter) {}");
6672   verifyFormat("template <typename T>\n"
6673                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6674                "string OldFunction(const string &parameter) {}");
6675 
6676   // Not function annotations.
6677   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6678                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
6679   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
6680                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
6681   verifyFormat("MACRO(abc).function() // wrap\n"
6682                "    << abc;");
6683   verifyFormat("MACRO(abc)->function() // wrap\n"
6684                "    << abc;");
6685   verifyFormat("MACRO(abc)::function() // wrap\n"
6686                "    << abc;");
6687 }
6688 
6689 TEST_F(FormatTest, BreaksDesireably) {
6690   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6691                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6692                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6693   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6694                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6695                "}");
6696 
6697   verifyFormat(
6698       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6699       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6700 
6701   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6702                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6703                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6704 
6705   verifyFormat(
6706       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6707       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6708       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6709       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6710       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
6711 
6712   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6713                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6714 
6715   verifyFormat(
6716       "void f() {\n"
6717       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6718       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6719       "}");
6720   verifyFormat(
6721       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6722       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6723   verifyFormat(
6724       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6725       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6726   verifyFormat(
6727       "aaaaaa(aaa,\n"
6728       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6729       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6730       "       aaaa);");
6731   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6732                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6733                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6734 
6735   // Indent consistently independent of call expression and unary operator.
6736   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6737                "    dddddddddddddddddddddddddddddd));");
6738   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6739                "    dddddddddddddddddddddddddddddd));");
6740   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
6741                "    dddddddddddddddddddddddddddddd));");
6742 
6743   // This test case breaks on an incorrect memoization, i.e. an optimization not
6744   // taking into account the StopAt value.
6745   verifyFormat(
6746       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6747       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6748       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6749       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6750 
6751   verifyFormat("{\n  {\n    {\n"
6752                "      Annotation.SpaceRequiredBefore =\n"
6753                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
6754                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
6755                "    }\n  }\n}");
6756 
6757   // Break on an outer level if there was a break on an inner level.
6758   EXPECT_EQ("f(g(h(a, // comment\n"
6759             "      b, c),\n"
6760             "    d, e),\n"
6761             "  x, y);",
6762             format("f(g(h(a, // comment\n"
6763                    "    b, c), d, e), x, y);"));
6764 
6765   // Prefer breaking similar line breaks.
6766   verifyFormat(
6767       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
6768       "                             NSTrackingMouseEnteredAndExited |\n"
6769       "                             NSTrackingActiveAlways;");
6770 }
6771 
6772 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
6773   FormatStyle NoBinPacking = getGoogleStyle();
6774   NoBinPacking.BinPackParameters = false;
6775   NoBinPacking.BinPackArguments = true;
6776   verifyFormat("void f() {\n"
6777                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
6778                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6779                "}",
6780                NoBinPacking);
6781   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
6782                "       int aaaaaaaaaaaaaaaaaaaa,\n"
6783                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6784                NoBinPacking);
6785 
6786   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6787   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6788                "                        vector<int> bbbbbbbbbbbbbbb);",
6789                NoBinPacking);
6790   // FIXME: This behavior difference is probably not wanted. However, currently
6791   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
6792   // template arguments from BreakBeforeParameter being set because of the
6793   // one-per-line formatting.
6794   verifyFormat(
6795       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6796       "                                             aaaaaaaaaa> aaaaaaaaaa);",
6797       NoBinPacking);
6798   verifyFormat(
6799       "void fffffffffff(\n"
6800       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
6801       "        aaaaaaaaaa);");
6802 }
6803 
6804 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
6805   FormatStyle NoBinPacking = getGoogleStyle();
6806   NoBinPacking.BinPackParameters = false;
6807   NoBinPacking.BinPackArguments = false;
6808   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
6809                "  aaaaaaaaaaaaaaaaaaaa,\n"
6810                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
6811                NoBinPacking);
6812   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
6813                "        aaaaaaaaaaaaa,\n"
6814                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
6815                NoBinPacking);
6816   verifyFormat(
6817       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6818       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6819       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6820       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6821       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
6822       NoBinPacking);
6823   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6824                "    .aaaaaaaaaaaaaaaaaa();",
6825                NoBinPacking);
6826   verifyFormat("void f() {\n"
6827                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6828                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
6829                "}",
6830                NoBinPacking);
6831 
6832   verifyFormat(
6833       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6834       "             aaaaaaaaaaaa,\n"
6835       "             aaaaaaaaaaaa);",
6836       NoBinPacking);
6837   verifyFormat(
6838       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
6839       "                               ddddddddddddddddddddddddddddd),\n"
6840       "             test);",
6841       NoBinPacking);
6842 
6843   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6844                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
6845                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
6846                "    aaaaaaaaaaaaaaaaaa;",
6847                NoBinPacking);
6848   verifyFormat("a(\"a\"\n"
6849                "  \"a\",\n"
6850                "  a);");
6851 
6852   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6853   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
6854                "                aaaaaaaaa,\n"
6855                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6856                NoBinPacking);
6857   verifyFormat(
6858       "void f() {\n"
6859       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6860       "      .aaaaaaa();\n"
6861       "}",
6862       NoBinPacking);
6863   verifyFormat(
6864       "template <class SomeType, class SomeOtherType>\n"
6865       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
6866       NoBinPacking);
6867 }
6868 
6869 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
6870   FormatStyle Style = getLLVMStyleWithColumns(15);
6871   Style.ExperimentalAutoDetectBinPacking = true;
6872   EXPECT_EQ("aaa(aaaa,\n"
6873             "    aaaa,\n"
6874             "    aaaa);\n"
6875             "aaa(aaaa,\n"
6876             "    aaaa,\n"
6877             "    aaaa);",
6878             format("aaa(aaaa,\n" // one-per-line
6879                    "  aaaa,\n"
6880                    "    aaaa  );\n"
6881                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
6882                    Style));
6883   EXPECT_EQ("aaa(aaaa, aaaa,\n"
6884             "    aaaa);\n"
6885             "aaa(aaaa, aaaa,\n"
6886             "    aaaa);",
6887             format("aaa(aaaa,  aaaa,\n" // bin-packed
6888                    "    aaaa  );\n"
6889                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
6890                    Style));
6891 }
6892 
6893 TEST_F(FormatTest, FormatsBuilderPattern) {
6894   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
6895                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
6896                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
6897                "    .StartsWith(\".init\", ORDER_INIT)\n"
6898                "    .StartsWith(\".fini\", ORDER_FINI)\n"
6899                "    .StartsWith(\".hash\", ORDER_HASH)\n"
6900                "    .Default(ORDER_TEXT);\n");
6901 
6902   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
6903                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
6904   verifyFormat("aaaaaaa->aaaaaaa\n"
6905                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6906                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6907                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
6908   verifyFormat(
6909       "aaaaaaa->aaaaaaa\n"
6910       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6911       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
6912   verifyFormat(
6913       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
6914       "    aaaaaaaaaaaaaa);");
6915   verifyFormat(
6916       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
6917       "    aaaaaa->aaaaaaaaaaaa()\n"
6918       "        ->aaaaaaaaaaaaaaaa(\n"
6919       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6920       "        ->aaaaaaaaaaaaaaaaa();");
6921   verifyGoogleFormat(
6922       "void f() {\n"
6923       "  someo->Add((new util::filetools::Handler(dir))\n"
6924       "                 ->OnEvent1(NewPermanentCallback(\n"
6925       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
6926       "                 ->OnEvent2(NewPermanentCallback(\n"
6927       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
6928       "                 ->OnEvent3(NewPermanentCallback(\n"
6929       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
6930       "                 ->OnEvent5(NewPermanentCallback(\n"
6931       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
6932       "                 ->OnEvent6(NewPermanentCallback(\n"
6933       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
6934       "}");
6935 
6936   verifyFormat(
6937       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
6938   verifyFormat("aaaaaaaaaaaaaaa()\n"
6939                "    .aaaaaaaaaaaaaaa()\n"
6940                "    .aaaaaaaaaaaaaaa()\n"
6941                "    .aaaaaaaaaaaaaaa()\n"
6942                "    .aaaaaaaaaaaaaaa();");
6943   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
6944                "    .aaaaaaaaaaaaaaa()\n"
6945                "    .aaaaaaaaaaaaaaa()\n"
6946                "    .aaaaaaaaaaaaaaa();");
6947   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
6948                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
6949                "    .aaaaaaaaaaaaaaa();");
6950   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
6951                "    ->aaaaaaaaaaaaaae(0)\n"
6952                "    ->aaaaaaaaaaaaaaa();");
6953 
6954   // Don't linewrap after very short segments.
6955   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6956                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6957                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6958   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6959                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6960                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6961   verifyFormat("aaa()\n"
6962                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6963                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6964                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6965 
6966   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
6967                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6968                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
6969   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
6970                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6971                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
6972 
6973   // Prefer not to break after empty parentheses.
6974   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
6975                "    First->LastNewlineOffset);");
6976 
6977   // Prefer not to create "hanging" indents.
6978   verifyFormat(
6979       "return !soooooooooooooome_map\n"
6980       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6981       "            .second;");
6982   verifyFormat(
6983       "return aaaaaaaaaaaaaaaa\n"
6984       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
6985       "    .aaaa(aaaaaaaaaaaaaa);");
6986   // No hanging indent here.
6987   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
6988                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6989   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
6990                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6991   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
6992                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6993                getLLVMStyleWithColumns(60));
6994   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
6995                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
6996                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6997                getLLVMStyleWithColumns(59));
6998   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6999                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7000                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7001 
7002   // Dont break if only closing statements before member call
7003   verifyFormat("test() {\n"
7004                "  ([]() -> {\n"
7005                "    int b = 32;\n"
7006                "    return 3;\n"
7007                "  }).foo();\n"
7008                "}");
7009   verifyFormat("test() {\n"
7010                "  (\n"
7011                "      []() -> {\n"
7012                "        int b = 32;\n"
7013                "        return 3;\n"
7014                "      },\n"
7015                "      foo, bar)\n"
7016                "      .foo();\n"
7017                "}");
7018   verifyFormat("test() {\n"
7019                "  ([]() -> {\n"
7020                "    int b = 32;\n"
7021                "    return 3;\n"
7022                "  })\n"
7023                "      .foo()\n"
7024                "      .bar();\n"
7025                "}");
7026   verifyFormat("test() {\n"
7027                "  ([]() -> {\n"
7028                "    int b = 32;\n"
7029                "    return 3;\n"
7030                "  })\n"
7031                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7032                "           \"bbbb\");\n"
7033                "}",
7034                getLLVMStyleWithColumns(30));
7035 }
7036 
7037 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7038   verifyFormat(
7039       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7040       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7041   verifyFormat(
7042       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7043       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7044 
7045   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7046                "    ccccccccccccccccccccccccc) {\n}");
7047   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7048                "    ccccccccccccccccccccccccc) {\n}");
7049 
7050   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7051                "    ccccccccccccccccccccccccc) {\n}");
7052   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7053                "    ccccccccccccccccccccccccc) {\n}");
7054 
7055   verifyFormat(
7056       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7057       "    ccccccccccccccccccccccccc) {\n}");
7058   verifyFormat(
7059       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7060       "    ccccccccccccccccccccccccc) {\n}");
7061 
7062   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7063                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7064                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7065                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7066   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7067                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7068                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7069                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7070 
7071   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7072                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7073                "    aaaaaaaaaaaaaaa != aa) {\n}");
7074   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7075                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7076                "    aaaaaaaaaaaaaaa != aa) {\n}");
7077 }
7078 
7079 TEST_F(FormatTest, BreaksAfterAssignments) {
7080   verifyFormat(
7081       "unsigned Cost =\n"
7082       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7083       "                        SI->getPointerAddressSpaceee());\n");
7084   verifyFormat(
7085       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7086       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7087 
7088   verifyFormat(
7089       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7090       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7091   verifyFormat("unsigned OriginalStartColumn =\n"
7092                "    SourceMgr.getSpellingColumnNumber(\n"
7093                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7094                "    1;");
7095 }
7096 
7097 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7098   FormatStyle Style = getLLVMStyle();
7099   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7100                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7101                Style);
7102 
7103   Style.PenaltyBreakAssignment = 20;
7104   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7105                "                                 cccccccccccccccccccccccccc;",
7106                Style);
7107 }
7108 
7109 TEST_F(FormatTest, AlignsAfterAssignments) {
7110   verifyFormat(
7111       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7112       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7113   verifyFormat(
7114       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7115       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7116   verifyFormat(
7117       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7118       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7119   verifyFormat(
7120       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7121       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7122   verifyFormat(
7123       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7124       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7125       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7126 }
7127 
7128 TEST_F(FormatTest, AlignsAfterReturn) {
7129   verifyFormat(
7130       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7131       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7132   verifyFormat(
7133       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7134       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7135   verifyFormat(
7136       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7137       "       aaaaaaaaaaaaaaaaaaaaaa();");
7138   verifyFormat(
7139       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7140       "        aaaaaaaaaaaaaaaaaaaaaa());");
7141   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7142                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7143   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7144                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7145                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7146   verifyFormat("return\n"
7147                "    // true if code is one of a or b.\n"
7148                "    code == a || code == b;");
7149 }
7150 
7151 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7152   verifyFormat(
7153       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7154       "                                                aaaaaaaaa aaaaaaa) {}");
7155   verifyFormat(
7156       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7157       "                                               aaaaaaaaaaa aaaaaaaaa);");
7158   verifyFormat(
7159       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7160       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7161   FormatStyle Style = getLLVMStyle();
7162   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7163   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7164                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7165                Style);
7166   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7167                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7168                Style);
7169   verifyFormat("SomeLongVariableName->someFunction(\n"
7170                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7171                Style);
7172   verifyFormat(
7173       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7174       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7175       Style);
7176   verifyFormat(
7177       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7178       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7179       Style);
7180   verifyFormat(
7181       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7182       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7183       Style);
7184 
7185   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7186                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7187                "        b));",
7188                Style);
7189 
7190   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7191   Style.BinPackArguments = false;
7192   Style.BinPackParameters = false;
7193   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7194                "    aaaaaaaaaaa aaaaaaaa,\n"
7195                "    aaaaaaaaa aaaaaaa,\n"
7196                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7197                Style);
7198   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7199                "    aaaaaaaaaaa aaaaaaaaa,\n"
7200                "    aaaaaaaaaaa aaaaaaaaa,\n"
7201                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7202                Style);
7203   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7204                "    aaaaaaaaaaaaaaa,\n"
7205                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7206                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7207                Style);
7208   verifyFormat(
7209       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7210       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7211       Style);
7212   verifyFormat(
7213       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7214       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7215       Style);
7216   verifyFormat(
7217       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7218       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7219       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7220       "    aaaaaaaaaaaaaaaa);",
7221       Style);
7222   verifyFormat(
7223       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7224       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7225       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7226       "    aaaaaaaaaaaaaaaa);",
7227       Style);
7228 }
7229 
7230 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7231   FormatStyle Style = getLLVMStyleWithColumns(40);
7232   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7233                "          bbbbbbbbbbbbbbbbbbbbbb);",
7234                Style);
7235   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7236   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7237   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7238                "          bbbbbbbbbbbbbbbbbbbbbb);",
7239                Style);
7240   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7241   Style.AlignOperands = FormatStyle::OAS_Align;
7242   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7243                "          bbbbbbbbbbbbbbbbbbbbbb);",
7244                Style);
7245   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7246   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7247   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7248                "    bbbbbbbbbbbbbbbbbbbbbb);",
7249                Style);
7250 }
7251 
7252 TEST_F(FormatTest, BreaksConditionalExpressions) {
7253   verifyFormat(
7254       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7255       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7256       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7257   verifyFormat(
7258       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7259       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7260       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7261   verifyFormat(
7262       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7263       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7264   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7265                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7266                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7267   verifyFormat(
7268       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7269       "                                                    : aaaaaaaaaaaaa);");
7270   verifyFormat(
7271       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7272       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7273       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7274       "                   aaaaaaaaaaaaa);");
7275   verifyFormat(
7276       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7277       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7278       "                   aaaaaaaaaaaaa);");
7279   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7280                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7281                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7282                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7283                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7284   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7285                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7286                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7287                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7288                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7289                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7290                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7291   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7292                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7293                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7294                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7295                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7296   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7297                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7298                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7299   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7300                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7301                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7302                "        : aaaaaaaaaaaaaaaa;");
7303   verifyFormat(
7304       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7305       "    ? aaaaaaaaaaaaaaa\n"
7306       "    : aaaaaaaaaaaaaaa;");
7307   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7308                "          aaaaaaaaa\n"
7309                "      ? b\n"
7310                "      : c);");
7311   verifyFormat("return aaaa == bbbb\n"
7312                "           // comment\n"
7313                "           ? aaaa\n"
7314                "           : bbbb;");
7315   verifyFormat("unsigned Indent =\n"
7316                "    format(TheLine.First,\n"
7317                "           IndentForLevel[TheLine.Level] >= 0\n"
7318                "               ? IndentForLevel[TheLine.Level]\n"
7319                "               : TheLine * 2,\n"
7320                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7321                getLLVMStyleWithColumns(60));
7322   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7323                "                  ? aaaaaaaaaaaaaaa\n"
7324                "                  : bbbbbbbbbbbbbbb //\n"
7325                "                        ? ccccccccccccccc\n"
7326                "                        : ddddddddddddddd;");
7327   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7328                "                  ? aaaaaaaaaaaaaaa\n"
7329                "                  : (bbbbbbbbbbbbbbb //\n"
7330                "                         ? ccccccccccccccc\n"
7331                "                         : ddddddddddddddd);");
7332   verifyFormat(
7333       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7334       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7335       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7336       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7337       "                                      : aaaaaaaaaa;");
7338   verifyFormat(
7339       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7340       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7341       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7342 
7343   FormatStyle NoBinPacking = getLLVMStyle();
7344   NoBinPacking.BinPackArguments = false;
7345   verifyFormat(
7346       "void f() {\n"
7347       "  g(aaa,\n"
7348       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7349       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7350       "        ? aaaaaaaaaaaaaaa\n"
7351       "        : aaaaaaaaaaaaaaa);\n"
7352       "}",
7353       NoBinPacking);
7354   verifyFormat(
7355       "void f() {\n"
7356       "  g(aaa,\n"
7357       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7358       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7359       "        ?: aaaaaaaaaaaaaaa);\n"
7360       "}",
7361       NoBinPacking);
7362 
7363   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7364                "             // comment.\n"
7365                "             ccccccccccccccccccccccccccccccccccccccc\n"
7366                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7367                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7368 
7369   // Assignments in conditional expressions. Apparently not uncommon :-(.
7370   verifyFormat("return a != b\n"
7371                "           // comment\n"
7372                "           ? a = b\n"
7373                "           : a = b;");
7374   verifyFormat("return a != b\n"
7375                "           // comment\n"
7376                "           ? a = a != b\n"
7377                "                     // comment\n"
7378                "                     ? a = b\n"
7379                "                     : a\n"
7380                "           : a;\n");
7381   verifyFormat("return a != b\n"
7382                "           // comment\n"
7383                "           ? a\n"
7384                "           : a = a != b\n"
7385                "                     // comment\n"
7386                "                     ? a = b\n"
7387                "                     : a;");
7388 
7389   // Chained conditionals
7390   FormatStyle Style = getLLVMStyle();
7391   Style.ColumnLimit = 70;
7392   Style.AlignOperands = FormatStyle::OAS_Align;
7393   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7394                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7395                "                        : 3333333333333333;",
7396                Style);
7397   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7398                "       : bbbbbbbbbb     ? 2222222222222222\n"
7399                "                        : 3333333333333333;",
7400                Style);
7401   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7402                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7403                "                          : 3333333333333333;",
7404                Style);
7405   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7406                "       : bbbbbbbbbbbbbb ? 222222\n"
7407                "                        : 333333;",
7408                Style);
7409   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7410                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7411                "       : cccccccccccccc ? 3333333333333333\n"
7412                "                        : 4444444444444444;",
7413                Style);
7414   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7415                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7416                "                        : 3333333333333333;",
7417                Style);
7418   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7419                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7420                "                        : (aaa ? bbb : ccc);",
7421                Style);
7422   verifyFormat(
7423       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7424       "                                             : cccccccccccccccccc)\n"
7425       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7426       "                        : 3333333333333333;",
7427       Style);
7428   verifyFormat(
7429       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7430       "                                             : cccccccccccccccccc)\n"
7431       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7432       "                        : 3333333333333333;",
7433       Style);
7434   verifyFormat(
7435       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7436       "                                             : dddddddddddddddddd)\n"
7437       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7438       "                        : 3333333333333333;",
7439       Style);
7440   verifyFormat(
7441       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7442       "                                             : dddddddddddddddddd)\n"
7443       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7444       "                        : 3333333333333333;",
7445       Style);
7446   verifyFormat(
7447       "return aaaaaaaaa        ? 1111111111111111\n"
7448       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7449       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7450       "                                             : dddddddddddddddddd)\n",
7451       Style);
7452   verifyFormat(
7453       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7454       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7455       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7456       "                                             : cccccccccccccccccc);",
7457       Style);
7458   verifyFormat(
7459       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7460       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7461       "                                             : eeeeeeeeeeeeeeeeee)\n"
7462       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7463       "                        : 3333333333333333;",
7464       Style);
7465   verifyFormat(
7466       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
7467       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7468       "                                             : eeeeeeeeeeeeeeeeee)\n"
7469       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7470       "                        : 3333333333333333;",
7471       Style);
7472   verifyFormat(
7473       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7474       "                           : cccccccccccc    ? dddddddddddddddddd\n"
7475       "                                             : eeeeeeeeeeeeeeeeee)\n"
7476       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7477       "                        : 3333333333333333;",
7478       Style);
7479   verifyFormat(
7480       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7481       "                                             : cccccccccccccccccc\n"
7482       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7483       "                        : 3333333333333333;",
7484       Style);
7485   verifyFormat(
7486       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7487       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
7488       "                                             : eeeeeeeeeeeeeeeeee\n"
7489       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7490       "                        : 3333333333333333;",
7491       Style);
7492   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
7493                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
7494                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
7495                "                                   : eeeeeeeeeeeeeeeeee)\n"
7496                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7497                "                             : 3333333333333333;",
7498                Style);
7499   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7500                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7501                "             : cccccccccccccccc ? dddddddddddddddddd\n"
7502                "                                : eeeeeeeeeeeeeeeeee\n"
7503                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7504                "                                 : 3333333333333333;",
7505                Style);
7506 
7507   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7508   Style.BreakBeforeTernaryOperators = false;
7509   // FIXME: Aligning the question marks is weird given DontAlign.
7510   // Consider disabling this alignment in this case. Also check whether this
7511   // will render the adjustment from https://reviews.llvm.org/D82199
7512   // unnecessary.
7513   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
7514                "    bbbb                ? cccccccccccccccccc :\n"
7515                "                          ddddd;\n",
7516                Style);
7517 
7518   EXPECT_EQ(
7519       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7520       "    /*\n"
7521       "     */\n"
7522       "    function() {\n"
7523       "      try {\n"
7524       "        return JJJJJJJJJJJJJJ(\n"
7525       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7526       "      }\n"
7527       "    } :\n"
7528       "    function() {};",
7529       format(
7530           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7531           "     /*\n"
7532           "      */\n"
7533           "     function() {\n"
7534           "      try {\n"
7535           "        return JJJJJJJJJJJJJJ(\n"
7536           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7537           "      }\n"
7538           "    } :\n"
7539           "    function() {};",
7540           getGoogleStyle(FormatStyle::LK_JavaScript)));
7541 }
7542 
7543 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
7544   FormatStyle Style = getLLVMStyle();
7545   Style.BreakBeforeTernaryOperators = false;
7546   Style.ColumnLimit = 70;
7547   verifyFormat(
7548       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7549       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7550       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7551       Style);
7552   verifyFormat(
7553       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7554       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7555       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7556       Style);
7557   verifyFormat(
7558       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7559       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7560       Style);
7561   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
7562                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7563                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7564                Style);
7565   verifyFormat(
7566       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
7567       "                                                      aaaaaaaaaaaaa);",
7568       Style);
7569   verifyFormat(
7570       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7571       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7572       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7573       "                   aaaaaaaaaaaaa);",
7574       Style);
7575   verifyFormat(
7576       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7577       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7578       "                   aaaaaaaaaaaaa);",
7579       Style);
7580   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7581                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7582                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7583                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7584                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7585                Style);
7586   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7587                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7588                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7589                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7590                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7591                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7592                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7593                Style);
7594   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7595                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
7596                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7597                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7598                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7599                Style);
7600   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7601                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7602                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7603                Style);
7604   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7605                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7606                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7607                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7608                Style);
7609   verifyFormat(
7610       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7611       "    aaaaaaaaaaaaaaa :\n"
7612       "    aaaaaaaaaaaaaaa;",
7613       Style);
7614   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7615                "          aaaaaaaaa ?\n"
7616                "      b :\n"
7617                "      c);",
7618                Style);
7619   verifyFormat("unsigned Indent =\n"
7620                "    format(TheLine.First,\n"
7621                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
7622                "               IndentForLevel[TheLine.Level] :\n"
7623                "               TheLine * 2,\n"
7624                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7625                Style);
7626   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7627                "                  aaaaaaaaaaaaaaa :\n"
7628                "                  bbbbbbbbbbbbbbb ? //\n"
7629                "                      ccccccccccccccc :\n"
7630                "                      ddddddddddddddd;",
7631                Style);
7632   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7633                "                  aaaaaaaaaaaaaaa :\n"
7634                "                  (bbbbbbbbbbbbbbb ? //\n"
7635                "                       ccccccccccccccc :\n"
7636                "                       ddddddddddddddd);",
7637                Style);
7638   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7639                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
7640                "            ccccccccccccccccccccccccccc;",
7641                Style);
7642   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7643                "           aaaaa :\n"
7644                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
7645                Style);
7646 
7647   // Chained conditionals
7648   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7649                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7650                "                          3333333333333333;",
7651                Style);
7652   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7653                "       bbbbbbbbbb       ? 2222222222222222 :\n"
7654                "                          3333333333333333;",
7655                Style);
7656   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
7657                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7658                "                          3333333333333333;",
7659                Style);
7660   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7661                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
7662                "                          333333;",
7663                Style);
7664   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7665                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7666                "       cccccccccccccccc ? 3333333333333333 :\n"
7667                "                          4444444444444444;",
7668                Style);
7669   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
7670                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7671                "                          3333333333333333;",
7672                Style);
7673   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7674                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7675                "                          (aaa ? bbb : ccc);",
7676                Style);
7677   verifyFormat(
7678       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7679       "                                               cccccccccccccccccc) :\n"
7680       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7681       "                          3333333333333333;",
7682       Style);
7683   verifyFormat(
7684       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7685       "                                               cccccccccccccccccc) :\n"
7686       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7687       "                          3333333333333333;",
7688       Style);
7689   verifyFormat(
7690       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7691       "                                               dddddddddddddddddd) :\n"
7692       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7693       "                          3333333333333333;",
7694       Style);
7695   verifyFormat(
7696       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7697       "                                               dddddddddddddddddd) :\n"
7698       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7699       "                          3333333333333333;",
7700       Style);
7701   verifyFormat(
7702       "return aaaaaaaaa        ? 1111111111111111 :\n"
7703       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7704       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7705       "                                               dddddddddddddddddd)\n",
7706       Style);
7707   verifyFormat(
7708       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7709       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7710       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7711       "                                               cccccccccccccccccc);",
7712       Style);
7713   verifyFormat(
7714       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7715       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7716       "                                               eeeeeeeeeeeeeeeeee) :\n"
7717       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7718       "                          3333333333333333;",
7719       Style);
7720   verifyFormat(
7721       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7722       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
7723       "                                               eeeeeeeeeeeeeeeeee) :\n"
7724       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7725       "                          3333333333333333;",
7726       Style);
7727   verifyFormat(
7728       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
7729       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7730       "                                               eeeeeeeeeeeeeeeeee) :\n"
7731       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7732       "                          3333333333333333;",
7733       Style);
7734   verifyFormat(
7735       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7736       "                                               cccccccccccccccccc :\n"
7737       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7738       "                          3333333333333333;",
7739       Style);
7740   verifyFormat(
7741       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7742       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
7743       "                                               eeeeeeeeeeeeeeeeee :\n"
7744       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7745       "                          3333333333333333;",
7746       Style);
7747   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7748                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7749                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
7750                "                                 eeeeeeeeeeeeeeeeee) :\n"
7751                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7752                "                               3333333333333333;",
7753                Style);
7754   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7755                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7756                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
7757                "                                  eeeeeeeeeeeeeeeeee :\n"
7758                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7759                "                               3333333333333333;",
7760                Style);
7761 }
7762 
7763 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
7764   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
7765                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
7766   verifyFormat("bool a = true, b = false;");
7767 
7768   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7769                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
7770                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
7771                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
7772   verifyFormat(
7773       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7774       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
7775       "     d = e && f;");
7776   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
7777                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
7778   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7779                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
7780   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
7781                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
7782 
7783   FormatStyle Style = getGoogleStyle();
7784   Style.PointerAlignment = FormatStyle::PAS_Left;
7785   Style.DerivePointerAlignment = false;
7786   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7787                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
7788                "    *b = bbbbbbbbbbbbbbbbbbb;",
7789                Style);
7790   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7791                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
7792                Style);
7793   verifyFormat("vector<int*> a, b;", Style);
7794   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
7795 }
7796 
7797 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
7798   verifyFormat("arr[foo ? bar : baz];");
7799   verifyFormat("f()[foo ? bar : baz];");
7800   verifyFormat("(a + b)[foo ? bar : baz];");
7801   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
7802 }
7803 
7804 TEST_F(FormatTest, AlignsStringLiterals) {
7805   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
7806                "                                      \"short literal\");");
7807   verifyFormat(
7808       "looooooooooooooooooooooooongFunction(\n"
7809       "    \"short literal\"\n"
7810       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
7811   verifyFormat("someFunction(\"Always break between multi-line\"\n"
7812                "             \" string literals\",\n"
7813                "             and, other, parameters);");
7814   EXPECT_EQ("fun + \"1243\" /* comment */\n"
7815             "      \"5678\";",
7816             format("fun + \"1243\" /* comment */\n"
7817                    "    \"5678\";",
7818                    getLLVMStyleWithColumns(28)));
7819   EXPECT_EQ(
7820       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7821       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
7822       "         \"aaaaaaaaaaaaaaaa\";",
7823       format("aaaaaa ="
7824              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7825              "aaaaaaaaaaaaaaaaaaaaa\" "
7826              "\"aaaaaaaaaaaaaaaa\";"));
7827   verifyFormat("a = a + \"a\"\n"
7828                "        \"a\"\n"
7829                "        \"a\";");
7830   verifyFormat("f(\"a\", \"b\"\n"
7831                "       \"c\");");
7832 
7833   verifyFormat(
7834       "#define LL_FORMAT \"ll\"\n"
7835       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
7836       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
7837 
7838   verifyFormat("#define A(X)          \\\n"
7839                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
7840                "  \"ccccc\"",
7841                getLLVMStyleWithColumns(23));
7842   verifyFormat("#define A \"def\"\n"
7843                "f(\"abc\" A \"ghi\"\n"
7844                "  \"jkl\");");
7845 
7846   verifyFormat("f(L\"a\"\n"
7847                "  L\"b\");");
7848   verifyFormat("#define A(X)            \\\n"
7849                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
7850                "  L\"ccccc\"",
7851                getLLVMStyleWithColumns(25));
7852 
7853   verifyFormat("f(@\"a\"\n"
7854                "  @\"b\");");
7855   verifyFormat("NSString s = @\"a\"\n"
7856                "             @\"b\"\n"
7857                "             @\"c\";");
7858   verifyFormat("NSString s = @\"a\"\n"
7859                "              \"b\"\n"
7860                "              \"c\";");
7861 }
7862 
7863 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
7864   FormatStyle Style = getLLVMStyle();
7865   // No declarations or definitions should be moved to own line.
7866   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
7867   verifyFormat("class A {\n"
7868                "  int f() { return 1; }\n"
7869                "  int g();\n"
7870                "};\n"
7871                "int f() { return 1; }\n"
7872                "int g();\n",
7873                Style);
7874 
7875   // All declarations and definitions should have the return type moved to its
7876   // own line.
7877   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
7878   Style.TypenameMacros = {"LIST"};
7879   verifyFormat("SomeType\n"
7880                "funcdecl(LIST(uint64_t));",
7881                Style);
7882   verifyFormat("class E {\n"
7883                "  int\n"
7884                "  f() {\n"
7885                "    return 1;\n"
7886                "  }\n"
7887                "  int\n"
7888                "  g();\n"
7889                "};\n"
7890                "int\n"
7891                "f() {\n"
7892                "  return 1;\n"
7893                "}\n"
7894                "int\n"
7895                "g();\n",
7896                Style);
7897 
7898   // Top-level definitions, and no kinds of declarations should have the
7899   // return type moved to its own line.
7900   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
7901   verifyFormat("class B {\n"
7902                "  int f() { return 1; }\n"
7903                "  int g();\n"
7904                "};\n"
7905                "int\n"
7906                "f() {\n"
7907                "  return 1;\n"
7908                "}\n"
7909                "int g();\n",
7910                Style);
7911 
7912   // Top-level definitions and declarations should have the return type moved
7913   // to its own line.
7914   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
7915   verifyFormat("class C {\n"
7916                "  int f() { return 1; }\n"
7917                "  int g();\n"
7918                "};\n"
7919                "int\n"
7920                "f() {\n"
7921                "  return 1;\n"
7922                "}\n"
7923                "int\n"
7924                "g();\n",
7925                Style);
7926 
7927   // All definitions should have the return type moved to its own line, but no
7928   // kinds of declarations.
7929   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
7930   verifyFormat("class D {\n"
7931                "  int\n"
7932                "  f() {\n"
7933                "    return 1;\n"
7934                "  }\n"
7935                "  int g();\n"
7936                "};\n"
7937                "int\n"
7938                "f() {\n"
7939                "  return 1;\n"
7940                "}\n"
7941                "int g();\n",
7942                Style);
7943   verifyFormat("const char *\n"
7944                "f(void) {\n" // Break here.
7945                "  return \"\";\n"
7946                "}\n"
7947                "const char *bar(void);\n", // No break here.
7948                Style);
7949   verifyFormat("template <class T>\n"
7950                "T *\n"
7951                "f(T &c) {\n" // Break here.
7952                "  return NULL;\n"
7953                "}\n"
7954                "template <class T> T *f(T &c);\n", // No break here.
7955                Style);
7956   verifyFormat("class C {\n"
7957                "  int\n"
7958                "  operator+() {\n"
7959                "    return 1;\n"
7960                "  }\n"
7961                "  int\n"
7962                "  operator()() {\n"
7963                "    return 1;\n"
7964                "  }\n"
7965                "};\n",
7966                Style);
7967   verifyFormat("void\n"
7968                "A::operator()() {}\n"
7969                "void\n"
7970                "A::operator>>() {}\n"
7971                "void\n"
7972                "A::operator+() {}\n"
7973                "void\n"
7974                "A::operator*() {}\n"
7975                "void\n"
7976                "A::operator->() {}\n"
7977                "void\n"
7978                "A::operator void *() {}\n"
7979                "void\n"
7980                "A::operator void &() {}\n"
7981                "void\n"
7982                "A::operator void &&() {}\n"
7983                "void\n"
7984                "A::operator char *() {}\n"
7985                "void\n"
7986                "A::operator[]() {}\n"
7987                "void\n"
7988                "A::operator!() {}\n"
7989                "void\n"
7990                "A::operator**() {}\n"
7991                "void\n"
7992                "A::operator<Foo> *() {}\n"
7993                "void\n"
7994                "A::operator<Foo> **() {}\n"
7995                "void\n"
7996                "A::operator<Foo> &() {}\n"
7997                "void\n"
7998                "A::operator void **() {}\n",
7999                Style);
8000   verifyFormat("constexpr auto\n"
8001                "operator()() const -> reference {}\n"
8002                "constexpr auto\n"
8003                "operator>>() const -> reference {}\n"
8004                "constexpr auto\n"
8005                "operator+() const -> reference {}\n"
8006                "constexpr auto\n"
8007                "operator*() const -> reference {}\n"
8008                "constexpr auto\n"
8009                "operator->() const -> reference {}\n"
8010                "constexpr auto\n"
8011                "operator++() const -> reference {}\n"
8012                "constexpr auto\n"
8013                "operator void *() const -> reference {}\n"
8014                "constexpr auto\n"
8015                "operator void **() const -> reference {}\n"
8016                "constexpr auto\n"
8017                "operator void *() const -> reference {}\n"
8018                "constexpr auto\n"
8019                "operator void &() const -> reference {}\n"
8020                "constexpr auto\n"
8021                "operator void &&() const -> reference {}\n"
8022                "constexpr auto\n"
8023                "operator char *() const -> reference {}\n"
8024                "constexpr auto\n"
8025                "operator!() const -> reference {}\n"
8026                "constexpr auto\n"
8027                "operator[]() const -> reference {}\n",
8028                Style);
8029   verifyFormat("void *operator new(std::size_t s);", // No break here.
8030                Style);
8031   verifyFormat("void *\n"
8032                "operator new(std::size_t s) {}",
8033                Style);
8034   verifyFormat("void *\n"
8035                "operator delete[](void *ptr) {}",
8036                Style);
8037   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8038   verifyFormat("const char *\n"
8039                "f(void)\n" // Break here.
8040                "{\n"
8041                "  return \"\";\n"
8042                "}\n"
8043                "const char *bar(void);\n", // No break here.
8044                Style);
8045   verifyFormat("template <class T>\n"
8046                "T *\n"     // Problem here: no line break
8047                "f(T &c)\n" // Break here.
8048                "{\n"
8049                "  return NULL;\n"
8050                "}\n"
8051                "template <class T> T *f(T &c);\n", // No break here.
8052                Style);
8053   verifyFormat("int\n"
8054                "foo(A<bool> a)\n"
8055                "{\n"
8056                "  return a;\n"
8057                "}\n",
8058                Style);
8059   verifyFormat("int\n"
8060                "foo(A<8> a)\n"
8061                "{\n"
8062                "  return a;\n"
8063                "}\n",
8064                Style);
8065   verifyFormat("int\n"
8066                "foo(A<B<bool>, 8> a)\n"
8067                "{\n"
8068                "  return a;\n"
8069                "}\n",
8070                Style);
8071   verifyFormat("int\n"
8072                "foo(A<B<8>, bool> a)\n"
8073                "{\n"
8074                "  return a;\n"
8075                "}\n",
8076                Style);
8077   verifyFormat("int\n"
8078                "foo(A<B<bool>, bool> a)\n"
8079                "{\n"
8080                "  return a;\n"
8081                "}\n",
8082                Style);
8083   verifyFormat("int\n"
8084                "foo(A<B<8>, 8> a)\n"
8085                "{\n"
8086                "  return a;\n"
8087                "}\n",
8088                Style);
8089 
8090   Style = getGNUStyle();
8091 
8092   // Test for comments at the end of function declarations.
8093   verifyFormat("void\n"
8094                "foo (int a, /*abc*/ int b) // def\n"
8095                "{\n"
8096                "}\n",
8097                Style);
8098 
8099   verifyFormat("void\n"
8100                "foo (int a, /* abc */ int b) /* def */\n"
8101                "{\n"
8102                "}\n",
8103                Style);
8104 
8105   // Definitions that should not break after return type
8106   verifyFormat("void foo (int a, int b); // def\n", Style);
8107   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8108   verifyFormat("void foo (int a, int b);\n", Style);
8109 }
8110 
8111 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8112   FormatStyle NoBreak = getLLVMStyle();
8113   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8114   FormatStyle Break = getLLVMStyle();
8115   Break.AlwaysBreakBeforeMultilineStrings = true;
8116   verifyFormat("aaaa = \"bbbb\"\n"
8117                "       \"cccc\";",
8118                NoBreak);
8119   verifyFormat("aaaa =\n"
8120                "    \"bbbb\"\n"
8121                "    \"cccc\";",
8122                Break);
8123   verifyFormat("aaaa(\"bbbb\"\n"
8124                "     \"cccc\");",
8125                NoBreak);
8126   verifyFormat("aaaa(\n"
8127                "    \"bbbb\"\n"
8128                "    \"cccc\");",
8129                Break);
8130   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8131                "          \"cccc\");",
8132                NoBreak);
8133   verifyFormat("aaaa(qqq,\n"
8134                "     \"bbbb\"\n"
8135                "     \"cccc\");",
8136                Break);
8137   verifyFormat("aaaa(qqq,\n"
8138                "     L\"bbbb\"\n"
8139                "     L\"cccc\");",
8140                Break);
8141   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8142                "                      \"bbbb\"));",
8143                Break);
8144   verifyFormat("string s = someFunction(\n"
8145                "    \"abc\"\n"
8146                "    \"abc\");",
8147                Break);
8148 
8149   // As we break before unary operators, breaking right after them is bad.
8150   verifyFormat("string foo = abc ? \"x\"\n"
8151                "                   \"blah blah blah blah blah blah\"\n"
8152                "                 : \"y\";",
8153                Break);
8154 
8155   // Don't break if there is no column gain.
8156   verifyFormat("f(\"aaaa\"\n"
8157                "  \"bbbb\");",
8158                Break);
8159 
8160   // Treat literals with escaped newlines like multi-line string literals.
8161   EXPECT_EQ("x = \"a\\\n"
8162             "b\\\n"
8163             "c\";",
8164             format("x = \"a\\\n"
8165                    "b\\\n"
8166                    "c\";",
8167                    NoBreak));
8168   EXPECT_EQ("xxxx =\n"
8169             "    \"a\\\n"
8170             "b\\\n"
8171             "c\";",
8172             format("xxxx = \"a\\\n"
8173                    "b\\\n"
8174                    "c\";",
8175                    Break));
8176 
8177   EXPECT_EQ("NSString *const kString =\n"
8178             "    @\"aaaa\"\n"
8179             "    @\"bbbb\";",
8180             format("NSString *const kString = @\"aaaa\"\n"
8181                    "@\"bbbb\";",
8182                    Break));
8183 
8184   Break.ColumnLimit = 0;
8185   verifyFormat("const char *hello = \"hello llvm\";", Break);
8186 }
8187 
8188 TEST_F(FormatTest, AlignsPipes) {
8189   verifyFormat(
8190       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8191       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8192       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8193   verifyFormat(
8194       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8195       "                     << aaaaaaaaaaaaaaaaaaaa;");
8196   verifyFormat(
8197       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8198       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8199   verifyFormat(
8200       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8201       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8202   verifyFormat(
8203       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8204       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8205       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8206   verifyFormat(
8207       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8208       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8209       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8210   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8211                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8212                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8213                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8214   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8215                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8216   verifyFormat(
8217       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8218       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8219   verifyFormat(
8220       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8221       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8222 
8223   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8224                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8225   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8226                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8227                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8228                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8229   verifyFormat("LOG_IF(aaa == //\n"
8230                "       bbb)\n"
8231                "    << a << b;");
8232 
8233   // But sometimes, breaking before the first "<<" is desirable.
8234   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8235                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8236   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8237                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8238                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8239   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8240                "    << BEF << IsTemplate << Description << E->getType();");
8241   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8242                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8243                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8244   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8245                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8246                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8247                "    << aaa;");
8248 
8249   verifyFormat(
8250       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8251       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8252 
8253   // Incomplete string literal.
8254   EXPECT_EQ("llvm::errs() << \"\n"
8255             "             << a;",
8256             format("llvm::errs() << \"\n<<a;"));
8257 
8258   verifyFormat("void f() {\n"
8259                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8260                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8261                "}");
8262 
8263   // Handle 'endl'.
8264   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8265                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8266   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8267 
8268   // Handle '\n'.
8269   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8270                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8271   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8272                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8273   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8274                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8275   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8276 }
8277 
8278 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8279   verifyFormat("return out << \"somepacket = {\\n\"\n"
8280                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8281                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8282                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8283                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8284                "           << \"}\";");
8285 
8286   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8287                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8288                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8289   verifyFormat(
8290       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8291       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8292       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8293       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8294       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8295   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8296                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8297   verifyFormat(
8298       "void f() {\n"
8299       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8300       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8301       "}");
8302 
8303   // Breaking before the first "<<" is generally not desirable.
8304   verifyFormat(
8305       "llvm::errs()\n"
8306       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8307       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8308       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8309       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8310       getLLVMStyleWithColumns(70));
8311   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8312                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8313                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8314                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8315                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8316                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8317                getLLVMStyleWithColumns(70));
8318 
8319   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8320                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8321                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8322   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8323                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8324                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8325   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8326                "           (aaaa + aaaa);",
8327                getLLVMStyleWithColumns(40));
8328   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8329                "                  (aaaaaaa + aaaaa));",
8330                getLLVMStyleWithColumns(40));
8331   verifyFormat(
8332       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8333       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8334       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8335 }
8336 
8337 TEST_F(FormatTest, UnderstandsEquals) {
8338   verifyFormat(
8339       "aaaaaaaaaaaaaaaaa =\n"
8340       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8341   verifyFormat(
8342       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8343       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8344   verifyFormat(
8345       "if (a) {\n"
8346       "  f();\n"
8347       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8348       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8349       "}");
8350 
8351   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8352                "        100000000 + 10000000) {\n}");
8353 }
8354 
8355 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8356   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8357                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8358 
8359   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8360                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8361 
8362   verifyFormat(
8363       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8364       "                                                          Parameter2);");
8365 
8366   verifyFormat(
8367       "ShortObject->shortFunction(\n"
8368       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8369       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8370 
8371   verifyFormat("loooooooooooooongFunction(\n"
8372                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8373 
8374   verifyFormat(
8375       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8376       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8377 
8378   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8379                "    .WillRepeatedly(Return(SomeValue));");
8380   verifyFormat("void f() {\n"
8381                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8382                "      .Times(2)\n"
8383                "      .WillRepeatedly(Return(SomeValue));\n"
8384                "}");
8385   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
8386                "    ccccccccccccccccccccccc);");
8387   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8388                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8389                "          .aaaaa(aaaaa),\n"
8390                "      aaaaaaaaaaaaaaaaaaaaa);");
8391   verifyFormat("void f() {\n"
8392                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8393                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
8394                "}");
8395   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8396                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8397                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8398                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8399                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8400   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8401                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8402                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8403                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
8404                "}");
8405 
8406   // Here, it is not necessary to wrap at "." or "->".
8407   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
8408                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8409   verifyFormat(
8410       "aaaaaaaaaaa->aaaaaaaaa(\n"
8411       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8412       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
8413 
8414   verifyFormat(
8415       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8416       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
8417   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
8418                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8419   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
8420                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8421 
8422   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8423                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8424                "    .a();");
8425 
8426   FormatStyle NoBinPacking = getLLVMStyle();
8427   NoBinPacking.BinPackParameters = false;
8428   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8429                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8430                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
8431                "                         aaaaaaaaaaaaaaaaaaa,\n"
8432                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8433                NoBinPacking);
8434 
8435   // If there is a subsequent call, change to hanging indentation.
8436   verifyFormat(
8437       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8438       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
8439       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8440   verifyFormat(
8441       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8442       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
8443   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8444                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8445                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8446   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8447                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8448                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8449 }
8450 
8451 TEST_F(FormatTest, WrapsTemplateDeclarations) {
8452   verifyFormat("template <typename T>\n"
8453                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8454   verifyFormat("template <typename T>\n"
8455                "// T should be one of {A, B}.\n"
8456                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8457   verifyFormat(
8458       "template <typename T>\n"
8459       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
8460   verifyFormat("template <typename T>\n"
8461                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
8462                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
8463   verifyFormat(
8464       "template <typename T>\n"
8465       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
8466       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
8467   verifyFormat(
8468       "template <typename T>\n"
8469       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
8470       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
8471       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8472   verifyFormat("template <typename T>\n"
8473                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8474                "    int aaaaaaaaaaaaaaaaaaaaaa);");
8475   verifyFormat(
8476       "template <typename T1, typename T2 = char, typename T3 = char,\n"
8477       "          typename T4 = char>\n"
8478       "void f();");
8479   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
8480                "          template <typename> class cccccccccccccccccccccc,\n"
8481                "          typename ddddddddddddd>\n"
8482                "class C {};");
8483   verifyFormat(
8484       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
8485       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8486 
8487   verifyFormat("void f() {\n"
8488                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
8489                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
8490                "}");
8491 
8492   verifyFormat("template <typename T> class C {};");
8493   verifyFormat("template <typename T> void f();");
8494   verifyFormat("template <typename T> void f() {}");
8495   verifyFormat(
8496       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8497       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8498       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
8499       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8500       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8501       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
8502       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
8503       getLLVMStyleWithColumns(72));
8504   EXPECT_EQ("static_cast<A< //\n"
8505             "    B> *>(\n"
8506             "\n"
8507             ");",
8508             format("static_cast<A<//\n"
8509                    "    B>*>(\n"
8510                    "\n"
8511                    "    );"));
8512   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8513                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
8514 
8515   FormatStyle AlwaysBreak = getLLVMStyle();
8516   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8517   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
8518   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
8519   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
8520   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8521                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8522                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
8523   verifyFormat("template <template <typename> class Fooooooo,\n"
8524                "          template <typename> class Baaaaaaar>\n"
8525                "struct C {};",
8526                AlwaysBreak);
8527   verifyFormat("template <typename T> // T can be A, B or C.\n"
8528                "struct C {};",
8529                AlwaysBreak);
8530   verifyFormat("template <enum E> class A {\n"
8531                "public:\n"
8532                "  E *f();\n"
8533                "};");
8534 
8535   FormatStyle NeverBreak = getLLVMStyle();
8536   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
8537   verifyFormat("template <typename T> class C {};", NeverBreak);
8538   verifyFormat("template <typename T> void f();", NeverBreak);
8539   verifyFormat("template <typename T> void f() {}", NeverBreak);
8540   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8541                "bbbbbbbbbbbbbbbbbbbb) {}",
8542                NeverBreak);
8543   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8544                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8545                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
8546                NeverBreak);
8547   verifyFormat("template <template <typename> class Fooooooo,\n"
8548                "          template <typename> class Baaaaaaar>\n"
8549                "struct C {};",
8550                NeverBreak);
8551   verifyFormat("template <typename T> // T can be A, B or C.\n"
8552                "struct C {};",
8553                NeverBreak);
8554   verifyFormat("template <enum E> class A {\n"
8555                "public:\n"
8556                "  E *f();\n"
8557                "};",
8558                NeverBreak);
8559   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
8560   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8561                "bbbbbbbbbbbbbbbbbbbb) {}",
8562                NeverBreak);
8563 }
8564 
8565 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
8566   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
8567   Style.ColumnLimit = 60;
8568   EXPECT_EQ("// Baseline - no comments.\n"
8569             "template <\n"
8570             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8571             "void f() {}",
8572             format("// Baseline - no comments.\n"
8573                    "template <\n"
8574                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8575                    "void f() {}",
8576                    Style));
8577 
8578   EXPECT_EQ("template <\n"
8579             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8580             "void f() {}",
8581             format("template <\n"
8582                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8583                    "void f() {}",
8584                    Style));
8585 
8586   EXPECT_EQ(
8587       "template <\n"
8588       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
8589       "void f() {}",
8590       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
8591              "void f() {}",
8592              Style));
8593 
8594   EXPECT_EQ(
8595       "template <\n"
8596       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8597       "                                               // multiline\n"
8598       "void f() {}",
8599       format("template <\n"
8600              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8601              "                                              // multiline\n"
8602              "void f() {}",
8603              Style));
8604 
8605   EXPECT_EQ(
8606       "template <typename aaaaaaaaaa<\n"
8607       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
8608       "void f() {}",
8609       format(
8610           "template <\n"
8611           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
8612           "void f() {}",
8613           Style));
8614 }
8615 
8616 TEST_F(FormatTest, WrapsTemplateParameters) {
8617   FormatStyle Style = getLLVMStyle();
8618   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8619   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8620   verifyFormat(
8621       "template <typename... a> struct q {};\n"
8622       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8623       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8624       "    y;",
8625       Style);
8626   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8627   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8628   verifyFormat(
8629       "template <typename... a> struct r {};\n"
8630       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8631       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8632       "    y;",
8633       Style);
8634   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8635   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8636   verifyFormat("template <typename... a> struct s {};\n"
8637                "extern s<\n"
8638                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8639                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8640                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8641                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8642                "    y;",
8643                Style);
8644   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8645   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8646   verifyFormat("template <typename... a> struct t {};\n"
8647                "extern t<\n"
8648                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8649                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8650                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8651                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8652                "    y;",
8653                Style);
8654 }
8655 
8656 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
8657   verifyFormat(
8658       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8659       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8660   verifyFormat(
8661       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8662       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8663       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8664 
8665   // FIXME: Should we have the extra indent after the second break?
8666   verifyFormat(
8667       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8668       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8669       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8670 
8671   verifyFormat(
8672       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
8673       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
8674 
8675   // Breaking at nested name specifiers is generally not desirable.
8676   verifyFormat(
8677       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8678       "    aaaaaaaaaaaaaaaaaaaaaaa);");
8679 
8680   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
8681                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8682                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8683                "                   aaaaaaaaaaaaaaaaaaaaa);",
8684                getLLVMStyleWithColumns(74));
8685 
8686   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8687                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8688                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8689 }
8690 
8691 TEST_F(FormatTest, UnderstandsTemplateParameters) {
8692   verifyFormat("A<int> a;");
8693   verifyFormat("A<A<A<int>>> a;");
8694   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
8695   verifyFormat("bool x = a < 1 || 2 > a;");
8696   verifyFormat("bool x = 5 < f<int>();");
8697   verifyFormat("bool x = f<int>() > 5;");
8698   verifyFormat("bool x = 5 < a<int>::x;");
8699   verifyFormat("bool x = a < 4 ? a > 2 : false;");
8700   verifyFormat("bool x = f() ? a < 2 : a > 2;");
8701 
8702   verifyGoogleFormat("A<A<int>> a;");
8703   verifyGoogleFormat("A<A<A<int>>> a;");
8704   verifyGoogleFormat("A<A<A<A<int>>>> a;");
8705   verifyGoogleFormat("A<A<int> > a;");
8706   verifyGoogleFormat("A<A<A<int> > > a;");
8707   verifyGoogleFormat("A<A<A<A<int> > > > a;");
8708   verifyGoogleFormat("A<::A<int>> a;");
8709   verifyGoogleFormat("A<::A> a;");
8710   verifyGoogleFormat("A< ::A> a;");
8711   verifyGoogleFormat("A< ::A<int> > a;");
8712   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
8713   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
8714   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
8715   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
8716   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
8717             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
8718 
8719   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
8720 
8721   // template closer followed by a token that starts with > or =
8722   verifyFormat("bool b = a<1> > 1;");
8723   verifyFormat("bool b = a<1> >= 1;");
8724   verifyFormat("int i = a<1> >> 1;");
8725   FormatStyle Style = getLLVMStyle();
8726   Style.SpaceBeforeAssignmentOperators = false;
8727   verifyFormat("bool b= a<1> == 1;", Style);
8728   verifyFormat("a<int> = 1;", Style);
8729   verifyFormat("a<int> >>= 1;", Style);
8730 
8731   verifyFormat("test < a | b >> c;");
8732   verifyFormat("test<test<a | b>> c;");
8733   verifyFormat("test >> a >> b;");
8734   verifyFormat("test << a >> b;");
8735 
8736   verifyFormat("f<int>();");
8737   verifyFormat("template <typename T> void f() {}");
8738   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
8739   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
8740                "sizeof(char)>::type>;");
8741   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
8742   verifyFormat("f(a.operator()<A>());");
8743   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8744                "      .template operator()<A>());",
8745                getLLVMStyleWithColumns(35));
8746 
8747   // Not template parameters.
8748   verifyFormat("return a < b && c > d;");
8749   verifyFormat("void f() {\n"
8750                "  while (a < b && c > d) {\n"
8751                "  }\n"
8752                "}");
8753   verifyFormat("template <typename... Types>\n"
8754                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
8755 
8756   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8757                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
8758                getLLVMStyleWithColumns(60));
8759   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
8760   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
8761   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
8762   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
8763 }
8764 
8765 TEST_F(FormatTest, UnderstandsShiftOperators) {
8766   verifyFormat("if (i < x >> 1)");
8767   verifyFormat("while (i < x >> 1)");
8768   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
8769   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
8770   verifyFormat(
8771       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
8772   verifyFormat("Foo.call<Bar<Function>>()");
8773   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
8774   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
8775                "++i, v = v >> 1)");
8776   verifyFormat("if (w<u<v<x>>, 1>::t)");
8777 }
8778 
8779 TEST_F(FormatTest, BitshiftOperatorWidth) {
8780   EXPECT_EQ("int a = 1 << 2; /* foo\n"
8781             "                   bar */",
8782             format("int    a=1<<2;  /* foo\n"
8783                    "                   bar */"));
8784 
8785   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
8786             "                     bar */",
8787             format("int  b  =256>>1 ;  /* foo\n"
8788                    "                      bar */"));
8789 }
8790 
8791 TEST_F(FormatTest, UnderstandsBinaryOperators) {
8792   verifyFormat("COMPARE(a, ==, b);");
8793   verifyFormat("auto s = sizeof...(Ts) - 1;");
8794 }
8795 
8796 TEST_F(FormatTest, UnderstandsPointersToMembers) {
8797   verifyFormat("int A::*x;");
8798   verifyFormat("int (S::*func)(void *);");
8799   verifyFormat("void f() { int (S::*func)(void *); }");
8800   verifyFormat("typedef bool *(Class::*Member)() const;");
8801   verifyFormat("void f() {\n"
8802                "  (a->*f)();\n"
8803                "  a->*x;\n"
8804                "  (a.*f)();\n"
8805                "  ((*a).*f)();\n"
8806                "  a.*x;\n"
8807                "}");
8808   verifyFormat("void f() {\n"
8809                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
8810                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
8811                "}");
8812   verifyFormat(
8813       "(aaaaaaaaaa->*bbbbbbb)(\n"
8814       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8815   FormatStyle Style = getLLVMStyle();
8816   Style.PointerAlignment = FormatStyle::PAS_Left;
8817   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
8818 }
8819 
8820 TEST_F(FormatTest, UnderstandsUnaryOperators) {
8821   verifyFormat("int a = -2;");
8822   verifyFormat("f(-1, -2, -3);");
8823   verifyFormat("a[-1] = 5;");
8824   verifyFormat("int a = 5 + -2;");
8825   verifyFormat("if (i == -1) {\n}");
8826   verifyFormat("if (i != -1) {\n}");
8827   verifyFormat("if (i > -1) {\n}");
8828   verifyFormat("if (i < -1) {\n}");
8829   verifyFormat("++(a->f());");
8830   verifyFormat("--(a->f());");
8831   verifyFormat("(a->f())++;");
8832   verifyFormat("a[42]++;");
8833   verifyFormat("if (!(a->f())) {\n}");
8834   verifyFormat("if (!+i) {\n}");
8835   verifyFormat("~&a;");
8836 
8837   verifyFormat("a-- > b;");
8838   verifyFormat("b ? -a : c;");
8839   verifyFormat("n * sizeof char16;");
8840   verifyFormat("n * alignof char16;", getGoogleStyle());
8841   verifyFormat("sizeof(char);");
8842   verifyFormat("alignof(char);", getGoogleStyle());
8843 
8844   verifyFormat("return -1;");
8845   verifyFormat("throw -1;");
8846   verifyFormat("switch (a) {\n"
8847                "case -1:\n"
8848                "  break;\n"
8849                "}");
8850   verifyFormat("#define X -1");
8851   verifyFormat("#define X -kConstant");
8852 
8853   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
8854   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
8855 
8856   verifyFormat("int a = /* confusing comment */ -1;");
8857   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
8858   verifyFormat("int a = i /* confusing comment */++;");
8859 
8860   verifyFormat("co_yield -1;");
8861   verifyFormat("co_return -1;");
8862 
8863   // Check that * is not treated as a binary operator when we set
8864   // PointerAlignment as PAS_Left after a keyword and not a declaration.
8865   FormatStyle PASLeftStyle = getLLVMStyle();
8866   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
8867   verifyFormat("co_return *a;", PASLeftStyle);
8868   verifyFormat("co_await *a;", PASLeftStyle);
8869   verifyFormat("co_yield *a", PASLeftStyle);
8870   verifyFormat("return *a;", PASLeftStyle);
8871 }
8872 
8873 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
8874   verifyFormat("if (!aaaaaaaaaa( // break\n"
8875                "        aaaaa)) {\n"
8876                "}");
8877   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
8878                "    aaaaa));");
8879   verifyFormat("*aaa = aaaaaaa( // break\n"
8880                "    bbbbbb);");
8881 }
8882 
8883 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
8884   verifyFormat("bool operator<();");
8885   verifyFormat("bool operator>();");
8886   verifyFormat("bool operator=();");
8887   verifyFormat("bool operator==();");
8888   verifyFormat("bool operator!=();");
8889   verifyFormat("int operator+();");
8890   verifyFormat("int operator++();");
8891   verifyFormat("int operator++(int) volatile noexcept;");
8892   verifyFormat("bool operator,();");
8893   verifyFormat("bool operator();");
8894   verifyFormat("bool operator()();");
8895   verifyFormat("bool operator[]();");
8896   verifyFormat("operator bool();");
8897   verifyFormat("operator int();");
8898   verifyFormat("operator void *();");
8899   verifyFormat("operator SomeType<int>();");
8900   verifyFormat("operator SomeType<int, int>();");
8901   verifyFormat("operator SomeType<SomeType<int>>();");
8902   verifyFormat("void *operator new(std::size_t size);");
8903   verifyFormat("void *operator new[](std::size_t size);");
8904   verifyFormat("void operator delete(void *ptr);");
8905   verifyFormat("void operator delete[](void *ptr);");
8906   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
8907                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
8908   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
8909                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
8910 
8911   verifyFormat(
8912       "ostream &operator<<(ostream &OutputStream,\n"
8913       "                    SomeReallyLongType WithSomeReallyLongValue);");
8914   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
8915                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
8916                "  return left.group < right.group;\n"
8917                "}");
8918   verifyFormat("SomeType &operator=(const SomeType &S);");
8919   verifyFormat("f.template operator()<int>();");
8920 
8921   verifyGoogleFormat("operator void*();");
8922   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
8923   verifyGoogleFormat("operator ::A();");
8924 
8925   verifyFormat("using A::operator+;");
8926   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
8927                "int i;");
8928 
8929   // Calling an operator as a member function.
8930   verifyFormat("void f() { a.operator*(); }");
8931   verifyFormat("void f() { a.operator*(b & b); }");
8932   verifyFormat("void f() { a->operator&(a * b); }");
8933   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
8934   // TODO: Calling an operator as a non-member function is hard to distinguish.
8935   // https://llvm.org/PR50629
8936   // verifyFormat("void f() { operator*(a & a); }");
8937   // verifyFormat("void f() { operator&(a, b * b); }");
8938 }
8939 
8940 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
8941   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
8942   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
8943   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
8944   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
8945   verifyFormat("Deleted &operator=(const Deleted &) &;");
8946   verifyFormat("Deleted &operator=(const Deleted &) &&;");
8947   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
8948   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
8949   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
8950   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
8951   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
8952   verifyFormat("void Fn(T const &) const &;");
8953   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
8954   verifyFormat("template <typename T>\n"
8955                "void F(T) && = delete;",
8956                getGoogleStyle());
8957 
8958   FormatStyle AlignLeft = getLLVMStyle();
8959   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
8960   verifyFormat("void A::b() && {}", AlignLeft);
8961   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
8962   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
8963                AlignLeft);
8964   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
8965   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
8966   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
8967   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
8968   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
8969   verifyFormat("auto Function(T) & -> void;", AlignLeft);
8970   verifyFormat("void Fn(T const&) const&;", AlignLeft);
8971   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
8972 
8973   FormatStyle Spaces = getLLVMStyle();
8974   Spaces.SpacesInCStyleCastParentheses = true;
8975   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
8976   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
8977   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
8978   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
8979 
8980   Spaces.SpacesInCStyleCastParentheses = false;
8981   Spaces.SpacesInParentheses = true;
8982   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
8983   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
8984                Spaces);
8985   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
8986   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
8987 
8988   FormatStyle BreakTemplate = getLLVMStyle();
8989   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8990 
8991   verifyFormat("struct f {\n"
8992                "  template <class T>\n"
8993                "  int &foo(const std::string &str) &noexcept {}\n"
8994                "};",
8995                BreakTemplate);
8996 
8997   verifyFormat("struct f {\n"
8998                "  template <class T>\n"
8999                "  int &foo(const std::string &str) &&noexcept {}\n"
9000                "};",
9001                BreakTemplate);
9002 
9003   verifyFormat("struct f {\n"
9004                "  template <class T>\n"
9005                "  int &foo(const std::string &str) const &noexcept {}\n"
9006                "};",
9007                BreakTemplate);
9008 
9009   verifyFormat("struct f {\n"
9010                "  template <class T>\n"
9011                "  int &foo(const std::string &str) const &noexcept {}\n"
9012                "};",
9013                BreakTemplate);
9014 
9015   verifyFormat("struct f {\n"
9016                "  template <class T>\n"
9017                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9018                "};",
9019                BreakTemplate);
9020 
9021   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9022   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9023       FormatStyle::BTDS_Yes;
9024   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9025 
9026   verifyFormat("struct f {\n"
9027                "  template <class T>\n"
9028                "  int& foo(const std::string& str) & noexcept {}\n"
9029                "};",
9030                AlignLeftBreakTemplate);
9031 
9032   verifyFormat("struct f {\n"
9033                "  template <class T>\n"
9034                "  int& foo(const std::string& str) && noexcept {}\n"
9035                "};",
9036                AlignLeftBreakTemplate);
9037 
9038   verifyFormat("struct f {\n"
9039                "  template <class T>\n"
9040                "  int& foo(const std::string& str) const& noexcept {}\n"
9041                "};",
9042                AlignLeftBreakTemplate);
9043 
9044   verifyFormat("struct f {\n"
9045                "  template <class T>\n"
9046                "  int& foo(const std::string& str) const&& noexcept {}\n"
9047                "};",
9048                AlignLeftBreakTemplate);
9049 
9050   verifyFormat("struct f {\n"
9051                "  template <class T>\n"
9052                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9053                "};",
9054                AlignLeftBreakTemplate);
9055 
9056   // The `&` in `Type&` should not be confused with a trailing `&` of
9057   // DEPRECATED(reason) member function.
9058   verifyFormat("struct f {\n"
9059                "  template <class T>\n"
9060                "  DEPRECATED(reason)\n"
9061                "  Type &foo(arguments) {}\n"
9062                "};",
9063                BreakTemplate);
9064 
9065   verifyFormat("struct f {\n"
9066                "  template <class T>\n"
9067                "  DEPRECATED(reason)\n"
9068                "  Type& foo(arguments) {}\n"
9069                "};",
9070                AlignLeftBreakTemplate);
9071 
9072   verifyFormat("void (*foopt)(int) = &func;");
9073 }
9074 
9075 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9076   verifyFormat("void f() {\n"
9077                "  A *a = new A;\n"
9078                "  A *a = new (placement) A;\n"
9079                "  delete a;\n"
9080                "  delete (A *)a;\n"
9081                "}");
9082   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9083                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9084   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9085                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9086                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9087   verifyFormat("delete[] h->p;");
9088 }
9089 
9090 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9091   verifyFormat("int *f(int *a) {}");
9092   verifyFormat("int main(int argc, char **argv) {}");
9093   verifyFormat("Test::Test(int b) : a(b * b) {}");
9094   verifyIndependentOfContext("f(a, *a);");
9095   verifyFormat("void g() { f(*a); }");
9096   verifyIndependentOfContext("int a = b * 10;");
9097   verifyIndependentOfContext("int a = 10 * b;");
9098   verifyIndependentOfContext("int a = b * c;");
9099   verifyIndependentOfContext("int a += b * c;");
9100   verifyIndependentOfContext("int a -= b * c;");
9101   verifyIndependentOfContext("int a *= b * c;");
9102   verifyIndependentOfContext("int a /= b * c;");
9103   verifyIndependentOfContext("int a = *b;");
9104   verifyIndependentOfContext("int a = *b * c;");
9105   verifyIndependentOfContext("int a = b * *c;");
9106   verifyIndependentOfContext("int a = b * (10);");
9107   verifyIndependentOfContext("S << b * (10);");
9108   verifyIndependentOfContext("return 10 * b;");
9109   verifyIndependentOfContext("return *b * *c;");
9110   verifyIndependentOfContext("return a & ~b;");
9111   verifyIndependentOfContext("f(b ? *c : *d);");
9112   verifyIndependentOfContext("int a = b ? *c : *d;");
9113   verifyIndependentOfContext("*b = a;");
9114   verifyIndependentOfContext("a * ~b;");
9115   verifyIndependentOfContext("a * !b;");
9116   verifyIndependentOfContext("a * +b;");
9117   verifyIndependentOfContext("a * -b;");
9118   verifyIndependentOfContext("a * ++b;");
9119   verifyIndependentOfContext("a * --b;");
9120   verifyIndependentOfContext("a[4] * b;");
9121   verifyIndependentOfContext("a[a * a] = 1;");
9122   verifyIndependentOfContext("f() * b;");
9123   verifyIndependentOfContext("a * [self dostuff];");
9124   verifyIndependentOfContext("int x = a * (a + b);");
9125   verifyIndependentOfContext("(a *)(a + b);");
9126   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9127   verifyIndependentOfContext("int *pa = (int *)&a;");
9128   verifyIndependentOfContext("return sizeof(int **);");
9129   verifyIndependentOfContext("return sizeof(int ******);");
9130   verifyIndependentOfContext("return (int **&)a;");
9131   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9132   verifyFormat("void f(Type (*parameter)[10]) {}");
9133   verifyFormat("void f(Type (&parameter)[10]) {}");
9134   verifyGoogleFormat("return sizeof(int**);");
9135   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9136   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9137   verifyFormat("auto a = [](int **&, int ***) {};");
9138   verifyFormat("auto PointerBinding = [](const char *S) {};");
9139   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9140   verifyFormat("[](const decltype(*a) &value) {}");
9141   verifyFormat("[](const typeof(*a) &value) {}");
9142   verifyFormat("[](const _Atomic(a *) &value) {}");
9143   verifyFormat("[](const __underlying_type(a) &value) {}");
9144   verifyFormat("decltype(a * b) F();");
9145   verifyFormat("typeof(a * b) F();");
9146   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9147   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9148   verifyIndependentOfContext("typedef void (*f)(int *a);");
9149   verifyIndependentOfContext("int i{a * b};");
9150   verifyIndependentOfContext("aaa && aaa->f();");
9151   verifyIndependentOfContext("int x = ~*p;");
9152   verifyFormat("Constructor() : a(a), area(width * height) {}");
9153   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9154   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9155   verifyFormat("void f() { f(a, c * d); }");
9156   verifyFormat("void f() { f(new a(), c * d); }");
9157   verifyFormat("void f(const MyOverride &override);");
9158   verifyFormat("void f(const MyFinal &final);");
9159   verifyIndependentOfContext("bool a = f() && override.f();");
9160   verifyIndependentOfContext("bool a = f() && final.f();");
9161 
9162   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9163 
9164   verifyIndependentOfContext("A<int *> a;");
9165   verifyIndependentOfContext("A<int **> a;");
9166   verifyIndependentOfContext("A<int *, int *> a;");
9167   verifyIndependentOfContext("A<int *[]> a;");
9168   verifyIndependentOfContext(
9169       "const char *const p = reinterpret_cast<const char *const>(q);");
9170   verifyIndependentOfContext("A<int **, int **> a;");
9171   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9172   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9173   verifyFormat("for (; a && b;) {\n}");
9174   verifyFormat("bool foo = true && [] { return false; }();");
9175 
9176   verifyFormat(
9177       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9178       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9179 
9180   verifyGoogleFormat("int const* a = &b;");
9181   verifyGoogleFormat("**outparam = 1;");
9182   verifyGoogleFormat("*outparam = a * b;");
9183   verifyGoogleFormat("int main(int argc, char** argv) {}");
9184   verifyGoogleFormat("A<int*> a;");
9185   verifyGoogleFormat("A<int**> a;");
9186   verifyGoogleFormat("A<int*, int*> a;");
9187   verifyGoogleFormat("A<int**, int**> a;");
9188   verifyGoogleFormat("f(b ? *c : *d);");
9189   verifyGoogleFormat("int a = b ? *c : *d;");
9190   verifyGoogleFormat("Type* t = **x;");
9191   verifyGoogleFormat("Type* t = *++*x;");
9192   verifyGoogleFormat("*++*x;");
9193   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9194   verifyGoogleFormat("Type* t = x++ * y;");
9195   verifyGoogleFormat(
9196       "const char* const p = reinterpret_cast<const char* const>(q);");
9197   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9198   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9199   verifyGoogleFormat("template <typename T>\n"
9200                      "void f(int i = 0, SomeType** temps = NULL);");
9201 
9202   FormatStyle Left = getLLVMStyle();
9203   Left.PointerAlignment = FormatStyle::PAS_Left;
9204   verifyFormat("x = *a(x) = *a(y);", Left);
9205   verifyFormat("for (;; *a = b) {\n}", Left);
9206   verifyFormat("return *this += 1;", Left);
9207   verifyFormat("throw *x;", Left);
9208   verifyFormat("delete *x;", Left);
9209   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9210   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9211   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9212   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9213   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9214   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9215   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9216   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9217   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9218 
9219   verifyIndependentOfContext("a = *(x + y);");
9220   verifyIndependentOfContext("a = &(x + y);");
9221   verifyIndependentOfContext("*(x + y).call();");
9222   verifyIndependentOfContext("&(x + y)->call();");
9223   verifyFormat("void f() { &(*I).first; }");
9224 
9225   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9226   verifyFormat(
9227       "int *MyValues = {\n"
9228       "    *A, // Operator detection might be confused by the '{'\n"
9229       "    *BB // Operator detection might be confused by previous comment\n"
9230       "};");
9231 
9232   verifyIndependentOfContext("if (int *a = &b)");
9233   verifyIndependentOfContext("if (int &a = *b)");
9234   verifyIndependentOfContext("if (a & b[i])");
9235   verifyIndependentOfContext("if constexpr (a & b[i])");
9236   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9237   verifyIndependentOfContext("if (a * (b * c))");
9238   verifyIndependentOfContext("if constexpr (a * (b * c))");
9239   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9240   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9241   verifyIndependentOfContext("if (*b[i])");
9242   verifyIndependentOfContext("if (int *a = (&b))");
9243   verifyIndependentOfContext("while (int *a = &b)");
9244   verifyIndependentOfContext("while (a * (b * c))");
9245   verifyIndependentOfContext("size = sizeof *a;");
9246   verifyIndependentOfContext("if (a && (b = c))");
9247   verifyFormat("void f() {\n"
9248                "  for (const int &v : Values) {\n"
9249                "  }\n"
9250                "}");
9251   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9252   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9253   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9254 
9255   verifyFormat("#define A (!a * b)");
9256   verifyFormat("#define MACRO     \\\n"
9257                "  int *i = a * b; \\\n"
9258                "  void f(a *b);",
9259                getLLVMStyleWithColumns(19));
9260 
9261   verifyIndependentOfContext("A = new SomeType *[Length];");
9262   verifyIndependentOfContext("A = new SomeType *[Length]();");
9263   verifyIndependentOfContext("T **t = new T *;");
9264   verifyIndependentOfContext("T **t = new T *();");
9265   verifyGoogleFormat("A = new SomeType*[Length]();");
9266   verifyGoogleFormat("A = new SomeType*[Length];");
9267   verifyGoogleFormat("T** t = new T*;");
9268   verifyGoogleFormat("T** t = new T*();");
9269 
9270   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9271   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9272   verifyFormat("template <bool a, bool b> "
9273                "typename t::if<x && y>::type f() {}");
9274   verifyFormat("template <int *y> f() {}");
9275   verifyFormat("vector<int *> v;");
9276   verifyFormat("vector<int *const> v;");
9277   verifyFormat("vector<int *const **const *> v;");
9278   verifyFormat("vector<int *volatile> v;");
9279   verifyFormat("vector<a *_Nonnull> v;");
9280   verifyFormat("vector<a *_Nullable> v;");
9281   verifyFormat("vector<a *_Null_unspecified> v;");
9282   verifyFormat("vector<a *__ptr32> v;");
9283   verifyFormat("vector<a *__ptr64> v;");
9284   verifyFormat("vector<a *__capability> v;");
9285   FormatStyle TypeMacros = getLLVMStyle();
9286   TypeMacros.TypenameMacros = {"LIST"};
9287   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9288   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9289   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9290   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9291   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9292 
9293   FormatStyle CustomQualifier = getLLVMStyle();
9294   // Add indentifers that should not be parsed as a qualifier by default.
9295   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9296   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9297   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9298   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9299   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9300   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9301   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9302   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9303   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9304   verifyFormat("vector<a * _NotAQualifier> v;");
9305   verifyFormat("vector<a * __not_a_qualifier> v;");
9306   verifyFormat("vector<a * b> v;");
9307   verifyFormat("foo<b && false>();");
9308   verifyFormat("foo<b & 1>();");
9309   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9310   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9311   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9312   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9313   verifyFormat(
9314       "template <class T, class = typename std::enable_if<\n"
9315       "                       std::is_integral<T>::value &&\n"
9316       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9317       "void F();",
9318       getLLVMStyleWithColumns(70));
9319   verifyFormat("template <class T,\n"
9320                "          class = typename std::enable_if<\n"
9321                "              std::is_integral<T>::value &&\n"
9322                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9323                "          class U>\n"
9324                "void F();",
9325                getLLVMStyleWithColumns(70));
9326   verifyFormat(
9327       "template <class T,\n"
9328       "          class = typename ::std::enable_if<\n"
9329       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9330       "void F();",
9331       getGoogleStyleWithColumns(68));
9332 
9333   verifyIndependentOfContext("MACRO(int *i);");
9334   verifyIndependentOfContext("MACRO(auto *a);");
9335   verifyIndependentOfContext("MACRO(const A *a);");
9336   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9337   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9338   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9339   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9340   verifyIndependentOfContext("MACRO(A *const a);");
9341   verifyIndependentOfContext("MACRO(A *restrict a);");
9342   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9343   verifyIndependentOfContext("MACRO(A *__restrict a);");
9344   verifyIndependentOfContext("MACRO(A *volatile a);");
9345   verifyIndependentOfContext("MACRO(A *__volatile a);");
9346   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9347   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9348   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9349   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9350   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9351   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9352   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9353   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9354   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9355   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9356   verifyIndependentOfContext("MACRO(A *__capability);");
9357   verifyIndependentOfContext("MACRO(A &__capability);");
9358   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9359   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9360   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9361   // a type declaration:
9362   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
9363   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
9364   // Also check that TypenameMacros prevents parsing it as multiplication:
9365   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
9366   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
9367 
9368   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
9369   verifyFormat("void f() { f(float{1}, a * a); }");
9370   verifyFormat("void f() { f(float(1), a * a); }");
9371 
9372   verifyFormat("f((void (*)(int))g);");
9373   verifyFormat("f((void (&)(int))g);");
9374   verifyFormat("f((void (^)(int))g);");
9375 
9376   // FIXME: Is there a way to make this work?
9377   // verifyIndependentOfContext("MACRO(A *a);");
9378   verifyFormat("MACRO(A &B);");
9379   verifyFormat("MACRO(A *B);");
9380   verifyFormat("void f() { MACRO(A * B); }");
9381   verifyFormat("void f() { MACRO(A & B); }");
9382 
9383   // This lambda was mis-formatted after D88956 (treating it as a binop):
9384   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
9385   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
9386   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
9387   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
9388 
9389   verifyFormat("DatumHandle const *operator->() const { return input_; }");
9390   verifyFormat("return options != nullptr && operator==(*options);");
9391 
9392   EXPECT_EQ("#define OP(x)                                    \\\n"
9393             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
9394             "    return s << a.DebugString();                 \\\n"
9395             "  }",
9396             format("#define OP(x) \\\n"
9397                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
9398                    "    return s << a.DebugString(); \\\n"
9399                    "  }",
9400                    getLLVMStyleWithColumns(50)));
9401 
9402   // FIXME: We cannot handle this case yet; we might be able to figure out that
9403   // foo<x> d > v; doesn't make sense.
9404   verifyFormat("foo<a<b && c> d> v;");
9405 
9406   FormatStyle PointerMiddle = getLLVMStyle();
9407   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9408   verifyFormat("delete *x;", PointerMiddle);
9409   verifyFormat("int * x;", PointerMiddle);
9410   verifyFormat("int *[] x;", PointerMiddle);
9411   verifyFormat("template <int * y> f() {}", PointerMiddle);
9412   verifyFormat("int * f(int * a) {}", PointerMiddle);
9413   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
9414   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
9415   verifyFormat("A<int *> a;", PointerMiddle);
9416   verifyFormat("A<int **> a;", PointerMiddle);
9417   verifyFormat("A<int *, int *> a;", PointerMiddle);
9418   verifyFormat("A<int *[]> a;", PointerMiddle);
9419   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
9420   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
9421   verifyFormat("T ** t = new T *;", PointerMiddle);
9422 
9423   // Member function reference qualifiers aren't binary operators.
9424   verifyFormat("string // break\n"
9425                "operator()() & {}");
9426   verifyFormat("string // break\n"
9427                "operator()() && {}");
9428   verifyGoogleFormat("template <typename T>\n"
9429                      "auto x() & -> int {}");
9430 
9431   // Should be binary operators when used as an argument expression (overloaded
9432   // operator invoked as a member function).
9433   verifyFormat("void f() { a.operator()(a * a); }");
9434   verifyFormat("void f() { a->operator()(a & a); }");
9435   verifyFormat("void f() { a.operator()(*a & *a); }");
9436   verifyFormat("void f() { a->operator()(*a * *a); }");
9437 }
9438 
9439 TEST_F(FormatTest, UnderstandsAttributes) {
9440   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
9441   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
9442                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9443   FormatStyle AfterType = getLLVMStyle();
9444   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9445   verifyFormat("__attribute__((nodebug)) void\n"
9446                "foo() {}\n",
9447                AfterType);
9448   verifyFormat("__unused void\n"
9449                "foo() {}",
9450                AfterType);
9451 
9452   FormatStyle CustomAttrs = getLLVMStyle();
9453   CustomAttrs.AttributeMacros.push_back("__unused");
9454   CustomAttrs.AttributeMacros.push_back("__attr1");
9455   CustomAttrs.AttributeMacros.push_back("__attr2");
9456   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
9457   verifyFormat("vector<SomeType *__attribute((foo))> v;");
9458   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
9459   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
9460   // Check that it is parsed as a multiplication without AttributeMacros and
9461   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
9462   verifyFormat("vector<SomeType * __attr1> v;");
9463   verifyFormat("vector<SomeType __attr1 *> v;");
9464   verifyFormat("vector<SomeType __attr1 *const> v;");
9465   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
9466   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
9467   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
9468   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
9469   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
9470   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
9471   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
9472   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
9473 
9474   // Check that these are not parsed as function declarations:
9475   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9476   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
9477   verifyFormat("SomeType s(InitValue);", CustomAttrs);
9478   verifyFormat("SomeType s{InitValue};", CustomAttrs);
9479   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
9480   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
9481   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
9482   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
9483   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
9484   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
9485 }
9486 
9487 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
9488   // Check that qualifiers on pointers don't break parsing of casts.
9489   verifyFormat("x = (foo *const)*v;");
9490   verifyFormat("x = (foo *volatile)*v;");
9491   verifyFormat("x = (foo *restrict)*v;");
9492   verifyFormat("x = (foo *__attribute__((foo)))*v;");
9493   verifyFormat("x = (foo *_Nonnull)*v;");
9494   verifyFormat("x = (foo *_Nullable)*v;");
9495   verifyFormat("x = (foo *_Null_unspecified)*v;");
9496   verifyFormat("x = (foo *_Nonnull)*v;");
9497   verifyFormat("x = (foo *[[clang::attr]])*v;");
9498   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
9499   verifyFormat("x = (foo *__ptr32)*v;");
9500   verifyFormat("x = (foo *__ptr64)*v;");
9501   verifyFormat("x = (foo *__capability)*v;");
9502 
9503   // Check that we handle multiple trailing qualifiers and skip them all to
9504   // determine that the expression is a cast to a pointer type.
9505   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
9506   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
9507   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
9508   StringRef AllQualifiers =
9509       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
9510       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
9511   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
9512   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
9513 
9514   // Also check that address-of is not parsed as a binary bitwise-and:
9515   verifyFormat("x = (foo *const)&v;");
9516   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
9517   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
9518 
9519   // Check custom qualifiers:
9520   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
9521   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9522   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
9523   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
9524   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
9525                CustomQualifier);
9526   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
9527                CustomQualifier);
9528 
9529   // Check that unknown identifiers result in binary operator parsing:
9530   verifyFormat("x = (foo * __unknown_qualifier) * v;");
9531   verifyFormat("x = (foo * __unknown_qualifier) & v;");
9532 }
9533 
9534 TEST_F(FormatTest, UnderstandsSquareAttributes) {
9535   verifyFormat("SomeType s [[unused]] (InitValue);");
9536   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
9537   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
9538   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
9539   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
9540   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9541                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9542   verifyFormat("[[nodiscard]] bool f() { return false; }");
9543   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
9544   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
9545   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
9546 
9547   // Make sure we do not mistake attributes for array subscripts.
9548   verifyFormat("int a() {}\n"
9549                "[[unused]] int b() {}\n");
9550   verifyFormat("NSArray *arr;\n"
9551                "arr[[Foo() bar]];");
9552 
9553   // On the other hand, we still need to correctly find array subscripts.
9554   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
9555 
9556   // Make sure that we do not mistake Objective-C method inside array literals
9557   // as attributes, even if those method names are also keywords.
9558   verifyFormat("@[ [foo bar] ];");
9559   verifyFormat("@[ [NSArray class] ];");
9560   verifyFormat("@[ [foo enum] ];");
9561 
9562   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
9563 
9564   // Make sure we do not parse attributes as lambda introducers.
9565   FormatStyle MultiLineFunctions = getLLVMStyle();
9566   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9567   verifyFormat("[[unused]] int b() {\n"
9568                "  return 42;\n"
9569                "}\n",
9570                MultiLineFunctions);
9571 }
9572 
9573 TEST_F(FormatTest, AttributeClass) {
9574   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
9575   verifyFormat("class S {\n"
9576                "  S(S&&) = default;\n"
9577                "};",
9578                Style);
9579   verifyFormat("class [[nodiscard]] S {\n"
9580                "  S(S&&) = default;\n"
9581                "};",
9582                Style);
9583   verifyFormat("class __attribute((maybeunused)) S {\n"
9584                "  S(S&&) = default;\n"
9585                "};",
9586                Style);
9587   verifyFormat("struct S {\n"
9588                "  S(S&&) = default;\n"
9589                "};",
9590                Style);
9591   verifyFormat("struct [[nodiscard]] S {\n"
9592                "  S(S&&) = default;\n"
9593                "};",
9594                Style);
9595 }
9596 
9597 TEST_F(FormatTest, AttributesAfterMacro) {
9598   FormatStyle Style = getLLVMStyle();
9599   verifyFormat("MACRO;\n"
9600                "__attribute__((maybe_unused)) int foo() {\n"
9601                "  //...\n"
9602                "}");
9603 
9604   verifyFormat("MACRO;\n"
9605                "[[nodiscard]] int foo() {\n"
9606                "  //...\n"
9607                "}");
9608 
9609   EXPECT_EQ("MACRO\n\n"
9610             "__attribute__((maybe_unused)) int foo() {\n"
9611             "  //...\n"
9612             "}",
9613             format("MACRO\n\n"
9614                    "__attribute__((maybe_unused)) int foo() {\n"
9615                    "  //...\n"
9616                    "}"));
9617 
9618   EXPECT_EQ("MACRO\n\n"
9619             "[[nodiscard]] int foo() {\n"
9620             "  //...\n"
9621             "}",
9622             format("MACRO\n\n"
9623                    "[[nodiscard]] int foo() {\n"
9624                    "  //...\n"
9625                    "}"));
9626 }
9627 
9628 TEST_F(FormatTest, AttributePenaltyBreaking) {
9629   FormatStyle Style = getLLVMStyle();
9630   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
9631                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9632                Style);
9633   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
9634                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9635                Style);
9636   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
9637                "shared_ptr<ALongTypeName> &C d) {\n}",
9638                Style);
9639 }
9640 
9641 TEST_F(FormatTest, UnderstandsEllipsis) {
9642   FormatStyle Style = getLLVMStyle();
9643   verifyFormat("int printf(const char *fmt, ...);");
9644   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
9645   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
9646 
9647   verifyFormat("template <int *...PP> a;", Style);
9648 
9649   Style.PointerAlignment = FormatStyle::PAS_Left;
9650   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
9651 
9652   verifyFormat("template <int*... PP> a;", Style);
9653 
9654   Style.PointerAlignment = FormatStyle::PAS_Middle;
9655   verifyFormat("template <int *... PP> a;", Style);
9656 }
9657 
9658 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
9659   EXPECT_EQ("int *a;\n"
9660             "int *a;\n"
9661             "int *a;",
9662             format("int *a;\n"
9663                    "int* a;\n"
9664                    "int *a;",
9665                    getGoogleStyle()));
9666   EXPECT_EQ("int* a;\n"
9667             "int* a;\n"
9668             "int* a;",
9669             format("int* a;\n"
9670                    "int* a;\n"
9671                    "int *a;",
9672                    getGoogleStyle()));
9673   EXPECT_EQ("int *a;\n"
9674             "int *a;\n"
9675             "int *a;",
9676             format("int *a;\n"
9677                    "int * a;\n"
9678                    "int *  a;",
9679                    getGoogleStyle()));
9680   EXPECT_EQ("auto x = [] {\n"
9681             "  int *a;\n"
9682             "  int *a;\n"
9683             "  int *a;\n"
9684             "};",
9685             format("auto x=[]{int *a;\n"
9686                    "int * a;\n"
9687                    "int *  a;};",
9688                    getGoogleStyle()));
9689 }
9690 
9691 TEST_F(FormatTest, UnderstandsRvalueReferences) {
9692   verifyFormat("int f(int &&a) {}");
9693   verifyFormat("int f(int a, char &&b) {}");
9694   verifyFormat("void f() { int &&a = b; }");
9695   verifyGoogleFormat("int f(int a, char&& b) {}");
9696   verifyGoogleFormat("void f() { int&& a = b; }");
9697 
9698   verifyIndependentOfContext("A<int &&> a;");
9699   verifyIndependentOfContext("A<int &&, int &&> a;");
9700   verifyGoogleFormat("A<int&&> a;");
9701   verifyGoogleFormat("A<int&&, int&&> a;");
9702 
9703   // Not rvalue references:
9704   verifyFormat("template <bool B, bool C> class A {\n"
9705                "  static_assert(B && C, \"Something is wrong\");\n"
9706                "};");
9707   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
9708   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
9709   verifyFormat("#define A(a, b) (a && b)");
9710 }
9711 
9712 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
9713   verifyFormat("void f() {\n"
9714                "  x[aaaaaaaaa -\n"
9715                "    b] = 23;\n"
9716                "}",
9717                getLLVMStyleWithColumns(15));
9718 }
9719 
9720 TEST_F(FormatTest, FormatsCasts) {
9721   verifyFormat("Type *A = static_cast<Type *>(P);");
9722   verifyFormat("Type *A = (Type *)P;");
9723   verifyFormat("Type *A = (vector<Type *, int *>)P;");
9724   verifyFormat("int a = (int)(2.0f);");
9725   verifyFormat("int a = (int)2.0f;");
9726   verifyFormat("x[(int32)y];");
9727   verifyFormat("x = (int32)y;");
9728   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
9729   verifyFormat("int a = (int)*b;");
9730   verifyFormat("int a = (int)2.0f;");
9731   verifyFormat("int a = (int)~0;");
9732   verifyFormat("int a = (int)++a;");
9733   verifyFormat("int a = (int)sizeof(int);");
9734   verifyFormat("int a = (int)+2;");
9735   verifyFormat("my_int a = (my_int)2.0f;");
9736   verifyFormat("my_int a = (my_int)sizeof(int);");
9737   verifyFormat("return (my_int)aaa;");
9738   verifyFormat("#define x ((int)-1)");
9739   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
9740   verifyFormat("#define p(q) ((int *)&q)");
9741   verifyFormat("fn(a)(b) + 1;");
9742 
9743   verifyFormat("void f() { my_int a = (my_int)*b; }");
9744   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
9745   verifyFormat("my_int a = (my_int)~0;");
9746   verifyFormat("my_int a = (my_int)++a;");
9747   verifyFormat("my_int a = (my_int)-2;");
9748   verifyFormat("my_int a = (my_int)1;");
9749   verifyFormat("my_int a = (my_int *)1;");
9750   verifyFormat("my_int a = (const my_int)-1;");
9751   verifyFormat("my_int a = (const my_int *)-1;");
9752   verifyFormat("my_int a = (my_int)(my_int)-1;");
9753   verifyFormat("my_int a = (ns::my_int)-2;");
9754   verifyFormat("case (my_int)ONE:");
9755   verifyFormat("auto x = (X)this;");
9756   // Casts in Obj-C style calls used to not be recognized as such.
9757   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
9758 
9759   // FIXME: single value wrapped with paren will be treated as cast.
9760   verifyFormat("void f(int i = (kValue)*kMask) {}");
9761 
9762   verifyFormat("{ (void)F; }");
9763 
9764   // Don't break after a cast's
9765   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9766                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
9767                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
9768 
9769   // These are not casts.
9770   verifyFormat("void f(int *) {}");
9771   verifyFormat("f(foo)->b;");
9772   verifyFormat("f(foo).b;");
9773   verifyFormat("f(foo)(b);");
9774   verifyFormat("f(foo)[b];");
9775   verifyFormat("[](foo) { return 4; }(bar);");
9776   verifyFormat("(*funptr)(foo)[4];");
9777   verifyFormat("funptrs[4](foo)[4];");
9778   verifyFormat("void f(int *);");
9779   verifyFormat("void f(int *) = 0;");
9780   verifyFormat("void f(SmallVector<int>) {}");
9781   verifyFormat("void f(SmallVector<int>);");
9782   verifyFormat("void f(SmallVector<int>) = 0;");
9783   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
9784   verifyFormat("int a = sizeof(int) * b;");
9785   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
9786   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
9787   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
9788   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
9789 
9790   // These are not casts, but at some point were confused with casts.
9791   verifyFormat("virtual void foo(int *) override;");
9792   verifyFormat("virtual void foo(char &) const;");
9793   verifyFormat("virtual void foo(int *a, char *) const;");
9794   verifyFormat("int a = sizeof(int *) + b;");
9795   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
9796   verifyFormat("bool b = f(g<int>) && c;");
9797   verifyFormat("typedef void (*f)(int i) func;");
9798   verifyFormat("void operator++(int) noexcept;");
9799   verifyFormat("void operator++(int &) noexcept;");
9800   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
9801                "&) noexcept;");
9802   verifyFormat(
9803       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
9804   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
9805   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
9806   verifyFormat("void operator delete(nothrow_t &) noexcept;");
9807   verifyFormat("void operator delete(foo &) noexcept;");
9808   verifyFormat("void operator delete(foo) noexcept;");
9809   verifyFormat("void operator delete(int) noexcept;");
9810   verifyFormat("void operator delete(int &) noexcept;");
9811   verifyFormat("void operator delete(int &) volatile noexcept;");
9812   verifyFormat("void operator delete(int &) const");
9813   verifyFormat("void operator delete(int &) = default");
9814   verifyFormat("void operator delete(int &) = delete");
9815   verifyFormat("void operator delete(int &) [[noreturn]]");
9816   verifyFormat("void operator delete(int &) throw();");
9817   verifyFormat("void operator delete(int &) throw(int);");
9818   verifyFormat("auto operator delete(int &) -> int;");
9819   verifyFormat("auto operator delete(int &) override");
9820   verifyFormat("auto operator delete(int &) final");
9821 
9822   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
9823                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
9824   // FIXME: The indentation here is not ideal.
9825   verifyFormat(
9826       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9827       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
9828       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
9829 }
9830 
9831 TEST_F(FormatTest, FormatsFunctionTypes) {
9832   verifyFormat("A<bool()> a;");
9833   verifyFormat("A<SomeType()> a;");
9834   verifyFormat("A<void (*)(int, std::string)> a;");
9835   verifyFormat("A<void *(int)>;");
9836   verifyFormat("void *(*a)(int *, SomeType *);");
9837   verifyFormat("int (*func)(void *);");
9838   verifyFormat("void f() { int (*func)(void *); }");
9839   verifyFormat("template <class CallbackClass>\n"
9840                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
9841 
9842   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
9843   verifyGoogleFormat("void* (*a)(int);");
9844   verifyGoogleFormat(
9845       "template <class CallbackClass>\n"
9846       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
9847 
9848   // Other constructs can look somewhat like function types:
9849   verifyFormat("A<sizeof(*x)> a;");
9850   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
9851   verifyFormat("some_var = function(*some_pointer_var)[0];");
9852   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
9853   verifyFormat("int x = f(&h)();");
9854   verifyFormat("returnsFunction(&param1, &param2)(param);");
9855   verifyFormat("std::function<\n"
9856                "    LooooooooooongTemplatedType<\n"
9857                "        SomeType>*(\n"
9858                "        LooooooooooooooooongType type)>\n"
9859                "    function;",
9860                getGoogleStyleWithColumns(40));
9861 }
9862 
9863 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
9864   verifyFormat("A (*foo_)[6];");
9865   verifyFormat("vector<int> (*foo_)[6];");
9866 }
9867 
9868 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
9869   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9870                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
9871   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
9872                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
9873   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9874                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
9875 
9876   // Different ways of ()-initializiation.
9877   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9878                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
9879   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9880                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
9881   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9882                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
9883   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9884                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
9885 
9886   // Lambdas should not confuse the variable declaration heuristic.
9887   verifyFormat("LooooooooooooooooongType\n"
9888                "    variable(nullptr, [](A *a) {});",
9889                getLLVMStyleWithColumns(40));
9890 }
9891 
9892 TEST_F(FormatTest, BreaksLongDeclarations) {
9893   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
9894                "    AnotherNameForTheLongType;");
9895   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
9896                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9897   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9898                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
9899   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
9900                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
9901   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9902                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9903   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
9904                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9905   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
9906                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9907   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
9908                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9909   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
9910                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9911   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
9912                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9913   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
9914                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9915   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9916                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
9917   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9918                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
9919   FormatStyle Indented = getLLVMStyle();
9920   Indented.IndentWrappedFunctionNames = true;
9921   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9922                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
9923                Indented);
9924   verifyFormat(
9925       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9926       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
9927       Indented);
9928   verifyFormat(
9929       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
9930       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
9931       Indented);
9932   verifyFormat(
9933       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
9934       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
9935       Indented);
9936 
9937   // FIXME: Without the comment, this breaks after "(".
9938   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
9939                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
9940                getGoogleStyle());
9941 
9942   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
9943                "                  int LoooooooooooooooooooongParam2) {}");
9944   verifyFormat(
9945       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
9946       "                                   SourceLocation L, IdentifierIn *II,\n"
9947       "                                   Type *T) {}");
9948   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
9949                "ReallyReaaallyLongFunctionName(\n"
9950                "    const std::string &SomeParameter,\n"
9951                "    const SomeType<string, SomeOtherTemplateParameter>\n"
9952                "        &ReallyReallyLongParameterName,\n"
9953                "    const SomeType<string, SomeOtherTemplateParameter>\n"
9954                "        &AnotherLongParameterName) {}");
9955   verifyFormat("template <typename A>\n"
9956                "SomeLoooooooooooooooooooooongType<\n"
9957                "    typename some_namespace::SomeOtherType<A>::Type>\n"
9958                "Function() {}");
9959 
9960   verifyGoogleFormat(
9961       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
9962       "    aaaaaaaaaaaaaaaaaaaaaaa;");
9963   verifyGoogleFormat(
9964       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
9965       "                                   SourceLocation L) {}");
9966   verifyGoogleFormat(
9967       "some_namespace::LongReturnType\n"
9968       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
9969       "    int first_long_parameter, int second_parameter) {}");
9970 
9971   verifyGoogleFormat("template <typename T>\n"
9972                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
9973                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
9974   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9975                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
9976 
9977   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9978                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9979                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9980   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9981                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
9982                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
9983   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9984                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9985                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
9986                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9987 
9988   verifyFormat("template <typename T> // Templates on own line.\n"
9989                "static int            // Some comment.\n"
9990                "MyFunction(int a);",
9991                getLLVMStyle());
9992 }
9993 
9994 TEST_F(FormatTest, FormatsAccessModifiers) {
9995   FormatStyle Style = getLLVMStyle();
9996   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
9997             FormatStyle::ELBAMS_LogicalBlock);
9998   verifyFormat("struct foo {\n"
9999                "private:\n"
10000                "  void f() {}\n"
10001                "\n"
10002                "private:\n"
10003                "  int i;\n"
10004                "\n"
10005                "protected:\n"
10006                "  int j;\n"
10007                "};\n",
10008                Style);
10009   verifyFormat("struct foo {\n"
10010                "private:\n"
10011                "  void f() {}\n"
10012                "\n"
10013                "private:\n"
10014                "  int i;\n"
10015                "\n"
10016                "protected:\n"
10017                "  int j;\n"
10018                "};\n",
10019                "struct foo {\n"
10020                "private:\n"
10021                "  void f() {}\n"
10022                "private:\n"
10023                "  int i;\n"
10024                "protected:\n"
10025                "  int j;\n"
10026                "};\n",
10027                Style);
10028   verifyFormat("struct foo { /* comment */\n"
10029                "private:\n"
10030                "  int i;\n"
10031                "  // comment\n"
10032                "private:\n"
10033                "  int j;\n"
10034                "};\n",
10035                Style);
10036   verifyFormat("struct foo {\n"
10037                "#ifdef FOO\n"
10038                "#endif\n"
10039                "private:\n"
10040                "  int i;\n"
10041                "#ifdef FOO\n"
10042                "private:\n"
10043                "#endif\n"
10044                "  int j;\n"
10045                "};\n",
10046                Style);
10047   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10048   verifyFormat("struct foo {\n"
10049                "private:\n"
10050                "  void f() {}\n"
10051                "private:\n"
10052                "  int i;\n"
10053                "protected:\n"
10054                "  int j;\n"
10055                "};\n",
10056                Style);
10057   verifyFormat("struct foo {\n"
10058                "private:\n"
10059                "  void f() {}\n"
10060                "private:\n"
10061                "  int i;\n"
10062                "protected:\n"
10063                "  int j;\n"
10064                "};\n",
10065                "struct foo {\n"
10066                "\n"
10067                "private:\n"
10068                "  void f() {}\n"
10069                "\n"
10070                "private:\n"
10071                "  int i;\n"
10072                "\n"
10073                "protected:\n"
10074                "  int j;\n"
10075                "};\n",
10076                Style);
10077   verifyFormat("struct foo { /* comment */\n"
10078                "private:\n"
10079                "  int i;\n"
10080                "  // comment\n"
10081                "private:\n"
10082                "  int j;\n"
10083                "};\n",
10084                "struct foo { /* comment */\n"
10085                "\n"
10086                "private:\n"
10087                "  int i;\n"
10088                "  // comment\n"
10089                "\n"
10090                "private:\n"
10091                "  int j;\n"
10092                "};\n",
10093                Style);
10094   verifyFormat("struct foo {\n"
10095                "#ifdef FOO\n"
10096                "#endif\n"
10097                "private:\n"
10098                "  int i;\n"
10099                "#ifdef FOO\n"
10100                "private:\n"
10101                "#endif\n"
10102                "  int j;\n"
10103                "};\n",
10104                "struct foo {\n"
10105                "#ifdef FOO\n"
10106                "#endif\n"
10107                "\n"
10108                "private:\n"
10109                "  int i;\n"
10110                "#ifdef FOO\n"
10111                "\n"
10112                "private:\n"
10113                "#endif\n"
10114                "  int j;\n"
10115                "};\n",
10116                Style);
10117   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10118   verifyFormat("struct foo {\n"
10119                "private:\n"
10120                "  void f() {}\n"
10121                "\n"
10122                "private:\n"
10123                "  int i;\n"
10124                "\n"
10125                "protected:\n"
10126                "  int j;\n"
10127                "};\n",
10128                Style);
10129   verifyFormat("struct foo {\n"
10130                "private:\n"
10131                "  void f() {}\n"
10132                "\n"
10133                "private:\n"
10134                "  int i;\n"
10135                "\n"
10136                "protected:\n"
10137                "  int j;\n"
10138                "};\n",
10139                "struct foo {\n"
10140                "private:\n"
10141                "  void f() {}\n"
10142                "private:\n"
10143                "  int i;\n"
10144                "protected:\n"
10145                "  int j;\n"
10146                "};\n",
10147                Style);
10148   verifyFormat("struct foo { /* comment */\n"
10149                "private:\n"
10150                "  int i;\n"
10151                "  // comment\n"
10152                "\n"
10153                "private:\n"
10154                "  int j;\n"
10155                "};\n",
10156                "struct foo { /* comment */\n"
10157                "private:\n"
10158                "  int i;\n"
10159                "  // comment\n"
10160                "\n"
10161                "private:\n"
10162                "  int j;\n"
10163                "};\n",
10164                Style);
10165   verifyFormat("struct foo {\n"
10166                "#ifdef FOO\n"
10167                "#endif\n"
10168                "\n"
10169                "private:\n"
10170                "  int i;\n"
10171                "#ifdef FOO\n"
10172                "\n"
10173                "private:\n"
10174                "#endif\n"
10175                "  int j;\n"
10176                "};\n",
10177                "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_Leave;
10189   EXPECT_EQ("struct foo {\n"
10190             "\n"
10191             "private:\n"
10192             "  void f() {}\n"
10193             "\n"
10194             "private:\n"
10195             "  int i;\n"
10196             "\n"
10197             "protected:\n"
10198             "  int j;\n"
10199             "};\n",
10200             format("struct foo {\n"
10201                    "\n"
10202                    "private:\n"
10203                    "  void f() {}\n"
10204                    "\n"
10205                    "private:\n"
10206                    "  int i;\n"
10207                    "\n"
10208                    "protected:\n"
10209                    "  int j;\n"
10210                    "};\n",
10211                    Style));
10212   verifyFormat("struct foo {\n"
10213                "private:\n"
10214                "  void f() {}\n"
10215                "private:\n"
10216                "  int i;\n"
10217                "protected:\n"
10218                "  int j;\n"
10219                "};\n",
10220                Style);
10221   EXPECT_EQ("struct foo { /* comment */\n"
10222             "\n"
10223             "private:\n"
10224             "  int i;\n"
10225             "  // comment\n"
10226             "\n"
10227             "private:\n"
10228             "  int j;\n"
10229             "};\n",
10230             format("struct foo { /* comment */\n"
10231                    "\n"
10232                    "private:\n"
10233                    "  int i;\n"
10234                    "  // comment\n"
10235                    "\n"
10236                    "private:\n"
10237                    "  int j;\n"
10238                    "};\n",
10239                    Style));
10240   verifyFormat("struct foo { /* comment */\n"
10241                "private:\n"
10242                "  int i;\n"
10243                "  // comment\n"
10244                "private:\n"
10245                "  int j;\n"
10246                "};\n",
10247                Style);
10248   EXPECT_EQ("struct foo {\n"
10249             "#ifdef FOO\n"
10250             "#endif\n"
10251             "\n"
10252             "private:\n"
10253             "  int i;\n"
10254             "#ifdef FOO\n"
10255             "\n"
10256             "private:\n"
10257             "#endif\n"
10258             "  int j;\n"
10259             "};\n",
10260             format("struct foo {\n"
10261                    "#ifdef FOO\n"
10262                    "#endif\n"
10263                    "\n"
10264                    "private:\n"
10265                    "  int i;\n"
10266                    "#ifdef FOO\n"
10267                    "\n"
10268                    "private:\n"
10269                    "#endif\n"
10270                    "  int j;\n"
10271                    "};\n",
10272                    Style));
10273   verifyFormat("struct foo {\n"
10274                "#ifdef FOO\n"
10275                "#endif\n"
10276                "private:\n"
10277                "  int i;\n"
10278                "#ifdef FOO\n"
10279                "private:\n"
10280                "#endif\n"
10281                "  int j;\n"
10282                "};\n",
10283                Style);
10284 
10285   FormatStyle NoEmptyLines = getLLVMStyle();
10286   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10287   verifyFormat("struct foo {\n"
10288                "private:\n"
10289                "  void f() {}\n"
10290                "\n"
10291                "private:\n"
10292                "  int i;\n"
10293                "\n"
10294                "public:\n"
10295                "protected:\n"
10296                "  int j;\n"
10297                "};\n",
10298                NoEmptyLines);
10299 
10300   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10301   verifyFormat("struct foo {\n"
10302                "private:\n"
10303                "  void f() {}\n"
10304                "private:\n"
10305                "  int i;\n"
10306                "public:\n"
10307                "protected:\n"
10308                "  int j;\n"
10309                "};\n",
10310                NoEmptyLines);
10311 
10312   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10313   verifyFormat("struct foo {\n"
10314                "private:\n"
10315                "  void f() {}\n"
10316                "\n"
10317                "private:\n"
10318                "  int i;\n"
10319                "\n"
10320                "public:\n"
10321                "\n"
10322                "protected:\n"
10323                "  int j;\n"
10324                "};\n",
10325                NoEmptyLines);
10326 }
10327 
10328 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10329 
10330   FormatStyle Style = getLLVMStyle();
10331   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10332   verifyFormat("struct foo {\n"
10333                "private:\n"
10334                "  void f() {}\n"
10335                "\n"
10336                "private:\n"
10337                "  int i;\n"
10338                "\n"
10339                "protected:\n"
10340                "  int j;\n"
10341                "};\n",
10342                Style);
10343 
10344   // Check if lines are removed.
10345   verifyFormat("struct foo {\n"
10346                "private:\n"
10347                "  void f() {}\n"
10348                "\n"
10349                "private:\n"
10350                "  int i;\n"
10351                "\n"
10352                "protected:\n"
10353                "  int j;\n"
10354                "};\n",
10355                "struct foo {\n"
10356                "private:\n"
10357                "\n"
10358                "  void f() {}\n"
10359                "\n"
10360                "private:\n"
10361                "\n"
10362                "  int i;\n"
10363                "\n"
10364                "protected:\n"
10365                "\n"
10366                "  int j;\n"
10367                "};\n",
10368                Style);
10369 
10370   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10371   verifyFormat("struct foo {\n"
10372                "private:\n"
10373                "\n"
10374                "  void f() {}\n"
10375                "\n"
10376                "private:\n"
10377                "\n"
10378                "  int i;\n"
10379                "\n"
10380                "protected:\n"
10381                "\n"
10382                "  int j;\n"
10383                "};\n",
10384                Style);
10385 
10386   // Check if lines are added.
10387   verifyFormat("struct foo {\n"
10388                "private:\n"
10389                "\n"
10390                "  void f() {}\n"
10391                "\n"
10392                "private:\n"
10393                "\n"
10394                "  int i;\n"
10395                "\n"
10396                "protected:\n"
10397                "\n"
10398                "  int j;\n"
10399                "};\n",
10400                "struct foo {\n"
10401                "private:\n"
10402                "  void f() {}\n"
10403                "\n"
10404                "private:\n"
10405                "  int i;\n"
10406                "\n"
10407                "protected:\n"
10408                "  int j;\n"
10409                "};\n",
10410                Style);
10411 
10412   // Leave tests rely on the code layout, test::messUp can not be used.
10413   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10414   Style.MaxEmptyLinesToKeep = 0u;
10415   verifyFormat("struct foo {\n"
10416                "private:\n"
10417                "  void f() {}\n"
10418                "\n"
10419                "private:\n"
10420                "  int i;\n"
10421                "\n"
10422                "protected:\n"
10423                "  int j;\n"
10424                "};\n",
10425                Style);
10426 
10427   // Check if MaxEmptyLinesToKeep is respected.
10428   EXPECT_EQ("struct foo {\n"
10429             "private:\n"
10430             "  void f() {}\n"
10431             "\n"
10432             "private:\n"
10433             "  int i;\n"
10434             "\n"
10435             "protected:\n"
10436             "  int j;\n"
10437             "};\n",
10438             format("struct foo {\n"
10439                    "private:\n"
10440                    "\n\n\n"
10441                    "  void f() {}\n"
10442                    "\n"
10443                    "private:\n"
10444                    "\n\n\n"
10445                    "  int i;\n"
10446                    "\n"
10447                    "protected:\n"
10448                    "\n\n\n"
10449                    "  int j;\n"
10450                    "};\n",
10451                    Style));
10452 
10453   Style.MaxEmptyLinesToKeep = 1u;
10454   EXPECT_EQ("struct foo {\n"
10455             "private:\n"
10456             "\n"
10457             "  void f() {}\n"
10458             "\n"
10459             "private:\n"
10460             "\n"
10461             "  int i;\n"
10462             "\n"
10463             "protected:\n"
10464             "\n"
10465             "  int j;\n"
10466             "};\n",
10467             format("struct foo {\n"
10468                    "private:\n"
10469                    "\n"
10470                    "  void f() {}\n"
10471                    "\n"
10472                    "private:\n"
10473                    "\n"
10474                    "  int i;\n"
10475                    "\n"
10476                    "protected:\n"
10477                    "\n"
10478                    "  int j;\n"
10479                    "};\n",
10480                    Style));
10481   // Check if no lines are kept.
10482   EXPECT_EQ("struct foo {\n"
10483             "private:\n"
10484             "  void f() {}\n"
10485             "\n"
10486             "private:\n"
10487             "  int i;\n"
10488             "\n"
10489             "protected:\n"
10490             "  int j;\n"
10491             "};\n",
10492             format("struct foo {\n"
10493                    "private:\n"
10494                    "  void f() {}\n"
10495                    "\n"
10496                    "private:\n"
10497                    "  int i;\n"
10498                    "\n"
10499                    "protected:\n"
10500                    "  int j;\n"
10501                    "};\n",
10502                    Style));
10503   // Check if MaxEmptyLinesToKeep is respected.
10504   EXPECT_EQ("struct foo {\n"
10505             "private:\n"
10506             "\n"
10507             "  void f() {}\n"
10508             "\n"
10509             "private:\n"
10510             "\n"
10511             "  int i;\n"
10512             "\n"
10513             "protected:\n"
10514             "\n"
10515             "  int j;\n"
10516             "};\n",
10517             format("struct foo {\n"
10518                    "private:\n"
10519                    "\n\n\n"
10520                    "  void f() {}\n"
10521                    "\n"
10522                    "private:\n"
10523                    "\n\n\n"
10524                    "  int i;\n"
10525                    "\n"
10526                    "protected:\n"
10527                    "\n\n\n"
10528                    "  int j;\n"
10529                    "};\n",
10530                    Style));
10531 
10532   Style.MaxEmptyLinesToKeep = 10u;
10533   EXPECT_EQ("struct foo {\n"
10534             "private:\n"
10535             "\n\n\n"
10536             "  void f() {}\n"
10537             "\n"
10538             "private:\n"
10539             "\n\n\n"
10540             "  int i;\n"
10541             "\n"
10542             "protected:\n"
10543             "\n\n\n"
10544             "  int j;\n"
10545             "};\n",
10546             format("struct foo {\n"
10547                    "private:\n"
10548                    "\n\n\n"
10549                    "  void f() {}\n"
10550                    "\n"
10551                    "private:\n"
10552                    "\n\n\n"
10553                    "  int i;\n"
10554                    "\n"
10555                    "protected:\n"
10556                    "\n\n\n"
10557                    "  int j;\n"
10558                    "};\n",
10559                    Style));
10560 
10561   // Test with comments.
10562   Style = getLLVMStyle();
10563   verifyFormat("struct foo {\n"
10564                "private:\n"
10565                "  // comment\n"
10566                "  void f() {}\n"
10567                "\n"
10568                "private: /* comment */\n"
10569                "  int i;\n"
10570                "};\n",
10571                Style);
10572   verifyFormat("struct foo {\n"
10573                "private:\n"
10574                "  // comment\n"
10575                "  void f() {}\n"
10576                "\n"
10577                "private: /* comment */\n"
10578                "  int i;\n"
10579                "};\n",
10580                "struct foo {\n"
10581                "private:\n"
10582                "\n"
10583                "  // comment\n"
10584                "  void f() {}\n"
10585                "\n"
10586                "private: /* comment */\n"
10587                "\n"
10588                "  int i;\n"
10589                "};\n",
10590                Style);
10591 
10592   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10593   verifyFormat("struct foo {\n"
10594                "private:\n"
10595                "\n"
10596                "  // comment\n"
10597                "  void f() {}\n"
10598                "\n"
10599                "private: /* comment */\n"
10600                "\n"
10601                "  int i;\n"
10602                "};\n",
10603                "struct foo {\n"
10604                "private:\n"
10605                "  // comment\n"
10606                "  void f() {}\n"
10607                "\n"
10608                "private: /* comment */\n"
10609                "  int i;\n"
10610                "};\n",
10611                Style);
10612   verifyFormat("struct foo {\n"
10613                "private:\n"
10614                "\n"
10615                "  // comment\n"
10616                "  void f() {}\n"
10617                "\n"
10618                "private: /* comment */\n"
10619                "\n"
10620                "  int i;\n"
10621                "};\n",
10622                Style);
10623 
10624   // Test with preprocessor defines.
10625   Style = getLLVMStyle();
10626   verifyFormat("struct foo {\n"
10627                "private:\n"
10628                "#ifdef FOO\n"
10629                "#endif\n"
10630                "  void f() {}\n"
10631                "};\n",
10632                Style);
10633   verifyFormat("struct foo {\n"
10634                "private:\n"
10635                "#ifdef FOO\n"
10636                "#endif\n"
10637                "  void f() {}\n"
10638                "};\n",
10639                "struct foo {\n"
10640                "private:\n"
10641                "\n"
10642                "#ifdef FOO\n"
10643                "#endif\n"
10644                "  void f() {}\n"
10645                "};\n",
10646                Style);
10647 
10648   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10649   verifyFormat("struct foo {\n"
10650                "private:\n"
10651                "\n"
10652                "#ifdef FOO\n"
10653                "#endif\n"
10654                "  void f() {}\n"
10655                "};\n",
10656                "struct foo {\n"
10657                "private:\n"
10658                "#ifdef FOO\n"
10659                "#endif\n"
10660                "  void f() {}\n"
10661                "};\n",
10662                Style);
10663   verifyFormat("struct foo {\n"
10664                "private:\n"
10665                "\n"
10666                "#ifdef FOO\n"
10667                "#endif\n"
10668                "  void f() {}\n"
10669                "};\n",
10670                Style);
10671 }
10672 
10673 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
10674   // Combined tests of EmptyLineAfterAccessModifier and
10675   // EmptyLineBeforeAccessModifier.
10676   FormatStyle Style = getLLVMStyle();
10677   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10678   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10679   verifyFormat("struct foo {\n"
10680                "private:\n"
10681                "\n"
10682                "protected:\n"
10683                "};\n",
10684                Style);
10685 
10686   Style.MaxEmptyLinesToKeep = 10u;
10687   // Both remove all new lines.
10688   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10689   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10690   verifyFormat("struct foo {\n"
10691                "private:\n"
10692                "protected:\n"
10693                "};\n",
10694                "struct foo {\n"
10695                "private:\n"
10696                "\n\n\n"
10697                "protected:\n"
10698                "};\n",
10699                Style);
10700 
10701   // Leave tests rely on the code layout, test::messUp can not be used.
10702   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10703   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10704   Style.MaxEmptyLinesToKeep = 10u;
10705   EXPECT_EQ("struct foo {\n"
10706             "private:\n"
10707             "\n\n\n"
10708             "protected:\n"
10709             "};\n",
10710             format("struct foo {\n"
10711                    "private:\n"
10712                    "\n\n\n"
10713                    "protected:\n"
10714                    "};\n",
10715                    Style));
10716   Style.MaxEmptyLinesToKeep = 3u;
10717   EXPECT_EQ("struct foo {\n"
10718             "private:\n"
10719             "\n\n\n"
10720             "protected:\n"
10721             "};\n",
10722             format("struct foo {\n"
10723                    "private:\n"
10724                    "\n\n\n"
10725                    "protected:\n"
10726                    "};\n",
10727                    Style));
10728   Style.MaxEmptyLinesToKeep = 1u;
10729   EXPECT_EQ("struct foo {\n"
10730             "private:\n"
10731             "\n\n\n"
10732             "protected:\n"
10733             "};\n",
10734             format("struct foo {\n"
10735                    "private:\n"
10736                    "\n\n\n"
10737                    "protected:\n"
10738                    "};\n",
10739                    Style)); // Based on new lines in original document and not
10740                             // on the setting.
10741 
10742   Style.MaxEmptyLinesToKeep = 10u;
10743   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10744   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10745   // Newlines are kept if they are greater than zero,
10746   // test::messUp removes all new lines which changes the logic
10747   EXPECT_EQ("struct foo {\n"
10748             "private:\n"
10749             "\n\n\n"
10750             "protected:\n"
10751             "};\n",
10752             format("struct foo {\n"
10753                    "private:\n"
10754                    "\n\n\n"
10755                    "protected:\n"
10756                    "};\n",
10757                    Style));
10758 
10759   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10760   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10761   // test::messUp removes all new lines which changes the logic
10762   EXPECT_EQ("struct foo {\n"
10763             "private:\n"
10764             "\n\n\n"
10765             "protected:\n"
10766             "};\n",
10767             format("struct foo {\n"
10768                    "private:\n"
10769                    "\n\n\n"
10770                    "protected:\n"
10771                    "};\n",
10772                    Style));
10773 
10774   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10775   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10776   EXPECT_EQ("struct foo {\n"
10777             "private:\n"
10778             "\n\n\n"
10779             "protected:\n"
10780             "};\n",
10781             format("struct foo {\n"
10782                    "private:\n"
10783                    "\n\n\n"
10784                    "protected:\n"
10785                    "};\n",
10786                    Style)); // test::messUp removes all new lines which changes
10787                             // the logic.
10788 
10789   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10790   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10791   verifyFormat("struct foo {\n"
10792                "private:\n"
10793                "protected:\n"
10794                "};\n",
10795                "struct foo {\n"
10796                "private:\n"
10797                "\n\n\n"
10798                "protected:\n"
10799                "};\n",
10800                Style);
10801 
10802   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10803   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10804   EXPECT_EQ("struct foo {\n"
10805             "private:\n"
10806             "\n\n\n"
10807             "protected:\n"
10808             "};\n",
10809             format("struct foo {\n"
10810                    "private:\n"
10811                    "\n\n\n"
10812                    "protected:\n"
10813                    "};\n",
10814                    Style)); // test::messUp removes all new lines which changes
10815                             // the logic.
10816 
10817   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10818   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10819   verifyFormat("struct foo {\n"
10820                "private:\n"
10821                "protected:\n"
10822                "};\n",
10823                "struct foo {\n"
10824                "private:\n"
10825                "\n\n\n"
10826                "protected:\n"
10827                "};\n",
10828                Style);
10829 
10830   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10831   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10832   verifyFormat("struct foo {\n"
10833                "private:\n"
10834                "protected:\n"
10835                "};\n",
10836                "struct foo {\n"
10837                "private:\n"
10838                "\n\n\n"
10839                "protected:\n"
10840                "};\n",
10841                Style);
10842 
10843   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10844   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10845   verifyFormat("struct foo {\n"
10846                "private:\n"
10847                "protected:\n"
10848                "};\n",
10849                "struct foo {\n"
10850                "private:\n"
10851                "\n\n\n"
10852                "protected:\n"
10853                "};\n",
10854                Style);
10855 
10856   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10857   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10858   verifyFormat("struct foo {\n"
10859                "private:\n"
10860                "protected:\n"
10861                "};\n",
10862                "struct foo {\n"
10863                "private:\n"
10864                "\n\n\n"
10865                "protected:\n"
10866                "};\n",
10867                Style);
10868 }
10869 
10870 TEST_F(FormatTest, FormatsArrays) {
10871   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10872                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
10873   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
10874                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
10875   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
10876                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
10877   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10878                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
10879   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10880                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
10881   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10882                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10883                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
10884   verifyFormat(
10885       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
10886       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10887       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
10888   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
10889                "    .aaaaaaaaaaaaaaaaaaaaaa();");
10890 
10891   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
10892                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
10893   verifyFormat(
10894       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
10895       "                                  .aaaaaaa[0]\n"
10896       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
10897   verifyFormat("a[::b::c];");
10898 
10899   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
10900 
10901   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
10902   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
10903 }
10904 
10905 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
10906   verifyFormat("(a)->b();");
10907   verifyFormat("--a;");
10908 }
10909 
10910 TEST_F(FormatTest, HandlesIncludeDirectives) {
10911   verifyFormat("#include <string>\n"
10912                "#include <a/b/c.h>\n"
10913                "#include \"a/b/string\"\n"
10914                "#include \"string.h\"\n"
10915                "#include \"string.h\"\n"
10916                "#include <a-a>\n"
10917                "#include < path with space >\n"
10918                "#include_next <test.h>"
10919                "#include \"abc.h\" // this is included for ABC\n"
10920                "#include \"some long include\" // with a comment\n"
10921                "#include \"some very long include path\"\n"
10922                "#include <some/very/long/include/path>\n",
10923                getLLVMStyleWithColumns(35));
10924   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
10925   EXPECT_EQ("#include <a>", format("#include<a>"));
10926 
10927   verifyFormat("#import <string>");
10928   verifyFormat("#import <a/b/c.h>");
10929   verifyFormat("#import \"a/b/string\"");
10930   verifyFormat("#import \"string.h\"");
10931   verifyFormat("#import \"string.h\"");
10932   verifyFormat("#if __has_include(<strstream>)\n"
10933                "#include <strstream>\n"
10934                "#endif");
10935 
10936   verifyFormat("#define MY_IMPORT <a/b>");
10937 
10938   verifyFormat("#if __has_include(<a/b>)");
10939   verifyFormat("#if __has_include_next(<a/b>)");
10940   verifyFormat("#define F __has_include(<a/b>)");
10941   verifyFormat("#define F __has_include_next(<a/b>)");
10942 
10943   // Protocol buffer definition or missing "#".
10944   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
10945                getLLVMStyleWithColumns(30));
10946 
10947   FormatStyle Style = getLLVMStyle();
10948   Style.AlwaysBreakBeforeMultilineStrings = true;
10949   Style.ColumnLimit = 0;
10950   verifyFormat("#import \"abc.h\"", Style);
10951 
10952   // But 'import' might also be a regular C++ namespace.
10953   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10954                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10955 }
10956 
10957 //===----------------------------------------------------------------------===//
10958 // Error recovery tests.
10959 //===----------------------------------------------------------------------===//
10960 
10961 TEST_F(FormatTest, IncompleteParameterLists) {
10962   FormatStyle NoBinPacking = getLLVMStyle();
10963   NoBinPacking.BinPackParameters = false;
10964   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
10965                "                        double *min_x,\n"
10966                "                        double *max_x,\n"
10967                "                        double *min_y,\n"
10968                "                        double *max_y,\n"
10969                "                        double *min_z,\n"
10970                "                        double *max_z, ) {}",
10971                NoBinPacking);
10972 }
10973 
10974 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
10975   verifyFormat("void f() { return; }\n42");
10976   verifyFormat("void f() {\n"
10977                "  if (0)\n"
10978                "    return;\n"
10979                "}\n"
10980                "42");
10981   verifyFormat("void f() { return }\n42");
10982   verifyFormat("void f() {\n"
10983                "  if (0)\n"
10984                "    return\n"
10985                "}\n"
10986                "42");
10987 }
10988 
10989 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
10990   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
10991   EXPECT_EQ("void f() {\n"
10992             "  if (a)\n"
10993             "    return\n"
10994             "}",
10995             format("void  f  (  )  {  if  ( a )  return  }"));
10996   EXPECT_EQ("namespace N {\n"
10997             "void f()\n"
10998             "}",
10999             format("namespace  N  {  void f()  }"));
11000   EXPECT_EQ("namespace N {\n"
11001             "void f() {}\n"
11002             "void g()\n"
11003             "} // namespace N",
11004             format("namespace N  { void f( ) { } void g( ) }"));
11005 }
11006 
11007 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11008   verifyFormat("int aaaaaaaa =\n"
11009                "    // Overlylongcomment\n"
11010                "    b;",
11011                getLLVMStyleWithColumns(20));
11012   verifyFormat("function(\n"
11013                "    ShortArgument,\n"
11014                "    LoooooooooooongArgument);\n",
11015                getLLVMStyleWithColumns(20));
11016 }
11017 
11018 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11019   verifyFormat("public:");
11020   verifyFormat("class A {\n"
11021                "public\n"
11022                "  void f() {}\n"
11023                "};");
11024   verifyFormat("public\n"
11025                "int qwerty;");
11026   verifyFormat("public\n"
11027                "B {}");
11028   verifyFormat("public\n"
11029                "{}");
11030   verifyFormat("public\n"
11031                "B { int x; }");
11032 }
11033 
11034 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11035   verifyFormat("{");
11036   verifyFormat("#})");
11037   verifyNoCrash("(/**/[:!] ?[).");
11038 }
11039 
11040 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11041   // Found by oss-fuzz:
11042   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11043   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11044   Style.ColumnLimit = 60;
11045   verifyNoCrash(
11046       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11047       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11048       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11049       Style);
11050 }
11051 
11052 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11053   verifyFormat("do {\n}");
11054   verifyFormat("do {\n}\n"
11055                "f();");
11056   verifyFormat("do {\n}\n"
11057                "wheeee(fun);");
11058   verifyFormat("do {\n"
11059                "  f();\n"
11060                "}");
11061 }
11062 
11063 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11064   verifyFormat("if {\n  foo;\n  foo();\n}");
11065   verifyFormat("switch {\n  foo;\n  foo();\n}");
11066   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11067   verifyFormat("while {\n  foo;\n  foo();\n}");
11068   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11069 }
11070 
11071 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11072   verifyIncompleteFormat("namespace {\n"
11073                          "class Foo { Foo (\n"
11074                          "};\n"
11075                          "} // namespace");
11076 }
11077 
11078 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11079   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11080   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11081   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11082   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11083 
11084   EXPECT_EQ("{\n"
11085             "  {\n"
11086             "    breakme(\n"
11087             "        qwe);\n"
11088             "  }\n",
11089             format("{\n"
11090                    "    {\n"
11091                    " breakme(qwe);\n"
11092                    "}\n",
11093                    getLLVMStyleWithColumns(10)));
11094 }
11095 
11096 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11097   verifyFormat("int x = {\n"
11098                "    avariable,\n"
11099                "    b(alongervariable)};",
11100                getLLVMStyleWithColumns(25));
11101 }
11102 
11103 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11104   verifyFormat("return (a)(b){1, 2, 3};");
11105 }
11106 
11107 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11108   verifyFormat("vector<int> x{1, 2, 3, 4};");
11109   verifyFormat("vector<int> x{\n"
11110                "    1,\n"
11111                "    2,\n"
11112                "    3,\n"
11113                "    4,\n"
11114                "};");
11115   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11116   verifyFormat("f({1, 2});");
11117   verifyFormat("auto v = Foo{-1};");
11118   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11119   verifyFormat("Class::Class : member{1, 2, 3} {}");
11120   verifyFormat("new vector<int>{1, 2, 3};");
11121   verifyFormat("new int[3]{1, 2, 3};");
11122   verifyFormat("new int{1};");
11123   verifyFormat("return {arg1, arg2};");
11124   verifyFormat("return {arg1, SomeType{parameter}};");
11125   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11126   verifyFormat("new T{arg1, arg2};");
11127   verifyFormat("f(MyMap[{composite, key}]);");
11128   verifyFormat("class Class {\n"
11129                "  T member = {arg1, arg2};\n"
11130                "};");
11131   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11132   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11133   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11134   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11135   verifyFormat("int a = std::is_integral<int>{} + 0;");
11136 
11137   verifyFormat("int foo(int i) { return fo1{}(i); }");
11138   verifyFormat("int foo(int i) { return fo1{}(i); }");
11139   verifyFormat("auto i = decltype(x){};");
11140   verifyFormat("auto i = typeof(x){};");
11141   verifyFormat("auto i = _Atomic(x){};");
11142   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11143   verifyFormat("Node n{1, Node{1000}, //\n"
11144                "       2};");
11145   verifyFormat("Aaaa aaaaaaa{\n"
11146                "    {\n"
11147                "        aaaa,\n"
11148                "    },\n"
11149                "};");
11150   verifyFormat("class C : public D {\n"
11151                "  SomeClass SC{2};\n"
11152                "};");
11153   verifyFormat("class C : public A {\n"
11154                "  class D : public B {\n"
11155                "    void f() { int i{2}; }\n"
11156                "  };\n"
11157                "};");
11158   verifyFormat("#define A {a, a},");
11159 
11160   // Avoid breaking between equal sign and opening brace
11161   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11162   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11163   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11164                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11165                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11166                "     {\"ccccccccccccccccccccc\", 2}};",
11167                AvoidBreakingFirstArgument);
11168 
11169   // Binpacking only if there is no trailing comma
11170   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11171                "                      cccccccccc, dddddddddd};",
11172                getLLVMStyleWithColumns(50));
11173   verifyFormat("const Aaaaaa aaaaa = {\n"
11174                "    aaaaaaaaaaa,\n"
11175                "    bbbbbbbbbbb,\n"
11176                "    ccccccccccc,\n"
11177                "    ddddddddddd,\n"
11178                "};",
11179                getLLVMStyleWithColumns(50));
11180 
11181   // Cases where distinguising braced lists and blocks is hard.
11182   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11183   verifyFormat("void f() {\n"
11184                "  return; // comment\n"
11185                "}\n"
11186                "SomeType t;");
11187   verifyFormat("void f() {\n"
11188                "  if (a) {\n"
11189                "    f();\n"
11190                "  }\n"
11191                "}\n"
11192                "SomeType t;");
11193 
11194   // In combination with BinPackArguments = false.
11195   FormatStyle NoBinPacking = getLLVMStyle();
11196   NoBinPacking.BinPackArguments = false;
11197   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11198                "                      bbbbb,\n"
11199                "                      ccccc,\n"
11200                "                      ddddd,\n"
11201                "                      eeeee,\n"
11202                "                      ffffff,\n"
11203                "                      ggggg,\n"
11204                "                      hhhhhh,\n"
11205                "                      iiiiii,\n"
11206                "                      jjjjjj,\n"
11207                "                      kkkkkk};",
11208                NoBinPacking);
11209   verifyFormat("const Aaaaaa aaaaa = {\n"
11210                "    aaaaa,\n"
11211                "    bbbbb,\n"
11212                "    ccccc,\n"
11213                "    ddddd,\n"
11214                "    eeeee,\n"
11215                "    ffffff,\n"
11216                "    ggggg,\n"
11217                "    hhhhhh,\n"
11218                "    iiiiii,\n"
11219                "    jjjjjj,\n"
11220                "    kkkkkk,\n"
11221                "};",
11222                NoBinPacking);
11223   verifyFormat(
11224       "const Aaaaaa aaaaa = {\n"
11225       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11226       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11227       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11228       "};",
11229       NoBinPacking);
11230 
11231   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11232   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11233             "    CDDDP83848_BMCR_REGISTER,\n"
11234             "    CDDDP83848_BMSR_REGISTER,\n"
11235             "    CDDDP83848_RBR_REGISTER};",
11236             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11237                    "                                CDDDP83848_BMSR_REGISTER,\n"
11238                    "                                CDDDP83848_RBR_REGISTER};",
11239                    NoBinPacking));
11240 
11241   // FIXME: The alignment of these trailing comments might be bad. Then again,
11242   // this might be utterly useless in real code.
11243   verifyFormat("Constructor::Constructor()\n"
11244                "    : some_value{         //\n"
11245                "                 aaaaaaa, //\n"
11246                "                 bbbbbbb} {}");
11247 
11248   // In braced lists, the first comment is always assumed to belong to the
11249   // first element. Thus, it can be moved to the next or previous line as
11250   // appropriate.
11251   EXPECT_EQ("function({// First element:\n"
11252             "          1,\n"
11253             "          // Second element:\n"
11254             "          2});",
11255             format("function({\n"
11256                    "    // First element:\n"
11257                    "    1,\n"
11258                    "    // Second element:\n"
11259                    "    2});"));
11260   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11261             "    // First element:\n"
11262             "    1,\n"
11263             "    // Second element:\n"
11264             "    2};",
11265             format("std::vector<int> MyNumbers{// First element:\n"
11266                    "                           1,\n"
11267                    "                           // Second element:\n"
11268                    "                           2};",
11269                    getLLVMStyleWithColumns(30)));
11270   // A trailing comma should still lead to an enforced line break and no
11271   // binpacking.
11272   EXPECT_EQ("vector<int> SomeVector = {\n"
11273             "    // aaa\n"
11274             "    1,\n"
11275             "    2,\n"
11276             "};",
11277             format("vector<int> SomeVector = { // aaa\n"
11278                    "    1, 2, };"));
11279 
11280   // C++11 brace initializer list l-braces should not be treated any differently
11281   // when breaking before lambda bodies is enabled
11282   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11283   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11284   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11285   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11286   verifyFormat(
11287       "std::runtime_error{\n"
11288       "    \"Long string which will force a break onto the next line...\"};",
11289       BreakBeforeLambdaBody);
11290 
11291   FormatStyle ExtraSpaces = getLLVMStyle();
11292   ExtraSpaces.Cpp11BracedListStyle = false;
11293   ExtraSpaces.ColumnLimit = 75;
11294   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11295   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11296   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11297   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11298   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11299   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11300   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11301   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11302   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11303   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11304   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11305   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11306   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11307   verifyFormat("class Class {\n"
11308                "  T member = { arg1, arg2 };\n"
11309                "};",
11310                ExtraSpaces);
11311   verifyFormat(
11312       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11313       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11314       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11315       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11316       ExtraSpaces);
11317   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11318   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11319                ExtraSpaces);
11320   verifyFormat(
11321       "someFunction(OtherParam,\n"
11322       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11323       "                         param1, param2,\n"
11324       "                         // comment 2\n"
11325       "                         param3, param4 });",
11326       ExtraSpaces);
11327   verifyFormat(
11328       "std::this_thread::sleep_for(\n"
11329       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11330       ExtraSpaces);
11331   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11332                "    aaaaaaa,\n"
11333                "    aaaaaaaaaa,\n"
11334                "    aaaaa,\n"
11335                "    aaaaaaaaaaaaaaa,\n"
11336                "    aaa,\n"
11337                "    aaaaaaaaaa,\n"
11338                "    a,\n"
11339                "    aaaaaaaaaaaaaaaaaaaaa,\n"
11340                "    aaaaaaaaaaaa,\n"
11341                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
11342                "    aaaaaaa,\n"
11343                "    a};");
11344   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
11345   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
11346   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
11347 
11348   // Avoid breaking between initializer/equal sign and opening brace
11349   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
11350   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
11351                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11352                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11353                "  { \"ccccccccccccccccccccc\", 2 }\n"
11354                "};",
11355                ExtraSpaces);
11356   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
11357                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11358                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11359                "  { \"ccccccccccccccccccccc\", 2 }\n"
11360                "};",
11361                ExtraSpaces);
11362 
11363   FormatStyle SpaceBeforeBrace = getLLVMStyle();
11364   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
11365   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
11366   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
11367 
11368   FormatStyle SpaceBetweenBraces = getLLVMStyle();
11369   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
11370   SpaceBetweenBraces.SpacesInParentheses = true;
11371   SpaceBetweenBraces.SpacesInSquareBrackets = true;
11372   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
11373   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
11374   verifyFormat("vector< int > x{ // comment 1\n"
11375                "                 1, 2, 3, 4 };",
11376                SpaceBetweenBraces);
11377   SpaceBetweenBraces.ColumnLimit = 20;
11378   EXPECT_EQ("vector< int > x{\n"
11379             "    1, 2, 3, 4 };",
11380             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11381   SpaceBetweenBraces.ColumnLimit = 24;
11382   EXPECT_EQ("vector< int > x{ 1, 2,\n"
11383             "                 3, 4 };",
11384             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11385   EXPECT_EQ("vector< int > x{\n"
11386             "    1,\n"
11387             "    2,\n"
11388             "    3,\n"
11389             "    4,\n"
11390             "};",
11391             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
11392   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
11393   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
11394   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
11395 }
11396 
11397 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
11398   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11399                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11400                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11401                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11402                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11403                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11404   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
11405                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11406                "                 1, 22, 333, 4444, 55555, //\n"
11407                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11408                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11409   verifyFormat(
11410       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11411       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11412       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
11413       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11414       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11415       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11416       "                 7777777};");
11417   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11418                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11419                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11420   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11421                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11422                "    // Separating comment.\n"
11423                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
11424   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11425                "    // Leading comment\n"
11426                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11427                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11428   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11429                "                 1, 1, 1, 1};",
11430                getLLVMStyleWithColumns(39));
11431   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11432                "                 1, 1, 1, 1};",
11433                getLLVMStyleWithColumns(38));
11434   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
11435                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
11436                getLLVMStyleWithColumns(43));
11437   verifyFormat(
11438       "static unsigned SomeValues[10][3] = {\n"
11439       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
11440       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
11441   verifyFormat("static auto fields = new vector<string>{\n"
11442                "    \"aaaaaaaaaaaaa\",\n"
11443                "    \"aaaaaaaaaaaaa\",\n"
11444                "    \"aaaaaaaaaaaa\",\n"
11445                "    \"aaaaaaaaaaaaaa\",\n"
11446                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11447                "    \"aaaaaaaaaaaa\",\n"
11448                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11449                "};");
11450   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
11451   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
11452                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
11453                "                 3, cccccccccccccccccccccc};",
11454                getLLVMStyleWithColumns(60));
11455 
11456   // Trailing commas.
11457   verifyFormat("vector<int> x = {\n"
11458                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
11459                "};",
11460                getLLVMStyleWithColumns(39));
11461   verifyFormat("vector<int> x = {\n"
11462                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
11463                "};",
11464                getLLVMStyleWithColumns(39));
11465   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11466                "                 1, 1, 1, 1,\n"
11467                "                 /**/ /**/};",
11468                getLLVMStyleWithColumns(39));
11469 
11470   // Trailing comment in the first line.
11471   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
11472                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
11473                "    111111111,  222222222,  3333333333,  444444444,  //\n"
11474                "    11111111,   22222222,   333333333,   44444444};");
11475   // Trailing comment in the last line.
11476   verifyFormat("int aaaaa[] = {\n"
11477                "    1, 2, 3, // comment\n"
11478                "    4, 5, 6  // comment\n"
11479                "};");
11480 
11481   // With nested lists, we should either format one item per line or all nested
11482   // lists one on line.
11483   // FIXME: For some nested lists, we can do better.
11484   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
11485                "        {aaaaaaaaaaaaaaaaaaa},\n"
11486                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
11487                "        {aaaaaaaaaaaaaaaaa}};",
11488                getLLVMStyleWithColumns(60));
11489   verifyFormat(
11490       "SomeStruct my_struct_array = {\n"
11491       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
11492       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
11493       "    {aaa, aaa},\n"
11494       "    {aaa, aaa},\n"
11495       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
11496       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
11497       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
11498 
11499   // No column layout should be used here.
11500   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
11501                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
11502 
11503   verifyNoCrash("a<,");
11504 
11505   // No braced initializer here.
11506   verifyFormat("void f() {\n"
11507                "  struct Dummy {};\n"
11508                "  f(v);\n"
11509                "}");
11510 
11511   // Long lists should be formatted in columns even if they are nested.
11512   verifyFormat(
11513       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11514       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11515       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11516       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11517       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11518       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
11519 
11520   // Allow "single-column" layout even if that violates the column limit. There
11521   // isn't going to be a better way.
11522   verifyFormat("std::vector<int> a = {\n"
11523                "    aaaaaaaa,\n"
11524                "    aaaaaaaa,\n"
11525                "    aaaaaaaa,\n"
11526                "    aaaaaaaa,\n"
11527                "    aaaaaaaaaa,\n"
11528                "    aaaaaaaa,\n"
11529                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
11530                getLLVMStyleWithColumns(30));
11531   verifyFormat("vector<int> aaaa = {\n"
11532                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11533                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11534                "    aaaaaa.aaaaaaa,\n"
11535                "    aaaaaa.aaaaaaa,\n"
11536                "    aaaaaa.aaaaaaa,\n"
11537                "    aaaaaa.aaaaaaa,\n"
11538                "};");
11539 
11540   // Don't create hanging lists.
11541   verifyFormat("someFunction(Param, {List1, List2,\n"
11542                "                     List3});",
11543                getLLVMStyleWithColumns(35));
11544   verifyFormat("someFunction(Param, Param,\n"
11545                "             {List1, List2,\n"
11546                "              List3});",
11547                getLLVMStyleWithColumns(35));
11548   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
11549                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
11550 }
11551 
11552 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
11553   FormatStyle DoNotMerge = getLLVMStyle();
11554   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11555 
11556   verifyFormat("void f() { return 42; }");
11557   verifyFormat("void f() {\n"
11558                "  return 42;\n"
11559                "}",
11560                DoNotMerge);
11561   verifyFormat("void f() {\n"
11562                "  // Comment\n"
11563                "}");
11564   verifyFormat("{\n"
11565                "#error {\n"
11566                "  int a;\n"
11567                "}");
11568   verifyFormat("{\n"
11569                "  int a;\n"
11570                "#error {\n"
11571                "}");
11572   verifyFormat("void f() {} // comment");
11573   verifyFormat("void f() { int a; } // comment");
11574   verifyFormat("void f() {\n"
11575                "} // comment",
11576                DoNotMerge);
11577   verifyFormat("void f() {\n"
11578                "  int a;\n"
11579                "} // comment",
11580                DoNotMerge);
11581   verifyFormat("void f() {\n"
11582                "} // comment",
11583                getLLVMStyleWithColumns(15));
11584 
11585   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
11586   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
11587 
11588   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
11589   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
11590   verifyFormat("class C {\n"
11591                "  C()\n"
11592                "      : iiiiiiii(nullptr),\n"
11593                "        kkkkkkk(nullptr),\n"
11594                "        mmmmmmm(nullptr),\n"
11595                "        nnnnnnn(nullptr) {}\n"
11596                "};",
11597                getGoogleStyle());
11598 
11599   FormatStyle NoColumnLimit = getLLVMStyle();
11600   NoColumnLimit.ColumnLimit = 0;
11601   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
11602   EXPECT_EQ("class C {\n"
11603             "  A() : b(0) {}\n"
11604             "};",
11605             format("class C{A():b(0){}};", NoColumnLimit));
11606   EXPECT_EQ("A()\n"
11607             "    : b(0) {\n"
11608             "}",
11609             format("A()\n:b(0)\n{\n}", NoColumnLimit));
11610 
11611   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
11612   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
11613       FormatStyle::SFS_None;
11614   EXPECT_EQ("A()\n"
11615             "    : b(0) {\n"
11616             "}",
11617             format("A():b(0){}", DoNotMergeNoColumnLimit));
11618   EXPECT_EQ("A()\n"
11619             "    : b(0) {\n"
11620             "}",
11621             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
11622 
11623   verifyFormat("#define A          \\\n"
11624                "  void f() {       \\\n"
11625                "    int i;         \\\n"
11626                "  }",
11627                getLLVMStyleWithColumns(20));
11628   verifyFormat("#define A           \\\n"
11629                "  void f() { int i; }",
11630                getLLVMStyleWithColumns(21));
11631   verifyFormat("#define A            \\\n"
11632                "  void f() {         \\\n"
11633                "    int i;           \\\n"
11634                "  }                  \\\n"
11635                "  int j;",
11636                getLLVMStyleWithColumns(22));
11637   verifyFormat("#define A             \\\n"
11638                "  void f() { int i; } \\\n"
11639                "  int j;",
11640                getLLVMStyleWithColumns(23));
11641 }
11642 
11643 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
11644   FormatStyle MergeEmptyOnly = getLLVMStyle();
11645   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11646   verifyFormat("class C {\n"
11647                "  int f() {}\n"
11648                "};",
11649                MergeEmptyOnly);
11650   verifyFormat("class C {\n"
11651                "  int f() {\n"
11652                "    return 42;\n"
11653                "  }\n"
11654                "};",
11655                MergeEmptyOnly);
11656   verifyFormat("int f() {}", MergeEmptyOnly);
11657   verifyFormat("int f() {\n"
11658                "  return 42;\n"
11659                "}",
11660                MergeEmptyOnly);
11661 
11662   // Also verify behavior when BraceWrapping.AfterFunction = true
11663   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11664   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
11665   verifyFormat("int f() {}", MergeEmptyOnly);
11666   verifyFormat("class C {\n"
11667                "  int f() {}\n"
11668                "};",
11669                MergeEmptyOnly);
11670 }
11671 
11672 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
11673   FormatStyle MergeInlineOnly = getLLVMStyle();
11674   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11675   verifyFormat("class C {\n"
11676                "  int f() { return 42; }\n"
11677                "};",
11678                MergeInlineOnly);
11679   verifyFormat("int f() {\n"
11680                "  return 42;\n"
11681                "}",
11682                MergeInlineOnly);
11683 
11684   // SFS_Inline implies SFS_Empty
11685   verifyFormat("class C {\n"
11686                "  int f() {}\n"
11687                "};",
11688                MergeInlineOnly);
11689   verifyFormat("int f() {}", MergeInlineOnly);
11690 
11691   // Also verify behavior when BraceWrapping.AfterFunction = true
11692   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11693   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11694   verifyFormat("class C {\n"
11695                "  int f() { return 42; }\n"
11696                "};",
11697                MergeInlineOnly);
11698   verifyFormat("int f()\n"
11699                "{\n"
11700                "  return 42;\n"
11701                "}",
11702                MergeInlineOnly);
11703 
11704   // SFS_Inline implies SFS_Empty
11705   verifyFormat("int f() {}", MergeInlineOnly);
11706   verifyFormat("class C {\n"
11707                "  int f() {}\n"
11708                "};",
11709                MergeInlineOnly);
11710 }
11711 
11712 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
11713   FormatStyle MergeInlineOnly = getLLVMStyle();
11714   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
11715       FormatStyle::SFS_InlineOnly;
11716   verifyFormat("class C {\n"
11717                "  int f() { return 42; }\n"
11718                "};",
11719                MergeInlineOnly);
11720   verifyFormat("int f() {\n"
11721                "  return 42;\n"
11722                "}",
11723                MergeInlineOnly);
11724 
11725   // SFS_InlineOnly does not imply SFS_Empty
11726   verifyFormat("class C {\n"
11727                "  int f() {}\n"
11728                "};",
11729                MergeInlineOnly);
11730   verifyFormat("int f() {\n"
11731                "}",
11732                MergeInlineOnly);
11733 
11734   // Also verify behavior when BraceWrapping.AfterFunction = true
11735   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11736   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11737   verifyFormat("class C {\n"
11738                "  int f() { return 42; }\n"
11739                "};",
11740                MergeInlineOnly);
11741   verifyFormat("int f()\n"
11742                "{\n"
11743                "  return 42;\n"
11744                "}",
11745                MergeInlineOnly);
11746 
11747   // SFS_InlineOnly does not imply SFS_Empty
11748   verifyFormat("int f()\n"
11749                "{\n"
11750                "}",
11751                MergeInlineOnly);
11752   verifyFormat("class C {\n"
11753                "  int f() {}\n"
11754                "};",
11755                MergeInlineOnly);
11756 }
11757 
11758 TEST_F(FormatTest, SplitEmptyFunction) {
11759   FormatStyle Style = getLLVMStyle();
11760   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11761   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11762   Style.BraceWrapping.AfterFunction = true;
11763   Style.BraceWrapping.SplitEmptyFunction = false;
11764   Style.ColumnLimit = 40;
11765 
11766   verifyFormat("int f()\n"
11767                "{}",
11768                Style);
11769   verifyFormat("int f()\n"
11770                "{\n"
11771                "  return 42;\n"
11772                "}",
11773                Style);
11774   verifyFormat("int f()\n"
11775                "{\n"
11776                "  // some comment\n"
11777                "}",
11778                Style);
11779 
11780   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11781   verifyFormat("int f() {}", Style);
11782   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11783                "{}",
11784                Style);
11785   verifyFormat("int f()\n"
11786                "{\n"
11787                "  return 0;\n"
11788                "}",
11789                Style);
11790 
11791   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11792   verifyFormat("class Foo {\n"
11793                "  int f() {}\n"
11794                "};\n",
11795                Style);
11796   verifyFormat("class Foo {\n"
11797                "  int f() { return 0; }\n"
11798                "};\n",
11799                Style);
11800   verifyFormat("class Foo {\n"
11801                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11802                "  {}\n"
11803                "};\n",
11804                Style);
11805   verifyFormat("class Foo {\n"
11806                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11807                "  {\n"
11808                "    return 0;\n"
11809                "  }\n"
11810                "};\n",
11811                Style);
11812 
11813   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11814   verifyFormat("int f() {}", Style);
11815   verifyFormat("int f() { return 0; }", Style);
11816   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11817                "{}",
11818                Style);
11819   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11820                "{\n"
11821                "  return 0;\n"
11822                "}",
11823                Style);
11824 }
11825 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
11826   FormatStyle Style = getLLVMStyle();
11827   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11828   verifyFormat("#ifdef A\n"
11829                "int f() {}\n"
11830                "#else\n"
11831                "int g() {}\n"
11832                "#endif",
11833                Style);
11834 }
11835 
11836 TEST_F(FormatTest, SplitEmptyClass) {
11837   FormatStyle Style = getLLVMStyle();
11838   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11839   Style.BraceWrapping.AfterClass = true;
11840   Style.BraceWrapping.SplitEmptyRecord = false;
11841 
11842   verifyFormat("class Foo\n"
11843                "{};",
11844                Style);
11845   verifyFormat("/* something */ class Foo\n"
11846                "{};",
11847                Style);
11848   verifyFormat("template <typename X> class Foo\n"
11849                "{};",
11850                Style);
11851   verifyFormat("class Foo\n"
11852                "{\n"
11853                "  Foo();\n"
11854                "};",
11855                Style);
11856   verifyFormat("typedef class Foo\n"
11857                "{\n"
11858                "} Foo_t;",
11859                Style);
11860 
11861   Style.BraceWrapping.SplitEmptyRecord = true;
11862   Style.BraceWrapping.AfterStruct = true;
11863   verifyFormat("class rep\n"
11864                "{\n"
11865                "};",
11866                Style);
11867   verifyFormat("struct rep\n"
11868                "{\n"
11869                "};",
11870                Style);
11871   verifyFormat("template <typename T> class rep\n"
11872                "{\n"
11873                "};",
11874                Style);
11875   verifyFormat("template <typename T> struct rep\n"
11876                "{\n"
11877                "};",
11878                Style);
11879   verifyFormat("class rep\n"
11880                "{\n"
11881                "  int x;\n"
11882                "};",
11883                Style);
11884   verifyFormat("struct rep\n"
11885                "{\n"
11886                "  int x;\n"
11887                "};",
11888                Style);
11889   verifyFormat("template <typename T> class rep\n"
11890                "{\n"
11891                "  int x;\n"
11892                "};",
11893                Style);
11894   verifyFormat("template <typename T> struct rep\n"
11895                "{\n"
11896                "  int x;\n"
11897                "};",
11898                Style);
11899   verifyFormat("template <typename T> class rep // Foo\n"
11900                "{\n"
11901                "  int x;\n"
11902                "};",
11903                Style);
11904   verifyFormat("template <typename T> struct rep // Bar\n"
11905                "{\n"
11906                "  int x;\n"
11907                "};",
11908                Style);
11909 
11910   verifyFormat("template <typename T> class rep<T>\n"
11911                "{\n"
11912                "  int x;\n"
11913                "};",
11914                Style);
11915 
11916   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
11917                "{\n"
11918                "  int x;\n"
11919                "};",
11920                Style);
11921   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
11922                "{\n"
11923                "};",
11924                Style);
11925 
11926   verifyFormat("#include \"stdint.h\"\n"
11927                "namespace rep {}",
11928                Style);
11929   verifyFormat("#include <stdint.h>\n"
11930                "namespace rep {}",
11931                Style);
11932   verifyFormat("#include <stdint.h>\n"
11933                "namespace rep {}",
11934                "#include <stdint.h>\n"
11935                "namespace rep {\n"
11936                "\n"
11937                "\n"
11938                "}",
11939                Style);
11940 }
11941 
11942 TEST_F(FormatTest, SplitEmptyStruct) {
11943   FormatStyle Style = getLLVMStyle();
11944   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11945   Style.BraceWrapping.AfterStruct = true;
11946   Style.BraceWrapping.SplitEmptyRecord = false;
11947 
11948   verifyFormat("struct Foo\n"
11949                "{};",
11950                Style);
11951   verifyFormat("/* something */ struct Foo\n"
11952                "{};",
11953                Style);
11954   verifyFormat("template <typename X> struct Foo\n"
11955                "{};",
11956                Style);
11957   verifyFormat("struct Foo\n"
11958                "{\n"
11959                "  Foo();\n"
11960                "};",
11961                Style);
11962   verifyFormat("typedef struct Foo\n"
11963                "{\n"
11964                "} Foo_t;",
11965                Style);
11966   // typedef struct Bar {} Bar_t;
11967 }
11968 
11969 TEST_F(FormatTest, SplitEmptyUnion) {
11970   FormatStyle Style = getLLVMStyle();
11971   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11972   Style.BraceWrapping.AfterUnion = true;
11973   Style.BraceWrapping.SplitEmptyRecord = false;
11974 
11975   verifyFormat("union Foo\n"
11976                "{};",
11977                Style);
11978   verifyFormat("/* something */ union Foo\n"
11979                "{};",
11980                Style);
11981   verifyFormat("union Foo\n"
11982                "{\n"
11983                "  A,\n"
11984                "};",
11985                Style);
11986   verifyFormat("typedef union Foo\n"
11987                "{\n"
11988                "} Foo_t;",
11989                Style);
11990 }
11991 
11992 TEST_F(FormatTest, SplitEmptyNamespace) {
11993   FormatStyle Style = getLLVMStyle();
11994   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11995   Style.BraceWrapping.AfterNamespace = true;
11996   Style.BraceWrapping.SplitEmptyNamespace = false;
11997 
11998   verifyFormat("namespace Foo\n"
11999                "{};",
12000                Style);
12001   verifyFormat("/* something */ namespace Foo\n"
12002                "{};",
12003                Style);
12004   verifyFormat("inline namespace Foo\n"
12005                "{};",
12006                Style);
12007   verifyFormat("/* something */ inline namespace Foo\n"
12008                "{};",
12009                Style);
12010   verifyFormat("export namespace Foo\n"
12011                "{};",
12012                Style);
12013   verifyFormat("namespace Foo\n"
12014                "{\n"
12015                "void Bar();\n"
12016                "};",
12017                Style);
12018 }
12019 
12020 TEST_F(FormatTest, NeverMergeShortRecords) {
12021   FormatStyle Style = getLLVMStyle();
12022 
12023   verifyFormat("class Foo {\n"
12024                "  Foo();\n"
12025                "};",
12026                Style);
12027   verifyFormat("typedef class Foo {\n"
12028                "  Foo();\n"
12029                "} Foo_t;",
12030                Style);
12031   verifyFormat("struct Foo {\n"
12032                "  Foo();\n"
12033                "};",
12034                Style);
12035   verifyFormat("typedef struct Foo {\n"
12036                "  Foo();\n"
12037                "} Foo_t;",
12038                Style);
12039   verifyFormat("union Foo {\n"
12040                "  A,\n"
12041                "};",
12042                Style);
12043   verifyFormat("typedef union Foo {\n"
12044                "  A,\n"
12045                "} Foo_t;",
12046                Style);
12047   verifyFormat("namespace Foo {\n"
12048                "void Bar();\n"
12049                "};",
12050                Style);
12051 
12052   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12053   Style.BraceWrapping.AfterClass = true;
12054   Style.BraceWrapping.AfterStruct = true;
12055   Style.BraceWrapping.AfterUnion = true;
12056   Style.BraceWrapping.AfterNamespace = true;
12057   verifyFormat("class Foo\n"
12058                "{\n"
12059                "  Foo();\n"
12060                "};",
12061                Style);
12062   verifyFormat("typedef class Foo\n"
12063                "{\n"
12064                "  Foo();\n"
12065                "} Foo_t;",
12066                Style);
12067   verifyFormat("struct Foo\n"
12068                "{\n"
12069                "  Foo();\n"
12070                "};",
12071                Style);
12072   verifyFormat("typedef struct Foo\n"
12073                "{\n"
12074                "  Foo();\n"
12075                "} Foo_t;",
12076                Style);
12077   verifyFormat("union Foo\n"
12078                "{\n"
12079                "  A,\n"
12080                "};",
12081                Style);
12082   verifyFormat("typedef union Foo\n"
12083                "{\n"
12084                "  A,\n"
12085                "} Foo_t;",
12086                Style);
12087   verifyFormat("namespace Foo\n"
12088                "{\n"
12089                "void Bar();\n"
12090                "};",
12091                Style);
12092 }
12093 
12094 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12095   // Elaborate type variable declarations.
12096   verifyFormat("struct foo a = {bar};\nint n;");
12097   verifyFormat("class foo a = {bar};\nint n;");
12098   verifyFormat("union foo a = {bar};\nint n;");
12099 
12100   // Elaborate types inside function definitions.
12101   verifyFormat("struct foo f() {}\nint n;");
12102   verifyFormat("class foo f() {}\nint n;");
12103   verifyFormat("union foo f() {}\nint n;");
12104 
12105   // Templates.
12106   verifyFormat("template <class X> void f() {}\nint n;");
12107   verifyFormat("template <struct X> void f() {}\nint n;");
12108   verifyFormat("template <union X> void f() {}\nint n;");
12109 
12110   // Actual definitions...
12111   verifyFormat("struct {\n} n;");
12112   verifyFormat(
12113       "template <template <class T, class Y>, class Z> class X {\n} n;");
12114   verifyFormat("union Z {\n  int n;\n} x;");
12115   verifyFormat("class MACRO Z {\n} n;");
12116   verifyFormat("class MACRO(X) Z {\n} n;");
12117   verifyFormat("class __attribute__(X) Z {\n} n;");
12118   verifyFormat("class __declspec(X) Z {\n} n;");
12119   verifyFormat("class A##B##C {\n} n;");
12120   verifyFormat("class alignas(16) Z {\n} n;");
12121   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12122   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12123 
12124   // Redefinition from nested context:
12125   verifyFormat("class A::B::C {\n} n;");
12126 
12127   // Template definitions.
12128   verifyFormat(
12129       "template <typename F>\n"
12130       "Matcher(const Matcher<F> &Other,\n"
12131       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12132       "                             !is_same<F, T>::value>::type * = 0)\n"
12133       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12134 
12135   // FIXME: This is still incorrectly handled at the formatter side.
12136   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12137   verifyFormat("int i = SomeFunction(a<b, a> b);");
12138 
12139   // FIXME:
12140   // This now gets parsed incorrectly as class definition.
12141   // verifyFormat("class A<int> f() {\n}\nint n;");
12142 
12143   // Elaborate types where incorrectly parsing the structural element would
12144   // break the indent.
12145   verifyFormat("if (true)\n"
12146                "  class X x;\n"
12147                "else\n"
12148                "  f();\n");
12149 
12150   // This is simply incomplete. Formatting is not important, but must not crash.
12151   verifyFormat("class A:");
12152 }
12153 
12154 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12155   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12156             format("#error Leave     all         white!!!!! space* alone!\n"));
12157   EXPECT_EQ(
12158       "#warning Leave     all         white!!!!! space* alone!\n",
12159       format("#warning Leave     all         white!!!!! space* alone!\n"));
12160   EXPECT_EQ("#error 1", format("  #  error   1"));
12161   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12162 }
12163 
12164 TEST_F(FormatTest, FormatHashIfExpressions) {
12165   verifyFormat("#if AAAA && BBBB");
12166   verifyFormat("#if (AAAA && BBBB)");
12167   verifyFormat("#elif (AAAA && BBBB)");
12168   // FIXME: Come up with a better indentation for #elif.
12169   verifyFormat(
12170       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12171       "    defined(BBBBBBBB)\n"
12172       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12173       "    defined(BBBBBBBB)\n"
12174       "#endif",
12175       getLLVMStyleWithColumns(65));
12176 }
12177 
12178 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12179   FormatStyle AllowsMergedIf = getGoogleStyle();
12180   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12181       FormatStyle::SIS_WithoutElse;
12182   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12183   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12184   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12185   EXPECT_EQ("if (true) return 42;",
12186             format("if (true)\nreturn 42;", AllowsMergedIf));
12187   FormatStyle ShortMergedIf = AllowsMergedIf;
12188   ShortMergedIf.ColumnLimit = 25;
12189   verifyFormat("#define A \\\n"
12190                "  if (true) return 42;",
12191                ShortMergedIf);
12192   verifyFormat("#define A \\\n"
12193                "  f();    \\\n"
12194                "  if (true)\n"
12195                "#define B",
12196                ShortMergedIf);
12197   verifyFormat("#define A \\\n"
12198                "  f();    \\\n"
12199                "  if (true)\n"
12200                "g();",
12201                ShortMergedIf);
12202   verifyFormat("{\n"
12203                "#ifdef A\n"
12204                "  // Comment\n"
12205                "  if (true) continue;\n"
12206                "#endif\n"
12207                "  // Comment\n"
12208                "  if (true) continue;\n"
12209                "}",
12210                ShortMergedIf);
12211   ShortMergedIf.ColumnLimit = 33;
12212   verifyFormat("#define A \\\n"
12213                "  if constexpr (true) return 42;",
12214                ShortMergedIf);
12215   verifyFormat("#define A \\\n"
12216                "  if CONSTEXPR (true) return 42;",
12217                ShortMergedIf);
12218   ShortMergedIf.ColumnLimit = 29;
12219   verifyFormat("#define A                   \\\n"
12220                "  if (aaaaaaaaaa) return 1; \\\n"
12221                "  return 2;",
12222                ShortMergedIf);
12223   ShortMergedIf.ColumnLimit = 28;
12224   verifyFormat("#define A         \\\n"
12225                "  if (aaaaaaaaaa) \\\n"
12226                "    return 1;     \\\n"
12227                "  return 2;",
12228                ShortMergedIf);
12229   verifyFormat("#define A                \\\n"
12230                "  if constexpr (aaaaaaa) \\\n"
12231                "    return 1;            \\\n"
12232                "  return 2;",
12233                ShortMergedIf);
12234   verifyFormat("#define A                \\\n"
12235                "  if CONSTEXPR (aaaaaaa) \\\n"
12236                "    return 1;            \\\n"
12237                "  return 2;",
12238                ShortMergedIf);
12239 }
12240 
12241 TEST_F(FormatTest, FormatStarDependingOnContext) {
12242   verifyFormat("void f(int *a);");
12243   verifyFormat("void f() { f(fint * b); }");
12244   verifyFormat("class A {\n  void f(int *a);\n};");
12245   verifyFormat("class A {\n  int *a;\n};");
12246   verifyFormat("namespace a {\n"
12247                "namespace b {\n"
12248                "class A {\n"
12249                "  void f() {}\n"
12250                "  int *a;\n"
12251                "};\n"
12252                "} // namespace b\n"
12253                "} // namespace a");
12254 }
12255 
12256 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12257   verifyFormat("while");
12258   verifyFormat("operator");
12259 }
12260 
12261 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12262   // This code would be painfully slow to format if we didn't skip it.
12263   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
12264                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12265                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12266                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12267                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12268                    "A(1, 1)\n"
12269                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12270                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12271                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12272                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12273                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12274                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12275                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12276                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12277                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12278                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
12279   // Deeply nested part is untouched, rest is formatted.
12280   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
12281             format(std::string("int    i;\n") + Code + "int    j;\n",
12282                    getLLVMStyle(), SC_ExpectIncomplete));
12283 }
12284 
12285 //===----------------------------------------------------------------------===//
12286 // Objective-C tests.
12287 //===----------------------------------------------------------------------===//
12288 
12289 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
12290   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
12291   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
12292             format("-(NSUInteger)indexOfObject:(id)anObject;"));
12293   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
12294   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
12295   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
12296             format("-(NSInteger)Method3:(id)anObject;"));
12297   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
12298             format("-(NSInteger)Method4:(id)anObject;"));
12299   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
12300             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
12301   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
12302             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
12303   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12304             "forAllCells:(BOOL)flag;",
12305             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12306                    "forAllCells:(BOOL)flag;"));
12307 
12308   // Very long objectiveC method declaration.
12309   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
12310                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
12311   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
12312                "                    inRange:(NSRange)range\n"
12313                "                   outRange:(NSRange)out_range\n"
12314                "                  outRange1:(NSRange)out_range1\n"
12315                "                  outRange2:(NSRange)out_range2\n"
12316                "                  outRange3:(NSRange)out_range3\n"
12317                "                  outRange4:(NSRange)out_range4\n"
12318                "                  outRange5:(NSRange)out_range5\n"
12319                "                  outRange6:(NSRange)out_range6\n"
12320                "                  outRange7:(NSRange)out_range7\n"
12321                "                  outRange8:(NSRange)out_range8\n"
12322                "                  outRange9:(NSRange)out_range9;");
12323 
12324   // When the function name has to be wrapped.
12325   FormatStyle Style = getLLVMStyle();
12326   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
12327   // and always indents instead.
12328   Style.IndentWrappedFunctionNames = false;
12329   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12330                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
12331                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
12332                "}",
12333                Style);
12334   Style.IndentWrappedFunctionNames = true;
12335   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12336                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
12337                "               anotherName:(NSString)dddddddddddddd {\n"
12338                "}",
12339                Style);
12340 
12341   verifyFormat("- (int)sum:(vector<int>)numbers;");
12342   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
12343   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
12344   // protocol lists (but not for template classes):
12345   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
12346 
12347   verifyFormat("- (int (*)())foo:(int (*)())f;");
12348   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
12349 
12350   // If there's no return type (very rare in practice!), LLVM and Google style
12351   // agree.
12352   verifyFormat("- foo;");
12353   verifyFormat("- foo:(int)f;");
12354   verifyGoogleFormat("- foo:(int)foo;");
12355 }
12356 
12357 TEST_F(FormatTest, BreaksStringLiterals) {
12358   EXPECT_EQ("\"some text \"\n"
12359             "\"other\";",
12360             format("\"some text other\";", getLLVMStyleWithColumns(12)));
12361   EXPECT_EQ("\"some text \"\n"
12362             "\"other\";",
12363             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
12364   EXPECT_EQ(
12365       "#define A  \\\n"
12366       "  \"some \"  \\\n"
12367       "  \"text \"  \\\n"
12368       "  \"other\";",
12369       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
12370   EXPECT_EQ(
12371       "#define A  \\\n"
12372       "  \"so \"    \\\n"
12373       "  \"text \"  \\\n"
12374       "  \"other\";",
12375       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
12376 
12377   EXPECT_EQ("\"some text\"",
12378             format("\"some text\"", getLLVMStyleWithColumns(1)));
12379   EXPECT_EQ("\"some text\"",
12380             format("\"some text\"", getLLVMStyleWithColumns(11)));
12381   EXPECT_EQ("\"some \"\n"
12382             "\"text\"",
12383             format("\"some text\"", getLLVMStyleWithColumns(10)));
12384   EXPECT_EQ("\"some \"\n"
12385             "\"text\"",
12386             format("\"some text\"", getLLVMStyleWithColumns(7)));
12387   EXPECT_EQ("\"some\"\n"
12388             "\" tex\"\n"
12389             "\"t\"",
12390             format("\"some text\"", getLLVMStyleWithColumns(6)));
12391   EXPECT_EQ("\"some\"\n"
12392             "\" tex\"\n"
12393             "\" and\"",
12394             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
12395   EXPECT_EQ("\"some\"\n"
12396             "\"/tex\"\n"
12397             "\"/and\"",
12398             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
12399 
12400   EXPECT_EQ("variable =\n"
12401             "    \"long string \"\n"
12402             "    \"literal\";",
12403             format("variable = \"long string literal\";",
12404                    getLLVMStyleWithColumns(20)));
12405 
12406   EXPECT_EQ("variable = f(\n"
12407             "    \"long string \"\n"
12408             "    \"literal\",\n"
12409             "    short,\n"
12410             "    loooooooooooooooooooong);",
12411             format("variable = f(\"long string literal\", short, "
12412                    "loooooooooooooooooooong);",
12413                    getLLVMStyleWithColumns(20)));
12414 
12415   EXPECT_EQ(
12416       "f(g(\"long string \"\n"
12417       "    \"literal\"),\n"
12418       "  b);",
12419       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
12420   EXPECT_EQ("f(g(\"long string \"\n"
12421             "    \"literal\",\n"
12422             "    a),\n"
12423             "  b);",
12424             format("f(g(\"long string literal\", a), b);",
12425                    getLLVMStyleWithColumns(20)));
12426   EXPECT_EQ(
12427       "f(\"one two\".split(\n"
12428       "    variable));",
12429       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
12430   EXPECT_EQ("f(\"one two three four five six \"\n"
12431             "  \"seven\".split(\n"
12432             "      really_looooong_variable));",
12433             format("f(\"one two three four five six seven\"."
12434                    "split(really_looooong_variable));",
12435                    getLLVMStyleWithColumns(33)));
12436 
12437   EXPECT_EQ("f(\"some \"\n"
12438             "  \"text\",\n"
12439             "  other);",
12440             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
12441 
12442   // Only break as a last resort.
12443   verifyFormat(
12444       "aaaaaaaaaaaaaaaaaaaa(\n"
12445       "    aaaaaaaaaaaaaaaaaaaa,\n"
12446       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
12447 
12448   EXPECT_EQ("\"splitmea\"\n"
12449             "\"trandomp\"\n"
12450             "\"oint\"",
12451             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
12452 
12453   EXPECT_EQ("\"split/\"\n"
12454             "\"pathat/\"\n"
12455             "\"slashes\"",
12456             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12457 
12458   EXPECT_EQ("\"split/\"\n"
12459             "\"pathat/\"\n"
12460             "\"slashes\"",
12461             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12462   EXPECT_EQ("\"split at \"\n"
12463             "\"spaces/at/\"\n"
12464             "\"slashes.at.any$\"\n"
12465             "\"non-alphanumeric%\"\n"
12466             "\"1111111111characte\"\n"
12467             "\"rs\"",
12468             format("\"split at "
12469                    "spaces/at/"
12470                    "slashes.at."
12471                    "any$non-"
12472                    "alphanumeric%"
12473                    "1111111111characte"
12474                    "rs\"",
12475                    getLLVMStyleWithColumns(20)));
12476 
12477   // Verify that splitting the strings understands
12478   // Style::AlwaysBreakBeforeMultilineStrings.
12479   EXPECT_EQ("aaaaaaaaaaaa(\n"
12480             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
12481             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
12482             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
12483                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12484                    "aaaaaaaaaaaaaaaaaaaaaa\");",
12485                    getGoogleStyle()));
12486   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12487             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
12488             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
12489                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12490                    "aaaaaaaaaaaaaaaaaaaaaa\";",
12491                    getGoogleStyle()));
12492   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12493             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12494             format("llvm::outs() << "
12495                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
12496                    "aaaaaaaaaaaaaaaaaaa\";"));
12497   EXPECT_EQ("ffff(\n"
12498             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12499             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12500             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
12501                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12502                    getGoogleStyle()));
12503 
12504   FormatStyle Style = getLLVMStyleWithColumns(12);
12505   Style.BreakStringLiterals = false;
12506   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
12507 
12508   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
12509   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12510   EXPECT_EQ("#define A \\\n"
12511             "  \"some \" \\\n"
12512             "  \"text \" \\\n"
12513             "  \"other\";",
12514             format("#define A \"some text other\";", AlignLeft));
12515 }
12516 
12517 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
12518   EXPECT_EQ("C a = \"some more \"\n"
12519             "      \"text\";",
12520             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
12521 }
12522 
12523 TEST_F(FormatTest, FullyRemoveEmptyLines) {
12524   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
12525   NoEmptyLines.MaxEmptyLinesToKeep = 0;
12526   EXPECT_EQ("int i = a(b());",
12527             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
12528 }
12529 
12530 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
12531   EXPECT_EQ(
12532       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12533       "(\n"
12534       "    \"x\t\");",
12535       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12536              "aaaaaaa("
12537              "\"x\t\");"));
12538 }
12539 
12540 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
12541   EXPECT_EQ(
12542       "u8\"utf8 string \"\n"
12543       "u8\"literal\";",
12544       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
12545   EXPECT_EQ(
12546       "u\"utf16 string \"\n"
12547       "u\"literal\";",
12548       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
12549   EXPECT_EQ(
12550       "U\"utf32 string \"\n"
12551       "U\"literal\";",
12552       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
12553   EXPECT_EQ("L\"wide string \"\n"
12554             "L\"literal\";",
12555             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
12556   EXPECT_EQ("@\"NSString \"\n"
12557             "@\"literal\";",
12558             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
12559   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
12560 
12561   // This input makes clang-format try to split the incomplete unicode escape
12562   // sequence, which used to lead to a crasher.
12563   verifyNoCrash(
12564       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12565       getLLVMStyleWithColumns(60));
12566 }
12567 
12568 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
12569   FormatStyle Style = getGoogleStyleWithColumns(15);
12570   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
12571   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
12572   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
12573   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
12574   EXPECT_EQ("u8R\"x(raw literal)x\";",
12575             format("u8R\"x(raw literal)x\";", Style));
12576 }
12577 
12578 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
12579   FormatStyle Style = getLLVMStyleWithColumns(20);
12580   EXPECT_EQ(
12581       "_T(\"aaaaaaaaaaaaaa\")\n"
12582       "_T(\"aaaaaaaaaaaaaa\")\n"
12583       "_T(\"aaaaaaaaaaaa\")",
12584       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
12585   EXPECT_EQ("f(x,\n"
12586             "  _T(\"aaaaaaaaaaaa\")\n"
12587             "  _T(\"aaa\"),\n"
12588             "  z);",
12589             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
12590 
12591   // FIXME: Handle embedded spaces in one iteration.
12592   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
12593   //            "_T(\"aaaaaaaaaaaaa\")\n"
12594   //            "_T(\"aaaaaaaaaaaaa\")\n"
12595   //            "_T(\"a\")",
12596   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12597   //                   getLLVMStyleWithColumns(20)));
12598   EXPECT_EQ(
12599       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12600       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
12601   EXPECT_EQ("f(\n"
12602             "#if !TEST\n"
12603             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12604             "#endif\n"
12605             ");",
12606             format("f(\n"
12607                    "#if !TEST\n"
12608                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12609                    "#endif\n"
12610                    ");"));
12611   EXPECT_EQ("f(\n"
12612             "\n"
12613             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
12614             format("f(\n"
12615                    "\n"
12616                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
12617 }
12618 
12619 TEST_F(FormatTest, BreaksStringLiteralOperands) {
12620   // In a function call with two operands, the second can be broken with no line
12621   // break before it.
12622   EXPECT_EQ(
12623       "func(a, \"long long \"\n"
12624       "        \"long long\");",
12625       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
12626   // In a function call with three operands, the second must be broken with a
12627   // line break before it.
12628   EXPECT_EQ("func(a,\n"
12629             "     \"long long long \"\n"
12630             "     \"long\",\n"
12631             "     c);",
12632             format("func(a, \"long long long long\", c);",
12633                    getLLVMStyleWithColumns(24)));
12634   // In a function call with three operands, the third must be broken with a
12635   // line break before it.
12636   EXPECT_EQ("func(a, b,\n"
12637             "     \"long long long \"\n"
12638             "     \"long\");",
12639             format("func(a, b, \"long long long long\");",
12640                    getLLVMStyleWithColumns(24)));
12641   // In a function call with three operands, both the second and the third must
12642   // be broken with a line break before them.
12643   EXPECT_EQ("func(a,\n"
12644             "     \"long long long \"\n"
12645             "     \"long\",\n"
12646             "     \"long long long \"\n"
12647             "     \"long\");",
12648             format("func(a, \"long long long long\", \"long long long long\");",
12649                    getLLVMStyleWithColumns(24)));
12650   // In a chain of << with two operands, the second can be broken with no line
12651   // break before it.
12652   EXPECT_EQ("a << \"line line \"\n"
12653             "     \"line\";",
12654             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
12655   // In a chain of << with three operands, the second can be broken with no line
12656   // break before it.
12657   EXPECT_EQ(
12658       "abcde << \"line \"\n"
12659       "         \"line line\"\n"
12660       "      << c;",
12661       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
12662   // In a chain of << with three operands, the third must be broken with a line
12663   // break before it.
12664   EXPECT_EQ(
12665       "a << b\n"
12666       "  << \"line line \"\n"
12667       "     \"line\";",
12668       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
12669   // In a chain of << with three operands, the second can be broken with no line
12670   // break before it and the third must be broken with a line break before it.
12671   EXPECT_EQ("abcd << \"line line \"\n"
12672             "        \"line\"\n"
12673             "     << \"line line \"\n"
12674             "        \"line\";",
12675             format("abcd << \"line line line\" << \"line line line\";",
12676                    getLLVMStyleWithColumns(20)));
12677   // In a chain of binary operators with two operands, the second can be broken
12678   // with no line break before it.
12679   EXPECT_EQ(
12680       "abcd + \"line line \"\n"
12681       "       \"line line\";",
12682       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
12683   // In a chain of binary operators with three operands, the second must be
12684   // broken with a line break before it.
12685   EXPECT_EQ("abcd +\n"
12686             "    \"line line \"\n"
12687             "    \"line line\" +\n"
12688             "    e;",
12689             format("abcd + \"line line line line\" + e;",
12690                    getLLVMStyleWithColumns(20)));
12691   // In a function call with two operands, with AlignAfterOpenBracket enabled,
12692   // the first must be broken with a line break before it.
12693   FormatStyle Style = getLLVMStyleWithColumns(25);
12694   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12695   EXPECT_EQ("someFunction(\n"
12696             "    \"long long long \"\n"
12697             "    \"long\",\n"
12698             "    a);",
12699             format("someFunction(\"long long long long\", a);", Style));
12700 }
12701 
12702 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
12703   EXPECT_EQ(
12704       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12705       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12706       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12707       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12708              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12709              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
12710 }
12711 
12712 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
12713   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
12714             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
12715   EXPECT_EQ("fffffffffff(g(R\"x(\n"
12716             "multiline raw string literal xxxxxxxxxxxxxx\n"
12717             ")x\",\n"
12718             "              a),\n"
12719             "            b);",
12720             format("fffffffffff(g(R\"x(\n"
12721                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12722                    ")x\", a), b);",
12723                    getGoogleStyleWithColumns(20)));
12724   EXPECT_EQ("fffffffffff(\n"
12725             "    g(R\"x(qqq\n"
12726             "multiline raw string literal xxxxxxxxxxxxxx\n"
12727             ")x\",\n"
12728             "      a),\n"
12729             "    b);",
12730             format("fffffffffff(g(R\"x(qqq\n"
12731                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12732                    ")x\", a), b);",
12733                    getGoogleStyleWithColumns(20)));
12734 
12735   EXPECT_EQ("fffffffffff(R\"x(\n"
12736             "multiline raw string literal xxxxxxxxxxxxxx\n"
12737             ")x\");",
12738             format("fffffffffff(R\"x(\n"
12739                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12740                    ")x\");",
12741                    getGoogleStyleWithColumns(20)));
12742   EXPECT_EQ("fffffffffff(R\"x(\n"
12743             "multiline raw string literal xxxxxxxxxxxxxx\n"
12744             ")x\" + bbbbbb);",
12745             format("fffffffffff(R\"x(\n"
12746                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12747                    ")x\" +   bbbbbb);",
12748                    getGoogleStyleWithColumns(20)));
12749   EXPECT_EQ("fffffffffff(\n"
12750             "    R\"x(\n"
12751             "multiline raw string literal xxxxxxxxxxxxxx\n"
12752             ")x\" +\n"
12753             "    bbbbbb);",
12754             format("fffffffffff(\n"
12755                    " R\"x(\n"
12756                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12757                    ")x\" + bbbbbb);",
12758                    getGoogleStyleWithColumns(20)));
12759   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
12760             format("fffffffffff(\n"
12761                    " R\"(single line raw string)\" + bbbbbb);"));
12762 }
12763 
12764 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
12765   verifyFormat("string a = \"unterminated;");
12766   EXPECT_EQ("function(\"unterminated,\n"
12767             "         OtherParameter);",
12768             format("function(  \"unterminated,\n"
12769                    "    OtherParameter);"));
12770 }
12771 
12772 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
12773   FormatStyle Style = getLLVMStyle();
12774   Style.Standard = FormatStyle::LS_Cpp03;
12775   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
12776             format("#define x(_a) printf(\"foo\"_a);", Style));
12777 }
12778 
12779 TEST_F(FormatTest, CppLexVersion) {
12780   FormatStyle Style = getLLVMStyle();
12781   // Formatting of x * y differs if x is a type.
12782   verifyFormat("void foo() { MACRO(a * b); }", Style);
12783   verifyFormat("void foo() { MACRO(int *b); }", Style);
12784 
12785   // LLVM style uses latest lexer.
12786   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
12787   Style.Standard = FormatStyle::LS_Cpp17;
12788   // But in c++17, char8_t isn't a keyword.
12789   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
12790 }
12791 
12792 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
12793 
12794 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
12795   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
12796             "             \"ddeeefff\");",
12797             format("someFunction(\"aaabbbcccdddeeefff\");",
12798                    getLLVMStyleWithColumns(25)));
12799   EXPECT_EQ("someFunction1234567890(\n"
12800             "    \"aaabbbcccdddeeefff\");",
12801             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12802                    getLLVMStyleWithColumns(26)));
12803   EXPECT_EQ("someFunction1234567890(\n"
12804             "    \"aaabbbcccdddeeeff\"\n"
12805             "    \"f\");",
12806             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12807                    getLLVMStyleWithColumns(25)));
12808   EXPECT_EQ("someFunction1234567890(\n"
12809             "    \"aaabbbcccdddeeeff\"\n"
12810             "    \"f\");",
12811             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12812                    getLLVMStyleWithColumns(24)));
12813   EXPECT_EQ("someFunction(\n"
12814             "    \"aaabbbcc ddde \"\n"
12815             "    \"efff\");",
12816             format("someFunction(\"aaabbbcc ddde efff\");",
12817                    getLLVMStyleWithColumns(25)));
12818   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
12819             "             \"ddeeefff\");",
12820             format("someFunction(\"aaabbbccc ddeeefff\");",
12821                    getLLVMStyleWithColumns(25)));
12822   EXPECT_EQ("someFunction1234567890(\n"
12823             "    \"aaabb \"\n"
12824             "    \"cccdddeeefff\");",
12825             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
12826                    getLLVMStyleWithColumns(25)));
12827   EXPECT_EQ("#define A          \\\n"
12828             "  string s =       \\\n"
12829             "      \"123456789\"  \\\n"
12830             "      \"0\";         \\\n"
12831             "  int i;",
12832             format("#define A string s = \"1234567890\"; int i;",
12833                    getLLVMStyleWithColumns(20)));
12834   EXPECT_EQ("someFunction(\n"
12835             "    \"aaabbbcc \"\n"
12836             "    \"dddeeefff\");",
12837             format("someFunction(\"aaabbbcc dddeeefff\");",
12838                    getLLVMStyleWithColumns(25)));
12839 }
12840 
12841 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
12842   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
12843   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
12844   EXPECT_EQ("\"test\"\n"
12845             "\"\\n\"",
12846             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
12847   EXPECT_EQ("\"tes\\\\\"\n"
12848             "\"n\"",
12849             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
12850   EXPECT_EQ("\"\\\\\\\\\"\n"
12851             "\"\\n\"",
12852             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
12853   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
12854   EXPECT_EQ("\"\\uff01\"\n"
12855             "\"test\"",
12856             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
12857   EXPECT_EQ("\"\\Uff01ff02\"",
12858             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
12859   EXPECT_EQ("\"\\x000000000001\"\n"
12860             "\"next\"",
12861             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
12862   EXPECT_EQ("\"\\x000000000001next\"",
12863             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
12864   EXPECT_EQ("\"\\x000000000001\"",
12865             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
12866   EXPECT_EQ("\"test\"\n"
12867             "\"\\000000\"\n"
12868             "\"000001\"",
12869             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
12870   EXPECT_EQ("\"test\\000\"\n"
12871             "\"00000000\"\n"
12872             "\"1\"",
12873             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
12874 }
12875 
12876 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
12877   verifyFormat("void f() {\n"
12878                "  return g() {}\n"
12879                "  void h() {}");
12880   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
12881                "g();\n"
12882                "}");
12883 }
12884 
12885 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
12886   verifyFormat(
12887       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
12888 }
12889 
12890 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
12891   verifyFormat("class X {\n"
12892                "  void f() {\n"
12893                "  }\n"
12894                "};",
12895                getLLVMStyleWithColumns(12));
12896 }
12897 
12898 TEST_F(FormatTest, ConfigurableIndentWidth) {
12899   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
12900   EightIndent.IndentWidth = 8;
12901   EightIndent.ContinuationIndentWidth = 8;
12902   verifyFormat("void f() {\n"
12903                "        someFunction();\n"
12904                "        if (true) {\n"
12905                "                f();\n"
12906                "        }\n"
12907                "}",
12908                EightIndent);
12909   verifyFormat("class X {\n"
12910                "        void f() {\n"
12911                "        }\n"
12912                "};",
12913                EightIndent);
12914   verifyFormat("int x[] = {\n"
12915                "        call(),\n"
12916                "        call()};",
12917                EightIndent);
12918 }
12919 
12920 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
12921   verifyFormat("double\n"
12922                "f();",
12923                getLLVMStyleWithColumns(8));
12924 }
12925 
12926 TEST_F(FormatTest, ConfigurableUseOfTab) {
12927   FormatStyle Tab = getLLVMStyleWithColumns(42);
12928   Tab.IndentWidth = 8;
12929   Tab.UseTab = FormatStyle::UT_Always;
12930   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12931 
12932   EXPECT_EQ("if (aaaaaaaa && // q\n"
12933             "    bb)\t\t// w\n"
12934             "\t;",
12935             format("if (aaaaaaaa &&// q\n"
12936                    "bb)// w\n"
12937                    ";",
12938                    Tab));
12939   EXPECT_EQ("if (aaa && bbb) // w\n"
12940             "\t;",
12941             format("if(aaa&&bbb)// w\n"
12942                    ";",
12943                    Tab));
12944 
12945   verifyFormat("class X {\n"
12946                "\tvoid f() {\n"
12947                "\t\tsomeFunction(parameter1,\n"
12948                "\t\t\t     parameter2);\n"
12949                "\t}\n"
12950                "};",
12951                Tab);
12952   verifyFormat("#define A                        \\\n"
12953                "\tvoid f() {               \\\n"
12954                "\t\tsomeFunction(    \\\n"
12955                "\t\t    parameter1,  \\\n"
12956                "\t\t    parameter2); \\\n"
12957                "\t}",
12958                Tab);
12959   verifyFormat("int a;\t      // x\n"
12960                "int bbbbbbbb; // x\n",
12961                Tab);
12962 
12963   Tab.TabWidth = 4;
12964   Tab.IndentWidth = 8;
12965   verifyFormat("class TabWidth4Indent8 {\n"
12966                "\t\tvoid f() {\n"
12967                "\t\t\t\tsomeFunction(parameter1,\n"
12968                "\t\t\t\t\t\t\t parameter2);\n"
12969                "\t\t}\n"
12970                "};",
12971                Tab);
12972 
12973   Tab.TabWidth = 4;
12974   Tab.IndentWidth = 4;
12975   verifyFormat("class TabWidth4Indent4 {\n"
12976                "\tvoid f() {\n"
12977                "\t\tsomeFunction(parameter1,\n"
12978                "\t\t\t\t\t parameter2);\n"
12979                "\t}\n"
12980                "};",
12981                Tab);
12982 
12983   Tab.TabWidth = 8;
12984   Tab.IndentWidth = 4;
12985   verifyFormat("class TabWidth8Indent4 {\n"
12986                "    void f() {\n"
12987                "\tsomeFunction(parameter1,\n"
12988                "\t\t     parameter2);\n"
12989                "    }\n"
12990                "};",
12991                Tab);
12992 
12993   Tab.TabWidth = 8;
12994   Tab.IndentWidth = 8;
12995   EXPECT_EQ("/*\n"
12996             "\t      a\t\tcomment\n"
12997             "\t      in multiple lines\n"
12998             "       */",
12999             format("   /*\t \t \n"
13000                    " \t \t a\t\tcomment\t \t\n"
13001                    " \t \t in multiple lines\t\n"
13002                    " \t  */",
13003                    Tab));
13004 
13005   Tab.UseTab = FormatStyle::UT_ForIndentation;
13006   verifyFormat("{\n"
13007                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13008                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13009                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13010                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13011                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13012                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13013                "};",
13014                Tab);
13015   verifyFormat("enum AA {\n"
13016                "\ta1, // Force multiple lines\n"
13017                "\ta2,\n"
13018                "\ta3\n"
13019                "};",
13020                Tab);
13021   EXPECT_EQ("if (aaaaaaaa && // q\n"
13022             "    bb)         // w\n"
13023             "\t;",
13024             format("if (aaaaaaaa &&// q\n"
13025                    "bb)// w\n"
13026                    ";",
13027                    Tab));
13028   verifyFormat("class X {\n"
13029                "\tvoid f() {\n"
13030                "\t\tsomeFunction(parameter1,\n"
13031                "\t\t             parameter2);\n"
13032                "\t}\n"
13033                "};",
13034                Tab);
13035   verifyFormat("{\n"
13036                "\tQ(\n"
13037                "\t    {\n"
13038                "\t\t    int a;\n"
13039                "\t\t    someFunction(aaaaaaaa,\n"
13040                "\t\t                 bbbbbbb);\n"
13041                "\t    },\n"
13042                "\t    p);\n"
13043                "}",
13044                Tab);
13045   EXPECT_EQ("{\n"
13046             "\t/* aaaa\n"
13047             "\t   bbbb */\n"
13048             "}",
13049             format("{\n"
13050                    "/* aaaa\n"
13051                    "   bbbb */\n"
13052                    "}",
13053                    Tab));
13054   EXPECT_EQ("{\n"
13055             "\t/*\n"
13056             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13057             "\t  bbbbbbbbbbbbb\n"
13058             "\t*/\n"
13059             "}",
13060             format("{\n"
13061                    "/*\n"
13062                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13063                    "*/\n"
13064                    "}",
13065                    Tab));
13066   EXPECT_EQ("{\n"
13067             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13068             "\t// bbbbbbbbbbbbb\n"
13069             "}",
13070             format("{\n"
13071                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13072                    "}",
13073                    Tab));
13074   EXPECT_EQ("{\n"
13075             "\t/*\n"
13076             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13077             "\t  bbbbbbbbbbbbb\n"
13078             "\t*/\n"
13079             "}",
13080             format("{\n"
13081                    "\t/*\n"
13082                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13083                    "\t*/\n"
13084                    "}",
13085                    Tab));
13086   EXPECT_EQ("{\n"
13087             "\t/*\n"
13088             "\n"
13089             "\t*/\n"
13090             "}",
13091             format("{\n"
13092                    "\t/*\n"
13093                    "\n"
13094                    "\t*/\n"
13095                    "}",
13096                    Tab));
13097   EXPECT_EQ("{\n"
13098             "\t/*\n"
13099             " asdf\n"
13100             "\t*/\n"
13101             "}",
13102             format("{\n"
13103                    "\t/*\n"
13104                    " asdf\n"
13105                    "\t*/\n"
13106                    "}",
13107                    Tab));
13108 
13109   Tab.UseTab = FormatStyle::UT_Never;
13110   EXPECT_EQ("/*\n"
13111             "              a\t\tcomment\n"
13112             "              in multiple lines\n"
13113             "       */",
13114             format("   /*\t \t \n"
13115                    " \t \t a\t\tcomment\t \t\n"
13116                    " \t \t in multiple lines\t\n"
13117                    " \t  */",
13118                    Tab));
13119   EXPECT_EQ("/* some\n"
13120             "   comment */",
13121             format(" \t \t /* some\n"
13122                    " \t \t    comment */",
13123                    Tab));
13124   EXPECT_EQ("int a; /* some\n"
13125             "   comment */",
13126             format(" \t \t int a; /* some\n"
13127                    " \t \t    comment */",
13128                    Tab));
13129 
13130   EXPECT_EQ("int a; /* some\n"
13131             "comment */",
13132             format(" \t \t int\ta; /* some\n"
13133                    " \t \t    comment */",
13134                    Tab));
13135   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13136             "    comment */",
13137             format(" \t \t f(\"\t\t\"); /* some\n"
13138                    " \t \t    comment */",
13139                    Tab));
13140   EXPECT_EQ("{\n"
13141             "        /*\n"
13142             "         * Comment\n"
13143             "         */\n"
13144             "        int i;\n"
13145             "}",
13146             format("{\n"
13147                    "\t/*\n"
13148                    "\t * Comment\n"
13149                    "\t */\n"
13150                    "\t int i;\n"
13151                    "}",
13152                    Tab));
13153 
13154   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13155   Tab.TabWidth = 8;
13156   Tab.IndentWidth = 8;
13157   EXPECT_EQ("if (aaaaaaaa && // q\n"
13158             "    bb)         // w\n"
13159             "\t;",
13160             format("if (aaaaaaaa &&// q\n"
13161                    "bb)// w\n"
13162                    ";",
13163                    Tab));
13164   EXPECT_EQ("if (aaa && bbb) // w\n"
13165             "\t;",
13166             format("if(aaa&&bbb)// w\n"
13167                    ";",
13168                    Tab));
13169   verifyFormat("class X {\n"
13170                "\tvoid f() {\n"
13171                "\t\tsomeFunction(parameter1,\n"
13172                "\t\t\t     parameter2);\n"
13173                "\t}\n"
13174                "};",
13175                Tab);
13176   verifyFormat("#define A                        \\\n"
13177                "\tvoid f() {               \\\n"
13178                "\t\tsomeFunction(    \\\n"
13179                "\t\t    parameter1,  \\\n"
13180                "\t\t    parameter2); \\\n"
13181                "\t}",
13182                Tab);
13183   Tab.TabWidth = 4;
13184   Tab.IndentWidth = 8;
13185   verifyFormat("class TabWidth4Indent8 {\n"
13186                "\t\tvoid f() {\n"
13187                "\t\t\t\tsomeFunction(parameter1,\n"
13188                "\t\t\t\t\t\t\t parameter2);\n"
13189                "\t\t}\n"
13190                "};",
13191                Tab);
13192   Tab.TabWidth = 4;
13193   Tab.IndentWidth = 4;
13194   verifyFormat("class TabWidth4Indent4 {\n"
13195                "\tvoid f() {\n"
13196                "\t\tsomeFunction(parameter1,\n"
13197                "\t\t\t\t\t parameter2);\n"
13198                "\t}\n"
13199                "};",
13200                Tab);
13201   Tab.TabWidth = 8;
13202   Tab.IndentWidth = 4;
13203   verifyFormat("class TabWidth8Indent4 {\n"
13204                "    void f() {\n"
13205                "\tsomeFunction(parameter1,\n"
13206                "\t\t     parameter2);\n"
13207                "    }\n"
13208                "};",
13209                Tab);
13210   Tab.TabWidth = 8;
13211   Tab.IndentWidth = 8;
13212   EXPECT_EQ("/*\n"
13213             "\t      a\t\tcomment\n"
13214             "\t      in multiple lines\n"
13215             "       */",
13216             format("   /*\t \t \n"
13217                    " \t \t a\t\tcomment\t \t\n"
13218                    " \t \t in multiple lines\t\n"
13219                    " \t  */",
13220                    Tab));
13221   verifyFormat("{\n"
13222                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13223                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13224                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13225                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13226                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13227                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13228                "};",
13229                Tab);
13230   verifyFormat("enum AA {\n"
13231                "\ta1, // Force multiple lines\n"
13232                "\ta2,\n"
13233                "\ta3\n"
13234                "};",
13235                Tab);
13236   EXPECT_EQ("if (aaaaaaaa && // q\n"
13237             "    bb)         // w\n"
13238             "\t;",
13239             format("if (aaaaaaaa &&// q\n"
13240                    "bb)// w\n"
13241                    ";",
13242                    Tab));
13243   verifyFormat("class X {\n"
13244                "\tvoid f() {\n"
13245                "\t\tsomeFunction(parameter1,\n"
13246                "\t\t\t     parameter2);\n"
13247                "\t}\n"
13248                "};",
13249                Tab);
13250   verifyFormat("{\n"
13251                "\tQ(\n"
13252                "\t    {\n"
13253                "\t\t    int a;\n"
13254                "\t\t    someFunction(aaaaaaaa,\n"
13255                "\t\t\t\t bbbbbbb);\n"
13256                "\t    },\n"
13257                "\t    p);\n"
13258                "}",
13259                Tab);
13260   EXPECT_EQ("{\n"
13261             "\t/* aaaa\n"
13262             "\t   bbbb */\n"
13263             "}",
13264             format("{\n"
13265                    "/* aaaa\n"
13266                    "   bbbb */\n"
13267                    "}",
13268                    Tab));
13269   EXPECT_EQ("{\n"
13270             "\t/*\n"
13271             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13272             "\t  bbbbbbbbbbbbb\n"
13273             "\t*/\n"
13274             "}",
13275             format("{\n"
13276                    "/*\n"
13277                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13278                    "*/\n"
13279                    "}",
13280                    Tab));
13281   EXPECT_EQ("{\n"
13282             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13283             "\t// bbbbbbbbbbbbb\n"
13284             "}",
13285             format("{\n"
13286                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13287                    "}",
13288                    Tab));
13289   EXPECT_EQ("{\n"
13290             "\t/*\n"
13291             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13292             "\t  bbbbbbbbbbbbb\n"
13293             "\t*/\n"
13294             "}",
13295             format("{\n"
13296                    "\t/*\n"
13297                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13298                    "\t*/\n"
13299                    "}",
13300                    Tab));
13301   EXPECT_EQ("{\n"
13302             "\t/*\n"
13303             "\n"
13304             "\t*/\n"
13305             "}",
13306             format("{\n"
13307                    "\t/*\n"
13308                    "\n"
13309                    "\t*/\n"
13310                    "}",
13311                    Tab));
13312   EXPECT_EQ("{\n"
13313             "\t/*\n"
13314             " asdf\n"
13315             "\t*/\n"
13316             "}",
13317             format("{\n"
13318                    "\t/*\n"
13319                    " asdf\n"
13320                    "\t*/\n"
13321                    "}",
13322                    Tab));
13323   EXPECT_EQ("/* some\n"
13324             "   comment */",
13325             format(" \t \t /* some\n"
13326                    " \t \t    comment */",
13327                    Tab));
13328   EXPECT_EQ("int a; /* some\n"
13329             "   comment */",
13330             format(" \t \t int a; /* some\n"
13331                    " \t \t    comment */",
13332                    Tab));
13333   EXPECT_EQ("int a; /* some\n"
13334             "comment */",
13335             format(" \t \t int\ta; /* some\n"
13336                    " \t \t    comment */",
13337                    Tab));
13338   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13339             "    comment */",
13340             format(" \t \t f(\"\t\t\"); /* some\n"
13341                    " \t \t    comment */",
13342                    Tab));
13343   EXPECT_EQ("{\n"
13344             "\t/*\n"
13345             "\t * Comment\n"
13346             "\t */\n"
13347             "\tint i;\n"
13348             "}",
13349             format("{\n"
13350                    "\t/*\n"
13351                    "\t * Comment\n"
13352                    "\t */\n"
13353                    "\t int i;\n"
13354                    "}",
13355                    Tab));
13356   Tab.TabWidth = 2;
13357   Tab.IndentWidth = 2;
13358   EXPECT_EQ("{\n"
13359             "\t/* aaaa\n"
13360             "\t\t bbbb */\n"
13361             "}",
13362             format("{\n"
13363                    "/* aaaa\n"
13364                    "\t bbbb */\n"
13365                    "}",
13366                    Tab));
13367   EXPECT_EQ("{\n"
13368             "\t/*\n"
13369             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13370             "\t\tbbbbbbbbbbbbb\n"
13371             "\t*/\n"
13372             "}",
13373             format("{\n"
13374                    "/*\n"
13375                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13376                    "*/\n"
13377                    "}",
13378                    Tab));
13379   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13380   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13381   Tab.TabWidth = 4;
13382   Tab.IndentWidth = 4;
13383   verifyFormat("class Assign {\n"
13384                "\tvoid f() {\n"
13385                "\t\tint         x      = 123;\n"
13386                "\t\tint         random = 4;\n"
13387                "\t\tstd::string alphabet =\n"
13388                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13389                "\t}\n"
13390                "};",
13391                Tab);
13392 
13393   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13394   Tab.TabWidth = 8;
13395   Tab.IndentWidth = 8;
13396   EXPECT_EQ("if (aaaaaaaa && // q\n"
13397             "    bb)         // w\n"
13398             "\t;",
13399             format("if (aaaaaaaa &&// q\n"
13400                    "bb)// w\n"
13401                    ";",
13402                    Tab));
13403   EXPECT_EQ("if (aaa && bbb) // w\n"
13404             "\t;",
13405             format("if(aaa&&bbb)// w\n"
13406                    ";",
13407                    Tab));
13408   verifyFormat("class X {\n"
13409                "\tvoid f() {\n"
13410                "\t\tsomeFunction(parameter1,\n"
13411                "\t\t             parameter2);\n"
13412                "\t}\n"
13413                "};",
13414                Tab);
13415   verifyFormat("#define A                        \\\n"
13416                "\tvoid f() {               \\\n"
13417                "\t\tsomeFunction(    \\\n"
13418                "\t\t    parameter1,  \\\n"
13419                "\t\t    parameter2); \\\n"
13420                "\t}",
13421                Tab);
13422   Tab.TabWidth = 4;
13423   Tab.IndentWidth = 8;
13424   verifyFormat("class TabWidth4Indent8 {\n"
13425                "\t\tvoid f() {\n"
13426                "\t\t\t\tsomeFunction(parameter1,\n"
13427                "\t\t\t\t             parameter2);\n"
13428                "\t\t}\n"
13429                "};",
13430                Tab);
13431   Tab.TabWidth = 4;
13432   Tab.IndentWidth = 4;
13433   verifyFormat("class TabWidth4Indent4 {\n"
13434                "\tvoid f() {\n"
13435                "\t\tsomeFunction(parameter1,\n"
13436                "\t\t             parameter2);\n"
13437                "\t}\n"
13438                "};",
13439                Tab);
13440   Tab.TabWidth = 8;
13441   Tab.IndentWidth = 4;
13442   verifyFormat("class TabWidth8Indent4 {\n"
13443                "    void f() {\n"
13444                "\tsomeFunction(parameter1,\n"
13445                "\t             parameter2);\n"
13446                "    }\n"
13447                "};",
13448                Tab);
13449   Tab.TabWidth = 8;
13450   Tab.IndentWidth = 8;
13451   EXPECT_EQ("/*\n"
13452             "              a\t\tcomment\n"
13453             "              in multiple lines\n"
13454             "       */",
13455             format("   /*\t \t \n"
13456                    " \t \t a\t\tcomment\t \t\n"
13457                    " \t \t in multiple lines\t\n"
13458                    " \t  */",
13459                    Tab));
13460   verifyFormat("{\n"
13461                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13462                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13463                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13464                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13465                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13466                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13467                "};",
13468                Tab);
13469   verifyFormat("enum AA {\n"
13470                "\ta1, // Force multiple lines\n"
13471                "\ta2,\n"
13472                "\ta3\n"
13473                "};",
13474                Tab);
13475   EXPECT_EQ("if (aaaaaaaa && // q\n"
13476             "    bb)         // w\n"
13477             "\t;",
13478             format("if (aaaaaaaa &&// q\n"
13479                    "bb)// w\n"
13480                    ";",
13481                    Tab));
13482   verifyFormat("class X {\n"
13483                "\tvoid f() {\n"
13484                "\t\tsomeFunction(parameter1,\n"
13485                "\t\t             parameter2);\n"
13486                "\t}\n"
13487                "};",
13488                Tab);
13489   verifyFormat("{\n"
13490                "\tQ(\n"
13491                "\t    {\n"
13492                "\t\t    int a;\n"
13493                "\t\t    someFunction(aaaaaaaa,\n"
13494                "\t\t                 bbbbbbb);\n"
13495                "\t    },\n"
13496                "\t    p);\n"
13497                "}",
13498                Tab);
13499   EXPECT_EQ("{\n"
13500             "\t/* aaaa\n"
13501             "\t   bbbb */\n"
13502             "}",
13503             format("{\n"
13504                    "/* aaaa\n"
13505                    "   bbbb */\n"
13506                    "}",
13507                    Tab));
13508   EXPECT_EQ("{\n"
13509             "\t/*\n"
13510             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13511             "\t  bbbbbbbbbbbbb\n"
13512             "\t*/\n"
13513             "}",
13514             format("{\n"
13515                    "/*\n"
13516                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13517                    "*/\n"
13518                    "}",
13519                    Tab));
13520   EXPECT_EQ("{\n"
13521             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13522             "\t// bbbbbbbbbbbbb\n"
13523             "}",
13524             format("{\n"
13525                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13526                    "}",
13527                    Tab));
13528   EXPECT_EQ("{\n"
13529             "\t/*\n"
13530             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13531             "\t  bbbbbbbbbbbbb\n"
13532             "\t*/\n"
13533             "}",
13534             format("{\n"
13535                    "\t/*\n"
13536                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13537                    "\t*/\n"
13538                    "}",
13539                    Tab));
13540   EXPECT_EQ("{\n"
13541             "\t/*\n"
13542             "\n"
13543             "\t*/\n"
13544             "}",
13545             format("{\n"
13546                    "\t/*\n"
13547                    "\n"
13548                    "\t*/\n"
13549                    "}",
13550                    Tab));
13551   EXPECT_EQ("{\n"
13552             "\t/*\n"
13553             " asdf\n"
13554             "\t*/\n"
13555             "}",
13556             format("{\n"
13557                    "\t/*\n"
13558                    " asdf\n"
13559                    "\t*/\n"
13560                    "}",
13561                    Tab));
13562   EXPECT_EQ("/* some\n"
13563             "   comment */",
13564             format(" \t \t /* some\n"
13565                    " \t \t    comment */",
13566                    Tab));
13567   EXPECT_EQ("int a; /* some\n"
13568             "   comment */",
13569             format(" \t \t int a; /* some\n"
13570                    " \t \t    comment */",
13571                    Tab));
13572   EXPECT_EQ("int a; /* some\n"
13573             "comment */",
13574             format(" \t \t int\ta; /* some\n"
13575                    " \t \t    comment */",
13576                    Tab));
13577   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13578             "    comment */",
13579             format(" \t \t f(\"\t\t\"); /* some\n"
13580                    " \t \t    comment */",
13581                    Tab));
13582   EXPECT_EQ("{\n"
13583             "\t/*\n"
13584             "\t * Comment\n"
13585             "\t */\n"
13586             "\tint i;\n"
13587             "}",
13588             format("{\n"
13589                    "\t/*\n"
13590                    "\t * Comment\n"
13591                    "\t */\n"
13592                    "\t int i;\n"
13593                    "}",
13594                    Tab));
13595   Tab.TabWidth = 2;
13596   Tab.IndentWidth = 2;
13597   EXPECT_EQ("{\n"
13598             "\t/* aaaa\n"
13599             "\t   bbbb */\n"
13600             "}",
13601             format("{\n"
13602                    "/* aaaa\n"
13603                    "   bbbb */\n"
13604                    "}",
13605                    Tab));
13606   EXPECT_EQ("{\n"
13607             "\t/*\n"
13608             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13609             "\t  bbbbbbbbbbbbb\n"
13610             "\t*/\n"
13611             "}",
13612             format("{\n"
13613                    "/*\n"
13614                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13615                    "*/\n"
13616                    "}",
13617                    Tab));
13618   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13619   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13620   Tab.TabWidth = 4;
13621   Tab.IndentWidth = 4;
13622   verifyFormat("class Assign {\n"
13623                "\tvoid f() {\n"
13624                "\t\tint         x      = 123;\n"
13625                "\t\tint         random = 4;\n"
13626                "\t\tstd::string alphabet =\n"
13627                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13628                "\t}\n"
13629                "};",
13630                Tab);
13631   Tab.AlignOperands = FormatStyle::OAS_Align;
13632   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
13633                "                 cccccccccccccccccccc;",
13634                Tab);
13635   // no alignment
13636   verifyFormat("int aaaaaaaaaa =\n"
13637                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
13638                Tab);
13639   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
13640                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
13641                "                        : 333333333333333;",
13642                Tab);
13643   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
13644   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
13645   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
13646                "               + cccccccccccccccccccc;",
13647                Tab);
13648 }
13649 
13650 TEST_F(FormatTest, ZeroTabWidth) {
13651   FormatStyle Tab = getLLVMStyleWithColumns(42);
13652   Tab.IndentWidth = 8;
13653   Tab.UseTab = FormatStyle::UT_Never;
13654   Tab.TabWidth = 0;
13655   EXPECT_EQ("void a(){\n"
13656             "    // line starts with '\t'\n"
13657             "};",
13658             format("void a(){\n"
13659                    "\t// line starts with '\t'\n"
13660                    "};",
13661                    Tab));
13662 
13663   EXPECT_EQ("void a(){\n"
13664             "    // line starts with '\t'\n"
13665             "};",
13666             format("void a(){\n"
13667                    "\t\t// line starts with '\t'\n"
13668                    "};",
13669                    Tab));
13670 
13671   Tab.UseTab = FormatStyle::UT_ForIndentation;
13672   EXPECT_EQ("void a(){\n"
13673             "    // line starts with '\t'\n"
13674             "};",
13675             format("void a(){\n"
13676                    "\t// line starts with '\t'\n"
13677                    "};",
13678                    Tab));
13679 
13680   EXPECT_EQ("void a(){\n"
13681             "    // line starts with '\t'\n"
13682             "};",
13683             format("void a(){\n"
13684                    "\t\t// line starts with '\t'\n"
13685                    "};",
13686                    Tab));
13687 
13688   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13689   EXPECT_EQ("void a(){\n"
13690             "    // line starts with '\t'\n"
13691             "};",
13692             format("void a(){\n"
13693                    "\t// line starts with '\t'\n"
13694                    "};",
13695                    Tab));
13696 
13697   EXPECT_EQ("void a(){\n"
13698             "    // line starts with '\t'\n"
13699             "};",
13700             format("void a(){\n"
13701                    "\t\t// line starts with '\t'\n"
13702                    "};",
13703                    Tab));
13704 
13705   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13706   EXPECT_EQ("void a(){\n"
13707             "    // line starts with '\t'\n"
13708             "};",
13709             format("void a(){\n"
13710                    "\t// line starts with '\t'\n"
13711                    "};",
13712                    Tab));
13713 
13714   EXPECT_EQ("void a(){\n"
13715             "    // line starts with '\t'\n"
13716             "};",
13717             format("void a(){\n"
13718                    "\t\t// line starts with '\t'\n"
13719                    "};",
13720                    Tab));
13721 
13722   Tab.UseTab = FormatStyle::UT_Always;
13723   EXPECT_EQ("void a(){\n"
13724             "// line starts with '\t'\n"
13725             "};",
13726             format("void a(){\n"
13727                    "\t// line starts with '\t'\n"
13728                    "};",
13729                    Tab));
13730 
13731   EXPECT_EQ("void a(){\n"
13732             "// line starts with '\t'\n"
13733             "};",
13734             format("void a(){\n"
13735                    "\t\t// line starts with '\t'\n"
13736                    "};",
13737                    Tab));
13738 }
13739 
13740 TEST_F(FormatTest, CalculatesOriginalColumn) {
13741   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13742             "q\"; /* some\n"
13743             "       comment */",
13744             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13745                    "q\"; /* some\n"
13746                    "       comment */",
13747                    getLLVMStyle()));
13748   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13749             "/* some\n"
13750             "   comment */",
13751             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13752                    " /* some\n"
13753                    "    comment */",
13754                    getLLVMStyle()));
13755   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13756             "qqq\n"
13757             "/* some\n"
13758             "   comment */",
13759             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13760                    "qqq\n"
13761                    " /* some\n"
13762                    "    comment */",
13763                    getLLVMStyle()));
13764   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13765             "wwww; /* some\n"
13766             "         comment */",
13767             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13768                    "wwww; /* some\n"
13769                    "         comment */",
13770                    getLLVMStyle()));
13771 }
13772 
13773 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
13774   FormatStyle NoSpace = getLLVMStyle();
13775   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
13776 
13777   verifyFormat("while(true)\n"
13778                "  continue;",
13779                NoSpace);
13780   verifyFormat("for(;;)\n"
13781                "  continue;",
13782                NoSpace);
13783   verifyFormat("if(true)\n"
13784                "  f();\n"
13785                "else if(true)\n"
13786                "  f();",
13787                NoSpace);
13788   verifyFormat("do {\n"
13789                "  do_something();\n"
13790                "} while(something());",
13791                NoSpace);
13792   verifyFormat("switch(x) {\n"
13793                "default:\n"
13794                "  break;\n"
13795                "}",
13796                NoSpace);
13797   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
13798   verifyFormat("size_t x = sizeof(x);", NoSpace);
13799   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
13800   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
13801   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
13802   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
13803   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
13804   verifyFormat("alignas(128) char a[128];", NoSpace);
13805   verifyFormat("size_t x = alignof(MyType);", NoSpace);
13806   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
13807   verifyFormat("int f() throw(Deprecated);", NoSpace);
13808   verifyFormat("typedef void (*cb)(int);", NoSpace);
13809   verifyFormat("T A::operator()();", NoSpace);
13810   verifyFormat("X A::operator++(T);", NoSpace);
13811   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
13812 
13813   FormatStyle Space = getLLVMStyle();
13814   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
13815 
13816   verifyFormat("int f ();", Space);
13817   verifyFormat("void f (int a, T b) {\n"
13818                "  while (true)\n"
13819                "    continue;\n"
13820                "}",
13821                Space);
13822   verifyFormat("if (true)\n"
13823                "  f ();\n"
13824                "else if (true)\n"
13825                "  f ();",
13826                Space);
13827   verifyFormat("do {\n"
13828                "  do_something ();\n"
13829                "} while (something ());",
13830                Space);
13831   verifyFormat("switch (x) {\n"
13832                "default:\n"
13833                "  break;\n"
13834                "}",
13835                Space);
13836   verifyFormat("A::A () : a (1) {}", Space);
13837   verifyFormat("void f () __attribute__ ((asdf));", Space);
13838   verifyFormat("*(&a + 1);\n"
13839                "&((&a)[1]);\n"
13840                "a[(b + c) * d];\n"
13841                "(((a + 1) * 2) + 3) * 4;",
13842                Space);
13843   verifyFormat("#define A(x) x", Space);
13844   verifyFormat("#define A (x) x", Space);
13845   verifyFormat("#if defined(x)\n"
13846                "#endif",
13847                Space);
13848   verifyFormat("auto i = std::make_unique<int> (5);", Space);
13849   verifyFormat("size_t x = sizeof (x);", Space);
13850   verifyFormat("auto f (int x) -> decltype (x);", Space);
13851   verifyFormat("auto f (int x) -> typeof (x);", Space);
13852   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
13853   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
13854   verifyFormat("int f (T x) noexcept (x.create ());", Space);
13855   verifyFormat("alignas (128) char a[128];", Space);
13856   verifyFormat("size_t x = alignof (MyType);", Space);
13857   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
13858   verifyFormat("int f () throw (Deprecated);", Space);
13859   verifyFormat("typedef void (*cb) (int);", Space);
13860   verifyFormat("T A::operator() ();", Space);
13861   verifyFormat("X A::operator++ (T);", Space);
13862   verifyFormat("auto lambda = [] () { return 0; };", Space);
13863   verifyFormat("int x = int (y);", Space);
13864 
13865   FormatStyle SomeSpace = getLLVMStyle();
13866   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
13867 
13868   verifyFormat("[]() -> float {}", SomeSpace);
13869   verifyFormat("[] (auto foo) {}", SomeSpace);
13870   verifyFormat("[foo]() -> int {}", SomeSpace);
13871   verifyFormat("int f();", SomeSpace);
13872   verifyFormat("void f (int a, T b) {\n"
13873                "  while (true)\n"
13874                "    continue;\n"
13875                "}",
13876                SomeSpace);
13877   verifyFormat("if (true)\n"
13878                "  f();\n"
13879                "else if (true)\n"
13880                "  f();",
13881                SomeSpace);
13882   verifyFormat("do {\n"
13883                "  do_something();\n"
13884                "} while (something());",
13885                SomeSpace);
13886   verifyFormat("switch (x) {\n"
13887                "default:\n"
13888                "  break;\n"
13889                "}",
13890                SomeSpace);
13891   verifyFormat("A::A() : a (1) {}", SomeSpace);
13892   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
13893   verifyFormat("*(&a + 1);\n"
13894                "&((&a)[1]);\n"
13895                "a[(b + c) * d];\n"
13896                "(((a + 1) * 2) + 3) * 4;",
13897                SomeSpace);
13898   verifyFormat("#define A(x) x", SomeSpace);
13899   verifyFormat("#define A (x) x", SomeSpace);
13900   verifyFormat("#if defined(x)\n"
13901                "#endif",
13902                SomeSpace);
13903   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
13904   verifyFormat("size_t x = sizeof (x);", SomeSpace);
13905   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
13906   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
13907   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
13908   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
13909   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
13910   verifyFormat("alignas (128) char a[128];", SomeSpace);
13911   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
13912   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
13913                SomeSpace);
13914   verifyFormat("int f() throw (Deprecated);", SomeSpace);
13915   verifyFormat("typedef void (*cb) (int);", SomeSpace);
13916   verifyFormat("T A::operator()();", SomeSpace);
13917   verifyFormat("X A::operator++ (T);", SomeSpace);
13918   verifyFormat("int x = int (y);", SomeSpace);
13919   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
13920 }
13921 
13922 TEST_F(FormatTest, SpaceAfterLogicalNot) {
13923   FormatStyle Spaces = getLLVMStyle();
13924   Spaces.SpaceAfterLogicalNot = true;
13925 
13926   verifyFormat("bool x = ! y", Spaces);
13927   verifyFormat("if (! isFailure())", Spaces);
13928   verifyFormat("if (! (a && b))", Spaces);
13929   verifyFormat("\"Error!\"", Spaces);
13930   verifyFormat("! ! x", Spaces);
13931 }
13932 
13933 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
13934   FormatStyle Spaces = getLLVMStyle();
13935 
13936   Spaces.SpacesInParentheses = true;
13937   verifyFormat("do_something( ::globalVar );", Spaces);
13938   verifyFormat("call( x, y, z );", Spaces);
13939   verifyFormat("call();", Spaces);
13940   verifyFormat("std::function<void( int, int )> callback;", Spaces);
13941   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
13942                Spaces);
13943   verifyFormat("while ( (bool)1 )\n"
13944                "  continue;",
13945                Spaces);
13946   verifyFormat("for ( ;; )\n"
13947                "  continue;",
13948                Spaces);
13949   verifyFormat("if ( true )\n"
13950                "  f();\n"
13951                "else if ( true )\n"
13952                "  f();",
13953                Spaces);
13954   verifyFormat("do {\n"
13955                "  do_something( (int)i );\n"
13956                "} while ( something() );",
13957                Spaces);
13958   verifyFormat("switch ( x ) {\n"
13959                "default:\n"
13960                "  break;\n"
13961                "}",
13962                Spaces);
13963 
13964   Spaces.SpacesInParentheses = false;
13965   Spaces.SpacesInCStyleCastParentheses = true;
13966   verifyFormat("Type *A = ( Type * )P;", Spaces);
13967   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
13968   verifyFormat("x = ( int32 )y;", Spaces);
13969   verifyFormat("int a = ( int )(2.0f);", Spaces);
13970   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
13971   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
13972   verifyFormat("#define x (( int )-1)", Spaces);
13973 
13974   // Run the first set of tests again with:
13975   Spaces.SpacesInParentheses = false;
13976   Spaces.SpaceInEmptyParentheses = true;
13977   Spaces.SpacesInCStyleCastParentheses = true;
13978   verifyFormat("call(x, y, z);", Spaces);
13979   verifyFormat("call( );", Spaces);
13980   verifyFormat("std::function<void(int, int)> callback;", Spaces);
13981   verifyFormat("while (( bool )1)\n"
13982                "  continue;",
13983                Spaces);
13984   verifyFormat("for (;;)\n"
13985                "  continue;",
13986                Spaces);
13987   verifyFormat("if (true)\n"
13988                "  f( );\n"
13989                "else if (true)\n"
13990                "  f( );",
13991                Spaces);
13992   verifyFormat("do {\n"
13993                "  do_something(( int )i);\n"
13994                "} while (something( ));",
13995                Spaces);
13996   verifyFormat("switch (x) {\n"
13997                "default:\n"
13998                "  break;\n"
13999                "}",
14000                Spaces);
14001 
14002   // Run the first set of tests again with:
14003   Spaces.SpaceAfterCStyleCast = true;
14004   verifyFormat("call(x, y, z);", Spaces);
14005   verifyFormat("call( );", Spaces);
14006   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14007   verifyFormat("while (( bool ) 1)\n"
14008                "  continue;",
14009                Spaces);
14010   verifyFormat("for (;;)\n"
14011                "  continue;",
14012                Spaces);
14013   verifyFormat("if (true)\n"
14014                "  f( );\n"
14015                "else if (true)\n"
14016                "  f( );",
14017                Spaces);
14018   verifyFormat("do {\n"
14019                "  do_something(( int ) i);\n"
14020                "} while (something( ));",
14021                Spaces);
14022   verifyFormat("switch (x) {\n"
14023                "default:\n"
14024                "  break;\n"
14025                "}",
14026                Spaces);
14027 
14028   // Run subset of tests again with:
14029   Spaces.SpacesInCStyleCastParentheses = false;
14030   Spaces.SpaceAfterCStyleCast = true;
14031   verifyFormat("while ((bool) 1)\n"
14032                "  continue;",
14033                Spaces);
14034   verifyFormat("do {\n"
14035                "  do_something((int) i);\n"
14036                "} while (something( ));",
14037                Spaces);
14038 
14039   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14040   verifyFormat("size_t idx = (size_t) a;", Spaces);
14041   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14042   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14043   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14044   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14045   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14046   Spaces.ColumnLimit = 80;
14047   Spaces.IndentWidth = 4;
14048   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14049   verifyFormat("void foo( ) {\n"
14050                "    size_t foo = (*(function))(\n"
14051                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14052                "BarrrrrrrrrrrrLong,\n"
14053                "        FoooooooooLooooong);\n"
14054                "}",
14055                Spaces);
14056   Spaces.SpaceAfterCStyleCast = false;
14057   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
14058   verifyFormat("size_t idx = (size_t)a;", Spaces);
14059   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
14060   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14061   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14062   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14063   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14064 
14065   verifyFormat("void foo( ) {\n"
14066                "    size_t foo = (*(function))(\n"
14067                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14068                "BarrrrrrrrrrrrLong,\n"
14069                "        FoooooooooLooooong);\n"
14070                "}",
14071                Spaces);
14072 }
14073 
14074 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
14075   verifyFormat("int a[5];");
14076   verifyFormat("a[3] += 42;");
14077 
14078   FormatStyle Spaces = getLLVMStyle();
14079   Spaces.SpacesInSquareBrackets = true;
14080   // Not lambdas.
14081   verifyFormat("int a[ 5 ];", Spaces);
14082   verifyFormat("a[ 3 ] += 42;", Spaces);
14083   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
14084   verifyFormat("double &operator[](int i) { return 0; }\n"
14085                "int i;",
14086                Spaces);
14087   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
14088   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
14089   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
14090   // Lambdas.
14091   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
14092   verifyFormat("return [ i, args... ] {};", Spaces);
14093   verifyFormat("int foo = [ &bar ]() {};", Spaces);
14094   verifyFormat("int foo = [ = ]() {};", Spaces);
14095   verifyFormat("int foo = [ & ]() {};", Spaces);
14096   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
14097   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
14098 }
14099 
14100 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
14101   FormatStyle NoSpaceStyle = getLLVMStyle();
14102   verifyFormat("int a[5];", NoSpaceStyle);
14103   verifyFormat("a[3] += 42;", NoSpaceStyle);
14104 
14105   verifyFormat("int a[1];", NoSpaceStyle);
14106   verifyFormat("int 1 [a];", NoSpaceStyle);
14107   verifyFormat("int a[1][2];", NoSpaceStyle);
14108   verifyFormat("a[7] = 5;", NoSpaceStyle);
14109   verifyFormat("int a = (f())[23];", NoSpaceStyle);
14110   verifyFormat("f([] {})", NoSpaceStyle);
14111 
14112   FormatStyle Space = getLLVMStyle();
14113   Space.SpaceBeforeSquareBrackets = true;
14114   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
14115   verifyFormat("return [i, args...] {};", Space);
14116 
14117   verifyFormat("int a [5];", Space);
14118   verifyFormat("a [3] += 42;", Space);
14119   verifyFormat("constexpr char hello []{\"hello\"};", Space);
14120   verifyFormat("double &operator[](int i) { return 0; }\n"
14121                "int i;",
14122                Space);
14123   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
14124   verifyFormat("int i = a [a][a]->f();", Space);
14125   verifyFormat("int i = (*b) [a]->f();", Space);
14126 
14127   verifyFormat("int a [1];", Space);
14128   verifyFormat("int 1 [a];", Space);
14129   verifyFormat("int a [1][2];", Space);
14130   verifyFormat("a [7] = 5;", Space);
14131   verifyFormat("int a = (f()) [23];", Space);
14132   verifyFormat("f([] {})", Space);
14133 }
14134 
14135 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
14136   verifyFormat("int a = 5;");
14137   verifyFormat("a += 42;");
14138   verifyFormat("a or_eq 8;");
14139 
14140   FormatStyle Spaces = getLLVMStyle();
14141   Spaces.SpaceBeforeAssignmentOperators = false;
14142   verifyFormat("int a= 5;", Spaces);
14143   verifyFormat("a+= 42;", Spaces);
14144   verifyFormat("a or_eq 8;", Spaces);
14145 }
14146 
14147 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
14148   verifyFormat("class Foo : public Bar {};");
14149   verifyFormat("Foo::Foo() : foo(1) {}");
14150   verifyFormat("for (auto a : b) {\n}");
14151   verifyFormat("int x = a ? b : c;");
14152   verifyFormat("{\n"
14153                "label0:\n"
14154                "  int x = 0;\n"
14155                "}");
14156   verifyFormat("switch (x) {\n"
14157                "case 1:\n"
14158                "default:\n"
14159                "}");
14160   verifyFormat("switch (allBraces) {\n"
14161                "case 1: {\n"
14162                "  break;\n"
14163                "}\n"
14164                "case 2: {\n"
14165                "  [[fallthrough]];\n"
14166                "}\n"
14167                "default: {\n"
14168                "  break;\n"
14169                "}\n"
14170                "}");
14171 
14172   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
14173   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
14174   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
14175   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
14176   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
14177   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
14178   verifyFormat("{\n"
14179                "label1:\n"
14180                "  int x = 0;\n"
14181                "}",
14182                CtorInitializerStyle);
14183   verifyFormat("switch (x) {\n"
14184                "case 1:\n"
14185                "default:\n"
14186                "}",
14187                CtorInitializerStyle);
14188   verifyFormat("switch (allBraces) {\n"
14189                "case 1: {\n"
14190                "  break;\n"
14191                "}\n"
14192                "case 2: {\n"
14193                "  [[fallthrough]];\n"
14194                "}\n"
14195                "default: {\n"
14196                "  break;\n"
14197                "}\n"
14198                "}",
14199                CtorInitializerStyle);
14200   CtorInitializerStyle.BreakConstructorInitializers =
14201       FormatStyle::BCIS_AfterColon;
14202   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
14203                "    aaaaaaaaaaaaaaaa(1),\n"
14204                "    bbbbbbbbbbbbbbbb(2) {}",
14205                CtorInitializerStyle);
14206   CtorInitializerStyle.BreakConstructorInitializers =
14207       FormatStyle::BCIS_BeforeComma;
14208   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14209                "    : aaaaaaaaaaaaaaaa(1)\n"
14210                "    , bbbbbbbbbbbbbbbb(2) {}",
14211                CtorInitializerStyle);
14212   CtorInitializerStyle.BreakConstructorInitializers =
14213       FormatStyle::BCIS_BeforeColon;
14214   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14215                "    : aaaaaaaaaaaaaaaa(1),\n"
14216                "      bbbbbbbbbbbbbbbb(2) {}",
14217                CtorInitializerStyle);
14218   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
14219   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14220                ": aaaaaaaaaaaaaaaa(1),\n"
14221                "  bbbbbbbbbbbbbbbb(2) {}",
14222                CtorInitializerStyle);
14223 
14224   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
14225   InheritanceStyle.SpaceBeforeInheritanceColon = false;
14226   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
14227   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
14228   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
14229   verifyFormat("int x = a ? b : c;", InheritanceStyle);
14230   verifyFormat("{\n"
14231                "label2:\n"
14232                "  int x = 0;\n"
14233                "}",
14234                InheritanceStyle);
14235   verifyFormat("switch (x) {\n"
14236                "case 1:\n"
14237                "default:\n"
14238                "}",
14239                InheritanceStyle);
14240   verifyFormat("switch (allBraces) {\n"
14241                "case 1: {\n"
14242                "  break;\n"
14243                "}\n"
14244                "case 2: {\n"
14245                "  [[fallthrough]];\n"
14246                "}\n"
14247                "default: {\n"
14248                "  break;\n"
14249                "}\n"
14250                "}",
14251                InheritanceStyle);
14252   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
14253   verifyFormat("class Foooooooooooooooooooooo\n"
14254                "    : public aaaaaaaaaaaaaaaaaa,\n"
14255                "      public bbbbbbbbbbbbbbbbbb {\n"
14256                "}",
14257                InheritanceStyle);
14258   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
14259   verifyFormat("class Foooooooooooooooooooooo:\n"
14260                "    public aaaaaaaaaaaaaaaaaa,\n"
14261                "    public bbbbbbbbbbbbbbbbbb {\n"
14262                "}",
14263                InheritanceStyle);
14264   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
14265   verifyFormat("class Foooooooooooooooooooooo\n"
14266                "    : public aaaaaaaaaaaaaaaaaa\n"
14267                "    , public bbbbbbbbbbbbbbbbbb {\n"
14268                "}",
14269                InheritanceStyle);
14270   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
14271   verifyFormat("class Foooooooooooooooooooooo\n"
14272                "    : public aaaaaaaaaaaaaaaaaa,\n"
14273                "      public bbbbbbbbbbbbbbbbbb {\n"
14274                "}",
14275                InheritanceStyle);
14276   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
14277   verifyFormat("class Foooooooooooooooooooooo\n"
14278                ": public aaaaaaaaaaaaaaaaaa,\n"
14279                "  public bbbbbbbbbbbbbbbbbb {}",
14280                InheritanceStyle);
14281 
14282   FormatStyle ForLoopStyle = getLLVMStyle();
14283   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
14284   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
14285   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
14286   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
14287   verifyFormat("int x = a ? b : c;", ForLoopStyle);
14288   verifyFormat("{\n"
14289                "label2:\n"
14290                "  int x = 0;\n"
14291                "}",
14292                ForLoopStyle);
14293   verifyFormat("switch (x) {\n"
14294                "case 1:\n"
14295                "default:\n"
14296                "}",
14297                ForLoopStyle);
14298   verifyFormat("switch (allBraces) {\n"
14299                "case 1: {\n"
14300                "  break;\n"
14301                "}\n"
14302                "case 2: {\n"
14303                "  [[fallthrough]];\n"
14304                "}\n"
14305                "default: {\n"
14306                "  break;\n"
14307                "}\n"
14308                "}",
14309                ForLoopStyle);
14310 
14311   FormatStyle CaseStyle = getLLVMStyle();
14312   CaseStyle.SpaceBeforeCaseColon = true;
14313   verifyFormat("class Foo : public Bar {};", CaseStyle);
14314   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
14315   verifyFormat("for (auto a : b) {\n}", CaseStyle);
14316   verifyFormat("int x = a ? b : c;", CaseStyle);
14317   verifyFormat("switch (x) {\n"
14318                "case 1 :\n"
14319                "default :\n"
14320                "}",
14321                CaseStyle);
14322   verifyFormat("switch (allBraces) {\n"
14323                "case 1 : {\n"
14324                "  break;\n"
14325                "}\n"
14326                "case 2 : {\n"
14327                "  [[fallthrough]];\n"
14328                "}\n"
14329                "default : {\n"
14330                "  break;\n"
14331                "}\n"
14332                "}",
14333                CaseStyle);
14334 
14335   FormatStyle NoSpaceStyle = getLLVMStyle();
14336   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
14337   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14338   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
14339   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14340   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
14341   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
14342   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
14343   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
14344   verifyFormat("{\n"
14345                "label3:\n"
14346                "  int x = 0;\n"
14347                "}",
14348                NoSpaceStyle);
14349   verifyFormat("switch (x) {\n"
14350                "case 1:\n"
14351                "default:\n"
14352                "}",
14353                NoSpaceStyle);
14354   verifyFormat("switch (allBraces) {\n"
14355                "case 1: {\n"
14356                "  break;\n"
14357                "}\n"
14358                "case 2: {\n"
14359                "  [[fallthrough]];\n"
14360                "}\n"
14361                "default: {\n"
14362                "  break;\n"
14363                "}\n"
14364                "}",
14365                NoSpaceStyle);
14366 
14367   FormatStyle InvertedSpaceStyle = getLLVMStyle();
14368   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
14369   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14370   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
14371   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14372   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
14373   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
14374   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
14375   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
14376   verifyFormat("{\n"
14377                "label3:\n"
14378                "  int x = 0;\n"
14379                "}",
14380                InvertedSpaceStyle);
14381   verifyFormat("switch (x) {\n"
14382                "case 1 :\n"
14383                "case 2 : {\n"
14384                "  break;\n"
14385                "}\n"
14386                "default :\n"
14387                "  break;\n"
14388                "}",
14389                InvertedSpaceStyle);
14390   verifyFormat("switch (allBraces) {\n"
14391                "case 1 : {\n"
14392                "  break;\n"
14393                "}\n"
14394                "case 2 : {\n"
14395                "  [[fallthrough]];\n"
14396                "}\n"
14397                "default : {\n"
14398                "  break;\n"
14399                "}\n"
14400                "}",
14401                InvertedSpaceStyle);
14402 }
14403 
14404 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
14405   FormatStyle Style = getLLVMStyle();
14406 
14407   Style.PointerAlignment = FormatStyle::PAS_Left;
14408   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14409   verifyFormat("void* const* x = NULL;", Style);
14410 
14411 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
14412   do {                                                                         \
14413     Style.PointerAlignment = FormatStyle::Pointers;                            \
14414     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
14415     verifyFormat(Code, Style);                                                 \
14416   } while (false)
14417 
14418   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
14419   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
14420   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
14421 
14422   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
14423   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
14424   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
14425 
14426   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
14427   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
14428   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
14429 
14430   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
14431   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
14432   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
14433 
14434   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
14435   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14436                         SAPQ_Default);
14437   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14438                         SAPQ_Default);
14439 
14440   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
14441   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14442                         SAPQ_Before);
14443   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14444                         SAPQ_Before);
14445 
14446   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
14447   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
14448   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14449                         SAPQ_After);
14450 
14451   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
14452   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
14453   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
14454 
14455 #undef verifyQualifierSpaces
14456 
14457   FormatStyle Spaces = getLLVMStyle();
14458   Spaces.AttributeMacros.push_back("qualified");
14459   Spaces.PointerAlignment = FormatStyle::PAS_Right;
14460   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14461   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
14462   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
14463   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
14464   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
14465   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14466   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14467   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
14468   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
14469   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14470   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14471   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14472 
14473   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
14474   Spaces.PointerAlignment = FormatStyle::PAS_Left;
14475   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14476   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
14477   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
14478   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
14479   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
14480   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14481   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
14482   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14483   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
14484   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
14485   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
14486   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
14487   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14488 
14489   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
14490   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
14491   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14492   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
14493   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
14494   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14495   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14496   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14497 }
14498 
14499 TEST_F(FormatTest, AlignConsecutiveMacros) {
14500   FormatStyle Style = getLLVMStyle();
14501   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14502   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14503   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14504 
14505   verifyFormat("#define a 3\n"
14506                "#define bbbb 4\n"
14507                "#define ccc (5)",
14508                Style);
14509 
14510   verifyFormat("#define f(x) (x * x)\n"
14511                "#define fff(x, y, z) (x * y + z)\n"
14512                "#define ffff(x, y) (x - y)",
14513                Style);
14514 
14515   verifyFormat("#define foo(x, y) (x + y)\n"
14516                "#define bar (5, 6)(2 + 2)",
14517                Style);
14518 
14519   verifyFormat("#define a 3\n"
14520                "#define bbbb 4\n"
14521                "#define ccc (5)\n"
14522                "#define f(x) (x * x)\n"
14523                "#define fff(x, y, z) (x * y + z)\n"
14524                "#define ffff(x, y) (x - y)",
14525                Style);
14526 
14527   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14528   verifyFormat("#define a    3\n"
14529                "#define bbbb 4\n"
14530                "#define ccc  (5)",
14531                Style);
14532 
14533   verifyFormat("#define f(x)         (x * x)\n"
14534                "#define fff(x, y, z) (x * y + z)\n"
14535                "#define ffff(x, y)   (x - y)",
14536                Style);
14537 
14538   verifyFormat("#define foo(x, y) (x + y)\n"
14539                "#define bar       (5, 6)(2 + 2)",
14540                Style);
14541 
14542   verifyFormat("#define a            3\n"
14543                "#define bbbb         4\n"
14544                "#define ccc          (5)\n"
14545                "#define f(x)         (x * x)\n"
14546                "#define fff(x, y, z) (x * y + z)\n"
14547                "#define ffff(x, y)   (x - y)",
14548                Style);
14549 
14550   verifyFormat("#define a         5\n"
14551                "#define foo(x, y) (x + y)\n"
14552                "#define CCC       (6)\n"
14553                "auto lambda = []() {\n"
14554                "  auto  ii = 0;\n"
14555                "  float j  = 0;\n"
14556                "  return 0;\n"
14557                "};\n"
14558                "int   i  = 0;\n"
14559                "float i2 = 0;\n"
14560                "auto  v  = type{\n"
14561                "    i = 1,   //\n"
14562                "    (i = 2), //\n"
14563                "    i = 3    //\n"
14564                "};",
14565                Style);
14566 
14567   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14568   Style.ColumnLimit = 20;
14569 
14570   verifyFormat("#define a          \\\n"
14571                "  \"aabbbbbbbbbbbb\"\n"
14572                "#define D          \\\n"
14573                "  \"aabbbbbbbbbbbb\" \\\n"
14574                "  \"ccddeeeeeeeee\"\n"
14575                "#define B          \\\n"
14576                "  \"QQQQQQQQQQQQQ\"  \\\n"
14577                "  \"FFFFFFFFFFFFF\"  \\\n"
14578                "  \"LLLLLLLL\"\n",
14579                Style);
14580 
14581   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14582   verifyFormat("#define a          \\\n"
14583                "  \"aabbbbbbbbbbbb\"\n"
14584                "#define D          \\\n"
14585                "  \"aabbbbbbbbbbbb\" \\\n"
14586                "  \"ccddeeeeeeeee\"\n"
14587                "#define B          \\\n"
14588                "  \"QQQQQQQQQQQQQ\"  \\\n"
14589                "  \"FFFFFFFFFFFFF\"  \\\n"
14590                "  \"LLLLLLLL\"\n",
14591                Style);
14592 
14593   // Test across comments
14594   Style.MaxEmptyLinesToKeep = 10;
14595   Style.ReflowComments = false;
14596   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
14597   EXPECT_EQ("#define a    3\n"
14598             "// line comment\n"
14599             "#define bbbb 4\n"
14600             "#define ccc  (5)",
14601             format("#define a 3\n"
14602                    "// line comment\n"
14603                    "#define bbbb 4\n"
14604                    "#define ccc (5)",
14605                    Style));
14606 
14607   EXPECT_EQ("#define a    3\n"
14608             "/* block comment */\n"
14609             "#define bbbb 4\n"
14610             "#define ccc  (5)",
14611             format("#define a  3\n"
14612                    "/* block comment */\n"
14613                    "#define bbbb 4\n"
14614                    "#define ccc (5)",
14615                    Style));
14616 
14617   EXPECT_EQ("#define a    3\n"
14618             "/* multi-line *\n"
14619             " * block comment */\n"
14620             "#define bbbb 4\n"
14621             "#define ccc  (5)",
14622             format("#define a 3\n"
14623                    "/* multi-line *\n"
14624                    " * block comment */\n"
14625                    "#define bbbb 4\n"
14626                    "#define ccc (5)",
14627                    Style));
14628 
14629   EXPECT_EQ("#define a    3\n"
14630             "// multi-line line comment\n"
14631             "//\n"
14632             "#define bbbb 4\n"
14633             "#define ccc  (5)",
14634             format("#define a  3\n"
14635                    "// multi-line line comment\n"
14636                    "//\n"
14637                    "#define bbbb 4\n"
14638                    "#define ccc (5)",
14639                    Style));
14640 
14641   EXPECT_EQ("#define a 3\n"
14642             "// empty lines still break.\n"
14643             "\n"
14644             "#define bbbb 4\n"
14645             "#define ccc  (5)",
14646             format("#define a     3\n"
14647                    "// empty lines still break.\n"
14648                    "\n"
14649                    "#define bbbb     4\n"
14650                    "#define ccc  (5)",
14651                    Style));
14652 
14653   // Test across empty lines
14654   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
14655   EXPECT_EQ("#define a    3\n"
14656             "\n"
14657             "#define bbbb 4\n"
14658             "#define ccc  (5)",
14659             format("#define a 3\n"
14660                    "\n"
14661                    "#define bbbb 4\n"
14662                    "#define ccc (5)",
14663                    Style));
14664 
14665   EXPECT_EQ("#define a    3\n"
14666             "\n"
14667             "\n"
14668             "\n"
14669             "#define bbbb 4\n"
14670             "#define ccc  (5)",
14671             format("#define a        3\n"
14672                    "\n"
14673                    "\n"
14674                    "\n"
14675                    "#define bbbb 4\n"
14676                    "#define ccc (5)",
14677                    Style));
14678 
14679   EXPECT_EQ("#define a 3\n"
14680             "// comments should break alignment\n"
14681             "//\n"
14682             "#define bbbb 4\n"
14683             "#define ccc  (5)",
14684             format("#define a        3\n"
14685                    "// comments should break alignment\n"
14686                    "//\n"
14687                    "#define bbbb 4\n"
14688                    "#define ccc (5)",
14689                    Style));
14690 
14691   // Test across empty lines and comments
14692   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
14693   verifyFormat("#define a    3\n"
14694                "\n"
14695                "// line comment\n"
14696                "#define bbbb 4\n"
14697                "#define ccc  (5)",
14698                Style);
14699 
14700   EXPECT_EQ("#define a    3\n"
14701             "\n"
14702             "\n"
14703             "/* multi-line *\n"
14704             " * block comment */\n"
14705             "\n"
14706             "\n"
14707             "#define bbbb 4\n"
14708             "#define ccc  (5)",
14709             format("#define a 3\n"
14710                    "\n"
14711                    "\n"
14712                    "/* multi-line *\n"
14713                    " * block comment */\n"
14714                    "\n"
14715                    "\n"
14716                    "#define bbbb 4\n"
14717                    "#define ccc (5)",
14718                    Style));
14719 
14720   EXPECT_EQ("#define a    3\n"
14721             "\n"
14722             "\n"
14723             "/* multi-line *\n"
14724             " * block comment */\n"
14725             "\n"
14726             "\n"
14727             "#define bbbb 4\n"
14728             "#define ccc  (5)",
14729             format("#define a 3\n"
14730                    "\n"
14731                    "\n"
14732                    "/* multi-line *\n"
14733                    " * block comment */\n"
14734                    "\n"
14735                    "\n"
14736                    "#define bbbb 4\n"
14737                    "#define ccc       (5)",
14738                    Style));
14739 }
14740 
14741 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
14742   FormatStyle Alignment = getLLVMStyle();
14743   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14744   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
14745 
14746   Alignment.MaxEmptyLinesToKeep = 10;
14747   /* Test alignment across empty lines */
14748   EXPECT_EQ("int a           = 5;\n"
14749             "\n"
14750             "int oneTwoThree = 123;",
14751             format("int a       = 5;\n"
14752                    "\n"
14753                    "int oneTwoThree= 123;",
14754                    Alignment));
14755   EXPECT_EQ("int a           = 5;\n"
14756             "int one         = 1;\n"
14757             "\n"
14758             "int oneTwoThree = 123;",
14759             format("int a = 5;\n"
14760                    "int one = 1;\n"
14761                    "\n"
14762                    "int oneTwoThree = 123;",
14763                    Alignment));
14764   EXPECT_EQ("int a           = 5;\n"
14765             "int one         = 1;\n"
14766             "\n"
14767             "int oneTwoThree = 123;\n"
14768             "int oneTwo      = 12;",
14769             format("int a = 5;\n"
14770                    "int one = 1;\n"
14771                    "\n"
14772                    "int oneTwoThree = 123;\n"
14773                    "int oneTwo = 12;",
14774                    Alignment));
14775 
14776   /* Test across comments */
14777   EXPECT_EQ("int a = 5;\n"
14778             "/* block comment */\n"
14779             "int oneTwoThree = 123;",
14780             format("int a = 5;\n"
14781                    "/* block comment */\n"
14782                    "int oneTwoThree=123;",
14783                    Alignment));
14784 
14785   EXPECT_EQ("int a = 5;\n"
14786             "// line comment\n"
14787             "int oneTwoThree = 123;",
14788             format("int a = 5;\n"
14789                    "// line comment\n"
14790                    "int oneTwoThree=123;",
14791                    Alignment));
14792 
14793   /* Test across comments and newlines */
14794   EXPECT_EQ("int a = 5;\n"
14795             "\n"
14796             "/* block comment */\n"
14797             "int oneTwoThree = 123;",
14798             format("int a = 5;\n"
14799                    "\n"
14800                    "/* block comment */\n"
14801                    "int oneTwoThree=123;",
14802                    Alignment));
14803 
14804   EXPECT_EQ("int a = 5;\n"
14805             "\n"
14806             "// line comment\n"
14807             "int oneTwoThree = 123;",
14808             format("int a = 5;\n"
14809                    "\n"
14810                    "// line comment\n"
14811                    "int oneTwoThree=123;",
14812                    Alignment));
14813 }
14814 
14815 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
14816   FormatStyle Alignment = getLLVMStyle();
14817   Alignment.AlignConsecutiveDeclarations =
14818       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14819   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
14820 
14821   Alignment.MaxEmptyLinesToKeep = 10;
14822   /* Test alignment across empty lines */
14823   EXPECT_EQ("int         a = 5;\n"
14824             "\n"
14825             "float const oneTwoThree = 123;",
14826             format("int a = 5;\n"
14827                    "\n"
14828                    "float const oneTwoThree = 123;",
14829                    Alignment));
14830   EXPECT_EQ("int         a = 5;\n"
14831             "float const one = 1;\n"
14832             "\n"
14833             "int         oneTwoThree = 123;",
14834             format("int a = 5;\n"
14835                    "float const one = 1;\n"
14836                    "\n"
14837                    "int oneTwoThree = 123;",
14838                    Alignment));
14839 
14840   /* Test across comments */
14841   EXPECT_EQ("float const a = 5;\n"
14842             "/* block comment */\n"
14843             "int         oneTwoThree = 123;",
14844             format("float const a = 5;\n"
14845                    "/* block comment */\n"
14846                    "int oneTwoThree=123;",
14847                    Alignment));
14848 
14849   EXPECT_EQ("float const a = 5;\n"
14850             "// line comment\n"
14851             "int         oneTwoThree = 123;",
14852             format("float const a = 5;\n"
14853                    "// line comment\n"
14854                    "int oneTwoThree=123;",
14855                    Alignment));
14856 
14857   /* Test across comments and newlines */
14858   EXPECT_EQ("float const a = 5;\n"
14859             "\n"
14860             "/* block comment */\n"
14861             "int         oneTwoThree = 123;",
14862             format("float const a = 5;\n"
14863                    "\n"
14864                    "/* block comment */\n"
14865                    "int         oneTwoThree=123;",
14866                    Alignment));
14867 
14868   EXPECT_EQ("float const a = 5;\n"
14869             "\n"
14870             "// line comment\n"
14871             "int         oneTwoThree = 123;",
14872             format("float const a = 5;\n"
14873                    "\n"
14874                    "// line comment\n"
14875                    "int oneTwoThree=123;",
14876                    Alignment));
14877 }
14878 
14879 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
14880   FormatStyle Alignment = getLLVMStyle();
14881   Alignment.AlignConsecutiveBitFields =
14882       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14883 
14884   Alignment.MaxEmptyLinesToKeep = 10;
14885   /* Test alignment across empty lines */
14886   EXPECT_EQ("int a            : 5;\n"
14887             "\n"
14888             "int longbitfield : 6;",
14889             format("int a : 5;\n"
14890                    "\n"
14891                    "int longbitfield : 6;",
14892                    Alignment));
14893   EXPECT_EQ("int a            : 5;\n"
14894             "int one          : 1;\n"
14895             "\n"
14896             "int longbitfield : 6;",
14897             format("int a : 5;\n"
14898                    "int one : 1;\n"
14899                    "\n"
14900                    "int longbitfield : 6;",
14901                    Alignment));
14902 
14903   /* Test across comments */
14904   EXPECT_EQ("int a            : 5;\n"
14905             "/* block comment */\n"
14906             "int longbitfield : 6;",
14907             format("int a : 5;\n"
14908                    "/* block comment */\n"
14909                    "int longbitfield : 6;",
14910                    Alignment));
14911   EXPECT_EQ("int a            : 5;\n"
14912             "int one          : 1;\n"
14913             "// line comment\n"
14914             "int longbitfield : 6;",
14915             format("int a : 5;\n"
14916                    "int one : 1;\n"
14917                    "// line comment\n"
14918                    "int longbitfield : 6;",
14919                    Alignment));
14920 
14921   /* Test across comments and newlines */
14922   EXPECT_EQ("int a            : 5;\n"
14923             "/* block comment */\n"
14924             "\n"
14925             "int longbitfield : 6;",
14926             format("int a : 5;\n"
14927                    "/* block comment */\n"
14928                    "\n"
14929                    "int longbitfield : 6;",
14930                    Alignment));
14931   EXPECT_EQ("int a            : 5;\n"
14932             "int one          : 1;\n"
14933             "\n"
14934             "// line comment\n"
14935             "\n"
14936             "int longbitfield : 6;",
14937             format("int a : 5;\n"
14938                    "int one : 1;\n"
14939                    "\n"
14940                    "// line comment \n"
14941                    "\n"
14942                    "int longbitfield : 6;",
14943                    Alignment));
14944 }
14945 
14946 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
14947   FormatStyle Alignment = getLLVMStyle();
14948   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14949   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
14950 
14951   Alignment.MaxEmptyLinesToKeep = 10;
14952   /* Test alignment across empty lines */
14953   EXPECT_EQ("int a = 5;\n"
14954             "\n"
14955             "int oneTwoThree = 123;",
14956             format("int a       = 5;\n"
14957                    "\n"
14958                    "int oneTwoThree= 123;",
14959                    Alignment));
14960   EXPECT_EQ("int a   = 5;\n"
14961             "int one = 1;\n"
14962             "\n"
14963             "int oneTwoThree = 123;",
14964             format("int a = 5;\n"
14965                    "int one = 1;\n"
14966                    "\n"
14967                    "int oneTwoThree = 123;",
14968                    Alignment));
14969 
14970   /* Test across comments */
14971   EXPECT_EQ("int a           = 5;\n"
14972             "/* block comment */\n"
14973             "int oneTwoThree = 123;",
14974             format("int a = 5;\n"
14975                    "/* block comment */\n"
14976                    "int oneTwoThree=123;",
14977                    Alignment));
14978 
14979   EXPECT_EQ("int a           = 5;\n"
14980             "// line comment\n"
14981             "int oneTwoThree = 123;",
14982             format("int a = 5;\n"
14983                    "// line comment\n"
14984                    "int oneTwoThree=123;",
14985                    Alignment));
14986 
14987   EXPECT_EQ("int a           = 5;\n"
14988             "/*\n"
14989             " * multi-line block comment\n"
14990             " */\n"
14991             "int oneTwoThree = 123;",
14992             format("int a = 5;\n"
14993                    "/*\n"
14994                    " * multi-line block comment\n"
14995                    " */\n"
14996                    "int oneTwoThree=123;",
14997                    Alignment));
14998 
14999   EXPECT_EQ("int a           = 5;\n"
15000             "//\n"
15001             "// multi-line line comment\n"
15002             "//\n"
15003             "int oneTwoThree = 123;",
15004             format("int a = 5;\n"
15005                    "//\n"
15006                    "// multi-line line comment\n"
15007                    "//\n"
15008                    "int oneTwoThree=123;",
15009                    Alignment));
15010 
15011   /* Test across comments and newlines */
15012   EXPECT_EQ("int a = 5;\n"
15013             "\n"
15014             "/* block comment */\n"
15015             "int oneTwoThree = 123;",
15016             format("int a = 5;\n"
15017                    "\n"
15018                    "/* block comment */\n"
15019                    "int oneTwoThree=123;",
15020                    Alignment));
15021 
15022   EXPECT_EQ("int a = 5;\n"
15023             "\n"
15024             "// line comment\n"
15025             "int oneTwoThree = 123;",
15026             format("int a = 5;\n"
15027                    "\n"
15028                    "// line comment\n"
15029                    "int oneTwoThree=123;",
15030                    Alignment));
15031 }
15032 
15033 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15034   FormatStyle Alignment = getLLVMStyle();
15035   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15036   Alignment.AlignConsecutiveAssignments =
15037       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15038   verifyFormat("int a           = 5;\n"
15039                "int oneTwoThree = 123;",
15040                Alignment);
15041   verifyFormat("int a           = method();\n"
15042                "int oneTwoThree = 133;",
15043                Alignment);
15044   verifyFormat("a &= 5;\n"
15045                "bcd *= 5;\n"
15046                "ghtyf += 5;\n"
15047                "dvfvdb -= 5;\n"
15048                "a /= 5;\n"
15049                "vdsvsv %= 5;\n"
15050                "sfdbddfbdfbb ^= 5;\n"
15051                "dvsdsv |= 5;\n"
15052                "int dsvvdvsdvvv = 123;",
15053                Alignment);
15054   verifyFormat("int i = 1, j = 10;\n"
15055                "something = 2000;",
15056                Alignment);
15057   verifyFormat("something = 2000;\n"
15058                "int i = 1, j = 10;\n",
15059                Alignment);
15060   verifyFormat("something = 2000;\n"
15061                "another   = 911;\n"
15062                "int i = 1, j = 10;\n"
15063                "oneMore = 1;\n"
15064                "i       = 2;",
15065                Alignment);
15066   verifyFormat("int a   = 5;\n"
15067                "int one = 1;\n"
15068                "method();\n"
15069                "int oneTwoThree = 123;\n"
15070                "int oneTwo      = 12;",
15071                Alignment);
15072   verifyFormat("int oneTwoThree = 123;\n"
15073                "int oneTwo      = 12;\n"
15074                "method();\n",
15075                Alignment);
15076   verifyFormat("int oneTwoThree = 123; // comment\n"
15077                "int oneTwo      = 12;  // comment",
15078                Alignment);
15079 
15080   // Bug 25167
15081   /* Uncomment when fixed
15082     verifyFormat("#if A\n"
15083                  "#else\n"
15084                  "int aaaaaaaa = 12;\n"
15085                  "#endif\n"
15086                  "#if B\n"
15087                  "#else\n"
15088                  "int a = 12;\n"
15089                  "#endif\n",
15090                  Alignment);
15091     verifyFormat("enum foo {\n"
15092                  "#if A\n"
15093                  "#else\n"
15094                  "  aaaaaaaa = 12;\n"
15095                  "#endif\n"
15096                  "#if B\n"
15097                  "#else\n"
15098                  "  a = 12;\n"
15099                  "#endif\n"
15100                  "};\n",
15101                  Alignment);
15102   */
15103 
15104   Alignment.MaxEmptyLinesToKeep = 10;
15105   /* Test alignment across empty lines */
15106   EXPECT_EQ("int a           = 5;\n"
15107             "\n"
15108             "int oneTwoThree = 123;",
15109             format("int a       = 5;\n"
15110                    "\n"
15111                    "int oneTwoThree= 123;",
15112                    Alignment));
15113   EXPECT_EQ("int a           = 5;\n"
15114             "int one         = 1;\n"
15115             "\n"
15116             "int oneTwoThree = 123;",
15117             format("int a = 5;\n"
15118                    "int one = 1;\n"
15119                    "\n"
15120                    "int oneTwoThree = 123;",
15121                    Alignment));
15122   EXPECT_EQ("int a           = 5;\n"
15123             "int one         = 1;\n"
15124             "\n"
15125             "int oneTwoThree = 123;\n"
15126             "int oneTwo      = 12;",
15127             format("int a = 5;\n"
15128                    "int one = 1;\n"
15129                    "\n"
15130                    "int oneTwoThree = 123;\n"
15131                    "int oneTwo = 12;",
15132                    Alignment));
15133 
15134   /* Test across comments */
15135   EXPECT_EQ("int a           = 5;\n"
15136             "/* block comment */\n"
15137             "int oneTwoThree = 123;",
15138             format("int a = 5;\n"
15139                    "/* block comment */\n"
15140                    "int oneTwoThree=123;",
15141                    Alignment));
15142 
15143   EXPECT_EQ("int a           = 5;\n"
15144             "// line comment\n"
15145             "int oneTwoThree = 123;",
15146             format("int a = 5;\n"
15147                    "// line comment\n"
15148                    "int oneTwoThree=123;",
15149                    Alignment));
15150 
15151   /* Test across comments and newlines */
15152   EXPECT_EQ("int a           = 5;\n"
15153             "\n"
15154             "/* block comment */\n"
15155             "int oneTwoThree = 123;",
15156             format("int a = 5;\n"
15157                    "\n"
15158                    "/* block comment */\n"
15159                    "int oneTwoThree=123;",
15160                    Alignment));
15161 
15162   EXPECT_EQ("int a           = 5;\n"
15163             "\n"
15164             "// line comment\n"
15165             "int oneTwoThree = 123;",
15166             format("int a = 5;\n"
15167                    "\n"
15168                    "// line comment\n"
15169                    "int oneTwoThree=123;",
15170                    Alignment));
15171 
15172   EXPECT_EQ("int a           = 5;\n"
15173             "//\n"
15174             "// multi-line line comment\n"
15175             "//\n"
15176             "int oneTwoThree = 123;",
15177             format("int a = 5;\n"
15178                    "//\n"
15179                    "// multi-line line comment\n"
15180                    "//\n"
15181                    "int oneTwoThree=123;",
15182                    Alignment));
15183 
15184   EXPECT_EQ("int a           = 5;\n"
15185             "/*\n"
15186             " *  multi-line block comment\n"
15187             " */\n"
15188             "int oneTwoThree = 123;",
15189             format("int a = 5;\n"
15190                    "/*\n"
15191                    " *  multi-line block comment\n"
15192                    " */\n"
15193                    "int oneTwoThree=123;",
15194                    Alignment));
15195 
15196   EXPECT_EQ("int a           = 5;\n"
15197             "\n"
15198             "/* block comment */\n"
15199             "\n"
15200             "\n"
15201             "\n"
15202             "int oneTwoThree = 123;",
15203             format("int a = 5;\n"
15204                    "\n"
15205                    "/* block comment */\n"
15206                    "\n"
15207                    "\n"
15208                    "\n"
15209                    "int oneTwoThree=123;",
15210                    Alignment));
15211 
15212   EXPECT_EQ("int a           = 5;\n"
15213             "\n"
15214             "// line comment\n"
15215             "\n"
15216             "\n"
15217             "\n"
15218             "int oneTwoThree = 123;",
15219             format("int a = 5;\n"
15220                    "\n"
15221                    "// line comment\n"
15222                    "\n"
15223                    "\n"
15224                    "\n"
15225                    "int oneTwoThree=123;",
15226                    Alignment));
15227 
15228   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15229   verifyFormat("#define A \\\n"
15230                "  int aaaa       = 12; \\\n"
15231                "  int b          = 23; \\\n"
15232                "  int ccc        = 234; \\\n"
15233                "  int dddddddddd = 2345;",
15234                Alignment);
15235   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15236   verifyFormat("#define A               \\\n"
15237                "  int aaaa       = 12;  \\\n"
15238                "  int b          = 23;  \\\n"
15239                "  int ccc        = 234; \\\n"
15240                "  int dddddddddd = 2345;",
15241                Alignment);
15242   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15243   verifyFormat("#define A                                                      "
15244                "                \\\n"
15245                "  int aaaa       = 12;                                         "
15246                "                \\\n"
15247                "  int b          = 23;                                         "
15248                "                \\\n"
15249                "  int ccc        = 234;                                        "
15250                "                \\\n"
15251                "  int dddddddddd = 2345;",
15252                Alignment);
15253   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15254                "k = 4, int l = 5,\n"
15255                "                  int m = 6) {\n"
15256                "  int j      = 10;\n"
15257                "  otherThing = 1;\n"
15258                "}",
15259                Alignment);
15260   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15261                "  int i   = 1;\n"
15262                "  int j   = 2;\n"
15263                "  int big = 10000;\n"
15264                "}",
15265                Alignment);
15266   verifyFormat("class C {\n"
15267                "public:\n"
15268                "  int i            = 1;\n"
15269                "  virtual void f() = 0;\n"
15270                "};",
15271                Alignment);
15272   verifyFormat("int i = 1;\n"
15273                "if (SomeType t = getSomething()) {\n"
15274                "}\n"
15275                "int j   = 2;\n"
15276                "int big = 10000;",
15277                Alignment);
15278   verifyFormat("int j = 7;\n"
15279                "for (int k = 0; k < N; ++k) {\n"
15280                "}\n"
15281                "int j   = 2;\n"
15282                "int big = 10000;\n"
15283                "}",
15284                Alignment);
15285   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15286   verifyFormat("int i = 1;\n"
15287                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15288                "    = someLooooooooooooooooongFunction();\n"
15289                "int j = 2;",
15290                Alignment);
15291   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15292   verifyFormat("int i = 1;\n"
15293                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15294                "    someLooooooooooooooooongFunction();\n"
15295                "int j = 2;",
15296                Alignment);
15297 
15298   verifyFormat("auto lambda = []() {\n"
15299                "  auto i = 0;\n"
15300                "  return 0;\n"
15301                "};\n"
15302                "int i  = 0;\n"
15303                "auto v = type{\n"
15304                "    i = 1,   //\n"
15305                "    (i = 2), //\n"
15306                "    i = 3    //\n"
15307                "};",
15308                Alignment);
15309 
15310   verifyFormat(
15311       "int i      = 1;\n"
15312       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15313       "                          loooooooooooooooooooooongParameterB);\n"
15314       "int j      = 2;",
15315       Alignment);
15316 
15317   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15318                "          typename B   = very_long_type_name_1,\n"
15319                "          typename T_2 = very_long_type_name_2>\n"
15320                "auto foo() {}\n",
15321                Alignment);
15322   verifyFormat("int a, b = 1;\n"
15323                "int c  = 2;\n"
15324                "int dd = 3;\n",
15325                Alignment);
15326   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15327                "float b[1][] = {{3.f}};\n",
15328                Alignment);
15329   verifyFormat("for (int i = 0; i < 1; i++)\n"
15330                "  int x = 1;\n",
15331                Alignment);
15332   verifyFormat("for (i = 0; i < 1; i++)\n"
15333                "  x = 1;\n"
15334                "y = 1;\n",
15335                Alignment);
15336 
15337   Alignment.ReflowComments = true;
15338   Alignment.ColumnLimit = 50;
15339   EXPECT_EQ("int x   = 0;\n"
15340             "int yy  = 1; /// specificlennospace\n"
15341             "int zzz = 2;\n",
15342             format("int x   = 0;\n"
15343                    "int yy  = 1; ///specificlennospace\n"
15344                    "int zzz = 2;\n",
15345                    Alignment));
15346 }
15347 
15348 TEST_F(FormatTest, AlignConsecutiveAssignments) {
15349   FormatStyle Alignment = getLLVMStyle();
15350   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15351   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15352   verifyFormat("int a = 5;\n"
15353                "int oneTwoThree = 123;",
15354                Alignment);
15355   verifyFormat("int a = 5;\n"
15356                "int oneTwoThree = 123;",
15357                Alignment);
15358 
15359   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15360   verifyFormat("int a           = 5;\n"
15361                "int oneTwoThree = 123;",
15362                Alignment);
15363   verifyFormat("int a           = method();\n"
15364                "int oneTwoThree = 133;",
15365                Alignment);
15366   verifyFormat("a &= 5;\n"
15367                "bcd *= 5;\n"
15368                "ghtyf += 5;\n"
15369                "dvfvdb -= 5;\n"
15370                "a /= 5;\n"
15371                "vdsvsv %= 5;\n"
15372                "sfdbddfbdfbb ^= 5;\n"
15373                "dvsdsv |= 5;\n"
15374                "int dsvvdvsdvvv = 123;",
15375                Alignment);
15376   verifyFormat("int i = 1, j = 10;\n"
15377                "something = 2000;",
15378                Alignment);
15379   verifyFormat("something = 2000;\n"
15380                "int i = 1, j = 10;\n",
15381                Alignment);
15382   verifyFormat("something = 2000;\n"
15383                "another   = 911;\n"
15384                "int i = 1, j = 10;\n"
15385                "oneMore = 1;\n"
15386                "i       = 2;",
15387                Alignment);
15388   verifyFormat("int a   = 5;\n"
15389                "int one = 1;\n"
15390                "method();\n"
15391                "int oneTwoThree = 123;\n"
15392                "int oneTwo      = 12;",
15393                Alignment);
15394   verifyFormat("int oneTwoThree = 123;\n"
15395                "int oneTwo      = 12;\n"
15396                "method();\n",
15397                Alignment);
15398   verifyFormat("int oneTwoThree = 123; // comment\n"
15399                "int oneTwo      = 12;  // comment",
15400                Alignment);
15401 
15402   // Bug 25167
15403   /* Uncomment when fixed
15404     verifyFormat("#if A\n"
15405                  "#else\n"
15406                  "int aaaaaaaa = 12;\n"
15407                  "#endif\n"
15408                  "#if B\n"
15409                  "#else\n"
15410                  "int a = 12;\n"
15411                  "#endif\n",
15412                  Alignment);
15413     verifyFormat("enum foo {\n"
15414                  "#if A\n"
15415                  "#else\n"
15416                  "  aaaaaaaa = 12;\n"
15417                  "#endif\n"
15418                  "#if B\n"
15419                  "#else\n"
15420                  "  a = 12;\n"
15421                  "#endif\n"
15422                  "};\n",
15423                  Alignment);
15424   */
15425 
15426   EXPECT_EQ("int a = 5;\n"
15427             "\n"
15428             "int oneTwoThree = 123;",
15429             format("int a       = 5;\n"
15430                    "\n"
15431                    "int oneTwoThree= 123;",
15432                    Alignment));
15433   EXPECT_EQ("int a   = 5;\n"
15434             "int one = 1;\n"
15435             "\n"
15436             "int oneTwoThree = 123;",
15437             format("int a = 5;\n"
15438                    "int one = 1;\n"
15439                    "\n"
15440                    "int oneTwoThree = 123;",
15441                    Alignment));
15442   EXPECT_EQ("int a   = 5;\n"
15443             "int one = 1;\n"
15444             "\n"
15445             "int oneTwoThree = 123;\n"
15446             "int oneTwo      = 12;",
15447             format("int a = 5;\n"
15448                    "int one = 1;\n"
15449                    "\n"
15450                    "int oneTwoThree = 123;\n"
15451                    "int oneTwo = 12;",
15452                    Alignment));
15453   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15454   verifyFormat("#define A \\\n"
15455                "  int aaaa       = 12; \\\n"
15456                "  int b          = 23; \\\n"
15457                "  int ccc        = 234; \\\n"
15458                "  int dddddddddd = 2345;",
15459                Alignment);
15460   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15461   verifyFormat("#define A               \\\n"
15462                "  int aaaa       = 12;  \\\n"
15463                "  int b          = 23;  \\\n"
15464                "  int ccc        = 234; \\\n"
15465                "  int dddddddddd = 2345;",
15466                Alignment);
15467   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15468   verifyFormat("#define A                                                      "
15469                "                \\\n"
15470                "  int aaaa       = 12;                                         "
15471                "                \\\n"
15472                "  int b          = 23;                                         "
15473                "                \\\n"
15474                "  int ccc        = 234;                                        "
15475                "                \\\n"
15476                "  int dddddddddd = 2345;",
15477                Alignment);
15478   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15479                "k = 4, int l = 5,\n"
15480                "                  int m = 6) {\n"
15481                "  int j      = 10;\n"
15482                "  otherThing = 1;\n"
15483                "}",
15484                Alignment);
15485   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15486                "  int i   = 1;\n"
15487                "  int j   = 2;\n"
15488                "  int big = 10000;\n"
15489                "}",
15490                Alignment);
15491   verifyFormat("class C {\n"
15492                "public:\n"
15493                "  int i            = 1;\n"
15494                "  virtual void f() = 0;\n"
15495                "};",
15496                Alignment);
15497   verifyFormat("int i = 1;\n"
15498                "if (SomeType t = getSomething()) {\n"
15499                "}\n"
15500                "int j   = 2;\n"
15501                "int big = 10000;",
15502                Alignment);
15503   verifyFormat("int j = 7;\n"
15504                "for (int k = 0; k < N; ++k) {\n"
15505                "}\n"
15506                "int j   = 2;\n"
15507                "int big = 10000;\n"
15508                "}",
15509                Alignment);
15510   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15511   verifyFormat("int i = 1;\n"
15512                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15513                "    = someLooooooooooooooooongFunction();\n"
15514                "int j = 2;",
15515                Alignment);
15516   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15517   verifyFormat("int i = 1;\n"
15518                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15519                "    someLooooooooooooooooongFunction();\n"
15520                "int j = 2;",
15521                Alignment);
15522 
15523   verifyFormat("auto lambda = []() {\n"
15524                "  auto i = 0;\n"
15525                "  return 0;\n"
15526                "};\n"
15527                "int i  = 0;\n"
15528                "auto v = type{\n"
15529                "    i = 1,   //\n"
15530                "    (i = 2), //\n"
15531                "    i = 3    //\n"
15532                "};",
15533                Alignment);
15534 
15535   verifyFormat(
15536       "int i      = 1;\n"
15537       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15538       "                          loooooooooooooooooooooongParameterB);\n"
15539       "int j      = 2;",
15540       Alignment);
15541 
15542   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15543                "          typename B   = very_long_type_name_1,\n"
15544                "          typename T_2 = very_long_type_name_2>\n"
15545                "auto foo() {}\n",
15546                Alignment);
15547   verifyFormat("int a, b = 1;\n"
15548                "int c  = 2;\n"
15549                "int dd = 3;\n",
15550                Alignment);
15551   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15552                "float b[1][] = {{3.f}};\n",
15553                Alignment);
15554   verifyFormat("for (int i = 0; i < 1; i++)\n"
15555                "  int x = 1;\n",
15556                Alignment);
15557   verifyFormat("for (i = 0; i < 1; i++)\n"
15558                "  x = 1;\n"
15559                "y = 1;\n",
15560                Alignment);
15561 
15562   Alignment.ReflowComments = true;
15563   Alignment.ColumnLimit = 50;
15564   EXPECT_EQ("int x   = 0;\n"
15565             "int yy  = 1; /// specificlennospace\n"
15566             "int zzz = 2;\n",
15567             format("int x   = 0;\n"
15568                    "int yy  = 1; ///specificlennospace\n"
15569                    "int zzz = 2;\n",
15570                    Alignment));
15571 }
15572 
15573 TEST_F(FormatTest, AlignConsecutiveBitFields) {
15574   FormatStyle Alignment = getLLVMStyle();
15575   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
15576   verifyFormat("int const a     : 5;\n"
15577                "int oneTwoThree : 23;",
15578                Alignment);
15579 
15580   // Initializers are allowed starting with c++2a
15581   verifyFormat("int const a     : 5 = 1;\n"
15582                "int oneTwoThree : 23 = 0;",
15583                Alignment);
15584 
15585   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15586   verifyFormat("int const a           : 5;\n"
15587                "int       oneTwoThree : 23;",
15588                Alignment);
15589 
15590   verifyFormat("int const a           : 5;  // comment\n"
15591                "int       oneTwoThree : 23; // comment",
15592                Alignment);
15593 
15594   verifyFormat("int const a           : 5 = 1;\n"
15595                "int       oneTwoThree : 23 = 0;",
15596                Alignment);
15597 
15598   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15599   verifyFormat("int const a           : 5  = 1;\n"
15600                "int       oneTwoThree : 23 = 0;",
15601                Alignment);
15602   verifyFormat("int const a           : 5  = {1};\n"
15603                "int       oneTwoThree : 23 = 0;",
15604                Alignment);
15605 
15606   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
15607   verifyFormat("int const a          :5;\n"
15608                "int       oneTwoThree:23;",
15609                Alignment);
15610 
15611   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
15612   verifyFormat("int const a           :5;\n"
15613                "int       oneTwoThree :23;",
15614                Alignment);
15615 
15616   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
15617   verifyFormat("int const a          : 5;\n"
15618                "int       oneTwoThree: 23;",
15619                Alignment);
15620 
15621   // Known limitations: ':' is only recognized as a bitfield colon when
15622   // followed by a number.
15623   /*
15624   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
15625                "int a           : 5;",
15626                Alignment);
15627   */
15628 }
15629 
15630 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
15631   FormatStyle Alignment = getLLVMStyle();
15632   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15633   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
15634   Alignment.PointerAlignment = FormatStyle::PAS_Right;
15635   verifyFormat("float const a = 5;\n"
15636                "int oneTwoThree = 123;",
15637                Alignment);
15638   verifyFormat("int a = 5;\n"
15639                "float const oneTwoThree = 123;",
15640                Alignment);
15641 
15642   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15643   verifyFormat("float const a = 5;\n"
15644                "int         oneTwoThree = 123;",
15645                Alignment);
15646   verifyFormat("int         a = method();\n"
15647                "float const oneTwoThree = 133;",
15648                Alignment);
15649   verifyFormat("int i = 1, j = 10;\n"
15650                "something = 2000;",
15651                Alignment);
15652   verifyFormat("something = 2000;\n"
15653                "int i = 1, j = 10;\n",
15654                Alignment);
15655   verifyFormat("float      something = 2000;\n"
15656                "double     another = 911;\n"
15657                "int        i = 1, j = 10;\n"
15658                "const int *oneMore = 1;\n"
15659                "unsigned   i = 2;",
15660                Alignment);
15661   verifyFormat("float a = 5;\n"
15662                "int   one = 1;\n"
15663                "method();\n"
15664                "const double       oneTwoThree = 123;\n"
15665                "const unsigned int oneTwo = 12;",
15666                Alignment);
15667   verifyFormat("int      oneTwoThree{0}; // comment\n"
15668                "unsigned oneTwo;         // comment",
15669                Alignment);
15670   verifyFormat("unsigned int       *a;\n"
15671                "int                *b;\n"
15672                "unsigned int Const *c;\n"
15673                "unsigned int const *d;\n"
15674                "unsigned int Const &e;\n"
15675                "unsigned int const &f;",
15676                Alignment);
15677   verifyFormat("Const unsigned int *c;\n"
15678                "const unsigned int *d;\n"
15679                "Const unsigned int &e;\n"
15680                "const unsigned int &f;\n"
15681                "const unsigned      g;\n"
15682                "Const unsigned      h;",
15683                Alignment);
15684   EXPECT_EQ("float const a = 5;\n"
15685             "\n"
15686             "int oneTwoThree = 123;",
15687             format("float const   a = 5;\n"
15688                    "\n"
15689                    "int           oneTwoThree= 123;",
15690                    Alignment));
15691   EXPECT_EQ("float a = 5;\n"
15692             "int   one = 1;\n"
15693             "\n"
15694             "unsigned oneTwoThree = 123;",
15695             format("float    a = 5;\n"
15696                    "int      one = 1;\n"
15697                    "\n"
15698                    "unsigned oneTwoThree = 123;",
15699                    Alignment));
15700   EXPECT_EQ("float a = 5;\n"
15701             "int   one = 1;\n"
15702             "\n"
15703             "unsigned oneTwoThree = 123;\n"
15704             "int      oneTwo = 12;",
15705             format("float    a = 5;\n"
15706                    "int one = 1;\n"
15707                    "\n"
15708                    "unsigned oneTwoThree = 123;\n"
15709                    "int oneTwo = 12;",
15710                    Alignment));
15711   // Function prototype alignment
15712   verifyFormat("int    a();\n"
15713                "double b();",
15714                Alignment);
15715   verifyFormat("int    a(int x);\n"
15716                "double b();",
15717                Alignment);
15718   unsigned OldColumnLimit = Alignment.ColumnLimit;
15719   // We need to set ColumnLimit to zero, in order to stress nested alignments,
15720   // otherwise the function parameters will be re-flowed onto a single line.
15721   Alignment.ColumnLimit = 0;
15722   EXPECT_EQ("int    a(int   x,\n"
15723             "         float y);\n"
15724             "double b(int    x,\n"
15725             "         double y);",
15726             format("int a(int x,\n"
15727                    " float y);\n"
15728                    "double b(int x,\n"
15729                    " double y);",
15730                    Alignment));
15731   // This ensures that function parameters of function declarations are
15732   // correctly indented when their owning functions are indented.
15733   // The failure case here is for 'double y' to not be indented enough.
15734   EXPECT_EQ("double a(int x);\n"
15735             "int    b(int    y,\n"
15736             "         double z);",
15737             format("double a(int x);\n"
15738                    "int b(int y,\n"
15739                    " double z);",
15740                    Alignment));
15741   // Set ColumnLimit low so that we induce wrapping immediately after
15742   // the function name and opening paren.
15743   Alignment.ColumnLimit = 13;
15744   verifyFormat("int function(\n"
15745                "    int  x,\n"
15746                "    bool y);",
15747                Alignment);
15748   Alignment.ColumnLimit = OldColumnLimit;
15749   // Ensure function pointers don't screw up recursive alignment
15750   verifyFormat("int    a(int x, void (*fp)(int y));\n"
15751                "double b();",
15752                Alignment);
15753   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15754   // Ensure recursive alignment is broken by function braces, so that the
15755   // "a = 1" does not align with subsequent assignments inside the function
15756   // body.
15757   verifyFormat("int func(int a = 1) {\n"
15758                "  int b  = 2;\n"
15759                "  int cc = 3;\n"
15760                "}",
15761                Alignment);
15762   verifyFormat("float      something = 2000;\n"
15763                "double     another   = 911;\n"
15764                "int        i = 1, j = 10;\n"
15765                "const int *oneMore = 1;\n"
15766                "unsigned   i       = 2;",
15767                Alignment);
15768   verifyFormat("int      oneTwoThree = {0}; // comment\n"
15769                "unsigned oneTwo      = 0;   // comment",
15770                Alignment);
15771   // Make sure that scope is correctly tracked, in the absence of braces
15772   verifyFormat("for (int i = 0; i < n; i++)\n"
15773                "  j = i;\n"
15774                "double x = 1;\n",
15775                Alignment);
15776   verifyFormat("if (int i = 0)\n"
15777                "  j = i;\n"
15778                "double x = 1;\n",
15779                Alignment);
15780   // Ensure operator[] and operator() are comprehended
15781   verifyFormat("struct test {\n"
15782                "  long long int foo();\n"
15783                "  int           operator[](int a);\n"
15784                "  double        bar();\n"
15785                "};\n",
15786                Alignment);
15787   verifyFormat("struct test {\n"
15788                "  long long int foo();\n"
15789                "  int           operator()(int a);\n"
15790                "  double        bar();\n"
15791                "};\n",
15792                Alignment);
15793 
15794   // PAS_Right
15795   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15796             "  int const i   = 1;\n"
15797             "  int      *j   = 2;\n"
15798             "  int       big = 10000;\n"
15799             "\n"
15800             "  unsigned oneTwoThree = 123;\n"
15801             "  int      oneTwo      = 12;\n"
15802             "  method();\n"
15803             "  float k  = 2;\n"
15804             "  int   ll = 10000;\n"
15805             "}",
15806             format("void SomeFunction(int parameter= 0) {\n"
15807                    " int const  i= 1;\n"
15808                    "  int *j=2;\n"
15809                    " int big  =  10000;\n"
15810                    "\n"
15811                    "unsigned oneTwoThree  =123;\n"
15812                    "int oneTwo = 12;\n"
15813                    "  method();\n"
15814                    "float k= 2;\n"
15815                    "int ll=10000;\n"
15816                    "}",
15817                    Alignment));
15818   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15819             "  int const i   = 1;\n"
15820             "  int     **j   = 2, ***k;\n"
15821             "  int      &k   = i;\n"
15822             "  int     &&l   = i + j;\n"
15823             "  int       big = 10000;\n"
15824             "\n"
15825             "  unsigned oneTwoThree = 123;\n"
15826             "  int      oneTwo      = 12;\n"
15827             "  method();\n"
15828             "  float k  = 2;\n"
15829             "  int   ll = 10000;\n"
15830             "}",
15831             format("void SomeFunction(int parameter= 0) {\n"
15832                    " int const  i= 1;\n"
15833                    "  int **j=2,***k;\n"
15834                    "int &k=i;\n"
15835                    "int &&l=i+j;\n"
15836                    " int big  =  10000;\n"
15837                    "\n"
15838                    "unsigned oneTwoThree  =123;\n"
15839                    "int oneTwo = 12;\n"
15840                    "  method();\n"
15841                    "float k= 2;\n"
15842                    "int ll=10000;\n"
15843                    "}",
15844                    Alignment));
15845   // variables are aligned at their name, pointers are at the right most
15846   // position
15847   verifyFormat("int   *a;\n"
15848                "int  **b;\n"
15849                "int ***c;\n"
15850                "int    foobar;\n",
15851                Alignment);
15852 
15853   // PAS_Left
15854   FormatStyle AlignmentLeft = Alignment;
15855   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
15856   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15857             "  int const i   = 1;\n"
15858             "  int*      j   = 2;\n"
15859             "  int       big = 10000;\n"
15860             "\n"
15861             "  unsigned oneTwoThree = 123;\n"
15862             "  int      oneTwo      = 12;\n"
15863             "  method();\n"
15864             "  float k  = 2;\n"
15865             "  int   ll = 10000;\n"
15866             "}",
15867             format("void SomeFunction(int parameter= 0) {\n"
15868                    " int const  i= 1;\n"
15869                    "  int *j=2;\n"
15870                    " int big  =  10000;\n"
15871                    "\n"
15872                    "unsigned oneTwoThree  =123;\n"
15873                    "int oneTwo = 12;\n"
15874                    "  method();\n"
15875                    "float k= 2;\n"
15876                    "int ll=10000;\n"
15877                    "}",
15878                    AlignmentLeft));
15879   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15880             "  int const i   = 1;\n"
15881             "  int**     j   = 2;\n"
15882             "  int&      k   = i;\n"
15883             "  int&&     l   = i + j;\n"
15884             "  int       big = 10000;\n"
15885             "\n"
15886             "  unsigned oneTwoThree = 123;\n"
15887             "  int      oneTwo      = 12;\n"
15888             "  method();\n"
15889             "  float k  = 2;\n"
15890             "  int   ll = 10000;\n"
15891             "}",
15892             format("void SomeFunction(int parameter= 0) {\n"
15893                    " int const  i= 1;\n"
15894                    "  int **j=2;\n"
15895                    "int &k=i;\n"
15896                    "int &&l=i+j;\n"
15897                    " int big  =  10000;\n"
15898                    "\n"
15899                    "unsigned oneTwoThree  =123;\n"
15900                    "int oneTwo = 12;\n"
15901                    "  method();\n"
15902                    "float k= 2;\n"
15903                    "int ll=10000;\n"
15904                    "}",
15905                    AlignmentLeft));
15906   // variables are aligned at their name, pointers are at the left most position
15907   verifyFormat("int*   a;\n"
15908                "int**  b;\n"
15909                "int*** c;\n"
15910                "int    foobar;\n",
15911                AlignmentLeft);
15912 
15913   // PAS_Middle
15914   FormatStyle AlignmentMiddle = Alignment;
15915   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
15916   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15917             "  int const i   = 1;\n"
15918             "  int *     j   = 2;\n"
15919             "  int       big = 10000;\n"
15920             "\n"
15921             "  unsigned oneTwoThree = 123;\n"
15922             "  int      oneTwo      = 12;\n"
15923             "  method();\n"
15924             "  float k  = 2;\n"
15925             "  int   ll = 10000;\n"
15926             "}",
15927             format("void SomeFunction(int parameter= 0) {\n"
15928                    " int const  i= 1;\n"
15929                    "  int *j=2;\n"
15930                    " int big  =  10000;\n"
15931                    "\n"
15932                    "unsigned oneTwoThree  =123;\n"
15933                    "int oneTwo = 12;\n"
15934                    "  method();\n"
15935                    "float k= 2;\n"
15936                    "int ll=10000;\n"
15937                    "}",
15938                    AlignmentMiddle));
15939   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15940             "  int const i   = 1;\n"
15941             "  int **    j   = 2, ***k;\n"
15942             "  int &     k   = i;\n"
15943             "  int &&    l   = i + j;\n"
15944             "  int       big = 10000;\n"
15945             "\n"
15946             "  unsigned oneTwoThree = 123;\n"
15947             "  int      oneTwo      = 12;\n"
15948             "  method();\n"
15949             "  float k  = 2;\n"
15950             "  int   ll = 10000;\n"
15951             "}",
15952             format("void SomeFunction(int parameter= 0) {\n"
15953                    " int const  i= 1;\n"
15954                    "  int **j=2,***k;\n"
15955                    "int &k=i;\n"
15956                    "int &&l=i+j;\n"
15957                    " int big  =  10000;\n"
15958                    "\n"
15959                    "unsigned oneTwoThree  =123;\n"
15960                    "int oneTwo = 12;\n"
15961                    "  method();\n"
15962                    "float k= 2;\n"
15963                    "int ll=10000;\n"
15964                    "}",
15965                    AlignmentMiddle));
15966   // variables are aligned at their name, pointers are in the middle
15967   verifyFormat("int *   a;\n"
15968                "int *   b;\n"
15969                "int *** c;\n"
15970                "int     foobar;\n",
15971                AlignmentMiddle);
15972 
15973   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15974   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15975   verifyFormat("#define A \\\n"
15976                "  int       aaaa = 12; \\\n"
15977                "  float     b = 23; \\\n"
15978                "  const int ccc = 234; \\\n"
15979                "  unsigned  dddddddddd = 2345;",
15980                Alignment);
15981   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15982   verifyFormat("#define A              \\\n"
15983                "  int       aaaa = 12; \\\n"
15984                "  float     b = 23;    \\\n"
15985                "  const int ccc = 234; \\\n"
15986                "  unsigned  dddddddddd = 2345;",
15987                Alignment);
15988   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15989   Alignment.ColumnLimit = 30;
15990   verifyFormat("#define A                    \\\n"
15991                "  int       aaaa = 12;       \\\n"
15992                "  float     b = 23;          \\\n"
15993                "  const int ccc = 234;       \\\n"
15994                "  int       dddddddddd = 2345;",
15995                Alignment);
15996   Alignment.ColumnLimit = 80;
15997   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15998                "k = 4, int l = 5,\n"
15999                "                  int m = 6) {\n"
16000                "  const int j = 10;\n"
16001                "  otherThing = 1;\n"
16002                "}",
16003                Alignment);
16004   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16005                "  int const i = 1;\n"
16006                "  int      *j = 2;\n"
16007                "  int       big = 10000;\n"
16008                "}",
16009                Alignment);
16010   verifyFormat("class C {\n"
16011                "public:\n"
16012                "  int          i = 1;\n"
16013                "  virtual void f() = 0;\n"
16014                "};",
16015                Alignment);
16016   verifyFormat("float i = 1;\n"
16017                "if (SomeType t = getSomething()) {\n"
16018                "}\n"
16019                "const unsigned j = 2;\n"
16020                "int            big = 10000;",
16021                Alignment);
16022   verifyFormat("float j = 7;\n"
16023                "for (int k = 0; k < N; ++k) {\n"
16024                "}\n"
16025                "unsigned j = 2;\n"
16026                "int      big = 10000;\n"
16027                "}",
16028                Alignment);
16029   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16030   verifyFormat("float              i = 1;\n"
16031                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16032                "    = someLooooooooooooooooongFunction();\n"
16033                "int j = 2;",
16034                Alignment);
16035   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16036   verifyFormat("int                i = 1;\n"
16037                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16038                "    someLooooooooooooooooongFunction();\n"
16039                "int j = 2;",
16040                Alignment);
16041 
16042   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16043   verifyFormat("auto lambda = []() {\n"
16044                "  auto  ii = 0;\n"
16045                "  float j  = 0;\n"
16046                "  return 0;\n"
16047                "};\n"
16048                "int   i  = 0;\n"
16049                "float i2 = 0;\n"
16050                "auto  v  = type{\n"
16051                "    i = 1,   //\n"
16052                "    (i = 2), //\n"
16053                "    i = 3    //\n"
16054                "};",
16055                Alignment);
16056   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16057 
16058   verifyFormat(
16059       "int      i = 1;\n"
16060       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16061       "                          loooooooooooooooooooooongParameterB);\n"
16062       "int      j = 2;",
16063       Alignment);
16064 
16065   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
16066   // We expect declarations and assignments to align, as long as it doesn't
16067   // exceed the column limit, starting a new alignment sequence whenever it
16068   // happens.
16069   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16070   Alignment.ColumnLimit = 30;
16071   verifyFormat("float    ii              = 1;\n"
16072                "unsigned j               = 2;\n"
16073                "int someVerylongVariable = 1;\n"
16074                "AnotherLongType  ll = 123456;\n"
16075                "VeryVeryLongType k  = 2;\n"
16076                "int              myvar = 1;",
16077                Alignment);
16078   Alignment.ColumnLimit = 80;
16079   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16080 
16081   verifyFormat(
16082       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
16083       "          typename LongType, typename B>\n"
16084       "auto foo() {}\n",
16085       Alignment);
16086   verifyFormat("float a, b = 1;\n"
16087                "int   c = 2;\n"
16088                "int   dd = 3;\n",
16089                Alignment);
16090   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
16091                "float b[1][] = {{3.f}};\n",
16092                Alignment);
16093   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16094   verifyFormat("float a, b = 1;\n"
16095                "int   c  = 2;\n"
16096                "int   dd = 3;\n",
16097                Alignment);
16098   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
16099                "float b[1][] = {{3.f}};\n",
16100                Alignment);
16101   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16102 
16103   Alignment.ColumnLimit = 30;
16104   Alignment.BinPackParameters = false;
16105   verifyFormat("void foo(float     a,\n"
16106                "         float     b,\n"
16107                "         int       c,\n"
16108                "         uint32_t *d) {\n"
16109                "  int   *e = 0;\n"
16110                "  float  f = 0;\n"
16111                "  double g = 0;\n"
16112                "}\n"
16113                "void bar(ino_t     a,\n"
16114                "         int       b,\n"
16115                "         uint32_t *c,\n"
16116                "         bool      d) {}\n",
16117                Alignment);
16118   Alignment.BinPackParameters = true;
16119   Alignment.ColumnLimit = 80;
16120 
16121   // Bug 33507
16122   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16123   verifyFormat(
16124       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
16125       "  static const Version verVs2017;\n"
16126       "  return true;\n"
16127       "});\n",
16128       Alignment);
16129   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16130 
16131   // See llvm.org/PR35641
16132   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16133   verifyFormat("int func() { //\n"
16134                "  int      b;\n"
16135                "  unsigned c;\n"
16136                "}",
16137                Alignment);
16138 
16139   // See PR37175
16140   FormatStyle Style = getMozillaStyle();
16141   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16142   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
16143             "foo(int a);",
16144             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
16145 
16146   Alignment.PointerAlignment = FormatStyle::PAS_Left;
16147   verifyFormat("unsigned int*       a;\n"
16148                "int*                b;\n"
16149                "unsigned int Const* c;\n"
16150                "unsigned int const* d;\n"
16151                "unsigned int Const& e;\n"
16152                "unsigned int const& f;",
16153                Alignment);
16154   verifyFormat("Const unsigned int* c;\n"
16155                "const unsigned int* d;\n"
16156                "Const unsigned int& e;\n"
16157                "const unsigned int& f;\n"
16158                "const unsigned      g;\n"
16159                "Const unsigned      h;",
16160                Alignment);
16161 
16162   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16163   verifyFormat("unsigned int *       a;\n"
16164                "int *                b;\n"
16165                "unsigned int Const * c;\n"
16166                "unsigned int const * d;\n"
16167                "unsigned int Const & e;\n"
16168                "unsigned int const & f;",
16169                Alignment);
16170   verifyFormat("Const unsigned int * c;\n"
16171                "const unsigned int * d;\n"
16172                "Const unsigned int & e;\n"
16173                "const unsigned int & f;\n"
16174                "const unsigned       g;\n"
16175                "Const unsigned       h;",
16176                Alignment);
16177 }
16178 
16179 TEST_F(FormatTest, AlignWithLineBreaks) {
16180   auto Style = getLLVMStyleWithColumns(120);
16181 
16182   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
16183   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16184   verifyFormat("void foo() {\n"
16185                "  int myVar = 5;\n"
16186                "  double x = 3.14;\n"
16187                "  auto str = \"Hello \"\n"
16188                "             \"World\";\n"
16189                "  auto s = \"Hello \"\n"
16190                "           \"Again\";\n"
16191                "}",
16192                Style);
16193 
16194   // clang-format off
16195   verifyFormat("void foo() {\n"
16196                "  const int capacityBefore = Entries.capacity();\n"
16197                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16198                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16199                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16200                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16201                "}",
16202                Style);
16203   // clang-format on
16204 
16205   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16206   verifyFormat("void foo() {\n"
16207                "  int myVar = 5;\n"
16208                "  double x  = 3.14;\n"
16209                "  auto str  = \"Hello \"\n"
16210                "              \"World\";\n"
16211                "  auto s    = \"Hello \"\n"
16212                "              \"Again\";\n"
16213                "}",
16214                Style);
16215 
16216   // clang-format off
16217   verifyFormat("void foo() {\n"
16218                "  const int capacityBefore = Entries.capacity();\n"
16219                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16220                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16221                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16222                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16223                "}",
16224                Style);
16225   // clang-format on
16226 
16227   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16228   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16229   verifyFormat("void foo() {\n"
16230                "  int    myVar = 5;\n"
16231                "  double x = 3.14;\n"
16232                "  auto   str = \"Hello \"\n"
16233                "               \"World\";\n"
16234                "  auto   s = \"Hello \"\n"
16235                "             \"Again\";\n"
16236                "}",
16237                Style);
16238 
16239   // clang-format off
16240   verifyFormat("void foo() {\n"
16241                "  const int  capacityBefore = Entries.capacity();\n"
16242                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16243                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16244                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16245                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16246                "}",
16247                Style);
16248   // clang-format on
16249 
16250   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16251   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16252 
16253   verifyFormat("void foo() {\n"
16254                "  int    myVar = 5;\n"
16255                "  double x     = 3.14;\n"
16256                "  auto   str   = \"Hello \"\n"
16257                "                 \"World\";\n"
16258                "  auto   s     = \"Hello \"\n"
16259                "                 \"Again\";\n"
16260                "}",
16261                Style);
16262 
16263   // clang-format off
16264   verifyFormat("void foo() {\n"
16265                "  const int  capacityBefore = Entries.capacity();\n"
16266                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16267                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16268                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16269                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16270                "}",
16271                Style);
16272   // clang-format on
16273 }
16274 
16275 TEST_F(FormatTest, AlignWithInitializerPeriods) {
16276   auto Style = getLLVMStyleWithColumns(60);
16277 
16278   verifyFormat("void foo1(void) {\n"
16279                "  BYTE p[1] = 1;\n"
16280                "  A B = {.one_foooooooooooooooo = 2,\n"
16281                "         .two_fooooooooooooo = 3,\n"
16282                "         .three_fooooooooooooo = 4};\n"
16283                "  BYTE payload = 2;\n"
16284                "}",
16285                Style);
16286 
16287   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16288   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16289   verifyFormat("void foo2(void) {\n"
16290                "  BYTE p[1]    = 1;\n"
16291                "  A B          = {.one_foooooooooooooooo = 2,\n"
16292                "                  .two_fooooooooooooo    = 3,\n"
16293                "                  .three_fooooooooooooo  = 4};\n"
16294                "  BYTE payload = 2;\n"
16295                "}",
16296                Style);
16297 
16298   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16299   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16300   verifyFormat("void foo3(void) {\n"
16301                "  BYTE p[1] = 1;\n"
16302                "  A    B = {.one_foooooooooooooooo = 2,\n"
16303                "            .two_fooooooooooooo = 3,\n"
16304                "            .three_fooooooooooooo = 4};\n"
16305                "  BYTE payload = 2;\n"
16306                "}",
16307                Style);
16308 
16309   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16310   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16311   verifyFormat("void foo4(void) {\n"
16312                "  BYTE p[1]    = 1;\n"
16313                "  A    B       = {.one_foooooooooooooooo = 2,\n"
16314                "                  .two_fooooooooooooo    = 3,\n"
16315                "                  .three_fooooooooooooo  = 4};\n"
16316                "  BYTE payload = 2;\n"
16317                "}",
16318                Style);
16319 }
16320 
16321 TEST_F(FormatTest, LinuxBraceBreaking) {
16322   FormatStyle LinuxBraceStyle = getLLVMStyle();
16323   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
16324   verifyFormat("namespace a\n"
16325                "{\n"
16326                "class A\n"
16327                "{\n"
16328                "  void f()\n"
16329                "  {\n"
16330                "    if (true) {\n"
16331                "      a();\n"
16332                "      b();\n"
16333                "    } else {\n"
16334                "      a();\n"
16335                "    }\n"
16336                "  }\n"
16337                "  void g() { return; }\n"
16338                "};\n"
16339                "struct B {\n"
16340                "  int x;\n"
16341                "};\n"
16342                "} // namespace a\n",
16343                LinuxBraceStyle);
16344   verifyFormat("enum X {\n"
16345                "  Y = 0,\n"
16346                "}\n",
16347                LinuxBraceStyle);
16348   verifyFormat("struct S {\n"
16349                "  int Type;\n"
16350                "  union {\n"
16351                "    int x;\n"
16352                "    double y;\n"
16353                "  } Value;\n"
16354                "  class C\n"
16355                "  {\n"
16356                "    MyFavoriteType Value;\n"
16357                "  } Class;\n"
16358                "}\n",
16359                LinuxBraceStyle);
16360 }
16361 
16362 TEST_F(FormatTest, MozillaBraceBreaking) {
16363   FormatStyle MozillaBraceStyle = getLLVMStyle();
16364   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
16365   MozillaBraceStyle.FixNamespaceComments = false;
16366   verifyFormat("namespace a {\n"
16367                "class A\n"
16368                "{\n"
16369                "  void f()\n"
16370                "  {\n"
16371                "    if (true) {\n"
16372                "      a();\n"
16373                "      b();\n"
16374                "    }\n"
16375                "  }\n"
16376                "  void g() { return; }\n"
16377                "};\n"
16378                "enum E\n"
16379                "{\n"
16380                "  A,\n"
16381                "  // foo\n"
16382                "  B,\n"
16383                "  C\n"
16384                "};\n"
16385                "struct B\n"
16386                "{\n"
16387                "  int x;\n"
16388                "};\n"
16389                "}\n",
16390                MozillaBraceStyle);
16391   verifyFormat("struct S\n"
16392                "{\n"
16393                "  int Type;\n"
16394                "  union\n"
16395                "  {\n"
16396                "    int x;\n"
16397                "    double y;\n"
16398                "  } Value;\n"
16399                "  class C\n"
16400                "  {\n"
16401                "    MyFavoriteType Value;\n"
16402                "  } Class;\n"
16403                "}\n",
16404                MozillaBraceStyle);
16405 }
16406 
16407 TEST_F(FormatTest, StroustrupBraceBreaking) {
16408   FormatStyle StroustrupBraceStyle = getLLVMStyle();
16409   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
16410   verifyFormat("namespace a {\n"
16411                "class A {\n"
16412                "  void f()\n"
16413                "  {\n"
16414                "    if (true) {\n"
16415                "      a();\n"
16416                "      b();\n"
16417                "    }\n"
16418                "  }\n"
16419                "  void g() { return; }\n"
16420                "};\n"
16421                "struct B {\n"
16422                "  int x;\n"
16423                "};\n"
16424                "} // namespace a\n",
16425                StroustrupBraceStyle);
16426 
16427   verifyFormat("void foo()\n"
16428                "{\n"
16429                "  if (a) {\n"
16430                "    a();\n"
16431                "  }\n"
16432                "  else {\n"
16433                "    b();\n"
16434                "  }\n"
16435                "}\n",
16436                StroustrupBraceStyle);
16437 
16438   verifyFormat("#ifdef _DEBUG\n"
16439                "int foo(int i = 0)\n"
16440                "#else\n"
16441                "int foo(int i = 5)\n"
16442                "#endif\n"
16443                "{\n"
16444                "  return i;\n"
16445                "}",
16446                StroustrupBraceStyle);
16447 
16448   verifyFormat("void foo() {}\n"
16449                "void bar()\n"
16450                "#ifdef _DEBUG\n"
16451                "{\n"
16452                "  foo();\n"
16453                "}\n"
16454                "#else\n"
16455                "{\n"
16456                "}\n"
16457                "#endif",
16458                StroustrupBraceStyle);
16459 
16460   verifyFormat("void foobar() { int i = 5; }\n"
16461                "#ifdef _DEBUG\n"
16462                "void bar() {}\n"
16463                "#else\n"
16464                "void bar() { foobar(); }\n"
16465                "#endif",
16466                StroustrupBraceStyle);
16467 }
16468 
16469 TEST_F(FormatTest, AllmanBraceBreaking) {
16470   FormatStyle AllmanBraceStyle = getLLVMStyle();
16471   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
16472 
16473   EXPECT_EQ("namespace a\n"
16474             "{\n"
16475             "void f();\n"
16476             "void g();\n"
16477             "} // namespace a\n",
16478             format("namespace a\n"
16479                    "{\n"
16480                    "void f();\n"
16481                    "void g();\n"
16482                    "}\n",
16483                    AllmanBraceStyle));
16484 
16485   verifyFormat("namespace a\n"
16486                "{\n"
16487                "class A\n"
16488                "{\n"
16489                "  void f()\n"
16490                "  {\n"
16491                "    if (true)\n"
16492                "    {\n"
16493                "      a();\n"
16494                "      b();\n"
16495                "    }\n"
16496                "  }\n"
16497                "  void g() { return; }\n"
16498                "};\n"
16499                "struct B\n"
16500                "{\n"
16501                "  int x;\n"
16502                "};\n"
16503                "union C\n"
16504                "{\n"
16505                "};\n"
16506                "} // namespace a",
16507                AllmanBraceStyle);
16508 
16509   verifyFormat("void f()\n"
16510                "{\n"
16511                "  if (true)\n"
16512                "  {\n"
16513                "    a();\n"
16514                "  }\n"
16515                "  else if (false)\n"
16516                "  {\n"
16517                "    b();\n"
16518                "  }\n"
16519                "  else\n"
16520                "  {\n"
16521                "    c();\n"
16522                "  }\n"
16523                "}\n",
16524                AllmanBraceStyle);
16525 
16526   verifyFormat("void f()\n"
16527                "{\n"
16528                "  for (int i = 0; i < 10; ++i)\n"
16529                "  {\n"
16530                "    a();\n"
16531                "  }\n"
16532                "  while (false)\n"
16533                "  {\n"
16534                "    b();\n"
16535                "  }\n"
16536                "  do\n"
16537                "  {\n"
16538                "    c();\n"
16539                "  } while (false)\n"
16540                "}\n",
16541                AllmanBraceStyle);
16542 
16543   verifyFormat("void f(int a)\n"
16544                "{\n"
16545                "  switch (a)\n"
16546                "  {\n"
16547                "  case 0:\n"
16548                "    break;\n"
16549                "  case 1:\n"
16550                "  {\n"
16551                "    break;\n"
16552                "  }\n"
16553                "  case 2:\n"
16554                "  {\n"
16555                "  }\n"
16556                "  break;\n"
16557                "  default:\n"
16558                "    break;\n"
16559                "  }\n"
16560                "}\n",
16561                AllmanBraceStyle);
16562 
16563   verifyFormat("enum X\n"
16564                "{\n"
16565                "  Y = 0,\n"
16566                "}\n",
16567                AllmanBraceStyle);
16568   verifyFormat("enum X\n"
16569                "{\n"
16570                "  Y = 0\n"
16571                "}\n",
16572                AllmanBraceStyle);
16573 
16574   verifyFormat("@interface BSApplicationController ()\n"
16575                "{\n"
16576                "@private\n"
16577                "  id _extraIvar;\n"
16578                "}\n"
16579                "@end\n",
16580                AllmanBraceStyle);
16581 
16582   verifyFormat("#ifdef _DEBUG\n"
16583                "int foo(int i = 0)\n"
16584                "#else\n"
16585                "int foo(int i = 5)\n"
16586                "#endif\n"
16587                "{\n"
16588                "  return i;\n"
16589                "}",
16590                AllmanBraceStyle);
16591 
16592   verifyFormat("void foo() {}\n"
16593                "void bar()\n"
16594                "#ifdef _DEBUG\n"
16595                "{\n"
16596                "  foo();\n"
16597                "}\n"
16598                "#else\n"
16599                "{\n"
16600                "}\n"
16601                "#endif",
16602                AllmanBraceStyle);
16603 
16604   verifyFormat("void foobar() { int i = 5; }\n"
16605                "#ifdef _DEBUG\n"
16606                "void bar() {}\n"
16607                "#else\n"
16608                "void bar() { foobar(); }\n"
16609                "#endif",
16610                AllmanBraceStyle);
16611 
16612   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
16613             FormatStyle::SLS_All);
16614 
16615   verifyFormat("[](int i) { return i + 2; };\n"
16616                "[](int i, int j)\n"
16617                "{\n"
16618                "  auto x = i + j;\n"
16619                "  auto y = i * j;\n"
16620                "  return x ^ y;\n"
16621                "};\n"
16622                "void foo()\n"
16623                "{\n"
16624                "  auto shortLambda = [](int i) { return i + 2; };\n"
16625                "  auto longLambda = [](int i, int j)\n"
16626                "  {\n"
16627                "    auto x = i + j;\n"
16628                "    auto y = i * j;\n"
16629                "    return x ^ y;\n"
16630                "  };\n"
16631                "}",
16632                AllmanBraceStyle);
16633 
16634   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
16635 
16636   verifyFormat("[](int i)\n"
16637                "{\n"
16638                "  return i + 2;\n"
16639                "};\n"
16640                "[](int i, int j)\n"
16641                "{\n"
16642                "  auto x = i + j;\n"
16643                "  auto y = i * j;\n"
16644                "  return x ^ y;\n"
16645                "};\n"
16646                "void foo()\n"
16647                "{\n"
16648                "  auto shortLambda = [](int i)\n"
16649                "  {\n"
16650                "    return i + 2;\n"
16651                "  };\n"
16652                "  auto longLambda = [](int i, int j)\n"
16653                "  {\n"
16654                "    auto x = i + j;\n"
16655                "    auto y = i * j;\n"
16656                "    return x ^ y;\n"
16657                "  };\n"
16658                "}",
16659                AllmanBraceStyle);
16660 
16661   // Reset
16662   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
16663 
16664   // This shouldn't affect ObjC blocks..
16665   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
16666                "  // ...\n"
16667                "  int i;\n"
16668                "}];",
16669                AllmanBraceStyle);
16670   verifyFormat("void (^block)(void) = ^{\n"
16671                "  // ...\n"
16672                "  int i;\n"
16673                "};",
16674                AllmanBraceStyle);
16675   // .. or dict literals.
16676   verifyFormat("void f()\n"
16677                "{\n"
16678                "  // ...\n"
16679                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
16680                "}",
16681                AllmanBraceStyle);
16682   verifyFormat("void f()\n"
16683                "{\n"
16684                "  // ...\n"
16685                "  [object someMethod:@{a : @\"b\"}];\n"
16686                "}",
16687                AllmanBraceStyle);
16688   verifyFormat("int f()\n"
16689                "{ // comment\n"
16690                "  return 42;\n"
16691                "}",
16692                AllmanBraceStyle);
16693 
16694   AllmanBraceStyle.ColumnLimit = 19;
16695   verifyFormat("void f() { int i; }", AllmanBraceStyle);
16696   AllmanBraceStyle.ColumnLimit = 18;
16697   verifyFormat("void f()\n"
16698                "{\n"
16699                "  int i;\n"
16700                "}",
16701                AllmanBraceStyle);
16702   AllmanBraceStyle.ColumnLimit = 80;
16703 
16704   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
16705   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
16706       FormatStyle::SIS_WithoutElse;
16707   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
16708   verifyFormat("void f(bool b)\n"
16709                "{\n"
16710                "  if (b)\n"
16711                "  {\n"
16712                "    return;\n"
16713                "  }\n"
16714                "}\n",
16715                BreakBeforeBraceShortIfs);
16716   verifyFormat("void f(bool b)\n"
16717                "{\n"
16718                "  if constexpr (b)\n"
16719                "  {\n"
16720                "    return;\n"
16721                "  }\n"
16722                "}\n",
16723                BreakBeforeBraceShortIfs);
16724   verifyFormat("void f(bool b)\n"
16725                "{\n"
16726                "  if CONSTEXPR (b)\n"
16727                "  {\n"
16728                "    return;\n"
16729                "  }\n"
16730                "}\n",
16731                BreakBeforeBraceShortIfs);
16732   verifyFormat("void f(bool b)\n"
16733                "{\n"
16734                "  if (b) return;\n"
16735                "}\n",
16736                BreakBeforeBraceShortIfs);
16737   verifyFormat("void f(bool b)\n"
16738                "{\n"
16739                "  if constexpr (b) return;\n"
16740                "}\n",
16741                BreakBeforeBraceShortIfs);
16742   verifyFormat("void f(bool b)\n"
16743                "{\n"
16744                "  if CONSTEXPR (b) return;\n"
16745                "}\n",
16746                BreakBeforeBraceShortIfs);
16747   verifyFormat("void f(bool b)\n"
16748                "{\n"
16749                "  while (b)\n"
16750                "  {\n"
16751                "    return;\n"
16752                "  }\n"
16753                "}\n",
16754                BreakBeforeBraceShortIfs);
16755 }
16756 
16757 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
16758   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
16759   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
16760 
16761   // Make a few changes to the style for testing purposes
16762   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
16763       FormatStyle::SFS_Empty;
16764   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
16765   WhitesmithsBraceStyle.ColumnLimit = 0;
16766 
16767   // FIXME: this test case can't decide whether there should be a blank line
16768   // after the ~D() line or not. It adds one if one doesn't exist in the test
16769   // and it removes the line if one exists.
16770   /*
16771   verifyFormat("class A;\n"
16772                "namespace B\n"
16773                "  {\n"
16774                "class C;\n"
16775                "// Comment\n"
16776                "class D\n"
16777                "  {\n"
16778                "public:\n"
16779                "  D();\n"
16780                "  ~D() {}\n"
16781                "private:\n"
16782                "  enum E\n"
16783                "    {\n"
16784                "    F\n"
16785                "    }\n"
16786                "  };\n"
16787                "  } // namespace B\n",
16788                WhitesmithsBraceStyle);
16789   */
16790 
16791   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
16792   verifyFormat("namespace a\n"
16793                "  {\n"
16794                "class A\n"
16795                "  {\n"
16796                "  void f()\n"
16797                "    {\n"
16798                "    if (true)\n"
16799                "      {\n"
16800                "      a();\n"
16801                "      b();\n"
16802                "      }\n"
16803                "    }\n"
16804                "  void g()\n"
16805                "    {\n"
16806                "    return;\n"
16807                "    }\n"
16808                "  };\n"
16809                "struct B\n"
16810                "  {\n"
16811                "  int x;\n"
16812                "  };\n"
16813                "  } // namespace a",
16814                WhitesmithsBraceStyle);
16815 
16816   verifyFormat("namespace a\n"
16817                "  {\n"
16818                "namespace b\n"
16819                "  {\n"
16820                "class A\n"
16821                "  {\n"
16822                "  void f()\n"
16823                "    {\n"
16824                "    if (true)\n"
16825                "      {\n"
16826                "      a();\n"
16827                "      b();\n"
16828                "      }\n"
16829                "    }\n"
16830                "  void g()\n"
16831                "    {\n"
16832                "    return;\n"
16833                "    }\n"
16834                "  };\n"
16835                "struct B\n"
16836                "  {\n"
16837                "  int x;\n"
16838                "  };\n"
16839                "  } // namespace b\n"
16840                "  } // namespace a",
16841                WhitesmithsBraceStyle);
16842 
16843   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
16844   verifyFormat("namespace a\n"
16845                "  {\n"
16846                "namespace b\n"
16847                "  {\n"
16848                "  class A\n"
16849                "    {\n"
16850                "    void f()\n"
16851                "      {\n"
16852                "      if (true)\n"
16853                "        {\n"
16854                "        a();\n"
16855                "        b();\n"
16856                "        }\n"
16857                "      }\n"
16858                "    void g()\n"
16859                "      {\n"
16860                "      return;\n"
16861                "      }\n"
16862                "    };\n"
16863                "  struct B\n"
16864                "    {\n"
16865                "    int x;\n"
16866                "    };\n"
16867                "  } // namespace b\n"
16868                "  } // namespace a",
16869                WhitesmithsBraceStyle);
16870 
16871   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
16872   verifyFormat("namespace a\n"
16873                "  {\n"
16874                "  namespace b\n"
16875                "    {\n"
16876                "    class A\n"
16877                "      {\n"
16878                "      void f()\n"
16879                "        {\n"
16880                "        if (true)\n"
16881                "          {\n"
16882                "          a();\n"
16883                "          b();\n"
16884                "          }\n"
16885                "        }\n"
16886                "      void g()\n"
16887                "        {\n"
16888                "        return;\n"
16889                "        }\n"
16890                "      };\n"
16891                "    struct B\n"
16892                "      {\n"
16893                "      int x;\n"
16894                "      };\n"
16895                "    } // namespace b\n"
16896                "  }   // namespace a",
16897                WhitesmithsBraceStyle);
16898 
16899   verifyFormat("void f()\n"
16900                "  {\n"
16901                "  if (true)\n"
16902                "    {\n"
16903                "    a();\n"
16904                "    }\n"
16905                "  else if (false)\n"
16906                "    {\n"
16907                "    b();\n"
16908                "    }\n"
16909                "  else\n"
16910                "    {\n"
16911                "    c();\n"
16912                "    }\n"
16913                "  }\n",
16914                WhitesmithsBraceStyle);
16915 
16916   verifyFormat("void f()\n"
16917                "  {\n"
16918                "  for (int i = 0; i < 10; ++i)\n"
16919                "    {\n"
16920                "    a();\n"
16921                "    }\n"
16922                "  while (false)\n"
16923                "    {\n"
16924                "    b();\n"
16925                "    }\n"
16926                "  do\n"
16927                "    {\n"
16928                "    c();\n"
16929                "    } while (false)\n"
16930                "  }\n",
16931                WhitesmithsBraceStyle);
16932 
16933   WhitesmithsBraceStyle.IndentCaseLabels = true;
16934   verifyFormat("void switchTest1(int a)\n"
16935                "  {\n"
16936                "  switch (a)\n"
16937                "    {\n"
16938                "    case 2:\n"
16939                "      {\n"
16940                "      }\n"
16941                "      break;\n"
16942                "    }\n"
16943                "  }\n",
16944                WhitesmithsBraceStyle);
16945 
16946   verifyFormat("void switchTest2(int a)\n"
16947                "  {\n"
16948                "  switch (a)\n"
16949                "    {\n"
16950                "    case 0:\n"
16951                "      break;\n"
16952                "    case 1:\n"
16953                "      {\n"
16954                "      break;\n"
16955                "      }\n"
16956                "    case 2:\n"
16957                "      {\n"
16958                "      }\n"
16959                "      break;\n"
16960                "    default:\n"
16961                "      break;\n"
16962                "    }\n"
16963                "  }\n",
16964                WhitesmithsBraceStyle);
16965 
16966   verifyFormat("void switchTest3(int a)\n"
16967                "  {\n"
16968                "  switch (a)\n"
16969                "    {\n"
16970                "    case 0:\n"
16971                "      {\n"
16972                "      foo(x);\n"
16973                "      }\n"
16974                "      break;\n"
16975                "    default:\n"
16976                "      {\n"
16977                "      foo(1);\n"
16978                "      }\n"
16979                "      break;\n"
16980                "    }\n"
16981                "  }\n",
16982                WhitesmithsBraceStyle);
16983 
16984   WhitesmithsBraceStyle.IndentCaseLabels = false;
16985 
16986   verifyFormat("void switchTest4(int a)\n"
16987                "  {\n"
16988                "  switch (a)\n"
16989                "    {\n"
16990                "  case 2:\n"
16991                "    {\n"
16992                "    }\n"
16993                "    break;\n"
16994                "    }\n"
16995                "  }\n",
16996                WhitesmithsBraceStyle);
16997 
16998   verifyFormat("void switchTest5(int a)\n"
16999                "  {\n"
17000                "  switch (a)\n"
17001                "    {\n"
17002                "  case 0:\n"
17003                "    break;\n"
17004                "  case 1:\n"
17005                "    {\n"
17006                "    foo();\n"
17007                "    break;\n"
17008                "    }\n"
17009                "  case 2:\n"
17010                "    {\n"
17011                "    }\n"
17012                "    break;\n"
17013                "  default:\n"
17014                "    break;\n"
17015                "    }\n"
17016                "  }\n",
17017                WhitesmithsBraceStyle);
17018 
17019   verifyFormat("void switchTest6(int a)\n"
17020                "  {\n"
17021                "  switch (a)\n"
17022                "    {\n"
17023                "  case 0:\n"
17024                "    {\n"
17025                "    foo(x);\n"
17026                "    }\n"
17027                "    break;\n"
17028                "  default:\n"
17029                "    {\n"
17030                "    foo(1);\n"
17031                "    }\n"
17032                "    break;\n"
17033                "    }\n"
17034                "  }\n",
17035                WhitesmithsBraceStyle);
17036 
17037   verifyFormat("enum X\n"
17038                "  {\n"
17039                "  Y = 0, // testing\n"
17040                "  }\n",
17041                WhitesmithsBraceStyle);
17042 
17043   verifyFormat("enum X\n"
17044                "  {\n"
17045                "  Y = 0\n"
17046                "  }\n",
17047                WhitesmithsBraceStyle);
17048   verifyFormat("enum X\n"
17049                "  {\n"
17050                "  Y = 0,\n"
17051                "  Z = 1\n"
17052                "  };\n",
17053                WhitesmithsBraceStyle);
17054 
17055   verifyFormat("@interface BSApplicationController ()\n"
17056                "  {\n"
17057                "@private\n"
17058                "  id _extraIvar;\n"
17059                "  }\n"
17060                "@end\n",
17061                WhitesmithsBraceStyle);
17062 
17063   verifyFormat("#ifdef _DEBUG\n"
17064                "int foo(int i = 0)\n"
17065                "#else\n"
17066                "int foo(int i = 5)\n"
17067                "#endif\n"
17068                "  {\n"
17069                "  return i;\n"
17070                "  }",
17071                WhitesmithsBraceStyle);
17072 
17073   verifyFormat("void foo() {}\n"
17074                "void bar()\n"
17075                "#ifdef _DEBUG\n"
17076                "  {\n"
17077                "  foo();\n"
17078                "  }\n"
17079                "#else\n"
17080                "  {\n"
17081                "  }\n"
17082                "#endif",
17083                WhitesmithsBraceStyle);
17084 
17085   verifyFormat("void foobar()\n"
17086                "  {\n"
17087                "  int i = 5;\n"
17088                "  }\n"
17089                "#ifdef _DEBUG\n"
17090                "void bar()\n"
17091                "  {\n"
17092                "  }\n"
17093                "#else\n"
17094                "void bar()\n"
17095                "  {\n"
17096                "  foobar();\n"
17097                "  }\n"
17098                "#endif",
17099                WhitesmithsBraceStyle);
17100 
17101   // This shouldn't affect ObjC blocks..
17102   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17103                "  // ...\n"
17104                "  int i;\n"
17105                "}];",
17106                WhitesmithsBraceStyle);
17107   verifyFormat("void (^block)(void) = ^{\n"
17108                "  // ...\n"
17109                "  int i;\n"
17110                "};",
17111                WhitesmithsBraceStyle);
17112   // .. or dict literals.
17113   verifyFormat("void f()\n"
17114                "  {\n"
17115                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17116                "  }",
17117                WhitesmithsBraceStyle);
17118 
17119   verifyFormat("int f()\n"
17120                "  { // comment\n"
17121                "  return 42;\n"
17122                "  }",
17123                WhitesmithsBraceStyle);
17124 
17125   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
17126   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17127       FormatStyle::SIS_OnlyFirstIf;
17128   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17129   verifyFormat("void f(bool b)\n"
17130                "  {\n"
17131                "  if (b)\n"
17132                "    {\n"
17133                "    return;\n"
17134                "    }\n"
17135                "  }\n",
17136                BreakBeforeBraceShortIfs);
17137   verifyFormat("void f(bool b)\n"
17138                "  {\n"
17139                "  if (b) return;\n"
17140                "  }\n",
17141                BreakBeforeBraceShortIfs);
17142   verifyFormat("void f(bool b)\n"
17143                "  {\n"
17144                "  while (b)\n"
17145                "    {\n"
17146                "    return;\n"
17147                "    }\n"
17148                "  }\n",
17149                BreakBeforeBraceShortIfs);
17150 }
17151 
17152 TEST_F(FormatTest, GNUBraceBreaking) {
17153   FormatStyle GNUBraceStyle = getLLVMStyle();
17154   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
17155   verifyFormat("namespace a\n"
17156                "{\n"
17157                "class A\n"
17158                "{\n"
17159                "  void f()\n"
17160                "  {\n"
17161                "    int a;\n"
17162                "    {\n"
17163                "      int b;\n"
17164                "    }\n"
17165                "    if (true)\n"
17166                "      {\n"
17167                "        a();\n"
17168                "        b();\n"
17169                "      }\n"
17170                "  }\n"
17171                "  void g() { return; }\n"
17172                "}\n"
17173                "} // namespace a",
17174                GNUBraceStyle);
17175 
17176   verifyFormat("void f()\n"
17177                "{\n"
17178                "  if (true)\n"
17179                "    {\n"
17180                "      a();\n"
17181                "    }\n"
17182                "  else if (false)\n"
17183                "    {\n"
17184                "      b();\n"
17185                "    }\n"
17186                "  else\n"
17187                "    {\n"
17188                "      c();\n"
17189                "    }\n"
17190                "}\n",
17191                GNUBraceStyle);
17192 
17193   verifyFormat("void f()\n"
17194                "{\n"
17195                "  for (int i = 0; i < 10; ++i)\n"
17196                "    {\n"
17197                "      a();\n"
17198                "    }\n"
17199                "  while (false)\n"
17200                "    {\n"
17201                "      b();\n"
17202                "    }\n"
17203                "  do\n"
17204                "    {\n"
17205                "      c();\n"
17206                "    }\n"
17207                "  while (false);\n"
17208                "}\n",
17209                GNUBraceStyle);
17210 
17211   verifyFormat("void f(int a)\n"
17212                "{\n"
17213                "  switch (a)\n"
17214                "    {\n"
17215                "    case 0:\n"
17216                "      break;\n"
17217                "    case 1:\n"
17218                "      {\n"
17219                "        break;\n"
17220                "      }\n"
17221                "    case 2:\n"
17222                "      {\n"
17223                "      }\n"
17224                "      break;\n"
17225                "    default:\n"
17226                "      break;\n"
17227                "    }\n"
17228                "}\n",
17229                GNUBraceStyle);
17230 
17231   verifyFormat("enum X\n"
17232                "{\n"
17233                "  Y = 0,\n"
17234                "}\n",
17235                GNUBraceStyle);
17236 
17237   verifyFormat("@interface BSApplicationController ()\n"
17238                "{\n"
17239                "@private\n"
17240                "  id _extraIvar;\n"
17241                "}\n"
17242                "@end\n",
17243                GNUBraceStyle);
17244 
17245   verifyFormat("#ifdef _DEBUG\n"
17246                "int foo(int i = 0)\n"
17247                "#else\n"
17248                "int foo(int i = 5)\n"
17249                "#endif\n"
17250                "{\n"
17251                "  return i;\n"
17252                "}",
17253                GNUBraceStyle);
17254 
17255   verifyFormat("void foo() {}\n"
17256                "void bar()\n"
17257                "#ifdef _DEBUG\n"
17258                "{\n"
17259                "  foo();\n"
17260                "}\n"
17261                "#else\n"
17262                "{\n"
17263                "}\n"
17264                "#endif",
17265                GNUBraceStyle);
17266 
17267   verifyFormat("void foobar() { int i = 5; }\n"
17268                "#ifdef _DEBUG\n"
17269                "void bar() {}\n"
17270                "#else\n"
17271                "void bar() { foobar(); }\n"
17272                "#endif",
17273                GNUBraceStyle);
17274 }
17275 
17276 TEST_F(FormatTest, WebKitBraceBreaking) {
17277   FormatStyle WebKitBraceStyle = getLLVMStyle();
17278   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
17279   WebKitBraceStyle.FixNamespaceComments = false;
17280   verifyFormat("namespace a {\n"
17281                "class A {\n"
17282                "  void f()\n"
17283                "  {\n"
17284                "    if (true) {\n"
17285                "      a();\n"
17286                "      b();\n"
17287                "    }\n"
17288                "  }\n"
17289                "  void g() { return; }\n"
17290                "};\n"
17291                "enum E {\n"
17292                "  A,\n"
17293                "  // foo\n"
17294                "  B,\n"
17295                "  C\n"
17296                "};\n"
17297                "struct B {\n"
17298                "  int x;\n"
17299                "};\n"
17300                "}\n",
17301                WebKitBraceStyle);
17302   verifyFormat("struct S {\n"
17303                "  int Type;\n"
17304                "  union {\n"
17305                "    int x;\n"
17306                "    double y;\n"
17307                "  } Value;\n"
17308                "  class C {\n"
17309                "    MyFavoriteType Value;\n"
17310                "  } Class;\n"
17311                "};\n",
17312                WebKitBraceStyle);
17313 }
17314 
17315 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
17316   verifyFormat("void f() {\n"
17317                "  try {\n"
17318                "  } catch (const Exception &e) {\n"
17319                "  }\n"
17320                "}\n",
17321                getLLVMStyle());
17322 }
17323 
17324 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
17325   auto Style = getLLVMStyle();
17326   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17327   Style.AlignConsecutiveAssignments =
17328       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17329   Style.AlignConsecutiveDeclarations =
17330       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17331   verifyFormat("struct test demo[] = {\n"
17332                "    {56,    23, \"hello\"},\n"
17333                "    {-1, 93463, \"world\"},\n"
17334                "    { 7,     5,    \"!!\"}\n"
17335                "};\n",
17336                Style);
17337 
17338   verifyFormat("struct test demo[] = {\n"
17339                "    {56,    23, \"hello\"}, // first line\n"
17340                "    {-1, 93463, \"world\"}, // second line\n"
17341                "    { 7,     5,    \"!!\"}  // third line\n"
17342                "};\n",
17343                Style);
17344 
17345   verifyFormat("struct test demo[4] = {\n"
17346                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17347                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17348                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17349                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17350                "};\n",
17351                Style);
17352 
17353   verifyFormat("struct test demo[3] = {\n"
17354                "    {56,    23, \"hello\"},\n"
17355                "    {-1, 93463, \"world\"},\n"
17356                "    { 7,     5,    \"!!\"}\n"
17357                "};\n",
17358                Style);
17359 
17360   verifyFormat("struct test demo[3] = {\n"
17361                "    {int{56},    23, \"hello\"},\n"
17362                "    {int{-1}, 93463, \"world\"},\n"
17363                "    { int{7},     5,    \"!!\"}\n"
17364                "};\n",
17365                Style);
17366 
17367   verifyFormat("struct test demo[] = {\n"
17368                "    {56,    23, \"hello\"},\n"
17369                "    {-1, 93463, \"world\"},\n"
17370                "    { 7,     5,    \"!!\"},\n"
17371                "};\n",
17372                Style);
17373 
17374   verifyFormat("test demo[] = {\n"
17375                "    {56,    23, \"hello\"},\n"
17376                "    {-1, 93463, \"world\"},\n"
17377                "    { 7,     5,    \"!!\"},\n"
17378                "};\n",
17379                Style);
17380 
17381   verifyFormat("demo = std::array<struct test, 3>{\n"
17382                "    test{56,    23, \"hello\"},\n"
17383                "    test{-1, 93463, \"world\"},\n"
17384                "    test{ 7,     5,    \"!!\"},\n"
17385                "};\n",
17386                Style);
17387 
17388   verifyFormat("test demo[] = {\n"
17389                "    {56,    23, \"hello\"},\n"
17390                "#if X\n"
17391                "    {-1, 93463, \"world\"},\n"
17392                "#endif\n"
17393                "    { 7,     5,    \"!!\"}\n"
17394                "};\n",
17395                Style);
17396 
17397   verifyFormat(
17398       "test demo[] = {\n"
17399       "    { 7,    23,\n"
17400       "     \"hello world i am a very long line that really, in any\"\n"
17401       "     \"just world, ought to be split over multiple lines\"},\n"
17402       "    {-1, 93463,                                  \"world\"},\n"
17403       "    {56,     5,                                     \"!!\"}\n"
17404       "};\n",
17405       Style);
17406 
17407   verifyFormat("return GradForUnaryCwise(g, {\n"
17408                "                                {{\"sign\"}, \"Sign\",  "
17409                "  {\"x\", \"dy\"}},\n"
17410                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
17411                ", \"sign\"}},\n"
17412                "});\n",
17413                Style);
17414 
17415   Style.ColumnLimit = 0;
17416   EXPECT_EQ(
17417       "test demo[] = {\n"
17418       "    {56,    23, \"hello world i am a very long line that really, "
17419       "in any just world, ought to be split over multiple lines\"},\n"
17420       "    {-1, 93463,                                                  "
17421       "                                                 \"world\"},\n"
17422       "    { 7,     5,                                                  "
17423       "                                                    \"!!\"},\n"
17424       "};",
17425       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17426              "that really, in any just world, ought to be split over multiple "
17427              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17428              Style));
17429 
17430   Style.ColumnLimit = 80;
17431   verifyFormat("test demo[] = {\n"
17432                "    {56,    23, /* a comment */ \"hello\"},\n"
17433                "    {-1, 93463,                 \"world\"},\n"
17434                "    { 7,     5,                    \"!!\"}\n"
17435                "};\n",
17436                Style);
17437 
17438   verifyFormat("test demo[] = {\n"
17439                "    {56,    23,                    \"hello\"},\n"
17440                "    {-1, 93463, \"world\" /* comment here */},\n"
17441                "    { 7,     5,                       \"!!\"}\n"
17442                "};\n",
17443                Style);
17444 
17445   verifyFormat("test demo[] = {\n"
17446                "    {56, /* a comment */ 23, \"hello\"},\n"
17447                "    {-1,              93463, \"world\"},\n"
17448                "    { 7,                  5,    \"!!\"}\n"
17449                "};\n",
17450                Style);
17451 
17452   Style.ColumnLimit = 20;
17453   EXPECT_EQ(
17454       "demo = std::array<\n"
17455       "    struct test, 3>{\n"
17456       "    test{\n"
17457       "         56,    23,\n"
17458       "         \"hello \"\n"
17459       "         \"world i \"\n"
17460       "         \"am a very \"\n"
17461       "         \"long line \"\n"
17462       "         \"that \"\n"
17463       "         \"really, \"\n"
17464       "         \"in any \"\n"
17465       "         \"just \"\n"
17466       "         \"world, \"\n"
17467       "         \"ought to \"\n"
17468       "         \"be split \"\n"
17469       "         \"over \"\n"
17470       "         \"multiple \"\n"
17471       "         \"lines\"},\n"
17472       "    test{-1, 93463,\n"
17473       "         \"world\"},\n"
17474       "    test{ 7,     5,\n"
17475       "         \"!!\"   },\n"
17476       "};",
17477       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17478              "i am a very long line that really, in any just world, ought "
17479              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17480              "test{7, 5, \"!!\"},};",
17481              Style));
17482   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17483   Style = getLLVMStyleWithColumns(50);
17484   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17485   verifyFormat("static A x = {\n"
17486                "    {{init1, init2, init3, init4},\n"
17487                "     {init1, init2, init3, init4}}\n"
17488                "};",
17489                Style);
17490   Style.ColumnLimit = 100;
17491   EXPECT_EQ(
17492       "test demo[] = {\n"
17493       "    {56,    23,\n"
17494       "     \"hello world i am a very long line that really, in any just world"
17495       ", ought to be split over \"\n"
17496       "     \"multiple lines\"  },\n"
17497       "    {-1, 93463, \"world\"},\n"
17498       "    { 7,     5,    \"!!\"},\n"
17499       "};",
17500       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17501              "that really, in any just world, ought to be split over multiple "
17502              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17503              Style));
17504 
17505   Style = getLLVMStyleWithColumns(50);
17506   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17507   Style.AlignConsecutiveAssignments =
17508       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17509   Style.AlignConsecutiveDeclarations =
17510       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17511   verifyFormat("struct test demo[] = {\n"
17512                "    {56,    23, \"hello\"},\n"
17513                "    {-1, 93463, \"world\"},\n"
17514                "    { 7,     5,    \"!!\"}\n"
17515                "};\n"
17516                "static A x = {\n"
17517                "    {{init1, init2, init3, init4},\n"
17518                "     {init1, init2, init3, init4}}\n"
17519                "};",
17520                Style);
17521   Style.ColumnLimit = 100;
17522   Style.AlignConsecutiveAssignments =
17523       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17524   Style.AlignConsecutiveDeclarations =
17525       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17526   verifyFormat("struct test demo[] = {\n"
17527                "    {56,    23, \"hello\"},\n"
17528                "    {-1, 93463, \"world\"},\n"
17529                "    { 7,     5,    \"!!\"}\n"
17530                "};\n"
17531                "struct test demo[4] = {\n"
17532                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17533                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17534                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17535                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17536                "};\n",
17537                Style);
17538   EXPECT_EQ(
17539       "test demo[] = {\n"
17540       "    {56,\n"
17541       "     \"hello world i am a very long line that really, in any just world"
17542       ", ought to be split over \"\n"
17543       "     \"multiple lines\",    23},\n"
17544       "    {-1,      \"world\", 93463},\n"
17545       "    { 7,         \"!!\",     5},\n"
17546       "};",
17547       format("test demo[] = {{56, \"hello world i am a very long line "
17548              "that really, in any just world, ought to be split over multiple "
17549              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
17550              Style));
17551 }
17552 
17553 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
17554   auto Style = getLLVMStyle();
17555   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
17556   verifyFormat("struct test demo[] = {\n"
17557                "    {56, 23,    \"hello\"},\n"
17558                "    {-1, 93463, \"world\"},\n"
17559                "    {7,  5,     \"!!\"   }\n"
17560                "};\n",
17561                Style);
17562 
17563   verifyFormat("struct test demo[] = {\n"
17564                "    {56, 23,    \"hello\"}, // first line\n"
17565                "    {-1, 93463, \"world\"}, // second line\n"
17566                "    {7,  5,     \"!!\"   }  // third line\n"
17567                "};\n",
17568                Style);
17569   verifyFormat("struct test demo[4] = {\n"
17570                "    {56,  23,    21, \"oh\"      }, // first line\n"
17571                "    {-1,  93463, 22, \"my\"      }, // second line\n"
17572                "    {7,   5,     1,  \"goodness\"}  // third line\n"
17573                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
17574                "};\n",
17575                Style);
17576   verifyFormat("struct test demo[3] = {\n"
17577                "    {56, 23,    \"hello\"},\n"
17578                "    {-1, 93463, \"world\"},\n"
17579                "    {7,  5,     \"!!\"   }\n"
17580                "};\n",
17581                Style);
17582 
17583   verifyFormat("struct test demo[3] = {\n"
17584                "    {int{56}, 23,    \"hello\"},\n"
17585                "    {int{-1}, 93463, \"world\"},\n"
17586                "    {int{7},  5,     \"!!\"   }\n"
17587                "};\n",
17588                Style);
17589   verifyFormat("struct test demo[] = {\n"
17590                "    {56, 23,    \"hello\"},\n"
17591                "    {-1, 93463, \"world\"},\n"
17592                "    {7,  5,     \"!!\"   },\n"
17593                "};\n",
17594                Style);
17595   verifyFormat("test demo[] = {\n"
17596                "    {56, 23,    \"hello\"},\n"
17597                "    {-1, 93463, \"world\"},\n"
17598                "    {7,  5,     \"!!\"   },\n"
17599                "};\n",
17600                Style);
17601   verifyFormat("demo = std::array<struct test, 3>{\n"
17602                "    test{56, 23,    \"hello\"},\n"
17603                "    test{-1, 93463, \"world\"},\n"
17604                "    test{7,  5,     \"!!\"   },\n"
17605                "};\n",
17606                Style);
17607   verifyFormat("test demo[] = {\n"
17608                "    {56, 23,    \"hello\"},\n"
17609                "#if X\n"
17610                "    {-1, 93463, \"world\"},\n"
17611                "#endif\n"
17612                "    {7,  5,     \"!!\"   }\n"
17613                "};\n",
17614                Style);
17615   verifyFormat(
17616       "test demo[] = {\n"
17617       "    {7,  23,\n"
17618       "     \"hello world i am a very long line that really, in any\"\n"
17619       "     \"just world, ought to be split over multiple lines\"},\n"
17620       "    {-1, 93463, \"world\"                                 },\n"
17621       "    {56, 5,     \"!!\"                                    }\n"
17622       "};\n",
17623       Style);
17624 
17625   verifyFormat("return GradForUnaryCwise(g, {\n"
17626                "                                {{\"sign\"}, \"Sign\", {\"x\", "
17627                "\"dy\"}   },\n"
17628                "                                {{\"dx\"},   \"Mul\",  "
17629                "{\"dy\", \"sign\"}},\n"
17630                "});\n",
17631                Style);
17632 
17633   Style.ColumnLimit = 0;
17634   EXPECT_EQ(
17635       "test demo[] = {\n"
17636       "    {56, 23,    \"hello world i am a very long line that really, in any "
17637       "just world, ought to be split over multiple lines\"},\n"
17638       "    {-1, 93463, \"world\"                                               "
17639       "                                                   },\n"
17640       "    {7,  5,     \"!!\"                                                  "
17641       "                                                   },\n"
17642       "};",
17643       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17644              "that really, in any just world, ought to be split over multiple "
17645              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17646              Style));
17647 
17648   Style.ColumnLimit = 80;
17649   verifyFormat("test demo[] = {\n"
17650                "    {56, 23,    /* a comment */ \"hello\"},\n"
17651                "    {-1, 93463, \"world\"                },\n"
17652                "    {7,  5,     \"!!\"                   }\n"
17653                "};\n",
17654                Style);
17655 
17656   verifyFormat("test demo[] = {\n"
17657                "    {56, 23,    \"hello\"                   },\n"
17658                "    {-1, 93463, \"world\" /* comment here */},\n"
17659                "    {7,  5,     \"!!\"                      }\n"
17660                "};\n",
17661                Style);
17662 
17663   verifyFormat("test demo[] = {\n"
17664                "    {56, /* a comment */ 23, \"hello\"},\n"
17665                "    {-1, 93463,              \"world\"},\n"
17666                "    {7,  5,                  \"!!\"   }\n"
17667                "};\n",
17668                Style);
17669 
17670   Style.ColumnLimit = 20;
17671   EXPECT_EQ(
17672       "demo = std::array<\n"
17673       "    struct test, 3>{\n"
17674       "    test{\n"
17675       "         56, 23,\n"
17676       "         \"hello \"\n"
17677       "         \"world i \"\n"
17678       "         \"am a very \"\n"
17679       "         \"long line \"\n"
17680       "         \"that \"\n"
17681       "         \"really, \"\n"
17682       "         \"in any \"\n"
17683       "         \"just \"\n"
17684       "         \"world, \"\n"
17685       "         \"ought to \"\n"
17686       "         \"be split \"\n"
17687       "         \"over \"\n"
17688       "         \"multiple \"\n"
17689       "         \"lines\"},\n"
17690       "    test{-1, 93463,\n"
17691       "         \"world\"},\n"
17692       "    test{7,  5,\n"
17693       "         \"!!\"   },\n"
17694       "};",
17695       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17696              "i am a very long line that really, in any just world, ought "
17697              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17698              "test{7, 5, \"!!\"},};",
17699              Style));
17700 
17701   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17702   Style = getLLVMStyleWithColumns(50);
17703   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
17704   verifyFormat("static A x = {\n"
17705                "    {{init1, init2, init3, init4},\n"
17706                "     {init1, init2, init3, init4}}\n"
17707                "};",
17708                Style);
17709   Style.ColumnLimit = 100;
17710   EXPECT_EQ(
17711       "test demo[] = {\n"
17712       "    {56, 23,\n"
17713       "     \"hello world i am a very long line that really, in any just world"
17714       ", ought to be split over \"\n"
17715       "     \"multiple lines\"  },\n"
17716       "    {-1, 93463, \"world\"},\n"
17717       "    {7,  5,     \"!!\"   },\n"
17718       "};",
17719       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17720              "that really, in any just world, ought to be split over multiple "
17721              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17722              Style));
17723 }
17724 
17725 TEST_F(FormatTest, UnderstandsPragmas) {
17726   verifyFormat("#pragma omp reduction(| : var)");
17727   verifyFormat("#pragma omp reduction(+ : var)");
17728 
17729   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
17730             "(including parentheses).",
17731             format("#pragma    mark   Any non-hyphenated or hyphenated string "
17732                    "(including parentheses)."));
17733 }
17734 
17735 TEST_F(FormatTest, UnderstandPragmaOption) {
17736   verifyFormat("#pragma option -C -A");
17737 
17738   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
17739 }
17740 
17741 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
17742   FormatStyle Style = getLLVMStyle();
17743   Style.ColumnLimit = 20;
17744 
17745   // See PR41213
17746   EXPECT_EQ("/*\n"
17747             " *\t9012345\n"
17748             " * /8901\n"
17749             " */",
17750             format("/*\n"
17751                    " *\t9012345 /8901\n"
17752                    " */",
17753                    Style));
17754   EXPECT_EQ("/*\n"
17755             " *345678\n"
17756             " *\t/8901\n"
17757             " */",
17758             format("/*\n"
17759                    " *345678\t/8901\n"
17760                    " */",
17761                    Style));
17762 
17763   verifyFormat("int a; // the\n"
17764                "       // comment",
17765                Style);
17766   EXPECT_EQ("int a; /* first line\n"
17767             "        * second\n"
17768             "        * line third\n"
17769             "        * line\n"
17770             "        */",
17771             format("int a; /* first line\n"
17772                    "        * second\n"
17773                    "        * line third\n"
17774                    "        * line\n"
17775                    "        */",
17776                    Style));
17777   EXPECT_EQ("int a; // first line\n"
17778             "       // second\n"
17779             "       // line third\n"
17780             "       // line",
17781             format("int a; // first line\n"
17782                    "       // second line\n"
17783                    "       // third line",
17784                    Style));
17785 
17786   Style.PenaltyExcessCharacter = 90;
17787   verifyFormat("int a; // the comment", Style);
17788   EXPECT_EQ("int a; // the comment\n"
17789             "       // aaa",
17790             format("int a; // the comment aaa", Style));
17791   EXPECT_EQ("int a; /* first line\n"
17792             "        * second line\n"
17793             "        * third line\n"
17794             "        */",
17795             format("int a; /* first line\n"
17796                    "        * second line\n"
17797                    "        * third line\n"
17798                    "        */",
17799                    Style));
17800   EXPECT_EQ("int a; // first line\n"
17801             "       // second line\n"
17802             "       // third line",
17803             format("int a; // first line\n"
17804                    "       // second line\n"
17805                    "       // third line",
17806                    Style));
17807   // FIXME: Investigate why this is not getting the same layout as the test
17808   // above.
17809   EXPECT_EQ("int a; /* first line\n"
17810             "        * second line\n"
17811             "        * third line\n"
17812             "        */",
17813             format("int a; /* first line second line third line"
17814                    "\n*/",
17815                    Style));
17816 
17817   EXPECT_EQ("// foo bar baz bazfoo\n"
17818             "// foo bar foo bar\n",
17819             format("// foo bar baz bazfoo\n"
17820                    "// foo bar foo           bar\n",
17821                    Style));
17822   EXPECT_EQ("// foo bar baz bazfoo\n"
17823             "// foo bar foo bar\n",
17824             format("// foo bar baz      bazfoo\n"
17825                    "// foo            bar foo bar\n",
17826                    Style));
17827 
17828   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
17829   // next one.
17830   EXPECT_EQ("// foo bar baz bazfoo\n"
17831             "// bar foo bar\n",
17832             format("// foo bar baz      bazfoo bar\n"
17833                    "// foo            bar\n",
17834                    Style));
17835 
17836   EXPECT_EQ("// foo bar baz bazfoo\n"
17837             "// foo bar baz bazfoo\n"
17838             "// bar foo bar\n",
17839             format("// foo bar baz      bazfoo\n"
17840                    "// foo bar baz      bazfoo bar\n"
17841                    "// foo bar\n",
17842                    Style));
17843 
17844   EXPECT_EQ("// foo bar baz bazfoo\n"
17845             "// foo bar baz bazfoo\n"
17846             "// bar foo bar\n",
17847             format("// foo bar baz      bazfoo\n"
17848                    "// foo bar baz      bazfoo bar\n"
17849                    "// foo           bar\n",
17850                    Style));
17851 
17852   // Make sure we do not keep protruding characters if strict mode reflow is
17853   // cheaper than keeping protruding characters.
17854   Style.ColumnLimit = 21;
17855   EXPECT_EQ(
17856       "// foo foo foo foo\n"
17857       "// foo foo foo foo\n"
17858       "// foo foo foo foo\n",
17859       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
17860 
17861   EXPECT_EQ("int a = /* long block\n"
17862             "           comment */\n"
17863             "    42;",
17864             format("int a = /* long block comment */ 42;", Style));
17865 }
17866 
17867 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
17868   for (size_t i = 1; i < Styles.size(); ++i)                                   \
17869   EXPECT_EQ(Styles[0], Styles[i])                                              \
17870       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
17871 
17872 TEST_F(FormatTest, GetsPredefinedStyleByName) {
17873   SmallVector<FormatStyle, 3> Styles;
17874   Styles.resize(3);
17875 
17876   Styles[0] = getLLVMStyle();
17877   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
17878   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
17879   EXPECT_ALL_STYLES_EQUAL(Styles);
17880 
17881   Styles[0] = getGoogleStyle();
17882   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
17883   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
17884   EXPECT_ALL_STYLES_EQUAL(Styles);
17885 
17886   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
17887   EXPECT_TRUE(
17888       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
17889   EXPECT_TRUE(
17890       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
17891   EXPECT_ALL_STYLES_EQUAL(Styles);
17892 
17893   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
17894   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
17895   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
17896   EXPECT_ALL_STYLES_EQUAL(Styles);
17897 
17898   Styles[0] = getMozillaStyle();
17899   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
17900   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
17901   EXPECT_ALL_STYLES_EQUAL(Styles);
17902 
17903   Styles[0] = getWebKitStyle();
17904   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
17905   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
17906   EXPECT_ALL_STYLES_EQUAL(Styles);
17907 
17908   Styles[0] = getGNUStyle();
17909   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
17910   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
17911   EXPECT_ALL_STYLES_EQUAL(Styles);
17912 
17913   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
17914 }
17915 
17916 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
17917   SmallVector<FormatStyle, 8> Styles;
17918   Styles.resize(2);
17919 
17920   Styles[0] = getGoogleStyle();
17921   Styles[1] = getLLVMStyle();
17922   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
17923   EXPECT_ALL_STYLES_EQUAL(Styles);
17924 
17925   Styles.resize(5);
17926   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
17927   Styles[1] = getLLVMStyle();
17928   Styles[1].Language = FormatStyle::LK_JavaScript;
17929   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
17930 
17931   Styles[2] = getLLVMStyle();
17932   Styles[2].Language = FormatStyle::LK_JavaScript;
17933   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
17934                                   "BasedOnStyle: Google",
17935                                   &Styles[2])
17936                    .value());
17937 
17938   Styles[3] = getLLVMStyle();
17939   Styles[3].Language = FormatStyle::LK_JavaScript;
17940   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
17941                                   "Language: JavaScript",
17942                                   &Styles[3])
17943                    .value());
17944 
17945   Styles[4] = getLLVMStyle();
17946   Styles[4].Language = FormatStyle::LK_JavaScript;
17947   EXPECT_EQ(0, parseConfiguration("---\n"
17948                                   "BasedOnStyle: LLVM\n"
17949                                   "IndentWidth: 123\n"
17950                                   "---\n"
17951                                   "BasedOnStyle: Google\n"
17952                                   "Language: JavaScript",
17953                                   &Styles[4])
17954                    .value());
17955   EXPECT_ALL_STYLES_EQUAL(Styles);
17956 }
17957 
17958 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
17959   Style.FIELD = false;                                                         \
17960   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
17961   EXPECT_TRUE(Style.FIELD);                                                    \
17962   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
17963   EXPECT_FALSE(Style.FIELD);
17964 
17965 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
17966 
17967 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
17968   Style.STRUCT.FIELD = false;                                                  \
17969   EXPECT_EQ(0,                                                                 \
17970             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
17971                 .value());                                                     \
17972   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
17973   EXPECT_EQ(0,                                                                 \
17974             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
17975                 .value());                                                     \
17976   EXPECT_FALSE(Style.STRUCT.FIELD);
17977 
17978 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
17979   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
17980 
17981 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
17982   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
17983   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
17984   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
17985 
17986 TEST_F(FormatTest, ParsesConfigurationBools) {
17987   FormatStyle Style = {};
17988   Style.Language = FormatStyle::LK_Cpp;
17989   CHECK_PARSE_BOOL(AlignTrailingComments);
17990   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
17991   CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
17992   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
17993   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
17994   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
17995   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
17996   CHECK_PARSE_BOOL(BinPackArguments);
17997   CHECK_PARSE_BOOL(BinPackParameters);
17998   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
17999   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
18000   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
18001   CHECK_PARSE_BOOL(BreakStringLiterals);
18002   CHECK_PARSE_BOOL(CompactNamespaces);
18003   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
18004   CHECK_PARSE_BOOL(DeriveLineEnding);
18005   CHECK_PARSE_BOOL(DerivePointerAlignment);
18006   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
18007   CHECK_PARSE_BOOL(DisableFormat);
18008   CHECK_PARSE_BOOL(IndentAccessModifiers);
18009   CHECK_PARSE_BOOL(IndentCaseLabels);
18010   CHECK_PARSE_BOOL(IndentCaseBlocks);
18011   CHECK_PARSE_BOOL(IndentGotoLabels);
18012   CHECK_PARSE_BOOL(IndentRequires);
18013   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
18014   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
18015   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
18016   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
18017   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
18018   CHECK_PARSE_BOOL(ReflowComments);
18019   CHECK_PARSE_BOOL(SortUsingDeclarations);
18020   CHECK_PARSE_BOOL(SpacesInParentheses);
18021   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
18022   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
18023   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
18024   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
18025   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
18026   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
18027   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
18028   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
18029   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
18030   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
18031   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
18032   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
18033   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
18034   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
18035   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
18036   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
18037   CHECK_PARSE_BOOL(UseCRLF);
18038 
18039   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
18040   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
18041   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
18042   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
18043   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
18044   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
18045   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
18046   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
18047   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
18048   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
18049   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
18050   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
18051   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
18052   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
18053   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
18054   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
18055   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
18056 }
18057 
18058 #undef CHECK_PARSE_BOOL
18059 
18060 TEST_F(FormatTest, ParsesConfiguration) {
18061   FormatStyle Style = {};
18062   Style.Language = FormatStyle::LK_Cpp;
18063   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
18064   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
18065               ConstructorInitializerIndentWidth, 1234u);
18066   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
18067   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
18068   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
18069   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
18070   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
18071               PenaltyBreakBeforeFirstCallParameter, 1234u);
18072   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
18073               PenaltyBreakTemplateDeclaration, 1234u);
18074   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
18075   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
18076               PenaltyReturnTypeOnItsOwnLine, 1234u);
18077   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
18078               SpacesBeforeTrailingComments, 1234u);
18079   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
18080   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
18081   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
18082 
18083   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
18084   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
18085               FormatStyle::ACS_None);
18086   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
18087               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
18088   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
18089               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
18090   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
18091               AlignConsecutiveAssignments,
18092               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18093   // For backwards compability, false / true should still parse
18094   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
18095               FormatStyle::ACS_None);
18096   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
18097               FormatStyle::ACS_Consecutive);
18098 
18099   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
18100   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
18101               FormatStyle::ACS_None);
18102   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
18103               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
18104   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
18105               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
18106   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
18107               AlignConsecutiveBitFields,
18108               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18109   // For backwards compability, false / true should still parse
18110   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
18111               FormatStyle::ACS_None);
18112   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
18113               FormatStyle::ACS_Consecutive);
18114 
18115   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
18116   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
18117               FormatStyle::ACS_None);
18118   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
18119               FormatStyle::ACS_Consecutive);
18120   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
18121               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
18122   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
18123               AlignConsecutiveMacros,
18124               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18125   // For backwards compability, false / true should still parse
18126   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
18127               FormatStyle::ACS_None);
18128   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
18129               FormatStyle::ACS_Consecutive);
18130 
18131   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
18132   CHECK_PARSE("AlignConsecutiveDeclarations: None",
18133               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18134   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
18135               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18136   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
18137               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
18138   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
18139               AlignConsecutiveDeclarations,
18140               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18141   // For backwards compability, false / true should still parse
18142   CHECK_PARSE("AlignConsecutiveDeclarations: false",
18143               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18144   CHECK_PARSE("AlignConsecutiveDeclarations: true",
18145               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18146 
18147   Style.PointerAlignment = FormatStyle::PAS_Middle;
18148   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
18149               FormatStyle::PAS_Left);
18150   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
18151               FormatStyle::PAS_Right);
18152   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
18153               FormatStyle::PAS_Middle);
18154   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
18155   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
18156               FormatStyle::RAS_Pointer);
18157   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
18158               FormatStyle::RAS_Left);
18159   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
18160               FormatStyle::RAS_Right);
18161   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
18162               FormatStyle::RAS_Middle);
18163   // For backward compatibility:
18164   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
18165               FormatStyle::PAS_Left);
18166   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
18167               FormatStyle::PAS_Right);
18168   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
18169               FormatStyle::PAS_Middle);
18170 
18171   Style.Standard = FormatStyle::LS_Auto;
18172   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
18173   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
18174   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
18175   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
18176   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
18177   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
18178   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
18179   // Legacy aliases:
18180   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
18181   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
18182   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
18183   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
18184 
18185   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18186   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
18187               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
18188   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
18189               FormatStyle::BOS_None);
18190   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
18191               FormatStyle::BOS_All);
18192   // For backward compatibility:
18193   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
18194               FormatStyle::BOS_None);
18195   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
18196               FormatStyle::BOS_All);
18197 
18198   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
18199   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
18200               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18201   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
18202               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
18203   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
18204               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
18205   // For backward compatibility:
18206   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
18207               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18208 
18209   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18210   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
18211               FormatStyle::BILS_AfterComma);
18212   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
18213               FormatStyle::BILS_BeforeComma);
18214   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
18215               FormatStyle::BILS_AfterColon);
18216   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
18217               FormatStyle::BILS_BeforeColon);
18218   // For backward compatibility:
18219   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
18220               FormatStyle::BILS_BeforeComma);
18221 
18222   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
18223   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
18224               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
18225   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
18226               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
18227   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
18228               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
18229   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
18230               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
18231 
18232   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18233   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
18234               FormatStyle::BAS_Align);
18235   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
18236               FormatStyle::BAS_DontAlign);
18237   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
18238               FormatStyle::BAS_AlwaysBreak);
18239   // For backward compatibility:
18240   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
18241               FormatStyle::BAS_DontAlign);
18242   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
18243               FormatStyle::BAS_Align);
18244 
18245   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18246   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
18247               FormatStyle::ENAS_DontAlign);
18248   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
18249               FormatStyle::ENAS_Left);
18250   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
18251               FormatStyle::ENAS_Right);
18252   // For backward compatibility:
18253   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
18254               FormatStyle::ENAS_Left);
18255   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
18256               FormatStyle::ENAS_Right);
18257 
18258   Style.AlignOperands = FormatStyle::OAS_Align;
18259   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
18260               FormatStyle::OAS_DontAlign);
18261   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
18262   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
18263               FormatStyle::OAS_AlignAfterOperator);
18264   // For backward compatibility:
18265   CHECK_PARSE("AlignOperands: false", AlignOperands,
18266               FormatStyle::OAS_DontAlign);
18267   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
18268 
18269   Style.UseTab = FormatStyle::UT_ForIndentation;
18270   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
18271   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
18272   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
18273   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
18274               FormatStyle::UT_ForContinuationAndIndentation);
18275   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
18276               FormatStyle::UT_AlignWithSpaces);
18277   // For backward compatibility:
18278   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
18279   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
18280 
18281   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
18282   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
18283               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18284   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
18285               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
18286   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
18287               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18288   // For backward compatibility:
18289   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
18290               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18291   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
18292               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18293 
18294   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18295   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
18296               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18297   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
18298               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
18299   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
18300               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
18301   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
18302               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18303   // For backward compatibility:
18304   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
18305               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18306   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
18307               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18308 
18309   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
18310   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
18311               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
18312   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
18313               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
18314   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
18315               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
18316   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
18317               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
18318 
18319   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
18320   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
18321               FormatStyle::SBPO_Never);
18322   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
18323               FormatStyle::SBPO_Always);
18324   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
18325               FormatStyle::SBPO_ControlStatements);
18326   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
18327               SpaceBeforeParens,
18328               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18329   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
18330               FormatStyle::SBPO_NonEmptyParentheses);
18331   // For backward compatibility:
18332   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
18333               FormatStyle::SBPO_Never);
18334   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
18335               FormatStyle::SBPO_ControlStatements);
18336   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
18337               SpaceBeforeParens,
18338               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18339 
18340   Style.ColumnLimit = 123;
18341   FormatStyle BaseStyle = getLLVMStyle();
18342   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
18343   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
18344 
18345   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
18346   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
18347               FormatStyle::BS_Attach);
18348   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
18349               FormatStyle::BS_Linux);
18350   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
18351               FormatStyle::BS_Mozilla);
18352   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
18353               FormatStyle::BS_Stroustrup);
18354   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
18355               FormatStyle::BS_Allman);
18356   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
18357               FormatStyle::BS_Whitesmiths);
18358   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
18359   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
18360               FormatStyle::BS_WebKit);
18361   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
18362               FormatStyle::BS_Custom);
18363 
18364   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
18365   CHECK_PARSE("BraceWrapping:\n"
18366               "  AfterControlStatement: MultiLine",
18367               BraceWrapping.AfterControlStatement,
18368               FormatStyle::BWACS_MultiLine);
18369   CHECK_PARSE("BraceWrapping:\n"
18370               "  AfterControlStatement: Always",
18371               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18372   CHECK_PARSE("BraceWrapping:\n"
18373               "  AfterControlStatement: Never",
18374               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18375   // For backward compatibility:
18376   CHECK_PARSE("BraceWrapping:\n"
18377               "  AfterControlStatement: true",
18378               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18379   CHECK_PARSE("BraceWrapping:\n"
18380               "  AfterControlStatement: false",
18381               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18382 
18383   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
18384   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
18385               FormatStyle::RTBS_None);
18386   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
18387               FormatStyle::RTBS_All);
18388   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
18389               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
18390   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
18391               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
18392   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
18393               AlwaysBreakAfterReturnType,
18394               FormatStyle::RTBS_TopLevelDefinitions);
18395 
18396   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
18397   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
18398               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
18399   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
18400               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18401   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
18402               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18403   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
18404               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18405   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
18406               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18407 
18408   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
18409   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
18410               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
18411   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
18412               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
18413   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
18414               AlwaysBreakAfterDefinitionReturnType,
18415               FormatStyle::DRTBS_TopLevel);
18416 
18417   Style.NamespaceIndentation = FormatStyle::NI_All;
18418   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
18419               FormatStyle::NI_None);
18420   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
18421               FormatStyle::NI_Inner);
18422   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
18423               FormatStyle::NI_All);
18424 
18425   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
18426   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
18427               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18428   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
18429               AllowShortIfStatementsOnASingleLine,
18430               FormatStyle::SIS_WithoutElse);
18431   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
18432               AllowShortIfStatementsOnASingleLine,
18433               FormatStyle::SIS_OnlyFirstIf);
18434   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
18435               AllowShortIfStatementsOnASingleLine,
18436               FormatStyle::SIS_AllIfsAndElse);
18437   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
18438               AllowShortIfStatementsOnASingleLine,
18439               FormatStyle::SIS_OnlyFirstIf);
18440   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
18441               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18442   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
18443               AllowShortIfStatementsOnASingleLine,
18444               FormatStyle::SIS_WithoutElse);
18445 
18446   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
18447   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
18448               FormatStyle::IEBS_AfterExternBlock);
18449   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
18450               FormatStyle::IEBS_Indent);
18451   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
18452               FormatStyle::IEBS_NoIndent);
18453   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
18454               FormatStyle::IEBS_Indent);
18455   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
18456               FormatStyle::IEBS_NoIndent);
18457 
18458   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
18459   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
18460               FormatStyle::BFCS_Both);
18461   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
18462               FormatStyle::BFCS_None);
18463   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
18464               FormatStyle::BFCS_Before);
18465   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
18466               FormatStyle::BFCS_After);
18467 
18468   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
18469   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
18470               FormatStyle::SJSIO_After);
18471   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
18472               FormatStyle::SJSIO_Before);
18473 
18474   // FIXME: This is required because parsing a configuration simply overwrites
18475   // the first N elements of the list instead of resetting it.
18476   Style.ForEachMacros.clear();
18477   std::vector<std::string> BoostForeach;
18478   BoostForeach.push_back("BOOST_FOREACH");
18479   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
18480   std::vector<std::string> BoostAndQForeach;
18481   BoostAndQForeach.push_back("BOOST_FOREACH");
18482   BoostAndQForeach.push_back("Q_FOREACH");
18483   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
18484               BoostAndQForeach);
18485 
18486   Style.IfMacros.clear();
18487   std::vector<std::string> CustomIfs;
18488   CustomIfs.push_back("MYIF");
18489   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
18490 
18491   Style.AttributeMacros.clear();
18492   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
18493               std::vector<std::string>{"__capability"});
18494   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
18495               std::vector<std::string>({"attr1", "attr2"}));
18496 
18497   Style.StatementAttributeLikeMacros.clear();
18498   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
18499               StatementAttributeLikeMacros,
18500               std::vector<std::string>({"emit", "Q_EMIT"}));
18501 
18502   Style.StatementMacros.clear();
18503   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
18504               std::vector<std::string>{"QUNUSED"});
18505   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
18506               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
18507 
18508   Style.NamespaceMacros.clear();
18509   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
18510               std::vector<std::string>{"TESTSUITE"});
18511   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
18512               std::vector<std::string>({"TESTSUITE", "SUITE"}));
18513 
18514   Style.WhitespaceSensitiveMacros.clear();
18515   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
18516               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
18517   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
18518               WhitespaceSensitiveMacros,
18519               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
18520   Style.WhitespaceSensitiveMacros.clear();
18521   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
18522               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
18523   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
18524               WhitespaceSensitiveMacros,
18525               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
18526 
18527   Style.IncludeStyle.IncludeCategories.clear();
18528   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
18529       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
18530   CHECK_PARSE("IncludeCategories:\n"
18531               "  - Regex: abc/.*\n"
18532               "    Priority: 2\n"
18533               "  - Regex: .*\n"
18534               "    Priority: 1\n"
18535               "    CaseSensitive: true\n",
18536               IncludeStyle.IncludeCategories, ExpectedCategories);
18537   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
18538               "abc$");
18539   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
18540               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
18541 
18542   Style.SortIncludes = FormatStyle::SI_Never;
18543   CHECK_PARSE("SortIncludes: true", SortIncludes,
18544               FormatStyle::SI_CaseSensitive);
18545   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
18546   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
18547               FormatStyle::SI_CaseInsensitive);
18548   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
18549               FormatStyle::SI_CaseSensitive);
18550   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
18551 
18552   Style.RawStringFormats.clear();
18553   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
18554       {
18555           FormatStyle::LK_TextProto,
18556           {"pb", "proto"},
18557           {"PARSE_TEXT_PROTO"},
18558           /*CanonicalDelimiter=*/"",
18559           "llvm",
18560       },
18561       {
18562           FormatStyle::LK_Cpp,
18563           {"cc", "cpp"},
18564           {"C_CODEBLOCK", "CPPEVAL"},
18565           /*CanonicalDelimiter=*/"cc",
18566           /*BasedOnStyle=*/"",
18567       },
18568   };
18569 
18570   CHECK_PARSE("RawStringFormats:\n"
18571               "  - Language: TextProto\n"
18572               "    Delimiters:\n"
18573               "      - 'pb'\n"
18574               "      - 'proto'\n"
18575               "    EnclosingFunctions:\n"
18576               "      - 'PARSE_TEXT_PROTO'\n"
18577               "    BasedOnStyle: llvm\n"
18578               "  - Language: Cpp\n"
18579               "    Delimiters:\n"
18580               "      - 'cc'\n"
18581               "      - 'cpp'\n"
18582               "    EnclosingFunctions:\n"
18583               "      - 'C_CODEBLOCK'\n"
18584               "      - 'CPPEVAL'\n"
18585               "    CanonicalDelimiter: 'cc'",
18586               RawStringFormats, ExpectedRawStringFormats);
18587 
18588   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18589               "  Minimum: 0\n"
18590               "  Maximum: 0",
18591               SpacesInLineCommentPrefix.Minimum, 0u);
18592   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
18593   Style.SpacesInLineCommentPrefix.Minimum = 1;
18594   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18595               "  Minimum: 2",
18596               SpacesInLineCommentPrefix.Minimum, 0u);
18597   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18598               "  Maximum: -1",
18599               SpacesInLineCommentPrefix.Maximum, -1u);
18600   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18601               "  Minimum: 2",
18602               SpacesInLineCommentPrefix.Minimum, 2u);
18603   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18604               "  Maximum: 1",
18605               SpacesInLineCommentPrefix.Maximum, 1u);
18606   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
18607 
18608   Style.SpacesInAngles = FormatStyle::SIAS_Always;
18609   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
18610   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
18611               FormatStyle::SIAS_Always);
18612   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
18613   // For backward compatibility:
18614   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
18615   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
18616 }
18617 
18618 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
18619   FormatStyle Style = {};
18620   Style.Language = FormatStyle::LK_Cpp;
18621   CHECK_PARSE("Language: Cpp\n"
18622               "IndentWidth: 12",
18623               IndentWidth, 12u);
18624   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
18625                                "IndentWidth: 34",
18626                                &Style),
18627             ParseError::Unsuitable);
18628   FormatStyle BinPackedTCS = {};
18629   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
18630   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
18631                                "InsertTrailingCommas: Wrapped",
18632                                &BinPackedTCS),
18633             ParseError::BinPackTrailingCommaConflict);
18634   EXPECT_EQ(12u, Style.IndentWidth);
18635   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
18636   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
18637 
18638   Style.Language = FormatStyle::LK_JavaScript;
18639   CHECK_PARSE("Language: JavaScript\n"
18640               "IndentWidth: 12",
18641               IndentWidth, 12u);
18642   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
18643   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
18644                                "IndentWidth: 34",
18645                                &Style),
18646             ParseError::Unsuitable);
18647   EXPECT_EQ(23u, Style.IndentWidth);
18648   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
18649   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
18650 
18651   CHECK_PARSE("BasedOnStyle: LLVM\n"
18652               "IndentWidth: 67",
18653               IndentWidth, 67u);
18654 
18655   CHECK_PARSE("---\n"
18656               "Language: JavaScript\n"
18657               "IndentWidth: 12\n"
18658               "---\n"
18659               "Language: Cpp\n"
18660               "IndentWidth: 34\n"
18661               "...\n",
18662               IndentWidth, 12u);
18663 
18664   Style.Language = FormatStyle::LK_Cpp;
18665   CHECK_PARSE("---\n"
18666               "Language: JavaScript\n"
18667               "IndentWidth: 12\n"
18668               "---\n"
18669               "Language: Cpp\n"
18670               "IndentWidth: 34\n"
18671               "...\n",
18672               IndentWidth, 34u);
18673   CHECK_PARSE("---\n"
18674               "IndentWidth: 78\n"
18675               "---\n"
18676               "Language: JavaScript\n"
18677               "IndentWidth: 56\n"
18678               "...\n",
18679               IndentWidth, 78u);
18680 
18681   Style.ColumnLimit = 123;
18682   Style.IndentWidth = 234;
18683   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
18684   Style.TabWidth = 345;
18685   EXPECT_FALSE(parseConfiguration("---\n"
18686                                   "IndentWidth: 456\n"
18687                                   "BreakBeforeBraces: Allman\n"
18688                                   "---\n"
18689                                   "Language: JavaScript\n"
18690                                   "IndentWidth: 111\n"
18691                                   "TabWidth: 111\n"
18692                                   "---\n"
18693                                   "Language: Cpp\n"
18694                                   "BreakBeforeBraces: Stroustrup\n"
18695                                   "TabWidth: 789\n"
18696                                   "...\n",
18697                                   &Style));
18698   EXPECT_EQ(123u, Style.ColumnLimit);
18699   EXPECT_EQ(456u, Style.IndentWidth);
18700   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
18701   EXPECT_EQ(789u, Style.TabWidth);
18702 
18703   EXPECT_EQ(parseConfiguration("---\n"
18704                                "Language: JavaScript\n"
18705                                "IndentWidth: 56\n"
18706                                "---\n"
18707                                "IndentWidth: 78\n"
18708                                "...\n",
18709                                &Style),
18710             ParseError::Error);
18711   EXPECT_EQ(parseConfiguration("---\n"
18712                                "Language: JavaScript\n"
18713                                "IndentWidth: 56\n"
18714                                "---\n"
18715                                "Language: JavaScript\n"
18716                                "IndentWidth: 78\n"
18717                                "...\n",
18718                                &Style),
18719             ParseError::Error);
18720 
18721   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
18722 }
18723 
18724 #undef CHECK_PARSE
18725 
18726 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
18727   FormatStyle Style = {};
18728   Style.Language = FormatStyle::LK_JavaScript;
18729   Style.BreakBeforeTernaryOperators = true;
18730   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
18731   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
18732 
18733   Style.BreakBeforeTernaryOperators = true;
18734   EXPECT_EQ(0, parseConfiguration("---\n"
18735                                   "BasedOnStyle: Google\n"
18736                                   "---\n"
18737                                   "Language: JavaScript\n"
18738                                   "IndentWidth: 76\n"
18739                                   "...\n",
18740                                   &Style)
18741                    .value());
18742   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
18743   EXPECT_EQ(76u, Style.IndentWidth);
18744   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
18745 }
18746 
18747 TEST_F(FormatTest, ConfigurationRoundTripTest) {
18748   FormatStyle Style = getLLVMStyle();
18749   std::string YAML = configurationAsText(Style);
18750   FormatStyle ParsedStyle = {};
18751   ParsedStyle.Language = FormatStyle::LK_Cpp;
18752   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
18753   EXPECT_EQ(Style, ParsedStyle);
18754 }
18755 
18756 TEST_F(FormatTest, WorksFor8bitEncodings) {
18757   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
18758             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
18759             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
18760             "\"\xef\xee\xf0\xf3...\"",
18761             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
18762                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
18763                    "\xef\xee\xf0\xf3...\"",
18764                    getLLVMStyleWithColumns(12)));
18765 }
18766 
18767 TEST_F(FormatTest, HandlesUTF8BOM) {
18768   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
18769   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
18770             format("\xef\xbb\xbf#include <iostream>"));
18771   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
18772             format("\xef\xbb\xbf\n#include <iostream>"));
18773 }
18774 
18775 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
18776 #if !defined(_MSC_VER)
18777 
18778 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
18779   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
18780                getLLVMStyleWithColumns(35));
18781   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
18782                getLLVMStyleWithColumns(31));
18783   verifyFormat("// Однажды в студёную зимнюю пору...",
18784                getLLVMStyleWithColumns(36));
18785   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
18786   verifyFormat("/* Однажды в студёную зимнюю пору... */",
18787                getLLVMStyleWithColumns(39));
18788   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
18789                getLLVMStyleWithColumns(35));
18790 }
18791 
18792 TEST_F(FormatTest, SplitsUTF8Strings) {
18793   // Non-printable characters' width is currently considered to be the length in
18794   // bytes in UTF8. The characters can be displayed in very different manner
18795   // (zero-width, single width with a substitution glyph, expanded to their code
18796   // (e.g. "<8d>"), so there's no single correct way to handle them.
18797   EXPECT_EQ("\"aaaaÄ\"\n"
18798             "\"\xc2\x8d\";",
18799             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
18800   EXPECT_EQ("\"aaaaaaaÄ\"\n"
18801             "\"\xc2\x8d\";",
18802             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
18803   EXPECT_EQ("\"Однажды, в \"\n"
18804             "\"студёную \"\n"
18805             "\"зимнюю \"\n"
18806             "\"пору,\"",
18807             format("\"Однажды, в студёную зимнюю пору,\"",
18808                    getLLVMStyleWithColumns(13)));
18809   EXPECT_EQ(
18810       "\"一 二 三 \"\n"
18811       "\"四 五六 \"\n"
18812       "\"七 八 九 \"\n"
18813       "\"十\"",
18814       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
18815   EXPECT_EQ("\"一\t\"\n"
18816             "\"二 \t\"\n"
18817             "\"三 四 \"\n"
18818             "\"五\t\"\n"
18819             "\"六 \t\"\n"
18820             "\"七 \"\n"
18821             "\"八九十\tqq\"",
18822             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
18823                    getLLVMStyleWithColumns(11)));
18824 
18825   // UTF8 character in an escape sequence.
18826   EXPECT_EQ("\"aaaaaa\"\n"
18827             "\"\\\xC2\x8D\"",
18828             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
18829 }
18830 
18831 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
18832   EXPECT_EQ("const char *sssss =\n"
18833             "    \"一二三四五六七八\\\n"
18834             " 九 十\";",
18835             format("const char *sssss = \"一二三四五六七八\\\n"
18836                    " 九 十\";",
18837                    getLLVMStyleWithColumns(30)));
18838 }
18839 
18840 TEST_F(FormatTest, SplitsUTF8LineComments) {
18841   EXPECT_EQ("// aaaaÄ\xc2\x8d",
18842             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
18843   EXPECT_EQ("// Я из лесу\n"
18844             "// вышел; был\n"
18845             "// сильный\n"
18846             "// мороз.",
18847             format("// Я из лесу вышел; был сильный мороз.",
18848                    getLLVMStyleWithColumns(13)));
18849   EXPECT_EQ("// 一二三\n"
18850             "// 四五六七\n"
18851             "// 八  九\n"
18852             "// 十",
18853             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
18854 }
18855 
18856 TEST_F(FormatTest, SplitsUTF8BlockComments) {
18857   EXPECT_EQ("/* Гляжу,\n"
18858             " * поднимается\n"
18859             " * медленно в\n"
18860             " * гору\n"
18861             " * Лошадка,\n"
18862             " * везущая\n"
18863             " * хворосту\n"
18864             " * воз. */",
18865             format("/* Гляжу, поднимается медленно в гору\n"
18866                    " * Лошадка, везущая хворосту воз. */",
18867                    getLLVMStyleWithColumns(13)));
18868   EXPECT_EQ(
18869       "/* 一二三\n"
18870       " * 四五六七\n"
18871       " * 八  九\n"
18872       " * 十  */",
18873       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
18874   EXPECT_EQ("/* �������� ��������\n"
18875             " * ��������\n"
18876             " * ������-�� */",
18877             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
18878 }
18879 
18880 #endif // _MSC_VER
18881 
18882 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
18883   FormatStyle Style = getLLVMStyle();
18884 
18885   Style.ConstructorInitializerIndentWidth = 4;
18886   verifyFormat(
18887       "SomeClass::Constructor()\n"
18888       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
18889       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
18890       Style);
18891 
18892   Style.ConstructorInitializerIndentWidth = 2;
18893   verifyFormat(
18894       "SomeClass::Constructor()\n"
18895       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
18896       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
18897       Style);
18898 
18899   Style.ConstructorInitializerIndentWidth = 0;
18900   verifyFormat(
18901       "SomeClass::Constructor()\n"
18902       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
18903       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
18904       Style);
18905   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18906   verifyFormat(
18907       "SomeLongTemplateVariableName<\n"
18908       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
18909       Style);
18910   verifyFormat("bool smaller = 1 < "
18911                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
18912                "                       "
18913                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
18914                Style);
18915 
18916   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
18917   verifyFormat("SomeClass::Constructor() :\n"
18918                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
18919                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
18920                Style);
18921 }
18922 
18923 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
18924   FormatStyle Style = getLLVMStyle();
18925   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
18926   Style.ConstructorInitializerIndentWidth = 4;
18927   verifyFormat("SomeClass::Constructor()\n"
18928                "    : a(a)\n"
18929                "    , b(b)\n"
18930                "    , c(c) {}",
18931                Style);
18932   verifyFormat("SomeClass::Constructor()\n"
18933                "    : a(a) {}",
18934                Style);
18935 
18936   Style.ColumnLimit = 0;
18937   verifyFormat("SomeClass::Constructor()\n"
18938                "    : a(a) {}",
18939                Style);
18940   verifyFormat("SomeClass::Constructor() noexcept\n"
18941                "    : a(a) {}",
18942                Style);
18943   verifyFormat("SomeClass::Constructor()\n"
18944                "    : a(a)\n"
18945                "    , b(b)\n"
18946                "    , c(c) {}",
18947                Style);
18948   verifyFormat("SomeClass::Constructor()\n"
18949                "    : a(a) {\n"
18950                "  foo();\n"
18951                "  bar();\n"
18952                "}",
18953                Style);
18954 
18955   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
18956   verifyFormat("SomeClass::Constructor()\n"
18957                "    : a(a)\n"
18958                "    , b(b)\n"
18959                "    , c(c) {\n}",
18960                Style);
18961   verifyFormat("SomeClass::Constructor()\n"
18962                "    : a(a) {\n}",
18963                Style);
18964 
18965   Style.ColumnLimit = 80;
18966   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
18967   Style.ConstructorInitializerIndentWidth = 2;
18968   verifyFormat("SomeClass::Constructor()\n"
18969                "  : a(a)\n"
18970                "  , b(b)\n"
18971                "  , c(c) {}",
18972                Style);
18973 
18974   Style.ConstructorInitializerIndentWidth = 0;
18975   verifyFormat("SomeClass::Constructor()\n"
18976                ": a(a)\n"
18977                ", b(b)\n"
18978                ", c(c) {}",
18979                Style);
18980 
18981   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
18982   Style.ConstructorInitializerIndentWidth = 4;
18983   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
18984   verifyFormat(
18985       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
18986       Style);
18987   verifyFormat(
18988       "SomeClass::Constructor()\n"
18989       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
18990       Style);
18991   Style.ConstructorInitializerIndentWidth = 4;
18992   Style.ColumnLimit = 60;
18993   verifyFormat("SomeClass::Constructor()\n"
18994                "    : aaaaaaaa(aaaaaaaa)\n"
18995                "    , aaaaaaaa(aaaaaaaa)\n"
18996                "    , aaaaaaaa(aaaaaaaa) {}",
18997                Style);
18998 }
18999 
19000 TEST_F(FormatTest, Destructors) {
19001   verifyFormat("void F(int &i) { i.~int(); }");
19002   verifyFormat("void F(int &i) { i->~int(); }");
19003 }
19004 
19005 TEST_F(FormatTest, FormatsWithWebKitStyle) {
19006   FormatStyle Style = getWebKitStyle();
19007 
19008   // Don't indent in outer namespaces.
19009   verifyFormat("namespace outer {\n"
19010                "int i;\n"
19011                "namespace inner {\n"
19012                "    int i;\n"
19013                "} // namespace inner\n"
19014                "} // namespace outer\n"
19015                "namespace other_outer {\n"
19016                "int i;\n"
19017                "}",
19018                Style);
19019 
19020   // Don't indent case labels.
19021   verifyFormat("switch (variable) {\n"
19022                "case 1:\n"
19023                "case 2:\n"
19024                "    doSomething();\n"
19025                "    break;\n"
19026                "default:\n"
19027                "    ++variable;\n"
19028                "}",
19029                Style);
19030 
19031   // Wrap before binary operators.
19032   EXPECT_EQ("void f()\n"
19033             "{\n"
19034             "    if (aaaaaaaaaaaaaaaa\n"
19035             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
19036             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19037             "        return;\n"
19038             "}",
19039             format("void f() {\n"
19040                    "if (aaaaaaaaaaaaaaaa\n"
19041                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
19042                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19043                    "return;\n"
19044                    "}",
19045                    Style));
19046 
19047   // Allow functions on a single line.
19048   verifyFormat("void f() { return; }", Style);
19049 
19050   // Allow empty blocks on a single line and insert a space in empty blocks.
19051   EXPECT_EQ("void f() { }", format("void f() {}", Style));
19052   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
19053   // However, don't merge non-empty short loops.
19054   EXPECT_EQ("while (true) {\n"
19055             "    continue;\n"
19056             "}",
19057             format("while (true) { continue; }", Style));
19058 
19059   // Constructor initializers are formatted one per line with the "," on the
19060   // new line.
19061   verifyFormat("Constructor()\n"
19062                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
19063                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
19064                "          aaaaaaaaaaaaaa)\n"
19065                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
19066                "{\n"
19067                "}",
19068                Style);
19069   verifyFormat("SomeClass::Constructor()\n"
19070                "    : a(a)\n"
19071                "{\n"
19072                "}",
19073                Style);
19074   EXPECT_EQ("SomeClass::Constructor()\n"
19075             "    : a(a)\n"
19076             "{\n"
19077             "}",
19078             format("SomeClass::Constructor():a(a){}", Style));
19079   verifyFormat("SomeClass::Constructor()\n"
19080                "    : a(a)\n"
19081                "    , b(b)\n"
19082                "    , c(c)\n"
19083                "{\n"
19084                "}",
19085                Style);
19086   verifyFormat("SomeClass::Constructor()\n"
19087                "    : a(a)\n"
19088                "{\n"
19089                "    foo();\n"
19090                "    bar();\n"
19091                "}",
19092                Style);
19093 
19094   // Access specifiers should be aligned left.
19095   verifyFormat("class C {\n"
19096                "public:\n"
19097                "    int i;\n"
19098                "};",
19099                Style);
19100 
19101   // Do not align comments.
19102   verifyFormat("int a; // Do not\n"
19103                "double b; // align comments.",
19104                Style);
19105 
19106   // Do not align operands.
19107   EXPECT_EQ("ASSERT(aaaa\n"
19108             "    || bbbb);",
19109             format("ASSERT ( aaaa\n||bbbb);", Style));
19110 
19111   // Accept input's line breaks.
19112   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
19113             "    || bbbbbbbbbbbbbbb) {\n"
19114             "    i++;\n"
19115             "}",
19116             format("if (aaaaaaaaaaaaaaa\n"
19117                    "|| bbbbbbbbbbbbbbb) { i++; }",
19118                    Style));
19119   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
19120             "    i++;\n"
19121             "}",
19122             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
19123 
19124   // Don't automatically break all macro definitions (llvm.org/PR17842).
19125   verifyFormat("#define aNumber 10", Style);
19126   // However, generally keep the line breaks that the user authored.
19127   EXPECT_EQ("#define aNumber \\\n"
19128             "    10",
19129             format("#define aNumber \\\n"
19130                    " 10",
19131                    Style));
19132 
19133   // Keep empty and one-element array literals on a single line.
19134   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
19135             "                                  copyItems:YES];",
19136             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
19137                    "copyItems:YES];",
19138                    Style));
19139   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
19140             "                                  copyItems:YES];",
19141             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
19142                    "             copyItems:YES];",
19143                    Style));
19144   // FIXME: This does not seem right, there should be more indentation before
19145   // the array literal's entries. Nested blocks have the same problem.
19146   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19147             "    @\"a\",\n"
19148             "    @\"a\"\n"
19149             "]\n"
19150             "                                  copyItems:YES];",
19151             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19152                    "     @\"a\",\n"
19153                    "     @\"a\"\n"
19154                    "     ]\n"
19155                    "       copyItems:YES];",
19156                    Style));
19157   EXPECT_EQ(
19158       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19159       "                                  copyItems:YES];",
19160       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19161              "   copyItems:YES];",
19162              Style));
19163 
19164   verifyFormat("[self.a b:c c:d];", Style);
19165   EXPECT_EQ("[self.a b:c\n"
19166             "        c:d];",
19167             format("[self.a b:c\n"
19168                    "c:d];",
19169                    Style));
19170 }
19171 
19172 TEST_F(FormatTest, FormatsLambdas) {
19173   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
19174   verifyFormat(
19175       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
19176   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
19177   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
19178   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
19179   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
19180   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
19181   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
19182   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
19183   verifyFormat("int x = f(*+[] {});");
19184   verifyFormat("void f() {\n"
19185                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
19186                "}\n");
19187   verifyFormat("void f() {\n"
19188                "  other(x.begin(), //\n"
19189                "        x.end(),   //\n"
19190                "        [&](int, int) { return 1; });\n"
19191                "}\n");
19192   verifyFormat("void f() {\n"
19193                "  other.other.other.other.other(\n"
19194                "      x.begin(), x.end(),\n"
19195                "      [something, rather](int, int, int, int, int, int, int) { "
19196                "return 1; });\n"
19197                "}\n");
19198   verifyFormat(
19199       "void f() {\n"
19200       "  other.other.other.other.other(\n"
19201       "      x.begin(), x.end(),\n"
19202       "      [something, rather](int, int, int, int, int, int, int) {\n"
19203       "        //\n"
19204       "      });\n"
19205       "}\n");
19206   verifyFormat("SomeFunction([]() { // A cool function...\n"
19207                "  return 43;\n"
19208                "});");
19209   EXPECT_EQ("SomeFunction([]() {\n"
19210             "#define A a\n"
19211             "  return 43;\n"
19212             "});",
19213             format("SomeFunction([](){\n"
19214                    "#define A a\n"
19215                    "return 43;\n"
19216                    "});"));
19217   verifyFormat("void f() {\n"
19218                "  SomeFunction([](decltype(x), A *a) {});\n"
19219                "  SomeFunction([](typeof(x), A *a) {});\n"
19220                "  SomeFunction([](_Atomic(x), A *a) {});\n"
19221                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
19222                "}");
19223   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19224                "    [](const aaaaaaaaaa &a) { return a; });");
19225   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
19226                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
19227                "});");
19228   verifyFormat("Constructor()\n"
19229                "    : Field([] { // comment\n"
19230                "        int i;\n"
19231                "      }) {}");
19232   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
19233                "  return some_parameter.size();\n"
19234                "};");
19235   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
19236                "    [](const string &s) { return s; };");
19237   verifyFormat("int i = aaaaaa ? 1 //\n"
19238                "               : [] {\n"
19239                "                   return 2; //\n"
19240                "                 }();");
19241   verifyFormat("llvm::errs() << \"number of twos is \"\n"
19242                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
19243                "                  return x == 2; // force break\n"
19244                "                });");
19245   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19246                "    [=](int iiiiiiiiiiii) {\n"
19247                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
19248                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
19249                "    });",
19250                getLLVMStyleWithColumns(60));
19251 
19252   verifyFormat("SomeFunction({[&] {\n"
19253                "                // comment\n"
19254                "              },\n"
19255                "              [&] {\n"
19256                "                // comment\n"
19257                "              }});");
19258   verifyFormat("SomeFunction({[&] {\n"
19259                "  // comment\n"
19260                "}});");
19261   verifyFormat(
19262       "virtual aaaaaaaaaaaaaaaa(\n"
19263       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
19264       "    aaaaa aaaaaaaaa);");
19265 
19266   // Lambdas with return types.
19267   verifyFormat("int c = []() -> int { return 2; }();\n");
19268   verifyFormat("int c = []() -> int * { return 2; }();\n");
19269   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
19270   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
19271   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
19272   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
19273   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
19274   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
19275   verifyFormat("[a, a]() -> a<1> {};");
19276   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
19277   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
19278   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
19279   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
19280   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
19281   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
19282   verifyFormat("[]() -> foo<!5> { return {}; };");
19283   verifyFormat("[]() -> foo<~5> { return {}; };");
19284   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
19285   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
19286   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
19287   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
19288   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
19289   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
19290   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
19291   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
19292   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
19293   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
19294   verifyFormat("namespace bar {\n"
19295                "// broken:\n"
19296                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
19297                "} // namespace bar");
19298   verifyFormat("namespace bar {\n"
19299                "// broken:\n"
19300                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
19301                "} // namespace bar");
19302   verifyFormat("namespace bar {\n"
19303                "// broken:\n"
19304                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
19305                "} // namespace bar");
19306   verifyFormat("namespace bar {\n"
19307                "// broken:\n"
19308                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
19309                "} // namespace bar");
19310   verifyFormat("namespace bar {\n"
19311                "// broken:\n"
19312                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
19313                "} // namespace bar");
19314   verifyFormat("namespace bar {\n"
19315                "// broken:\n"
19316                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
19317                "} // namespace bar");
19318   verifyFormat("namespace bar {\n"
19319                "// broken:\n"
19320                "auto foo{[]() -> foo<!5> { return {}; }};\n"
19321                "} // namespace bar");
19322   verifyFormat("namespace bar {\n"
19323                "// broken:\n"
19324                "auto foo{[]() -> foo<~5> { return {}; }};\n"
19325                "} // namespace bar");
19326   verifyFormat("namespace bar {\n"
19327                "// broken:\n"
19328                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
19329                "} // namespace bar");
19330   verifyFormat("namespace bar {\n"
19331                "// broken:\n"
19332                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
19333                "} // namespace bar");
19334   verifyFormat("namespace bar {\n"
19335                "// broken:\n"
19336                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
19337                "} // namespace bar");
19338   verifyFormat("namespace bar {\n"
19339                "// broken:\n"
19340                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
19341                "} // namespace bar");
19342   verifyFormat("namespace bar {\n"
19343                "// broken:\n"
19344                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
19345                "} // namespace bar");
19346   verifyFormat("namespace bar {\n"
19347                "// broken:\n"
19348                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
19349                "} // namespace bar");
19350   verifyFormat("namespace bar {\n"
19351                "// broken:\n"
19352                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
19353                "} // namespace bar");
19354   verifyFormat("namespace bar {\n"
19355                "// broken:\n"
19356                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
19357                "} // namespace bar");
19358   verifyFormat("namespace bar {\n"
19359                "// broken:\n"
19360                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
19361                "} // namespace bar");
19362   verifyFormat("namespace bar {\n"
19363                "// broken:\n"
19364                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
19365                "} // namespace bar");
19366   verifyFormat("[]() -> a<1> {};");
19367   verifyFormat("[]() -> a<1> { ; };");
19368   verifyFormat("[]() -> a<1> { ; }();");
19369   verifyFormat("[a, a]() -> a<true> {};");
19370   verifyFormat("[]() -> a<true> {};");
19371   verifyFormat("[]() -> a<true> { ; };");
19372   verifyFormat("[]() -> a<true> { ; }();");
19373   verifyFormat("[a, a]() -> a<false> {};");
19374   verifyFormat("[]() -> a<false> {};");
19375   verifyFormat("[]() -> a<false> { ; };");
19376   verifyFormat("[]() -> a<false> { ; }();");
19377   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
19378   verifyFormat("namespace bar {\n"
19379                "auto foo{[]() -> foo<false> { ; }};\n"
19380                "} // namespace bar");
19381   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
19382                "                   int j) -> int {\n"
19383                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
19384                "};");
19385   verifyFormat(
19386       "aaaaaaaaaaaaaaaaaaaaaa(\n"
19387       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
19388       "      return aaaaaaaaaaaaaaaaa;\n"
19389       "    });",
19390       getLLVMStyleWithColumns(70));
19391   verifyFormat("[]() //\n"
19392                "    -> int {\n"
19393                "  return 1; //\n"
19394                "};");
19395   verifyFormat("[]() -> Void<T...> {};");
19396   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
19397 
19398   // Lambdas with explicit template argument lists.
19399   verifyFormat(
19400       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
19401 
19402   // Multiple lambdas in the same parentheses change indentation rules. These
19403   // lambdas are forced to start on new lines.
19404   verifyFormat("SomeFunction(\n"
19405                "    []() {\n"
19406                "      //\n"
19407                "    },\n"
19408                "    []() {\n"
19409                "      //\n"
19410                "    });");
19411 
19412   // A lambda passed as arg0 is always pushed to the next line.
19413   verifyFormat("SomeFunction(\n"
19414                "    [this] {\n"
19415                "      //\n"
19416                "    },\n"
19417                "    1);\n");
19418 
19419   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
19420   // the arg0 case above.
19421   auto Style = getGoogleStyle();
19422   Style.BinPackArguments = false;
19423   verifyFormat("SomeFunction(\n"
19424                "    a,\n"
19425                "    [this] {\n"
19426                "      //\n"
19427                "    },\n"
19428                "    b);\n",
19429                Style);
19430   verifyFormat("SomeFunction(\n"
19431                "    a,\n"
19432                "    [this] {\n"
19433                "      //\n"
19434                "    },\n"
19435                "    b);\n");
19436 
19437   // A lambda with a very long line forces arg0 to be pushed out irrespective of
19438   // the BinPackArguments value (as long as the code is wide enough).
19439   verifyFormat(
19440       "something->SomeFunction(\n"
19441       "    a,\n"
19442       "    [this] {\n"
19443       "      "
19444       "D0000000000000000000000000000000000000000000000000000000000001();\n"
19445       "    },\n"
19446       "    b);\n");
19447 
19448   // A multi-line lambda is pulled up as long as the introducer fits on the
19449   // previous line and there are no further args.
19450   verifyFormat("function(1, [this, that] {\n"
19451                "  //\n"
19452                "});\n");
19453   verifyFormat("function([this, that] {\n"
19454                "  //\n"
19455                "});\n");
19456   // FIXME: this format is not ideal and we should consider forcing the first
19457   // arg onto its own line.
19458   verifyFormat("function(a, b, c, //\n"
19459                "         d, [this, that] {\n"
19460                "           //\n"
19461                "         });\n");
19462 
19463   // Multiple lambdas are treated correctly even when there is a short arg0.
19464   verifyFormat("SomeFunction(\n"
19465                "    1,\n"
19466                "    [this] {\n"
19467                "      //\n"
19468                "    },\n"
19469                "    [this] {\n"
19470                "      //\n"
19471                "    },\n"
19472                "    1);\n");
19473 
19474   // More complex introducers.
19475   verifyFormat("return [i, args...] {};");
19476 
19477   // Not lambdas.
19478   verifyFormat("constexpr char hello[]{\"hello\"};");
19479   verifyFormat("double &operator[](int i) { return 0; }\n"
19480                "int i;");
19481   verifyFormat("std::unique_ptr<int[]> foo() {}");
19482   verifyFormat("int i = a[a][a]->f();");
19483   verifyFormat("int i = (*b)[a]->f();");
19484 
19485   // Other corner cases.
19486   verifyFormat("void f() {\n"
19487                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
19488                "  );\n"
19489                "}");
19490 
19491   // Lambdas created through weird macros.
19492   verifyFormat("void f() {\n"
19493                "  MACRO((const AA &a) { return 1; });\n"
19494                "  MACRO((AA &a) { return 1; });\n"
19495                "}");
19496 
19497   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
19498                "      doo_dah();\n"
19499                "      doo_dah();\n"
19500                "    })) {\n"
19501                "}");
19502   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
19503                "                doo_dah();\n"
19504                "                doo_dah();\n"
19505                "              })) {\n"
19506                "}");
19507   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
19508                "                doo_dah();\n"
19509                "                doo_dah();\n"
19510                "              })) {\n"
19511                "}");
19512   verifyFormat("auto lambda = []() {\n"
19513                "  int a = 2\n"
19514                "#if A\n"
19515                "          + 2\n"
19516                "#endif\n"
19517                "      ;\n"
19518                "};");
19519 
19520   // Lambdas with complex multiline introducers.
19521   verifyFormat(
19522       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19523       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
19524       "        -> ::std::unordered_set<\n"
19525       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
19526       "      //\n"
19527       "    });");
19528 
19529   FormatStyle DoNotMerge = getLLVMStyle();
19530   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
19531   verifyFormat("auto c = []() {\n"
19532                "  return b;\n"
19533                "};",
19534                "auto c = []() { return b; };", DoNotMerge);
19535   verifyFormat("auto c = []() {\n"
19536                "};",
19537                " auto c = []() {};", DoNotMerge);
19538 
19539   FormatStyle MergeEmptyOnly = getLLVMStyle();
19540   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
19541   verifyFormat("auto c = []() {\n"
19542                "  return b;\n"
19543                "};",
19544                "auto c = []() {\n"
19545                "  return b;\n"
19546                " };",
19547                MergeEmptyOnly);
19548   verifyFormat("auto c = []() {};",
19549                "auto c = []() {\n"
19550                "};",
19551                MergeEmptyOnly);
19552 
19553   FormatStyle MergeInline = getLLVMStyle();
19554   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
19555   verifyFormat("auto c = []() {\n"
19556                "  return b;\n"
19557                "};",
19558                "auto c = []() { return b; };", MergeInline);
19559   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
19560                MergeInline);
19561   verifyFormat("function([]() { return b; }, a)",
19562                "function([]() { return b; }, a)", MergeInline);
19563   verifyFormat("function(a, []() { return b; })",
19564                "function(a, []() { return b; })", MergeInline);
19565 
19566   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
19567   // AllowShortLambdasOnASingleLine
19568   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
19569   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
19570   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
19571   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19572       FormatStyle::ShortLambdaStyle::SLS_None;
19573   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
19574                "    []()\n"
19575                "    {\n"
19576                "      return 17;\n"
19577                "    });",
19578                LLVMWithBeforeLambdaBody);
19579   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
19580                "    []()\n"
19581                "    {\n"
19582                "    });",
19583                LLVMWithBeforeLambdaBody);
19584   verifyFormat("auto fct_SLS_None = []()\n"
19585                "{\n"
19586                "  return 17;\n"
19587                "};",
19588                LLVMWithBeforeLambdaBody);
19589   verifyFormat("TwoNestedLambdas_SLS_None(\n"
19590                "    []()\n"
19591                "    {\n"
19592                "      return Call(\n"
19593                "          []()\n"
19594                "          {\n"
19595                "            return 17;\n"
19596                "          });\n"
19597                "    });",
19598                LLVMWithBeforeLambdaBody);
19599   verifyFormat("void Fct()\n"
19600                "{\n"
19601                "  return {[]()\n"
19602                "          {\n"
19603                "            return 17;\n"
19604                "          }};\n"
19605                "}",
19606                LLVMWithBeforeLambdaBody);
19607 
19608   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19609       FormatStyle::ShortLambdaStyle::SLS_Empty;
19610   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
19611                "    []()\n"
19612                "    {\n"
19613                "      return 17;\n"
19614                "    });",
19615                LLVMWithBeforeLambdaBody);
19616   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
19617                LLVMWithBeforeLambdaBody);
19618   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
19619                "ongFunctionName_SLS_Empty(\n"
19620                "    []() {});",
19621                LLVMWithBeforeLambdaBody);
19622   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
19623                "                                []()\n"
19624                "                                {\n"
19625                "                                  return 17;\n"
19626                "                                });",
19627                LLVMWithBeforeLambdaBody);
19628   verifyFormat("auto fct_SLS_Empty = []()\n"
19629                "{\n"
19630                "  return 17;\n"
19631                "};",
19632                LLVMWithBeforeLambdaBody);
19633   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
19634                "    []()\n"
19635                "    {\n"
19636                "      return Call([]() {});\n"
19637                "    });",
19638                LLVMWithBeforeLambdaBody);
19639   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
19640                "                           []()\n"
19641                "                           {\n"
19642                "                             return Call([]() {});\n"
19643                "                           });",
19644                LLVMWithBeforeLambdaBody);
19645   verifyFormat(
19646       "FctWithLongLineInLambda_SLS_Empty(\n"
19647       "    []()\n"
19648       "    {\n"
19649       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19650       "                               AndShouldNotBeConsiderAsInline,\n"
19651       "                               LambdaBodyMustBeBreak);\n"
19652       "    });",
19653       LLVMWithBeforeLambdaBody);
19654 
19655   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19656       FormatStyle::ShortLambdaStyle::SLS_Inline;
19657   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
19658                LLVMWithBeforeLambdaBody);
19659   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
19660                LLVMWithBeforeLambdaBody);
19661   verifyFormat("auto fct_SLS_Inline = []()\n"
19662                "{\n"
19663                "  return 17;\n"
19664                "};",
19665                LLVMWithBeforeLambdaBody);
19666   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
19667                "17; }); });",
19668                LLVMWithBeforeLambdaBody);
19669   verifyFormat(
19670       "FctWithLongLineInLambda_SLS_Inline(\n"
19671       "    []()\n"
19672       "    {\n"
19673       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19674       "                               AndShouldNotBeConsiderAsInline,\n"
19675       "                               LambdaBodyMustBeBreak);\n"
19676       "    });",
19677       LLVMWithBeforeLambdaBody);
19678   verifyFormat("FctWithMultipleParams_SLS_Inline("
19679                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
19680                "                                 []() { return 17; });",
19681                LLVMWithBeforeLambdaBody);
19682   verifyFormat(
19683       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
19684       LLVMWithBeforeLambdaBody);
19685 
19686   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19687       FormatStyle::ShortLambdaStyle::SLS_All;
19688   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
19689                LLVMWithBeforeLambdaBody);
19690   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
19691                LLVMWithBeforeLambdaBody);
19692   verifyFormat("auto fct_SLS_All = []() { return 17; };",
19693                LLVMWithBeforeLambdaBody);
19694   verifyFormat("FctWithOneParam_SLS_All(\n"
19695                "    []()\n"
19696                "    {\n"
19697                "      // A cool function...\n"
19698                "      return 43;\n"
19699                "    });",
19700                LLVMWithBeforeLambdaBody);
19701   verifyFormat("FctWithMultipleParams_SLS_All("
19702                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
19703                "                              []() { return 17; });",
19704                LLVMWithBeforeLambdaBody);
19705   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
19706                LLVMWithBeforeLambdaBody);
19707   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
19708                LLVMWithBeforeLambdaBody);
19709   verifyFormat(
19710       "FctWithLongLineInLambda_SLS_All(\n"
19711       "    []()\n"
19712       "    {\n"
19713       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19714       "                               AndShouldNotBeConsiderAsInline,\n"
19715       "                               LambdaBodyMustBeBreak);\n"
19716       "    });",
19717       LLVMWithBeforeLambdaBody);
19718   verifyFormat(
19719       "auto fct_SLS_All = []()\n"
19720       "{\n"
19721       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19722       "                           AndShouldNotBeConsiderAsInline,\n"
19723       "                           LambdaBodyMustBeBreak);\n"
19724       "};",
19725       LLVMWithBeforeLambdaBody);
19726   LLVMWithBeforeLambdaBody.BinPackParameters = false;
19727   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
19728                LLVMWithBeforeLambdaBody);
19729   verifyFormat(
19730       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
19731       "                                FirstParam,\n"
19732       "                                SecondParam,\n"
19733       "                                ThirdParam,\n"
19734       "                                FourthParam);",
19735       LLVMWithBeforeLambdaBody);
19736   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19737                "    []() { return "
19738                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
19739                "    FirstParam,\n"
19740                "    SecondParam,\n"
19741                "    ThirdParam,\n"
19742                "    FourthParam);",
19743                LLVMWithBeforeLambdaBody);
19744   verifyFormat(
19745       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
19746       "                                SecondParam,\n"
19747       "                                ThirdParam,\n"
19748       "                                FourthParam,\n"
19749       "                                []() { return SomeValueNotSoLong; });",
19750       LLVMWithBeforeLambdaBody);
19751   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19752                "    []()\n"
19753                "    {\n"
19754                "      return "
19755                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
19756                "eConsiderAsInline;\n"
19757                "    });",
19758                LLVMWithBeforeLambdaBody);
19759   verifyFormat(
19760       "FctWithLongLineInLambda_SLS_All(\n"
19761       "    []()\n"
19762       "    {\n"
19763       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19764       "                               AndShouldNotBeConsiderAsInline,\n"
19765       "                               LambdaBodyMustBeBreak);\n"
19766       "    });",
19767       LLVMWithBeforeLambdaBody);
19768   verifyFormat("FctWithTwoParams_SLS_All(\n"
19769                "    []()\n"
19770                "    {\n"
19771                "      // A cool function...\n"
19772                "      return 43;\n"
19773                "    },\n"
19774                "    87);",
19775                LLVMWithBeforeLambdaBody);
19776   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
19777                LLVMWithBeforeLambdaBody);
19778   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
19779                LLVMWithBeforeLambdaBody);
19780   verifyFormat(
19781       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
19782       LLVMWithBeforeLambdaBody);
19783   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
19784                "}); }, x);",
19785                LLVMWithBeforeLambdaBody);
19786   verifyFormat("TwoNestedLambdas_SLS_All(\n"
19787                "    []()\n"
19788                "    {\n"
19789                "      // A cool function...\n"
19790                "      return Call([]() { return 17; });\n"
19791                "    });",
19792                LLVMWithBeforeLambdaBody);
19793   verifyFormat("TwoNestedLambdas_SLS_All(\n"
19794                "    []()\n"
19795                "    {\n"
19796                "      return Call(\n"
19797                "          []()\n"
19798                "          {\n"
19799                "            // A cool function...\n"
19800                "            return 17;\n"
19801                "          });\n"
19802                "    });",
19803                LLVMWithBeforeLambdaBody);
19804 
19805   // Lambdas with different indentation styles.
19806   Style = getLLVMStyleWithColumns(100);
19807   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
19808             "  return promise.then(\n"
19809             "      [this, &someVariable, someObject = "
19810             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
19811             "        return someObject.startAsyncAction().then(\n"
19812             "            [this, &someVariable](AsyncActionResult result) "
19813             "mutable { result.processMore(); });\n"
19814             "      });\n"
19815             "}\n",
19816             format("SomeResult doSomething(SomeObject promise) {\n"
19817                    "  return promise.then([this, &someVariable, someObject = "
19818                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
19819                    "    return someObject.startAsyncAction().then([this, "
19820                    "&someVariable](AsyncActionResult result) mutable {\n"
19821                    "      result.processMore();\n"
19822                    "    });\n"
19823                    "  });\n"
19824                    "}\n",
19825                    Style));
19826   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
19827   verifyFormat("test() {\n"
19828                "  ([]() -> {\n"
19829                "    int b = 32;\n"
19830                "    return 3;\n"
19831                "  }).foo();\n"
19832                "}",
19833                Style);
19834   verifyFormat("test() {\n"
19835                "  []() -> {\n"
19836                "    int b = 32;\n"
19837                "    return 3;\n"
19838                "  }\n"
19839                "}",
19840                Style);
19841   verifyFormat("std::sort(v.begin(), v.end(),\n"
19842                "          [](const auto &someLongArgumentName, const auto "
19843                "&someOtherLongArgumentName) {\n"
19844                "  return someLongArgumentName.someMemberVariable < "
19845                "someOtherLongArgumentName.someMemberVariable;\n"
19846                "});",
19847                Style);
19848   verifyFormat("test() {\n"
19849                "  (\n"
19850                "      []() -> {\n"
19851                "        int b = 32;\n"
19852                "        return 3;\n"
19853                "      },\n"
19854                "      foo, bar)\n"
19855                "      .foo();\n"
19856                "}",
19857                Style);
19858   verifyFormat("test() {\n"
19859                "  ([]() -> {\n"
19860                "    int b = 32;\n"
19861                "    return 3;\n"
19862                "  })\n"
19863                "      .foo()\n"
19864                "      .bar();\n"
19865                "}",
19866                Style);
19867   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
19868             "  return promise.then(\n"
19869             "      [this, &someVariable, someObject = "
19870             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
19871             "    return someObject.startAsyncAction().then(\n"
19872             "        [this, &someVariable](AsyncActionResult result) mutable { "
19873             "result.processMore(); });\n"
19874             "  });\n"
19875             "}\n",
19876             format("SomeResult doSomething(SomeObject promise) {\n"
19877                    "  return promise.then([this, &someVariable, someObject = "
19878                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
19879                    "    return someObject.startAsyncAction().then([this, "
19880                    "&someVariable](AsyncActionResult result) mutable {\n"
19881                    "      result.processMore();\n"
19882                    "    });\n"
19883                    "  });\n"
19884                    "}\n",
19885                    Style));
19886   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
19887             "  return promise.then([this, &someVariable] {\n"
19888             "    return someObject.startAsyncAction().then(\n"
19889             "        [this, &someVariable](AsyncActionResult result) mutable { "
19890             "result.processMore(); });\n"
19891             "  });\n"
19892             "}\n",
19893             format("SomeResult doSomething(SomeObject promise) {\n"
19894                    "  return promise.then([this, &someVariable] {\n"
19895                    "    return someObject.startAsyncAction().then([this, "
19896                    "&someVariable](AsyncActionResult result) mutable {\n"
19897                    "      result.processMore();\n"
19898                    "    });\n"
19899                    "  });\n"
19900                    "}\n",
19901                    Style));
19902   Style = getGoogleStyle();
19903   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
19904   EXPECT_EQ("#define A                                       \\\n"
19905             "  [] {                                          \\\n"
19906             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
19907             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
19908             "      }",
19909             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
19910                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
19911                    Style));
19912   // TODO: The current formatting has a minor issue that's not worth fixing
19913   // right now whereby the closing brace is indented relative to the signature
19914   // instead of being aligned. This only happens with macros.
19915 }
19916 
19917 TEST_F(FormatTest, LambdaWithLineComments) {
19918   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
19919   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
19920   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
19921   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19922       FormatStyle::ShortLambdaStyle::SLS_All;
19923 
19924   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
19925   verifyFormat("auto k = []() // comment\n"
19926                "{ return; }",
19927                LLVMWithBeforeLambdaBody);
19928   verifyFormat("auto k = []() /* comment */ { return; }",
19929                LLVMWithBeforeLambdaBody);
19930   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
19931                LLVMWithBeforeLambdaBody);
19932   verifyFormat("auto k = []() // X\n"
19933                "{ return; }",
19934                LLVMWithBeforeLambdaBody);
19935   verifyFormat(
19936       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
19937       "{ return; }",
19938       LLVMWithBeforeLambdaBody);
19939 }
19940 
19941 TEST_F(FormatTest, EmptyLinesInLambdas) {
19942   verifyFormat("auto lambda = []() {\n"
19943                "  x(); //\n"
19944                "};",
19945                "auto lambda = []() {\n"
19946                "\n"
19947                "  x(); //\n"
19948                "\n"
19949                "};");
19950 }
19951 
19952 TEST_F(FormatTest, FormatsBlocks) {
19953   FormatStyle ShortBlocks = getLLVMStyle();
19954   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
19955   verifyFormat("int (^Block)(int, int);", ShortBlocks);
19956   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
19957   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
19958   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
19959   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
19960   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
19961 
19962   verifyFormat("foo(^{ bar(); });", ShortBlocks);
19963   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
19964   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
19965 
19966   verifyFormat("[operation setCompletionBlock:^{\n"
19967                "  [self onOperationDone];\n"
19968                "}];");
19969   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
19970                "  [self onOperationDone];\n"
19971                "}]};");
19972   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
19973                "  f();\n"
19974                "}];");
19975   verifyFormat("int a = [operation block:^int(int *i) {\n"
19976                "  return 1;\n"
19977                "}];");
19978   verifyFormat("[myObject doSomethingWith:arg1\n"
19979                "                      aaa:^int(int *a) {\n"
19980                "                        return 1;\n"
19981                "                      }\n"
19982                "                      bbb:f(a * bbbbbbbb)];");
19983 
19984   verifyFormat("[operation setCompletionBlock:^{\n"
19985                "  [self.delegate newDataAvailable];\n"
19986                "}];",
19987                getLLVMStyleWithColumns(60));
19988   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
19989                "  NSString *path = [self sessionFilePath];\n"
19990                "  if (path) {\n"
19991                "    // ...\n"
19992                "  }\n"
19993                "});");
19994   verifyFormat("[[SessionService sharedService]\n"
19995                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
19996                "      if (window) {\n"
19997                "        [self windowDidLoad:window];\n"
19998                "      } else {\n"
19999                "        [self errorLoadingWindow];\n"
20000                "      }\n"
20001                "    }];");
20002   verifyFormat("void (^largeBlock)(void) = ^{\n"
20003                "  // ...\n"
20004                "};\n",
20005                getLLVMStyleWithColumns(40));
20006   verifyFormat("[[SessionService sharedService]\n"
20007                "    loadWindowWithCompletionBlock: //\n"
20008                "        ^(SessionWindow *window) {\n"
20009                "          if (window) {\n"
20010                "            [self windowDidLoad:window];\n"
20011                "          } else {\n"
20012                "            [self errorLoadingWindow];\n"
20013                "          }\n"
20014                "        }];",
20015                getLLVMStyleWithColumns(60));
20016   verifyFormat("[myObject doSomethingWith:arg1\n"
20017                "    firstBlock:^(Foo *a) {\n"
20018                "      // ...\n"
20019                "      int i;\n"
20020                "    }\n"
20021                "    secondBlock:^(Bar *b) {\n"
20022                "      // ...\n"
20023                "      int i;\n"
20024                "    }\n"
20025                "    thirdBlock:^Foo(Bar *b) {\n"
20026                "      // ...\n"
20027                "      int i;\n"
20028                "    }];");
20029   verifyFormat("[myObject doSomethingWith:arg1\n"
20030                "               firstBlock:-1\n"
20031                "              secondBlock:^(Bar *b) {\n"
20032                "                // ...\n"
20033                "                int i;\n"
20034                "              }];");
20035 
20036   verifyFormat("f(^{\n"
20037                "  @autoreleasepool {\n"
20038                "    if (a) {\n"
20039                "      g();\n"
20040                "    }\n"
20041                "  }\n"
20042                "});");
20043   verifyFormat("Block b = ^int *(A *a, B *b) {}");
20044   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
20045                "};");
20046 
20047   FormatStyle FourIndent = getLLVMStyle();
20048   FourIndent.ObjCBlockIndentWidth = 4;
20049   verifyFormat("[operation setCompletionBlock:^{\n"
20050                "    [self onOperationDone];\n"
20051                "}];",
20052                FourIndent);
20053 }
20054 
20055 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
20056   FormatStyle ZeroColumn = getLLVMStyle();
20057   ZeroColumn.ColumnLimit = 0;
20058 
20059   verifyFormat("[[SessionService sharedService] "
20060                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20061                "  if (window) {\n"
20062                "    [self windowDidLoad:window];\n"
20063                "  } else {\n"
20064                "    [self errorLoadingWindow];\n"
20065                "  }\n"
20066                "}];",
20067                ZeroColumn);
20068   EXPECT_EQ("[[SessionService sharedService]\n"
20069             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20070             "      if (window) {\n"
20071             "        [self windowDidLoad:window];\n"
20072             "      } else {\n"
20073             "        [self errorLoadingWindow];\n"
20074             "      }\n"
20075             "    }];",
20076             format("[[SessionService sharedService]\n"
20077                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20078                    "                if (window) {\n"
20079                    "    [self windowDidLoad:window];\n"
20080                    "  } else {\n"
20081                    "    [self errorLoadingWindow];\n"
20082                    "  }\n"
20083                    "}];",
20084                    ZeroColumn));
20085   verifyFormat("[myObject doSomethingWith:arg1\n"
20086                "    firstBlock:^(Foo *a) {\n"
20087                "      // ...\n"
20088                "      int i;\n"
20089                "    }\n"
20090                "    secondBlock:^(Bar *b) {\n"
20091                "      // ...\n"
20092                "      int i;\n"
20093                "    }\n"
20094                "    thirdBlock:^Foo(Bar *b) {\n"
20095                "      // ...\n"
20096                "      int i;\n"
20097                "    }];",
20098                ZeroColumn);
20099   verifyFormat("f(^{\n"
20100                "  @autoreleasepool {\n"
20101                "    if (a) {\n"
20102                "      g();\n"
20103                "    }\n"
20104                "  }\n"
20105                "});",
20106                ZeroColumn);
20107   verifyFormat("void (^largeBlock)(void) = ^{\n"
20108                "  // ...\n"
20109                "};",
20110                ZeroColumn);
20111 
20112   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20113   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
20114             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20115   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
20116   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
20117             "  int i;\n"
20118             "};",
20119             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20120 }
20121 
20122 TEST_F(FormatTest, SupportsCRLF) {
20123   EXPECT_EQ("int a;\r\n"
20124             "int b;\r\n"
20125             "int c;\r\n",
20126             format("int a;\r\n"
20127                    "  int b;\r\n"
20128                    "    int c;\r\n",
20129                    getLLVMStyle()));
20130   EXPECT_EQ("int a;\r\n"
20131             "int b;\r\n"
20132             "int c;\r\n",
20133             format("int a;\r\n"
20134                    "  int b;\n"
20135                    "    int c;\r\n",
20136                    getLLVMStyle()));
20137   EXPECT_EQ("int a;\n"
20138             "int b;\n"
20139             "int c;\n",
20140             format("int a;\r\n"
20141                    "  int b;\n"
20142                    "    int c;\n",
20143                    getLLVMStyle()));
20144   EXPECT_EQ("\"aaaaaaa \"\r\n"
20145             "\"bbbbbbb\";\r\n",
20146             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
20147   EXPECT_EQ("#define A \\\r\n"
20148             "  b;      \\\r\n"
20149             "  c;      \\\r\n"
20150             "  d;\r\n",
20151             format("#define A \\\r\n"
20152                    "  b; \\\r\n"
20153                    "  c; d; \r\n",
20154                    getGoogleStyle()));
20155 
20156   EXPECT_EQ("/*\r\n"
20157             "multi line block comments\r\n"
20158             "should not introduce\r\n"
20159             "an extra carriage return\r\n"
20160             "*/\r\n",
20161             format("/*\r\n"
20162                    "multi line block comments\r\n"
20163                    "should not introduce\r\n"
20164                    "an extra carriage return\r\n"
20165                    "*/\r\n"));
20166   EXPECT_EQ("/*\r\n"
20167             "\r\n"
20168             "*/",
20169             format("/*\r\n"
20170                    "    \r\r\r\n"
20171                    "*/"));
20172 
20173   FormatStyle style = getLLVMStyle();
20174 
20175   style.DeriveLineEnding = true;
20176   style.UseCRLF = false;
20177   EXPECT_EQ("union FooBarBazQux {\n"
20178             "  int foo;\n"
20179             "  int bar;\n"
20180             "  int baz;\n"
20181             "};",
20182             format("union FooBarBazQux {\r\n"
20183                    "  int foo;\n"
20184                    "  int bar;\r\n"
20185                    "  int baz;\n"
20186                    "};",
20187                    style));
20188   style.UseCRLF = true;
20189   EXPECT_EQ("union FooBarBazQux {\r\n"
20190             "  int foo;\r\n"
20191             "  int bar;\r\n"
20192             "  int baz;\r\n"
20193             "};",
20194             format("union FooBarBazQux {\r\n"
20195                    "  int foo;\n"
20196                    "  int bar;\r\n"
20197                    "  int baz;\n"
20198                    "};",
20199                    style));
20200 
20201   style.DeriveLineEnding = false;
20202   style.UseCRLF = false;
20203   EXPECT_EQ("union FooBarBazQux {\n"
20204             "  int foo;\n"
20205             "  int bar;\n"
20206             "  int baz;\n"
20207             "  int qux;\n"
20208             "};",
20209             format("union FooBarBazQux {\r\n"
20210                    "  int foo;\n"
20211                    "  int bar;\r\n"
20212                    "  int baz;\n"
20213                    "  int qux;\r\n"
20214                    "};",
20215                    style));
20216   style.UseCRLF = true;
20217   EXPECT_EQ("union FooBarBazQux {\r\n"
20218             "  int foo;\r\n"
20219             "  int bar;\r\n"
20220             "  int baz;\r\n"
20221             "  int qux;\r\n"
20222             "};",
20223             format("union FooBarBazQux {\r\n"
20224                    "  int foo;\n"
20225                    "  int bar;\r\n"
20226                    "  int baz;\n"
20227                    "  int qux;\n"
20228                    "};",
20229                    style));
20230 
20231   style.DeriveLineEnding = true;
20232   style.UseCRLF = false;
20233   EXPECT_EQ("union FooBarBazQux {\r\n"
20234             "  int foo;\r\n"
20235             "  int bar;\r\n"
20236             "  int baz;\r\n"
20237             "  int qux;\r\n"
20238             "};",
20239             format("union FooBarBazQux {\r\n"
20240                    "  int foo;\n"
20241                    "  int bar;\r\n"
20242                    "  int baz;\n"
20243                    "  int qux;\r\n"
20244                    "};",
20245                    style));
20246   style.UseCRLF = true;
20247   EXPECT_EQ("union FooBarBazQux {\n"
20248             "  int foo;\n"
20249             "  int bar;\n"
20250             "  int baz;\n"
20251             "  int qux;\n"
20252             "};",
20253             format("union FooBarBazQux {\r\n"
20254                    "  int foo;\n"
20255                    "  int bar;\r\n"
20256                    "  int baz;\n"
20257                    "  int qux;\n"
20258                    "};",
20259                    style));
20260 }
20261 
20262 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
20263   verifyFormat("MY_CLASS(C) {\n"
20264                "  int i;\n"
20265                "  int j;\n"
20266                "};");
20267 }
20268 
20269 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
20270   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
20271   TwoIndent.ContinuationIndentWidth = 2;
20272 
20273   EXPECT_EQ("int i =\n"
20274             "  longFunction(\n"
20275             "    arg);",
20276             format("int i = longFunction(arg);", TwoIndent));
20277 
20278   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
20279   SixIndent.ContinuationIndentWidth = 6;
20280 
20281   EXPECT_EQ("int i =\n"
20282             "      longFunction(\n"
20283             "            arg);",
20284             format("int i = longFunction(arg);", SixIndent));
20285 }
20286 
20287 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
20288   FormatStyle Style = getLLVMStyle();
20289   verifyFormat("int Foo::getter(\n"
20290                "    //\n"
20291                ") const {\n"
20292                "  return foo;\n"
20293                "}",
20294                Style);
20295   verifyFormat("void Foo::setter(\n"
20296                "    //\n"
20297                ") {\n"
20298                "  foo = 1;\n"
20299                "}",
20300                Style);
20301 }
20302 
20303 TEST_F(FormatTest, SpacesInAngles) {
20304   FormatStyle Spaces = getLLVMStyle();
20305   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20306 
20307   verifyFormat("vector< ::std::string > x1;", Spaces);
20308   verifyFormat("Foo< int, Bar > x2;", Spaces);
20309   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
20310 
20311   verifyFormat("static_cast< int >(arg);", Spaces);
20312   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
20313   verifyFormat("f< int, float >();", Spaces);
20314   verifyFormat("template <> g() {}", Spaces);
20315   verifyFormat("template < std::vector< int > > f() {}", Spaces);
20316   verifyFormat("std::function< void(int, int) > fct;", Spaces);
20317   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
20318                Spaces);
20319 
20320   Spaces.Standard = FormatStyle::LS_Cpp03;
20321   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20322   verifyFormat("A< A< int > >();", Spaces);
20323 
20324   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20325   verifyFormat("A<A<int> >();", Spaces);
20326 
20327   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20328   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
20329                Spaces);
20330   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
20331                Spaces);
20332 
20333   verifyFormat("A<A<int> >();", Spaces);
20334   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
20335   verifyFormat("A< A< int > >();", Spaces);
20336 
20337   Spaces.Standard = FormatStyle::LS_Cpp11;
20338   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20339   verifyFormat("A< A< int > >();", Spaces);
20340 
20341   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20342   verifyFormat("vector<::std::string> x4;", Spaces);
20343   verifyFormat("vector<int> x5;", Spaces);
20344   verifyFormat("Foo<int, Bar> x6;", Spaces);
20345   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20346 
20347   verifyFormat("A<A<int>>();", Spaces);
20348 
20349   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20350   verifyFormat("vector<::std::string> x4;", Spaces);
20351   verifyFormat("vector< ::std::string > x4;", Spaces);
20352   verifyFormat("vector<int> x5;", Spaces);
20353   verifyFormat("vector< int > x5;", Spaces);
20354   verifyFormat("Foo<int, Bar> x6;", Spaces);
20355   verifyFormat("Foo< int, Bar > x6;", Spaces);
20356   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20357   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
20358 
20359   verifyFormat("A<A<int>>();", Spaces);
20360   verifyFormat("A< A< int > >();", Spaces);
20361   verifyFormat("A<A<int > >();", Spaces);
20362   verifyFormat("A< A< int>>();", Spaces);
20363 }
20364 
20365 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
20366   FormatStyle Style = getLLVMStyle();
20367   Style.SpaceAfterTemplateKeyword = false;
20368   verifyFormat("template<int> void foo();", Style);
20369 }
20370 
20371 TEST_F(FormatTest, TripleAngleBrackets) {
20372   verifyFormat("f<<<1, 1>>>();");
20373   verifyFormat("f<<<1, 1, 1, s>>>();");
20374   verifyFormat("f<<<a, b, c, d>>>();");
20375   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
20376   verifyFormat("f<param><<<1, 1>>>();");
20377   verifyFormat("f<1><<<1, 1>>>();");
20378   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
20379   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20380                "aaaaaaaaaaa<<<\n    1, 1>>>();");
20381   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
20382                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
20383 }
20384 
20385 TEST_F(FormatTest, MergeLessLessAtEnd) {
20386   verifyFormat("<<");
20387   EXPECT_EQ("< < <", format("\\\n<<<"));
20388   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20389                "aaallvm::outs() <<");
20390   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20391                "aaaallvm::outs()\n    <<");
20392 }
20393 
20394 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
20395   std::string code = "#if A\n"
20396                      "#if B\n"
20397                      "a.\n"
20398                      "#endif\n"
20399                      "    a = 1;\n"
20400                      "#else\n"
20401                      "#endif\n"
20402                      "#if C\n"
20403                      "#else\n"
20404                      "#endif\n";
20405   EXPECT_EQ(code, format(code));
20406 }
20407 
20408 TEST_F(FormatTest, HandleConflictMarkers) {
20409   // Git/SVN conflict markers.
20410   EXPECT_EQ("int a;\n"
20411             "void f() {\n"
20412             "  callme(some(parameter1,\n"
20413             "<<<<<<< text by the vcs\n"
20414             "              parameter2),\n"
20415             "||||||| text by the vcs\n"
20416             "              parameter2),\n"
20417             "         parameter3,\n"
20418             "======= text by the vcs\n"
20419             "              parameter2, parameter3),\n"
20420             ">>>>>>> text by the vcs\n"
20421             "         otherparameter);\n",
20422             format("int a;\n"
20423                    "void f() {\n"
20424                    "  callme(some(parameter1,\n"
20425                    "<<<<<<< text by the vcs\n"
20426                    "  parameter2),\n"
20427                    "||||||| text by the vcs\n"
20428                    "  parameter2),\n"
20429                    "  parameter3,\n"
20430                    "======= text by the vcs\n"
20431                    "  parameter2,\n"
20432                    "  parameter3),\n"
20433                    ">>>>>>> text by the vcs\n"
20434                    "  otherparameter);\n"));
20435 
20436   // Perforce markers.
20437   EXPECT_EQ("void f() {\n"
20438             "  function(\n"
20439             ">>>> text by the vcs\n"
20440             "      parameter,\n"
20441             "==== text by the vcs\n"
20442             "      parameter,\n"
20443             "==== text by the vcs\n"
20444             "      parameter,\n"
20445             "<<<< text by the vcs\n"
20446             "      parameter);\n",
20447             format("void f() {\n"
20448                    "  function(\n"
20449                    ">>>> text by the vcs\n"
20450                    "  parameter,\n"
20451                    "==== text by the vcs\n"
20452                    "  parameter,\n"
20453                    "==== text by the vcs\n"
20454                    "  parameter,\n"
20455                    "<<<< text by the vcs\n"
20456                    "  parameter);\n"));
20457 
20458   EXPECT_EQ("<<<<<<<\n"
20459             "|||||||\n"
20460             "=======\n"
20461             ">>>>>>>",
20462             format("<<<<<<<\n"
20463                    "|||||||\n"
20464                    "=======\n"
20465                    ">>>>>>>"));
20466 
20467   EXPECT_EQ("<<<<<<<\n"
20468             "|||||||\n"
20469             "int i;\n"
20470             "=======\n"
20471             ">>>>>>>",
20472             format("<<<<<<<\n"
20473                    "|||||||\n"
20474                    "int i;\n"
20475                    "=======\n"
20476                    ">>>>>>>"));
20477 
20478   // FIXME: Handle parsing of macros around conflict markers correctly:
20479   EXPECT_EQ("#define Macro \\\n"
20480             "<<<<<<<\n"
20481             "Something \\\n"
20482             "|||||||\n"
20483             "Else \\\n"
20484             "=======\n"
20485             "Other \\\n"
20486             ">>>>>>>\n"
20487             "    End int i;\n",
20488             format("#define Macro \\\n"
20489                    "<<<<<<<\n"
20490                    "  Something \\\n"
20491                    "|||||||\n"
20492                    "  Else \\\n"
20493                    "=======\n"
20494                    "  Other \\\n"
20495                    ">>>>>>>\n"
20496                    "  End\n"
20497                    "int i;\n"));
20498 }
20499 
20500 TEST_F(FormatTest, DisableRegions) {
20501   EXPECT_EQ("int i;\n"
20502             "// clang-format off\n"
20503             "  int j;\n"
20504             "// clang-format on\n"
20505             "int k;",
20506             format(" int  i;\n"
20507                    "   // clang-format off\n"
20508                    "  int j;\n"
20509                    " // clang-format on\n"
20510                    "   int   k;"));
20511   EXPECT_EQ("int i;\n"
20512             "/* clang-format off */\n"
20513             "  int j;\n"
20514             "/* clang-format on */\n"
20515             "int k;",
20516             format(" int  i;\n"
20517                    "   /* clang-format off */\n"
20518                    "  int j;\n"
20519                    " /* clang-format on */\n"
20520                    "   int   k;"));
20521 
20522   // Don't reflow comments within disabled regions.
20523   EXPECT_EQ("// clang-format off\n"
20524             "// long long long long long long line\n"
20525             "/* clang-format on */\n"
20526             "/* long long long\n"
20527             " * long long long\n"
20528             " * line */\n"
20529             "int i;\n"
20530             "/* clang-format off */\n"
20531             "/* long long long long long long line */\n",
20532             format("// clang-format off\n"
20533                    "// long long long long long long line\n"
20534                    "/* clang-format on */\n"
20535                    "/* long long long long long long line */\n"
20536                    "int i;\n"
20537                    "/* clang-format off */\n"
20538                    "/* long long long long long long line */\n",
20539                    getLLVMStyleWithColumns(20)));
20540 }
20541 
20542 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
20543   format("? ) =");
20544   verifyNoCrash("#define a\\\n /**/}");
20545 }
20546 
20547 TEST_F(FormatTest, FormatsTableGenCode) {
20548   FormatStyle Style = getLLVMStyle();
20549   Style.Language = FormatStyle::LK_TableGen;
20550   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
20551 }
20552 
20553 TEST_F(FormatTest, ArrayOfTemplates) {
20554   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
20555             format("auto a = new unique_ptr<int > [ 10];"));
20556 
20557   FormatStyle Spaces = getLLVMStyle();
20558   Spaces.SpacesInSquareBrackets = true;
20559   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
20560             format("auto a = new unique_ptr<int > [10];", Spaces));
20561 }
20562 
20563 TEST_F(FormatTest, ArrayAsTemplateType) {
20564   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
20565             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
20566 
20567   FormatStyle Spaces = getLLVMStyle();
20568   Spaces.SpacesInSquareBrackets = true;
20569   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
20570             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
20571 }
20572 
20573 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
20574 
20575 TEST(FormatStyle, GetStyleWithEmptyFileName) {
20576   llvm::vfs::InMemoryFileSystem FS;
20577   auto Style1 = getStyle("file", "", "Google", "", &FS);
20578   ASSERT_TRUE((bool)Style1);
20579   ASSERT_EQ(*Style1, getGoogleStyle());
20580 }
20581 
20582 TEST(FormatStyle, GetStyleOfFile) {
20583   llvm::vfs::InMemoryFileSystem FS;
20584   // Test 1: format file in the same directory.
20585   ASSERT_TRUE(
20586       FS.addFile("/a/.clang-format", 0,
20587                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
20588   ASSERT_TRUE(
20589       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20590   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
20591   ASSERT_TRUE((bool)Style1);
20592   ASSERT_EQ(*Style1, getLLVMStyle());
20593 
20594   // Test 2.1: fallback to default.
20595   ASSERT_TRUE(
20596       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20597   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
20598   ASSERT_TRUE((bool)Style2);
20599   ASSERT_EQ(*Style2, getMozillaStyle());
20600 
20601   // Test 2.2: no format on 'none' fallback style.
20602   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
20603   ASSERT_TRUE((bool)Style2);
20604   ASSERT_EQ(*Style2, getNoStyle());
20605 
20606   // Test 2.3: format if config is found with no based style while fallback is
20607   // 'none'.
20608   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
20609                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
20610   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
20611   ASSERT_TRUE((bool)Style2);
20612   ASSERT_EQ(*Style2, getLLVMStyle());
20613 
20614   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
20615   Style2 = getStyle("{}", "a.h", "none", "", &FS);
20616   ASSERT_TRUE((bool)Style2);
20617   ASSERT_EQ(*Style2, getLLVMStyle());
20618 
20619   // Test 3: format file in parent directory.
20620   ASSERT_TRUE(
20621       FS.addFile("/c/.clang-format", 0,
20622                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
20623   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
20624                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20625   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
20626   ASSERT_TRUE((bool)Style3);
20627   ASSERT_EQ(*Style3, getGoogleStyle());
20628 
20629   // Test 4: error on invalid fallback style
20630   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
20631   ASSERT_FALSE((bool)Style4);
20632   llvm::consumeError(Style4.takeError());
20633 
20634   // Test 5: error on invalid yaml on command line
20635   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
20636   ASSERT_FALSE((bool)Style5);
20637   llvm::consumeError(Style5.takeError());
20638 
20639   // Test 6: error on invalid style
20640   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
20641   ASSERT_FALSE((bool)Style6);
20642   llvm::consumeError(Style6.takeError());
20643 
20644   // Test 7: found config file, error on parsing it
20645   ASSERT_TRUE(
20646       FS.addFile("/d/.clang-format", 0,
20647                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
20648                                                   "InvalidKey: InvalidValue")));
20649   ASSERT_TRUE(
20650       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20651   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
20652   ASSERT_FALSE((bool)Style7a);
20653   llvm::consumeError(Style7a.takeError());
20654 
20655   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
20656   ASSERT_TRUE((bool)Style7b);
20657 
20658   // Test 8: inferred per-language defaults apply.
20659   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
20660   ASSERT_TRUE((bool)StyleTd);
20661   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
20662 
20663   // Test 9.1: overwriting a file style, when parent no file exists with no
20664   // fallback style
20665   ASSERT_TRUE(FS.addFile(
20666       "/e/sub/.clang-format", 0,
20667       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
20668                                        "ColumnLimit: 20")));
20669   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
20670                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20671   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
20672   ASSERT_TRUE(static_cast<bool>(Style9));
20673   ASSERT_EQ(*Style9, [] {
20674     auto Style = getNoStyle();
20675     Style.ColumnLimit = 20;
20676     return Style;
20677   }());
20678 
20679   // Test 9.2: with LLVM fallback style
20680   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
20681   ASSERT_TRUE(static_cast<bool>(Style9));
20682   ASSERT_EQ(*Style9, [] {
20683     auto Style = getLLVMStyle();
20684     Style.ColumnLimit = 20;
20685     return Style;
20686   }());
20687 
20688   // Test 9.3: with a parent file
20689   ASSERT_TRUE(
20690       FS.addFile("/e/.clang-format", 0,
20691                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
20692                                                   "UseTab: Always")));
20693   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
20694   ASSERT_TRUE(static_cast<bool>(Style9));
20695   ASSERT_EQ(*Style9, [] {
20696     auto Style = getGoogleStyle();
20697     Style.ColumnLimit = 20;
20698     Style.UseTab = FormatStyle::UT_Always;
20699     return Style;
20700   }());
20701 
20702   // Test 9.4: propagate more than one level
20703   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
20704                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20705   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
20706                          llvm::MemoryBuffer::getMemBuffer(
20707                              "BasedOnStyle: InheritParentConfig\n"
20708                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
20709   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
20710 
20711   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
20712     auto Style = getGoogleStyle();
20713     Style.ColumnLimit = 20;
20714     Style.UseTab = FormatStyle::UT_Always;
20715     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
20716     return Style;
20717   }();
20718 
20719   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
20720   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
20721   ASSERT_TRUE(static_cast<bool>(Style9));
20722   ASSERT_EQ(*Style9, SubSubStyle);
20723 
20724   // Test 9.5: use InheritParentConfig as style name
20725   Style9 =
20726       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
20727   ASSERT_TRUE(static_cast<bool>(Style9));
20728   ASSERT_EQ(*Style9, SubSubStyle);
20729 
20730   // Test 9.6: use command line style with inheritance
20731   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
20732                     "none", "", &FS);
20733   ASSERT_TRUE(static_cast<bool>(Style9));
20734   ASSERT_EQ(*Style9, SubSubStyle);
20735 
20736   // Test 9.7: use command line style with inheritance and own config
20737   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
20738                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
20739                     "/e/sub/code.cpp", "none", "", &FS);
20740   ASSERT_TRUE(static_cast<bool>(Style9));
20741   ASSERT_EQ(*Style9, SubSubStyle);
20742 
20743   // Test 9.8: use inheritance from a file without BasedOnStyle
20744   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
20745                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
20746   ASSERT_TRUE(
20747       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
20748                  llvm::MemoryBuffer::getMemBuffer(
20749                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
20750   // Make sure we do not use the fallback style
20751   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
20752   ASSERT_TRUE(static_cast<bool>(Style9));
20753   ASSERT_EQ(*Style9, [] {
20754     auto Style = getLLVMStyle();
20755     Style.ColumnLimit = 123;
20756     return Style;
20757   }());
20758 
20759   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
20760   ASSERT_TRUE(static_cast<bool>(Style9));
20761   ASSERT_EQ(*Style9, [] {
20762     auto Style = getLLVMStyle();
20763     Style.ColumnLimit = 123;
20764     Style.IndentWidth = 7;
20765     return Style;
20766   }());
20767 }
20768 
20769 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
20770   // Column limit is 20.
20771   std::string Code = "Type *a =\n"
20772                      "    new Type();\n"
20773                      "g(iiiii, 0, jjjjj,\n"
20774                      "  0, kkkkk, 0, mm);\n"
20775                      "int  bad     = format   ;";
20776   std::string Expected = "auto a = new Type();\n"
20777                          "g(iiiii, nullptr,\n"
20778                          "  jjjjj, nullptr,\n"
20779                          "  kkkkk, nullptr,\n"
20780                          "  mm);\n"
20781                          "int  bad     = format   ;";
20782   FileID ID = Context.createInMemoryFile("format.cpp", Code);
20783   tooling::Replacements Replaces = toReplacements(
20784       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
20785                             "auto "),
20786        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
20787                             "nullptr"),
20788        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
20789                             "nullptr"),
20790        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
20791                             "nullptr")});
20792 
20793   format::FormatStyle Style = format::getLLVMStyle();
20794   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
20795   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
20796   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
20797       << llvm::toString(FormattedReplaces.takeError()) << "\n";
20798   auto Result = applyAllReplacements(Code, *FormattedReplaces);
20799   EXPECT_TRUE(static_cast<bool>(Result));
20800   EXPECT_EQ(Expected, *Result);
20801 }
20802 
20803 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
20804   std::string Code = "#include \"a.h\"\n"
20805                      "#include \"c.h\"\n"
20806                      "\n"
20807                      "int main() {\n"
20808                      "  return 0;\n"
20809                      "}";
20810   std::string Expected = "#include \"a.h\"\n"
20811                          "#include \"b.h\"\n"
20812                          "#include \"c.h\"\n"
20813                          "\n"
20814                          "int main() {\n"
20815                          "  return 0;\n"
20816                          "}";
20817   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
20818   tooling::Replacements Replaces = toReplacements(
20819       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
20820                             "#include \"b.h\"\n")});
20821 
20822   format::FormatStyle Style = format::getLLVMStyle();
20823   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
20824   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
20825   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
20826       << llvm::toString(FormattedReplaces.takeError()) << "\n";
20827   auto Result = applyAllReplacements(Code, *FormattedReplaces);
20828   EXPECT_TRUE(static_cast<bool>(Result));
20829   EXPECT_EQ(Expected, *Result);
20830 }
20831 
20832 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
20833   EXPECT_EQ("using std::cin;\n"
20834             "using std::cout;",
20835             format("using std::cout;\n"
20836                    "using std::cin;",
20837                    getGoogleStyle()));
20838 }
20839 
20840 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
20841   format::FormatStyle Style = format::getLLVMStyle();
20842   Style.Standard = FormatStyle::LS_Cpp03;
20843   // cpp03 recognize this string as identifier u8 and literal character 'a'
20844   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
20845 }
20846 
20847 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
20848   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
20849   // all modes, including C++11, C++14 and C++17
20850   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
20851 }
20852 
20853 TEST_F(FormatTest, DoNotFormatLikelyXml) {
20854   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
20855   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
20856 }
20857 
20858 TEST_F(FormatTest, StructuredBindings) {
20859   // Structured bindings is a C++17 feature.
20860   // all modes, including C++11, C++14 and C++17
20861   verifyFormat("auto [a, b] = f();");
20862   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
20863   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
20864   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
20865   EXPECT_EQ("auto const volatile [a, b] = f();",
20866             format("auto  const   volatile[a, b] = f();"));
20867   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
20868   EXPECT_EQ("auto &[a, b, c] = f();",
20869             format("auto   &[  a  ,  b,c   ] = f();"));
20870   EXPECT_EQ("auto &&[a, b, c] = f();",
20871             format("auto   &&[  a  ,  b,c   ] = f();"));
20872   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
20873   EXPECT_EQ("auto const volatile &&[a, b] = f();",
20874             format("auto  const  volatile  &&[a, b] = f();"));
20875   EXPECT_EQ("auto const &&[a, b] = f();",
20876             format("auto  const   &&  [a, b] = f();"));
20877   EXPECT_EQ("const auto &[a, b] = f();",
20878             format("const  auto  &  [a, b] = f();"));
20879   EXPECT_EQ("const auto volatile &&[a, b] = f();",
20880             format("const  auto   volatile  &&[a, b] = f();"));
20881   EXPECT_EQ("volatile const auto &&[a, b] = f();",
20882             format("volatile  const  auto   &&[a, b] = f();"));
20883   EXPECT_EQ("const auto &&[a, b] = f();",
20884             format("const  auto  &&  [a, b] = f();"));
20885 
20886   // Make sure we don't mistake structured bindings for lambdas.
20887   FormatStyle PointerMiddle = getLLVMStyle();
20888   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
20889   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
20890   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
20891   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
20892   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
20893   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
20894   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
20895   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
20896   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
20897   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
20898   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
20899   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
20900   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
20901 
20902   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
20903             format("for (const auto   &&   [a, b] : some_range) {\n}"));
20904   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
20905             format("for (const auto   &   [a, b] : some_range) {\n}"));
20906   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
20907             format("for (const auto[a, b] : some_range) {\n}"));
20908   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
20909   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
20910   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
20911   EXPECT_EQ("auto const &[x, y](expr);",
20912             format("auto  const  &  [x,y]  (expr);"));
20913   EXPECT_EQ("auto const &&[x, y](expr);",
20914             format("auto  const  &&  [x,y]  (expr);"));
20915   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
20916   EXPECT_EQ("auto const &[x, y]{expr};",
20917             format("auto  const  &  [x,y]  {expr};"));
20918   EXPECT_EQ("auto const &&[x, y]{expr};",
20919             format("auto  const  &&  [x,y]  {expr};"));
20920 
20921   format::FormatStyle Spaces = format::getLLVMStyle();
20922   Spaces.SpacesInSquareBrackets = true;
20923   verifyFormat("auto [ a, b ] = f();", Spaces);
20924   verifyFormat("auto &&[ a, b ] = f();", Spaces);
20925   verifyFormat("auto &[ a, b ] = f();", Spaces);
20926   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
20927   verifyFormat("auto const &[ a, b ] = f();", Spaces);
20928 }
20929 
20930 TEST_F(FormatTest, FileAndCode) {
20931   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
20932   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
20933   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
20934   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
20935   EXPECT_EQ(FormatStyle::LK_ObjC,
20936             guessLanguage("foo.h", "@interface Foo\n@end\n"));
20937   EXPECT_EQ(
20938       FormatStyle::LK_ObjC,
20939       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
20940   EXPECT_EQ(FormatStyle::LK_ObjC,
20941             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
20942   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
20943   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
20944   EXPECT_EQ(FormatStyle::LK_ObjC,
20945             guessLanguage("foo", "@interface Foo\n@end\n"));
20946   EXPECT_EQ(FormatStyle::LK_ObjC,
20947             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
20948   EXPECT_EQ(
20949       FormatStyle::LK_ObjC,
20950       guessLanguage("foo.h",
20951                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
20952   EXPECT_EQ(
20953       FormatStyle::LK_Cpp,
20954       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
20955 }
20956 
20957 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
20958   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
20959   EXPECT_EQ(FormatStyle::LK_ObjC,
20960             guessLanguage("foo.h", "array[[calculator getIndex]];"));
20961   EXPECT_EQ(FormatStyle::LK_Cpp,
20962             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
20963   EXPECT_EQ(
20964       FormatStyle::LK_Cpp,
20965       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
20966   EXPECT_EQ(FormatStyle::LK_ObjC,
20967             guessLanguage("foo.h", "[[noreturn foo] bar];"));
20968   EXPECT_EQ(FormatStyle::LK_Cpp,
20969             guessLanguage("foo.h", "[[clang::fallthrough]];"));
20970   EXPECT_EQ(FormatStyle::LK_ObjC,
20971             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
20972   EXPECT_EQ(FormatStyle::LK_Cpp,
20973             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
20974   EXPECT_EQ(FormatStyle::LK_Cpp,
20975             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
20976   EXPECT_EQ(FormatStyle::LK_ObjC,
20977             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
20978   EXPECT_EQ(FormatStyle::LK_Cpp,
20979             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
20980   EXPECT_EQ(
20981       FormatStyle::LK_Cpp,
20982       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
20983   EXPECT_EQ(
20984       FormatStyle::LK_Cpp,
20985       guessLanguage("foo.h",
20986                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
20987   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
20988 }
20989 
20990 TEST_F(FormatTest, GuessLanguageWithCaret) {
20991   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
20992   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
20993   EXPECT_EQ(FormatStyle::LK_ObjC,
20994             guessLanguage("foo.h", "int(^)(char, float);"));
20995   EXPECT_EQ(FormatStyle::LK_ObjC,
20996             guessLanguage("foo.h", "int(^foo)(char, float);"));
20997   EXPECT_EQ(FormatStyle::LK_ObjC,
20998             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
20999   EXPECT_EQ(FormatStyle::LK_ObjC,
21000             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
21001   EXPECT_EQ(
21002       FormatStyle::LK_ObjC,
21003       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
21004 }
21005 
21006 TEST_F(FormatTest, GuessLanguageWithPragmas) {
21007   EXPECT_EQ(FormatStyle::LK_Cpp,
21008             guessLanguage("foo.h", "__pragma(warning(disable:))"));
21009   EXPECT_EQ(FormatStyle::LK_Cpp,
21010             guessLanguage("foo.h", "#pragma(warning(disable:))"));
21011   EXPECT_EQ(FormatStyle::LK_Cpp,
21012             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
21013 }
21014 
21015 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
21016   // ASM symbolic names are identifiers that must be surrounded by [] without
21017   // space in between:
21018   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
21019 
21020   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
21021   verifyFormat(R"(//
21022 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
21023 )");
21024 
21025   // A list of several ASM symbolic names.
21026   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
21027 
21028   // ASM symbolic names in inline ASM with inputs and outputs.
21029   verifyFormat(R"(//
21030 asm("cmoveq %1, %2, %[result]"
21031     : [result] "=r"(result)
21032     : "r"(test), "r"(new), "[result]"(old));
21033 )");
21034 
21035   // ASM symbolic names in inline ASM with no outputs.
21036   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
21037 }
21038 
21039 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
21040   EXPECT_EQ(FormatStyle::LK_Cpp,
21041             guessLanguage("foo.h", "void f() {\n"
21042                                    "  asm (\"mov %[e], %[d]\"\n"
21043                                    "     : [d] \"=rm\" (d)\n"
21044                                    "       [e] \"rm\" (*e));\n"
21045                                    "}"));
21046   EXPECT_EQ(FormatStyle::LK_Cpp,
21047             guessLanguage("foo.h", "void f() {\n"
21048                                    "  _asm (\"mov %[e], %[d]\"\n"
21049                                    "     : [d] \"=rm\" (d)\n"
21050                                    "       [e] \"rm\" (*e));\n"
21051                                    "}"));
21052   EXPECT_EQ(FormatStyle::LK_Cpp,
21053             guessLanguage("foo.h", "void f() {\n"
21054                                    "  __asm (\"mov %[e], %[d]\"\n"
21055                                    "     : [d] \"=rm\" (d)\n"
21056                                    "       [e] \"rm\" (*e));\n"
21057                                    "}"));
21058   EXPECT_EQ(FormatStyle::LK_Cpp,
21059             guessLanguage("foo.h", "void f() {\n"
21060                                    "  __asm__ (\"mov %[e], %[d]\"\n"
21061                                    "     : [d] \"=rm\" (d)\n"
21062                                    "       [e] \"rm\" (*e));\n"
21063                                    "}"));
21064   EXPECT_EQ(FormatStyle::LK_Cpp,
21065             guessLanguage("foo.h", "void f() {\n"
21066                                    "  asm (\"mov %[e], %[d]\"\n"
21067                                    "     : [d] \"=rm\" (d),\n"
21068                                    "       [e] \"rm\" (*e));\n"
21069                                    "}"));
21070   EXPECT_EQ(FormatStyle::LK_Cpp,
21071             guessLanguage("foo.h", "void f() {\n"
21072                                    "  asm volatile (\"mov %[e], %[d]\"\n"
21073                                    "     : [d] \"=rm\" (d)\n"
21074                                    "       [e] \"rm\" (*e));\n"
21075                                    "}"));
21076 }
21077 
21078 TEST_F(FormatTest, GuessLanguageWithChildLines) {
21079   EXPECT_EQ(FormatStyle::LK_Cpp,
21080             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
21081   EXPECT_EQ(FormatStyle::LK_ObjC,
21082             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
21083   EXPECT_EQ(
21084       FormatStyle::LK_Cpp,
21085       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
21086   EXPECT_EQ(
21087       FormatStyle::LK_ObjC,
21088       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
21089 }
21090 
21091 TEST_F(FormatTest, TypenameMacros) {
21092   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
21093 
21094   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
21095   FormatStyle Google = getGoogleStyleWithColumns(0);
21096   Google.TypenameMacros = TypenameMacros;
21097   verifyFormat("struct foo {\n"
21098                "  int bar;\n"
21099                "  TAILQ_ENTRY(a) bleh;\n"
21100                "};",
21101                Google);
21102 
21103   FormatStyle Macros = getLLVMStyle();
21104   Macros.TypenameMacros = TypenameMacros;
21105 
21106   verifyFormat("STACK_OF(int) a;", Macros);
21107   verifyFormat("STACK_OF(int) *a;", Macros);
21108   verifyFormat("STACK_OF(int const *) *a;", Macros);
21109   verifyFormat("STACK_OF(int *const) *a;", Macros);
21110   verifyFormat("STACK_OF(int, string) a;", Macros);
21111   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
21112   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
21113   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
21114   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
21115   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
21116   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
21117 
21118   Macros.PointerAlignment = FormatStyle::PAS_Left;
21119   verifyFormat("STACK_OF(int)* a;", Macros);
21120   verifyFormat("STACK_OF(int*)* a;", Macros);
21121   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
21122   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
21123   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
21124 }
21125 
21126 TEST_F(FormatTest, AtomicQualifier) {
21127   // Check that we treate _Atomic as a type and not a function call
21128   FormatStyle Google = getGoogleStyleWithColumns(0);
21129   verifyFormat("struct foo {\n"
21130                "  int a1;\n"
21131                "  _Atomic(a) a2;\n"
21132                "  _Atomic(_Atomic(int) *const) a3;\n"
21133                "};",
21134                Google);
21135   verifyFormat("_Atomic(uint64_t) a;");
21136   verifyFormat("_Atomic(uint64_t) *a;");
21137   verifyFormat("_Atomic(uint64_t const *) *a;");
21138   verifyFormat("_Atomic(uint64_t *const) *a;");
21139   verifyFormat("_Atomic(const uint64_t *) *a;");
21140   verifyFormat("_Atomic(uint64_t) a;");
21141   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
21142   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
21143   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
21144   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
21145 
21146   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
21147   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
21148   FormatStyle Style = getLLVMStyle();
21149   Style.PointerAlignment = FormatStyle::PAS_Left;
21150   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
21151   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
21152   verifyFormat("_Atomic(int)* a;", Style);
21153   verifyFormat("_Atomic(int*)* a;", Style);
21154   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
21155 
21156   Style.SpacesInCStyleCastParentheses = true;
21157   Style.SpacesInParentheses = false;
21158   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
21159   Style.SpacesInCStyleCastParentheses = false;
21160   Style.SpacesInParentheses = true;
21161   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
21162   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
21163 }
21164 
21165 TEST_F(FormatTest, AmbersandInLamda) {
21166   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
21167   FormatStyle AlignStyle = getLLVMStyle();
21168   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
21169   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21170   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
21171   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21172 }
21173 
21174 TEST_F(FormatTest, SpacesInConditionalStatement) {
21175   FormatStyle Spaces = getLLVMStyle();
21176   Spaces.IfMacros.clear();
21177   Spaces.IfMacros.push_back("MYIF");
21178   Spaces.SpacesInConditionalStatement = true;
21179   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
21180   verifyFormat("if ( !a )\n  return;", Spaces);
21181   verifyFormat("if ( a )\n  return;", Spaces);
21182   verifyFormat("if constexpr ( a )\n  return;", Spaces);
21183   verifyFormat("MYIF ( a )\n  return;", Spaces);
21184   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
21185   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
21186   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
21187   verifyFormat("while ( a )\n  return;", Spaces);
21188   verifyFormat("while ( (a && b) )\n  return;", Spaces);
21189   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
21190   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
21191   // Check that space on the left of "::" is inserted as expected at beginning
21192   // of condition.
21193   verifyFormat("while ( ::func() )\n  return;", Spaces);
21194 
21195   // Check impact of ControlStatementsExceptControlMacros is honored.
21196   Spaces.SpaceBeforeParens =
21197       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
21198   verifyFormat("MYIF( a )\n  return;", Spaces);
21199   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
21200   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
21201 }
21202 
21203 TEST_F(FormatTest, AlternativeOperators) {
21204   // Test case for ensuring alternate operators are not
21205   // combined with their right most neighbour.
21206   verifyFormat("int a and b;");
21207   verifyFormat("int a and_eq b;");
21208   verifyFormat("int a bitand b;");
21209   verifyFormat("int a bitor b;");
21210   verifyFormat("int a compl b;");
21211   verifyFormat("int a not b;");
21212   verifyFormat("int a not_eq b;");
21213   verifyFormat("int a or b;");
21214   verifyFormat("int a xor b;");
21215   verifyFormat("int a xor_eq b;");
21216   verifyFormat("return this not_eq bitand other;");
21217   verifyFormat("bool operator not_eq(const X bitand other)");
21218 
21219   verifyFormat("int a and 5;");
21220   verifyFormat("int a and_eq 5;");
21221   verifyFormat("int a bitand 5;");
21222   verifyFormat("int a bitor 5;");
21223   verifyFormat("int a compl 5;");
21224   verifyFormat("int a not 5;");
21225   verifyFormat("int a not_eq 5;");
21226   verifyFormat("int a or 5;");
21227   verifyFormat("int a xor 5;");
21228   verifyFormat("int a xor_eq 5;");
21229 
21230   verifyFormat("int a compl(5);");
21231   verifyFormat("int a not(5);");
21232 
21233   /* FIXME handle alternate tokens
21234    * https://en.cppreference.com/w/cpp/language/operator_alternative
21235   // alternative tokens
21236   verifyFormat("compl foo();");     //  ~foo();
21237   verifyFormat("foo() <%%>;");      // foo();
21238   verifyFormat("void foo() <%%>;"); // void foo(){}
21239   verifyFormat("int a <:1:>;");     // int a[1];[
21240   verifyFormat("%:define ABC abc"); // #define ABC abc
21241   verifyFormat("%:%:");             // ##
21242   */
21243 }
21244 
21245 TEST_F(FormatTest, STLWhileNotDefineChed) {
21246   verifyFormat("#if defined(while)\n"
21247                "#define while EMIT WARNING C4005\n"
21248                "#endif // while");
21249 }
21250 
21251 TEST_F(FormatTest, OperatorSpacing) {
21252   FormatStyle Style = getLLVMStyle();
21253   Style.PointerAlignment = FormatStyle::PAS_Right;
21254   verifyFormat("Foo::operator*();", Style);
21255   verifyFormat("Foo::operator void *();", Style);
21256   verifyFormat("Foo::operator void **();", Style);
21257   verifyFormat("Foo::operator void *&();", Style);
21258   verifyFormat("Foo::operator void *&&();", Style);
21259   verifyFormat("Foo::operator void const *();", Style);
21260   verifyFormat("Foo::operator void const **();", Style);
21261   verifyFormat("Foo::operator void const *&();", Style);
21262   verifyFormat("Foo::operator void const *&&();", Style);
21263   verifyFormat("Foo::operator()(void *);", Style);
21264   verifyFormat("Foo::operator*(void *);", Style);
21265   verifyFormat("Foo::operator*();", Style);
21266   verifyFormat("Foo::operator**();", Style);
21267   verifyFormat("Foo::operator&();", Style);
21268   verifyFormat("Foo::operator<int> *();", Style);
21269   verifyFormat("Foo::operator<Foo> *();", Style);
21270   verifyFormat("Foo::operator<int> **();", Style);
21271   verifyFormat("Foo::operator<Foo> **();", Style);
21272   verifyFormat("Foo::operator<int> &();", Style);
21273   verifyFormat("Foo::operator<Foo> &();", Style);
21274   verifyFormat("Foo::operator<int> &&();", Style);
21275   verifyFormat("Foo::operator<Foo> &&();", Style);
21276   verifyFormat("Foo::operator<int> *&();", Style);
21277   verifyFormat("Foo::operator<Foo> *&();", Style);
21278   verifyFormat("Foo::operator<int> *&&();", Style);
21279   verifyFormat("Foo::operator<Foo> *&&();", Style);
21280   verifyFormat("operator*(int (*)(), class Foo);", Style);
21281 
21282   verifyFormat("Foo::operator&();", Style);
21283   verifyFormat("Foo::operator void &();", Style);
21284   verifyFormat("Foo::operator void const &();", Style);
21285   verifyFormat("Foo::operator()(void &);", Style);
21286   verifyFormat("Foo::operator&(void &);", Style);
21287   verifyFormat("Foo::operator&();", Style);
21288   verifyFormat("operator&(int (&)(), class Foo);", Style);
21289 
21290   verifyFormat("Foo::operator&&();", Style);
21291   verifyFormat("Foo::operator**();", Style);
21292   verifyFormat("Foo::operator void &&();", Style);
21293   verifyFormat("Foo::operator void const &&();", Style);
21294   verifyFormat("Foo::operator()(void &&);", Style);
21295   verifyFormat("Foo::operator&&(void &&);", Style);
21296   verifyFormat("Foo::operator&&();", Style);
21297   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21298   verifyFormat("operator const nsTArrayRight<E> &()", Style);
21299   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
21300                Style);
21301   verifyFormat("operator void **()", Style);
21302   verifyFormat("operator const FooRight<Object> &()", Style);
21303   verifyFormat("operator const FooRight<Object> *()", Style);
21304   verifyFormat("operator const FooRight<Object> **()", Style);
21305   verifyFormat("operator const FooRight<Object> *&()", Style);
21306   verifyFormat("operator const FooRight<Object> *&&()", Style);
21307 
21308   Style.PointerAlignment = FormatStyle::PAS_Left;
21309   verifyFormat("Foo::operator*();", Style);
21310   verifyFormat("Foo::operator**();", Style);
21311   verifyFormat("Foo::operator void*();", Style);
21312   verifyFormat("Foo::operator void**();", Style);
21313   verifyFormat("Foo::operator void*&();", Style);
21314   verifyFormat("Foo::operator void*&&();", Style);
21315   verifyFormat("Foo::operator void const*();", Style);
21316   verifyFormat("Foo::operator void const**();", Style);
21317   verifyFormat("Foo::operator void const*&();", Style);
21318   verifyFormat("Foo::operator void const*&&();", Style);
21319   verifyFormat("Foo::operator/*comment*/ void*();", Style);
21320   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
21321   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
21322   verifyFormat("Foo::operator()(void*);", Style);
21323   verifyFormat("Foo::operator*(void*);", Style);
21324   verifyFormat("Foo::operator*();", Style);
21325   verifyFormat("Foo::operator<int>*();", Style);
21326   verifyFormat("Foo::operator<Foo>*();", Style);
21327   verifyFormat("Foo::operator<int>**();", Style);
21328   verifyFormat("Foo::operator<Foo>**();", Style);
21329   verifyFormat("Foo::operator<Foo>*&();", Style);
21330   verifyFormat("Foo::operator<int>&();", Style);
21331   verifyFormat("Foo::operator<Foo>&();", Style);
21332   verifyFormat("Foo::operator<int>&&();", Style);
21333   verifyFormat("Foo::operator<Foo>&&();", Style);
21334   verifyFormat("Foo::operator<int>*&();", Style);
21335   verifyFormat("Foo::operator<Foo>*&();", Style);
21336   verifyFormat("operator*(int (*)(), class Foo);", Style);
21337 
21338   verifyFormat("Foo::operator&();", Style);
21339   verifyFormat("Foo::operator void&();", Style);
21340   verifyFormat("Foo::operator void const&();", Style);
21341   verifyFormat("Foo::operator/*comment*/ void&();", Style);
21342   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
21343   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
21344   verifyFormat("Foo::operator()(void&);", Style);
21345   verifyFormat("Foo::operator&(void&);", Style);
21346   verifyFormat("Foo::operator&();", Style);
21347   verifyFormat("operator&(int (&)(), class Foo);", Style);
21348 
21349   verifyFormat("Foo::operator&&();", Style);
21350   verifyFormat("Foo::operator void&&();", Style);
21351   verifyFormat("Foo::operator void const&&();", Style);
21352   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
21353   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
21354   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
21355   verifyFormat("Foo::operator()(void&&);", Style);
21356   verifyFormat("Foo::operator&&(void&&);", Style);
21357   verifyFormat("Foo::operator&&();", Style);
21358   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21359   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
21360   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
21361                Style);
21362   verifyFormat("operator void**()", Style);
21363   verifyFormat("operator const FooLeft<Object>&()", Style);
21364   verifyFormat("operator const FooLeft<Object>*()", Style);
21365   verifyFormat("operator const FooLeft<Object>**()", Style);
21366   verifyFormat("operator const FooLeft<Object>*&()", Style);
21367   verifyFormat("operator const FooLeft<Object>*&&()", Style);
21368 
21369   // PR45107
21370   verifyFormat("operator Vector<String>&();", Style);
21371   verifyFormat("operator const Vector<String>&();", Style);
21372   verifyFormat("operator foo::Bar*();", Style);
21373   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
21374   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
21375                Style);
21376 
21377   Style.PointerAlignment = FormatStyle::PAS_Middle;
21378   verifyFormat("Foo::operator*();", Style);
21379   verifyFormat("Foo::operator void *();", Style);
21380   verifyFormat("Foo::operator()(void *);", Style);
21381   verifyFormat("Foo::operator*(void *);", Style);
21382   verifyFormat("Foo::operator*();", Style);
21383   verifyFormat("operator*(int (*)(), class Foo);", Style);
21384 
21385   verifyFormat("Foo::operator&();", Style);
21386   verifyFormat("Foo::operator void &();", Style);
21387   verifyFormat("Foo::operator void const &();", Style);
21388   verifyFormat("Foo::operator()(void &);", Style);
21389   verifyFormat("Foo::operator&(void &);", Style);
21390   verifyFormat("Foo::operator&();", Style);
21391   verifyFormat("operator&(int (&)(), class Foo);", Style);
21392 
21393   verifyFormat("Foo::operator&&();", Style);
21394   verifyFormat("Foo::operator void &&();", Style);
21395   verifyFormat("Foo::operator void const &&();", Style);
21396   verifyFormat("Foo::operator()(void &&);", Style);
21397   verifyFormat("Foo::operator&&(void &&);", Style);
21398   verifyFormat("Foo::operator&&();", Style);
21399   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21400 }
21401 
21402 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
21403   FormatStyle Style = getLLVMStyle();
21404   // PR46157
21405   verifyFormat("foo(operator+, -42);", Style);
21406   verifyFormat("foo(operator++, -42);", Style);
21407   verifyFormat("foo(operator--, -42);", Style);
21408   verifyFormat("foo(-42, operator--);", Style);
21409   verifyFormat("foo(-42, operator, );", Style);
21410   verifyFormat("foo(operator, , -42);", Style);
21411 }
21412 
21413 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
21414   FormatStyle Style = getLLVMStyle();
21415   Style.WhitespaceSensitiveMacros.push_back("FOO");
21416 
21417   // Don't use the helpers here, since 'mess up' will change the whitespace
21418   // and these are all whitespace sensitive by definition
21419   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
21420             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
21421   EXPECT_EQ(
21422       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
21423       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
21424   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
21425             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
21426   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
21427             "       Still=Intentional);",
21428             format("FOO(String-ized&Messy+But,: :\n"
21429                    "       Still=Intentional);",
21430                    Style));
21431   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
21432   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
21433             "       Still=Intentional);",
21434             format("FOO(String-ized=&Messy+But,: :\n"
21435                    "       Still=Intentional);",
21436                    Style));
21437 
21438   Style.ColumnLimit = 21;
21439   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
21440             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
21441 }
21442 
21443 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
21444   // These tests are not in NamespaceFixer because that doesn't
21445   // test its interaction with line wrapping
21446   FormatStyle Style = getLLVMStyle();
21447   Style.ColumnLimit = 80;
21448   verifyFormat("namespace {\n"
21449                "int i;\n"
21450                "int j;\n"
21451                "} // namespace",
21452                Style);
21453 
21454   verifyFormat("namespace AAA {\n"
21455                "int i;\n"
21456                "int j;\n"
21457                "} // namespace AAA",
21458                Style);
21459 
21460   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
21461             "int i;\n"
21462             "int j;\n"
21463             "} // namespace Averyveryveryverylongnamespace",
21464             format("namespace Averyveryveryverylongnamespace {\n"
21465                    "int i;\n"
21466                    "int j;\n"
21467                    "}",
21468                    Style));
21469 
21470   EXPECT_EQ(
21471       "namespace "
21472       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
21473       "    went::mad::now {\n"
21474       "int i;\n"
21475       "int j;\n"
21476       "} // namespace\n"
21477       "  // "
21478       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
21479       "went::mad::now",
21480       format("namespace "
21481              "would::it::save::you::a::lot::of::time::if_::i::"
21482              "just::gave::up::and_::went::mad::now {\n"
21483              "int i;\n"
21484              "int j;\n"
21485              "}",
21486              Style));
21487 
21488   // This used to duplicate the comment again and again on subsequent runs
21489   EXPECT_EQ(
21490       "namespace "
21491       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
21492       "    went::mad::now {\n"
21493       "int i;\n"
21494       "int j;\n"
21495       "} // namespace\n"
21496       "  // "
21497       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
21498       "went::mad::now",
21499       format("namespace "
21500              "would::it::save::you::a::lot::of::time::if_::i::"
21501              "just::gave::up::and_::went::mad::now {\n"
21502              "int i;\n"
21503              "int j;\n"
21504              "} // namespace\n"
21505              "  // "
21506              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
21507              "and_::went::mad::now",
21508              Style));
21509 }
21510 
21511 TEST_F(FormatTest, LikelyUnlikely) {
21512   FormatStyle Style = getLLVMStyle();
21513 
21514   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21515                "  return 29;\n"
21516                "}",
21517                Style);
21518 
21519   verifyFormat("if (argc > 5) [[likely]] {\n"
21520                "  return 29;\n"
21521                "}",
21522                Style);
21523 
21524   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21525                "  return 29;\n"
21526                "} else [[likely]] {\n"
21527                "  return 42;\n"
21528                "}\n",
21529                Style);
21530 
21531   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21532                "  return 29;\n"
21533                "} else if (argc > 10) [[likely]] {\n"
21534                "  return 99;\n"
21535                "} else {\n"
21536                "  return 42;\n"
21537                "}\n",
21538                Style);
21539 
21540   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
21541                "  return 29;\n"
21542                "}",
21543                Style);
21544 }
21545 
21546 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
21547   verifyFormat("Constructor()\n"
21548                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21549                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
21550                "aaaaaaaaaaaaaaaaaat))");
21551   verifyFormat("Constructor()\n"
21552                "    : aaaaaaaaaaaaa(aaaaaa), "
21553                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
21554 
21555   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
21556   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
21557   verifyFormat("Constructor()\n"
21558                "    : aaaaaa(aaaaaa),\n"
21559                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21560                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
21561                StyleWithWhitespacePenalty);
21562   verifyFormat("Constructor()\n"
21563                "    : aaaaaaaaaaaaa(aaaaaa), "
21564                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
21565                StyleWithWhitespacePenalty);
21566 }
21567 
21568 TEST_F(FormatTest, LLVMDefaultStyle) {
21569   FormatStyle Style = getLLVMStyle();
21570   verifyFormat("extern \"C\" {\n"
21571                "int foo();\n"
21572                "}",
21573                Style);
21574 }
21575 TEST_F(FormatTest, GNUDefaultStyle) {
21576   FormatStyle Style = getGNUStyle();
21577   verifyFormat("extern \"C\"\n"
21578                "{\n"
21579                "  int foo ();\n"
21580                "}",
21581                Style);
21582 }
21583 TEST_F(FormatTest, MozillaDefaultStyle) {
21584   FormatStyle Style = getMozillaStyle();
21585   verifyFormat("extern \"C\"\n"
21586                "{\n"
21587                "  int foo();\n"
21588                "}",
21589                Style);
21590 }
21591 TEST_F(FormatTest, GoogleDefaultStyle) {
21592   FormatStyle Style = getGoogleStyle();
21593   verifyFormat("extern \"C\" {\n"
21594                "int foo();\n"
21595                "}",
21596                Style);
21597 }
21598 TEST_F(FormatTest, ChromiumDefaultStyle) {
21599   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
21600   verifyFormat("extern \"C\" {\n"
21601                "int foo();\n"
21602                "}",
21603                Style);
21604 }
21605 TEST_F(FormatTest, MicrosoftDefaultStyle) {
21606   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
21607   verifyFormat("extern \"C\"\n"
21608                "{\n"
21609                "    int foo();\n"
21610                "}",
21611                Style);
21612 }
21613 TEST_F(FormatTest, WebKitDefaultStyle) {
21614   FormatStyle Style = getWebKitStyle();
21615   verifyFormat("extern \"C\" {\n"
21616                "int foo();\n"
21617                "}",
21618                Style);
21619 }
21620 
21621 TEST_F(FormatTest, ConceptsAndRequires) {
21622   FormatStyle Style = getLLVMStyle();
21623   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
21624 
21625   verifyFormat("template <typename T>\n"
21626                "concept Hashable = requires(T a) {\n"
21627                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
21628                "};",
21629                Style);
21630   verifyFormat("template <typename T>\n"
21631                "concept EqualityComparable = requires(T a, T b) {\n"
21632                "  { a == b } -> bool;\n"
21633                "};",
21634                Style);
21635   verifyFormat("template <typename T>\n"
21636                "concept EqualityComparable = requires(T a, T b) {\n"
21637                "  { a == b } -> bool;\n"
21638                "  { a != b } -> bool;\n"
21639                "};",
21640                Style);
21641   verifyFormat("template <typename T>\n"
21642                "concept EqualityComparable = requires(T a, T b) {\n"
21643                "  { a == b } -> bool;\n"
21644                "  { a != b } -> bool;\n"
21645                "};",
21646                Style);
21647 
21648   verifyFormat("template <typename It>\n"
21649                "requires Iterator<It>\n"
21650                "void sort(It begin, It end) {\n"
21651                "  //....\n"
21652                "}",
21653                Style);
21654 
21655   verifyFormat("template <typename T>\n"
21656                "concept Large = sizeof(T) > 10;",
21657                Style);
21658 
21659   verifyFormat("template <typename T, typename U>\n"
21660                "concept FooableWith = requires(T t, U u) {\n"
21661                "  typename T::foo_type;\n"
21662                "  { t.foo(u) } -> typename T::foo_type;\n"
21663                "  t++;\n"
21664                "};\n"
21665                "void doFoo(FooableWith<int> auto t) {\n"
21666                "  t.foo(3);\n"
21667                "}",
21668                Style);
21669   verifyFormat("template <typename T>\n"
21670                "concept Context = sizeof(T) == 1;",
21671                Style);
21672   verifyFormat("template <typename T>\n"
21673                "concept Context = is_specialization_of_v<context, T>;",
21674                Style);
21675   verifyFormat("template <typename T>\n"
21676                "concept Node = std::is_object_v<T>;",
21677                Style);
21678   verifyFormat("template <typename T>\n"
21679                "concept Tree = true;",
21680                Style);
21681 
21682   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
21683                "  //...\n"
21684                "}",
21685                Style);
21686 
21687   verifyFormat(
21688       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
21689       "  //...\n"
21690       "}",
21691       Style);
21692 
21693   verifyFormat(
21694       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
21695       "  //...\n"
21696       "}",
21697       Style);
21698 
21699   verifyFormat("template <typename T>\n"
21700                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
21701                "Concept2<I> {\n"
21702                "  //...\n"
21703                "}",
21704                Style);
21705 
21706   verifyFormat("template <typename T>\n"
21707                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
21708                "Concept2<I> {\n"
21709                "  //...\n"
21710                "}",
21711                Style);
21712 
21713   verifyFormat(
21714       "template <typename T>\n"
21715       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
21716       "  //...\n"
21717       "}",
21718       Style);
21719 
21720   verifyFormat(
21721       "template <typename T>\n"
21722       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
21723       "  //...\n"
21724       "}",
21725       Style);
21726 
21727   verifyFormat("template <typename It>\n"
21728                "requires Foo<It>() && Bar<It> {\n"
21729                "  //....\n"
21730                "}",
21731                Style);
21732 
21733   verifyFormat("template <typename It>\n"
21734                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
21735                "  //....\n"
21736                "}",
21737                Style);
21738 
21739   verifyFormat("template <typename It>\n"
21740                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
21741                "  //....\n"
21742                "}",
21743                Style);
21744 
21745   verifyFormat(
21746       "template <typename It>\n"
21747       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
21748       "  //....\n"
21749       "}",
21750       Style);
21751 
21752   Style.IndentRequires = true;
21753   verifyFormat("template <typename It>\n"
21754                "  requires Iterator<It>\n"
21755                "void sort(It begin, It end) {\n"
21756                "  //....\n"
21757                "}",
21758                Style);
21759   verifyFormat("template <std::size index_>\n"
21760                "  requires(index_ < sizeof...(Children_))\n"
21761                "Tree auto &child() {\n"
21762                "  // ...\n"
21763                "}",
21764                Style);
21765 
21766   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
21767   verifyFormat("template <typename T>\n"
21768                "concept Hashable = requires (T a) {\n"
21769                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
21770                "};",
21771                Style);
21772 
21773   verifyFormat("template <class T = void>\n"
21774                "  requires EqualityComparable<T> || Same<T, void>\n"
21775                "struct equal_to;",
21776                Style);
21777 
21778   verifyFormat("template <class T>\n"
21779                "  requires requires {\n"
21780                "    T{};\n"
21781                "    T (int);\n"
21782                "  }\n",
21783                Style);
21784 
21785   Style.ColumnLimit = 78;
21786   verifyFormat("template <typename T>\n"
21787                "concept Context = Traits<typename T::traits_type> and\n"
21788                "    Interface<typename T::interface_type> and\n"
21789                "    Request<typename T::request_type> and\n"
21790                "    Response<typename T::response_type> and\n"
21791                "    ContextExtension<typename T::extension_type> and\n"
21792                "    ::std::is_copy_constructable<T> and "
21793                "::std::is_move_constructable<T> and\n"
21794                "    requires (T c) {\n"
21795                "  { c.response; } -> Response;\n"
21796                "} and requires (T c) {\n"
21797                "  { c.request; } -> Request;\n"
21798                "}\n",
21799                Style);
21800 
21801   verifyFormat("template <typename T>\n"
21802                "concept Context = Traits<typename T::traits_type> or\n"
21803                "    Interface<typename T::interface_type> or\n"
21804                "    Request<typename T::request_type> or\n"
21805                "    Response<typename T::response_type> or\n"
21806                "    ContextExtension<typename T::extension_type> or\n"
21807                "    ::std::is_copy_constructable<T> or "
21808                "::std::is_move_constructable<T> or\n"
21809                "    requires (T c) {\n"
21810                "  { c.response; } -> Response;\n"
21811                "} or requires (T c) {\n"
21812                "  { c.request; } -> Request;\n"
21813                "}\n",
21814                Style);
21815 
21816   verifyFormat("template <typename T>\n"
21817                "concept Context = Traits<typename T::traits_type> &&\n"
21818                "    Interface<typename T::interface_type> &&\n"
21819                "    Request<typename T::request_type> &&\n"
21820                "    Response<typename T::response_type> &&\n"
21821                "    ContextExtension<typename T::extension_type> &&\n"
21822                "    ::std::is_copy_constructable<T> && "
21823                "::std::is_move_constructable<T> &&\n"
21824                "    requires (T c) {\n"
21825                "  { c.response; } -> Response;\n"
21826                "} && requires (T c) {\n"
21827                "  { c.request; } -> Request;\n"
21828                "}\n",
21829                Style);
21830 
21831   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
21832                "Constraint2<T>;");
21833 
21834   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
21835   Style.BraceWrapping.AfterFunction = true;
21836   Style.BraceWrapping.AfterClass = true;
21837   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
21838   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
21839   verifyFormat("void Foo () requires (std::copyable<T>)\n"
21840                "{\n"
21841                "  return\n"
21842                "}\n",
21843                Style);
21844 
21845   verifyFormat("void Foo () requires std::copyable<T>\n"
21846                "{\n"
21847                "  return\n"
21848                "}\n",
21849                Style);
21850 
21851   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
21852                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
21853                "struct constant;",
21854                Style);
21855 
21856   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
21857                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
21858                "struct constant;",
21859                Style);
21860 
21861   verifyFormat("template <class T>\n"
21862                "class plane_with_very_very_very_long_name\n"
21863                "{\n"
21864                "  constexpr plane_with_very_very_very_long_name () requires "
21865                "std::copyable<T>\n"
21866                "      : plane_with_very_very_very_long_name (1)\n"
21867                "  {\n"
21868                "  }\n"
21869                "}\n",
21870                Style);
21871 
21872   verifyFormat("template <class T>\n"
21873                "class plane_with_long_name\n"
21874                "{\n"
21875                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
21876                "      : plane_with_long_name (1)\n"
21877                "  {\n"
21878                "  }\n"
21879                "}\n",
21880                Style);
21881 
21882   Style.BreakBeforeConceptDeclarations = false;
21883   verifyFormat("template <typename T> concept Tree = true;", Style);
21884 
21885   Style.IndentRequires = false;
21886   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
21887                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
21888                "struct constant;",
21889                Style);
21890 }
21891 
21892 TEST_F(FormatTest, StatementAttributeLikeMacros) {
21893   FormatStyle Style = getLLVMStyle();
21894   StringRef Source = "void Foo::slot() {\n"
21895                      "  unsigned char MyChar = 'x';\n"
21896                      "  emit signal(MyChar);\n"
21897                      "  Q_EMIT signal(MyChar);\n"
21898                      "}";
21899 
21900   EXPECT_EQ(Source, format(Source, Style));
21901 
21902   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
21903   EXPECT_EQ("void Foo::slot() {\n"
21904             "  unsigned char MyChar = 'x';\n"
21905             "  emit          signal(MyChar);\n"
21906             "  Q_EMIT signal(MyChar);\n"
21907             "}",
21908             format(Source, Style));
21909 
21910   Style.StatementAttributeLikeMacros.push_back("emit");
21911   EXPECT_EQ(Source, format(Source, Style));
21912 
21913   Style.StatementAttributeLikeMacros = {};
21914   EXPECT_EQ("void Foo::slot() {\n"
21915             "  unsigned char MyChar = 'x';\n"
21916             "  emit          signal(MyChar);\n"
21917             "  Q_EMIT        signal(MyChar);\n"
21918             "}",
21919             format(Source, Style));
21920 }
21921 
21922 TEST_F(FormatTest, IndentAccessModifiers) {
21923   FormatStyle Style = getLLVMStyle();
21924   Style.IndentAccessModifiers = true;
21925   // Members are *two* levels below the record;
21926   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
21927   verifyFormat("class C {\n"
21928                "    int i;\n"
21929                "};\n",
21930                Style);
21931   verifyFormat("union C {\n"
21932                "    int i;\n"
21933                "    unsigned u;\n"
21934                "};\n",
21935                Style);
21936   // Access modifiers should be indented one level below the record.
21937   verifyFormat("class C {\n"
21938                "  public:\n"
21939                "    int i;\n"
21940                "};\n",
21941                Style);
21942   verifyFormat("struct S {\n"
21943                "  private:\n"
21944                "    class C {\n"
21945                "        int j;\n"
21946                "\n"
21947                "      public:\n"
21948                "        C();\n"
21949                "    };\n"
21950                "\n"
21951                "  public:\n"
21952                "    int i;\n"
21953                "};\n",
21954                Style);
21955   // Enumerations are not records and should be unaffected.
21956   Style.AllowShortEnumsOnASingleLine = false;
21957   verifyFormat("enum class E\n"
21958                "{\n"
21959                "  A,\n"
21960                "  B\n"
21961                "};\n",
21962                Style);
21963   // Test with a different indentation width;
21964   // also proves that the result is Style.AccessModifierOffset agnostic.
21965   Style.IndentWidth = 3;
21966   verifyFormat("class C {\n"
21967                "   public:\n"
21968                "      int i;\n"
21969                "};\n",
21970                Style);
21971 }
21972 
21973 TEST_F(FormatTest, LimitlessStringsAndComments) {
21974   auto Style = getLLVMStyleWithColumns(0);
21975   constexpr StringRef Code =
21976       "/**\n"
21977       " * This is a multiline comment with quite some long lines, at least for "
21978       "the LLVM Style.\n"
21979       " * We will redo this with strings and line comments. Just to  check if "
21980       "everything is working.\n"
21981       " */\n"
21982       "bool foo() {\n"
21983       "  /* Single line multi line comment. */\n"
21984       "  const std::string String = \"This is a multiline string with quite "
21985       "some long lines, at least for the LLVM Style.\"\n"
21986       "                             \"We already did it with multi line "
21987       "comments, and we will do it with line comments. Just to check if "
21988       "everything is working.\";\n"
21989       "  // This is a line comment (block) with quite some long lines, at "
21990       "least for the LLVM Style.\n"
21991       "  // We already did this with multi line comments and strings. Just to "
21992       "check if everything is working.\n"
21993       "  const std::string SmallString = \"Hello World\";\n"
21994       "  // Small line comment\n"
21995       "  return String.size() > SmallString.size();\n"
21996       "}";
21997   EXPECT_EQ(Code, format(Code, Style));
21998 }
21999 } // namespace
22000 } // namespace format
22001 } // namespace clang
22002