Skip to content

Commit 6f2668d

Browse files
rniwamahesh-attarde
authored andcommitted
[alpha.webkit.RetainPtrCtorAdoptChecker] Allow leakRef in copy methods (llvm#160986)
Allow leakRef() in the return statement of an Objective-C copy method and other methods which return +1.
1 parent c498ced commit 6f2668d

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ class RetainPtrCtorAdoptChecker
385385
if (RTC.isUnretained(RetValue->getType()))
386386
return;
387387
}
388+
if (retainsRet && *retainsRet) {
389+
CreateOrCopyFnCall.insert(RetValue);
390+
return;
391+
}
388392
if (auto *CE = dyn_cast<CallExpr>(RetValue)) {
389393
auto *Callee = CE->getDirectCallee();
390394
if (!Callee || !isCreateOrCopyFunction(Callee))

clang/test/Analysis/Checkers/WebKit/objc-mock-types.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ template<typename T> typename remove_reference<T>::type&& move(T&& t);
1717

1818
#endif
1919

20+
namespace std {
21+
22+
template <bool, typename U = void> struct enable_if {
23+
};
24+
25+
template <typename T> struct enable_if<true, T> {
26+
using type = T;
27+
};
28+
29+
template <bool value, class T = void>
30+
using enable_if_t = typename enable_if<value, T>::type;
31+
32+
}
33+
2034
@class NSString;
2135
@class NSArray;
2236
@class NSMutableArray;
@@ -100,6 +114,7 @@ id CFBridgingRelease(CFTypeRef X) {
100114
__attribute__((objc_root_class))
101115
@interface NSObject
102116
+ (instancetype) alloc;
117+
+ (instancetype) allocWithZone:(NSZone *)zone;
103118
+ (Class) class;
104119
+ (Class) superclass;
105120
- (instancetype) init;
@@ -232,6 +247,14 @@ template <typename T> struct RemovePointer<T*> {
232247
typedef T Type;
233248
};
234249

250+
template <typename T> struct IsPointer {
251+
static constexpr bool value = false;
252+
};
253+
254+
template <typename T> struct IsPointer<T*> {
255+
static constexpr bool value = true;
256+
};
257+
235258
template <typename T> struct RetainPtr {
236259
using ValueType = typename RemovePointer<T>::Type;
237260
using PtrType = ValueType*;
@@ -285,12 +308,23 @@ template <typename T> struct RetainPtr {
285308
PtrType operator->() const { return t; }
286309
T &operator*() const { return *t; }
287310
RetainPtr &operator=(PtrType t);
288-
PtrType leakRef()
311+
312+
template <typename U = PtrType>
313+
std::enable_if_t<IsPointer<U>::value, U> leakRef() CF_RETURNS_RETAINED
314+
{
315+
PtrType s = t;
316+
t = nullptr;
317+
return s;
318+
}
319+
320+
template <typename U = PtrType>
321+
std::enable_if_t<!IsPointer<U>::value, U> leakRef() NS_RETURNS_RETAINED
289322
{
290323
PtrType s = t;
291324
t = nullptr;
292325
return s;
293326
}
327+
294328
operator PtrType() const { return t; }
295329
operator bool() const { return t; }
296330

clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ - (void)setValue:value {
104104
_number = value;
105105
}
106106

107+
- (id)copyWithZone:(NSZone *)zone {
108+
auto copy = adoptNS([(SomeObj *)[SomeObj allocWithZone:zone] init]);
109+
[copy setValue:_number];
110+
[copy setNext:_next];
111+
[copy setOther:_other];
112+
return copy.leakRef();
113+
}
114+
107115
@end;
108116

109117
RetainPtr<CVPixelBufferRef> cf_out_argument() {
@@ -151,7 +159,7 @@ CFTypeRef LeakWrapper() {
151159

152160
extern Class (*getNSArrayClass)();
153161
NSArray *allocArrayInstance() NS_RETURNS_RETAINED {
154-
return [[getNSArrayClass() alloc] init];
162+
return adoptNS([[getNSArrayClass() alloc] init]).leakRef();
155163
}
156164

157165
extern int (*GetObj)(CF_RETURNS_RETAINED CFTypeRef* objOut);
@@ -294,7 +302,7 @@ -(NSString *)leak_string {
294302
}
295303

296304
-(NSString *)make_string {
297-
return [[NSString alloc] initWithUTF8String:"hello"];
305+
return adoptNS([[NSString alloc] initWithUTF8String:"hello"]).leakRef();
298306
}
299307

300308
-(void)local_leak_string {

0 commit comments

Comments
 (0)