| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.NoUnretainedMemberChecker -verify %s |
| 2 |
|
| 3 |
@class SystemObject; |
| 4 |
|
| 5 |
#include "objc-mock-types.h" |
| 6 |
#include "mock-system-header.h" |
| 7 |
|
| 8 |
namespace members { |
| 9 |
|
| 10 |
struct Foo { |
| 11 |
private: |
| 12 |
SomeObj* a = nullptr; |
| 13 |
// expected-warning@-1{{Member variable 'a' in 'members::Foo' is a raw pointer to retainable type}} |
| 14 |
dispatch_queue_t a2 = nullptr; |
| 15 |
// expected-warning@-1{{Member variable 'a2' in 'members::Foo' is a retainable type 'dispatch_queue_t'}} |
| 16 |
|
| 17 |
[[clang::suppress]] |
| 18 |
SomeObj* a_suppressed = nullptr; |
| 19 |
// No warning. |
| 20 |
|
| 21 |
protected: |
| 22 |
RetainPtr<SomeObj> b; |
| 23 |
// No warning. |
| 24 |
OSObjectPtr<dispatch_queue_t> b2; |
| 25 |
// No warning. |
| 26 |
|
| 27 |
public: |
| 28 |
SomeObj* c = nullptr; |
| 29 |
// expected-warning@-1{{Member variable 'c' in 'members::Foo' is a raw pointer to retainable type}} |
| 30 |
RetainPtr<SomeObj> d; |
| 31 |
OSObjectPtr<dispatch_queue_t> d2; |
| 32 |
|
| 33 |
CFMutableArrayRef e = nullptr; |
| 34 |
// expected-warning@-1{{Member variable 'e' in 'members::Foo' is a retainable type 'CFMutableArrayRef'}} |
| 35 |
|
| 36 |
}; |
| 37 |
|
| 38 |
template<class T, class S, class R> |
| 39 |
struct FooTmpl { |
| 40 |
T* a; |
| 41 |
// expected-warning@-1{{Member variable 'a' in 'members::FooTmpl<SomeObj, __CFArray *, NSObject<OS_dispatch_queue> *>' is a raw pointer to retainable type}} |
| 42 |
S b; |
| 43 |
// expected-warning@-1{{Member variable 'b' in 'members::FooTmpl<SomeObj, __CFArray *, NSObject<OS_dispatch_queue> *>' is a raw pointer to retainable type}} |
| 44 |
R c; |
| 45 |
// expected-warning@-1{{Member variable 'c' in 'members::FooTmpl<SomeObj, __CFArray *, NSObject<OS_dispatch_queue> *>' is a raw pointer to retainable type}} |
| 46 |
}; |
| 47 |
|
| 48 |
void forceTmplToInstantiate(FooTmpl<SomeObj, CFMutableArrayRef, dispatch_queue_t>) {} |
| 49 |
|
| 50 |
struct [[clang::suppress]] FooSuppressed { |
| 51 |
private: |
| 52 |
SomeObj* a = nullptr; |
| 53 |
// No warning. |
| 54 |
}; |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
namespace unions { |
| 59 |
union Foo { |
| 60 |
SomeObj* a; |
| 61 |
// expected-warning@-1{{Member variable 'a' in 'unions::Foo' is a raw pointer to retainable type 'SomeObj'}} |
| 62 |
RetainPtr<SomeObj> b; |
| 63 |
CFMutableArrayRef c; |
| 64 |
// expected-warning@-1{{Member variable 'c' in 'unions::Foo' is a retainable type 'CFMutableArrayRef'}} |
| 65 |
dispatch_queue_t d; |
| 66 |
// expected-warning@-1{{Member variable 'd' in 'unions::Foo' is a retainable type 'dispatch_queue_t'}} |
| 67 |
}; |
| 68 |
|
| 69 |
template<class T> |
| 70 |
union FooTempl { |
| 71 |
T* a; |
| 72 |
// expected-warning@-1{{Member variable 'a' in 'unions::FooTempl<SomeObj>' is a raw pointer to retainable type 'SomeObj'}} |
| 73 |
}; |
| 74 |
|
| 75 |
void forceTmplToInstantiate(FooTempl<SomeObj>) {} |
| 76 |
} |
| 77 |
|
| 78 |
namespace ptr_to_ptr_to_retained { |
| 79 |
|
| 80 |
struct List { |
| 81 |
SomeObj** elements1; |
| 82 |
// expected-warning@-1{{Member variable 'elements1' in 'ptr_to_ptr_to_retained::List' contains a raw pointer to retainable type 'SomeObj'}} |
| 83 |
CFMutableArrayRef* elements2; |
| 84 |
// expected-warning@-1{{Member variable 'elements2' in 'ptr_to_ptr_to_retained::List' contains a retainable type 'CFMutableArrayRef'}} |
| 85 |
dispatch_queue_t* elements3; |
| 86 |
// expected-warning@-1{{Member variable 'elements3' in 'ptr_to_ptr_to_retained::List' contains a retainable type 'dispatch_queue_t'}} |
| 87 |
}; |
| 88 |
|
| 89 |
template <typename T, typename S, typename R> |
| 90 |
struct TemplateList { |
| 91 |
T** elements1; |
| 92 |
// expected-warning@-1{{Member variable 'elements1' in 'ptr_to_ptr_to_retained::TemplateList<SomeObj, __CFArray *, NSObject<OS_dispatch_queue> *>' contains a raw pointer to retainable type 'SomeObj'}} |
| 93 |
S* elements2; |
| 94 |
// expected-warning@-1{{Member variable 'elements2' in 'ptr_to_ptr_to_retained::TemplateList<SomeObj, __CFArray *, NSObject<OS_dispatch_queue> *>' contains a raw pointer to retainable type '__CFArray'}} |
| 95 |
R* elements3; |
| 96 |
// expected-warning@-1{{Member variable 'elements3' in 'ptr_to_ptr_to_retained::TemplateList<SomeObj, __CFArray *, NSObject<OS_dispatch_queue> *>' contains a raw pointer to retainable type 'NSObject'}} |
| 97 |
}; |
| 98 |
TemplateList<SomeObj, CFMutableArrayRef, dispatch_queue_t> list; |
| 99 |
|
| 100 |
struct SafeList { |
| 101 |
RetainPtr<SomeObj>* elements1; |
| 102 |
RetainPtr<CFMutableArrayRef>* elements2; |
| 103 |
}; |
| 104 |
|
| 105 |
} // namespace ptr_to_ptr_to_retained |
| 106 |
|
| 107 |
@interface AnotherObject : NSObject { |
| 108 |
NSString *ns_string; |
| 109 |
// expected-warning@-1{{Instance variable 'ns_string' in 'AnotherObject' is a raw pointer to retainable type 'NSString'}} |
| 110 |
CFStringRef cf_string; |
| 111 |
// expected-warning@-1{{Instance variable 'cf_string' in 'AnotherObject' is a retainable type 'CFStringRef'}} |
| 112 |
dispatch_queue_t dispatch; |
| 113 |
// expected-warning@-1{{Instance variable 'dispatch' in 'AnotherObject' is a retainable type 'dispatch_queue_t'}} |
| 114 |
} |
| 115 |
@property(nonatomic, readonly, strong) NSString *prop_string; |
| 116 |
@property(nonatomic, readonly) NSString *prop_safe; |
| 117 |
@end |
| 118 |
|
| 119 |
@implementation AnotherObject |
| 120 |
- (NSString *)prop_safe { |
| 121 |
return nil; |
| 122 |
} |
| 123 |
@end |
| 124 |
|
| 125 |
@interface DerivedObject : AnotherObject { |
| 126 |
NSNumber *ns_number; |
| 127 |
// expected-warning@-1{{Instance variable 'ns_number' in 'DerivedObject' is a raw pointer to retainable type 'NSNumber'}} |
| 128 |
CGImageRef cg_image; |
| 129 |
// expected-warning@-1{{Instance variable 'cg_image' in 'DerivedObject' is a retainable type 'CGImageRef'}} |
| 130 |
dispatch_queue_t os_dispatch; |
| 131 |
// expected-warning@-1{{Instance variable 'os_dispatch' in 'DerivedObject' is a retainable type 'dispatch_queue_t'}} |
| 132 |
} |
| 133 |
@property(nonatomic, strong) NSNumber *prop_number; |
| 134 |
@property(nonatomic, readonly) NSString *prop_string; |
| 135 |
@end |
| 136 |
|
| 137 |
@implementation DerivedObject |
| 138 |
- (NSString *)prop_string { |
| 139 |
return nil; |
| 140 |
} |
| 141 |
@end |
| 142 |
|
| 143 |
// No warnings for @interface declaration itself. |
| 144 |
@interface InterfaceOnlyObject : NSObject |
| 145 |
@property(nonatomic, strong) NSString *prop_string1; |
| 146 |
@property(nonatomic, assign) NSString *prop_string2; |
| 147 |
@property(nonatomic, unsafe_unretained) NSString *prop_string3; |
| 148 |
@property(nonatomic, readonly) NSString *prop_string4; |
| 149 |
@end |
| 150 |
|
| 151 |
@interface InterfaceOnlyObject2 : NSObject |
| 152 |
@property(nonatomic, strong) NSString *prop_string1; |
| 153 |
@property(nonatomic, assign) NSString *prop_string2; |
| 154 |
@property(nonatomic, unsafe_unretained) NSString *prop_string3; |
| 155 |
// expected-warning@-1{{Property 'prop_string3' in 'DerivedObject2' is a raw pointer to retainable type 'NSString'}} |
| 156 |
@property(nonatomic, readonly) NSString *prop_string4; |
| 157 |
@end |
| 158 |
|
| 159 |
@interface DerivedObject2 : InterfaceOnlyObject2 |
| 160 |
@property(nonatomic, readonly) NSString *prop_string5; |
| 161 |
// expected-warning@-1{{Property 'prop_string5' in 'DerivedObject2' is a raw pointer to retainable type 'NSString'}} |
| 162 |
@end |
| 163 |
|
| 164 |
@implementation DerivedObject2 |
| 165 |
@synthesize prop_string3; |
| 166 |
@end |
| 167 |
|
| 168 |
NS_REQUIRES_PROPERTY_DEFINITIONS |
| 169 |
@interface NoSynthObject : NSObject { |
| 170 |
NSString *ns_string; |
| 171 |
// expected-warning@-1{{Instance variable 'ns_string' in 'NoSynthObject' is a raw pointer to retainable type 'NSString'}} |
| 172 |
CFStringRef cf_string; |
| 173 |
// expected-warning@-1{{Instance variable 'cf_string' in 'NoSynthObject' is a retainable type 'CFStringRef'}} |
| 174 |
dispatch_queue_t dispatch; |
| 175 |
// expected-warning@-1{{Instance variable 'dispatch' in 'NoSynthObject' is a retainable type 'dispatch_queue_t'}} |
| 176 |
} |
| 177 |
@property(nonatomic, readonly, strong) NSString *prop_string1; |
| 178 |
@property(nonatomic, readonly, strong) NSString *prop_string2; |
| 179 |
@property(nonatomic, assign) NSString *prop_string3; |
| 180 |
// expected-warning@-1{{Property 'prop_string3' in 'NoSynthObject' is a raw pointer to retainable type 'NSString'; member variables must be a RetainPtr}} |
| 181 |
@property(nonatomic, unsafe_unretained) NSString *prop_string4; |
| 182 |
// expected-warning@-1{{Property 'prop_string4' in 'NoSynthObject' is a raw pointer to retainable type 'NSString'; member variables must be a RetainPtr}} |
| 183 |
@property(nonatomic, copy) NSString *prop_string5; |
| 184 |
@property(nonatomic, readonly, strong) dispatch_queue_t dispatch; |
| 185 |
@end |
| 186 |
|
| 187 |
@implementation NoSynthObject |
| 188 |
- (NSString *)prop_string1 { |
| 189 |
return nil; |
| 190 |
} |
| 191 |
@synthesize prop_string2; |
| 192 |
@synthesize prop_string3; |
| 193 |
@synthesize prop_string4; |
| 194 |
@synthesize prop_string5; |
| 195 |
- (dispatch_queue_t)dispatch { |
| 196 |
return nil; |
| 197 |
} |
| 198 |
@end |