brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.1 KiB · e0f4c4d Raw
166 lines · plain
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=text2// 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=text3 4typedef __typeof(sizeof(int)) size_t;5 6#define HAS_ARC __has_feature(objc_arc)7 8typedef unsigned long long CFOptionFlags;9typedef signed long long CFIndex;10 11typedef CFIndex CFPropertyListFormat; enum {12    kCFPropertyListOpenStepFormat = 1,13    kCFPropertyListXMLFormat_v1_0 = 100,14    kCFPropertyListBinaryFormat_v1_0 = 20015};16 17typedef const struct __CFAllocator * CFAllocatorRef;18extern const CFAllocatorRef kCFAllocatorDefault;19typedef struct __CFDictionary * CFDictionaryRef;20typedef struct __CFError * CFErrorRef;21typedef struct __CFDataRef * CFDataRef;22typedef void * CFPropertyListRef;23 24CFPropertyListRef CFPropertyListCreateWithData(CFAllocatorRef allocator, CFDataRef data, CFOptionFlags options, CFPropertyListFormat *format, CFErrorRef *error);25 26typedef signed char BOOL;27typedef struct _NSZone NSZone;28@class NSDictionary;29@class NSData;30@class NSString;31 32@protocol NSObject33- (BOOL)isEqual:(id)object;34- (id)retain;35- (oneway void)release;36- (id)autorelease;37- (NSString *)description;38- (id)init;39@end40@interface NSObject <NSObject> {}41+ (id)allocWithZone:(NSZone *)zone;42+ (id)alloc;43+ (id)new;44- (void)dealloc;45@end46 47@interface NSDictionary : NSObject48@end49 50#define OS_OBJECT_RETURNS_RETAINED __attribute__((__ns_returns_retained__))51#define DISPATCH_RETURNS_RETAINED OS_OBJECT_RETURNS_RETAINED52 53@protocol OS_dispatch_object54@end55@protocol OS_dispatch_data <OS_dispatch_object>56@end57@protocol OS_dispatch_queue <OS_dispatch_object>58@end59 60typedef NSObject<OS_dispatch_object> *dispatch_object_t;61typedef NSObject<OS_dispatch_data> *dispatch_data_t;62typedef NSObject<OS_dispatch_queue> *dispatch_queue_t;63 64typedef void (^dispatch_block_t)(void);65 66dispatch_queue_t dispatch_get_main_queue(void);67 68DISPATCH_RETURNS_RETAINED dispatch_data_t69dispatch_data_create(const void *buffer, size_t size,70                     dispatch_queue_t _Nullable queue,71                     dispatch_block_t _Nullable destructor);72 73void _dispatch_object_validate(dispatch_object_t object);74 75#define dispatch_retain(object) \76  __extension__({ dispatch_object_t _o = (object); \77                  _dispatch_object_validate(_o); \78                  (void)[_o retain]; })79#define dispatch_release(object) \80  __extension__({ dispatch_object_t _o = (object); \81                  _dispatch_object_validate(_o); \82                  [_o release]; })83 84 85@interface SomeClass86@end87 88@implementation SomeClass89- (NSDictionary *)copyTestWithBridgeReturningRetainable:(NSData *)plistData {90  CFErrorRef error;91  CFDictionaryRef testDict = CFPropertyListCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)plistData, 0, 0, &error);92#if HAS_ARC93      // expected-note@-2 {{Call to function 'CFPropertyListCreateWithData' returns a Core Foundation object of type 'CFPropertyListRef' with a +1 retain count}}94#endif95  return (__bridge NSDictionary *)testDict;96#if HAS_ARC97      // 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}}99#endif100}101 102- (NSDictionary *)copyTestWithoutBridgeReturningRetainable:(NSData *)plistData {103  NSDictionary *testDict = [[NSDictionary alloc] init];104  return testDict; // no-warning105}106 107- (NSDictionary *)copyTestWithBridgeTransferReturningRetainable:(NSData *)plistData {108  CFErrorRef error;109  CFDictionaryRef testDict = CFPropertyListCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)plistData, 0, 0, &error);110  return (__bridge_transfer NSDictionary *)testDict; // no-warning under ARC111#if !HAS_ARC112      // expected-warning@-2 {{'__bridge_transfer' casts have no effect when not using ARC}} // Warning from Sema113#endif114}115 116- (CFDictionaryRef)copyTestReturningCoreFoundation:(NSData *)plistData {117  CFErrorRef error;118  CFDictionaryRef testDict = CFPropertyListCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)plistData, 0, 0, &error);119  return testDict;120}121@end122 123int buf[1024];124 125void libdispatch_leaked_data(void) {126  dispatch_data_t data = dispatch_data_create(buf, 1024,127                                              dispatch_get_main_queue(), ^{});128}129#if !HAS_ARC130  // 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}}133#endif134 135void libdispatch_dispatch_released_data(void) {136  dispatch_data_t data = dispatch_data_create(buf, 1024,137                                              dispatch_get_main_queue(), ^{});138#if !HAS_ARC139  dispatch_release(data); // no-warning140#endif141}142 143void libdispatch_objc_released_data(void) {144  dispatch_data_t data = dispatch_data_create(buf, 1024,145                                              dispatch_get_main_queue(), ^{});146#if !HAS_ARC147  [data release]; // no-warning148#endif149}150 151void libdispatch_leaked_retained_data(void) {152  dispatch_data_t data = dispatch_data_create(buf, 1024,153                                              dispatch_get_main_queue(), ^{});154#if !HAS_ARC155  dispatch_retain(data);156  [data release];157#endif158}159#if !HAS_ARC160// 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}}165#endif166