direct-ivar-assignment-in-annotated-functions.m revision d7b1d2467d8bf01be5068dbbad1a6324cee8bf4a
1// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.osx.cocoa.DirectIvarAssignmentForAnnotatedFunctions -fobjc-default-synthesize-properties -verify -fblocks %s
2
3typedef 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
12
13@interface MyClass;
14@end
15
16@interface AnnotatedClass : NSObject {
17}
18  - (void) someMethod: (MyClass*)In __attribute__((annotate("objc_no_direct_instance_variable_assignment")));
19  - (void) someMethodNotAnnaotated: (MyClass*)In;
20@end
21
22
23@interface TestProperty : AnnotatedClass {
24  MyClass *_Z;
25  id _nonSynth;
26}
27
28  @property (assign, nonatomic) MyClass* A; // explicitely synthesized, not implemented, non-default ivar name
29
30  @property (assign) MyClass* X;  // automatically synthesized, not implemented
31
32  @property (assign, nonatomic) MyClass* Y; // automatically synthesized, implemented
33
34  @property (assign, nonatomic) MyClass* Z; // non synthesized ivar, implemented setter
35  @property (readonly) id nonSynth;  // non synthesized, explicitly implemented to return ivar with expected name
36
37  @property (assign) MyClass* NotX __attribute__((annotate("objc_allow_direct_instance_variable_assignment")));  // warnings should be suppressed
38
39  @end
40
41@implementation TestProperty
42  @synthesize A = __A;
43  
44  - (void) someMethod: (MyClass*)In {
45    (__A) = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
46    _X = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
47    _Y = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
48    _Z = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
49    _nonSynth = 0; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
50    _NotX = 0; // no-warning
51  }
52  - (void) someMethodNotAnnaotated: (MyClass*)In {
53    (__A) = In; 
54    _X = In; // no-warning
55    _Y = In; // no-warning
56    _Z = In; // no-warning
57    _nonSynth = 0; // no-warning
58  }
59
60@end