blob: cb53bb1c66b64f672cf2bdb628b0cdeb832ab0e5 [file] [log] [blame]
Ted Kremenek4b5484a2008-06-16 19:53:46 +00001// RUN: clang -checker-cfref -pedantic -verify %s
Ted Kremenek7662af42008-06-16 19:35:31 +00002
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 Kremenekdfc996c2008-06-16 19:51:41 +00009// both svelte and portable to non-Mac platforms.
Ted Kremenek7662af42008-06-16 19:35:31 +000010//===----------------------------------------------------------------------===//
11
Ted Kremenek4b5484a2008-06-16 19:53:46 +000012typedef unsigned long UInt32;
13typedef unsigned char Boolean;
Ted Kremenek7662af42008-06-16 19:35:31 +000014typedef signed long CFIndex;
Ted Kremenek4b5484a2008-06-16 19:53:46 +000015typedef const void * CFTypeRef;
Ted Kremenek7662af42008-06-16 19:35:31 +000016typedef const struct __CFString * CFStringRef;
Ted Kremenek4b5484a2008-06-16 19:53:46 +000017typedef struct { CFIndex location; } CFRange;
18typedef const struct __CFAllocator * CFAllocatorRef;
19extern void CFRelease(CFTypeRef cf);
20typedef Boolean (*CFArrayEqualCallBack)(const void *value1, const void *value2);
21typedef struct { CFArrayEqualCallBack equal; } CFArrayCallBacks;
Ted Kremenek7662af42008-06-16 19:35:31 +000022extern const CFArrayCallBacks kCFTypeArrayCallBacks;
23typedef const struct __CFArray * CFArrayRef;
24typedef struct __CFArray * CFMutableArrayRef;
Ted Kremenek4b5484a2008-06-16 19:53:46 +000025extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
Ted Kremenek7662af42008-06-16 19:35:31 +000026extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
Ted Kremenek4b5484a2008-06-16 19:53:46 +000027extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
28typedef UInt32 CFStringEncoding;
Ted Kremenek7662af42008-06-16 19:35:31 +000029enum { 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 Kremenek4b5484a2008-06-16 19:53:46 +000030extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
Ted Kremenek7662af42008-06-16 19:35:31 +000031
32//===----------------------------------------------------------------------===//
33// Test cases.
34//===----------------------------------------------------------------------===//
35
36void 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