Skip to content

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Sep 27, 2025

Add the support for recognizing smart pointer type appearing as the type of the object pointer in CXXDependentScopeMemberExpr.

@rniwa rniwa requested a review from t-rasmud September 27, 2025 21:29
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Sep 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 27, 2025

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

@llvm/pr-subscribers-clang

Author: Ryosuke Niwa (rniwa)

Changes

Add the support for recognizing smart pointer type appearing as the type of the object pointer in CXXDependentScopeMemberExpr.


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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp (+8)
  • (modified) clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm (+29)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
index 00a1b8b6e7e89..0bc7cb9db8272 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
@@ -165,6 +165,14 @@ bool tryToFindPtrOrigin(
           if (isSingleton(E->getFoundDecl()))
             return callback(E, true);
         }
+
+        if (auto *MemberExpr = dyn_cast<CXXDependentScopeMemberExpr>(CalleeE)) {
+          auto *Base = MemberExpr->getBase();
+          auto MemberName = MemberExpr->getMember().getAsString();
+          bool IsGetter = MemberName == "get" || MemberName == "ptr";
+          if (Base && isSafePtrType(Base->getType()) && IsGetter)
+            return callback(E, true);
+        }
       }
 
       // Sometimes, canonical type erroneously turns Ref<T> into T.
diff --git a/clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm b/clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm
index c9d2fe861bb49..111a22d6c8b73 100644
--- a/clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm
+++ b/clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm
@@ -561,6 +561,35 @@ void foo() {
 
 } // namespace ns_retained_return_value
 
+namespace template_function {
+
+class Base {
+public:
+    virtual ~Base() = default;
+    void send(dispatch_queue_t) const;
+    void ref() const;
+    void deref() const;
+};
+
+template<typename Traits>
+class Derived : public Base {
+public:
+    virtual ~Derived() = default;
+
+    void send(typename Traits::MessageType) const;
+
+    virtual OSObjectPtr<dispatch_queue_t> msg(typename Traits::MessageType) const = 0;
+};
+
+template<typename Traits>
+void Derived<Traits>::send(typename Traits::MessageType messageType) const
+{
+    OSObjectPtr dictionary = msg(messageType);
+    Base::send(dictionary.get());
+}
+
+} // namespace template_function
+
 @interface TestObject : NSObject
 - (void)doWork:(NSString *)msg, ...;
 - (void)doWorkOnSelf;

@rniwa rniwa force-pushed the fix-webkit-CXXDependentScopeMemberExpr-getter branch from b2f3e37 to 9bdb0cf Compare October 15, 2025 19:10
…smart pointer type as safe

Add the support for recognizing smart pointer type appearing as the type of the object pointer
in CXXDependentScopeMemberExpr.
@rniwa rniwa force-pushed the fix-webkit-CXXDependentScopeMemberExpr-getter branch from 9bdb0cf to 2d45c82 Compare October 15, 2025 21:33
Copy link
Contributor

@t-rasmud t-rasmud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rniwa
Copy link
Contributor Author

rniwa commented Oct 16, 2025

Thanks for the review!

@rniwa rniwa merged commit 65c895d into llvm:main Oct 16, 2025
10 checks passed
@rniwa rniwa deleted the fix-webkit-CXXDependentScopeMemberExpr-getter branch October 16, 2025 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:static analyzer clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants