[cfe-commits] r173075 - in /cfe/trunk: lib/Format/UnwrappedLineParser.cpp unittests/Format/FormatTest.cpp

Manuel Klimek klimek at google.com
Mon Jan 21 11:17:52 PST 2013


Author: klimek
Date: Mon Jan 21 13:17:52 2013
New Revision: 173075

URL: http://llvm.org/viewvc/llvm-project?rev=173075&view=rev
Log:
Fixes various problems around enum parsing.

Very similar to what we do for record definitions:
- tighten down what is an enum definition, so that we don't mistake a
  function for an enum
- allow common idioms around declarations (we'll want to handle that
  more centrally in the future)

We now correctly format:
enum X f() {
  a();
  return 42;
}

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

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=173075&r1=173074&r2=173075&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Jan 21 13:17:52 2013
@@ -329,7 +329,7 @@
     switch (FormatTok.Tok.getKind()) {
     case tok::kw_enum:
       parseEnum();
-      return;
+      break;
     case tok::kw_struct:
     case tok::kw_union:
     case tok::kw_class:
@@ -580,39 +580,50 @@
 }
 
 void UnwrappedLineParser::parseEnum() {
-  bool HasContents = false;
-  do {
-    switch (FormatTok.Tok.getKind()) {
-    case tok::l_brace:
-      nextToken();
-      addUnwrappedLine();
-      ++Line->Level;
-      parseComments();
-      break;
-    case tok::l_paren:
+  nextToken();
+  if (FormatTok.Tok.is(tok::identifier) ||
+      FormatTok.Tok.is(tok::kw___attribute) ||
+      FormatTok.Tok.is(tok::kw___declspec)) {
+    nextToken();
+    // We can have macros or attributes in between 'enum' and the enum name.
+    if (FormatTok.Tok.is(tok::l_paren)) {
       parseParens();
-      break;
-    case tok::comma:
+    }
+    if (FormatTok.Tok.is(tok::identifier))
       nextToken();
-      addUnwrappedLine();
-      parseComments();
-      break;
-    case tok::r_brace:
-      if (HasContents)
+  }
+  if (FormatTok.Tok.is(tok::l_brace)) {
+    nextToken();
+    addUnwrappedLine();
+    ++Line->Level;
+    do {
+      switch (FormatTok.Tok.getKind()) {
+      case tok::comment:
+        // FIXME: Handle comments centrally, instead of special casing
+        // them everywhere.
+        parseComments();
+        break;
+      case tok::l_paren:
+        parseParens();
+        break;
+      case tok::r_brace:
         addUnwrappedLine();
-      --Line->Level;
-      nextToken();
-      break;
-    case tok::semi:
-      nextToken();
-      addUnwrappedLine();
-      return;
-    default:
-      HasContents = true;
-      nextToken();
-      break;
-    }
-  } while (!eof());
+        nextToken();
+        --Line->Level;
+        return;
+      case tok::comma:
+        nextToken();
+        addUnwrappedLine();
+        break;
+      default:
+        nextToken();
+        break;
+      }
+    } while (!eof());
+  }
+  // We fall through to parsing a structural element afterwards, so that in
+  // enum A {} n, m;
+  // "} n, m;" will end up in one unwrapped line.
 }
 
 void UnwrappedLineParser::parseRecord() {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=173075&r1=173074&r2=173075&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 21 13:17:52 2013
@@ -499,6 +499,10 @@
                "};");
   verifyFormat("enum {\n"
                "};");
+  verifyFormat("enum X E {\n} d;");
+  verifyFormat("enum __attribute__((...)) E {\n} d;");
+  verifyFormat("enum __declspec__((...)) E {\n} d;");
+  verifyFormat("enum X f() {\n  a();\n  return 42;\n}");
 }
 
 TEST_F(FormatTest, FormatsBitfields) {





More information about the cfe-commits mailing list