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