[cfe-commits] r69331 - in /cfe/trunk: lib/Sema/SemaChecking.cpp lib/Sema/SemaExpr.cpp test/Sema/block-literal.c test/Sema/block-return.c

Mike Stump mrs at apple.com
Thu Apr 16 17:09:41 PDT 2009


Author: mrs
Date: Thu Apr 16 19:09:41 2009
New Revision: 69331

URL: http://llvm.org/viewvc/llvm-project?rev=69331&view=rev
Log:
Fixup semantic analysis for nested blocks, and allow block literal
expressions that can be of static duration to be returned.
Radar 6786551

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/block-literal.c
    cfe/trunk/test/Sema/block-return.c

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=69331&r1=69330&r2=69331&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu Apr 16 19:09:41 2009
@@ -872,8 +872,9 @@
       RetValExp = IcExpr->getSubExpr();
 
     if (BlockExpr *C = dyn_cast_or_null<BlockExpr>(RetValExp))
-      Diag(C->getLocStart(), diag::err_ret_local_block)
-        << C->getSourceRange();
+      if (C->hasBlockDeclRefExprs())
+        Diag(C->getLocStart(), diag::err_ret_local_block)
+          << C->getSourceRange();
   }
   // Perform checking for stack values returned by reference.
   else if (lhsType->isReferenceType()) {

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=69331&r1=69330&r2=69331&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Apr 16 19:09:41 2009
@@ -4774,6 +4774,11 @@
   if (BSI->ReturnType)
     RetTy = QualType(BSI->ReturnType, 0);
 
+  // A reference in a nested block, winds up being a reference in the outer
+  // block.
+  if (CurBlock)
+    CurBlock->hasBlockDeclRefExprs |= BSI->hasBlockDeclRefExprs;
+
   llvm::SmallVector<QualType, 8> ArgTypes;
   for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i)
     ArgTypes.push_back(BSI->Params[i]->getType());

Modified: cfe/trunk/test/Sema/block-literal.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/block-literal.c?rev=69331&r1=69330&r2=69331&view=diff

==============================================================================
--- cfe/trunk/test/Sema/block-literal.c (original)
+++ cfe/trunk/test/Sema/block-literal.c Thu Apr 16 19:09:41 2009
@@ -21,7 +21,7 @@
 
 	I(^{ });
 
-	return ^{printf("\nClosure\n"); };  // expected-error {{returning block that lives on the local stack}}
+	return ^{printf("\nClosure\n"); };
 }
 void test2() {
 	int x = 4;
@@ -46,7 +46,7 @@
 
 
 void (^test3())(void) { 
-  return ^{};   // expected-error {{returning block that lives on the local stack}}
+  return ^{};
 }
 
 void test4() {

Modified: cfe/trunk/test/Sema/block-return.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/block-return.c?rev=69331&r1=69330&r2=69331&view=diff

==============================================================================
--- cfe/trunk/test/Sema/block-return.c (original)
+++ cfe/trunk/test/Sema/block-return.c Thu Apr 16 19:09:41 2009
@@ -41,13 +41,13 @@
       return 2; // expected-warning {{incompatible integer to pointer conversion returning 'int', expected 'char *'}}
   };
 
-  return ^{ return 1; }; // expected-warning {{incompatible block pointer types returning 'int (^)(void)', expected 'CL'}} expected-error {{returning block that lives on the local stack}}
+  return ^{ return 1; }; // expected-warning {{incompatible block pointer types returning 'int (^)(void)', expected 'CL'}}
 }
 
 typedef int (^CL2)(void);
 
 CL2 foo2() {
-  return ^{ return 1; }; // expected-error {{returning block that lives on the local stack}}
+  return ^{ return 1; };
 }
 
 typedef unsigned int * uintptr_t;
@@ -83,3 +83,12 @@
   int (^nested)(char *s) = ^(char *str) { void (^nest)(void) = ^(void) { printf("%s\n", str); }; next(); return 1; }; // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \
   // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}}
 }
+
+typedef void (^bptr)(void);
+
+bptr foo5(int j) {
+  __block int i;
+  if (j)
+    return ^{ ^{ i=0; }(); };  // expected-error {{returning block that lives on the local stack}}
+  return ^{ i=0; };  // expected-error {{returning block that lives on the local stack}}
+}





More information about the cfe-commits mailing list