1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.RetainCount -verify %s
2 // RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.RetainCount -analyzer-inline-max-stack-depth=0 -verify %s
4 #pragma clang arc_cf_code_audited begin
5 typedef const void * CFTypeRef
;
6 extern CFTypeRef
CFRetain(CFTypeRef cf
);
7 extern void CFRelease(CFTypeRef cf
);
8 #pragma clang arc_cf_code_audited end
10 #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
11 #define CF_CONSUMED __attribute__((cf_consumed))
13 extern CFTypeRef
CFCreate(void) CF_RETURNS_RETAINED
;
15 // A "safe" variant of CFRetain that doesn't crash when a null pointer is
16 // retained. This is often defined by users in a similar manner. The
17 // CF_RETURNS_RETAINED annotation is misleading here, because the function
18 // is not supposed to return an object with a +1 retain count. Instead, it
19 // is supposed to return an object with +(N+1) retain count, where N is
20 // the original retain count of 'cf'. However, there is no good annotation
21 // to use in this case, and it is pointless to provide such annotation
22 // because the only use cases would be CFRetain and SafeCFRetain.
23 // So instead we teach the analyzer to be able to accept such code
24 // and ignore the misplaced annotation.
25 CFTypeRef
SafeCFRetain(CFTypeRef cf
) CF_RETURNS_RETAINED
{
32 // A "safe" variant of CFRelease that doesn't crash when a null pointer is
33 // released. The CF_CONSUMED annotation seems reasonable here.
34 void SafeCFRelease(CFTypeRef CF_CONSUMED cf
) {
36 CFRelease(cf
); // no-warning (when inlined)
39 // The same thing, just with a different naming style.
40 CFTypeRef
retainCFType(CFTypeRef cf
) CF_RETURNS_RETAINED
{
47 void releaseCFType(CFTypeRef CF_CONSUMED cf
) {
49 CFRelease(cf
); // no-warning (when inlined)
52 void escape(CFTypeRef cf
);
54 void makeSureTestsWork(void) {
55 CFTypeRef cf
= CFCreate();
57 CFRelease(cf
); // expected-warning{{Reference-counted object is used after it is released}}
60 // Make sure we understand that the second SafeCFRetain doesn't return an
61 // object with +1 retain count, which we won't be able to release twice.
62 void falseOverrelease(CFTypeRef cf
) {
66 SafeCFRelease(cf
); // no-warning after inlining this.
69 // Regular CFRelease() should behave similarly.
70 void sameWithNormalRelease(CFTypeRef cf
) {
74 CFRelease(cf
); // no-warning
77 // Make sure we understand that the second SafeCFRetain doesn't return an
78 // object with +1 retain count, which would no longer be owned by us after
79 // it escapes to escape() and released once.
80 void falseReleaseNotOwned(CFTypeRef cf
) {
85 SafeCFRelease(cf
); // no-warning after inlining this.
88 void testTheOtherNamingConvention(CFTypeRef cf
) {
92 releaseCFType(cf
); // no-warning