| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.NoUnretainedMemberChecker -fobjc-arc -verify %s |
| 2 |
|
| 3 |
#include "objc-mock-types.h" |
| 4 |
|
| 5 |
namespace members { |
| 6 |
|
| 7 |
struct Foo { |
| 8 |
private: |
| 9 |
SomeObj* a = nullptr; |
| 10 |
|
| 11 |
[[clang::suppress]] |
| 12 |
SomeObj* a_suppressed = nullptr; |
| 13 |
|
| 14 |
protected: |
| 15 |
RetainPtr<SomeObj> b; |
| 16 |
|
| 17 |
public: |
| 18 |
SomeObj* c = nullptr; |
| 19 |
RetainPtr<SomeObj> d; |
| 20 |
|
| 21 |
CFMutableArrayRef e = nullptr; |
| 22 |
// expected-warning@-1{{Member variable 'e' in 'members::Foo' is a retainable type 'CFMutableArrayRef'}} |
| 23 |
|
| 24 |
dispatch_queue_t f = nullptr; |
| 25 |
}; |
| 26 |
|
| 27 |
union FooUnion { |
| 28 |
SomeObj* a; |
| 29 |
CFMutableArrayRef b; |
| 30 |
// expected-warning@-1{{Member variable 'b' in 'members::FooUnion' is a retainable type 'CFMutableArrayRef'}} |
| 31 |
dispatch_queue_t c; |
| 32 |
}; |
| 33 |
|
| 34 |
template<class T, class S, class R> |
| 35 |
struct FooTmpl { |
| 36 |
T* x; |
| 37 |
S y; |
| 38 |
// expected-warning@-1{{Member variable 'y' in 'members::FooTmpl<SomeObj, __CFArray *, NSObject<OS_dispatch_queue> *>' is a raw pointer to retainable type}} |
| 39 |
R z; |
| 40 |
}; |
| 41 |
|
| 42 |
void forceTmplToInstantiate(FooTmpl<SomeObj, CFMutableArrayRef, dispatch_queue_t>) {} |
| 43 |
|
| 44 |
struct [[clang::suppress]] FooSuppressed { |
| 45 |
private: |
| 46 |
SomeObj* a = nullptr; |
| 47 |
}; |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
namespace ptr_to_ptr_to_retained { |
| 52 |
|
| 53 |
struct List { |
| 54 |
CFMutableArrayRef* elements2; |
| 55 |
// expected-warning@-1{{Member variable 'elements2' in 'ptr_to_ptr_to_retained::List' contains a retainable type 'CFMutableArrayRef'}} |
| 56 |
}; |
| 57 |
|
| 58 |
template <typename T, typename S, typename R> |
| 59 |
struct TemplateList { |
| 60 |
T* elements1; |
| 61 |
S* elements2; |
| 62 |
// 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'}} |
| 63 |
R* elements3; |
| 64 |
}; |
| 65 |
TemplateList<SomeObj, CFMutableArrayRef, dispatch_queue_t> list; |
| 66 |
|
| 67 |
struct SafeList { |
| 68 |
RetainPtr<SomeObj>* elements1; |
| 69 |
RetainPtr<CFMutableArrayRef>* elements2; |
| 70 |
OSObjectPtr<dispatch_queue_t> elements3; |
| 71 |
}; |
| 72 |
|
| 73 |
} // namespace ptr_to_ptr_to_retained |
| 74 |
|
| 75 |
@interface AnotherObject : NSObject { |
| 76 |
NSString *ns_string; |
| 77 |
CFStringRef cf_string; |
| 78 |
// expected-warning@-1{{Instance variable 'cf_string' in 'AnotherObject' is a retainable type 'CFStringRef'; member variables must be a RetainPtr}} |
| 79 |
dispatch_queue_t queue; |
| 80 |
} |
| 81 |
@property(nonatomic, strong) NSString *prop_string1; |
| 82 |
@property(nonatomic, assign) NSString *prop_string2; |
| 83 |
// expected-warning@-1{{Property 'prop_string2' in 'AnotherObject' is a raw pointer to retainable type 'NSString'; member variables must be a RetainPtr}} |
| 84 |
@property(nonatomic, unsafe_unretained) NSString *prop_string3; |
| 85 |
// expected-warning@-1{{Property 'prop_string3' in 'AnotherObject' is a raw pointer to retainable type 'NSString'; member variables must be a RetainPtr}} |
| 86 |
@property(nonatomic, readonly) NSString *prop_string4; |
| 87 |
@property(nonatomic, readonly) NSString *prop_safe; |
| 88 |
@end |
| 89 |
|
| 90 |
@implementation AnotherObject |
| 91 |
- (NSString *)prop_safe { |
| 92 |
return nil; |
| 93 |
} |
| 94 |
@end |
| 95 |
|
| 96 |
// No warnings for @interface declaration itself. |
| 97 |
@interface InterfaceOnlyObject : NSObject |
| 98 |
@property(nonatomic, strong) NSString *prop_string1; |
| 99 |
@property(nonatomic, assign) NSString *prop_string2; |
| 100 |
@property(nonatomic, unsafe_unretained) NSString *prop_string3; |
| 101 |
@property(nonatomic, readonly) NSString *prop_string4; |
| 102 |
@property(nonatomic, readonly) dispatch_queue_t prop_string5; |
| 103 |
@end |
| 104 |
|
| 105 |
NS_REQUIRES_PROPERTY_DEFINITIONS |
| 106 |
@interface NoSynthObject : NSObject { |
| 107 |
NSString *ns_string; |
| 108 |
CFStringRef cf_string; |
| 109 |
// expected-warning@-1{{Instance variable 'cf_string' in 'NoSynthObject' is a retainable type 'CFStringRef'; member variables must be a RetainPtr}} |
| 110 |
} |
| 111 |
@property(nonatomic, readonly, strong) NSString *prop_string1; |
| 112 |
@property(nonatomic, readonly, strong) NSString *prop_string2; |
| 113 |
@property(nonatomic, assign) NSString *prop_string3; |
| 114 |
// expected-warning@-1{{Property 'prop_string3' in 'NoSynthObject' is a raw pointer to retainable type 'NSString'; member variables must be a RetainPtr}} |
| 115 |
@property(nonatomic, unsafe_unretained) NSString *prop_string4; |
| 116 |
// expected-warning@-1{{Property 'prop_string4' in 'NoSynthObject' is a raw pointer to retainable type 'NSString'; member variables must be a RetainPtr}} |
| 117 |
@property(nonatomic, unsafe_unretained) dispatch_queue_t prop_string5; |
| 118 |
// expected-warning@-1{{Property 'prop_string5' in 'NoSynthObject' is a retainable type 'dispatch_queue_t'}} |
| 119 |
@end |
| 120 |
|
| 121 |
@implementation NoSynthObject |
| 122 |
- (NSString *)prop_string1 { |
| 123 |
return nil; |
| 124 |
} |
| 125 |
@synthesize prop_string2; |
| 126 |
@synthesize prop_string3; |
| 127 |
@synthesize prop_string4; |
| 128 |
@synthesize prop_string5; |
| 129 |
@end |