From 3ba6535096a4a1abd57f7813183ac8fa0bccfc39 Mon Sep 17 00:00:00 2001 From: Akira Hatanaka Date: Mon, 2 May 2016 21:52:57 +0000 Subject: [CodeGenObjCXX] Don't rematerialize default arguments of function parameters in the body of a block. This fixes a bug where clang would materialize the default argument inside the body of a block instead of passing the value via the block descriptor. For example, in the code below, foo1 would always print 42 regardless of the value of argument "a" passed to foo1. void foo1(const int a = 42 ) { auto block = ^{ printf("%d\n", a); }; block(); } rdar://problem/24449235 llvm-svn: 268314 --- clang/lib/CodeGen/CGBlocks.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'clang/lib/CodeGen/CGBlocks.cpp') diff --git a/clang/lib/CodeGen/CGBlocks.cpp b/clang/lib/CodeGen/CGBlocks.cpp index ea164aad915..88786bc9fcf 100644 --- a/clang/lib/CodeGen/CGBlocks.cpp +++ b/clang/lib/CodeGen/CGBlocks.cpp @@ -262,6 +262,11 @@ static bool isSafeForCXXConstantCapture(QualType type) { static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM, CodeGenFunction *CGF, const VarDecl *var) { + // Don't rematerialize default arguments of function parameters. + if (auto *PD = dyn_cast(var)) + if (PD->hasDefaultArg()) + return nullptr; + QualType type = var->getType(); // We can only do this if the variable is const. -- cgit v1.2.3