blob: bedd1e7fc25a407bf4ab00b8e99bb914a84c0ed2 [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=osx.cocoa.Dealloc -fblocks -verify %s
2// RUN: %clang_analyze_cc1 -analyzer-checker=osx.cocoa.Dealloc -fblocks -verify -triple x86_64-apple-darwin10 -fobjc-arc %s
Devin Coughlin6d0c8a02016-03-01 00:39:04 +00003
4#define NON_ARC !__has_feature(objc_arc)
5
6// No diagnostics expected under ARC.
7#if !NON_ARC
8 // expected-no-diagnostics
9#endif
Devin Coughlin30751342016-01-27 01:41:58 +000010
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +000011typedef signed char BOOL;
Ted Kremenek02b63b42009-02-13 22:26:30 +000012@protocol NSObject
13- (BOOL)isEqual:(id)object;
14- (Class)class;
15@end
16
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +000017@interface NSObject <NSObject> {}
18- (void)dealloc;
Ted Kremenek1f9dd452008-12-08 22:01:50 +000019- (id)init;
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +000020@end
21
Ted Kremenek1f9dd452008-12-08 22:01:50 +000022typedef struct objc_selector *SEL;
23
Devin Coughlin30751342016-01-27 01:41:58 +000024//===------------------------------------------------------------------------===
25// Do not warn about missing -dealloc method. Not enough context to know
26// whether the ivar is retained or not.
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +000027
Devin Coughlin30751342016-01-27 01:41:58 +000028@interface MissingDeallocWithIvar : NSObject {
29 NSObject *_ivar;
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +000030}
31@end
Ted Kremenek1f9dd452008-12-08 22:01:50 +000032
Devin Coughlin30751342016-01-27 01:41:58 +000033@implementation MissingDeallocWithIvar
34@end
35
36//===------------------------------------------------------------------------===
37// Do not warn about missing -dealloc method. These properties are not
38// retained or synthesized.
39
40@interface MissingDeallocWithIntProperty : NSObject
41@property (assign) int ivar;
42@end
43
44@implementation MissingDeallocWithIntProperty
45@end
46
47@interface MissingDeallocWithSELProperty : NSObject
48@property (assign) SEL ivar;
49@end
50
51@implementation MissingDeallocWithSELProperty
52@end
53
54//===------------------------------------------------------------------------===
55// Warn about missing -dealloc method.
56
57@interface MissingDeallocWithCopyProperty : NSObject
58@property (copy) NSObject *ivar;
59@end
60
Devin Coughlin6d0c8a02016-03-01 00:39:04 +000061#if NON_ARC
62// expected-warning@+2{{'MissingDeallocWithCopyProperty' lacks a 'dealloc' instance method but must release '_ivar'}}
63#endif
Devin Coughlin30751342016-01-27 01:41:58 +000064@implementation MissingDeallocWithCopyProperty
65@end
66
67@interface MissingDeallocWithRetainProperty : NSObject
68@property (retain) NSObject *ivar;
69@end
70
Devin Coughlin6d0c8a02016-03-01 00:39:04 +000071#if NON_ARC
72// expected-warning@+2{{'MissingDeallocWithRetainProperty' lacks a 'dealloc' instance method but must release '_ivar'}}
73#endif
Devin Coughlin30751342016-01-27 01:41:58 +000074@implementation MissingDeallocWithRetainProperty
75@end
76
Devin Coughlin6d0c8a02016-03-01 00:39:04 +000077@interface MissingDeallocWithMultipleProperties : NSObject
78@property (retain) NSObject *ivar1;
79@property (retain) NSObject *ivar2;
80@end
81
82#if NON_ARC
83// expected-warning@+2{{'MissingDeallocWithMultipleProperties' lacks a 'dealloc' instance method but must release '_ivar1' and others}}
84#endif
85@implementation MissingDeallocWithMultipleProperties
86@end
87
Devin Coughlin30751342016-01-27 01:41:58 +000088@interface MissingDeallocWithIVarAndRetainProperty : NSObject {
89 NSObject *_ivar2;
90}
91@property (retain) NSObject *ivar1;
92@end
93
Devin Coughlin6d0c8a02016-03-01 00:39:04 +000094#if NON_ARC
95// expected-warning@+2{{'MissingDeallocWithIVarAndRetainProperty' lacks a 'dealloc' instance method but must release '_ivar1'}}
96#endif
Devin Coughlin30751342016-01-27 01:41:58 +000097@implementation MissingDeallocWithIVarAndRetainProperty
98@end
99
100@interface MissingDeallocWithReadOnlyRetainedProperty : NSObject
101@property (readonly,retain) NSObject *ivar;
102@end
103
Devin Coughlin6d0c8a02016-03-01 00:39:04 +0000104#if NON_ARC
105// expected-warning@+2{{'MissingDeallocWithReadOnlyRetainedProperty' lacks a 'dealloc' instance method but must release '_ivar'}}
106#endif
Devin Coughlin30751342016-01-27 01:41:58 +0000107@implementation MissingDeallocWithReadOnlyRetainedProperty
108@end
109
110
Ted Kremenek1f9dd452008-12-08 22:01:50 +0000111//===------------------------------------------------------------------------===
112// Don't warn about iVars that are selectors.
113
114@interface TestSELs : NSObject {
115 SEL a;
116 SEL b;
117}
118
119@end
120
Fariborz Jahanian0afc5552009-11-23 18:04:25 +0000121@implementation TestSELs
Ted Kremenek1f9dd452008-12-08 22:01:50 +0000122- (id)init {
123 if( (self = [super init]) ) {
124 a = @selector(a);
125 b = @selector(b);
126 }
127
128 return self;
129}
130@end
Ted Kremenek223005f2008-12-08 22:05:43 +0000131
132//===------------------------------------------------------------------------===
133// Don't warn about iVars that are IBOutlets.
134
Ted Kremenek223005f2008-12-08 22:05:43 +0000135@class NSWindow;
136
137@interface HasOutlet : NSObject {
138IBOutlet NSWindow *window;
139}
140@end
141
142@implementation HasOutlet // no-warning
143@end
144
Ted Kremenek5dcf9032009-02-10 23:41:52 +0000145//===------------------------------------------------------------------------===
Ted Kremenek02b63b42009-02-13 22:26:30 +0000146// PR 3187: http://llvm.org/bugs/show_bug.cgi?id=3187
147// - Disable the missing -dealloc check for classes that subclass SenTestCase
148
149@class NSString;
150
151@interface SenTestCase : NSObject {}
152@end
153
154@interface MyClassTest : SenTestCase {
155 NSString *resourcePath;
156}
Devin Coughlinad9f53e2016-02-25 21:15:16 +0000157
158@property (retain) NSObject *ivar;
159
Ted Kremenek02b63b42009-02-13 22:26:30 +0000160@end
161
162@interface NSBundle : NSObject {}
163+ (NSBundle *)bundleForClass:(Class)aClass;
164- (NSString *)resourcePath;
165@end
166
167@implementation MyClassTest
168- (void)setUp {
169 resourcePath = [[NSBundle bundleForClass:[self class]] resourcePath];
170}
171- (void)testXXX {
172 // do something which uses resourcepath
173}
174@end
Devin Coughlinad9f53e2016-02-25 21:15:16 +0000175
176//===------------------------------------------------------------------------===
177// Don't warn for clases that aren't subclasses of NSObject
178
179__attribute__((objc_root_class))
180@interface NonNSObjectMissingDealloc
181@property (retain) NSObject *ivar;
182@end
183@implementation NonNSObjectMissingDealloc
184@end
185
Devin Coughlin30751342016-01-27 01:41:58 +0000186// CHECK: 4 warnings generated.