[clang] [alpha.webkit.RetainPtrCtorAdoptChecker] Don't treat calling (void)copy:(id) as a leak (PR #179713)
Ryosuke Niwa via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 4 09:39:53 PST 2026
https://github.com/rniwa created https://github.com/llvm/llvm-project/pull/179713
UIResponderStandardEditActions defines (void)copy:(id)sender but this selector should not be treated as a copy operation since it's a "copy" in the sense of application triggering copy & paste for the system pasteboard.
>From a27e3e30f6774f3f350250ac4221df26e1b8b874 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Wed, 4 Feb 2026 09:36:34 -0800
Subject: [PATCH] [alpha.webkit.RetainPtrCtorAdoptChecker] Don't treat calling
(void)copy:(id) as a leak
UIResponderStandardEditActions defines (void)copy:(id)sender but this selector should
not be treated as a copy operation since it's a "copy" in the sense of application
triggering copy & paste for the system pasteboard.
---
.../StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp | 9 ++++++++-
.../WebKit/retain-ptr-ctor-adopt-use-arc.mm | 14 ++++++++++++++
.../Checkers/WebKit/retain-ptr-ctor-adopt-use.mm | 14 ++++++++++++++
3 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
index 15971168934b58..f9d35c595682b0 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
@@ -337,8 +337,15 @@ bool isAllocInit(const Expr *E, const Expr **InnerExpr) {
auto NameForFirstSlot = Selector.getNameForSlot(0);
if (NameForFirstSlot.starts_with("alloc") ||
NameForFirstSlot.starts_with("copy") ||
- NameForFirstSlot.starts_with("mutableCopy"))
+ NameForFirstSlot.starts_with("mutableCopy")) {
+ if (auto *MD = ObjCMsgExpr->getMethodDecl()) {
+ if (auto *T = MD->getReturnType().getTypePtrOrNull()) {
+ if (T->isVoidType())
+ return false;
+ }
+ }
return true;
+ }
if (!NameForFirstSlot.starts_with("init") &&
!NameForFirstSlot.starts_with("_init"))
return false;
diff --git a/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm b/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm
index 47203cbd273554..609b274ce63b43 100644
--- a/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm
+++ b/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm
@@ -69,6 +69,9 @@ - (SomeObj *)copyWithValue:(int)value {
return copy;
}
+- (void)copy:(id)sender {
+}
+
- (void)doWork {
_number = [[NSNumber alloc] initWithInt:5];
}
@@ -99,6 +102,17 @@ - (void)setValue:(NSNumber *)value {
@end;
+ at interface SubObj : SomeObj
+ at end
+
+ at implementation SubObj
+
+- (void)copy:(id)sender {
+ [super copy:sender];
+}
+
+ at end
+
RetainPtr<CVPixelBufferRef> cf_out_argument() {
auto surface = adoptCF(IOSurfaceCreate(nullptr));
CVPixelBufferRef rawBuffer = nullptr;
diff --git a/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm b/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm
index 427affdbbd6010..20f951b27a149a 100644
--- a/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm
+++ b/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm
@@ -76,6 +76,9 @@ - (SomeObj *)copyWithValue:(int)value {
return copy;
}
+- (void)copy:(id)sender {
+}
+
- (void)doWork {
_number = [[NSNumber alloc] initWithInt:5];
}
@@ -114,6 +117,17 @@ - (id)copyWithZone:(NSZone *)zone {
@end;
+ at interface SubObj : SomeObj
+ at end
+
+ at implementation SubObj
+
+- (void)copy:(id)sender {
+ [super copy:sender];
+}
+
+ at end
+
RetainPtr<CVPixelBufferRef> cf_out_argument() {
auto surface = adoptCF(IOSurfaceCreate(nullptr));
CVPixelBufferRef rawBuffer = nullptr;
More information about the cfe-commits
mailing list