objc-properties.m revision 88a83e3f3bade5497ff371ed5a570b83d9373e3a
1// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.osx.cocoa.DirectIvarAssignment -fobjc-default-synthesize-properties -Wno-objc-root-class -verify -fblocks %s
2
3@interface MyClass;
4@end
5@interface TestProperty {
6  MyClass *_Z;
7  id _nonSynth;
8}
9
10  @property (assign, nonatomic) MyClass* A; // explicitely synthesized, not implemented, non-default ivar name
11
12  @property (assign) MyClass* X;  // automatically synthesized, not implemented
13
14  @property (assign, nonatomic) MyClass* Y; // automatically synthesized, implemented
15
16  @property (assign, nonatomic) MyClass* Z; // non synthesized, implemented
17  @property (readonly) id nonSynth;  // non synthesized, explicitly implemented to return ivar with expected name
18  
19  - (id) initWithPtr:(MyClass*) value;
20  - (id) myInitWithPtr:(MyClass*) value;
21  - (void) someMethod: (MyClass*)In;
22@end
23
24@implementation TestProperty
25  @synthesize A = __A;
26  
27  - (id) initWithPtr: (MyClass*) value {
28    _Y = value; // no-warning
29    return self;
30  }
31
32  - (id) myInitWithPtr: (MyClass*) value {
33    _Y = value; // no-warning
34    return self;
35  }
36  
37  - (void) setY:(MyClass*) NewValue {
38    _Y = NewValue; // no-warning
39  }
40
41  - (void) setZ:(MyClass*) NewValue {
42    _Z = NewValue; // no-warning
43  }
44
45  - (id)nonSynth {
46      return _nonSynth;
47  }
48
49  - (void) someMethod: (MyClass*)In {
50    __A = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
51    _X = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
52    _Y = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
53    _Z = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
54    _nonSynth = 0; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
55  }
56@end