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