[cfe-commits] r69760 - in /cfe/trunk: lib/Parse/ParseStmt.cpp test/SemaObjC/foreach-1.m

Chris Lattner sabre at nondot.org
Tue Apr 21 17:54:41 PDT 2009


Author: lattner
Date: Tue Apr 21 19:54:41 2009
New Revision: 69760

URL: http://llvm.org/viewvc/llvm-project?rev=69760&view=rev
Log:
Fix a problem with objc foreach loop.  It turns out that objc mode changes
for scoping to match C99 even when in C89 mode.  This patch fixes this 
(eliminating a "redefinition of thisKey" error), and also prevents non-sensical
diagnostics in -pedantic mode like this:

t.m:7:8: warning: variable declaration in for loop is a C99-specific feature
  for (id thisKey in keys) ;
       ^


Modified:
    cfe/trunk/lib/Parse/ParseStmt.cpp
    cfe/trunk/test/SemaObjC/foreach-1.m

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

==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Tue Apr 21 19:54:41 2009
@@ -867,7 +867,7 @@
     return StmtError();
   }
 
-  bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
+  bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
 
   // C99 6.8.5p5 - In C99, the for statement is a block.  This is not
   // the case for C90.  Start the loop scope.
@@ -885,7 +885,7 @@
   // as those declared in the condition.
   //
   unsigned ScopeFlags;
-  if (C99orCXX)
+  if (C99orCXXorObjC)
     ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
                  Scope::DeclScope  | Scope::ControlScope;
   else
@@ -906,7 +906,7 @@
     ConsumeToken();
   } else if (isSimpleDeclaration()) {  // for (int X = 4;
     // Parse declaration, which eats the ';'.
-    if (!C99orCXX)   // Use of C99-style for loops in C90 mode?
+    if (!C99orCXXorObjC)   // Use of C99-style for loops in C90 mode?
       Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
 
     SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
@@ -976,7 +976,7 @@
   // for-init-statement/condition and a new scope for substatement in C++.
   //
   ParseScope InnerScope(this, Scope::DeclScope, 
-                        C99orCXX && Tok.isNot(tok::l_brace));
+                        C99orCXXorObjC && Tok.isNot(tok::l_brace));
 
   // Read the body statement.
   OwningStmtResult Body(ParseStatement());

Modified: cfe/trunk/test/SemaObjC/foreach-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/foreach-1.m?rev=69760&r1=69759&r2=69760&view=diff

==============================================================================
--- cfe/trunk/test/SemaObjC/foreach-1.m (original)
+++ cfe/trunk/test/SemaObjC/foreach-1.m Tue Apr 21 19:54:41 2009
@@ -1,10 +1,18 @@
-// RUN: clang-cc -fsyntax-only -verify %s
+/* RUN: clang-cc -fsyntax-only -verify -std=c89 -pedantic %s
+ */
 
 @class NSArray;
 
-void f(NSArray *a)
-{
-    for (int i in a); // expected-error{{selector element type 'int' is not a valid object}}
-    for ((id)2 in a); // expected-error{{selector element is not a valid lvalue}}
-    for (2 in a); // expected-error{{selector element is not a valid lvalue}}
+void f(NSArray *a) {
+    id keys;
+    for (int i in a); /* expected-error{{selector element type 'int' is not a valid object}} */
+    for ((id)2 in a); /* expected-error{{selector element is not a valid lvalue}} */
+    for (2 in a); /* expected-error{{selector element is not a valid lvalue}} */
+  
+  /* This should be ok, 'thisKey' should be scoped to the loop in question,
+   * and no diagnostics even in pedantic mode should happen.
+   * rdar://6814674
+   */
+  for (id thisKey in keys);
+  for (id thisKey in keys);
 }





More information about the cfe-commits mailing list