[clang] [alpha.webkit.NoUnretainedMemberChecker] Only check @property when @implementation is seen (PR #159947)

via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 20 14:17:19 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Ryosuke Niwa (rniwa)

<details>
<summary>Changes</summary>

A @<!-- -->interface declaration with a raw pointer @<!-- -->property does not necessarily mean it synthesizes ivar of that type. To determine whether such a synthesis happens or not, we must wait for @<!-- -->implementation to appear. So this PR makes the checker only validate @<!-- -->property then.

---
Full diff: https://github.com/llvm/llvm-project/pull/159947.diff


3 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp (+9-10) 
- (modified) clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm (+15) 
- (modified) clang/test/Analysis/Checkers/WebKit/unretained-members.mm (+15) 


``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp
index 8faf6a219450a..baafc1bc61c15 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp
@@ -129,17 +129,16 @@ class RawPtrRefMemberChecker
     if (BR->getSourceManager().isInSystemHeader(CD->getLocation()))
       return;
 
-    ObjCContainerDecl::PropertyMap map;
-    CD->collectPropertiesToImplement(map);
-    for (auto it : map)
-      visitObjCPropertyDecl(CD, it.second);
-
-    if (auto *ID = dyn_cast<ObjCInterfaceDecl>(CD)) {
-      for (auto *Ivar : ID->ivars())
-        visitIvarDecl(CD, Ivar);
-      return;
-    }
     if (auto *ID = dyn_cast<ObjCImplementationDecl>(CD)) {
+      ObjCContainerDecl::PropertyMap map;
+      CD->collectPropertiesToImplement(map);
+      for (auto it : map)
+        visitObjCPropertyDecl(CD, it.second);
+
+      if (auto *Interface = ID->getClassInterface()) {
+        for (auto *Ivar : Interface->ivars())
+          visitIvarDecl(CD, Ivar);
+      }
       for (auto *PropImpl : ID->property_impls())
         visitPropImpl(CD, PropImpl);
       for (auto *Ivar : ID->ivars())
diff --git a/clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm b/clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm
index 00e6e6ec1dcfa..daa0c222251d9 100644
--- a/clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm
+++ b/clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm
@@ -76,6 +76,21 @@ @interface AnotherObject : NSObject {
 @property(nonatomic, unsafe_unretained) NSString *prop_string3;
 // expected-warning at -1{{Property 'prop_string3' in 'AnotherObject' is a raw pointer to retainable type 'NSString'; member variables must be a RetainPtr}}
 @property(nonatomic, readonly) NSString *prop_string4;
+ at property(nonatomic, readonly) NSString *prop_safe;
+ at end
+
+ at implementation AnotherObject
+- (NSString *)prop_safe {
+  return nil;
+}
+ at end
+
+// No warnings for @interface declaration itself. 
+ at interface InterfaceOnlyObject : NSObject
+ at property(nonatomic, strong) NSString *prop_string1;
+ at property(nonatomic, assign) NSString *prop_string2;
+ at property(nonatomic, unsafe_unretained) NSString *prop_string3;
+ at property(nonatomic, readonly) NSString *prop_string4;
 @end
 
 NS_REQUIRES_PROPERTY_DEFINITIONS
diff --git a/clang/test/Analysis/Checkers/WebKit/unretained-members.mm b/clang/test/Analysis/Checkers/WebKit/unretained-members.mm
index 46f65dfa603ad..800d882f79696 100644
--- a/clang/test/Analysis/Checkers/WebKit/unretained-members.mm
+++ b/clang/test/Analysis/Checkers/WebKit/unretained-members.mm
@@ -98,6 +98,21 @@ @interface AnotherObject : NSObject {
 }
 @property(nonatomic, strong) NSString *prop_string;
 // expected-warning at -1{{Property 'prop_string' in 'AnotherObject' is a raw pointer to retainable type 'NSString'; member variables must be a RetainPtr}}
+ at property(nonatomic, readonly) NSString *prop_safe;
+ at end
+
+ at implementation AnotherObject
+- (NSString *)prop_safe {
+  return nil;
+}
+ at end
+
+// No warnings for @interface declaration itself. 
+ at interface InterfaceOnlyObject : NSObject
+ at property(nonatomic, strong) NSString *prop_string1;
+ at property(nonatomic, assign) NSString *prop_string2;
+ at property(nonatomic, unsafe_unretained) NSString *prop_string3;
+ at property(nonatomic, readonly) NSString *prop_string4;
 @end
 
 NS_REQUIRES_PROPERTY_DEFINITIONS

``````````

</details>


https://github.com/llvm/llvm-project/pull/159947


More information about the cfe-commits mailing list