Skip to content

Commit 517605c

Browse files
authored
[alpha.webkit.UnretainedCallArgsChecker] Add the support for RetainPtrArc (#135532)
WebKit uses #define to rename RetainPtr to RetainPtrArc so add the support for it.
1 parent e676866 commit 517605c

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ bool isRefType(const std::string &Name) {
119119
Name == "RefPtr" || Name == "RefPtrAllowingPartiallyDestroyed";
120120
}
121121

122-
bool isRetainPtr(const std::string &Name) { return Name == "RetainPtr"; }
122+
bool isRetainPtr(const std::string &Name) {
123+
return Name == "RetainPtr" || Name == "RetainPtrArc";
124+
}
123125

124126
bool isCheckedPtr(const std::string &Name) {
125127
return Name == "CheckedPtr" || Name == "CheckedRef";
@@ -157,7 +159,8 @@ bool isCtorOfCheckedPtr(const clang::FunctionDecl *F) {
157159
bool isCtorOfRetainPtr(const clang::FunctionDecl *F) {
158160
const std::string &FunctionName = safeGetName(F);
159161
return FunctionName == "RetainPtr" || FunctionName == "adoptNS" ||
160-
FunctionName == "adoptCF" || FunctionName == "retainPtr";
162+
FunctionName == "adoptCF" || FunctionName == "retainPtr" ||
163+
FunctionName == "RetainPtrArc" || FunctionName == "adoptNSArc";
161164
}
162165

163166
bool isCtorOfSafePtr(const clang::FunctionDecl *F) {
@@ -190,7 +193,7 @@ bool isRefOrCheckedPtrType(const clang::QualType T) {
190193
}
191194

192195
bool isRetainPtrType(const clang::QualType T) {
193-
return isPtrOfType(T, [](auto Name) { return Name == "RetainPtr"; });
196+
return isPtrOfType(T, [](auto Name) { return isRetainPtr(Name); });
194197
}
195198

196199
bool isOwnerPtrType(const clang::QualType T) {
@@ -374,7 +377,7 @@ std::optional<bool> isGetterOfSafePtr(const CXXMethodDecl *M) {
374377
method == "impl"))
375378
return true;
376379

377-
if (className == "RetainPtr" && method == "get")
380+
if (isRetainPtr(className) && method == "get")
378381
return true;
379382

380383
// Ref<T> -> T conversion
@@ -395,7 +398,7 @@ std::optional<bool> isGetterOfSafePtr(const CXXMethodDecl *M) {
395398
}
396399
}
397400

398-
if (className == "RetainPtr") {
401+
if (isRetainPtr(className)) {
399402
if (auto *maybeRefToRawOperator = dyn_cast<CXXConversionDecl>(M)) {
400403
auto QT = maybeRefToRawOperator->getConversionType();
401404
auto *T = QT.getTypePtrOrNull();
@@ -429,7 +432,7 @@ bool isCheckedPtr(const CXXRecordDecl *R) {
429432
bool isRetainPtr(const CXXRecordDecl *R) {
430433
assert(R);
431434
if (auto *TmplR = R->getTemplateInstantiationPattern())
432-
return safeGetName(TmplR) == "RetainPtr";
435+
return isRetainPtr(safeGetName(TmplR));
433436
return false;
434437
}
435438

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class RetainPtrCtorAdoptChecker
7171
}
7272

7373
bool TraverseClassTemplateDecl(ClassTemplateDecl *CTD) {
74-
if (safeGetName(CTD) == "RetainPtr")
74+
if (isRetainPtr(safeGetName(CTD)))
7575
return true; // Skip the contents of RetainPtr.
7676
return Base::TraverseClassTemplateDecl(CTD);
7777
}
@@ -193,7 +193,7 @@ class RetainPtrCtorAdoptChecker
193193
if (!Cls)
194194
return;
195195

196-
if (safeGetName(Cls) != "RetainPtr" || !CE->getNumArgs())
196+
if (!isRetainPtr(safeGetName(Cls)) || !CE->getNumArgs())
197197
return;
198198

199199
// Ignore RetainPtr construction inside adoptNS, adoptCF, and retainPtr.
@@ -322,12 +322,12 @@ class RetainPtrCtorAdoptChecker
322322
if (auto *CD = dyn_cast<CXXConversionDecl>(MD)) {
323323
auto QT = CD->getConversionType().getCanonicalType();
324324
auto *ResultType = QT.getTypePtrOrNull();
325-
if (safeGetName(Cls) == "RetainPtr" && ResultType &&
325+
if (isRetainPtr(safeGetName(Cls)) && ResultType &&
326326
(ResultType->isPointerType() || ResultType->isReferenceType() ||
327327
ResultType->isObjCObjectPointerType()))
328328
return IsOwnedResult::NotOwned;
329329
}
330-
if (safeGetName(MD) == "leakRef" && safeGetName(Cls) == "RetainPtr")
330+
if (safeGetName(MD) == "leakRef" && isRetainPtr(safeGetName(Cls)))
331331
return IsOwnedResult::Owned;
332332
}
333333
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef const struct CF_BRIDGED_TYPE(NSString) __CFString * CFStringRef;
1717
typedef const struct CF_BRIDGED_TYPE(NSArray) __CFArray * CFArrayRef;
1818
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableArray) __CFArray * CFMutableArrayRef;
1919
typedef struct CF_BRIDGED_MUTABLE_TYPE(CFRunLoopRef) __CFRunLoop * CFRunLoopRef;
20+
typedef struct CF_BRIDGED_TYPE(id) CGImage *CGImageRef;
2021

2122
#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
2223
#define CF_CONSUMED __attribute__((cf_consumed))
@@ -150,6 +151,10 @@ namespace WTF {
150151

151152
void WTFCrash(void);
152153

154+
#if __has_feature(objc_arc)
155+
#define RetainPtr RetainPtrArc
156+
#endif
157+
153158
template<typename T> class RetainPtr;
154159
template<typename T> RetainPtr<T> adoptNS(T*);
155160
template<typename T> RetainPtr<T> adoptCF(T);

clang/test/Analysis/Checkers/WebKit/unretained-call-args-arc.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
SomeObj *provide();
66
CFMutableArrayRef provide_cf();
77
void someFunction();
8+
CGImageRef provideImage();
9+
NSString *stringForImage(CGImageRef);
810

911
namespace raw_ptr {
1012

@@ -36,4 +38,13 @@ - (SomeObj *)getSomeObj {
3638
- (void)doWorkOnSomeObj {
3739
[[self getSomeObj] doWork];
3840
}
41+
42+
- (CGImageRef)createImage {
43+
return provideImage();
44+
}
45+
46+
- (NSString *)convertImage {
47+
RetainPtr<CGImageRef> image = [self createImage];
48+
return stringForImage(image.get());
49+
}
3950
@end

clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
CFMutableArrayRef provide_cf();
1010
void consume_cf(CFMutableArrayRef);
1111

12+
CGImageRef provideImage();
13+
NSString *stringForImage(CGImageRef);
14+
1215
void some_function();
1316

1417
namespace simple {
@@ -440,4 +443,12 @@ - (void)doWorkOnSomeObj {
440443
[[self getSomeObj] doWork];
441444
}
442445

446+
- (CGImageRef)createImage {
447+
return provideImage();
448+
}
449+
450+
- (NSString *)convertImage {
451+
RetainPtr<CGImageRef> image = [self createImage];
452+
return stringForImage(image.get());
453+
}
443454
@end

0 commit comments

Comments
 (0)