[cfe-commits] r45539 - in /cfe/trunk: Parse/ParseDecl.cpp Parse/ParseObjc.cpp Parse/ParseStmt.cpp Sema/Sema.h Sema/SemaStmt.cpp include/clang/Parse/Action.h

Chris Lattner clattner at apple.com
Fri Jan 4 14:59:12 PST 2008


On Jan 3, 2008, at 9:55 AM, Fariborz Jahanian wrote:
> Patch to parse/build AST ObjC2's foreach statement.

>
> @@ -296,7 +296,9 @@
>     ConsumeToken();
>     return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
>   }
> -
> +  if (D.getContext()  == Declarator::ForContext &&  
> isObjCForCollectionInKW()) {
> +    return Actions.FinalizeDeclaratorGroup(CurScope,  
> LastDeclInGroup);
> +  }

Please add a comment above this that explains what is going on, since  
this is not near the for-each parsing code.  Something like:
   // If this is an ObjC2 for-each loop, this is a successful declarator
   // parse.  The syntax for these looks like:
   //   'for' '(' declaration 'in' expr ')' statement

This makes it more obvious to the reader what is going on.

> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/Parse/ParseStmt.cpp (original)
> +++ cfe/trunk/Parse/ParseStmt.cpp Thu Jan  3 11:55:25 2008
> @@ -721,6 +721,8 @@
> ///       for-statement: [C99 6.8.5.3]
> ///         'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')'  
> statement
> ///         'for' '(' declaration expr[opt] ';' expr[opt] ')'  
> statement
> +/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
> +/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
> Parser::StmtResult Parser::ParseForStatement() {
>   assert(Tok.is(tok::kw_for) && "Not a for stmt!");
>   SourceLocation ForLoc = ConsumeToken();  // eat the 'for'.
> @@ -744,6 +746,7 @@
>   StmtTy *FirstPart = 0;
>   ExprTy *SecondPart = 0;
>   StmtTy *ThirdPart = 0;
> +  bool foreach = false;

Please follow the naming conventions of the rest of the code, use  
"ForEach".

>
> -  return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,  
> SecondPart,
> -                              ThirdPart, RParenLoc, Body.Val);
> +  return !foreach ? Actions.ActOnForStmt(ForLoc, LParenLoc,  
> FirstPart,
> +                                         SecondPart, ThirdPart,  
> RParenLoc,
> +                                         Body.Val)
> +                  : Actions.ActOnObjcForCollectionStmt(ForLoc,  
> LParenLoc,
> +                                                       FirstPart,  
> SecondPart,
> +                                                       RParenLoc,  
> Body.Val);
> }

Please use an if statement instead of ?: to make the code more clear.

Thanks Fariborz,

-Chris



More information about the cfe-commits mailing list