[clang] [WebKit checkers] Recognize ensureFoo functions (PR #119681)
Ryosuke Niwa via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 12 13:26:04 PST 2024
================
@@ -169,4 +173,42 @@ bool isConstOwnerPtrMemberExpr(const clang::Expr *E) {
return isOwnerPtrType(T) && T.isConstQualified();
}
+class EnsureFunctionVisitor
+ : public ConstStmtVisitor<EnsureFunctionVisitor, bool> {
+public:
+ bool VisitStmt(const Stmt *S) {
+ for (const Stmt *Child : S->children()) {
+ if (Child && !Visit(Child))
+ return false;
+ }
+ return true;
+ }
+
+ bool VisitReturnStmt(const ReturnStmt *RS) {
+ if (auto *RV = RS->getRetValue()) {
+ RV = RV->IgnoreParenCasts();
+ if (isa<CXXNullPtrLiteralExpr>(RV))
+ return true;
+ return isConstOwnerPtrMemberExpr(RV);
+ }
+ return false;
+ }
+};
+
+bool EnsureFunctionAnalysis::isACallToEnsureFn(const clang::Expr *E) const {
+ auto *MCE = dyn_cast<CXXMemberCallExpr>(E);
+ if (!MCE)
+ return false;
+ auto *Callee = MCE->getDirectCallee();
+ if (!Callee)
+ return false;
+ auto *Body = Callee->getBody();
+ if (!Body)
+ return false;
+ auto [CacheIt, IsNew] = Cache.insert(std::make_pair(Callee, false));
+ if (IsNew)
+ CacheIt->second = EnsureFunctionVisitor().Visit(Body);
+ return CacheIt->second;
----------------
rniwa wrote:
Yeah, I figured we should cache the result instead of keep analyzing the same function repeatedly.
https://github.com/llvm/llvm-project/pull/119681
More information about the cfe-commits
mailing list