]> granicus.if.org Git - clang/commitdiff
Fixup semantic analysis for nested blocks, and allow block literal
authorMike Stump <mrs@apple.com>
Fri, 17 Apr 2009 00:09:41 +0000 (00:09 +0000)
committerMike Stump <mrs@apple.com>
Fri, 17 Apr 2009 00:09:41 +0000 (00:09 +0000)
expressions that can be of static duration to be returned.
Radar 6786551

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69331 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
lib/Sema/SemaExpr.cpp
test/Sema/block-literal.c
test/Sema/block-return.c

index 827f63cfcd4fe5eec603051e322c84e2eda4f950..8e3a4212e05757e9030f0b4ae73adb7551bd44c7 100644 (file)
@@ -872,8 +872,9 @@ Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
       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()) {
index f903e8f2535ef187566668118e67ad3c0f19c5a0..e0d28fa140ed0dceba11cf1925a9325281ff8fb2 100644 (file)
@@ -4774,6 +4774,11 @@ Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc,
   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());
index 040c383033b2ee12e0761b8b6f6e48cc03e17140..2c1700a7b71d1f52834cf468a7ac29cdf4d9a036 100644 (file)
@@ -21,7 +21,7 @@ T somefunction() {
 
        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 @@ foo:
 
 
 void (^test3())(void) { 
-  return ^{};   // expected-error {{returning block that lives on the local stack}}
+  return ^{};
 }
 
 void test4() {
index d7717b38ca379fd76255c6d41022030501b218d3..3eee002b6038a140fd56fe96d1118449fd73530e 100644 (file)
@@ -41,13 +41,13 @@ CL foo() {
       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 @@ void foo4() {
   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}}
+}