Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 1 | // 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 Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 4 | typedef signed char BOOL; |
Ted Kremenek | 02b63b4 | 2009-02-13 22:26:30 +0000 | [diff] [blame] | 5 | @protocol NSObject |
| 6 | - (BOOL)isEqual:(id)object; |
| 7 | - (Class)class; |
| 8 | @end |
| 9 | |
Ted Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 10 | @interface NSObject <NSObject> {} |
| 11 | - (void)dealloc; |
Ted Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 12 | - (id)init; |
Ted Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 13 | @end |
| 14 | |
Ted Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 15 | typedef struct objc_selector *SEL; |
| 16 | |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 17 | //===------------------------------------------------------------------------=== |
| 18 | // Do not warn about missing -dealloc method. Not enough context to know |
| 19 | // whether the ivar is retained or not. |
Ted Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 20 | |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 21 | @interface MissingDeallocWithIvar : NSObject { |
| 22 | NSObject *_ivar; |
Ted Kremenek | 8f5c0ed | 2008-12-08 21:59:21 +0000 | [diff] [blame] | 23 | } |
| 24 | @end |
Ted Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 25 | |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 26 | @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 Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 85 | //===------------------------------------------------------------------------=== |
| 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 Jahanian | 0afc555 | 2009-11-23 18:04:25 +0000 | [diff] [blame] | 95 | @implementation TestSELs |
Ted Kremenek | 1f9dd45 | 2008-12-08 22:01:50 +0000 | [diff] [blame] | 96 | - (id)init { |
| 97 | if( (self = [super init]) ) { |
| 98 | a = @selector(a); |
| 99 | b = @selector(b); |
| 100 | } |
| 101 | |
| 102 | return self; |
| 103 | } |
| 104 | @end |
Ted Kremenek | 223005f | 2008-12-08 22:05:43 +0000 | [diff] [blame] | 105 | |
| 106 | //===------------------------------------------------------------------------=== |
| 107 | // Don't warn about iVars that are IBOutlets. |
| 108 | |
Ted Kremenek | 223005f | 2008-12-08 22:05:43 +0000 | [diff] [blame] | 109 | @class NSWindow; |
| 110 | |
| 111 | @interface HasOutlet : NSObject { |
| 112 | IBOutlet NSWindow *window; |
| 113 | } |
| 114 | @end |
| 115 | |
| 116 | @implementation HasOutlet // no-warning |
| 117 | @end |
| 118 | |
Ted Kremenek | 5dcf903 | 2009-02-10 23:41:52 +0000 | [diff] [blame] | 119 | //===------------------------------------------------------------------------=== |
Ted Kremenek | 02b63b4 | 2009-02-13 22:26:30 +0000 | [diff] [blame] | 120 | // 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 Coughlin | ad9f53e | 2016-02-25 21:15:16 +0000 | [diff] [blame^] | 131 | |
| 132 | @property (retain) NSObject *ivar; |
| 133 | |
Ted Kremenek | 02b63b4 | 2009-02-13 22:26:30 +0000 | [diff] [blame] | 134 | @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 Coughlin | ad9f53e | 2016-02-25 21:15:16 +0000 | [diff] [blame^] | 149 | |
| 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 Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 160 | // CHECK: 4 warnings generated. |