Dominic Chen | 184c624 | 2017-03-03 18:02:02 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.osx.cocoa.DirectIvarAssignment -verify -fblocks %s |
Anna Zaks | f4fd145 | 2012-10-15 22:48:14 +0000 | [diff] [blame] | 2 | |
| 3 | typedef signed char BOOL; |
| 4 | @protocol NSObject - (BOOL)isEqual:(id)object; @end |
| 5 | @interface NSObject <NSObject> {} |
| 6 | +(id)alloc; |
| 7 | -(id)init; |
| 8 | -(id)autorelease; |
| 9 | -(id)copy; |
| 10 | -(id)retain; |
| 11 | @end |
Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 12 | |
| 13 | @interface MyClass; |
| 14 | @end |
Anna Zaks | f4fd145 | 2012-10-15 22:48:14 +0000 | [diff] [blame] | 15 | @interface TestProperty :NSObject { |
Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 16 | MyClass *_Z; |
| 17 | id _nonSynth; |
| 18 | } |
| 19 | |
| 20 | @property (assign, nonatomic) MyClass* A; // explicitely synthesized, not implemented, non-default ivar name |
| 21 | |
| 22 | @property (assign) MyClass* X; // automatically synthesized, not implemented |
| 23 | |
| 24 | @property (assign, nonatomic) MyClass* Y; // automatically synthesized, implemented |
| 25 | |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 26 | @property (assign, nonatomic) MyClass* Z; // non-synthesized ivar, implemented setter |
| 27 | @property (readonly) id nonSynth; // non-synthesized, explicitly implemented to return ivar with expected name |
Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 28 | |
| 29 | - (id) initWithPtr:(MyClass*) value; |
| 30 | - (id) myInitWithPtr:(MyClass*) value; |
| 31 | - (void) someMethod: (MyClass*)In; |
| 32 | @end |
| 33 | |
| 34 | @implementation TestProperty |
| 35 | @synthesize A = __A; |
| 36 | |
| 37 | - (id) initWithPtr: (MyClass*) value { |
| 38 | _Y = value; // no-warning |
| 39 | return self; |
| 40 | } |
| 41 | |
Anna Zaks | f4fd145 | 2012-10-15 22:48:14 +0000 | [diff] [blame] | 42 | - (id) copyWithPtrY: (TestProperty*) value { |
| 43 | TestProperty *another = [[TestProperty alloc] init]; |
| 44 | another->_Y = value->_Y; // no-warning |
| 45 | return another; |
| 46 | } |
| 47 | |
Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 48 | - (id) myInitWithPtr: (MyClass*) value { |
| 49 | _Y = value; // no-warning |
| 50 | return self; |
| 51 | } |
| 52 | |
| 53 | - (void) setY:(MyClass*) NewValue { |
| 54 | _Y = NewValue; // no-warning |
| 55 | } |
| 56 | |
| 57 | - (void) setZ:(MyClass*) NewValue { |
| 58 | _Z = NewValue; // no-warning |
| 59 | } |
| 60 | |
| 61 | - (id)nonSynth { |
| 62 | return _nonSynth; |
| 63 | } |
| 64 | |
| 65 | - (void) someMethod: (MyClass*)In { |
Anna Zaks | 6aef455 | 2012-09-27 21:57:17 +0000 | [diff] [blame] | 66 | (__A) = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}} |
Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 67 | _X = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}} |
| 68 | _Y = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}} |
| 69 | _Z = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}} |
| 70 | _nonSynth = 0; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}} |
| 71 | } |
| 72 | @end |