[clang] 32cdc61 - [analyzer][NFC] Use DynamicRecursiveASTVisitor in two WebKit checkers (#246951)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 29 06:22:01 PST 2026
Author: Balázs Benics
Date: 2026-11-29T14:21:55Z
New Revision: 32cdc61174eaf81abf3e4517be5597d5b71dd4bc
URL: https://github.com/llvm/llvm-project/commit/32cdc61174eaf81abf3e4517be5597d5b71dd4bc
DIFF: https://github.com/llvm/llvm-project/commit/32cdc61174eaf81abf3e4517be5597d5b71dd4bc.diff
LOG: [analyzer][NFC] Use DynamicRecursiveASTVisitor in two WebKit checkers (#246951)
RetainPtrCtorAdoptChecker and ForwardDeclChecker were the only checkers
in clang/lib/StaticAnalyzer still instantiating the CRTP
RecursiveASTVisitor. All the sibling WebKit checkers (RawPtrRef*,
NoDeleteChecker, RefCntblBaseVirtualDtorChecker, ...) already use
DynamicRecursiveASTVisitor, which is precompiled in clangAST and avoids
instantiating the full traversal in every user.
These two TUs were by far the most expensive in the analyzer. Converting
them to the same pattern as their siblings drops their compile cost from
~36.0G and ~34.6G to ~12.8G and ~11.6G retired instructions (release
with assertions, arm64, CMake PCH enabled, median of 3 runs):
RetainPtrCtorAdoptChecker.cpp -64%
ForwardDeclChecker.cpp -66%
which is about -1.8% of the total compile cost of all 196 static
analyzer translation units.
No functional change intended: the same Visit*/Traverse* hooks are
overridden with the same bodies, ShouldVisitTemplateInstantiations is
still true and ShouldVisitImplicitCode is still false.
Assisted-by: Claude Code
Added:
Modified:
clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
Removed:
################################################################################
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
index 6fb775c878e738..f2166fa5f25a49 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
@@ -11,7 +11,7 @@
#include "PtrTypesSemantics.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
-#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
@@ -45,54 +45,51 @@ class ForwardDeclChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
// The calls to checkAST* from AnalysisConsumer don't
// visit template instantiations or lambda classes. We
// want to visit those, so we make our own RecursiveASTVisitor.
- struct LocalVisitor : public RecursiveASTVisitor<LocalVisitor> {
- using Base = RecursiveASTVisitor<LocalVisitor>;
-
+ struct LocalVisitor : DynamicRecursiveASTVisitor {
const ForwardDeclChecker *Checker;
Decl *DeclWithIssue{nullptr};
explicit LocalVisitor(const ForwardDeclChecker *Checker)
: Checker(Checker) {
assert(Checker);
+ ShouldVisitTemplateInstantiations = true;
+ ShouldVisitImplicitCode = false;
}
- bool shouldVisitTemplateInstantiations() const { return true; }
- bool shouldVisitImplicitCode() const { return false; }
-
- bool VisitTypedefDecl(TypedefDecl *TD) {
+ bool VisitTypedefDecl(TypedefDecl *TD) override {
Checker->visitTypedef(TD);
return true;
}
- bool VisitRecordDecl(const RecordDecl *RD) {
+ bool VisitRecordDecl(RecordDecl *RD) override {
Checker->visitRecordDecl(RD, DeclWithIssue);
return true;
}
- bool TraverseDecl(Decl *D) {
+ bool TraverseDecl(Decl *D) override {
llvm::SaveAndRestore SavedDecl(DeclWithIssue);
if (D && (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)))
DeclWithIssue = D;
- return Base::TraverseDecl(D);
+ return DynamicRecursiveASTVisitor::TraverseDecl(D);
}
- bool VisitVarDecl(VarDecl *V) {
+ bool VisitVarDecl(VarDecl *V) override {
if (V->isLocalVarDecl())
Checker->visitVarDecl(V, DeclWithIssue);
return true;
}
- bool VisitCallExpr(const CallExpr *CE) {
+ bool VisitCallExpr(CallExpr *CE) override {
Checker->visitCallExpr(CE, DeclWithIssue);
return true;
}
- bool VisitCXXConstructExpr(const CXXConstructExpr *CE) {
+ bool VisitCXXConstructExpr(CXXConstructExpr *CE) override {
Checker->visitConstructExpr(CE, DeclWithIssue);
return true;
}
- bool VisitObjCMessageExpr(const ObjCMessageExpr *ObjCMsgExpr) {
+ bool VisitObjCMessageExpr(ObjCMessageExpr *ObjCMsgExpr) override {
Checker->visitObjCMessageExpr(ObjCMsgExpr, DeclWithIssue);
return true;
}
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
index 861ea9a6d52d3e..3fd5e2c74d437f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
@@ -8,7 +8,7 @@
#include "ASTUtils.h"
#include "PtrTypesSemantics.h"
-#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
#include "clang/Analysis/RetainSummaryManager.h"
#include "clang/Basic/SourceManager.h"
@@ -46,64 +46,61 @@ class RetainPtrCtorAdoptChecker
// The calls to checkAST* from AnalysisConsumer don't
// visit template instantiations or lambda classes. We
// want to visit those, so we make our own RecursiveASTVisitor.
- struct LocalVisitor : public RecursiveASTVisitor<LocalVisitor> {
+ struct LocalVisitor : DynamicRecursiveASTVisitor {
const RetainPtrCtorAdoptChecker *Checker;
Decl *DeclWithIssue{nullptr};
- using Base = RecursiveASTVisitor<LocalVisitor>;
-
explicit LocalVisitor(const RetainPtrCtorAdoptChecker *Checker)
: Checker(Checker) {
assert(Checker);
+ ShouldVisitTemplateInstantiations = true;
+ ShouldVisitImplicitCode = false;
}
- bool shouldVisitTemplateInstantiations() const { return true; }
- bool shouldVisitImplicitCode() const { return false; }
-
- bool TraverseDecl(Decl *D) {
+ bool TraverseDecl(Decl *D) override {
llvm::SaveAndRestore SavedDecl(DeclWithIssue);
if (D && (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)))
DeclWithIssue = D;
- return Base::TraverseDecl(D);
+ return DynamicRecursiveASTVisitor::TraverseDecl(D);
}
- bool TraverseClassTemplateDecl(ClassTemplateDecl *CTD) {
+ bool TraverseClassTemplateDecl(ClassTemplateDecl *CTD) override {
if (isRetainPtrOrOSPtr(safeGetName(CTD)))
return true; // Skip the contents of RetainPtr.
- return Base::TraverseClassTemplateDecl(CTD);
+ return DynamicRecursiveASTVisitor::TraverseClassTemplateDecl(CTD);
}
- bool VisitTypedefDecl(TypedefDecl *TD) {
+ bool VisitTypedefDecl(TypedefDecl *TD) override {
Checker->RTC.visitTypedef(TD);
return true;
}
- bool VisitCallExpr(const CallExpr *CE) {
+ bool VisitCallExpr(CallExpr *CE) override {
Checker->visitCallExpr(CE, DeclWithIssue);
return true;
}
- bool VisitCXXConstructExpr(const CXXConstructExpr *CE) {
+ bool VisitCXXConstructExpr(CXXConstructExpr *CE) override {
Checker->visitConstructExpr(CE, DeclWithIssue);
return true;
}
- bool VisitObjCMessageExpr(const ObjCMessageExpr *ObjCMsgExpr) {
+ bool VisitObjCMessageExpr(ObjCMessageExpr *ObjCMsgExpr) override {
Checker->visitObjCMessageExpr(ObjCMsgExpr, DeclWithIssue);
return true;
}
- bool VisitReturnStmt(const ReturnStmt *RS) {
+ bool VisitReturnStmt(ReturnStmt *RS) override {
Checker->visitReturnStmt(RS, DeclWithIssue);
return true;
}
- bool VisitVarDecl(const VarDecl *VD) {
+ bool VisitVarDecl(VarDecl *VD) override {
Checker->visitVarDecl(VD);
return true;
}
- bool VisitBinaryOperator(const BinaryOperator *BO) {
+ bool VisitBinaryOperator(BinaryOperator *BO) override {
Checker->visitBinaryOperator(BO);
return true;
}
More information about the cfe-commits
mailing list