]> granicus.if.org Git - clang/commitdiff
clang-format: Don't merge short else blocks.
authorDaniel Jasper <djasper@google.com>
Thu, 30 Apr 2015 09:24:17 +0000 (09:24 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 30 Apr 2015 09:24:17 +0000 (09:24 +0000)
Before (with the appropriate flags settings):
  if (a) {
    f();
  } else { g(); }

Before (with other appropriate flags settings):
  if (a) { f(); } else {
    g();
  }

After:
  if (a) {
    f();
  } else {
    g();
  }

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236217 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineFormatter.cpp
unittests/Format/FormatTest.cpp

index dd65230cc4060213e00b5ec06b4cbc24842d0eb5..888c69176ec4c5749d6acb99261339a1c35e13d9 100644 (file)
@@ -203,7 +203,8 @@ private:
 
     // Check that the current line allows merging. This depends on whether we
     // are in a control flow statements as well as several style flags.
-    if (Line.First->isOneOf(tok::kw_else, tok::kw_case))
+    if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
+        (Line.First->Next && Line.First->Next->is(tok::kw_else)))
       return 0;
     if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
                             tok::kw___try, tok::kw_catch, tok::kw___finally,
@@ -264,6 +265,10 @@ private:
       if (Tok->isNot(tok::r_brace))
         return 0;
 
+      // Don't merge "if (a) { .. } else {".
+      if (Tok->Next && Tok->Next->is(tok::kw_else))
+        return 0;
+
       return 2;
     }
     return 0;
index 9f340042c43df24f28524f9bb13666c32a47cc09..e2db4d10b952abd8e964f2356ea5996d5bc3117d 100644 (file)
@@ -416,6 +416,12 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
                "  f();\n"
                "}",
                AllowSimpleBracedStatements);
+  verifyFormat("if (true) {\n"
+               "  f();\n"
+               "} else {\n"
+               "  f();\n"
+               "}",
+               AllowSimpleBracedStatements);
 
   verifyFormat("template <int> struct A2 {\n"
                "  struct B {};\n"
@@ -427,6 +433,12 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
                "  f();\n"
                "}",
                AllowSimpleBracedStatements);
+  verifyFormat("if (true) {\n"
+               "  f();\n"
+               "} else {\n"
+               "  f();\n"
+               "}",
+               AllowSimpleBracedStatements);
 
   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
   verifyFormat("while (true) {\n"