xref: /llvm-project/clang/test/CodeGenObjCXX/blocks.mm (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1// RUN: %clang_cc1 -x objective-c++ -fblocks -triple x86_64-apple-darwin -fobjc-runtime=macosx-fragile-10.5 %s -verify -std=c++11 -emit-llvm -o %t
2
3@interface A
4@end
5
6@interface B : A
7@end
8
9void f(int (^bl)(B* b));
10void takeBlock(void (^block)());
11void useValues(...);
12
13// Test1
14void g() {
15  f(^(A* a) { return 0; });
16}
17
18// Test2
19void g1() {
20  int (^bl)(B* b) = ^(A* a) { return 0; };
21}
22
23// Test3
24@protocol NSObject;
25
26void bar(id(^)(void));
27
28void foo(id <NSObject>(^objectCreationBlock)(void)) {
29    return bar(objectCreationBlock);
30}
31
32// Test4
33struct S {
34  S *(^a)() = ^{
35    return this;
36  };
37};
38S s;
39
40// Test5
41struct X {
42  void f() {
43    ^ {
44      struct Nested { Nested *ptr = this; };
45    } ();
46  };
47};
48
49// Regression test for PR13314
50class FooClass { };
51void fun() {
52  FooClass foovar;
53  ^() {  // expected-warning {{expression result unused}}
54    return foovar;
55  };
56}
57void gun() {
58  FooClass foovar;
59  [=]() {  // expected-warning {{expression result unused}}
60    return foovar;
61  };
62}
63
64// PR24780
65class CaptureThisAndAnotherPointer {
66  void test(void *ptr) {
67    takeBlock(^{ useValues(ptr, this); });
68  }
69};
70
71// Check that we don't crash when using BLOCK_LAYOUT_STRONG.
72#pragma clang assume_nonnull begin
73@interface NSUUID @end
74#pragma clang assume_nonnull end
75
76struct Wrapper1 { NSUUID *Ref; };
77struct Wrapper2 { Wrapper1 W1; };
78
79@implementation B
80- (void) captureStrongRef {
81  __block Wrapper2 W2;
82}
83@end
84