]> granicus.if.org Git - clang/commitdiff
clang-format: Improve recovery from enums with errors.
authorDaniel Jasper <djasper@google.com>
Fri, 30 Aug 2013 10:10:19 +0000 (10:10 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 30 Aug 2013 10:10:19 +0000 (10:10 +0000)
Before:
  namespace n {
  enum Type {
    One,
    Two, // missing };
    int i;
  } void g() {
  }

After:
  namespace n {
  enum Type {
    One,
    Two, // missing };
    int i;
  }
  void g() {}

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

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

index 9e4600b430dbff8afab81569dfe89772f66a1463..fb63899f4719842ca376edfa4391da56e6d1483b 100644 (file)
@@ -937,6 +937,7 @@ void UnwrappedLineParser::parseEnum() {
     if (FormatTok->Tok.is(tok::identifier))
       nextToken();
   }
+  bool HasError = false;
   if (FormatTok->Tok.is(tok::l_brace)) {
     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
       addUnwrappedLine();
@@ -952,7 +953,17 @@ void UnwrappedLineParser::parseEnum() {
         addUnwrappedLine();
         nextToken();
         --Line->Level;
+        if (HasError) {
+          if (FormatTok->is(tok::semi))
+            nextToken();
+          addUnwrappedLine();
+        }
         return;
+      case tok::semi:
+        HasError = true;
+        nextToken();
+        addUnwrappedLine();
+        break;
       case tok::comma:
         nextToken();
         addUnwrappedLine();
index 9c8efd882c4de25eff3ba24dcb8ae09a10e92be9..aef1dae6fdf40ac68ea6f825cff65a2ee0da37f9 100644 (file)
@@ -1519,6 +1519,20 @@ TEST_F(FormatTest, FormatsEnum) {
   verifyFormat("enum X f() {\n  a();\n  return 42;\n}");
 }
 
+TEST_F(FormatTest, FormatsEnumsWithErrors) {
+  verifyFormat("enum Type {\n"
+               "  One = 0;\n" // These semicolons should be commas.
+               "  Two = 1;\n"
+               "};");
+  verifyFormat("namespace n {\n"
+               "enum Type {\n"
+               "  One,\n"
+               "  Two,\n" // missing };
+               "  int i;\n"
+               "}\n"
+               "void g() {}");
+}
+
 TEST_F(FormatTest, FormatsEnumStruct) {
   verifyFormat("enum struct {\n"
                "  Zero,\n"