Skip to content

[webkit.NoUncountedMemberChecker] Fix a regression that every class is treated as if it's ref countable. #131249

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

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Mar 14, 2025

This PR fixes a regression that webkit.NoUncountedMemberChecker and alpha.webkit.NoUncheckedMemberChecker emits warnings for every class as if they supported ref counting and checked ptr because we were erroneously coercing the return value of isRefCountable and isCheckedPtrCapable, which is std::optional, to boolean values.

…s treated as if it's ref countable.

This PR fixes a regression that webkit.NoUncountedMemberChecker and alpha.webkit.NoUncheckedMemberChecker
emits warnings for every class as if they supported ref counting and checked ptr because we were erroneously
coercing the return value of isRefCountable and isCheckedPtrCapable, which is std::optional<bool>,
to boolean values.
@rniwa rniwa requested review from t-rasmud and haoNoQ March 14, 2025 01:14
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Mar 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 14, 2025

@llvm/pr-subscribers-clang

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

Author: Ryosuke Niwa (rniwa)

Changes

This PR fixes a regression that webkit.NoUncountedMemberChecker and alpha.webkit.NoUncheckedMemberChecker emits warnings for every class as if they supported ref counting and checked ptr because we were erroneously coercing the return value of isRefCountable and isCheckedPtrCapable, which is std::optional<bool>, to boolean values.


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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp (+2-2)
  • (modified) clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp (+9-1)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp
index 593e6e3663639..dc4e2c7d168fb 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp
@@ -223,7 +223,7 @@ class NoUncountedMemberChecker final : public RawPtrRefMemberChecker {
   std::optional<bool>
   isPtrCompatible(const clang::QualType,
                   const clang::CXXRecordDecl *R) const final {
-    return R && isRefCountable(R);
+    return R ? isRefCountable(R) : std::nullopt;
   }
 
   bool isPtrCls(const clang::CXXRecordDecl *R) const final {
@@ -246,7 +246,7 @@ class NoUncheckedPtrMemberChecker final : public RawPtrRefMemberChecker {
   std::optional<bool>
   isPtrCompatible(const clang::QualType,
                   const clang::CXXRecordDecl *R) const final {
-    return R && isCheckedPtrCapable(R);
+    return R ? isCheckedPtrCapable(R) : std::nullopt;
   }
 
   bool isPtrCls(const clang::CXXRecordDecl *R) const final {
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp
index bca7b3bad3a15..1bdbaedefbfeb 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp
@@ -36,7 +36,6 @@ namespace members {
   };
 }
 
-
 namespace ignore_unions {
   union Foo {
     RefCountable* a;
@@ -60,3 +59,12 @@ void foo(RefCountable* t) {
 }
 
 } // ignore_system_header
+
+namespace ignore_non_ref_countable {
+  struct Foo {
+  };
+
+  struct Bar {
+    Foo* foo;
+  };
+}
\ No newline at end of file

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 rniwa merged commit bb0cc69 into llvm:main Mar 18, 2025
12 of 14 checks passed
@rniwa rniwa deleted the fix-member-refcounted-checked-warning-regression branch March 18, 2025 22:43
rniwa added a commit to rniwa/llvm-project that referenced this pull request Mar 18, 2025
…s treated as if it's ref countable. (llvm#131249)

This PR fixes a regression that webkit.NoUncountedMemberChecker and
alpha.webkit.NoUncheckedMemberChecker emits warnings for every class as
if they supported ref counting and checked ptr because we were
erroneously coercing the return value of isRefCountable and
isCheckedPtrCapable, which is std::optional<bool>, to boolean values.
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