1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount,alpha.core -analyzer-constraints=range -analyzer-store=region -fobjc-gc -verify %s 2 3 typedef const void * CFTypeRef; 4 typedef const struct __CFString * CFStringRef; 5 typedef const struct __CFAllocator * CFAllocatorRef; 6 typedef const struct __CFDictionary * CFDictionaryRef; 7 CFTypeRef CFMakeCollectable(CFTypeRef cf) ; 8 extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...); 9 typedef signed char BOOL; 10 typedef unsigned int NSUInteger; 11 typedef struct _NSZone NSZone; 12 @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 13 @protocol NSObject 14 - (BOOL)isEqual:(id)object; 15 - (id)autorelease; 16 @end 17 @protocol NSCopying 18 - (id)copyWithZone:(NSZone *)zone; 19 @end @protocol NSMutableCopying 20 - (id)mutableCopyWithZone:(NSZone *)zone; 21 @end 22 @protocol 23 NSCoding 24 - (void)encodeWithCoder:(NSCoder *)aCoder; 25 @end 26 @interface NSObject <NSObject> {} 27 - (id)init; 28 + (id)alloc; 29 @end 30 enum { NSASCIIStringEncoding = 1, NSNEXTSTEPStringEncoding = 2, NSJapaneseEUCStringEncoding = 3, NSUTF8StringEncoding = 4, NSISOLatin1StringEncoding = 5, NSSymbolStringEncoding = 6, NSNonLossyASCIIStringEncoding = 7, NSShiftJISStringEncoding = 8, NSISOLatin2StringEncoding = 9, NSUnicodeStringEncoding = 10, NSWindowsCP1251StringEncoding = 11, NSWindowsCP1252StringEncoding = 12, NSWindowsCP1253StringEncoding = 13, NSWindowsCP1254StringEncoding = 14, NSWindowsCP1250StringEncoding = 15, NSISO2022JPStringEncoding = 21, NSMacOSRomanStringEncoding = 30, NSUTF16StringEncoding = NSUnicodeStringEncoding, NSUTF16BigEndianStringEncoding = 0x90000100, NSUTF16LittleEndianStringEncoding = 0x94000100, NSUTF32StringEncoding = 0x8c000100, NSUTF32BigEndianStringEncoding = 0x98000100, NSUTF32LittleEndianStringEncoding = 0x9c000100 }; 31 typedef NSUInteger NSStringEncoding; 32 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> 33 - (NSUInteger)length; 34 - (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer; 35 @end 36 @interface NSAutoreleasePool : NSObject {} 37 - (void)drain; 38 @end 39 extern NSString * const NSXMLParserErrorDomain ; 40 41 // The actual test case. UTIL_AUTORELEASE_CF_AS_ID is a macro that doesn't 42 // actually do what it was intended to. 43 44 #define NSSTRINGWRAPPER(bytes,len) \ 45 [[[NSString alloc] initWithBytesNoCopy: (void*)(bytes) length: (len) encoding: NSUTF8StringEncoding freeWhenDone: (BOOL)0] autorelease] 46 47 #define UTIL_AUTORELEASE_CF_AS_ID(cf) ( (((void*)0) == (cf)) ? ((void*)0) : [(id) CFMakeCollectable( (CFTypeRef) cf) autorelease] ) 48 49 #define UTIL_AUTORELEASE_CF_AS_ID_WITHOUT_TEST(cf) ( [(id) CFMakeCollectable( (CFTypeRef) cf) autorelease] ) 50 51 static char *lorem = "fooBarBaz"; 52 53 void NSLog(NSString *, ...); 54 55 int main (int argc, const char * argv[]) { 56 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 57 NSString *tmp1 = NSSTRINGWRAPPER(lorem, 6); // no-warning 58 NSString *tmp2 = UTIL_AUTORELEASE_CF_AS_ID( CFStringCreateWithFormat(((void*)0), ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "lorem: %@" "")), tmp1) ); // expected-warning 2 {{leak}} 59 NSString *tmp3 = UTIL_AUTORELEASE_CF_AS_ID_WITHOUT_TEST( CFStringCreateWithFormat(((void*)0), ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "lorem: %@" "")), tmp1) ); 60 NSLog(@"tmp2: %@ tmp3: %@", tmp2, tmp3); 61 [pool drain]; 62 return 0; 63 } 64