Skip to content

Ignore assignment to Ref / RefPtr in alpha.webkit.UncountedCallArgsChecker. #80810

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 2 commits into from
Feb 12, 2024

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Feb 6, 2024

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Feb 6, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 6, 2024

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

Author: Ryosuke Niwa (rniwa)

Changes

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 --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());
+}

@llvmbot
Copy link
Member

llvmbot commented Feb 6, 2024

@llvm/pr-subscribers-clang

Author: Ryosuke Niwa (rniwa)

Changes

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 --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());
+}

Copy link

github-actions bot commented Feb 6, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@rniwa rniwa force-pushed the fix-assignment-to-refptr branch from 81057ef to e179bbe Compare February 7, 2024 00:21
@haoNoQ haoNoQ merged commit 85507f1 into llvm:main Feb 12, 2024
haoNoQ pushed a commit to haoNoQ/llvm-project that referenced this pull request Feb 13, 2024
@rniwa rniwa deleted the fix-assignment-to-refptr branch February 14, 2024 03:16
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