]> granicus.if.org Git - clang/commitdiff
Improve our parse recovery on 'case blah;' and 'default;'.
authorJohn McCall <rjmccall@apple.com>
Sat, 22 Jan 2011 09:28:32 +0000 (09:28 +0000)
committerJohn McCall <rjmccall@apple.com>
Sat, 22 Jan 2011 09:28:32 +0000 (09:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124025 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Parse/ParseStmt.cpp
test/Parser/switch-recovery.cpp

index 3a6f80da2d997620439e15bdecb4808dfed8d46d..5f932919b9250b7e6a00b8cbfcf3a474e0184e87 100644 (file)
@@ -311,13 +311,19 @@ StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs) {
     ColonProtection.restore();
 
     SourceLocation ColonLoc;
-    if (Tok.isNot(tok::colon)) {
+    if (Tok.is(tok::colon)) {
+      ColonLoc = ConsumeToken();
+
+    // Treat "case blah;" as a typo for "case blah:".
+    } else if (Tok.is(tok::semi)) {
+      ColonLoc = ConsumeToken();
+      Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
+        << FixItHint::CreateReplacement(ColonLoc, ":");
+    } else {
       SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
       Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
         << FixItHint::CreateInsertion(ExpectedLoc, ":");
       ColonLoc = ExpectedLoc;
-    } else {
-      ColonLoc = ConsumeToken();
     }
     
     StmtResult Case =
@@ -382,13 +388,19 @@ StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
   SourceLocation DefaultLoc = ConsumeToken();  // eat the 'default'.
 
   SourceLocation ColonLoc;
-  if (Tok.isNot(tok::colon)) {
+  if (Tok.is(tok::colon)) {
+    ColonLoc = ConsumeToken();
+
+  // Treat "default;" as a typo for "default:".
+  } else if (Tok.is(tok::semi)) {
+    ColonLoc = ConsumeToken();
+    Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
+      << FixItHint::CreateReplacement(ColonLoc, ":");
+  } else {
     SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
     Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
       << FixItHint::CreateInsertion(ExpectedLoc, ":");
     ColonLoc = ExpectedLoc;
-  } else {
-    ColonLoc = ConsumeToken();
   }
   
   // Diagnose the common error "switch (X) {... default: }", which is not valid.
index 7b6323a405c01040d682d25e53234a71ba439ae9..f11babc5b600f0b2d39831d04a92195158cab316 100644 (file)
@@ -17,4 +17,18 @@ struct B {
       return;
     }
   }
+
+  void test2() {
+    enum X { Xa, Xb } x;
+
+    switch (x) { // expected-warning {{enumeration value 'Xb' not handled in switch}}
+    case Xa; // expected-error {{expected ':' after 'case'}}
+      break;
+    }
+
+    switch (x) {
+    default; // expected-error {{expected ':' after 'default'}}
+      break;
+    }
+  }
 };