clang-format: Fix indentation of struct definitions with array init.
authorDaniel Jasper <djasper@google.com>
Mon, 20 Oct 2014 11:12:51 +0000 (11:12 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 20 Oct 2014 11:12:51 +0000 (11:12 +0000)
Before:
  struct {
    int x;
    int y;
  } points[] = {
        {1, 2}, {2, 3},
  };

After:
  struct {
    int x;
    int y;
  } points[] = {
      {1, 2}, {2, 3},
  };

llvm-svn: 220195

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

index 7e589c7..ad14aab 100644 (file)
@@ -1035,11 +1035,7 @@ static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
 /// operator precedence.
 class ExpressionParser {
 public:
-  ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
-    // Skip leading "}", e.g. in "} else if (...) {".
-    if (Current->is(tok::r_brace))
-      next();
-  }
+  ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {}
 
   /// \brief Parse expressions with the given operatore precedence.
   void parse(int Precedence = 0) {
@@ -1086,7 +1082,7 @@ public:
 
       // At the end of the line or when an operator with higher precedence is
       // found, insert fake parenthesis and return.
-      if (!Current || Current->closesScope() ||
+      if (!Current || (Current->closesScope() && Current->MatchingParen) ||
           (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
         if (LatestOperator) {
           LatestOperator->LastOperator = true;
index b669795..6961ab5 100644 (file)
@@ -2311,6 +2311,16 @@ TEST_F(FormatTest, NestedStaticInitializers) {
       "                 {kOsWin, \"Windows\"},\n"
       "                 {kOsLinux, \"Linux\"},\n"
       "                 {kOsCrOS, \"Chrome OS\"}};");
+  verifyFormat(
+      "struct {\n"
+      "  unsigned bit;\n"
+      "  const char *const name;\n"
+      "} kBitsToOs[] = {\n"
+      "    {kOsMac, \"Mac\"},\n"
+      "    {kOsWin, \"Windows\"},\n"
+      "    {kOsLinux, \"Linux\"},\n"
+      "    {kOsCrOS, \"Chrome OS\"},\n"
+      "};");
 }
 
 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {