1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,alpha.core.StackAddressAsyncEscape -fblocks -verify %s
3 typedef struct dispatch_queue_s *dispatch_queue_t;
4 typedef void (^dispatch_block_t)(void);
5 void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
6 extern dispatch_queue_t queue;
9 void test_block_inside_block_async_no_leak() {
12 void (^inner)(void) = ^void(void) {
16 // Block_copy(...) copies the captured block ("inner") too,
17 // there is no leak in this case.
18 dispatch_async(queue, ^void(void) {
25 dispatch_block_t test_block_inside_block_async_leak() {
27 void (^inner)(void) = ^void(void) {
31 void (^outer)(void) = ^void(void) {
36 return outer; // expected-warning-re{{Address of stack-allocated block declared on line {{.+}} is captured by a returned block}}
39 // The block literal defined in this function could leak once being
41 void output_block(dispatch_block_t * blk) {
43 *blk = ^{ f(x); }; // expected-warning {{Address of stack-allocated block declared on line 43 is still referred to by the stack variable 'blk' upon returning to the caller. This will be a dangling reference [core.StackAddressEscape]}}
46 // The block literal captures nothing thus is treated as a constant.
47 void output_constant_block(dispatch_block_t * blk) {
51 // A block can leak if it captures at least one variable and is not
52 // under ARC when its' stack frame expires.
53 void test_block_leak() {
54 __block dispatch_block_t blk;
56 dispatch_block_t p = ^{
57 blk = ^{ // expected-warning {{Address of stack-allocated block declared on line 57 is still referred to by the stack variable 'blk' upon returning to the caller. This will be a dangling reference [core.StackAddressEscape]}}
68 // A block captures nothing is a constant thus never leaks.
69 void test_constant_block_no_leak() {
70 __block dispatch_block_t blk;
71 dispatch_block_t p = ^{
79 output_constant_block(&blk);