1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease,osx.cocoa.RetainCount -fobjc-arc -fblocks -verify -Wno-objc-root-class %s -analyzer-output=text
2 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease,osx.cocoa.RetainCount -fblocks -verify -Wno-objc-root-class %s -analyzer-output=text
4 typedef __typeof(sizeof(int)) size_t;
6 #define HAS_ARC __has_feature(objc_arc)
8 typedef unsigned long long CFOptionFlags;
9 typedef signed long long CFIndex;
11 typedef CFIndex CFPropertyListFormat; enum {
12 kCFPropertyListOpenStepFormat = 1,
13 kCFPropertyListXMLFormat_v1_0 = 100,
14 kCFPropertyListBinaryFormat_v1_0 = 200
17 typedef const struct __CFAllocator * CFAllocatorRef;
18 extern const CFAllocatorRef kCFAllocatorDefault;
19 typedef struct __CFDictionary * CFDictionaryRef;
20 typedef struct __CFError * CFErrorRef;
21 typedef struct __CFDataRef * CFDataRef;
22 typedef void * CFPropertyListRef;
24 CFPropertyListRef CFPropertyListCreateWithData(CFAllocatorRef allocator, CFDataRef data, CFOptionFlags options, CFPropertyListFormat *format, CFErrorRef *error);
26 typedef signed char BOOL;
27 typedef struct _NSZone NSZone;
33 - (BOOL)isEqual:(id)object;
35 - (oneway void)release;
37 - (NSString *)description;
40 @interface NSObject <NSObject> {}
41 + (id)allocWithZone:(NSZone *)zone;
47 @interface NSDictionary : NSObject
50 #define OS_OBJECT_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
51 #define DISPATCH_RETURNS_RETAINED OS_OBJECT_RETURNS_RETAINED
53 @protocol OS_dispatch_object
55 @protocol OS_dispatch_data <OS_dispatch_object>
57 @protocol OS_dispatch_queue <OS_dispatch_object>
60 typedef NSObject<OS_dispatch_object> *dispatch_object_t;
61 typedef NSObject<OS_dispatch_data> *dispatch_data_t;
62 typedef NSObject<OS_dispatch_queue> *dispatch_queue_t;
64 typedef void (^dispatch_block_t)(void);
66 dispatch_queue_t dispatch_get_main_queue(void);
68 DISPATCH_RETURNS_RETAINED dispatch_data_t
69 dispatch_data_create(const void *buffer, size_t size,
70 dispatch_queue_t _Nullable queue,
71 dispatch_block_t _Nullable destructor);
73 void _dispatch_object_validate(dispatch_object_t object);
75 #define dispatch_retain(object) \
76 __extension__({ dispatch_object_t _o = (object); \
77 _dispatch_object_validate(_o); \
79 #define dispatch_release(object) \
80 __extension__({ dispatch_object_t _o = (object); \
81 _dispatch_object_validate(_o); \
88 @implementation SomeClass
89 - (NSDictionary *)copyTestWithBridgeReturningRetainable:(NSData *)plistData {
91 CFDictionaryRef testDict = CFPropertyListCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)plistData, 0, 0, &error);
93 // expected-note@-2 {{Call to function 'CFPropertyListCreateWithData' returns a Core Foundation object of type 'CFPropertyListRef' with a +1 retain count}}
95 return (__bridge NSDictionary *)testDict;
97 // expected-warning@-2 {{Potential leak of an object stored into 'testDict'}}
98 // expected-note@-3 {{Object leaked: object allocated and stored into 'testDict' is returned from a method managed by Automatic Reference Counting}}
102 - (NSDictionary *)copyTestWithoutBridgeReturningRetainable:(NSData *)plistData {
103 NSDictionary *testDict = [[NSDictionary alloc] init];
104 return testDict; // no-warning
107 - (NSDictionary *)copyTestWithBridgeTransferReturningRetainable:(NSData *)plistData {
109 CFDictionaryRef testDict = CFPropertyListCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)plistData, 0, 0, &error);
110 return (__bridge_transfer NSDictionary *)testDict; // no-warning under ARC
112 // expected-warning@-2 {{'__bridge_transfer' casts have no effect when not using ARC}} // Warning from Sema
116 - (CFDictionaryRef)copyTestReturningCoreFoundation:(NSData *)plistData {
118 CFDictionaryRef testDict = CFPropertyListCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)plistData, 0, 0, &error);
125 void libdispatch_leaked_data(void) {
126 dispatch_data_t data = dispatch_data_create(buf, 1024,
127 dispatch_get_main_queue(), ^{});
130 // expected-warning@-2{{Potential leak of an object stored into 'data'}}
131 // expected-note@-5{{Call to function 'dispatch_data_create' returns an Objective-C object with a +1 retain count}}
132 // expected-note@-4{{Object leaked: object allocated and stored into 'data' is not referenced later in this execution path and has a retain count of +1}}
135 void libdispatch_dispatch_released_data(void) {
136 dispatch_data_t data = dispatch_data_create(buf, 1024,
137 dispatch_get_main_queue(), ^{});
139 dispatch_release(data); // no-warning
143 void libdispatch_objc_released_data(void) {
144 dispatch_data_t data = dispatch_data_create(buf, 1024,
145 dispatch_get_main_queue(), ^{});
147 [data release]; // no-warning
151 void libdispatch_leaked_retained_data(void) {
152 dispatch_data_t data = dispatch_data_create(buf, 1024,
153 dispatch_get_main_queue(), ^{});
155 dispatch_retain(data);
160 // expected-warning@-2{{Potential leak of an object stored into 'data'}}
161 // expected-note@-9{{Call to function 'dispatch_data_create' returns an Objective-C object with a +1 retain count}}
162 // expected-note@-7{{Reference count incremented. The object now has a +2 retain count}}
163 // expected-note@-7{{Reference count decremented. The object now has a +1 retain count}}
164 // expected-note@-6{{Object leaked: object allocated and stored into 'data' is not referenced later in this execution path and has a retain count of +1}}