Clang Project

clang_source_code/test/CodeGenObjC/convert-messages-to-runtime-calls.m
1// RUN: %clang_cc1 -fobjc-runtime=macosx-10.10.0 -emit-llvm -o - %s -fno-objc-convert-messages-to-runtime-calls -fobjc-exceptions -fexceptions | FileCheck %s --check-prefix=MSGS
2// RUN: %clang_cc1 -fobjc-runtime=macosx-10.10.0 -emit-llvm -o - %s -fobjc-exceptions -fexceptions | FileCheck %s --check-prefix=CALLS
3// RUN: %clang_cc1 -fobjc-runtime=macosx-10.9.0 -emit-llvm -o - %s -fobjc-exceptions -fexceptions | FileCheck %s --check-prefix=MSGS
4// RUN: %clang_cc1 -fobjc-runtime=macosx-fragile-10.10.0 -emit-llvm -o - %s -fobjc-exceptions -fexceptions | FileCheck %s --check-prefix=MSGS
5// RUN: %clang_cc1 -fobjc-runtime=ios-8.0 -emit-llvm -o - %s -fobjc-exceptions -fexceptions | FileCheck %s --check-prefix=CALLS
6// RUN: %clang_cc1 -fobjc-runtime=ios-7.0 -emit-llvm -o - %s -fobjc-exceptions -fexceptions | FileCheck %s --check-prefix=MSGS
7// Note: This line below is for tvos for which the driver passes through to use the ios9.0 runtime.
8// RUN: %clang_cc1 -fobjc-runtime=ios-9.0 -emit-llvm -o - %s -fobjc-exceptions -fexceptions | FileCheck %s --check-prefix=CALLS
9// RUN: %clang_cc1 -fobjc-runtime=watchos-2.0 -emit-llvm -o - %s -fobjc-exceptions -fexceptions | FileCheck %s --check-prefix=CALLS
10
11#define nil (id)0
12
13@interface NSObject
14+ (id)alloc;
15+ (id)allocWithZone:(void*)zone;
16+ (id)alloc2;
17- (id)retain;
18- (void)release;
19- (id)autorelease;
20@end
21
22// CHECK-LABEL: define {{.*}}void @test1
23void test1(id x) {
24  // MSGS: {{call.*@objc_msgSend}}
25  // MSGS: {{call.*@objc_msgSend}}
26  // MSGS: {{call.*@objc_msgSend}}
27  // MSGS: {{call.*@objc_msgSend}}
28  // MSGS: {{call.*@objc_msgSend}}
29  // CALLS: {{call.*@objc_alloc}}
30  // CALLS: {{call.*@objc_allocWithZone}}
31  // CALLS: {{call.*@objc_retain}}
32  // CALLS: {{call.*@objc_release}}
33  // CALLS: {{call.*@objc_autorelease}}
34  [NSObject alloc];
35  [NSObject allocWithZone:nil];
36  [x retain];
37  [x release];
38  [x autorelease];
39}
40
41// CHECK-LABEL: define {{.*}}void @check_invoke
42void check_invoke() {
43  // MSGS: {{invoke.*@objc_msgSend}}
44  // MSGS: {{invoke.*@objc_msgSend}}
45  // CALLS: {{invoke.*@objc_alloc}}
46  // CALLS: {{invoke.*@objc_allocWithZone}}
47  @try {
48    [NSObject alloc];
49    [NSObject allocWithZone:nil];
50  } @catch (...) {
51  }
52}
53
54// CHECK-LABEL: define {{.*}}void @test2
55void test2(void* x) {
56  // MSGS: {{call.*@objc_msgSend}}
57  // MSGS: {{call.*@objc_msgSend}}
58  // MSGS: {{call.*@objc_msgSend}}
59  // CALLS: {{call.*@objc_msgSend}}
60  // CALLS: {{call.*@objc_msgSend}}
61  // CALLS: {{call.*@objc_msgSend}}
62  [NSObject alloc2];
63  [NSObject allocWithZone:(void*)-1];
64  [NSObject allocWithZone:x];
65}
66
67@class A;
68@interface B
69+ (A*) alloc;
70+ (A*) allocWithZone:(void*)zone;
71- (A*) alloc;
72- (A*) allocWithZone:(void*)zone;
73- (A*) retain;
74- (A*) autorelease;
75@end
76
77// Make sure we get a bitcast on the return type as the
78// call will return i8* which we have to cast to A*
79// CHECK-LABEL: define {{.*}}void @test_alloc_class_ptr
80A* test_alloc_class_ptr() {
81  // CALLS: {{call.*@objc_alloc}}
82  // CALLS-NEXT: bitcast i8*
83  // CALLS-NEXT: ret
84  return [B alloc];
85}
86
87// Make sure we get a bitcast on the return type as the
88// call will return i8* which we have to cast to A*
89// CHECK-LABEL: define {{.*}}void @test_alloc_class_ptr
90A* test_allocWithZone_class_ptr() {
91  // CALLS: {{call.*@objc_allocWithZone}}
92  // CALLS-NEXT: bitcast i8*
93  // CALLS-NEXT: ret
94  return [B allocWithZone:nil];
95}
96
97// Only call objc_alloc on a Class, not an instance
98// CHECK-LABEL: define {{.*}}void @test_alloc_instance
99void test_alloc_instance(A *a) {
100  // CALLS: {{call.*@objc_alloc}}
101  // CALLS: {{call.*@objc_allocWithZone}}
102  // CALLS: {{call.*@objc_msgSend}}
103  // CALLS: {{call.*@objc_msgSend}}
104  [A alloc];
105  [A allocWithZone:nil];
106  [a alloc];
107  [a allocWithZone:nil];
108}
109
110// Make sure we get a bitcast on the return type as the
111// call will return i8* which we have to cast to A*
112// CHECK-LABEL: define {{.*}}void @test_retain_class_ptr
113A* test_retain_class_ptr(B *b) {
114  // CALLS: {{call.*@objc_retain}}
115  // CALLS-NEXT: bitcast i8*
116  // CALLS-NEXT: ret
117  return [b retain];
118}
119
120// Make sure we get a bitcast on the return type as the
121// call will return i8* which we have to cast to A*
122// CHECK-LABEL: define {{.*}}void @test_autorelease_class_ptr
123A* test_autorelease_class_ptr(B *b) {
124  // CALLS: {{call.*@objc_autorelease}}
125  // CALLS-NEXT: bitcast i8*
126  // CALLS-NEXT: ret
127  return [b autorelease];
128}
129
130
131@interface C
132+ (id)allocWithZone:(int)intArg;
133- (float) retain;
134@end
135
136// Make sure we only accept pointer types
137// CHECK-LABEL: define {{.*}}void @test_allocWithZone_int
138C* test_allocWithZone_int() {
139  // MSGS: {{call.*@objc_msgSend}}
140  // CALLS: {{call.*@objc_msgSend}}
141  return [C allocWithZone:3];
142}
143
144// Make sure we use a message and not a call as the return type is
145// not a pointer type.
146// CHECK-LABEL: define {{.*}}void @test_cannot_message_return_float
147float test_cannot_message_return_float(C *c) {
148  // MSGS: {{call.*@objc_msgSend}}
149  // CALLS: {{call.*@objc_msgSend}}
150  return [c retain];
151}
152
153@interface NSString : NSObject
154+ (void)retain_self;
155- (void)retain_super;
156@end
157
158@implementation NSString
159
160// Make sure we can convert a message to a dynamic receiver to a call
161// CHECK-LABEL: define {{.*}}void @retain_self
162+ (void)retain_self {
163  // MSGS: {{call.*@objc_msgSend}}
164  // CALLS: {{call.*@objc_retain}}
165  [self retain];
166}
167
168// Make sure we never convert a message to super to a call
169// CHECK-LABEL: define {{.*}}void @retain_super
170- (void)retain_super {
171  // MSGS: {{call.*@objc_msgSend}}
172  // CALLS: {{call.*@objc_msgSend}}
173  [super retain];
174}
175
176@end
177
178