Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit b29b12d

Browse files
committed
When initializing a catch variable in ARC, be sure to emit retains
or whatever else is required for the initialization instead of assuming it can be done with a simple store. Fixes PR11732. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148325 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 29c3f81 commit b29b12d

File tree

4 files changed

+165
-3
lines changed

4 files changed

+165
-3
lines changed

lib/CodeGen/CGException.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -984,8 +984,23 @@ static void InitCatchParam(CodeGenFunction &CGF,
984984
if (CatchType->hasPointerRepresentation()) {
985985
llvm::Value *CastExn =
986986
CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
987-
CGF.Builder.CreateStore(CastExn, ParamAddr);
988-
return;
987+
988+
switch (CatchType.getQualifiers().getObjCLifetime()) {
989+
case Qualifiers::OCL_Strong:
990+
CastExn = CGF.EmitARCRetainNonBlock(CastExn);
991+
// fallthrough
992+
993+
case Qualifiers::OCL_None:
994+
case Qualifiers::OCL_ExplicitNone:
995+
case Qualifiers::OCL_Autoreleasing:
996+
CGF.Builder.CreateStore(CastExn, ParamAddr);
997+
return;
998+
999+
case Qualifiers::OCL_Weak:
1000+
CGF.EmitARCInitWeak(ParamAddr, CastExn);
1001+
return;
1002+
}
1003+
llvm_unreachable("bad ownership qualifier!");
9891004
}
9901005

9911006
// Otherwise, it returns a pointer into the exception object.

lib/CodeGen/CGObjCRuntime.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,24 @@ void CGObjCRuntime::EmitTryCatchStmt(CodeGenFunction &CGF,
246246
llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
247247

248248
CGF.EmitAutoVarDecl(*CatchParam);
249-
CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
249+
250+
llvm::Value *CatchParamAddr = CGF.GetAddrOfLocalVar(CatchParam);
251+
252+
switch (CatchParam->getType().getQualifiers().getObjCLifetime()) {
253+
case Qualifiers::OCL_Strong:
254+
CastExn = CGF.EmitARCRetainNonBlock(CastExn);
255+
// fallthrough
256+
257+
case Qualifiers::OCL_None:
258+
case Qualifiers::OCL_ExplicitNone:
259+
case Qualifiers::OCL_Autoreleasing:
260+
CGF.Builder.CreateStore(CastExn, CatchParamAddr);
261+
break;
262+
263+
case Qualifiers::OCL_Weak:
264+
CGF.EmitARCInitWeak(CatchParamAddr, CastExn);
265+
break;
266+
}
250267
}
251268

252269
CGF.ObjCEHValueStack.push_back(Exn);

test/CodeGenObjC/arc-exceptions.m

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fobjc-arc -fexceptions -fobjc-exceptions -fobjc-runtime-has-weak -o - %s | FileCheck %s
2+
3+
@class Ety;
4+
5+
// These first two tests are all PR11732 / rdar://problem/10667070.
6+
7+
void test0_helper(void);
8+
void test0(void) {
9+
@try {
10+
test0_helper();
11+
} @catch (Ety *e) {
12+
}
13+
}
14+
// CHECK: define void @test0()
15+
// CHECK: [[E:%.*]] = alloca [[ETY:%.*]]*, align 8
16+
// CHECK-NEXT: invoke void @test0_helper()
17+
// CHECK: [[T0:%.*]] = call i8* @objc_begin_catch(
18+
// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to [[ETY]]*
19+
// CHECK-NEXT: [[T2:%.*]] = bitcast [[ETY]]* [[T1]] to i8*
20+
// CHECK-NEXT: [[T3:%.*]] = call i8* @objc_retain(i8* [[T2]]) nounwind
21+
// CHECK-NEXT: [[T4:%.*]] = bitcast i8* [[T3]] to [[ETY]]*
22+
// CHECK-NEXT: store [[ETY]]* [[T4]], [[ETY]]** [[E]]
23+
// CHECK-NEXT: [[T0:%.*]] = load [[ETY]]** [[E]]
24+
// CHECK-NEXT: [[T1:%.*]] = bitcast [[ETY]]* [[T0]] to i8*
25+
// CHECK-NEXT: call void @objc_release(i8* [[T1]]) nounwind
26+
// CHECK-NEXT: call void @objc_end_catch() nounwind
27+
28+
void test1_helper(void);
29+
void test1(void) {
30+
@try {
31+
test1_helper();
32+
} @catch (__weak Ety *e) {
33+
}
34+
}
35+
// CHECK: define void @test1()
36+
// CHECK: [[E:%.*]] = alloca [[ETY:%.*]]*, align 8
37+
// CHECK-NEXT: invoke void @test1_helper()
38+
// CHECK: [[T0:%.*]] = call i8* @objc_begin_catch(
39+
// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to [[ETY]]*
40+
// CHECK-NEXT: [[T2:%.*]] = bitcast [[ETY]]** [[E]] to i8**
41+
// CHECK-NEXT: [[T3:%.*]] = bitcast [[ETY]]* [[T1]] to i8*
42+
// CHECK-NEXT: call i8* @objc_initWeak(i8** [[T2]], i8* [[T3]]) nounwind
43+
// CHECK-NEXT: [[T0:%.*]] = bitcast [[ETY]]** [[E]] to i8**
44+
// CHECK-NEXT: call void @objc_destroyWeak(i8** [[T0]]) nounwind
45+
// CHECK-NEXT: call void @objc_end_catch() nounwind

test/CodeGenObjCXX/arc-exceptions.mm

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fobjc-arc -fexceptions -fobjc-exceptions -fcxx-exceptions -fobjc-runtime-has-weak -o - %s | FileCheck %s
2+
3+
@class Ety;
4+
5+
// These first four tests are all PR11732 / rdar://problem/10667070.
6+
7+
void test0_helper(void);
8+
void test0(void) {
9+
@try {
10+
test0_helper();
11+
} @catch (Ety *e) {
12+
}
13+
}
14+
// CHECK: define void @_Z5test0v()
15+
// CHECK: [[E:%.*]] = alloca [[ETY:%.*]]*, align 8
16+
// CHECK-NEXT: invoke void @_Z12test0_helperv()
17+
// CHECK: [[T0:%.*]] = call i8* @objc_begin_catch(
18+
// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to [[ETY]]*
19+
// CHECK-NEXT: [[T2:%.*]] = bitcast [[ETY]]* [[T1]] to i8*
20+
// CHECK-NEXT: [[T3:%.*]] = call i8* @objc_retain(i8* [[T2]]) nounwind
21+
// CHECK-NEXT: [[T4:%.*]] = bitcast i8* [[T3]] to [[ETY]]*
22+
// CHECK-NEXT: store [[ETY]]* [[T4]], [[ETY]]** [[E]]
23+
// CHECK-NEXT: [[T0:%.*]] = load [[ETY]]** [[E]]
24+
// CHECK-NEXT: [[T1:%.*]] = bitcast [[ETY]]* [[T0]] to i8*
25+
// CHECK-NEXT: call void @objc_release(i8* [[T1]]) nounwind
26+
// CHECK-NEXT: call void @objc_end_catch() nounwind
27+
28+
void test1_helper(void);
29+
void test1(void) {
30+
@try {
31+
test1_helper();
32+
} @catch (__weak Ety *e) {
33+
}
34+
}
35+
// CHECK: define void @_Z5test1v()
36+
// CHECK: [[E:%.*]] = alloca [[ETY:%.*]]*, align 8
37+
// CHECK-NEXT: invoke void @_Z12test1_helperv()
38+
// CHECK: [[T0:%.*]] = call i8* @objc_begin_catch(
39+
// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to [[ETY]]*
40+
// CHECK-NEXT: [[T2:%.*]] = bitcast [[ETY]]** [[E]] to i8**
41+
// CHECK-NEXT: [[T3:%.*]] = bitcast [[ETY]]* [[T1]] to i8*
42+
// CHECK-NEXT: call i8* @objc_initWeak(i8** [[T2]], i8* [[T3]]) nounwind
43+
// CHECK-NEXT: [[T0:%.*]] = bitcast [[ETY]]** [[E]] to i8**
44+
// CHECK-NEXT: call void @objc_destroyWeak(i8** [[T0]]) nounwind
45+
// CHECK-NEXT: call void @objc_end_catch() nounwind
46+
47+
void test2_helper(void);
48+
void test2(void) {
49+
try {
50+
test2_helper();
51+
} catch (Ety *e) {
52+
}
53+
}
54+
// CHECK: define void @_Z5test2v()
55+
// CHECK: [[E:%.*]] = alloca [[ETY:%.*]]*, align 8
56+
// CHECK-NEXT: invoke void @_Z12test2_helperv()
57+
// CHECK: [[T0:%.*]] = call i8* @__cxa_begin_catch(
58+
// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to [[ETY]]*
59+
// CHECK-NEXT: [[T2:%.*]] = bitcast [[ETY]]* [[T1]] to i8*
60+
// CHECK-NEXT: [[T3:%.*]] = call i8* @objc_retain(i8* [[T2]]) nounwind
61+
// CHECK-NEXT: [[T4:%.*]] = bitcast i8* [[T3]] to [[ETY]]*
62+
// CHECK-NEXT: store [[ETY]]* [[T4]], [[ETY]]** [[E]]
63+
// CHECK-NEXT: [[T0:%.*]] = load [[ETY]]** [[E]]
64+
// CHECK-NEXT: [[T1:%.*]] = bitcast [[ETY]]* [[T0]] to i8*
65+
// CHECK-NEXT: call void @objc_release(i8* [[T1]]) nounwind
66+
// CHECK-NEXT: call void @__cxa_end_catch() nounwind
67+
68+
void test3_helper(void);
69+
void test3(void) {
70+
try {
71+
test3_helper();
72+
} catch (Ety * __weak e) {
73+
}
74+
}
75+
// CHECK: define void @_Z5test3v()
76+
// CHECK: [[E:%.*]] = alloca [[ETY:%.*]]*, align 8
77+
// CHECK-NEXT: invoke void @_Z12test3_helperv()
78+
// CHECK: [[T0:%.*]] = call i8* @__cxa_begin_catch(
79+
// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to [[ETY]]*
80+
// CHECK-NEXT: [[T2:%.*]] = bitcast [[ETY]]** [[E]] to i8**
81+
// CHECK-NEXT: [[T3:%.*]] = bitcast [[ETY]]* [[T1]] to i8*
82+
// CHECK-NEXT: call i8* @objc_initWeak(i8** [[T2]], i8* [[T3]]) nounwind
83+
// CHECK-NEXT: [[T0:%.*]] = bitcast [[ETY]]** [[E]] to i8**
84+
// CHECK-NEXT: call void @objc_destroyWeak(i8** [[T0]]) nounwind
85+
// CHECK-NEXT: call void @__cxa_end_catch() nounwind

0 commit comments

Comments
 (0)