Skip to content

[WebKit Checkers] Allow operator T&() in a const member function #126470

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 1 commit into from
Feb 11, 2025

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Feb 10, 2025

Allow operator T&() in a member function which returns a const member variable.

In particular, this will allow UniqueRef::operator T&() and Ref::operator T&() to be treated as a safe pointer origin when they're called on a const member.

Allow operator T&() in a member function which returns a const member variable.

In particular, this will allow UniqueRef::operator T&() and Ref::operator T&()
to be treated as a safe pointer origin when they're called on a const member.
@rniwa rniwa requested review from t-rasmud and haoNoQ February 10, 2025 05:53
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Feb 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 10, 2025

@llvm/pr-subscribers-clang

Author: Ryosuke Niwa (rniwa)

Changes

Allow operator T&() in a member function which returns a const member variable.

In particular, this will allow UniqueRef::operator T&() and Ref::operator T&() to be treated as a safe pointer origin when they're called on a const member.


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

3 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp (+4-4)
  • (modified) clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp (+4)
  • (modified) clang/test/Analysis/Checkers/WebKit/mock-types.h (+1)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
index abf5d3ec193a41a..9eb0c8ed5c8419f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
@@ -154,10 +154,10 @@ bool isConstOwnerPtrMemberExpr(const clang::Expr *E) {
   if (auto *MCE = dyn_cast<CXXMemberCallExpr>(E)) {
     if (auto *Callee = MCE->getDirectCallee()) {
       auto Name = safeGetName(Callee);
-      if (Name == "get" || Name == "ptr") {
-        auto *ThisArg = MCE->getImplicitObjectArgument();
-        E = ThisArg;
-      }
+      if (Name == "get" || Name == "ptr")
+        E = MCE->getImplicitObjectArgument();
+      if (auto *CD = dyn_cast<CXXConversionDecl>(Callee))
+        E = MCE->getImplicitObjectArgument();
     }
   } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
     if (OCE->getOperator() == OO_Star && OCE->getNumArgs() == 1)
diff --git a/clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp b/clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp
index 215238a7fcf0712..8da415a818a8296 100644
--- a/clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp
@@ -31,6 +31,7 @@ class Foo {
 public:
   Foo();
   void bar();
+  RefCountable& obj1() const { return m_obj1; }
 
 private:
   const Ref<RefCountable> m_obj1;
@@ -41,6 +42,7 @@ void Foo::bar() {
   m_obj1->method();
   m_obj2->method();
   // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
+  obj1().method();
 }
 
 } // namespace call_args_const_ref_member
@@ -100,6 +102,7 @@ class Foo {
 public:
   Foo();
   void bar();
+  RefCountable& obj1() { return m_obj1; }
 
 private:
   const UniqueRef<RefCountable> m_obj1;
@@ -110,6 +113,7 @@ void Foo::bar() {
   m_obj1->method();
   m_obj2->method();
   // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
+  obj1().method();
 }
 
 } // namespace call_args_const_unique_ref
diff --git a/clang/test/Analysis/Checkers/WebKit/mock-types.h b/clang/test/Analysis/Checkers/WebKit/mock-types.h
index 85397c2d259516d..a1f0cc8b046b995 100644
--- a/clang/test/Analysis/Checkers/WebKit/mock-types.h
+++ b/clang/test/Analysis/Checkers/WebKit/mock-types.h
@@ -289,6 +289,7 @@ class UniqueRef {
     u.t = nullptr;
   }
   T &get() const { return *t; }
+  operator T&() const { return *t; }
   T *operator->() const { return t; }
   UniqueRef &operator=(T &) { return *this; }
 };

@llvmbot
Copy link
Member

llvmbot commented Feb 10, 2025

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

Author: Ryosuke Niwa (rniwa)

Changes

Allow operator T&() in a member function which returns a const member variable.

In particular, this will allow UniqueRef::operator T&() and Ref::operator T&() to be treated as a safe pointer origin when they're called on a const member.


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

3 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp (+4-4)
  • (modified) clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp (+4)
  • (modified) clang/test/Analysis/Checkers/WebKit/mock-types.h (+1)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
