1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - -fblocks | FileCheck %s
2*f4a2713aSLionel Sambuc void (^f)(void) = ^{};
3*f4a2713aSLionel Sambuc
4*f4a2713aSLionel Sambuc // rdar://6768379
5*f4a2713aSLionel Sambuc int f0(int (^a0)()) {
6*f4a2713aSLionel Sambuc return a0(1, 2, 3);
7*f4a2713aSLionel Sambuc }
8*f4a2713aSLionel Sambuc
9*f4a2713aSLionel Sambuc // Verify that attributes on blocks are set correctly.
10*f4a2713aSLionel Sambuc typedef struct s0 T;
11*f4a2713aSLionel Sambuc struct s0 {
12*f4a2713aSLionel Sambuc int a[64];
13*f4a2713aSLionel Sambuc };
14*f4a2713aSLionel Sambuc
15*f4a2713aSLionel Sambuc // CHECK: define internal void @__f2_block_invoke(%struct.s0* noalias sret {{%.*}}, i8* {{%.*}}, %struct.s0* byval align 4 {{.*}})
f2(struct s0 a0)16*f4a2713aSLionel Sambuc struct s0 f2(struct s0 a0) {
17*f4a2713aSLionel Sambuc return ^(struct s0 a1){ return a1; }(a0);
18*f4a2713aSLionel Sambuc }
19*f4a2713aSLionel Sambuc
20*f4a2713aSLionel Sambuc // This should not crash: rdar://6808051
21*f4a2713aSLionel Sambuc void *P = ^{
22*f4a2713aSLionel Sambuc void *Q = __func__;
23*f4a2713aSLionel Sambuc };
24*f4a2713aSLionel Sambuc
25*f4a2713aSLionel Sambuc void (^test1)(void) = ^(void) {
26*f4a2713aSLionel Sambuc __block int i;
27*f4a2713aSLionel Sambuc ^ { i = 1; }();
28*f4a2713aSLionel Sambuc };
29*f4a2713aSLionel Sambuc
30*f4a2713aSLionel Sambuc typedef double ftype(double);
31*f4a2713aSLionel Sambuc // It's not clear that we *should* support this syntax, but until that decision
32*f4a2713aSLionel Sambuc // is made, we should support it properly and not crash.
33*f4a2713aSLionel Sambuc ftype ^test2 = ^ftype {
34*f4a2713aSLionel Sambuc return 0;
35*f4a2713aSLionel Sambuc };
36*f4a2713aSLionel Sambuc
37*f4a2713aSLionel Sambuc // rdar://problem/8605032
38*f4a2713aSLionel Sambuc void f3_helper(void (^)(void));
f3()39*f4a2713aSLionel Sambuc void f3() {
40*f4a2713aSLionel Sambuc _Bool b = 0;
41*f4a2713aSLionel Sambuc f3_helper(^{ if (b) {} });
42*f4a2713aSLionel Sambuc }
43*f4a2713aSLionel Sambuc
44*f4a2713aSLionel Sambuc // rdar://problem/11322251
45*f4a2713aSLionel Sambuc // The bool can fill in between the header and the long long.
46*f4a2713aSLionel Sambuc // Add the appropriate amount of padding between them.
47*f4a2713aSLionel Sambuc void f4_helper(long long (^)(void));
48*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @f4()
f4(void)49*f4a2713aSLionel Sambuc void f4(void) {
50*f4a2713aSLionel Sambuc _Bool b = 0;
51*f4a2713aSLionel Sambuc long long ll = 0;
52*f4a2713aSLionel Sambuc // CHECK: alloca <{ i8*, i32, i32, i8*, {{%.*}}*, i8, [3 x i8], i64 }>, align 8
53*f4a2713aSLionel Sambuc f4_helper(^{ if (b) return ll; return 0LL; });
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc
56*f4a2713aSLionel Sambuc // rdar://problem/11354538
57*f4a2713aSLionel Sambuc // The alignment after rounding up to the align of F5 is actually
58*f4a2713aSLionel Sambuc // greater than the required alignment. Don't assert.
59*f4a2713aSLionel Sambuc struct F5 {
60*f4a2713aSLionel Sambuc char buffer[32] __attribute((aligned));
61*f4a2713aSLionel Sambuc };
62*f4a2713aSLionel Sambuc void f5_helper(void (^)(struct F5 *));
63*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @f5()
f5(void)64*f4a2713aSLionel Sambuc void f5(void) {
65*f4a2713aSLionel Sambuc struct F5 value;
66*f4a2713aSLionel Sambuc // CHECK: alloca <{ i8*, i32, i32, i8*, {{%.*}}*, [12 x i8], [[F5:%.*]] }>, align 16
67*f4a2713aSLionel Sambuc f5_helper(^(struct F5 *slot) { *slot = value; });
68*f4a2713aSLionel Sambuc }
69*f4a2713aSLionel Sambuc
70*f4a2713aSLionel Sambuc // rdar://14085217
71*f4a2713aSLionel Sambuc void (^b)() = ^{};
main()72*f4a2713aSLionel Sambuc int main() {
73*f4a2713aSLionel Sambuc (b?: ^{})();
74*f4a2713aSLionel Sambuc }
75*f4a2713aSLionel Sambuc // CHECK: [[ZERO:%.*]] = load void (...)** @b
76*f4a2713aSLionel Sambuc // CHECK-NEXT: [[TB:%.*]] = icmp ne void (...)* [[ZERO]], null
77*f4a2713aSLionel Sambuc // CHECK-NEXT: br i1 [[TB]], label [[CT:%.*]], label [[CF:%.*]]
78*f4a2713aSLionel Sambuc // CHECK: [[ONE:%.*]] = bitcast void (...)* [[ZERO]] to void ()*
79*f4a2713aSLionel Sambuc // CHECK-NEXT: br label [[CE:%.*]]
80*f4a2713aSLionel Sambuc
81