From 0ecea03077117c7ea54c88091a44c73cef67923c Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 22 Aug 2007 05:28:50 +0000 Subject: [PATCH] Fix the scoping issue Neil pointed out for the rest of the selection statements and iteration statements. Add spec citations. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41264 91177308-0d34-0410-b5e6-96231b3b80d8 --- Parse/ParseStmt.cpp | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/Parse/ParseStmt.cpp b/Parse/ParseStmt.cpp index f96da44623..44026f627c 100644 --- a/Parse/ParseStmt.cpp +++ b/Parse/ParseStmt.cpp @@ -433,8 +433,8 @@ Parser::StmtResult Parser::ParseIfStatement() { return true; } - // In C99, the body of the if statement is a scope, even if there is no - // compound stmt. + // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if + // there is no compound stmt. C90 does not have this clause. if (getLang().C99) EnterScope(0); // Read the if condition. @@ -453,8 +453,8 @@ Parser::StmtResult Parser::ParseIfStatement() { if (Tok.getKind() == tok::kw_else) { ElseLoc = ConsumeToken(); - // In C99, the body of the if statement is a scope, even if there is no - // compound stmt. + // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if + // there is no compound stmt. C90 does not have this clause. if (getLang().C99) EnterScope(0); ElseStmt = ParseStatement(); @@ -496,9 +496,16 @@ Parser::StmtResult Parser::ParseSwitchStatement() { StmtResult Switch = Actions.StartSwitchStmt(Cond.Val); + // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if + // there is no compound stmt. C90 does not have this clause. + if (getLang().C99) EnterScope(0); + // Read the body statement. StmtResult Body = ParseStatement(); + // Pop the body scope if needed. + if (getLang().C99) ExitScope(); + if (Body.isInvalid) { Body = Actions.ParseNullStmt(Tok.getLocation()); // FIXME: Remove the case statement list from the Switch statement. @@ -529,9 +536,16 @@ Parser::StmtResult Parser::ParseWhileStatement() { // Parse the condition. ExprResult Cond = ParseSimpleParenExpression(); + // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if + // there is no compound stmt. C90 does not have this clause. + if (getLang().C99) EnterScope(0); + // Read the body statement. StmtResult Body = ParseStatement(); + // Pop the body scope if needed. + if (getLang().C99) ExitScope(); + ExitScope(); if (Cond.isInvalid || Body.isInvalid) return true; @@ -550,9 +564,16 @@ Parser::StmtResult Parser::ParseDoStatement() { // Start the loop scope. EnterScope(Scope::BreakScope | Scope::ContinueScope); + // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if + // there is no compound stmt. C90 does not have this clause. + if (getLang().C99) EnterScope(0); + // Read the body statement. StmtResult Body = ParseStatement(); + // Pop the body scope if needed. + if (getLang().C99) ExitScope(); + if (Tok.getKind() != tok::kw_while) { ExitScope(); Diag(Tok, diag::err_expected_while); @@ -665,9 +686,16 @@ Parser::StmtResult Parser::ParseForStatement() { // Match the ')'. SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); + // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if + // there is no compound stmt. C90 does not have this clause. + if (getLang().C99) EnterScope(0); + // Read the body statement. StmtResult Body = ParseStatement(); + // Pop the body scope if needed. + if (getLang().C99) ExitScope(); + // Leave the for-scope. ExitScope(); -- 2.40.0