[clang] [WebKit Checkers][NFC] Extract hasPublicMethodInHierarchy from isSmartPtrCompatible (PR #224697)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 18 10:53:00 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: geoffreygaren
<details>
<summary>Changes</summary>
So we can reuse it in borrow checking.
hasPublicMethodInHierarchy identifies RefCounted via 'ref' and 'deref' public
member functions.
Borrow checking will identify CanBorrow via a 'crashIfBorrowed' public member
function.
Assisted-by: Claude
---
Full diff: https://github.com/llvm/llvm-project/pull/224697.diff
1 Files Affected:
- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp (+25-27)
``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index d8a62db4daee1..50bb4c83833a6 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -73,55 +73,53 @@ hasPublicMethodInBase(const CXXBaseSpecifier *Base, StringRef NameToMatch) {
return hasPublicMethodInBaseClass(R, NameToMatch) ? R : nullptr;
}
-std::optional<bool> isSmartPtrCompatible(const CXXRecordDecl *R,
- StringRef IncMethodName,
- StringRef DecMethodName) {
+static std::optional<bool> hasPublicMethodInHierarchy(const CXXRecordDecl *R,
+ StringRef MethodName) {
assert(R);
R = R->getDefinition();
if (!R)
return std::nullopt;
- bool hasRef = hasPublicMethodInBaseClass(R, IncMethodName);
- bool hasDeref = hasPublicMethodInBaseClass(R, DecMethodName);
- if (hasRef && hasDeref)
+ if (hasPublicMethodInBaseClass(R, MethodName))
return true;
CXXBasePaths Paths;
Paths.setOrigin(const_cast<CXXRecordDecl *>(R));
bool AnyInconclusiveBase = false;
- const auto hasPublicRefInBase = [&](const CXXBaseSpecifier *Base,
- CXXBasePath &) {
- auto hasRefInBase = clang::hasPublicMethodInBase(Base, IncMethodName);
- if (!hasRefInBase) {
+ const auto hasPublicMethod = [&](const CXXBaseSpecifier *Base,
+ CXXBasePath &) {
+ auto HasMethodInBase = clang::hasPublicMethodInBase(Base, MethodName);
+ if (!HasMethodInBase) {
AnyInconclusiveBase = true;
return false;
}
- return (*hasRefInBase) != nullptr;
+ return (*HasMethodInBase) != nullptr;
};
- hasRef = hasRef || R->lookupInBases(hasPublicRefInBase, Paths,
- /*LookupInDependent =*/true);
+ bool Found = R->lookupInBases(hasPublicMethod, Paths,
+ /*LookupInDependent =*/true);
if (AnyInconclusiveBase)
return std::nullopt;
- Paths.clear();
- const auto hasPublicDerefInBase = [&](const CXXBaseSpecifier *Base,
- CXXBasePath &) {
- auto hasDerefInBase = clang::hasPublicMethodInBase(Base, DecMethodName);
- if (!hasDerefInBase) {
- AnyInconclusiveBase = true;
- return false;
- }
- return (*hasDerefInBase) != nullptr;
- };
- hasDeref = hasDeref || R->lookupInBases(hasPublicDerefInBase, Paths,
- /*LookupInDependent =*/true);
- if (AnyInconclusiveBase)
+ return Found;
+}
+
+std::optional<bool> isSmartPtrCompatible(const CXXRecordDecl *R,
+ StringRef IncMethodName,
+ StringRef DecMethodName) {
+ assert(R);
+
+ auto HasInc = hasPublicMethodInHierarchy(R, IncMethodName);
+ if (!HasInc)
+ return std::nullopt;
+
+ auto HasDec = hasPublicMethodInHierarchy(R, DecMethodName);
+ if (!HasDec)
return std::nullopt;
- return hasRef && hasDeref;
+ return *HasInc && *HasDec;
}
std::optional<bool> isRefCountable(const clang::CXXRecordDecl *R) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/224697
More information about the cfe-commits
mailing list