]> granicus.if.org Git - clang/commitdiff
Improve the diagnostics generated for switch statements missing expressions
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 13 Jun 2011 05:50:12 +0000 (05:50 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 13 Jun 2011 05:50:12 +0000 (05:50 +0000)
- Move the diagnostic to the case statement instead of at the end of the switch
- Add a fix-it hint as to how to fix the compilation error

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

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

index 6cc8b57b61febaa07e6cb53d78f0c5711346e2f0..6b4a0a404584a102ba5b0bed4a2890ce32974bd4 100644 (file)
@@ -502,6 +502,7 @@ StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
   StmtTy *DeepestParsedCaseStmt = 0;
 
   // While we have case statements, eat and stack them.
+  SourceLocation ColonLoc;
   do {
     SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
                                            ConsumeToken();  // eat the 'case'.
@@ -539,7 +540,6 @@ StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
     
     ColonProtection.restore();
 
-    SourceLocation ColonLoc;
     if (Tok.is(tok::colon)) {
       ColonLoc = ConsumeToken();
 
@@ -589,8 +589,9 @@ StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
   } else {
     // Nicely diagnose the common error "switch (X) { case 4: }", which is
     // not valid.
-    // FIXME: add insertion hint.
-    Diag(Tok, diag::err_label_end_of_compound_statement);
+    SourceLocation ExpectedLoc = PP.getLocForEndOfToken(ColonLoc);
+    Diag(ExpectedLoc, diag::err_label_end_of_compound_statement)
+      << FixItHint::CreateInsertion(ExpectedLoc, ";");
     SubStmt = true;
   }
 
@@ -634,7 +635,9 @@ StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
   
   // Diagnose the common error "switch (X) {... default: }", which is not valid.
   if (Tok.is(tok::r_brace)) {
-    Diag(Tok, diag::err_label_end_of_compound_statement);
+    SourceLocation ExpectedLoc = PP.getLocForEndOfToken(ColonLoc);
+    Diag(ExpectedLoc, diag::err_label_end_of_compound_statement)
+      << FixItHint::CreateInsertion(ExpectedLoc, ";");
     return StmtError();
   }
 
index 0e4dcfa3c6370c00c78805376021a3f557eea616..a1df4261db454c504e3e25cbab2a49ee9a2290a8 100644 (file)
@@ -156,3 +156,17 @@ void test12(int x) {
     }
   }
 }
+
+void missing_statement_case(int x) {
+  switch (x) {
+    case 1:
+    case 0: // expected-error {{label at end of compound statement: expected statement}}
+  }
+}
+
+void missing_statement_default(int x) {
+  switch (x) {
+    case 0:
+    default: // expected-error {{label at end of compound statement: expected statement}}
+  }
+}