1410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall// RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - -fblocks | FileCheck %s
24de9fce48e42cc7ec1345c0fd21b3dbc5b9114c8Anders Carlssonvoid (^f)(void) = ^{};
3a17d7ccc2ed77e321855990e180f2a34ec304bfcAnders Carlsson
4a17d7ccc2ed77e321855990e180f2a34ec304bfcAnders Carlsson// rdar://6768379
5a17d7ccc2ed77e321855990e180f2a34ec304bfcAnders Carlssonint f0(int (^a0)()) {
6a17d7ccc2ed77e321855990e180f2a34ec304bfcAnders Carlsson  return a0(1, 2, 3);
7a17d7ccc2ed77e321855990e180f2a34ec304bfcAnders Carlsson}
80e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar
90e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar// Verify that attributes on blocks are set correctly.
100e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbartypedef struct s0 T;
110e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbarstruct s0 {
120e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  int a[64];
130e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar};
140e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar
15c1ea4b96adca4767991bb0a7b21052cef4db059cBill Wendling// CHECK: define internal void @__f2_block_invoke(%struct.s0* noalias sret {{%.*}}, i8* {{%.*}}, %struct.s0* byval align 4 {{.*}})
160e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbarstruct s0 f2(struct s0 a0) {
170e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  return ^(struct s0 a1){ return a1; }(a0);
180e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar}
190e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar
20f1c97eb52e55d2d1340a0345ed91e345fddcb65dChris Lattner// This should not crash: rdar://6808051
21f1c97eb52e55d2d1340a0345ed91e345fddcb65dChris Lattnervoid *P = ^{
22f1c97eb52e55d2d1340a0345ed91e345fddcb65dChris Lattner  void *Q = __func__;
23f1c97eb52e55d2d1340a0345ed91e345fddcb65dChris Lattner};
24f1c97eb52e55d2d1340a0345ed91e345fddcb65dChris Lattner
25dd2fb9c5ebc02f48a5b91a9c2a5f1e4562d02a0bMike Stumpvoid (^test1)(void) = ^(void) {
26dd2fb9c5ebc02f48a5b91a9c2a5f1e4562d02a0bMike Stump  __block int i;
27dd2fb9c5ebc02f48a5b91a9c2a5f1e4562d02a0bMike Stump  ^ { i = 1; }();
28dd2fb9c5ebc02f48a5b91a9c2a5f1e4562d02a0bMike Stump};
29f1c97eb52e55d2d1340a0345ed91e345fddcb65dChris Lattner
30c71a4915ca216847599d03cab4ed1c5086b0eb43John McCalltypedef double ftype(double);
31c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall// It's not clear that we *should* support this syntax, but until that decision
32c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall// is made, we should support it properly and not crash.
33c71a4915ca216847599d03cab4ed1c5086b0eb43John McCallftype ^test2 = ^ftype {
34c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall  return 0;
35c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall};
3646ec70e2e6541a067d33b2513505bec74582b53cJohn McCall
3746ec70e2e6541a067d33b2513505bec74582b53cJohn McCall// rdar://problem/8605032
3846ec70e2e6541a067d33b2513505bec74582b53cJohn McCallvoid f3_helper(void (^)(void));
3946ec70e2e6541a067d33b2513505bec74582b53cJohn McCallvoid f3() {
4046ec70e2e6541a067d33b2513505bec74582b53cJohn McCall  _Bool b = 0;
4146ec70e2e6541a067d33b2513505bec74582b53cJohn McCall  f3_helper(^{ if (b) {} });
4246ec70e2e6541a067d33b2513505bec74582b53cJohn McCall}
436ea4841da1390b4f76d066f25333f11f6d8c5f40John McCall
446ea4841da1390b4f76d066f25333f11f6d8c5f40John McCall// rdar://problem/11322251
456c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall// The bool can fill in between the header and the long long.
466c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall// Add the appropriate amount of padding between them.
476ea4841da1390b4f76d066f25333f11f6d8c5f40John McCallvoid f4_helper(long long (^)(void));
4893ab6bf534fb6c26563c00f28a8fc5581bb71dfdStephen Lin// CHECK-LABEL: define void @f4()
496ea4841da1390b4f76d066f25333f11f6d8c5f40John McCallvoid f4(void) {
506ea4841da1390b4f76d066f25333f11f6d8c5f40John McCall  _Bool b = 0;
516ea4841da1390b4f76d066f25333f11f6d8c5f40John McCall  long long ll = 0;
526c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall  // CHECK: alloca <{ i8*, i32, i32, i8*, {{%.*}}*, i8, [3 x i8], i64 }>, align 8
536ea4841da1390b4f76d066f25333f11f6d8c5f40John McCall  f4_helper(^{ if (b) return ll; return 0LL; });
546ea4841da1390b4f76d066f25333f11f6d8c5f40John McCall}
556c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall
566c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall// rdar://problem/11354538
576c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall// The alignment after rounding up to the align of F5 is actually
586c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall// greater than the required alignment.  Don't assert.
596c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCallstruct F5 {
606c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall  char buffer[32] __attribute((aligned));
616c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall};
626c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCallvoid f5_helper(void (^)(struct F5 *));
6393ab6bf534fb6c26563c00f28a8fc5581bb71dfdStephen Lin// CHECK-LABEL: define void @f5()
646c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCallvoid f5(void) {
656c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall  struct F5 value;
666c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall  // CHECK: alloca <{ i8*, i32, i32, i8*, {{%.*}}*, [12 x i8], [[F5:%.*]] }>, align 16
676c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall  f5_helper(^(struct F5 *slot) { *slot = value; });
686c803f7f46533c69e2f8a9a882af9ae3b7fffb6fJohn McCall}
697523606638128e19d6993c356851fd3e88078201Fariborz Jahanian
707523606638128e19d6993c356851fd3e88078201Fariborz Jahanian// rdar://14085217
717523606638128e19d6993c356851fd3e88078201Fariborz Jahanianvoid (^b)() = ^{};
727523606638128e19d6993c356851fd3e88078201Fariborz Jahanianint main() {
737523606638128e19d6993c356851fd3e88078201Fariborz Jahanian   (b?: ^{})();
747523606638128e19d6993c356851fd3e88078201Fariborz Jahanian}
757523606638128e19d6993c356851fd3e88078201Fariborz Jahanian// CHECK: [[ZERO:%.*]] = load void (...)** @b
767523606638128e19d6993c356851fd3e88078201Fariborz Jahanian// CHECK-NEXT: [[TB:%.*]] = icmp ne void (...)* [[ZERO]], null
777523606638128e19d6993c356851fd3e88078201Fariborz Jahanian// CHECK-NEXT: br i1 [[TB]], label [[CT:%.*]], label [[CF:%.*]]
787523606638128e19d6993c356851fd3e88078201Fariborz Jahanian// CHECK: [[ONE:%.*]] = bitcast void (...)* [[ZERO]] to void ()*
797523606638128e19d6993c356851fd3e88078201Fariborz Jahanian// CHECK-NEXT:   br label [[CE:%.*]]
807523606638128e19d6993c356851fd3e88078201Fariborz Jahanian
81