diff options
author | Akira Hatanaka <ahatanaka@apple.com> | 2019-02-27 18:17:16 +0000 |
---|---|---|
committer | Akira Hatanaka <ahatanaka@apple.com> | 2019-02-27 18:17:16 +0000 |
commit | c5792aa90fa45a1842f190c146f19e2c71ea6fbd (patch) | |
tree | 3b0e58ac909124579b2391ffd93e404edea8dc6e /clang/lib/Sema/SemaExpr.cpp | |
parent | 69bec61998c702f790cf5c9f17a3a4a086bd5804 (diff) | |
download | bcm5719-llvm-c5792aa90fa45a1842f190c146f19e2c71ea6fbd.tar.gz bcm5719-llvm-c5792aa90fa45a1842f190c146f19e2c71ea6fbd.zip |
Avoid needlessly copying a block to the heap when a block literal
initializes a local auto variable or is assigned to a local auto
variable that is declared in the scope that introduced the block
literal.
rdar://problem/13289333
https://reviews.llvm.org/D58514
llvm-svn: 355012
Diffstat (limited to 'clang/lib/Sema/SemaExpr.cpp')
-rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index e91115aec5b..266120dfdc7 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -12443,6 +12443,25 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, if (!ResultTy.isNull()) { DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc, true); DiagnoseSelfMove(LHS.get(), RHS.get(), OpLoc); + + // Avoid copying a block to the heap if the block is assigned to a local + // auto variable that is declared in the same scope as the block. This + // optimization is unsafe if the local variable is declared in an outer + // scope. For example: + // + // BlockTy b; + // { + // b = ^{...}; + // } + // // It is unsafe to invoke the block here if it wasn't copied to the + // // heap. + // b(); + + if (auto *BE = dyn_cast<BlockExpr>(RHS.get()->IgnoreParens())) + if (auto *DRE = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParens())) + if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) + if (VD->hasLocalStorage() && getCurScope()->isDeclScope(VD)) + BE->getBlockDecl()->setCanAvoidCopyToHeap(); } RecordModifiableNonNullParam(*this, LHS.get()); break; |