]> granicus.if.org Git - clang/commitdiff
Objective-C foreach selector elements must be lvalues.
authorAnders Carlsson <andersca@mac.com>
Mon, 25 Aug 2008 18:16:36 +0000 (18:16 +0000)
committerAnders Carlsson <andersca@mac.com>
Mon, 25 Aug 2008 18:16:36 +0000 (18:16 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55316 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticKinds.def
lib/Sema/SemaStmt.cpp
test/SemaObjC/foreach-1.m [new file with mode: 0644]

index 7e1ccc7f259ee273ee1acf8b8c33ca314707a9e0..914434a7efea5b7f2c4947e2881c0b7f0f373437 100644 (file)
@@ -489,6 +489,8 @@ DIAG(err_collection_expr_type, ERROR,
      "collection expression type ('%0') is not a valid object")
 DIAG(err_selector_element_type, ERROR,
      "selector element type ('%0') is not a valid object")
+DIAG(err_selector_element_not_lvalue, ERROR,
+    "selector element is not a valid lvalue") 
 DIAG(err_toomany_element_decls, ERROR,
      "Only one element declaration is allowed")
 DIAG(warn_expected_implementation, WARNING,
index 619eb772c34896262d3524b2bef2066f57e1df2b..629ab3465ddec37a8d6bd4031a18a62d6ce0c05f 100644 (file)
@@ -562,11 +562,18 @@ Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
         return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for);
       if (D->getNextDeclarator())
         return Diag(D->getLocation(), diag::err_toomany_element_decls);
-    } else
-      FirstType = static_cast<Expr*>(first)->getType();
+    } else {
+      Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
+      
+      if (lval != Expr::LV_Valid)
+        return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue,
+                    First->getSourceRange());
+
+      FirstType = static_cast<Expr*>(first)->getType();        
+    }
     if (!Context.isObjCObjectPointerType(FirstType))
         Diag(ForLoc, diag::err_selector_element_type,
-             FirstType.getAsString(), First->getSourceRange());
+             FirstType.getAsString(), First->getSourceRange());          
   }
   if (Second) {
     DefaultFunctionArrayConversion(Second);
diff --git a/test/SemaObjC/foreach-1.m b/test/SemaObjC/foreach-1.m
new file mode 100644 (file)
index 0000000..17c0379
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: clang -fsyntax-only -verify %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}}
+}
\ No newline at end of file