From: George Karpenkov Date: Fri, 1 Feb 2019 02:13:02 +0000 (+0000) Subject: [analyzer] [RetainCountChecker] Fix object type for CF/Obj-C bridged casts X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1f33898962cf3a3c9f44736e14f269938af9248d;p=clang [analyzer] [RetainCountChecker] Fix object type for CF/Obj-C bridged casts Having an incorrect type for a cast causes the checker to incorrectly dismiss the operation under ARC, leading to a false positive use-after-release on the test. rdar://47709885 Differential Revision: https://reviews.llvm.org/D57557 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352824 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp b/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp index 2fe0d286bb..9d2d8cd82a 100644 --- a/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp @@ -185,7 +185,16 @@ void RetainCountChecker::checkPostStmt(const CastExpr *CE, if (!BE) return; - ArgEffect AE = ArgEffect(IncRef, ObjKind::ObjC); + QualType QT = CE->getType(); + ObjKind K; + if (coreFoundation::isCFObjectRef(QT)) { + K = ObjKind::CF; + } else { + assert(cocoa::isCocoaObjectRef(QT)); + K = ObjKind::ObjC; + } + + ArgEffect AE = ArgEffect(IncRef, K); switch (BE->getBridgeKind()) { case OBC_Bridge: diff --git a/test/Analysis/objc-arc.m b/test/Analysis/objc-arc.m index 08fca7659c..c5549fae84 100644 --- a/test/Analysis/objc-arc.m +++ b/test/Analysis/objc-arc.m @@ -123,7 +123,7 @@ void rdar9424882() { typedef const void *CFTypeRef; typedef const struct __CFString *CFStringRef; -@interface NSString +@interface NSString : NSObject - (id) self; @end @@ -231,3 +231,16 @@ id rdar14061675() { return result; } +typedef const void * CFTypeRef; +typedef const struct __CFString * CFStringRef; +typedef const struct __CFAllocator * CFAllocatorRef; +extern const CFAllocatorRef kCFAllocatorDefault; + +extern CFTypeRef CFRetain(CFTypeRef cf); +extern void CFRelease(CFTypeRef cf); + +void check_bridge_retained_cast() { + NSString *nsStr = [[NSString alloc] init]; + CFStringRef cfStr = (__bridge_retained CFStringRef)nsStr; + CFRelease(cfStr); // no-warning +}