[clang] [WebKit Checkers][NFC] Extract hasGuardian from isPtrOriginSafe in RawPtrRefLocalVarsChecker (PR #224726)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 18 13:05:50 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: geoffreygaren
<details>
<summary>Changes</summary>
So an upcoming borrow checker can skip it.
(A guardian variable is an independent declaration that ensures a lifetime. Borrow checking does not accept guardian variables because they do not convey `lifetimebound` links.)
Assisted-by: Claude
---
Full diff: https://github.com/llvm/llvm-project/pull/224726.diff
1 Files Affected:
- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp (+30-31)
``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp
index b0ebfea4b14108..e67808fd407eb2 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp
@@ -372,42 +372,41 @@ class RawPtrRefLocalVarsChecker
if (Model->isSafeExpr(InitArgOrigin))
return true;
- if (auto *Ref = llvm::dyn_cast<DeclRefExpr>(InitArgOrigin)) {
- if (auto *MaybeGuardian =
- dyn_cast_or_null<VarDecl>(Ref->getFoundDecl())) {
- const auto *MaybeGuardianArgType =
- MaybeGuardian->getType().getTypePtr();
- if (MaybeGuardianArgType) {
- const CXXRecordDecl *const MaybeGuardianArgCXXRecord =
- MaybeGuardianArgType->getAsCXXRecordDecl();
- if (MaybeGuardianArgCXXRecord) {
- if (MaybeGuardian->isLocalVarDecl() &&
- (Model->isSafePtr(MaybeGuardianArgCXXRecord) ||
- isRefcountedStringsHack(MaybeGuardian)) &&
- isGuardedScopeEmbeddedInGuardianScope(V, MaybeGuardian))
- return true;
- }
- }
-
- if (isa<ParmVarDecl>(MaybeGuardian)) {
- if (auto *FD = dyn_cast<FunctionDecl>(DeclWithIssue)) {
- if (GuardianVisitor{MaybeGuardian}.TraverseStmt(
- FD->getBody()))
- return true;
- }
- if (auto *MD = dyn_cast<ObjCMethodDecl>(DeclWithIssue)) {
- if (GuardianVisitor{MaybeGuardian}.TraverseStmt(
- MD->getBody()))
- return true;
- }
- }
- }
- }
+ if (hasGuardian(V, InitArgOrigin, DeclWithIssue))
+ return true;
return false;
});
}
+ bool hasGuardian(const VarDecl *V, const Expr *InitArgOrigin,
+ const Decl *DeclWithIssue) const {
+ auto *Ref = dyn_cast<DeclRefExpr>(InitArgOrigin);
+ if (!Ref)
+ return false;
+
+ auto *MaybeGuardian = dyn_cast_or_null<VarDecl>(Ref->getFoundDecl());
+ if (!MaybeGuardian)
+ return false;
+
+ if (auto *Record = MaybeGuardian->getType()->getAsCXXRecordDecl()) {
+ if (MaybeGuardian->isLocalVarDecl() &&
+ (Model->isSafePtr(Record) ||
+ isRefcountedStringsHack(MaybeGuardian)) &&
+ isGuardedScopeEmbeddedInGuardianScope(V, MaybeGuardian))
+ return true;
+ }
+
+ if (isa<ParmVarDecl>(MaybeGuardian)) {
+ if (auto *FD = dyn_cast<FunctionDecl>(DeclWithIssue))
+ return GuardianVisitor{MaybeGuardian}.TraverseStmt(FD->getBody());
+ if (auto *MD = dyn_cast<ObjCMethodDecl>(DeclWithIssue))
+ return GuardianVisitor{MaybeGuardian}.TraverseStmt(MD->getBody());
+ }
+
+ return false;
+ }
+
bool shouldSkipVarDecl(const VarDecl *V) const {
assert(V);
if (isa<ImplicitParamDecl>(V))
``````````
</details>
https://github.com/llvm/llvm-project/pull/224726
More information about the cfe-commits
mailing list