[cfe-commits] [PATCH] Enum formatting implementation

Alexander Kornienko alexfh at google.com
Tue Dec 4 05:44:54 PST 2012


Hi djasper, klimek,

http://llvm-reviews.chandlerc.com/D161

Files:
  lib/Format/UnwrappedLineParser.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -99,19 +99,38 @@
 }
 
 void UnwrappedLineParser::parseStatement() {
-  if (FormatTok.Tok.is(tok::kw_public) || FormatTok.Tok.is(tok::kw_protected) ||
-      FormatTok.Tok.is(tok::kw_private)) {
+  switch (FormatTok.Tok.getKind()) {
+  case tok::kw_public:
+  case tok::kw_protected:
+  case tok::kw_private:
     parseAccessSpecifier();
     return;
-  }
-  if (FormatTok.Tok.is(tok::kw_enum)) {
-    parseEnum();
+  case tok::kw_if:
+    parseIfThenElse();
+    return;
+  case tok::kw_do:
+    parseDoWhile();
+    return;
+  case tok::kw_switch:
+    parseSwitch();
+    return;
+  case tok::kw_default:
+    nextToken();
+    parseLabel();
+    return;
+  case tok::kw_case:
+    parseCaseLabel();
     return;
+  default:
+    break;
   }
   int TokenNumber = 0;
   do {
     ++TokenNumber;
     switch (FormatTok.Tok.getKind()) {
+    case tok::kw_enum:
+      parseEnum();
+      return;
     case tok::semi:
       nextToken();
       addUnwrappedLine();
@@ -123,32 +142,16 @@
       parseBlock();
       addUnwrappedLine();
       return;
-    case tok::kw_if:
-      parseIfThenElse();
-      return;
-    case tok::kw_do:
-      parseDoWhile();
-      return;
-    case tok::kw_switch:
-      parseSwitch();
-      return;
-    case tok::kw_default:
-      nextToken();
-      parseLabel();
-      return;
-    case tok::kw_case:
-      parseCaseLabel();
-      return;
-    case tok::raw_identifier:
-      nextToken();
-      break;
-    default:
+    case tok::identifier:
       nextToken();
       if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) {
         parseLabel();
         return;
       }
       break;
+    default:
+      nextToken();
+      break;
     }
   } while (!eof());
 }
@@ -265,12 +268,35 @@
 }
 
 void UnwrappedLineParser::parseEnum() {
+  bool HasContents = false;
   do {
-    nextToken();
-    if (FormatTok.Tok.is(tok::semi)) {
+    switch (FormatTok.Tok.getKind()) {
+    case tok::l_brace:
+      nextToken();
+      addUnwrappedLine();
+      ++Line.Level;
+      break;
+    case tok::l_paren:
+      parseParens();
+      break;
+    case tok::comma:
+      nextToken();
+      addUnwrappedLine();
+      break;
+    case tok::r_brace:
+      if (HasContents)
+        addUnwrappedLine();
+      --Line.Level;
+      nextToken();
+      break;
+    case tok::semi:
       nextToken();
       addUnwrappedLine();
       return;
+    default:
+      HasContents = true;
+      nextToken();
+      break;
     }
   } while (!eof());
 }
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -244,6 +244,21 @@
                "while (something());");
 }
 
+TEST_F(FormatTest, Enum) {
+  verifyFormat("enum {\n"
+               "  Zero,\n"
+               "  One = 1,\n"
+               "  Two = One + 1,\n"
+               "  Three = (One + Two),\n"
+               "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
+               "  Five = (One, Two, Three, Four, 5)\n"
+               "};");
+  verifyFormat("enum Enum {\n"
+               "};");
+  verifyFormat("enum {\n"
+               "};");
+}
+
 TEST_F(FormatTest, BreaksDesireably) {
   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D161.1.patch
Type: text/x-patch
Size: 3540 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121204/564fe3a2/attachment.bin>


More information about the cfe-commits mailing list