[cfe-commits] r38851 - in /cfe/cfe/trunk: Parse/ParseStmt.cpp include/clang/Basic/DiagnosticKinds.def include/clang/Parse/Parser.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:25:11 PDT 2007


Author: sabre
Date: Wed Jul 11 11:25:10 2007
New Revision: 38851

URL: http://llvm.org/viewvc/llvm-project?rev=38851&view=rev
Log:
Parse case/default labeled-statements.

Modified:
    cfe/cfe/trunk/Parse/ParseStmt.cpp
    cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/cfe/trunk/include/clang/Parse/Parser.h

Modified: cfe/cfe/trunk/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseStmt.cpp?rev=38851&r1=38850&r2=38851&view=diff

==============================================================================
--- cfe/cfe/trunk/Parse/ParseStmt.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseStmt.cpp Wed Jul 11 11:25:10 2007
@@ -71,15 +71,33 @@
 void Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
   const char *SemiError = 0;
   
+ParseNextStatement:
   // Cases in this switch statement should fall through if the parser expects
   // the token to end in a semicolon (in which case SemiError should be set),
   // or they directly 'return;' if not.
   switch (Tok.getKind()) {
   default:
-    Diag(Tok, diag::err_expected_statement_declaration);
+    Diag(Tok, OnlyStatement ? diag::err_expected_statement :
+                              diag::err_expected_statement_declaration);
     SkipUntil(tok::semi);
     return;
     
+  case tok::kw_case:                // C99 6.8.1: labeled-statement
+    ParseCaseStatement();
+    if (Tok.getKind() == tok::r_brace) {
+      Diag(Tok, diag::err_label_end_of_compound_statement);
+      return;
+    }
+    OnlyStatement = true;
+    goto ParseNextStatement;
+  case tok::kw_default:             // C99 6.8.1: labeled-statement
+    ParseDefaultStatement();
+    if (Tok.getKind() == tok::r_brace) {
+      Diag(Tok, diag::err_label_end_of_compound_statement);
+      return;
+    }
+    OnlyStatement = true;
+    goto ParseNextStatement;
     
   case tok::l_brace:                // C99 6.8.2: compound-statement
     ParseCompoundStatement();
@@ -135,6 +153,44 @@
   }
 }
 
+/// ParseCaseStatement
+///       labeled-statement:
+///         'case' constant-expression ':' statement
+///
+/// Note that this does not parse the 'statement' at the end.
+///
+void Parser::ParseCaseStatement() {
+  assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
+  ConsumeToken();  // eat the 'case'.
+
+  ParseExpression();
+  
+  if (Tok.getKind() == tok::colon) {
+    ConsumeToken();
+  } else {
+    Diag(Tok, diag::err_expected_colon_after, "'case'");
+    SkipUntil(tok::colon);
+  }
+}
+
+/// ParseDefaultStatement
+///       labeled-statement:
+///         'default' ':' statement
+/// Note that this does not parse the 'statement' at the end.
+///
+void Parser::ParseDefaultStatement() {
+  assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
+  ConsumeToken();  // eat the 'default'.
+
+  if (Tok.getKind() == tok::colon) {
+    ConsumeToken();
+  } else {
+    Diag(Tok, diag::err_expected_colon_after, "'default'");
+    SkipUntil(tok::colon);
+  }
+}
+
+
 /// ParseCompoundStatement - Parse a "{}" block.
 ///
 ///       compound-statement: [C99 6.8.2]

Modified: cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=38851&r1=38850&r2=38851&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:25:10 2007
@@ -276,6 +276,8 @@
      "expected function body after function declarator")
 DIAG(err_expected_after_declarator, ERROR,
      "expected '=', ',', ';', 'asm', or '__attribute__' after declarator")
+DIAG(err_expected_statement, ERROR,
+     "expected statement")
 DIAG(err_expected_statement_declaration, ERROR,
      "expected statement or declaration")
 DIAG(err_expected_lparen_after, ERROR,
@@ -286,6 +288,10 @@
      "expected ';' after %s")
 DIAG(err_expected_semi_for, ERROR,
      "expected ';' in 'for' statement specifier")
+DIAG(err_expected_colon_after, ERROR,
+     "expected ':' after %s")
+DIAG(err_label_end_of_compound_statement, ERROR,
+     "label at end of compound statement: expected statement")
 
 /// err_matching - this is used as a continuation of a previous error, e.g. to 
 /// specify the '(' when we expected a ')'.  This should probably be some

Modified: cfe/cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Parse/Parser.h?rev=38851&r1=38850&r2=38851&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Parser.h Wed Jul 11 11:25:10 2007
@@ -160,6 +160,8 @@
   // C99 6.8: Statements and Blocks.
   void ParseStatement() { ParseStatementOrDeclaration(true); }
   void ParseStatementOrDeclaration(bool OnlyStatement = false);
+  void ParseCaseStatement();
+  void ParseDefaultStatement();
   void ParseCompoundStatement();
   void ParseIfStatement();
   void ParseSwitchStatement();





More information about the cfe-commits mailing list