r236217 - clang-format: Don't merge short else blocks.

Daniel Jasper djasper at google.com
Thu Apr 30 02:24:29 PDT 2015


Author: djasper
Date: Thu Apr 30 04:24:17 2015
New Revision: 236217

URL: http://llvm.org/viewvc/llvm-project?rev=236217&view=rev
Log:
clang-format: Don't merge short else blocks.

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();
  }

Modified:
    cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp?rev=236217&r1=236216&r2=236217&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp Thu Apr 30 04:24:17 2015
@@ -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;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=236217&r1=236216&r2=236217&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Apr 30 04:24:17 2015
@@ -416,6 +416,12 @@ TEST_F(FormatTest, FormatShortBracedStat
                "  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, FormatShortBracedStat
                "  f();\n"
                "}",
                AllowSimpleBracedStatements);
+  verifyFormat("if (true) {\n"
+               "  f();\n"
+               "} else {\n"
+               "  f();\n"
+               "}",
+               AllowSimpleBracedStatements);
 
   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
   verifyFormat("while (true) {\n"





More information about the cfe-commits mailing list