Ted Kremenek | 4b5484a | 2008-06-16 19:53:46 +0000 | [diff] [blame^] | 1 | // RUN: clang -checker-cfref -pedantic -verify %s |
Ted Kremenek | 7662af4 | 2008-06-16 19:35:31 +0000 | [diff] [blame] | 2 | |
| 3 | //===----------------------------------------------------------------------===// |
| 4 | // The following code is reduced using delta-debugging from |
| 5 | // CoreFoundation.h (Mac OS X). |
| 6 | // |
| 7 | // It includes the basic definitions for the test cases below. |
| 8 | // Not directly including CoreFoundation.h directly makes this test case |
Ted Kremenek | dfc996c | 2008-06-16 19:51:41 +0000 | [diff] [blame] | 9 | // both svelte and portable to non-Mac platforms. |
Ted Kremenek | 7662af4 | 2008-06-16 19:35:31 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Ted Kremenek | 4b5484a | 2008-06-16 19:53:46 +0000 | [diff] [blame^] | 12 | typedef unsigned long UInt32; |
| 13 | typedef unsigned char Boolean; |
Ted Kremenek | 7662af4 | 2008-06-16 19:35:31 +0000 | [diff] [blame] | 14 | typedef signed long CFIndex; |
Ted Kremenek | 4b5484a | 2008-06-16 19:53:46 +0000 | [diff] [blame^] | 15 | typedef const void * CFTypeRef; |
Ted Kremenek | 7662af4 | 2008-06-16 19:35:31 +0000 | [diff] [blame] | 16 | typedef const struct __CFString * CFStringRef; |
Ted Kremenek | 4b5484a | 2008-06-16 19:53:46 +0000 | [diff] [blame^] | 17 | typedef struct { CFIndex location; } CFRange; |
| 18 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 19 | extern void CFRelease(CFTypeRef cf); |
| 20 | typedef Boolean (*CFArrayEqualCallBack)(const void *value1, const void *value2); |
| 21 | typedef struct { CFArrayEqualCallBack equal; } CFArrayCallBacks; |
Ted Kremenek | 7662af4 | 2008-06-16 19:35:31 +0000 | [diff] [blame] | 22 | extern const CFArrayCallBacks kCFTypeArrayCallBacks; |
| 23 | typedef const struct __CFArray * CFArrayRef; |
| 24 | typedef struct __CFArray * CFMutableArrayRef; |
Ted Kremenek | 4b5484a | 2008-06-16 19:53:46 +0000 | [diff] [blame^] | 25 | extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); |
Ted Kremenek | 7662af4 | 2008-06-16 19:35:31 +0000 | [diff] [blame] | 26 | extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); |
Ted Kremenek | 4b5484a | 2008-06-16 19:53:46 +0000 | [diff] [blame^] | 27 | extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); |
| 28 | typedef UInt32 CFStringEncoding; |
Ted Kremenek | 7662af4 | 2008-06-16 19:35:31 +0000 | [diff] [blame] | 29 | enum { kCFStringEncodingMacRoman = 0, kCFStringEncodingWindowsLatin1 = 0x0500, kCFStringEncodingISOLatin1 = 0x0201, kCFStringEncodingNextStepLatin = 0x0B01, kCFStringEncodingASCII = 0x0600, kCFStringEncodingUnicode = 0x0100, kCFStringEncodingUTF8 = 0x08000100, kCFStringEncodingNonLossyASCII = 0x0BFF , kCFStringEncodingUTF16 = 0x0100, kCFStringEncodingUTF16BE = 0x10000100, kCFStringEncodingUTF16LE = 0x14000100, kCFStringEncodingUTF32 = 0x0c000100, kCFStringEncodingUTF32BE = 0x18000100, kCFStringEncodingUTF32LE = 0x1c000100 }; |
Ted Kremenek | 4b5484a | 2008-06-16 19:53:46 +0000 | [diff] [blame^] | 30 | extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); |
Ted Kremenek | 7662af4 | 2008-06-16 19:35:31 +0000 | [diff] [blame] | 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // Test cases. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | void f1() { |
| 37 | |
| 38 | // Create the array. |
| 39 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); |
| 40 | |
| 41 | // Create a string. |
| 42 | CFStringRef s1 = CFStringCreateWithCString(0, "hello world", |
| 43 | kCFStringEncodingUTF8); |
| 44 | |
| 45 | // Add the string to the array. |
| 46 | CFArrayAppendValue(A, s1); |
| 47 | |
| 48 | // Decrement the reference count. |
| 49 | CFRelease(s1); // no-warning |
| 50 | |
| 51 | // Get the string. We don't own it. |
| 52 | s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0); |
| 53 | |
| 54 | // Release the array. |
| 55 | CFRelease(A); // no-warning |
| 56 | |
| 57 | // Release the string. This is a bug. |
| 58 | CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}} |
| 59 | } |
| 60 | |