Fixup semantic analysis for nested blocks, and allow block literal
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
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp
index 827f63c..8e3a421 100644
--- a/lib/Sema/SemaChecking.cpp
+++ b/lib/Sema/SemaChecking.cpp
@@ -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()) {
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index f903e8f..e0d28fa 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -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());