1// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -fblocks -verify -Wno-objc-root-class %s
2// rdar://9535237
3
4typedef struct dispatch_queue_s *dispatch_queue_t;
5
6typedef void (^dispatch_block_t)(void);
7
8void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
9
10extern __attribute__((visibility("default"))) struct dispatch_queue_s _dispatch_main_q;
11
12@interface SwitchBlockCrashAppDelegate
13- (void)pageLeft;
14- (void)pageRight;;
15@end
16
17@implementation SwitchBlockCrashAppDelegate
18
19- (void)choose:(int)button {
20    switch (button) {
21    case 0:
22        dispatch_async((&_dispatch_main_q), ^{ [self pageLeft]; }); // expected-note 3 {{jump enters lifetime of block which strongly captures a variable}}
23        break;
24    case 2:  // expected-error {{cannot jump}}
25        dispatch_async((&_dispatch_main_q), ^{ [self pageRight]; }); // expected-note 2 {{jump enters lifetime of block which strongly captures a variable}}
26        break;
27    case 3: // expected-error {{cannot jump}}
28        {
29          dispatch_async((&_dispatch_main_q), ^{ [self pageRight]; });
30          break;
31        }
32    case 4: // expected-error {{cannot jump}}
33        break;
34    }
35
36    __block SwitchBlockCrashAppDelegate *captured_block_obj;
37    switch (button) {
38    case 10:
39      {
40        dispatch_async((&_dispatch_main_q), ^{ [self pageLeft]; });
41        break;
42      }
43    case 12:
44        if (button)
45          dispatch_async((&_dispatch_main_q), ^{ [captured_block_obj pageRight]; });
46        break;
47    case 13:
48        while (button)
49          dispatch_async((&_dispatch_main_q), ^{ [self pageRight]; });
50        break;
51    case 14:
52        break;
53    }
54
55    switch (button) {
56    case 10:
57      {
58        dispatch_async((&_dispatch_main_q), ^{ [self pageLeft]; });
59        break;
60      }
61    case 12:
62        if (button)
63          dispatch_async((&_dispatch_main_q), ^{ [self pageRight]; });
64        switch (button) {
65          case 0:
66            {
67              dispatch_async((&_dispatch_main_q), ^{ [self pageLeft]; });
68              break;
69            }
70         case 4:
71          break;
72        }
73        break;
74    case 13:
75        while (button)
76          dispatch_async((&_dispatch_main_q), ^{ [self pageRight]; });
77        break;
78    case 14:
79        break;
80    }
81}
82- (void)pageLeft {}
83- (void)pageRight {}
84@end
85
86// Test 2.  rdar://problem/11150919
87int test2(id obj, int state) { // expected-note {{jump enters lifetime of block}} FIXME: weird location
88  switch (state) {
89  case 0:
90    (void) ^{ (void) obj; };
91    return 0;
92
93  default: // expected-error {{cannot jump}}
94    return 1;
95  }
96}
97
98