[clang] Ignore assignment to Ref / RefPtr in alpha.webkit.UncountedCallArgsChecker. (PR #80810)

via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 6 01:15:53 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: Ryosuke Niwa (rniwa)

<details>
<summary>Changes</summary>



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


2 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp (+9) 
- (added) clang/test/Analysis/Checkers/WebKit/assignment-to-refptr.cpp (+71) 


``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
index 407b6ba7a7642..b9ccf46f52649 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
@@ -126,6 +126,15 @@ class UncountedCallArgsChecker
     // of object on LHS.
     if (auto *MemberOp = dyn_cast<CXXOperatorCallExpr>(CE)) {
       // Note: assignemnt to built-in type isn't derived from CallExpr.
+      if (MemberOp->getOperator() == OO_Equal) { // Ignore assignment to Ref/RefPtr.
+        auto *callee = MemberOp->getDirectCallee();
+        if (auto *calleeDecl = dyn_cast<CXXMethodDecl>(callee)) {
+          if (const CXXRecordDecl *classDecl = calleeDecl->getParent()) {
+            if (isRefCounted(classDecl))
+              return true;
+          }
+        }
+      }
       if (MemberOp->isAssignmentOp())
         return false;
     }
diff --git a/clang/test/Analysis/Checkers/WebKit/assignment-to-refptr.cpp b/clang/test/Analysis/Checkers/WebKit/assignment-to-refptr.cpp
new file mode 100644
index 0000000000000..c8ad634a5493f
--- /dev/null
+++ b/clang/test/Analysis/Checkers/WebKit/assignment-to-refptr.cpp
@@ -0,0 +1,71 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
+// expected-no-diagnostics
+
+template<typename T>
+class RefPtr {
+public:
+    inline constexpr RefPtr() : m_ptr(nullptr) { }
+    inline RefPtr(T* ptr)
+      : m_ptr(ptr)
+    {
+        if (m_ptr)
+            m_ptr->ref();
+    }
+
+    inline RefPtr(const RefPtr& o)
+        : m_ptr(o.m_ptr)
+    {
+        if (m_ptr)
+            m_ptr->ref();
+    }
+
+    inline ~RefPtr()
+    {
+        if (m_ptr)
+            m_ptr->deref();
+    }
+
+    inline T* operator->() const { return m_ptr; }
+    explicit operator bool() const { return m_ptr; }
+  
+    RefPtr& operator=(const RefPtr&);
+    RefPtr& operator=(T*);
+
+private:
+    T* m_ptr;
+};
+
+template<typename T>
+inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr& o)
+{
+    if (m_ptr)
+        m_ptr->deref();
+    m_ptr = o.m_ptr;
+    if (m_ptr)
+        m_ptr->ref();
+    return *this;
+}
+
+template<typename T>
+inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
+{
+    if (m_ptr)
+        m_ptr->deref();
+    m_ptr = optr;
+    if (m_ptr)
+        m_ptr->ref();
+    return *this;
+}
+
+class Node {
+public:
+    Node* nextSibling() const;
+
+    void ref() const;
+    void deref() const;
+};
+
+static void removeDetachedChildren(Node* firstChild)
+{
+    for (RefPtr child = firstChild; child; child = child->nextSibling());
+}

``````````

</details>


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


More information about the cfe-commits mailing list