198 lines · plain
1// RUN: %clang_cc1 -verify -Wno-deprecated-declarations -Wno-objc-root-class %s2 3@interface Unrelated4@end5 6@interface NSObject7+ (id)new;8+ (id)alloc;9- (NSObject *)init;10 11- (id)retain; // expected-note{{instance method 'retain' is assumed to return an instance of its receiver type ('NSArray *')}}12- autorelease;13 14- (id)self;15 16- (id)copy;17- (id)mutableCopy;18 19// Do not infer when instance/class mismatches20- (id)newNotInferred;21- (id)alloc;22+ (id)initWithBlarg;23+ (id)self;24 25// Do not infer when the return types mismatch.26- (Unrelated *)initAsUnrelated;27@end28 29@interface NSString : NSObject30- (id)init;31- (id)initWithCString:(const char*)string;32@end33 34@interface NSArray : NSObject35- (unsigned)count;36@end37 38@interface NSBlah 39@end40 41@interface NSMutableArray : NSArray42@end43 44@interface NSBlah ()45+ (Unrelated *)newUnrelated;46@end47 48void test_inference(void) {49 // Inference based on method family50 __typeof__(([[NSString alloc] init])) *str = (NSString**)0;51 __typeof__(([[[[NSString new] self] retain] autorelease])) *str2 = (NSString **)0;52 __typeof__(([[NSString alloc] initWithCString:"blah"])) *str3 = (NSString**)0;53 54 // Not inferred55 __typeof__(([[NSString new] copy])) *id1 = (id*)0;56 57 // Not inferred due to instance/class mismatches58 __typeof__(([[NSString new] newNotInferred])) *id2 = (id*)0;59 __typeof__(([[NSString new] alloc])) *id3 = (id*)0;60 __typeof__(([NSString self])) *id4 = (id*)0;61 __typeof__(([NSString initWithBlarg])) *id5 = (id*)0;62 63 // Not inferred due to return type mismatch64 __typeof__(([[NSString alloc] initAsUnrelated])) *unrelated = (Unrelated**)0;65 __typeof__(([NSBlah newUnrelated])) *unrelated2 = (Unrelated**)0;66 67 68 NSArray *arr = [[NSMutableArray alloc] init];69 NSMutableArray *marr = [arr retain]; // expected-error{{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}70}71 72@implementation NSBlah73+ (Unrelated *)newUnrelated {74 return (Unrelated *)0;75}76@end77 78@implementation NSBlah (Cat)79+ (Unrelated *)newUnrelated2 {80 return (Unrelated *)0;81}82@end83 84@interface A85- (id)initBlah; // expected-note 2{{overridden method is part of the 'init' method family}}86@end87 88@interface B : A89- (Unrelated *)initBlah; // expected-warning{{method is expected to return an instance of its class type 'B', but is declared to return 'Unrelated *'}}90@end91 92@interface C : A93@end94 95@implementation C96- (Unrelated *)initBlah { // expected-warning{{method is expected to return an instance of its class type 'C', but is declared to return 'Unrelated *'}}97 return (Unrelated *)0;98}99@end100 101@interface D102+ (id)newBlarg; // expected-note{{overridden method is part of the 'new' method family}}103@end104 105@interface D ()106+ alloc; // expected-note{{overridden method is part of the 'alloc' method family}}107@end108 109@implementation D110+ (Unrelated *)newBlarg { // expected-warning{{method is expected to return an instance of its class type 'D', but is declared to return 'Unrelated *'}}111 return (Unrelated *)0;112}113 114+ (Unrelated *)alloc { // expected-warning{{method is expected to return an instance of its class type 'D', but is declared to return 'Unrelated *'}}115 return (Unrelated *)0;116}117@end118 119@protocol P1120- (id)initBlah; // expected-note{{overridden method is part of the 'init' method family}}121- (int)initBlarg;122@end123 124@protocol P2 <P1>125- (int)initBlah; // expected-warning{{protocol method is expected to return an instance of the implementing class, but is declared to return 'int'}}126- (int)initBlarg;127- (int)initBlech;128@end129 130@interface E131- init;132@end133 134@implementation E135- init {136 return self;137}138@end139 140@protocol P3141+ (NSString *)newString;142@end143 144@interface F<P3>145@end146 147@implementation F148+ (NSString *)newString { return @"blah"; }149@end150 151@interface G 152- (id)_ABC_init __attribute__((objc_method_family(init))); // expected-note {{method '_ABC_init' declared here}}153@end154 155@interface G (Additions)156- (id)_ABC_init2 __attribute__((objc_method_family(init)));157@end158 159@implementation G (Additions)160- (id)_ABC_init { // expected-warning {{category is implementing a method which will also be implemented by its primary class}}161 return 0;162}163- (id)_ABC_init2 {164 return 0;165}166- (id)_ABC_init3 {167 return 0;168}169@end170 171// PR12384172@interface Fail @end173@protocol X @end174@implementation Fail175- (id<X>) initWithX // expected-note {{compiler has implicitly changed method 'initWithX' return type}}176{177 return (id)self; // expected-warning {{casting 'Fail *' to incompatible type 'id<X>'}}178}179@end180 181@interface WeirdNSString : NSString182- (id)initWithCString:(const char*)string, void *blah;183@end184 185@protocol PMFilterManager186@end187 188@interface UIViewController : NSObject189@end190 191@implementation UIViewController192+ (UIViewController<PMFilterManager> *)newFilterViewControllerForType // expected-note {{compiler has implicitly changed method 'newFilterViewControllerForType' return type}}193{194 UIViewController<PMFilterManager> *filterVC;195 return filterVC; // expected-error {{incompatible pointer types casting 'UIViewController *' to type 'UIViewController<PMFilterManager> *'}}196}197@end198