blob: ae880546ff96f3b324c6012522d407989ab3e2c6 [file] [log] [blame]
Devin Coughlin30751342016-01-27 01:41:58 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.osx.cocoa.Dealloc -fblocks %s 2>&1 | FileCheck -check-prefix=CHECK %s
2// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.osx.cocoa.Dealloc -fblocks -triple x86_64-apple-darwin10 -fobjc-arc %s 2>&1 | FileCheck -check-prefix=CHECK-ARC -allow-empty '--implicit-check-not=error:' '--implicit-check-not=warning:' %s
3
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +00004typedef signed char BOOL;
Ted Kremenek02b63b42009-02-13 22:26:30 +00005@protocol NSObject
6- (BOOL)isEqual:(id)object;
7- (Class)class;
8@end
9
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +000010@interface NSObject <NSObject> {}
11- (void)dealloc;
Ted Kremenek1f9dd452008-12-08 22:01:50 +000012- (id)init;
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +000013@end
14
Ted Kremenek1f9dd452008-12-08 22:01:50 +000015typedef struct objc_selector *SEL;
16
Devin Coughlin30751342016-01-27 01:41:58 +000017//===------------------------------------------------------------------------===
18// Do not warn about missing -dealloc method. Not enough context to know
19// whether the ivar is retained or not.
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +000020
Devin Coughlin30751342016-01-27 01:41:58 +000021@interface MissingDeallocWithIvar : NSObject {
22 NSObject *_ivar;
Ted Kremenek8f5c0ed2008-12-08 21:59:21 +000023}
24@end
Ted Kremenek1f9dd452008-12-08 22:01:50 +000025
Devin Coughlin30751342016-01-27 01:41:58 +000026@implementation MissingDeallocWithIvar
27@end
28
29//===------------------------------------------------------------------------===
30// Do not warn about missing -dealloc method. These properties are not
31// retained or synthesized.
32
33@interface MissingDeallocWithIntProperty : NSObject
34@property (assign) int ivar;
35@end
36
37@implementation MissingDeallocWithIntProperty
38@end
39
40@interface MissingDeallocWithSELProperty : NSObject
41@property (assign) SEL ivar;
42@end
43
44@implementation MissingDeallocWithSELProperty
45@end
46
47//===------------------------------------------------------------------------===
48// Warn about missing -dealloc method.
49
50@interface MissingDeallocWithCopyProperty : NSObject
51@property (copy) NSObject *ivar;
52@end
53
54// CHECK: MissingDealloc.m:[[@LINE+1]]:1: warning: Objective-C class 'MissingDeallocWithCopyProperty' lacks a 'dealloc' instance method
55@implementation MissingDeallocWithCopyProperty
56@end
57
58@interface MissingDeallocWithRetainProperty : NSObject
59@property (retain) NSObject *ivar;
60@end
61
62// CHECK: MissingDealloc.m:[[@LINE+1]]:1: warning: Objective-C class 'MissingDeallocWithRetainProperty' lacks a 'dealloc' instance method
63@implementation MissingDeallocWithRetainProperty
64@end
65
66@interface MissingDeallocWithIVarAndRetainProperty : NSObject {
67 NSObject *_ivar2;
68}
69@property (retain) NSObject *ivar1;
70@end
71
72// CHECK: MissingDealloc.m:[[@LINE+1]]:1: warning: Objective-C class 'MissingDeallocWithIVarAndRetainProperty' lacks a 'dealloc' instance method
73@implementation MissingDeallocWithIVarAndRetainProperty
74@end
75
76@interface MissingDeallocWithReadOnlyRetainedProperty : NSObject
77@property (readonly,retain) NSObject *ivar;
78@end
79
80// CHECK: MissingDealloc.m:[[@LINE+1]]:1: warning: Objective-C class 'MissingDeallocWithReadOnlyRetainedProperty' lacks a 'dealloc' instance method
81@implementation MissingDeallocWithReadOnlyRetainedProperty
82@end
83
84
Ted Kremenek1f9dd452008-12-08 22:01:50 +000085//===------------------------------------------------------------------------===
86// Don't warn about iVars that are selectors.
87
88@interface TestSELs : NSObject {
89 SEL a;
90 SEL b;
91}
92
93@end
94
Fariborz Jahanian0afc5552009-11-23 18:04:25 +000095@implementation TestSELs
Ted Kremenek1f9dd452008-12-08 22:01:50 +000096- (id)init {
97 if( (self = [super init]) ) {
98 a = @selector(a);
99 b = @selector(b);
100 }
101
102 return self;
103}
104@end
Ted Kremenek223005f2008-12-08 22:05:43 +0000105
106//===------------------------------------------------------------------------===
107// Don't warn about iVars that are IBOutlets.
108
Ted Kremenek223005f2008-12-08 22:05:43 +0000109@class NSWindow;
110
111@interface HasOutlet : NSObject {
112IBOutlet NSWindow *window;
113}
114@end
115
116@implementation HasOutlet // no-warning
117@end
118
Ted Kremenek5dcf9032009-02-10 23:41:52 +0000119//===------------------------------------------------------------------------===
Ted Kremenek02b63b42009-02-13 22:26:30 +0000120// PR 3187: http://llvm.org/bugs/show_bug.cgi?id=3187
121// - Disable the missing -dealloc check for classes that subclass SenTestCase
122
123@class NSString;
124
125@interface SenTestCase : NSObject {}
126@end
127
128@interface MyClassTest : SenTestCase {
129 NSString *resourcePath;
130}
Devin Coughlinad9f53e2016-02-25 21:15:16 +0000131
132@property (retain) NSObject *ivar;
133
Ted Kremenek02b63b42009-02-13 22:26:30 +0000134@end
135
136@interface NSBundle : NSObject {}
137+ (NSBundle *)bundleForClass:(Class)aClass;
138- (NSString *)resourcePath;
139@end
140
141@implementation MyClassTest
142- (void)setUp {
143 resourcePath = [[NSBundle bundleForClass:[self class]] resourcePath];
144}
145- (void)testXXX {
146 // do something which uses resourcepath
147}
148@end
Devin Coughlinad9f53e2016-02-25 21:15:16 +0000149
150//===------------------------------------------------------------------------===
151// Don't warn for clases that aren't subclasses of NSObject
152
153__attribute__((objc_root_class))
154@interface NonNSObjectMissingDealloc
155@property (retain) NSObject *ivar;
156@end
157@implementation NonNSObjectMissingDealloc
158@end
159
Devin Coughlin30751342016-01-27 01:41:58 +0000160// CHECK: 4 warnings generated.