Home | History | Annotate | Download | only in SemaObjC
      1 // RUN: %clang_cc1 -fsyntax-only -fobjc-arc -fobjc-runtime-has-weak -verify -fblocks %s
      2 
      3 typedef const void * CFTypeRef;
      4 CFTypeRef CFBridgingRetain(id X);
      5 id CFBridgingRelease(CFTypeRef);
      6 
      7 void * cvt(id arg)
      8 {
      9   void* voidp_val;
     10   (void)(int*)arg; // expected-error {{cast of an Objective-C pointer to 'int *' is disallowed with ARC}}
     11   (void)(id)arg;
     12   (void)(__autoreleasing id*)arg; // expected-error {{cast of an Objective-C pointer to '__autoreleasing id *' is disallowed with ARC}}
     13   (void)(id*)arg; // expected-error {{cast of an Objective-C pointer to '__strong id *' is disallowed with ARC}}
     14 
     15   (void)(__autoreleasing id**)voidp_val;
     16   (void)(void*)voidp_val;
     17   (void)(void**)arg; // expected-error {{cast of an Objective-C pointer to 'void **' is disallowed with ARC}}
     18   cvt((void*)arg); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'void *' requires a bridged cast}} \
     19                    // expected-error {{implicit conversion of C pointer type 'void *' to Objective-C pointer type 'id' requires a bridged cast}} \
     20                    // expected-note 2 {{use __bridge to convert directly (no change in ownership)}} \
     21                    // expected-note {{use CFBridgingRetain call to make an ARC object available as a +1 'void *'}} \
     22                    // expected-note {{use CFBridgingRelease call to transfer ownership of a +1 'void *' into ARC}}
     23   cvt(0);
     24   (void)(__strong id**)(0);
     25   return arg; // expected-error {{implicit conversion of Objective-C pointer type 'id' to C pointer type 'void *' requires a bridged cast}} \
     26                    // expected-note {{use __bridge to convert directly (no change in ownership)}} \
     27                    // expected-note {{use CFBridgingRetain call to make an ARC object available as a +1 'void *'}}
     28 }
     29 
     30 void to_void(__strong id *sip, __weak id *wip,
     31              __autoreleasing id *aip,
     32              __unsafe_unretained id *uip) {
     33   void *vp1 = sip;
     34   void *vp2 = wip;
     35   void *vp3 = aip;
     36   void *vp4 = uip;
     37   (void)(void*)sip;
     38   (void)(void*)wip;
     39   (void)(void*)aip;
     40   (void)(void*)uip;
     41   (void)(void*)&sip;
     42   (void)(void*)&wip;
     43   (void)(void*)&aip;
     44   (void)(void*)&uip;
     45 }
     46 
     47 void from_void(void *vp) {
     48   __strong id *sip = (__strong id *)vp;
     49   __weak id *wip = (__weak id *)vp;
     50   __autoreleasing id *aip = (__autoreleasing id *)vp;
     51   __unsafe_unretained id *uip = (__unsafe_unretained id *)vp;
     52 
     53   __strong id **sipp = (__strong id **)vp;
     54   __weak id **wipp = (__weak id **)vp;
     55   __autoreleasing id **aipp = (__autoreleasing id **)vp;
     56   __unsafe_unretained id **uipp = (__unsafe_unretained id **)vp;
     57 
     58   sip = vp; // expected-error{{implicit conversion of a non-Objective-C pointer type 'void *' to '__strong id *' is disallowed with ARC}}
     59   wip = vp; // expected-error{{implicit conversion of a non-Objective-C pointer type 'void *' to '__weak id *' is disallowed with ARC}}
     60   aip = vp; // expected-error{{implicit conversion of a non-Objective-C pointer type 'void *' to '__autoreleasing id *' is disallowed with ARC}}
     61   uip = vp; // expected-error{{implicit conversion of a non-Objective-C pointer type 'void *' to '__unsafe_unretained id *' is disallowed with ARC}}
     62 }
     63 
     64 typedef void (^Block)();
     65 typedef void (^Block_strong)() __strong;
     66 typedef void (^Block_autoreleasing)() __autoreleasing;
     67 
     68 @class NSString;
     69 
     70 void ownership_transfer_in_cast(void *vp, Block *pblk) {
     71   __strong NSString **sip = (NSString**)(__strong id *)vp;
     72   __weak NSString **wip = (NSString**)(__weak id *)vp;
     73   __autoreleasing id *aip = (id*)(__autoreleasing id *)vp;
     74   __unsafe_unretained id *uip = (id*)(__unsafe_unretained id *)vp;
     75 
     76   __strong id **sipp = (id**)(__strong id **)vp;
     77   __weak id **wipp = (id**)(__weak id **)vp;
     78   __autoreleasing id **aipp = (id**)(__autoreleasing id **)vp;
     79   __unsafe_unretained id **uipp = (id**)(__unsafe_unretained id **)vp;
     80 
     81   Block_strong blk_strong1;
     82   Block_strong blk_strong2 = (Block)blk_strong1;
     83   Block_autoreleasing *blk_auto = (Block*)pblk;
     84 
     85   id lv;
     86   (void)(id)&lv; // expected-error {{cast of an indirect pointer to an Objective-C pointer to 'id'}}
     87   (void)(id*)lv; // expected-error {{cast of an Objective-C pointer to '__strong id *'}}
     88   (void)(NSString*)&lv; // expected-error {{cast of an indirect pointer to an Objective-C pointer to 'NSString *'}}
     89   (void)(NSString**)lv; // expected-error {{cast of an Objective-C pointer to 'NSString *__strong *'}}
     90   (void)(Block)&lv; // expected-error {{cast of an indirect pointer to an Objective-C pointer to 'Block'}}
     91   (void)(Block*)lv; // expected-error {{cast of an Objective-C pointer to '__strong Block *'}}
     92 }
     93 
     94 // <rdar://problem/10486347>
     95 void conversion_in_conditional(id a, void* b) {
     96   id c = 1 ? a : b; // expected-error {{operands to conditional of types 'id' and 'void *' are incompatible in ARC mode}}
     97   id d = 1 ? b : a; // expected-error {{operands to conditional of types 'void *' and 'id' are incompatible in ARC mode}}
     98 }
     99