Dominic Chen | 184c624 | 2017-03-03 18:02:02 +0000 | [diff] [blame] | 1 | // 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 Coughlin | 6d0c8a0 | 2016-03-01 00:39:04 +0000 | [diff] [blame] | 3 | |
| 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 Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 10 | |
Ted Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 11 | typedef signed char BOOL; |
Ted Kremenek | 02b63b4 | 2009-02-13 22:26:30 +0000 | [diff] [blame] | 12 | @protocol NSObject |
| 13 | - (BOOL)isEqual:(id)object; |
| 14 | - (Class)class; |
| 15 | @end |
| 16 | |
Ted Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 17 | @interface NSObject <NSObject> {} |
| 18 | - (void)dealloc; |
Ted Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 19 | - (id)init; |
Ted Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 20 | @end |
| 21 | |
Ted Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 22 | typedef struct objc_selector *SEL; |
| 23 | |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 24 | //===------------------------------------------------------------------------=== |
| 25 | // Do not warn about missing -dealloc method. Not enough context to know |
| 26 | // whether the ivar is retained or not. |
Ted Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 27 | |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 28 | @interface MissingDeallocWithIvar : NSObject { |
| 29 | NSObject *_ivar; |
Ted Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 30 | } |
| 31 | @end |
Ted Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 32 | |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 33 | @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 Coughlin | 6d0c8a0 | 2016-03-01 00:39:04 +0000 | [diff] [blame] | 61 | #if NON_ARC |
| 62 | // expected-warning@+2{{'MissingDeallocWithCopyProperty' lacks a 'dealloc' instance method but must release '_ivar'}} |
| 63 | #endif |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 64 | @implementation MissingDeallocWithCopyProperty |
| 65 | @end |
| 66 | |
| 67 | @interface MissingDeallocWithRetainProperty : NSObject |
| 68 | @property (retain) NSObject *ivar; |
| 69 | @end |
| 70 | |
Devin Coughlin | 6d0c8a0 | 2016-03-01 00:39:04 +0000 | [diff] [blame] | 71 | #if NON_ARC |
| 72 | // expected-warning@+2{{'MissingDeallocWithRetainProperty' lacks a 'dealloc' instance method but must release '_ivar'}} |
| 73 | #endif |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 74 | @implementation MissingDeallocWithRetainProperty |
| 75 | @end |
| 76 | |
Devin Coughlin | 6d0c8a0 | 2016-03-01 00:39:04 +0000 | [diff] [blame] | 77 | @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 Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 88 | @interface MissingDeallocWithIVarAndRetainProperty : NSObject { |
| 89 | NSObject *_ivar2; |
| 90 | } |
| 91 | @property (retain) NSObject *ivar1; |
| 92 | @end |
| 93 | |
Devin Coughlin | 6d0c8a0 | 2016-03-01 00:39:04 +0000 | [diff] [blame] | 94 | #if NON_ARC |
| 95 | // expected-warning@+2{{'MissingDeallocWithIVarAndRetainProperty' lacks a 'dealloc' instance method but must release '_ivar1'}} |
| 96 | #endif |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 97 | @implementation MissingDeallocWithIVarAndRetainProperty |
| 98 | @end |
| 99 | |
| 100 | @interface MissingDeallocWithReadOnlyRetainedProperty : NSObject |
| 101 | @property (readonly,retain) NSObject *ivar; |
| 102 | @end |
| 103 | |
Devin Coughlin | 6d0c8a0 | 2016-03-01 00:39:04 +0000 | [diff] [blame] | 104 | #if NON_ARC |
| 105 | // expected-warning@+2{{'MissingDeallocWithReadOnlyRetainedProperty' lacks a 'dealloc' instance method but must release '_ivar'}} |
| 106 | #endif |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 107 | @implementation MissingDeallocWithReadOnlyRetainedProperty |
| 108 | @end |
| 109 | |
| 110 | |
Ted Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 111 | //===------------------------------------------------------------------------=== |
| 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 Jahanian | 0afc555 | 2009-11-23 18:04:25 +0000 | [diff] [blame] | 121 | @implementation TestSELs |
Ted Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 122 | - (id)init { |
| 123 | if( (self = [super init]) ) { |
| 124 | a = @selector(a); |
| 125 | b = @selector(b); |
| 126 | } |
| 127 | |
| 128 | return self; |
| 129 | } |
| 130 | @end |
Ted Kremenek | 223005f | 2008-12-08 22:05:43 +0000 | [diff] [blame] | 131 | |
| 132 | //===------------------------------------------------------------------------=== |
| 133 | // Don't warn about iVars that are IBOutlets. |
| 134 | |
Ted Kremenek | 223005f | 2008-12-08 22:05:43 +0000 | [diff] [blame] | 135 | @class NSWindow; |
| 136 | |
| 137 | @interface HasOutlet : NSObject { |
| 138 | IBOutlet NSWindow *window; |
| 139 | } |
| 140 | @end |
| 141 | |
| 142 | @implementation HasOutlet // no-warning |
| 143 | @end |
| 144 | |
Ted Kremenek | 5dcf903 | 2009-02-10 23:41:52 +0000 | [diff] [blame] | 145 | //===------------------------------------------------------------------------=== |
Ted Kremenek | 02b63b4 | 2009-02-13 22:26:30 +0000 | [diff] [blame] | 146 | // 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 Coughlin | ad9f53e | 2016-02-25 21:15:16 +0000 | [diff] [blame] | 157 | |
| 158 | @property (retain) NSObject *ivar; |
| 159 | |
Ted Kremenek | 02b63b4 | 2009-02-13 22:26:30 +0000 | [diff] [blame] | 160 | @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 Coughlin | ad9f53e | 2016-02-25 21:15:16 +0000 | [diff] [blame] | 175 | |
| 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 Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 186 | // CHECK: 4 warnings generated. |