[llvm-branch-commits] [cfe-branch] r126420 - in /cfe/branches/Apple/palisade: lib/CodeGen/CGDecl.cpp lib/CodeGen/CGObjC.cpp lib/CodeGen/CodeGenFunction.h test/CodeGenObjC/for-in.m

Daniel Dunbar daniel at zuster.org
Thu Feb 24 12:02:48 PST 2011


Author: ddunbar
Date: Thu Feb 24 14:02:48 2011
New Revision: 126420

URL: http://llvm.org/viewvc/llvm-project?rev=126420&view=rev
Log:
Merge r126193:
--
Author: John McCall <rjmccall at apple.com>
Date:   Tue Feb 22 07:16:58 2011 +0000

    Establish the iteration variable of an ObjC for-in loop before
    emitting the collection expression.  Fixes some really, really broken
    code.

Modified:
    cfe/branches/Apple/palisade/lib/CodeGen/CGDecl.cpp
    cfe/branches/Apple/palisade/lib/CodeGen/CGObjC.cpp
    cfe/branches/Apple/palisade/lib/CodeGen/CodeGenFunction.h
    cfe/branches/Apple/palisade/test/CodeGenObjC/for-in.m

Modified: cfe/branches/Apple/palisade/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/palisade/lib/CodeGen/CGDecl.cpp?rev=126420&r1=126419&r2=126420&view=diff
==============================================================================
--- cfe/branches/Apple/palisade/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/branches/Apple/palisade/lib/CodeGen/CGDecl.cpp Thu Feb 24 14:02:48 2011
@@ -782,10 +782,12 @@
 }
 
 void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
+  assert(emission.Variable && "emission was not valid!");
+
   // If this was emitted as a global constant, we're done.
   if (emission.wasEmittedAsGlobal()) return;
 
-  const VarDecl &D = emission.Variable;
+  const VarDecl &D = *emission.Variable;
   QualType type = D.getType();
 
   // If this local has an initializer, emit it now.
@@ -940,10 +942,12 @@
 }
 
 void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
+  assert(emission.Variable && "emission was not valid!");
+
   // If this was emitted as a global constant, we're done.
   if (emission.wasEmittedAsGlobal()) return;
 
-  const VarDecl &D = emission.Variable;
+  const VarDecl &D = *emission.Variable;
 
   // Handle C++ destruction of variables.
   if (getLangOptions().CPlusPlus) {

Modified: cfe/branches/Apple/palisade/lib/CodeGen/CGObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/palisade/lib/CodeGen/CGObjC.cpp?rev=126420&r1=126419&r2=126420&view=diff
==============================================================================
--- cfe/branches/Apple/palisade/lib/CodeGen/CGObjC.cpp (original)
+++ cfe/branches/Apple/palisade/lib/CodeGen/CGObjC.cpp Thu Feb 24 14:02:48 2011
@@ -625,6 +625,11 @@
     return;
   }
 
+  // The local variable comes into scope immediately.
+  AutoVarEmission variable = AutoVarEmission::invalid();
+  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
+    variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
+
   CGDebugInfo *DI = getDebugInfo();
   if (DI) {
     DI->setLocation(S.getSourceRange().getBegin());
@@ -761,22 +766,23 @@
 
   // Initialize the element variable.
   RunCleanupsScope elementVariableScope(*this);
-  bool elementIsDecl;
+  bool elementIsVariable;
   LValue elementLValue;
   QualType elementType;
   if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
-    EmitStmt(SD);
-    const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
+    // Initialize the variable, in case it's a __block variable or something.
+    EmitAutoVarInit(variable);
 
+    const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
     DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(),
                         VK_LValue, SourceLocation());
     elementLValue = EmitLValue(&tempDRE);
     elementType = D->getType();
-    elementIsDecl = true;
+    elementIsVariable = true;
   } else {
     elementLValue = LValue(); // suppress warning
     elementType = cast<Expr>(S.getElement())->getType();
-    elementIsDecl = false;
+    elementIsVariable = false;
   }
   const llvm::Type *convertedElementType = ConvertType(elementType);
 
@@ -799,11 +805,16 @@
 
   // Make sure we have an l-value.  Yes, this gets evaluated every
   // time through the loop.
-  if (!elementIsDecl)
+  if (!elementIsVariable)
     elementLValue = EmitLValue(cast<Expr>(S.getElement()));
 
   EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, elementType);
 
+  // If we do have an element variable, this assignment is the end of
+  // its initialization.
+  if (elementIsVariable)
+    EmitAutoVarCleanups(variable);
+
   // Perform the loop body, setting up break and continue labels.
   BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
   {
@@ -853,7 +864,7 @@
   // No more elements.
   EmitBlock(EmptyBB);
 
-  if (!elementIsDecl) {
+  if (!elementIsVariable) {
     // If the element was not a declaration, set it to be null.
 
     llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);

Modified: cfe/branches/Apple/palisade/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/palisade/lib/CodeGen/CodeGenFunction.h?rev=126420&r1=126419&r2=126420&view=diff
==============================================================================
--- cfe/branches/Apple/palisade/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/branches/Apple/palisade/lib/CodeGen/CodeGenFunction.h Thu Feb 24 14:02:48 2011
@@ -1557,7 +1557,7 @@
   class AutoVarEmission {
     friend class CodeGenFunction;
 
-    const VarDecl &Variable;
+    const VarDecl *Variable;
 
     /// The alignment of the variable.
     CharUnits Alignment;
@@ -1575,13 +1575,18 @@
     /// initializer.
     bool IsConstantAggregate;
 
+    struct Invalid {};
+    AutoVarEmission(Invalid) : Variable(0) {}
+
     AutoVarEmission(const VarDecl &variable)
-      : Variable(variable), Address(0), NRVOFlag(0),
+      : Variable(&variable), Address(0), NRVOFlag(0),
         IsByRef(false), IsConstantAggregate(false) {}
 
     bool wasEmittedAsGlobal() const { return Address == 0; }
 
   public:
+    static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
+
     /// Returns the address of the object within this declaration.
     /// Note that this does not chase the forwarding pointer for
     /// __block decls.
@@ -1589,8 +1594,8 @@
       if (!IsByRef) return Address;
 
       return CGF.Builder.CreateStructGEP(Address,
-                                         CGF.getByRefValueLLVMField(&Variable),
-                                         Variable.getNameAsString());
+                                         CGF.getByRefValueLLVMField(Variable),
+                                         Variable->getNameAsString());
     }
   };
   AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);

Modified: cfe/branches/Apple/palisade/test/CodeGenObjC/for-in.m
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/palisade/test/CodeGenObjC/for-in.m?rev=126420&r1=126419&r2=126420&view=diff
==============================================================================
--- cfe/branches/Apple/palisade/test/CodeGenObjC/for-in.m (original)
+++ cfe/branches/Apple/palisade/test/CodeGenObjC/for-in.m Thu Feb 24 14:02:48 2011
@@ -42,3 +42,9 @@
       break;
   }
 }
+
+// rdar://problem/9027663
+void t2(NSArray *array) {
+  for (NSArray *array in array) { // expected-warning {{collection expression type 'NSArray *' may not respond}}
+  }
+}





More information about the llvm-branch-commits mailing list