1 // RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-store=region -verify %s
3 typedef signed char BOOL;
4 typedef unsigned int NSUInteger;
5 typedef struct _NSZone NSZone;
6 @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
7 @protocol NSObject - (BOOL)isEqual:(id)object; @end
8 @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
9 @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end
10 @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
11 @interface NSObject <NSObject> {}
18 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
20 -(id)initWithFormat:(NSString *)f,...;
21 -(BOOL)isEqualToString:(NSString *)s;
24 @interface NSNumber : NSObject {}
26 -(id)initWithInteger:(int)i;
31 @interface Test1 : NSObject {
35 @property (nonatomic, assign) NSString *text;
44 Test1 *cell = [[[Test1 alloc] init] autorelease];
46 NSString *string1 = [[NSString alloc] initWithFormat:@"test %f", 0.0]; // expected-warning {{Potential leak}}
57 @interface MyNumber : NSObject
62 - (id)initWithNumber:(NSNumber *)number;
64 @property (nonatomic, readonly) NSNumber* myNumber;
65 @property (nonatomic, readonly) NSNumber* newMyNumber;
69 @implementation MyNumber
70 @synthesize myNumber=_myNumber;
72 - (id)initWithNumber:(NSNumber *)number
78 _myNumber = [number copy];
84 - (NSNumber*)newMyNumber
87 return [_myNumber retain];
89 return [[NSNumber alloc] initWithInteger:1];
92 - (id)valueForUndefinedKey:(NSString*)key
96 if ([key isEqualToString:@"MyIvarNumberAsPropertyOverReleased"])
97 value = self.myNumber; // _myNumber will be over released, since the value returned from self.myNumber is not retained.
98 else if ([key isEqualToString:@"MyIvarNumberAsPropertyOk"])
99 value = [self.myNumber retain]; // this line fixes the over release
100 else if ([key isEqualToString:@"MyIvarNumberAsNewMyNumber"])
101 value = self.newMyNumber; // this one is ok, since value is returned retained
103 value = [[NSNumber alloc] initWithInteger:0];
105 return [value autorelease]; // expected-warning {{Object sent -autorelease too many times}}
110 NSNumber* numberFromMyNumberProperty(MyNumber* aMyNumber)
112 NSNumber* result = aMyNumber.myNumber;
114 return [result autorelease]; // expected-warning {{Object sent -autorelease too many times}}
120 @interface Person : NSObject {
123 @property (retain) NSString * name;
126 @implementation Person
127 @synthesize name = _name;
131 Person *p = [[[Person alloc] init] autorelease];
133 p.name = [[NSString string] retain]; // expected-warning {{leak}}
134 p.name = [[NSString alloc] init]; // expected-warning {{leak}}
137 @interface SubPerson : Person
141 @implementation SubPerson