index abf5d3ec193a41a..9eb0c8ed5c8419f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
@@ -154,10 +154,10 @@ bool isConstOwnerPtrMemberExpr(const clang::Expr *E) {
   if (auto *MCE = dyn_cast<CXXMemberCallExpr>(E)) {
     if (auto *Callee = MCE->getDirectCallee()) {
       auto Name = safeGetName(Callee);
-      if (Name == "get" || Name == "ptr") {
-        auto *ThisArg = MCE->getImplicitObjectArgument();
-        E = ThisArg;
-      }
+      if (Name == "get" || Name == "ptr")
+        E = MCE->getImplicitObjectArgument();
+      if (auto *CD = dyn_cast<CXXConversionDecl>(Callee))
+        E = MCE->getImplicitObjectArgument();
     }
   } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
     if (OCE->getOperator() == OO_Star && OCE->getNumArgs() == 1)
diff --git a/clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp b/clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp
index 215238a7fcf0712..8da415a818a8296 100644
--- a/clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp
@@ -31,6 +31,7 @@ class Foo {
 public:
   Foo();
   void bar();
+  RefCountable& obj1() const { return m_obj1; }
 
 private:
   const Ref<RefCountable> m_obj1;
@@ -41,6 +42,7 @@ void Foo::bar() {
   m_obj1->method();
   m_obj2->method();
   // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
+  obj1().method();
 }
 
 } // namespace call_args_const_ref_member
@@ -100,6 +102,7 @@ class Foo {
 public:
   Foo();
   void bar();
+  RefCountable& obj1() { return m_obj1; }
 
 private:
   const UniqueRef<RefCountable> m_obj1;
@@ -110,6 +113,7 @@ void Foo::bar() {
   m_obj1->method();
   m_obj2->method();
   // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
+  obj1().method();
 }
 
 } // namespace call_args_const_unique_ref
diff --git a/clang/test/Analysis/Checkers/WebKit/mock-types.h b/clang/test/Analysis/Checkers/WebKit/mock-types.h
index 85397c2d259516d..a1f0cc8b046b995 100644
--- a/clang/test/Analysis/Checkers/WebKit/mock-types.h
+++ b/clang/test/Analysis/Checkers/WebKit/mock-types.h
@@ -289,6 +289,7 @@ class UniqueRef {
     u.t = nullptr;
   }
   T &get() const { return *t; }
+  operator T&() const { return *t; }
   T *operator->() const { return t; }
   UniqueRef &operator=(T &) { return *this; }
 };

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 Feb 11, 2025

Thanks for the review!

@rniwa rniwa merged commit 8c67f14 into llvm:main Feb 11, 2025
11 checks passed
@rniwa rniwa deleted the allow-const-member-cast-op branch February 11, 2025 19:08
rniwa added a commit to rniwa/llvm-project that referenced this pull request Feb 11, 2025
…m#126470)

Allow operator T&() in a member function which returns a const member
variable.

In particular, this will allow UniqueRef::operator T&() and
Ref::operator T&() to be treated as a safe pointer origin when they're
called on a const member.
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…m#126470)

Allow operator T&() in a member function which returns a const member
variable.

In particular, this will allow UniqueRef::operator T&() and
Ref::operator T&() to be treated as a safe pointer origin when they're
called on a const member.
flovent pushed a commit to flovent/llvm-project that referenced this pull request Feb 13, 2025
…m#126470)

Allow operator T&() in a member function which returns a const member
variable.

In particular, this will allow UniqueRef::operator T&() and
Ref::operator T&() to be treated as a safe pointer origin when they're
called on a const member.
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…m#126470)

Allow operator T&() in a member function which returns a const member
variable.

In particular, this will allow UniqueRef::operator T&() and
Ref::operator T&() to be treated as a safe pointer origin when they're
called on a const member.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…m#126470)

Allow operator T&() in a member function which returns a const member
variable.

In particular, this will allow UniqueRef::operator T&() and
Ref::operator T&() to be treated as a safe pointer origin when they're
called on a const member.
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