Skip to content

[alpha.webkit.UnretainedCallArgsChecker] Add the support for RetainPtrArc #135532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ bool isRefType(const std::string &Name) {
Name == "RefPtr" || Name == "RefPtrAllowingPartiallyDestroyed";
}

bool isRetainPtr(const std::string &Name) { return Name == "RetainPtr"; }
bool isRetainPtr(const std::string &Name) {
return Name == "RetainPtr" || Name == "RetainPtrArc";
}

bool isCheckedPtr(const std::string &Name) {
return Name == "CheckedPtr" || Name == "CheckedRef";
Expand Down Expand Up @@ -157,7 +159,8 @@ bool isCtorOfCheckedPtr(const clang::FunctionDecl *F) {
bool isCtorOfRetainPtr(const clang::FunctionDecl *F) {
const std::string &FunctionName = safeGetName(F);
return FunctionName == "RetainPtr" || FunctionName == "adoptNS" ||
FunctionName == "adoptCF" || FunctionName == "retainPtr";
FunctionName == "adoptCF" || FunctionName == "retainPtr" ||
FunctionName == "RetainPtrArc" || FunctionName == "adoptNSArc";
}

bool isCtorOfSafePtr(const clang::FunctionDecl *F) {
Expand Down Expand Up @@ -190,7 +193,7 @@ bool isRefOrCheckedPtrType(const clang::QualType T) {
}

bool isRetainPtrType(const clang::QualType T) {
return isPtrOfType(T, [](auto Name) { return Name == "RetainPtr"; });
return isPtrOfType(T, [](auto Name) { return isRetainPtr(Name); });
}

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

if (className == "RetainPtr" && method == "get")
if (isRetainPtr(className) && method == "get")
return true;

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

if (className == "RetainPtr") {
if (isRetainPtr(className)) {
if (auto *maybeRefToRawOperator = dyn_cast<CXXConversionDecl>(M)) {
auto QT = maybeRefToRawOperator->getConversionType();
auto *T = QT.getTypePtrOrNull();
Expand Down Expand Up @@ -429,7 +432,7 @@ bool isCheckedPtr(const CXXRecordDecl *R) {
bool isRetainPtr(const CXXRecordDecl *R) {
assert(R);
if (auto *TmplR = R->getTemplateInstantiationPattern())
return safeGetName(TmplR) == "RetainPtr";
return isRetainPtr(safeGetName(TmplR));
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RetainPtrCtorAdoptChecker
}

bool TraverseClassTemplateDecl(ClassTemplateDecl *CTD) {
if (safeGetName(CTD) == "RetainPtr")
if (isRetainPtr(safeGetName(CTD)))
return true; // Skip the contents of RetainPtr.
return Base::TraverseClassTemplateDecl(CTD);
}
Expand Down Expand Up @@ -193,7 +193,7 @@ class RetainPtrCtorAdoptChecker
if (!Cls)
return;

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

// Ignore RetainPtr construction inside adoptNS, adoptCF, and retainPtr.
Expand Down Expand Up @@ -322,12 +322,12 @@ class RetainPtrCtorAdoptChecker
if (auto *CD = dyn_cast<CXXConversionDecl>(MD)) {
auto QT = CD->getConversionType().getCanonicalType();
auto *ResultType = QT.getTypePtrOrNull();
if (safeGetName(Cls) == "RetainPtr" && ResultType &&
if (isRetainPtr(safeGetName(Cls)) && ResultType &&
(ResultType->isPointerType() || ResultType->isReferenceType() ||
ResultType->isObjCObjectPointerType()))
return IsOwnedResult::NotOwned;
}
if (safeGetName(MD) == "leakRef" && safeGetName(Cls) == "RetainPtr")
if (safeGetName(MD) == "leakRef" && isRetainPtr(safeGetName(Cls)))
return IsOwnedResult::Owned;
}
}
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/objc-mock-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef const struct CF_BRIDGED_TYPE(NSString) __CFString * CFStringRef;
typedef const struct CF_BRIDGED_TYPE(NSArray) __CFArray * CFArrayRef;
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableArray) __CFArray * CFMutableArrayRef;
typedef struct CF_BRIDGED_MUTABLE_TYPE(CFRunLoopRef) __CFRunLoop * CFRunLoopRef;
typedef struct CF_BRIDGED_TYPE(id) CGImage *CGImageRef;

#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
#define CF_CONSUMED __attribute__((cf_consumed))
Expand Down Expand Up @@ -150,6 +151,10 @@ namespace WTF {

void WTFCrash(void);

#if __has_feature(objc_arc)
#define RetainPtr RetainPtrArc
#endif

template<typename T> class RetainPtr;
template<typename T> RetainPtr<T> adoptNS(T*);
template<typename T> RetainPtr<T> adoptCF(T);
Expand Down
11 changes: 11 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/unretained-call-args-arc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
SomeObj *provide();
CFMutableArrayRef provide_cf();
void someFunction();
CGImageRef provideImage();
NSString *stringForImage(CGImageRef);

namespace raw_ptr {

Expand Down Expand Up @@ -36,4 +38,13 @@ - (SomeObj *)getSomeObj {
- (void)doWorkOnSomeObj {
[[self getSomeObj] doWork];
}

- (CGImageRef)createImage {
return provideImage();
}

- (NSString *)convertImage {
RetainPtr<CGImageRef> image = [self createImage];
return stringForImage(image.get());
}
@end
11 changes: 11 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
CFMutableArrayRef provide_cf();
void consume_cf(CFMutableArrayRef);

CGImageRef provideImage();
NSString *stringForImage(CGImageRef);

void some_function();

namespace simple {
Expand Down Expand Up @@ -430,4 +433,12 @@ - (void)doWorkOnSomeObj {
[[self getSomeObj] doWork];
}

- (CGImageRef)createImage {
return provideImage();
}

- (NSString *)convertImage {
RetainPtr<CGImageRef> image = [self createImage];
return stringForImage(image.get());
}
@end
Loading