[clang-format] Improve detection of Objective-C block types
[lldb.git] / clang / unittests / Format / FormatTest.cpp
1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "clang/Format/Format.h"
11
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
14
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19
20 #define DEBUG_TYPE "format-test"
21
22 using clang::tooling::ReplacementTest;
23 using clang::tooling::toReplacements;
24
25 namespace clang {
26 namespace format {
27 namespace {
28
29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
30
31 class FormatTest : public ::testing::Test {
32 protected:
33   enum StatusCheck {
34     SC_ExpectComplete,
35     SC_ExpectIncomplete,
36     SC_DoNotCheck
37   };
38
39   std::string format(llvm::StringRef Code,
40                      const FormatStyle &Style = getLLVMStyle(),
41                      StatusCheck CheckComplete = SC_ExpectComplete) {
42     DEBUG(llvm::errs() << "---\n");
43     DEBUG(llvm::errs() << Code << "\n\n");
44     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
45     FormattingAttemptStatus Status;
46     tooling::Replacements Replaces =
47         reformat(Style, Code, Ranges, "<stdin>", &Status);
48     if (CheckComplete != SC_DoNotCheck) {
49       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
50       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
51           << Code << "\n\n";
52     }
53     ReplacementCount = Replaces.size();
54     auto Result = applyAllReplacements(Code, Replaces);
55     EXPECT_TRUE(static_cast<bool>(Result));
56     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
57     return *Result;
58   }
59
60   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64
65   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
66     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
67   }
68
69   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
70     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
71   }
72
73   void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code,
74                     const FormatStyle &Style = getLLVMStyle()) {
75     EXPECT_EQ(Expected.str(), format(Code, Style));
76     if (Style.Language == FormatStyle::LK_Cpp) {
77       // Objective-C++ is a superset of C++, so everything checked for C++
78       // needs to be checked for Objective-C++ as well.
79       FormatStyle ObjCStyle = Style;
80       ObjCStyle.Language = FormatStyle::LK_ObjC;
81       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
82     }
83   }
84
85   void verifyFormat(llvm::StringRef Code,
86                     const FormatStyle &Style = getLLVMStyle()) {
87     verifyFormat(Code, test::messUp(Code), Style);
88   }
89
90   void verifyIncompleteFormat(llvm::StringRef Code,
91                               const FormatStyle &Style = getLLVMStyle()) {
92     EXPECT_EQ(Code.str(),
93               format(test::messUp(Code), Style, SC_ExpectIncomplete));
94   }
95
96   void verifyGoogleFormat(llvm::StringRef Code) {
97     verifyFormat(Code, getGoogleStyle());
98   }
99
100   void verifyIndependentOfContext(llvm::StringRef text) {
101     verifyFormat(text);
102     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
103   }
104
105   /// \brief Verify that clang-format does not crash on the given input.
106   void verifyNoCrash(llvm::StringRef Code,
107                      const FormatStyle &Style = getLLVMStyle()) {
108     format(Code, Style, SC_DoNotCheck);
109   }
110
111   int ReplacementCount;
112 };
113
114 TEST_F(FormatTest, MessUp) {
115   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
116   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
117   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
118   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
119   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
120 }
121
122 //===----------------------------------------------------------------------===//
123 // Basic function tests.
124 //===----------------------------------------------------------------------===//
125
126 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
127   EXPECT_EQ(";", format(";"));
128 }
129
130 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
131   EXPECT_EQ("int i;", format("  int i;"));
132   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
133   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
134   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
135 }
136
137 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
138   EXPECT_EQ("int i;", format("int\ni;"));
139 }
140
141 TEST_F(FormatTest, FormatsNestedBlockStatements) {
142   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
143 }
144
145 TEST_F(FormatTest, FormatsNestedCall) {
146   verifyFormat("Method(f1, f2(f3));");
147   verifyFormat("Method(f1(f2, f3()));");
148   verifyFormat("Method(f1(f2, (f3())));");
149 }
150
151 TEST_F(FormatTest, NestedNameSpecifiers) {
152   verifyFormat("vector<::Type> v;");
153   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
154   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
155   verifyFormat("bool a = 2 < ::SomeFunction();");
156   verifyFormat("ALWAYS_INLINE ::std::string getName();");
157   verifyFormat("some::string getName();");
158 }
159
160 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
161   EXPECT_EQ("if (a) {\n"
162             "  f();\n"
163             "}",
164             format("if(a){f();}"));
165   EXPECT_EQ(4, ReplacementCount);
166   EXPECT_EQ("if (a) {\n"
167             "  f();\n"
168             "}",
169             format("if (a) {\n"
170                    "  f();\n"
171                    "}"));
172   EXPECT_EQ(0, ReplacementCount);
173   EXPECT_EQ("/*\r\n"
174             "\r\n"
175             "*/\r\n",
176             format("/*\r\n"
177                    "\r\n"
178                    "*/\r\n"));
179   EXPECT_EQ(0, ReplacementCount);
180 }
181
182 TEST_F(FormatTest, RemovesEmptyLines) {
183   EXPECT_EQ("class C {\n"
184             "  int i;\n"
185             "};",
186             format("class C {\n"
187                    " int i;\n"
188                    "\n"
189                    "};"));
190
191   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
192   EXPECT_EQ("namespace N {\n"
193             "\n"
194             "int i;\n"
195             "}",
196             format("namespace N {\n"
197                    "\n"
198                    "int    i;\n"
199                    "}",
200                    getGoogleStyle()));
201   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
202             "\n"
203             "int i;\n"
204             "}",
205             format("extern /**/ \"C\" /**/ {\n"
206                    "\n"
207                    "int    i;\n"
208                    "}",
209                    getGoogleStyle()));
210
211   // ...but do keep inlining and removing empty lines for non-block extern "C"
212   // functions.
213   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
214   EXPECT_EQ("extern \"C\" int f() {\n"
215             "  int i = 42;\n"
216             "  return i;\n"
217             "}",
218             format("extern \"C\" int f() {\n"
219                    "\n"
220                    "  int i = 42;\n"
221                    "  return i;\n"
222                    "}",
223                    getGoogleStyle()));
224
225   // Remove empty lines at the beginning and end of blocks.
226   EXPECT_EQ("void f() {\n"
227             "\n"
228             "  if (a) {\n"
229             "\n"
230             "    f();\n"
231             "  }\n"
232             "}",
233             format("void f() {\n"
234                    "\n"
235                    "  if (a) {\n"
236                    "\n"
237                    "    f();\n"
238                    "\n"
239                    "  }\n"
240                    "\n"
241                    "}",
242                    getLLVMStyle()));
243   EXPECT_EQ("void f() {\n"
244             "  if (a) {\n"
245             "    f();\n"
246             "  }\n"
247             "}",
248             format("void f() {\n"
249                    "\n"
250                    "  if (a) {\n"
251                    "\n"
252                    "    f();\n"
253                    "\n"
254                    "  }\n"
255                    "\n"
256                    "}",
257                    getGoogleStyle()));
258
259   // Don't remove empty lines in more complex control statements.
260   EXPECT_EQ("void f() {\n"
261             "  if (a) {\n"
262             "    f();\n"
263             "\n"
264             "  } else if (b) {\n"
265             "    f();\n"
266             "  }\n"
267             "}",
268             format("void f() {\n"
269                    "  if (a) {\n"
270                    "    f();\n"
271                    "\n"
272                    "  } else if (b) {\n"
273                    "    f();\n"
274                    "\n"
275                    "  }\n"
276                    "\n"
277                    "}"));
278
279   // FIXME: This is slightly inconsistent.
280   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
281   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
282   EXPECT_EQ("namespace {\n"
283             "int i;\n"
284             "}",
285             format("namespace {\n"
286                    "int i;\n"
287                    "\n"
288                    "}", LLVMWithNoNamespaceFix));
289   EXPECT_EQ("namespace {\n"
290             "int i;\n"
291             "}",
292             format("namespace {\n"
293                    "int i;\n"
294                    "\n"
295                    "}"));
296   EXPECT_EQ("namespace {\n"
297             "int i;\n"
298             "\n"
299             "} // namespace",
300             format("namespace {\n"
301                    "int i;\n"
302                    "\n"
303                    "}  // namespace"));
304
305   FormatStyle Style = getLLVMStyle();
306   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
307   Style.MaxEmptyLinesToKeep = 2;
308   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
309   Style.BraceWrapping.AfterClass = true;
310   Style.BraceWrapping.AfterFunction = true;
311   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
312
313   EXPECT_EQ("class Foo\n"
314             "{\n"
315             "  Foo() {}\n"
316             "\n"
317             "  void funk() {}\n"
318             "};",
319             format("class Foo\n"
320                    "{\n"
321                    "  Foo()\n"
322                    "  {\n"
323                    "  }\n"
324                    "\n"
325                    "  void funk() {}\n"
326                    "};",
327                    Style));
328 }
329
330 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
331   verifyFormat("x = (a) and (b);");
332   verifyFormat("x = (a) or (b);");
333   verifyFormat("x = (a) bitand (b);");
334   verifyFormat("x = (a) bitor (b);");
335   verifyFormat("x = (a) not_eq (b);");
336   verifyFormat("x = (a) and_eq (b);");
337   verifyFormat("x = (a) or_eq (b);");
338   verifyFormat("x = (a) xor (b);");
339 }
340
341 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
342   verifyFormat("x = compl(a);");
343   verifyFormat("x = not(a);");
344   verifyFormat("x = bitand(a);");
345   // Unary operator must not be merged with the next identifier
346   verifyFormat("x = compl a;");
347   verifyFormat("x = not a;");
348   verifyFormat("x = bitand a;");
349 }
350
351 //===----------------------------------------------------------------------===//
352 // Tests for control statements.
353 //===----------------------------------------------------------------------===//
354
355 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
356   verifyFormat("if (true)\n  f();\ng();");
357   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
358   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
359   verifyFormat("if constexpr (true)\n"
360                "  f();\ng();");
361   verifyFormat("if constexpr (a)\n"
362                "  if constexpr (b)\n"
363                "    if constexpr (c)\n"
364                "      g();\n"
365                "h();");
366   verifyFormat("if constexpr (a)\n"
367                "  if constexpr (b) {\n"
368                "    f();\n"
369                "  }\n"
370                "g();");
371
372   FormatStyle AllowsMergedIf = getLLVMStyle();
373   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
374   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
375   verifyFormat("if (a)\n"
376                "  // comment\n"
377                "  f();",
378                AllowsMergedIf);
379   verifyFormat("{\n"
380                "  if (a)\n"
381                "  label:\n"
382                "    f();\n"
383                "}",
384                AllowsMergedIf);
385   verifyFormat("#define A \\\n"
386                "  if (a)  \\\n"
387                "  label:  \\\n"
388                "    f()",
389                AllowsMergedIf);
390   verifyFormat("if (a)\n"
391                "  ;",
392                AllowsMergedIf);
393   verifyFormat("if (a)\n"
394                "  if (b) return;",
395                AllowsMergedIf);
396
397   verifyFormat("if (a) // Can't merge this\n"
398                "  f();\n",
399                AllowsMergedIf);
400   verifyFormat("if (a) /* still don't merge */\n"
401                "  f();",
402                AllowsMergedIf);
403   verifyFormat("if (a) { // Never merge this\n"
404                "  f();\n"
405                "}",
406                AllowsMergedIf);
407   verifyFormat("if (a) { /* Never merge this */\n"
408                "  f();\n"
409                "}",
410                AllowsMergedIf);
411
412   AllowsMergedIf.ColumnLimit = 14;
413   verifyFormat("if (a) return;", AllowsMergedIf);
414   verifyFormat("if (aaaaaaaaa)\n"
415                "  return;",
416                AllowsMergedIf);
417
418   AllowsMergedIf.ColumnLimit = 13;
419   verifyFormat("if (a)\n  return;", AllowsMergedIf);
420 }
421
422 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
423   FormatStyle AllowsMergedLoops = getLLVMStyle();
424   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
425   verifyFormat("while (true) continue;", AllowsMergedLoops);
426   verifyFormat("for (;;) continue;", AllowsMergedLoops);
427   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
428   verifyFormat("while (true)\n"
429                "  ;",
430                AllowsMergedLoops);
431   verifyFormat("for (;;)\n"
432                "  ;",
433                AllowsMergedLoops);
434   verifyFormat("for (;;)\n"
435                "  for (;;) continue;",
436                AllowsMergedLoops);
437   verifyFormat("for (;;) // Can't merge this\n"
438                "  continue;",
439                AllowsMergedLoops);
440   verifyFormat("for (;;) /* still don't merge */\n"
441                "  continue;",
442                AllowsMergedLoops);
443 }
444
445 TEST_F(FormatTest, FormatShortBracedStatements) {
446   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
447   AllowSimpleBracedStatements.ColumnLimit = 40;
448   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
449
450   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
451   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
452
453   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
454   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
455   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
456
457   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
458   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
459   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
460   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
461   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
462   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
463   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
464   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
465   verifyFormat("if (true) {\n"
466                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
467                "}",
468                AllowSimpleBracedStatements);
469   verifyFormat("if (true) { //\n"
470                "  f();\n"
471                "}",
472                AllowSimpleBracedStatements);
473   verifyFormat("if (true) {\n"
474                "  f();\n"
475                "  f();\n"
476                "}",
477                AllowSimpleBracedStatements);
478   verifyFormat("if (true) {\n"
479                "  f();\n"
480                "} else {\n"
481                "  f();\n"
482                "}",
483                AllowSimpleBracedStatements);
484
485   verifyFormat("struct A2 {\n"
486                "  int X;\n"
487                "};",
488                AllowSimpleBracedStatements);
489   verifyFormat("typedef struct A2 {\n"
490                "  int X;\n"
491                "} A2_t;",
492                AllowSimpleBracedStatements);
493   verifyFormat("template <int> struct A2 {\n"
494                "  struct B {};\n"
495                "};",
496                AllowSimpleBracedStatements);
497
498   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
499   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
500   verifyFormat("if (true) {\n"
501                "  f();\n"
502                "}",
503                AllowSimpleBracedStatements);
504   verifyFormat("if (true) {\n"
505                "  f();\n"
506                "} else {\n"
507                "  f();\n"
508                "}",
509                AllowSimpleBracedStatements);
510
511   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
512   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
513   verifyFormat("while (true) {\n"
514                "  f();\n"
515                "}",
516                AllowSimpleBracedStatements);
517   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
518   verifyFormat("for (;;) {\n"
519                "  f();\n"
520                "}",
521                AllowSimpleBracedStatements);
522
523   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
524   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
525   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
526
527   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
528   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
529   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
530   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
531   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
532   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
533   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
534   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
535   verifyFormat("if (true)\n"
536                "{\n"
537                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
538                "}",
539                AllowSimpleBracedStatements);
540   verifyFormat("if (true)\n"
541                "{ //\n"
542                "  f();\n"
543                "}",
544                AllowSimpleBracedStatements);
545   verifyFormat("if (true)\n"
546                "{\n"
547                "  f();\n"
548                "  f();\n"
549                "}",
550                AllowSimpleBracedStatements);
551   verifyFormat("if (true)\n"
552                "{\n"
553                "  f();\n"
554                "} else\n"
555                "{\n"
556                "  f();\n"
557                "}",
558                AllowSimpleBracedStatements);
559
560   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
561   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
562   verifyFormat("if (true)\n"
563                "{\n"
564                "  f();\n"
565                "}",
566                AllowSimpleBracedStatements);
567   verifyFormat("if (true)\n"
568                "{\n"
569                "  f();\n"
570                "} else\n"
571                "{\n"
572                "  f();\n"
573                "}",
574                AllowSimpleBracedStatements);
575
576   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
577   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
578   verifyFormat("while (true)\n"
579                "{\n"
580                "  f();\n"
581                "}",
582                AllowSimpleBracedStatements);
583   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
584   verifyFormat("for (;;)\n"
585                "{\n"
586                "  f();\n"
587                "}",
588                AllowSimpleBracedStatements);
589 }
590
591 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
592   FormatStyle Style = getLLVMStyleWithColumns(60);
593   Style.AllowShortBlocksOnASingleLine = true;
594   Style.AllowShortIfStatementsOnASingleLine = true;
595   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
596   EXPECT_EQ("#define A                                                  \\\n"
597             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
598             "  { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n"
599             "X;",
600             format("#define A \\\n"
601                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
602                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
603                    "   }\n"
604                    "X;",
605                    Style));
606 }
607
608 TEST_F(FormatTest, ParseIfElse) {
609   verifyFormat("if (true)\n"
610                "  if (true)\n"
611                "    if (true)\n"
612                "      f();\n"
613                "    else\n"
614                "      g();\n"
615                "  else\n"
616                "    h();\n"
617                "else\n"
618                "  i();");
619   verifyFormat("if (true)\n"
620                "  if (true)\n"
621                "    if (true) {\n"
622                "      if (true)\n"
623                "        f();\n"
624                "    } else {\n"
625                "      g();\n"
626                "    }\n"
627                "  else\n"
628                "    h();\n"
629                "else {\n"
630                "  i();\n"
631                "}");
632   verifyFormat("if (true)\n"
633                "  if constexpr (true)\n"
634                "    if (true) {\n"
635                "      if constexpr (true)\n"
636                "        f();\n"
637                "    } else {\n"
638                "      g();\n"
639                "    }\n"
640                "  else\n"
641                "    h();\n"
642                "else {\n"
643                "  i();\n"
644                "}");
645   verifyFormat("void f() {\n"
646                "  if (a) {\n"
647                "  } else {\n"
648                "  }\n"
649                "}");
650 }
651
652 TEST_F(FormatTest, ElseIf) {
653   verifyFormat("if (a) {\n} else if (b) {\n}");
654   verifyFormat("if (a)\n"
655                "  f();\n"
656                "else if (b)\n"
657                "  g();\n"
658                "else\n"
659                "  h();");
660   verifyFormat("if constexpr (a)\n"
661                "  f();\n"
662                "else if constexpr (b)\n"
663                "  g();\n"
664                "else\n"
665                "  h();");
666   verifyFormat("if (a) {\n"
667                "  f();\n"
668                "}\n"
669                "// or else ..\n"
670                "else {\n"
671                "  g()\n"
672                "}");
673
674   verifyFormat("if (a) {\n"
675                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
676                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
677                "}");
678   verifyFormat("if (a) {\n"
679                "} else if (\n"
680                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
681                "}",
682                getLLVMStyleWithColumns(62));
683   verifyFormat("if (a) {\n"
684                "} else if constexpr (\n"
685                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
686                "}",
687                getLLVMStyleWithColumns(62));
688 }
689
690 TEST_F(FormatTest, FormatsForLoop) {
691   verifyFormat(
692       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
693       "     ++VeryVeryLongLoopVariable)\n"
694       "  ;");
695   verifyFormat("for (;;)\n"
696                "  f();");
697   verifyFormat("for (;;) {\n}");
698   verifyFormat("for (;;) {\n"
699                "  f();\n"
700                "}");
701   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
702
703   verifyFormat(
704       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
705       "                                          E = UnwrappedLines.end();\n"
706       "     I != E; ++I) {\n}");
707
708   verifyFormat(
709       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
710       "     ++IIIII) {\n}");
711   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
712                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
713                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
714   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
715                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
716                "         E = FD->getDeclsInPrototypeScope().end();\n"
717                "     I != E; ++I) {\n}");
718   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
719                "         I = Container.begin(),\n"
720                "         E = Container.end();\n"
721                "     I != E; ++I) {\n}",
722                getLLVMStyleWithColumns(76));
723
724   verifyFormat(
725       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
726       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
727       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
728       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
729       "     ++aaaaaaaaaaa) {\n}");
730   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
731                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
732                "     ++i) {\n}");
733   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
734                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
735                "}");
736   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
737                "         aaaaaaaaaa);\n"
738                "     iter; ++iter) {\n"
739                "}");
740   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
741                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
742                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
743                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
744
745   // These should not be formatted as Objective-C for-in loops.
746   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
747   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
748   verifyFormat("Foo *x;\nfor (x in y) {\n}");
749   verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
750
751   FormatStyle NoBinPacking = getLLVMStyle();
752   NoBinPacking.BinPackParameters = false;
753   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
754                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
755                "                                           aaaaaaaaaaaaaaaa,\n"
756                "                                           aaaaaaaaaaaaaaaa,\n"
757                "                                           aaaaaaaaaaaaaaaa);\n"
758                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
759                "}",
760                NoBinPacking);
761   verifyFormat(
762       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
763       "                                          E = UnwrappedLines.end();\n"
764       "     I != E;\n"
765       "     ++I) {\n}",
766       NoBinPacking);
767
768   FormatStyle AlignLeft = getLLVMStyle();
769   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
770   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
771 }
772
773 TEST_F(FormatTest, RangeBasedForLoops) {
774   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
775                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
776   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
777                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
778   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
779                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
780   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
781                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
782 }
783
784 TEST_F(FormatTest, ForEachLoops) {
785   verifyFormat("void f() {\n"
786                "  foreach (Item *item, itemlist) {}\n"
787                "  Q_FOREACH (Item *item, itemlist) {}\n"
788                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
789                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
790                "}");
791
792   // As function-like macros.
793   verifyFormat("#define foreach(x, y)\n"
794                "#define Q_FOREACH(x, y)\n"
795                "#define BOOST_FOREACH(x, y)\n"
796                "#define UNKNOWN_FOREACH(x, y)\n");
797
798   // Not as function-like macros.
799   verifyFormat("#define foreach (x, y)\n"
800                "#define Q_FOREACH (x, y)\n"
801                "#define BOOST_FOREACH (x, y)\n"
802                "#define UNKNOWN_FOREACH (x, y)\n");
803 }
804
805 TEST_F(FormatTest, FormatsWhileLoop) {
806   verifyFormat("while (true) {\n}");
807   verifyFormat("while (true)\n"
808                "  f();");
809   verifyFormat("while () {\n}");
810   verifyFormat("while () {\n"
811                "  f();\n"
812                "}");
813 }
814
815 TEST_F(FormatTest, FormatsDoWhile) {
816   verifyFormat("do {\n"
817                "  do_something();\n"
818                "} while (something());");
819   verifyFormat("do\n"
820                "  do_something();\n"
821                "while (something());");
822 }
823
824 TEST_F(FormatTest, FormatsSwitchStatement) {
825   verifyFormat("switch (x) {\n"
826                "case 1:\n"
827                "  f();\n"
828                "  break;\n"
829                "case kFoo:\n"
830                "case ns::kBar:\n"
831                "case kBaz:\n"
832                "  break;\n"
833                "default:\n"
834                "  g();\n"
835                "  break;\n"
836                "}");
837   verifyFormat("switch (x) {\n"
838                "case 1: {\n"
839                "  f();\n"
840                "  break;\n"
841                "}\n"
842                "case 2: {\n"
843                "  break;\n"
844                "}\n"
845                "}");
846   verifyFormat("switch (x) {\n"
847                "case 1: {\n"
848                "  f();\n"
849                "  {\n"
850                "    g();\n"
851                "    h();\n"
852                "  }\n"
853                "  break;\n"
854                "}\n"
855                "}");
856   verifyFormat("switch (x) {\n"
857                "case 1: {\n"
858                "  f();\n"
859                "  if (foo) {\n"
860                "    g();\n"
861                "    h();\n"
862                "  }\n"
863                "  break;\n"
864                "}\n"
865                "}");
866   verifyFormat("switch (x) {\n"
867                "case 1: {\n"
868                "  f();\n"
869                "  g();\n"
870                "} break;\n"
871                "}");
872   verifyFormat("switch (test)\n"
873                "  ;");
874   verifyFormat("switch (x) {\n"
875                "default: {\n"
876                "  // Do nothing.\n"
877                "}\n"
878                "}");
879   verifyFormat("switch (x) {\n"
880                "// comment\n"
881                "// if 1, do f()\n"
882                "case 1:\n"
883                "  f();\n"
884                "}");
885   verifyFormat("switch (x) {\n"
886                "case 1:\n"
887                "  // Do amazing stuff\n"
888                "  {\n"
889                "    f();\n"
890                "    g();\n"
891                "  }\n"
892                "  break;\n"
893                "}");
894   verifyFormat("#define A          \\\n"
895                "  switch (x) {     \\\n"
896                "  case a:          \\\n"
897                "    foo = b;       \\\n"
898                "  }",
899                getLLVMStyleWithColumns(20));
900   verifyFormat("#define OPERATION_CASE(name)           \\\n"
901                "  case OP_name:                        \\\n"
902                "    return operations::Operation##name\n",
903                getLLVMStyleWithColumns(40));
904   verifyFormat("switch (x) {\n"
905                "case 1:;\n"
906                "default:;\n"
907                "  int i;\n"
908                "}");
909
910   verifyGoogleFormat("switch (x) {\n"
911                      "  case 1:\n"
912                      "    f();\n"
913                      "    break;\n"
914                      "  case kFoo:\n"
915                      "  case ns::kBar:\n"
916                      "  case kBaz:\n"
917                      "    break;\n"
918                      "  default:\n"
919                      "    g();\n"
920                      "    break;\n"
921                      "}");
922   verifyGoogleFormat("switch (x) {\n"
923                      "  case 1: {\n"
924                      "    f();\n"
925                      "    break;\n"
926                      "  }\n"
927                      "}");
928   verifyGoogleFormat("switch (test)\n"
929                      "  ;");
930
931   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
932                      "  case OP_name:              \\\n"
933                      "    return operations::Operation##name\n");
934   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
935                      "  // Get the correction operation class.\n"
936                      "  switch (OpCode) {\n"
937                      "    CASE(Add);\n"
938                      "    CASE(Subtract);\n"
939                      "    default:\n"
940                      "      return operations::Unknown;\n"
941                      "  }\n"
942                      "#undef OPERATION_CASE\n"
943                      "}");
944   verifyFormat("DEBUG({\n"
945                "  switch (x) {\n"
946                "  case A:\n"
947                "    f();\n"
948                "    break;\n"
949                "    // fallthrough\n"
950                "  case B:\n"
951                "    g();\n"
952                "    break;\n"
953                "  }\n"
954                "});");
955   EXPECT_EQ("DEBUG({\n"
956             "  switch (x) {\n"
957             "  case A:\n"
958             "    f();\n"
959             "    break;\n"
960             "  // On B:\n"
961             "  case B:\n"
962             "    g();\n"
963             "    break;\n"
964             "  }\n"
965             "});",
966             format("DEBUG({\n"
967                    "  switch (x) {\n"
968                    "  case A:\n"
969                    "    f();\n"
970                    "    break;\n"
971                    "  // On B:\n"
972                    "  case B:\n"
973                    "    g();\n"
974                    "    break;\n"
975                    "  }\n"
976                    "});",
977                    getLLVMStyle()));
978   verifyFormat("switch (a) {\n"
979                "case (b):\n"
980                "  return;\n"
981                "}");
982
983   verifyFormat("switch (a) {\n"
984                "case some_namespace::\n"
985                "    some_constant:\n"
986                "  return;\n"
987                "}",
988                getLLVMStyleWithColumns(34));
989 }
990
991 TEST_F(FormatTest, CaseRanges) {
992   verifyFormat("switch (x) {\n"
993                "case 'A' ... 'Z':\n"
994                "case 1 ... 5:\n"
995                "case a ... b:\n"
996                "  break;\n"
997                "}");
998 }
999
1000 TEST_F(FormatTest, ShortCaseLabels) {
1001   FormatStyle Style = getLLVMStyle();
1002   Style.AllowShortCaseLabelsOnASingleLine = true;
1003   verifyFormat("switch (a) {\n"
1004                "case 1: x = 1; break;\n"
1005                "case 2: return;\n"
1006                "case 3:\n"
1007                "case 4:\n"
1008                "case 5: return;\n"
1009                "case 6: // comment\n"
1010                "  return;\n"
1011                "case 7:\n"
1012                "  // comment\n"
1013                "  return;\n"
1014                "case 8:\n"
1015                "  x = 8; // comment\n"
1016                "  break;\n"
1017                "default: y = 1; break;\n"
1018                "}",
1019                Style);
1020   verifyFormat("switch (a) {\n"
1021                "case 0: return; // comment\n"
1022                "case 1: break;  // comment\n"
1023                "case 2: return;\n"
1024                "// comment\n"
1025                "case 3: return;\n"
1026                "// comment 1\n"
1027                "// comment 2\n"
1028                "// comment 3\n"
1029                "case 4: break; /* comment */\n"
1030                "case 5:\n"
1031                "  // comment\n"
1032                "  break;\n"
1033                "case 6: /* comment */ x = 1; break;\n"
1034                "case 7: x = /* comment */ 1; break;\n"
1035                "case 8:\n"
1036                "  x = 1; /* comment */\n"
1037                "  break;\n"
1038                "case 9:\n"
1039                "  break; // comment line 1\n"
1040                "         // comment line 2\n"
1041                "}",
1042                Style);
1043   EXPECT_EQ("switch (a) {\n"
1044             "case 1:\n"
1045             "  x = 8;\n"
1046             "  // fall through\n"
1047             "case 2: x = 8;\n"
1048             "// comment\n"
1049             "case 3:\n"
1050             "  return; /* comment line 1\n"
1051             "           * comment line 2 */\n"
1052             "case 4: i = 8;\n"
1053             "// something else\n"
1054             "#if FOO\n"
1055             "case 5: break;\n"
1056             "#endif\n"
1057             "}",
1058             format("switch (a) {\n"
1059                    "case 1: x = 8;\n"
1060                    "  // fall through\n"
1061                    "case 2:\n"
1062                    "  x = 8;\n"
1063                    "// comment\n"
1064                    "case 3:\n"
1065                    "  return; /* comment line 1\n"
1066                    "           * comment line 2 */\n"
1067                    "case 4:\n"
1068                    "  i = 8;\n"
1069                    "// something else\n"
1070                    "#if FOO\n"
1071                    "case 5: break;\n"
1072                    "#endif\n"
1073                    "}",
1074                    Style));
1075   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1076             "  return; // long long long long long long long long long long long long comment\n"
1077             "          // line\n" "}",
1078             format("switch (a) {\n"
1079                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1080                    "}",
1081                    Style));
1082   EXPECT_EQ("switch (a) {\n"
1083             "case 0:\n"
1084             "  return; /* long long long long long long long long long long long long comment\n"
1085             "             line */\n"
1086             "}",
1087             format("switch (a) {\n"
1088                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1089                    "}",
1090                    Style));
1091   verifyFormat("switch (a) {\n"
1092                "#if FOO\n"
1093                "case 0: return 0;\n"
1094                "#endif\n"
1095                "}",
1096                Style);
1097   verifyFormat("switch (a) {\n"
1098                "case 1: {\n"
1099                "}\n"
1100                "case 2: {\n"
1101                "  return;\n"
1102                "}\n"
1103                "case 3: {\n"
1104                "  x = 1;\n"
1105                "  return;\n"
1106                "}\n"
1107                "case 4:\n"
1108                "  if (x)\n"
1109                "    return;\n"
1110                "}",
1111                Style);
1112   Style.ColumnLimit = 21;
1113   verifyFormat("switch (a) {\n"
1114                "case 1: x = 1; break;\n"
1115                "case 2: return;\n"
1116                "case 3:\n"
1117                "case 4:\n"
1118                "case 5: return;\n"
1119                "default:\n"
1120                "  y = 1;\n"
1121                "  break;\n"
1122                "}",
1123                Style);
1124 }
1125
1126 TEST_F(FormatTest, FormatsLabels) {
1127   verifyFormat("void f() {\n"
1128                "  some_code();\n"
1129                "test_label:\n"
1130                "  some_other_code();\n"
1131                "  {\n"
1132                "    some_more_code();\n"
1133                "  another_label:\n"
1134                "    some_more_code();\n"
1135                "  }\n"
1136                "}");
1137   verifyFormat("{\n"
1138                "  some_code();\n"
1139                "test_label:\n"
1140                "  some_other_code();\n"
1141                "}");
1142   verifyFormat("{\n"
1143                "  some_code();\n"
1144                "test_label:;\n"
1145                "  int i = 0;\n"
1146                "}");
1147 }
1148
1149 //===----------------------------------------------------------------------===//
1150 // Tests for classes, namespaces, etc.
1151 //===----------------------------------------------------------------------===//
1152
1153 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1154   verifyFormat("class A {};");
1155 }
1156
1157 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1158   verifyFormat("class A {\n"
1159                "public:\n"
1160                "public: // comment\n"
1161                "protected:\n"
1162                "private:\n"
1163                "  void f() {}\n"
1164                "};");
1165   verifyGoogleFormat("class A {\n"
1166                      " public:\n"
1167                      " protected:\n"
1168                      " private:\n"
1169                      "  void f() {}\n"
1170                      "};");
1171   verifyFormat("class A {\n"
1172                "public slots:\n"
1173                "  void f1() {}\n"
1174                "public Q_SLOTS:\n"
1175                "  void f2() {}\n"
1176                "protected slots:\n"
1177                "  void f3() {}\n"
1178                "protected Q_SLOTS:\n"
1179                "  void f4() {}\n"
1180                "private slots:\n"
1181                "  void f5() {}\n"
1182                "private Q_SLOTS:\n"
1183                "  void f6() {}\n"
1184                "signals:\n"
1185                "  void g1();\n"
1186                "Q_SIGNALS:\n"
1187                "  void g2();\n"
1188                "};");
1189
1190   // Don't interpret 'signals' the wrong way.
1191   verifyFormat("signals.set();");
1192   verifyFormat("for (Signals signals : f()) {\n}");
1193   verifyFormat("{\n"
1194                "  signals.set(); // This needs indentation.\n"
1195                "}");
1196   verifyFormat("void f() {\n"
1197                "label:\n"
1198                "  signals.baz();\n"
1199                "}");
1200 }
1201
1202 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1203   EXPECT_EQ("class A {\n"
1204             "public:\n"
1205             "  void f();\n"
1206             "\n"
1207             "private:\n"
1208             "  void g() {}\n"
1209             "  // test\n"
1210             "protected:\n"
1211             "  int h;\n"
1212             "};",
1213             format("class A {\n"
1214                    "public:\n"
1215                    "void f();\n"
1216                    "private:\n"
1217                    "void g() {}\n"
1218                    "// test\n"
1219                    "protected:\n"
1220                    "int h;\n"
1221                    "};"));
1222   EXPECT_EQ("class A {\n"
1223             "protected:\n"
1224             "public:\n"
1225             "  void f();\n"
1226             "};",
1227             format("class A {\n"
1228                    "protected:\n"
1229                    "\n"
1230                    "public:\n"
1231                    "\n"
1232                    "  void f();\n"
1233                    "};"));
1234
1235   // Even ensure proper spacing inside macros.
1236   EXPECT_EQ("#define B     \\\n"
1237             "  class A {   \\\n"
1238             "   protected: \\\n"
1239             "   public:    \\\n"
1240             "    void f(); \\\n"
1241             "  };",
1242             format("#define B     \\\n"
1243                    "  class A {   \\\n"
1244                    "   protected: \\\n"
1245                    "              \\\n"
1246                    "   public:    \\\n"
1247                    "              \\\n"
1248                    "    void f(); \\\n"
1249                    "  };",
1250                    getGoogleStyle()));
1251   // But don't remove empty lines after macros ending in access specifiers.
1252   EXPECT_EQ("#define A private:\n"
1253             "\n"
1254             "int i;",
1255             format("#define A         private:\n"
1256                    "\n"
1257                    "int              i;"));
1258 }
1259
1260 TEST_F(FormatTest, FormatsClasses) {
1261   verifyFormat("class A : public B {};");
1262   verifyFormat("class A : public ::B {};");
1263
1264   verifyFormat(
1265       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1266       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1267   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1268                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1269                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1270   verifyFormat(
1271       "class A : public B, public C, public D, public E, public F {};");
1272   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1273                "                     public C,\n"
1274                "                     public D,\n"
1275                "                     public E,\n"
1276                "                     public F,\n"
1277                "                     public G {};");
1278
1279   verifyFormat("class\n"
1280                "    ReallyReallyLongClassName {\n"
1281                "  int i;\n"
1282                "};",
1283                getLLVMStyleWithColumns(32));
1284   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1285                "                           aaaaaaaaaaaaaaaa> {};");
1286   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1287                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1288                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1289   verifyFormat("template <class R, class C>\n"
1290                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1291                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1292   verifyFormat("class ::A::B {};");
1293 }
1294
1295 TEST_F(FormatTest, BreakBeforeInheritanceComma) {
1296   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1297   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1298
1299   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1300   verifyFormat("class MyClass\n"
1301                "    : public X\n"
1302                "    , public Y {};",
1303                StyleWithInheritanceBreak);
1304 }
1305
1306 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1307   verifyFormat("class A {\n} a, b;");
1308   verifyFormat("struct A {\n} a, b;");
1309   verifyFormat("union A {\n} a;");
1310 }
1311
1312 TEST_F(FormatTest, FormatsEnum) {
1313   verifyFormat("enum {\n"
1314                "  Zero,\n"
1315                "  One = 1,\n"
1316                "  Two = One + 1,\n"
1317                "  Three = (One + Two),\n"
1318                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1319                "  Five = (One, Two, Three, Four, 5)\n"
1320                "};");
1321   verifyGoogleFormat("enum {\n"
1322                      "  Zero,\n"
1323                      "  One = 1,\n"
1324                      "  Two = One + 1,\n"
1325                      "  Three = (One + Two),\n"
1326                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1327                      "  Five = (One, Two, Three, Four, 5)\n"
1328                      "};");
1329   verifyFormat("enum Enum {};");
1330   verifyFormat("enum {};");
1331   verifyFormat("enum X E {} d;");
1332   verifyFormat("enum __attribute__((...)) E {} d;");
1333   verifyFormat("enum __declspec__((...)) E {} d;");
1334   verifyFormat("enum {\n"
1335                "  Bar = Foo<int, int>::value\n"
1336                "};",
1337                getLLVMStyleWithColumns(30));
1338
1339   verifyFormat("enum ShortEnum { A, B, C };");
1340   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1341
1342   EXPECT_EQ("enum KeepEmptyLines {\n"
1343             "  ONE,\n"
1344             "\n"
1345             "  TWO,\n"
1346             "\n"
1347             "  THREE\n"
1348             "}",
1349             format("enum KeepEmptyLines {\n"
1350                    "  ONE,\n"
1351                    "\n"
1352                    "  TWO,\n"
1353                    "\n"
1354                    "\n"
1355                    "  THREE\n"
1356                    "}"));
1357   verifyFormat("enum E { // comment\n"
1358                "  ONE,\n"
1359                "  TWO\n"
1360                "};\n"
1361                "int i;");
1362   // Not enums.
1363   verifyFormat("enum X f() {\n"
1364                "  a();\n"
1365                "  return 42;\n"
1366                "}");
1367   verifyFormat("enum X Type::f() {\n"
1368                "  a();\n"
1369                "  return 42;\n"
1370                "}");
1371   verifyFormat("enum ::X f() {\n"
1372                "  a();\n"
1373                "  return 42;\n"
1374                "}");
1375   verifyFormat("enum ns::X f() {\n"
1376                "  a();\n"
1377                "  return 42;\n"
1378                "}");
1379 }
1380
1381 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1382   verifyFormat("enum Type {\n"
1383                "  One = 0; // These semicolons should be commas.\n"
1384                "  Two = 1;\n"
1385                "};");
1386   verifyFormat("namespace n {\n"
1387                "enum Type {\n"
1388                "  One,\n"
1389                "  Two, // missing };\n"
1390                "  int i;\n"
1391                "}\n"
1392                "void g() {}");
1393 }
1394
1395 TEST_F(FormatTest, FormatsEnumStruct) {
1396   verifyFormat("enum struct {\n"
1397                "  Zero,\n"
1398                "  One = 1,\n"
1399                "  Two = One + 1,\n"
1400                "  Three = (One + Two),\n"
1401                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1402                "  Five = (One, Two, Three, Four, 5)\n"
1403                "};");
1404   verifyFormat("enum struct Enum {};");
1405   verifyFormat("enum struct {};");
1406   verifyFormat("enum struct X E {} d;");
1407   verifyFormat("enum struct __attribute__((...)) E {} d;");
1408   verifyFormat("enum struct __declspec__((...)) E {} d;");
1409   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1410 }
1411
1412 TEST_F(FormatTest, FormatsEnumClass) {
1413   verifyFormat("enum class {\n"
1414                "  Zero,\n"
1415                "  One = 1,\n"
1416                "  Two = One + 1,\n"
1417                "  Three = (One + Two),\n"
1418                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1419                "  Five = (One, Two, Three, Four, 5)\n"
1420                "};");
1421   verifyFormat("enum class Enum {};");
1422   verifyFormat("enum class {};");
1423   verifyFormat("enum class X E {} d;");
1424   verifyFormat("enum class __attribute__((...)) E {} d;");
1425   verifyFormat("enum class __declspec__((...)) E {} d;");
1426   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1427 }
1428
1429 TEST_F(FormatTest, FormatsEnumTypes) {
1430   verifyFormat("enum X : int {\n"
1431                "  A, // Force multiple lines.\n"
1432                "  B\n"
1433                "};");
1434   verifyFormat("enum X : int { A, B };");
1435   verifyFormat("enum X : std::uint32_t { A, B };");
1436 }
1437
1438 TEST_F(FormatTest, FormatsTypedefEnum) {
1439   FormatStyle Style = getLLVMStyle();
1440   Style.ColumnLimit = 40;
1441   verifyFormat("typedef enum {} EmptyEnum;");
1442   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1443   verifyFormat("typedef enum {\n"
1444                "  ZERO = 0,\n"
1445                "  ONE = 1,\n"
1446                "  TWO = 2,\n"
1447                "  THREE = 3\n"
1448                "} LongEnum;",
1449                Style);
1450   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1451   Style.BraceWrapping.AfterEnum = true;
1452   verifyFormat("typedef enum {} EmptyEnum;");
1453   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1454   verifyFormat("typedef enum\n"
1455                "{\n"
1456                "  ZERO = 0,\n"
1457                "  ONE = 1,\n"
1458                "  TWO = 2,\n"
1459                "  THREE = 3\n"
1460                "} LongEnum;",
1461                Style);
1462 }
1463
1464 TEST_F(FormatTest, FormatsNSEnums) {
1465   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1466   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1467                      "  // Information about someDecentlyLongValue.\n"
1468                      "  someDecentlyLongValue,\n"
1469                      "  // Information about anotherDecentlyLongValue.\n"
1470                      "  anotherDecentlyLongValue,\n"
1471                      "  // Information about aThirdDecentlyLongValue.\n"
1472                      "  aThirdDecentlyLongValue\n"
1473                      "};");
1474   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1475                      "  a = 1,\n"
1476                      "  b = 2,\n"
1477                      "  c = 3,\n"
1478                      "};");
1479   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1480                      "  a = 1,\n"
1481                      "  b = 2,\n"
1482                      "  c = 3,\n"
1483                      "};");
1484   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1485                      "  a = 1,\n"
1486                      "  b = 2,\n"
1487                      "  c = 3,\n"
1488                      "};");
1489 }
1490
1491 TEST_F(FormatTest, FormatsBitfields) {
1492   verifyFormat("struct Bitfields {\n"
1493                "  unsigned sClass : 8;\n"
1494                "  unsigned ValueKind : 2;\n"
1495                "};");
1496   verifyFormat("struct A {\n"
1497                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1498                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1499                "};");
1500   verifyFormat("struct MyStruct {\n"
1501                "  uchar data;\n"
1502                "  uchar : 8;\n"
1503                "  uchar : 8;\n"
1504                "  uchar other;\n"
1505                "};");
1506 }
1507
1508 TEST_F(FormatTest, FormatsNamespaces) {
1509   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1510   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1511
1512   verifyFormat("namespace some_namespace {\n"
1513                "class A {};\n"
1514                "void f() { f(); }\n"
1515                "}",
1516                LLVMWithNoNamespaceFix);
1517   verifyFormat("namespace {\n"
1518                "class A {};\n"
1519                "void f() { f(); }\n"
1520                "}",
1521                LLVMWithNoNamespaceFix);
1522   verifyFormat("inline namespace X {\n"
1523                "class A {};\n"
1524                "void f() { f(); }\n"
1525                "}",
1526                LLVMWithNoNamespaceFix);
1527   verifyFormat("using namespace some_namespace;\n"
1528                "class A {};\n"
1529                "void f() { f(); }",
1530                LLVMWithNoNamespaceFix);
1531
1532   // This code is more common than we thought; if we
1533   // layout this correctly the semicolon will go into
1534   // its own line, which is undesirable.
1535   verifyFormat("namespace {};",
1536                LLVMWithNoNamespaceFix);
1537   verifyFormat("namespace {\n"
1538                "class A {};\n"
1539                "};",
1540                LLVMWithNoNamespaceFix);
1541
1542   verifyFormat("namespace {\n"
1543                "int SomeVariable = 0; // comment\n"
1544                "} // namespace",
1545                LLVMWithNoNamespaceFix);
1546   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1547             "#define HEADER_GUARD\n"
1548             "namespace my_namespace {\n"
1549             "int i;\n"
1550             "} // my_namespace\n"
1551             "#endif // HEADER_GUARD",
1552             format("#ifndef HEADER_GUARD\n"
1553                    " #define HEADER_GUARD\n"
1554                    "   namespace my_namespace {\n"
1555                    "int i;\n"
1556                    "}    // my_namespace\n"
1557                    "#endif    // HEADER_GUARD",
1558                    LLVMWithNoNamespaceFix));
1559
1560   EXPECT_EQ("namespace A::B {\n"
1561             "class C {};\n"
1562             "}",
1563             format("namespace A::B {\n"
1564                    "class C {};\n"
1565                    "}",
1566                    LLVMWithNoNamespaceFix));
1567
1568   FormatStyle Style = getLLVMStyle();
1569   Style.NamespaceIndentation = FormatStyle::NI_All;
1570   EXPECT_EQ("namespace out {\n"
1571             "  int i;\n"
1572             "  namespace in {\n"
1573             "    int i;\n"
1574             "  } // namespace in\n"
1575             "} // namespace out",
1576             format("namespace out {\n"
1577                    "int i;\n"
1578                    "namespace in {\n"
1579                    "int i;\n"
1580                    "} // namespace in\n"
1581                    "} // namespace out",
1582                    Style));
1583
1584   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1585   EXPECT_EQ("namespace out {\n"
1586             "int i;\n"
1587             "namespace in {\n"
1588             "  int i;\n"
1589             "} // namespace in\n"
1590             "} // namespace out",
1591             format("namespace out {\n"
1592                    "int i;\n"
1593                    "namespace in {\n"
1594                    "int i;\n"
1595                    "} // namespace in\n"
1596                    "} // namespace out",
1597                    Style));
1598 }
1599
1600 TEST_F(FormatTest, FormatsCompactNamespaces) {
1601   FormatStyle Style = getLLVMStyle();
1602   Style.CompactNamespaces = true;
1603
1604   verifyFormat("namespace A { namespace B {\n"
1605                            "}} // namespace A::B",
1606                            Style);
1607
1608   EXPECT_EQ("namespace out { namespace in {\n"
1609             "}} // namespace out::in",
1610             format("namespace out {\n"
1611                    "namespace in {\n"
1612                    "} // namespace in\n"
1613                    "} // namespace out",
1614                    Style));
1615
1616   // Only namespaces which have both consecutive opening and end get compacted
1617   EXPECT_EQ("namespace out {\n"
1618             "namespace in1 {\n"
1619             "} // namespace in1\n"
1620             "namespace in2 {\n"
1621             "} // namespace in2\n"
1622             "} // namespace out",
1623             format("namespace out {\n"
1624                    "namespace in1 {\n"
1625                    "} // namespace in1\n"
1626                    "namespace in2 {\n"
1627                    "} // namespace in2\n"
1628                    "} // namespace out",
1629                    Style));
1630
1631   EXPECT_EQ("namespace out {\n"
1632             "int i;\n"
1633             "namespace in {\n"
1634             "int j;\n"
1635             "} // namespace in\n"
1636             "int k;\n"
1637             "} // namespace out",
1638             format("namespace out { int i;\n"
1639                    "namespace in { int j; } // namespace in\n"
1640                    "int k; } // namespace out",
1641                    Style));
1642
1643   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1644             "}}} // namespace A::B::C\n",
1645             format("namespace A { namespace B {\n"
1646                    "namespace C {\n"
1647                    "}} // namespace B::C\n"
1648                    "} // namespace A\n",
1649                    Style));
1650
1651   Style.ColumnLimit = 40;
1652   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1653             "namespace bbbbbbbbbb {\n"
1654             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1655             format("namespace aaaaaaaaaa {\n"
1656                    "namespace bbbbbbbbbb {\n"
1657                    "} // namespace bbbbbbbbbb\n"
1658                    "} // namespace aaaaaaaaaa",
1659                    Style));
1660
1661   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1662             "namespace cccccc {\n"
1663             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1664             format("namespace aaaaaa {\n"
1665                    "namespace bbbbbb {\n"
1666                    "namespace cccccc {\n"
1667                    "} // namespace cccccc\n"
1668                    "} // namespace bbbbbb\n"
1669                    "} // namespace aaaaaa",
1670                    Style));
1671   Style.ColumnLimit = 80;
1672
1673   // Extra semicolon after 'inner' closing brace prevents merging
1674   EXPECT_EQ("namespace out { namespace in {\n"
1675             "}; } // namespace out::in",
1676             format("namespace out {\n"
1677                    "namespace in {\n"
1678                    "}; // namespace in\n"
1679                    "} // namespace out",
1680                    Style));
1681
1682   // Extra semicolon after 'outer' closing brace is conserved
1683   EXPECT_EQ("namespace out { namespace in {\n"
1684             "}}; // namespace out::in",
1685             format("namespace out {\n"
1686                    "namespace in {\n"
1687                    "} // namespace in\n"
1688                    "}; // namespace out",
1689                    Style));
1690
1691   Style.NamespaceIndentation = FormatStyle::NI_All;
1692   EXPECT_EQ("namespace out { namespace in {\n"
1693             "  int i;\n"
1694             "}} // namespace out::in",
1695             format("namespace out {\n"
1696                    "namespace in {\n"
1697                    "int i;\n"
1698                    "} // namespace in\n"
1699                    "} // namespace out",
1700                    Style));
1701   EXPECT_EQ("namespace out { namespace mid {\n"
1702             "  namespace in {\n"
1703             "    int j;\n"
1704             "  } // namespace in\n"
1705             "  int k;\n"
1706             "}} // namespace out::mid",
1707             format("namespace out { namespace mid {\n"
1708                    "namespace in { int j; } // namespace in\n"
1709                    "int k; }} // namespace out::mid",
1710                    Style));
1711
1712   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1713   EXPECT_EQ("namespace out { namespace in {\n"
1714             "  int i;\n"
1715             "}} // namespace out::in",
1716             format("namespace out {\n"
1717                    "namespace in {\n"
1718                    "int i;\n"
1719                    "} // namespace in\n"
1720                    "} // namespace out",
1721                    Style));
1722   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1723             "  int i;\n"
1724             "}}} // namespace out::mid::in",
1725             format("namespace out {\n"
1726                    "namespace mid {\n"
1727                    "namespace in {\n"
1728                    "int i;\n"
1729                    "} // namespace in\n"
1730                    "} // namespace mid\n"
1731                    "} // namespace out",
1732                    Style));
1733 }
1734
1735 TEST_F(FormatTest, FormatsExternC) {
1736   verifyFormat("extern \"C\" {\nint a;");
1737   verifyFormat("extern \"C\" {}");
1738   verifyFormat("extern \"C\" {\n"
1739                "int foo();\n"
1740                "}");
1741   verifyFormat("extern \"C\" int foo() {}");
1742   verifyFormat("extern \"C\" int foo();");
1743   verifyFormat("extern \"C\" int foo() {\n"
1744                "  int i = 42;\n"
1745                "  return i;\n"
1746                "}");
1747
1748   FormatStyle Style = getLLVMStyle();
1749   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1750   Style.BraceWrapping.AfterFunction = true;
1751   verifyFormat("extern \"C\" int foo() {}", Style);
1752   verifyFormat("extern \"C\" int foo();", Style);
1753   verifyFormat("extern \"C\" int foo()\n"
1754                "{\n"
1755                "  int i = 42;\n"
1756                "  return i;\n"
1757                "}",
1758                Style);
1759
1760   Style.BraceWrapping.AfterExternBlock = true;
1761   Style.BraceWrapping.SplitEmptyRecord = false;
1762   verifyFormat("extern \"C\"\n"
1763                "{}",
1764                Style);
1765   verifyFormat("extern \"C\"\n"
1766                "{\n"
1767                "  int foo();\n"
1768                "}",
1769                Style);
1770 }
1771
1772 TEST_F(FormatTest, FormatsInlineASM) {
1773   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1774   verifyFormat("asm(\"nop\" ::: \"memory\");");
1775   verifyFormat(
1776       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1777       "    \"cpuid\\n\\t\"\n"
1778       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1779       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1780       "    : \"a\"(value));");
1781   EXPECT_EQ(
1782       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1783       "  __asm {\n"
1784       "        mov     edx,[that] // vtable in edx\n"
1785       "        mov     eax,methodIndex\n"
1786       "        call    [edx][eax*4] // stdcall\n"
1787       "  }\n"
1788       "}",
1789       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1790              "    __asm {\n"
1791              "        mov     edx,[that] // vtable in edx\n"
1792              "        mov     eax,methodIndex\n"
1793              "        call    [edx][eax*4] // stdcall\n"
1794              "    }\n"
1795              "}"));
1796   EXPECT_EQ("_asm {\n"
1797             "  xor eax, eax;\n"
1798             "  cpuid;\n"
1799             "}",
1800             format("_asm {\n"
1801                    "  xor eax, eax;\n"
1802                    "  cpuid;\n"
1803                    "}"));
1804   verifyFormat("void function() {\n"
1805                "  // comment\n"
1806                "  asm(\"\");\n"
1807                "}");
1808   EXPECT_EQ("__asm {\n"
1809             "}\n"
1810             "int i;",
1811             format("__asm   {\n"
1812                    "}\n"
1813                    "int   i;"));
1814 }
1815
1816 TEST_F(FormatTest, FormatTryCatch) {
1817   verifyFormat("try {\n"
1818                "  throw a * b;\n"
1819                "} catch (int a) {\n"
1820                "  // Do nothing.\n"
1821                "} catch (...) {\n"
1822                "  exit(42);\n"
1823                "}");
1824
1825   // Function-level try statements.
1826   verifyFormat("int f() try { return 4; } catch (...) {\n"
1827                "  return 5;\n"
1828                "}");
1829   verifyFormat("class A {\n"
1830                "  int a;\n"
1831                "  A() try : a(0) {\n"
1832                "  } catch (...) {\n"
1833                "    throw;\n"
1834                "  }\n"
1835                "};\n");
1836
1837   // Incomplete try-catch blocks.
1838   verifyIncompleteFormat("try {} catch (");
1839 }
1840
1841 TEST_F(FormatTest, FormatSEHTryCatch) {
1842   verifyFormat("__try {\n"
1843                "  int a = b * c;\n"
1844                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1845                "  // Do nothing.\n"
1846                "}");
1847
1848   verifyFormat("__try {\n"
1849                "  int a = b * c;\n"
1850                "} __finally {\n"
1851                "  // Do nothing.\n"
1852                "}");
1853
1854   verifyFormat("DEBUG({\n"
1855                "  __try {\n"
1856                "  } __finally {\n"
1857                "  }\n"
1858                "});\n");
1859 }
1860
1861 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1862   verifyFormat("try {\n"
1863                "  f();\n"
1864                "} catch {\n"
1865                "  g();\n"
1866                "}");
1867   verifyFormat("try {\n"
1868                "  f();\n"
1869                "} catch (A a) MACRO(x) {\n"
1870                "  g();\n"
1871                "} catch (B b) MACRO(x) {\n"
1872                "  g();\n"
1873                "}");
1874 }
1875
1876 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1877   FormatStyle Style = getLLVMStyle();
1878   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1879                           FormatStyle::BS_WebKit}) {
1880     Style.BreakBeforeBraces = BraceStyle;
1881     verifyFormat("try {\n"
1882                  "  // something\n"
1883                  "} catch (...) {\n"
1884                  "  // something\n"
1885                  "}",
1886                  Style);
1887   }
1888   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1889   verifyFormat("try {\n"
1890                "  // something\n"
1891                "}\n"
1892                "catch (...) {\n"
1893                "  // something\n"
1894                "}",
1895                Style);
1896   verifyFormat("__try {\n"
1897                "  // something\n"
1898                "}\n"
1899                "__finally {\n"
1900                "  // something\n"
1901                "}",
1902                Style);
1903   verifyFormat("@try {\n"
1904                "  // something\n"
1905                "}\n"
1906                "@finally {\n"
1907                "  // something\n"
1908                "}",
1909                Style);
1910   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1911   verifyFormat("try\n"
1912                "{\n"
1913                "  // something\n"
1914                "}\n"
1915                "catch (...)\n"
1916                "{\n"
1917                "  // something\n"
1918                "}",
1919                Style);
1920   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1921   verifyFormat("try\n"
1922                "  {\n"
1923                "    // something\n"
1924                "  }\n"
1925                "catch (...)\n"
1926                "  {\n"
1927                "    // something\n"
1928                "  }",
1929                Style);
1930   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1931   Style.BraceWrapping.BeforeCatch = true;
1932   verifyFormat("try {\n"
1933                "  // something\n"
1934                "}\n"
1935                "catch (...) {\n"
1936                "  // something\n"
1937                "}",
1938                Style);
1939 }
1940
1941 TEST_F(FormatTest, StaticInitializers) {
1942   verifyFormat("static SomeClass SC = {1, 'a'};");
1943
1944   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1945                "    100000000, "
1946                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1947
1948   // Here, everything other than the "}" would fit on a line.
1949   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1950                "    10000000000000000000000000};");
1951   EXPECT_EQ("S s = {a,\n"
1952             "\n"
1953             "       b};",
1954             format("S s = {\n"
1955                    "  a,\n"
1956                    "\n"
1957                    "  b\n"
1958                    "};"));
1959
1960   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1961   // line. However, the formatting looks a bit off and this probably doesn't
1962   // happen often in practice.
1963   verifyFormat("static int Variable[1] = {\n"
1964                "    {1000000000000000000000000000000000000}};",
1965                getLLVMStyleWithColumns(40));
1966 }
1967
1968 TEST_F(FormatTest, DesignatedInitializers) {
1969   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1970   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1971                "                    .bbbbbbbbbb = 2,\n"
1972                "                    .cccccccccc = 3,\n"
1973                "                    .dddddddddd = 4,\n"
1974                "                    .eeeeeeeeee = 5};");
1975   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1976                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1977                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1978                "    .ccccccccccccccccccccccccccc = 3,\n"
1979                "    .ddddddddddddddddddddddddddd = 4,\n"
1980                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1981
1982   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1983
1984   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
1985   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
1986                "                    [2] = bbbbbbbbbb,\n"
1987                "                    [3] = cccccccccc,\n"
1988                "                    [4] = dddddddddd,\n"
1989                "                    [5] = eeeeeeeeee};");
1990   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1991                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1992                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1993                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
1994                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
1995                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
1996 }
1997
1998 TEST_F(FormatTest, NestedStaticInitializers) {
1999   verifyFormat("static A x = {{{}}};\n");
2000   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2001                "               {init1, init2, init3, init4}}};",
2002                getLLVMStyleWithColumns(50));
2003
2004   verifyFormat("somes Status::global_reps[3] = {\n"
2005                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2006                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2007                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2008                getLLVMStyleWithColumns(60));
2009   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2010                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2011                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2012                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2013   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2014                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2015                "rect.fTop}};");
2016
2017   verifyFormat(
2018       "SomeArrayOfSomeType a = {\n"
2019       "    {{1, 2, 3},\n"
2020       "     {1, 2, 3},\n"
2021       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2022       "      333333333333333333333333333333},\n"
2023       "     {1, 2, 3},\n"
2024       "     {1, 2, 3}}};");
2025   verifyFormat(
2026       "SomeArrayOfSomeType a = {\n"
2027       "    {{1, 2, 3}},\n"
2028       "    {{1, 2, 3}},\n"
2029       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2030       "      333333333333333333333333333333}},\n"
2031       "    {{1, 2, 3}},\n"
2032       "    {{1, 2, 3}}};");
2033
2034   verifyFormat("struct {\n"
2035                "  unsigned bit;\n"
2036                "  const char *const name;\n"
2037                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2038                "                 {kOsWin, \"Windows\"},\n"
2039                "                 {kOsLinux, \"Linux\"},\n"
2040                "                 {kOsCrOS, \"Chrome OS\"}};");
2041   verifyFormat("struct {\n"
2042                "  unsigned bit;\n"
2043                "  const char *const name;\n"
2044                "} kBitsToOs[] = {\n"
2045                "    {kOsMac, \"Mac\"},\n"
2046                "    {kOsWin, \"Windows\"},\n"
2047                "    {kOsLinux, \"Linux\"},\n"
2048                "    {kOsCrOS, \"Chrome OS\"},\n"
2049                "};");
2050 }
2051
2052 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2053   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2054                "                      \\\n"
2055                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2056 }
2057
2058 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2059   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2060                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2061
2062   // Do break defaulted and deleted functions.
2063   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2064                "    default;",
2065                getLLVMStyleWithColumns(40));
2066   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2067                "    delete;",
2068                getLLVMStyleWithColumns(40));
2069 }
2070
2071 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2072   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2073                getLLVMStyleWithColumns(40));
2074   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2075                getLLVMStyleWithColumns(40));
2076   EXPECT_EQ("#define Q                              \\\n"
2077             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2078             "  \"aaaaaaaa.cpp\"",
2079             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2080                    getLLVMStyleWithColumns(40)));
2081 }
2082
2083 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2084   EXPECT_EQ("# 123 \"A string literal\"",
2085             format("   #     123    \"A string literal\""));
2086 }
2087
2088 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2089   EXPECT_EQ("#;", format("#;"));
2090   verifyFormat("#\n;\n;\n;");
2091 }
2092
2093 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2094   EXPECT_EQ("#line 42 \"test\"\n",
2095             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2096   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2097                                     getLLVMStyleWithColumns(12)));
2098 }
2099
2100 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2101   EXPECT_EQ("#line 42 \"test\"",
2102             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2103   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2104 }
2105
2106 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2107   verifyFormat("#define A \\x20");
2108   verifyFormat("#define A \\ x20");
2109   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2110   verifyFormat("#define A ''");
2111   verifyFormat("#define A ''qqq");
2112   verifyFormat("#define A `qqq");
2113   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2114   EXPECT_EQ("const char *c = STRINGIFY(\n"
2115             "\\na : b);",
2116             format("const char * c = STRINGIFY(\n"
2117                    "\\na : b);"));
2118
2119   verifyFormat("a\r\\");
2120   verifyFormat("a\v\\");
2121   verifyFormat("a\f\\");
2122 }
2123
2124 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2125   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2126   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2127   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2128   // FIXME: We never break before the macro name.
2129   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2130
2131   verifyFormat("#define A A\n#define A A");
2132   verifyFormat("#define A(X) A\n#define A A");
2133
2134   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2135   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2136 }
2137
2138 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2139   EXPECT_EQ("// somecomment\n"
2140             "#include \"a.h\"\n"
2141             "#define A(  \\\n"
2142             "    A, B)\n"
2143             "#include \"b.h\"\n"
2144             "// somecomment\n",
2145             format("  // somecomment\n"
2146                    "  #include \"a.h\"\n"
2147                    "#define A(A,\\\n"
2148                    "    B)\n"
2149                    "    #include \"b.h\"\n"
2150                    " // somecomment\n",
2151                    getLLVMStyleWithColumns(13)));
2152 }
2153
2154 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2155
2156 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2157   EXPECT_EQ("#define A    \\\n"
2158             "  c;         \\\n"
2159             "  e;\n"
2160             "f;",
2161             format("#define A c; e;\n"
2162                    "f;",
2163                    getLLVMStyleWithColumns(14)));
2164 }
2165
2166 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2167
2168 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2169   EXPECT_EQ("int x,\n"
2170             "#define A\n"
2171             "    y;",
2172             format("int x,\n#define A\ny;"));
2173 }
2174
2175 TEST_F(FormatTest, HashInMacroDefinition) {
2176   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2177   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2178   verifyFormat("#define A  \\\n"
2179                "  {        \\\n"
2180                "    f(#c); \\\n"
2181                "  }",
2182                getLLVMStyleWithColumns(11));
2183
2184   verifyFormat("#define A(X)         \\\n"
2185                "  void function##X()",
2186                getLLVMStyleWithColumns(22));
2187
2188   verifyFormat("#define A(a, b, c)   \\\n"
2189                "  void a##b##c()",
2190                getLLVMStyleWithColumns(22));
2191
2192   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2193 }
2194
2195 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2196   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2197   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2198 }
2199
2200 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2201   EXPECT_EQ("#define A b;", format("#define A \\\n"
2202                                    "          \\\n"
2203                                    "  b;",
2204                                    getLLVMStyleWithColumns(25)));
2205   EXPECT_EQ("#define A \\\n"
2206             "          \\\n"
2207             "  a;      \\\n"
2208             "  b;",
2209             format("#define A \\\n"
2210                    "          \\\n"
2211                    "  a;      \\\n"
2212                    "  b;",
2213                    getLLVMStyleWithColumns(11)));
2214   EXPECT_EQ("#define A \\\n"
2215             "  a;      \\\n"
2216             "          \\\n"
2217             "  b;",
2218             format("#define A \\\n"
2219                    "  a;      \\\n"
2220                    "          \\\n"
2221                    "  b;",
2222                    getLLVMStyleWithColumns(11)));
2223 }
2224
2225 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2226   verifyIncompleteFormat("#define A :");
2227   verifyFormat("#define SOMECASES  \\\n"
2228                "  case 1:          \\\n"
2229                "  case 2\n",
2230                getLLVMStyleWithColumns(20));
2231   verifyFormat("#define MACRO(a) \\\n"
2232                "  if (a)         \\\n"
2233                "    f();         \\\n"
2234                "  else           \\\n"
2235                "    g()",
2236                getLLVMStyleWithColumns(18));
2237   verifyFormat("#define A template <typename T>");
2238   verifyIncompleteFormat("#define STR(x) #x\n"
2239                          "f(STR(this_is_a_string_literal{));");
2240   verifyFormat("#pragma omp threadprivate( \\\n"
2241                "    y)), // expected-warning",
2242                getLLVMStyleWithColumns(28));
2243   verifyFormat("#d, = };");
2244   verifyFormat("#if \"a");
2245   verifyIncompleteFormat("({\n"
2246                          "#define b     \\\n"
2247                          "  }           \\\n"
2248                          "  a\n"
2249                          "a",
2250                          getLLVMStyleWithColumns(15));
2251   verifyFormat("#define A     \\\n"
2252                "  {           \\\n"
2253                "    {\n"
2254                "#define B     \\\n"
2255                "  }           \\\n"
2256                "  }",
2257                getLLVMStyleWithColumns(15));
2258   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2259   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2260   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2261   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2262 }
2263
2264 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2265   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2266   EXPECT_EQ("class A : public QObject {\n"
2267             "  Q_OBJECT\n"
2268             "\n"
2269             "  A() {}\n"
2270             "};",
2271             format("class A  :  public QObject {\n"
2272                    "     Q_OBJECT\n"
2273                    "\n"
2274                    "  A() {\n}\n"
2275                    "}  ;"));
2276   EXPECT_EQ("MACRO\n"
2277             "/*static*/ int i;",
2278             format("MACRO\n"
2279                    " /*static*/ int   i;"));
2280   EXPECT_EQ("SOME_MACRO\n"
2281             "namespace {\n"
2282             "void f();\n"
2283             "} // namespace",
2284             format("SOME_MACRO\n"
2285                    "  namespace    {\n"
2286                    "void   f(  );\n"
2287                    "} // namespace"));
2288   // Only if the identifier contains at least 5 characters.
2289   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2290   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2291   // Only if everything is upper case.
2292   EXPECT_EQ("class A : public QObject {\n"
2293             "  Q_Object A() {}\n"
2294             "};",
2295             format("class A  :  public QObject {\n"
2296                    "     Q_Object\n"
2297                    "  A() {\n}\n"
2298                    "}  ;"));
2299
2300   // Only if the next line can actually start an unwrapped line.
2301   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2302             format("SOME_WEIRD_LOG_MACRO\n"
2303                    "<< SomeThing;"));
2304
2305   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2306                "(n, buffers))\n",
2307                getChromiumStyle(FormatStyle::LK_Cpp));
2308 }
2309
2310 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2311   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2312             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2313             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2314             "class X {};\n"
2315             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2316             "int *createScopDetectionPass() { return 0; }",
2317             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2318                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2319                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2320                    "  class X {};\n"
2321                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2322                    "  int *createScopDetectionPass() { return 0; }"));
2323   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2324   // braces, so that inner block is indented one level more.
2325   EXPECT_EQ("int q() {\n"
2326             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2327             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2328             "  IPC_END_MESSAGE_MAP()\n"
2329             "}",
2330             format("int q() {\n"
2331                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2332                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2333                    "  IPC_END_MESSAGE_MAP()\n"
2334                    "}"));
2335
2336   // Same inside macros.
2337   EXPECT_EQ("#define LIST(L) \\\n"
2338             "  L(A)          \\\n"
2339             "  L(B)          \\\n"
2340             "  L(C)",
2341             format("#define LIST(L) \\\n"
2342                    "  L(A) \\\n"
2343                    "  L(B) \\\n"
2344                    "  L(C)",
2345                    getGoogleStyle()));
2346
2347   // These must not be recognized as macros.
2348   EXPECT_EQ("int q() {\n"
2349             "  f(x);\n"
2350             "  f(x) {}\n"
2351             "  f(x)->g();\n"
2352             "  f(x)->*g();\n"
2353             "  f(x).g();\n"
2354             "  f(x) = x;\n"
2355             "  f(x) += x;\n"
2356             "  f(x) -= x;\n"
2357             "  f(x) *= x;\n"
2358             "  f(x) /= x;\n"
2359             "  f(x) %= x;\n"
2360             "  f(x) &= x;\n"
2361             "  f(x) |= x;\n"
2362             "  f(x) ^= x;\n"
2363             "  f(x) >>= x;\n"
2364             "  f(x) <<= x;\n"
2365             "  f(x)[y].z();\n"
2366             "  LOG(INFO) << x;\n"
2367             "  ifstream(x) >> x;\n"
2368             "}\n",
2369             format("int q() {\n"
2370                    "  f(x)\n;\n"
2371                    "  f(x)\n {}\n"
2372                    "  f(x)\n->g();\n"
2373                    "  f(x)\n->*g();\n"
2374                    "  f(x)\n.g();\n"
2375                    "  f(x)\n = x;\n"
2376                    "  f(x)\n += x;\n"
2377                    "  f(x)\n -= x;\n"
2378                    "  f(x)\n *= x;\n"
2379                    "  f(x)\n /= x;\n"
2380                    "  f(x)\n %= x;\n"
2381                    "  f(x)\n &= x;\n"
2382                    "  f(x)\n |= x;\n"
2383                    "  f(x)\n ^= x;\n"
2384                    "  f(x)\n >>= x;\n"
2385                    "  f(x)\n <<= x;\n"
2386                    "  f(x)\n[y].z();\n"
2387                    "  LOG(INFO)\n << x;\n"
2388                    "  ifstream(x)\n >> x;\n"
2389                    "}\n"));
2390   EXPECT_EQ("int q() {\n"
2391             "  F(x)\n"
2392             "  if (1) {\n"
2393             "  }\n"
2394             "  F(x)\n"
2395             "  while (1) {\n"
2396             "  }\n"
2397             "  F(x)\n"
2398             "  G(x);\n"
2399             "  F(x)\n"
2400             "  try {\n"
2401             "    Q();\n"
2402             "  } catch (...) {\n"
2403             "  }\n"
2404             "}\n",
2405             format("int q() {\n"
2406                    "F(x)\n"
2407                    "if (1) {}\n"
2408                    "F(x)\n"
2409                    "while (1) {}\n"
2410                    "F(x)\n"
2411                    "G(x);\n"
2412                    "F(x)\n"
2413                    "try { Q(); } catch (...) {}\n"
2414                    "}\n"));
2415   EXPECT_EQ("class A {\n"
2416             "  A() : t(0) {}\n"
2417             "  A(int i) noexcept() : {}\n"
2418             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2419             "  try : t(0) {\n"
2420             "  } catch (...) {\n"
2421             "  }\n"
2422             "};",
2423             format("class A {\n"
2424                    "  A()\n : t(0) {}\n"
2425                    "  A(int i)\n noexcept() : {}\n"
2426                    "  A(X x)\n"
2427                    "  try : t(0) {} catch (...) {}\n"
2428                    "};"));
2429   EXPECT_EQ("class SomeClass {\n"
2430             "public:\n"
2431             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2432             "};",
2433             format("class SomeClass {\n"
2434                    "public:\n"
2435                    "  SomeClass()\n"
2436                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2437                    "};"));
2438   EXPECT_EQ("class SomeClass {\n"
2439             "public:\n"
2440             "  SomeClass()\n"
2441             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2442             "};",
2443             format("class SomeClass {\n"
2444                    "public:\n"
2445                    "  SomeClass()\n"
2446                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2447                    "};",
2448                    getLLVMStyleWithColumns(40)));
2449
2450   verifyFormat("MACRO(>)");
2451 }
2452
2453 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2454   verifyFormat("#define A \\\n"
2455                "  f({     \\\n"
2456                "    g();  \\\n"
2457                "  });",
2458                getLLVMStyleWithColumns(11));
2459 }
2460
2461 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2462   FormatStyle Style = getLLVMStyle();
2463   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2464   Style.ColumnLimit = 40;
2465   verifyFormat("#ifdef _WIN32\n"
2466                "#define A 0\n"
2467                "#ifdef VAR2\n"
2468                "#define B 1\n"
2469                "#include <someheader.h>\n"
2470                "#define MACRO                          \\\n"
2471                "  some_very_long_func_aaaaaaaaaa();\n"
2472                "#endif\n"
2473                "#else\n"
2474                "#define A 1\n"
2475                "#endif",
2476                Style);
2477   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2478   verifyFormat("#ifdef _WIN32\n"
2479                "#  define A 0\n"
2480                "#  ifdef VAR2\n"
2481                "#    define B 1\n"
2482                "#    include <someheader.h>\n"
2483                "#    define MACRO                      \\\n"
2484                "      some_very_long_func_aaaaaaaaaa();\n"
2485                "#  endif\n"
2486                "#else\n"
2487                "#  define A 1\n"
2488                "#endif",
2489                Style);
2490   verifyFormat("#if A\n"
2491                "#  define MACRO                        \\\n"
2492                "    void a(int x) {                    \\\n"
2493                "      b();                             \\\n"
2494                "      c();                             \\\n"
2495                "      d();                             \\\n"
2496                "      e();                             \\\n"
2497                "      f();                             \\\n"
2498                "    }\n"
2499                "#endif",
2500                Style);
2501   // Comments before include guard.
2502   verifyFormat("// file comment\n"
2503                "// file comment\n"
2504                "#ifndef HEADER_H\n"
2505                "#define HEADER_H\n"
2506                "code();\n"
2507                "#endif",
2508                Style);
2509   // Test with include guards.
2510   verifyFormat("#ifndef HEADER_H\n"
2511                "#define HEADER_H\n"
2512                "code();\n"
2513                "#endif",
2514                Style);
2515   // Include guards must have a #define with the same variable immediately
2516   // after #ifndef.
2517   verifyFormat("#ifndef NOT_GUARD\n"
2518                "#  define FOO\n"
2519                "code();\n"
2520                "#endif",
2521                Style);
2522
2523   // Include guards must cover the entire file.
2524   verifyFormat("code();\n"
2525                "code();\n"
2526                "#ifndef NOT_GUARD\n"
2527                "#  define NOT_GUARD\n"
2528                "code();\n"
2529                "#endif",
2530                Style);
2531   verifyFormat("#ifndef NOT_GUARD\n"
2532                "#  define NOT_GUARD\n"
2533                "code();\n"
2534                "#endif\n"
2535                "code();",
2536                Style);
2537   // Test with trailing blank lines.
2538   verifyFormat("#ifndef HEADER_H\n"
2539                "#define HEADER_H\n"
2540                "code();\n"
2541                "#endif\n",
2542                Style);
2543   // Include guards don't have #else.
2544   verifyFormat("#ifndef NOT_GUARD\n"
2545                "#  define NOT_GUARD\n"
2546                "code();\n"
2547                "#else\n"
2548                "#endif",
2549                Style);
2550   verifyFormat("#ifndef NOT_GUARD\n"
2551                "#  define NOT_GUARD\n"
2552                "code();\n"
2553                "#elif FOO\n"
2554                "#endif",
2555                Style);
2556   // Non-identifier #define after potential include guard.
2557   verifyFormat("#ifndef FOO\n"
2558                "#  define 1\n"
2559                "#endif\n",
2560                Style);
2561   // #if closes past last non-preprocessor line.
2562   verifyFormat("#ifndef FOO\n"
2563                "#define FOO\n"
2564                "#if 1\n"
2565                "int i;\n"
2566                "#  define A 0\n"
2567                "#endif\n"
2568                "#endif\n",
2569                Style);
2570   // FIXME: This doesn't handle the case where there's code between the
2571   // #ifndef and #define but all other conditions hold. This is because when
2572   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2573   // previous code line yet, so we can't detect it.
2574   EXPECT_EQ("#ifndef NOT_GUARD\n"
2575             "code();\n"
2576             "#define NOT_GUARD\n"
2577             "code();\n"
2578             "#endif",
2579             format("#ifndef NOT_GUARD\n"
2580                    "code();\n"
2581                    "#  define NOT_GUARD\n"
2582                    "code();\n"
2583                    "#endif",
2584                    Style));
2585   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2586   // be outside an include guard. Examples are #pragma once and
2587   // #pragma GCC diagnostic, or anything else that does not change the meaning
2588   // of the file if it's included multiple times.
2589   EXPECT_EQ("#ifdef WIN32\n"
2590             "#  pragma once\n"
2591             "#endif\n"
2592             "#ifndef HEADER_H\n"
2593             "#  define HEADER_H\n"
2594             "code();\n"
2595             "#endif",
2596             format("#ifdef WIN32\n"
2597                    "#  pragma once\n"
2598                    "#endif\n"
2599                    "#ifndef HEADER_H\n"
2600                    "#define HEADER_H\n"
2601                    "code();\n"
2602                    "#endif",
2603                    Style));
2604   // FIXME: This does not detect when there is a single non-preprocessor line
2605   // in front of an include-guard-like structure where other conditions hold
2606   // because ScopedLineState hides the line.
2607   EXPECT_EQ("code();\n"
2608             "#ifndef HEADER_H\n"
2609             "#define HEADER_H\n"
2610             "code();\n"
2611             "#endif",
2612             format("code();\n"
2613                    "#ifndef HEADER_H\n"
2614                    "#  define HEADER_H\n"
2615                    "code();\n"
2616                    "#endif",
2617                    Style));
2618   // Keep comments aligned with #, otherwise indent comments normally. These
2619   // tests cannot use verifyFormat because messUp manipulates leading
2620   // whitespace.
2621   {
2622     const char *Expected = ""
2623                            "void f() {\n"
2624                            "#if 1\n"
2625                            "// Preprocessor aligned.\n"
2626                            "#  define A 0\n"
2627                            "  // Code. Separated by blank line.\n"
2628                            "\n"
2629                            "#  define B 0\n"
2630                            "  // Code. Not aligned with #\n"
2631                            "#  define C 0\n"
2632                            "#endif";
2633     const char *ToFormat = ""
2634                            "void f() {\n"
2635                            "#if 1\n"
2636                            "// Preprocessor aligned.\n"
2637                            "#  define A 0\n"
2638                            "// Code. Separated by blank line.\n"
2639                            "\n"
2640                            "#  define B 0\n"
2641                            "   // Code. Not aligned with #\n"
2642                            "#  define C 0\n"
2643                            "#endif";
2644     EXPECT_EQ(Expected, format(ToFormat, Style));
2645     EXPECT_EQ(Expected, format(Expected, Style));
2646   }
2647   // Keep block quotes aligned.
2648   {
2649     const char *Expected = ""
2650                            "void f() {\n"
2651                            "#if 1\n"
2652                            "/* Preprocessor aligned. */\n"
2653                            "#  define A 0\n"
2654                            "  /* Code. Separated by blank line. */\n"
2655                            "\n"
2656                            "#  define B 0\n"
2657                            "  /* Code. Not aligned with # */\n"
2658                            "#  define C 0\n"
2659                            "#endif";
2660     const char *ToFormat = ""
2661                            "void f() {\n"
2662                            "#if 1\n"
2663                            "/* Preprocessor aligned. */\n"
2664                            "#  define A 0\n"
2665                            "/* Code. Separated by blank line. */\n"
2666                            "\n"
2667                            "#  define B 0\n"
2668                            "   /* Code. Not aligned with # */\n"
2669                            "#  define C 0\n"
2670                            "#endif";
2671     EXPECT_EQ(Expected, format(ToFormat, Style));
2672     EXPECT_EQ(Expected, format(Expected, Style));
2673   }
2674   // Keep comments aligned with un-indented directives.
2675   {
2676     const char *Expected = ""
2677                            "void f() {\n"
2678                            "// Preprocessor aligned.\n"
2679                            "#define A 0\n"
2680                            "  // Code. Separated by blank line.\n"
2681                            "\n"
2682                            "#define B 0\n"
2683                            "  // Code. Not aligned with #\n"
2684                            "#define C 0\n";
2685     const char *ToFormat = ""
2686                            "void f() {\n"
2687                            "// Preprocessor aligned.\n"
2688                            "#define A 0\n"
2689                            "// Code. Separated by blank line.\n"
2690                            "\n"
2691                            "#define B 0\n"
2692                            "   // Code. Not aligned with #\n"
2693                            "#define C 0\n";
2694     EXPECT_EQ(Expected, format(ToFormat, Style));
2695     EXPECT_EQ(Expected, format(Expected, Style));
2696   }
2697   // Test with tabs.
2698   Style.UseTab = FormatStyle::UT_Always;
2699   Style.IndentWidth = 8;
2700   Style.TabWidth = 8;
2701   verifyFormat("#ifdef _WIN32\n"
2702                "#\tdefine A 0\n"
2703                "#\tifdef VAR2\n"
2704                "#\t\tdefine B 1\n"
2705                "#\t\tinclude <someheader.h>\n"
2706                "#\t\tdefine MACRO          \\\n"
2707                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2708                "#\tendif\n"
2709                "#else\n"
2710                "#\tdefine A 1\n"
2711                "#endif",
2712                Style);
2713
2714   // Regression test: Multiline-macro inside include guards.
2715   verifyFormat("#ifndef HEADER_H\n"
2716                "#define HEADER_H\n"
2717                "#define A()        \\\n"
2718                "  int i;           \\\n"
2719                "  int j;\n"
2720                "#endif // HEADER_H",
2721                getLLVMStyleWithColumns(20));
2722 }
2723
2724 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2725   verifyFormat("{\n  { a #c; }\n}");
2726 }
2727
2728 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2729   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2730             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2731   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2732             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2733 }
2734
2735 TEST_F(FormatTest, EscapedNewlines) {
2736   FormatStyle Narrow = getLLVMStyleWithColumns(11);
2737   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
2738             format("#define A \\\nint i;\\\n  int j;", Narrow));
2739   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2740   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2741   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2742   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2743
2744   FormatStyle AlignLeft = getLLVMStyle();
2745   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
2746   EXPECT_EQ("#define MACRO(x) \\\n"
2747             "private:         \\\n"
2748             "  int x(int a);\n",
2749             format("#define MACRO(x) \\\n"
2750                    "private:         \\\n"
2751                    "  int x(int a);\n",
2752                    AlignLeft));
2753
2754   // CRLF line endings
2755   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
2756             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
2757   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
2758   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2759   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
2760   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
2761   EXPECT_EQ("#define MACRO(x) \\\r\n"
2762             "private:         \\\r\n"
2763             "  int x(int a);\r\n",
2764             format("#define MACRO(x) \\\r\n"
2765                    "private:         \\\r\n"
2766                    "  int x(int a);\r\n",
2767                    AlignLeft));
2768
2769   FormatStyle DontAlign = getLLVMStyle();
2770   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2771   DontAlign.MaxEmptyLinesToKeep = 3;
2772   // FIXME: can't use verifyFormat here because the newline before
2773   // "public:" is not inserted the first time it's reformatted
2774   EXPECT_EQ("#define A \\\n"
2775             "  class Foo { \\\n"
2776             "    void bar(); \\\n"
2777             "\\\n"
2778             "\\\n"
2779             "\\\n"
2780             "  public: \\\n"
2781             "    void baz(); \\\n"
2782             "  };",
2783             format("#define A \\\n"
2784                    "  class Foo { \\\n"
2785                    "    void bar(); \\\n"
2786                    "\\\n"
2787                    "\\\n"
2788                    "\\\n"
2789                    "  public: \\\n"
2790                    "    void baz(); \\\n"
2791                    "  };",
2792                    DontAlign));
2793 }
2794
2795 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2796   verifyFormat("#define A \\\n"
2797                "  int v(  \\\n"
2798                "      a); \\\n"
2799                "  int i;",
2800                getLLVMStyleWithColumns(11));
2801 }
2802
2803 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2804   EXPECT_EQ(
2805       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2806       "                      \\\n"
2807       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2808       "\n"
2809       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2810       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2811       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2812              "\\\n"
2813              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2814              "  \n"
2815              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2816              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2817 }
2818
2819 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2820   EXPECT_EQ("int\n"
2821             "#define A\n"
2822             "    a;",
2823             format("int\n#define A\na;"));
2824   verifyFormat("functionCallTo(\n"
2825                "    someOtherFunction(\n"
2826                "        withSomeParameters, whichInSequence,\n"
2827                "        areLongerThanALine(andAnotherCall,\n"
2828                "#define A B\n"
2829                "                           withMoreParamters,\n"
2830                "                           whichStronglyInfluenceTheLayout),\n"
2831                "        andMoreParameters),\n"
2832                "    trailing);",
2833                getLLVMStyleWithColumns(69));
2834   verifyFormat("Foo::Foo()\n"
2835                "#ifdef BAR\n"
2836                "    : baz(0)\n"
2837                "#endif\n"
2838                "{\n"
2839                "}");
2840   verifyFormat("void f() {\n"
2841                "  if (true)\n"
2842                "#ifdef A\n"
2843                "    f(42);\n"
2844                "  x();\n"
2845                "#else\n"
2846                "    g();\n"
2847                "  x();\n"
2848                "#endif\n"
2849                "}");
2850   verifyFormat("void f(param1, param2,\n"
2851                "       param3,\n"
2852                "#ifdef A\n"
2853                "       param4(param5,\n"
2854                "#ifdef A1\n"
2855                "              param6,\n"
2856                "#ifdef A2\n"
2857                "              param7),\n"
2858                "#else\n"
2859                "              param8),\n"
2860                "       param9,\n"
2861                "#endif\n"
2862                "       param10,\n"
2863                "#endif\n"
2864                "       param11)\n"
2865                "#else\n"
2866                "       param12)\n"
2867                "#endif\n"
2868                "{\n"
2869                "  x();\n"
2870                "}",
2871                getLLVMStyleWithColumns(28));
2872   verifyFormat("#if 1\n"
2873                "int i;");
2874   verifyFormat("#if 1\n"
2875                "#endif\n"
2876                "#if 1\n"
2877                "#else\n"
2878                "#endif\n");
2879   verifyFormat("DEBUG({\n"
2880                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2881                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2882                "});\n"
2883                "#if a\n"
2884                "#else\n"
2885                "#endif");
2886
2887   verifyIncompleteFormat("void f(\n"
2888                          "#if A\n"
2889                          ");\n"
2890                          "#else\n"
2891                          "#endif");
2892 }
2893
2894 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2895   verifyFormat("#endif\n"
2896                "#if B");
2897 }
2898
2899 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2900   FormatStyle SingleLine = getLLVMStyle();
2901   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2902   verifyFormat("#if 0\n"
2903                "#elif 1\n"
2904                "#endif\n"
2905                "void foo() {\n"
2906                "  if (test) foo2();\n"
2907                "}",
2908                SingleLine);
2909 }
2910
2911 TEST_F(FormatTest, LayoutBlockInsideParens) {
2912   verifyFormat("functionCall({ int i; });");
2913   verifyFormat("functionCall({\n"
2914                "  int i;\n"
2915                "  int j;\n"
2916                "});");
2917   verifyFormat("functionCall(\n"
2918                "    {\n"
2919                "      int i;\n"
2920                "      int j;\n"
2921                "    },\n"
2922                "    aaaa, bbbb, cccc);");
2923   verifyFormat("functionA(functionB({\n"
2924                "            int i;\n"
2925                "            int j;\n"
2926                "          }),\n"
2927                "          aaaa, bbbb, cccc);");
2928   verifyFormat("functionCall(\n"
2929                "    {\n"
2930                "      int i;\n"
2931                "      int j;\n"
2932                "    },\n"
2933                "    aaaa, bbbb, // comment\n"
2934                "    cccc);");
2935   verifyFormat("functionA(functionB({\n"
2936                "            int i;\n"
2937                "            int j;\n"
2938                "          }),\n"
2939                "          aaaa, bbbb, // comment\n"
2940                "          cccc);");
2941   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2942   verifyFormat("functionCall(aaaa, bbbb, {\n"
2943                "  int i;\n"
2944                "  int j;\n"
2945                "});");
2946   verifyFormat(
2947       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2948       "    {\n"
2949       "      int i; // break\n"
2950       "    },\n"
2951       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2952       "                                     ccccccccccccccccc));");
2953   verifyFormat("DEBUG({\n"
2954                "  if (a)\n"
2955                "    f();\n"
2956                "});");
2957 }
2958
2959 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2960   EXPECT_EQ("SOME_MACRO { int i; }\n"
2961             "int i;",
2962             format("  SOME_MACRO  {int i;}  int i;"));
2963 }
2964
2965 TEST_F(FormatTest, LayoutNestedBlocks) {
2966   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2967                "  struct s {\n"
2968                "    int i;\n"
2969                "  };\n"
2970                "  s kBitsToOs[] = {{10}};\n"
2971                "  for (int i = 0; i < 10; ++i)\n"
2972                "    return;\n"
2973                "}");
2974   verifyFormat("call(parameter, {\n"
2975                "  something();\n"
2976                "  // Comment using all columns.\n"
2977                "  somethingelse();\n"
2978                "});",
2979                getLLVMStyleWithColumns(40));
2980   verifyFormat("DEBUG( //\n"
2981                "    { f(); }, a);");
2982   verifyFormat("DEBUG( //\n"
2983                "    {\n"
2984                "      f(); //\n"
2985                "    },\n"
2986                "    a);");
2987
2988   EXPECT_EQ("call(parameter, {\n"
2989             "  something();\n"
2990             "  // Comment too\n"
2991             "  // looooooooooong.\n"
2992             "  somethingElse();\n"
2993             "});",
2994             format("call(parameter, {\n"
2995                    "  something();\n"
2996                    "  // Comment too looooooooooong.\n"
2997                    "  somethingElse();\n"
2998                    "});",
2999                    getLLVMStyleWithColumns(29)));
3000   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3001   EXPECT_EQ("DEBUG({ // comment\n"
3002             "  int i;\n"
3003             "});",
3004             format("DEBUG({ // comment\n"
3005                    "int  i;\n"
3006                    "});"));
3007   EXPECT_EQ("DEBUG({\n"
3008             "  int i;\n"
3009             "\n"
3010             "  // comment\n"
3011             "  int j;\n"
3012             "});",
3013             format("DEBUG({\n"
3014                    "  int  i;\n"
3015                    "\n"
3016                    "  // comment\n"
3017                    "  int  j;\n"
3018                    "});"));
3019
3020   verifyFormat("DEBUG({\n"
3021                "  if (a)\n"
3022                "    return;\n"
3023                "});");
3024   verifyGoogleFormat("DEBUG({\n"
3025                      "  if (a) return;\n"
3026                      "});");
3027   FormatStyle Style = getGoogleStyle();
3028   Style.ColumnLimit = 45;
3029   verifyFormat("Debug(aaaaa,\n"
3030                "      {\n"
3031                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3032                "      },\n"
3033                "      a);",
3034                Style);
3035
3036   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3037
3038   verifyNoCrash("^{v^{a}}");
3039 }
3040
3041 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3042   EXPECT_EQ("#define MACRO()                     \\\n"
3043             "  Debug(aaa, /* force line break */ \\\n"
3044             "        {                           \\\n"
3045             "          int i;                    \\\n"
3046             "          int j;                    \\\n"
3047             "        })",
3048             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3049                    "          {  int   i;  int  j;   })",
3050                    getGoogleStyle()));
3051
3052   EXPECT_EQ("#define A                                       \\\n"
3053             "  [] {                                          \\\n"
3054             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3055             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3056             "  }",
3057             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3058                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3059                    getGoogleStyle()));
3060 }
3061
3062 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3063   EXPECT_EQ("{}", format("{}"));
3064   verifyFormat("enum E {};");
3065   verifyFormat("enum E {}");
3066 }
3067
3068 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3069   FormatStyle Style = getLLVMStyle();
3070   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3071   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3072   verifyFormat("FOO_BEGIN\n"
3073                "  FOO_ENTRY\n"
3074                "FOO_END", Style);
3075   verifyFormat("FOO_BEGIN\n"
3076                "  NESTED_FOO_BEGIN\n"
3077                "    NESTED_FOO_ENTRY\n"
3078                "  NESTED_FOO_END\n"
3079                "FOO_END", Style);
3080   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3081                "  int x;\n"
3082                "  x = 1;\n"
3083                "FOO_END(Baz)", Style);
3084 }
3085
3086 //===----------------------------------------------------------------------===//
3087 // Line break tests.
3088 //===----------------------------------------------------------------------===//
3089
3090 TEST_F(FormatTest, PreventConfusingIndents) {
3091   verifyFormat(
3092       "void f() {\n"
3093       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3094       "                         parameter, parameter, parameter)),\n"
3095       "                     SecondLongCall(parameter));\n"
3096       "}");
3097   verifyFormat(
3098       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3099       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3100       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3101       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3102   verifyFormat(
3103       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3104       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3105       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3106       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3107   verifyFormat(
3108       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3109       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3110       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3111       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3112   verifyFormat("int a = bbbb && ccc &&\n"
3113                "        fffff(\n"
3114                "#define A Just forcing a new line\n"
3115                "            ddd);");
3116 }
3117
3118 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3119   verifyFormat(
3120       "bool aaaaaaa =\n"
3121       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3122       "    bbbbbbbb();");
3123   verifyFormat(
3124       "bool aaaaaaa =\n"
3125       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3126       "    bbbbbbbb();");
3127
3128   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3129                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3130                "    ccccccccc == ddddddddddd;");
3131   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3132                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3133                "    ccccccccc == ddddddddddd;");
3134   verifyFormat(
3135       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3136       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3137       "    ccccccccc == ddddddddddd;");
3138
3139   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3140                "                 aaaaaa) &&\n"
3141                "         bbbbbb && cccccc;");
3142   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3143                "                 aaaaaa) >>\n"
3144                "         bbbbbb;");
3145   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3146                "    SourceMgr.getSpellingColumnNumber(\n"
3147                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3148                "    1);");
3149
3150   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3151                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3152                "    cccccc) {\n}");
3153   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3154                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3155                "              cccccc) {\n}");
3156   verifyFormat("b = a &&\n"
3157                "    // Comment\n"
3158                "    b.c && d;");
3159
3160   // If the LHS of a comparison is not a binary expression itself, the
3161   // additional linebreak confuses many people.
3162   verifyFormat(
3163       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3164       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3165       "}");
3166   verifyFormat(
3167       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3168       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3169       "}");
3170   verifyFormat(
3171       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3172       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3173       "}");