https://github.com/steakhal created 
https://github.com/llvm/llvm-project/pull/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

From f979484ac19a14fb545844501164f5adaf39979a Mon Sep 17 00:00:00 2001
From: Balazs Benics <[email protected]>
Date: Sun, 29 Nov 2026 10:19:00 +0000
Subject: [PATCH] [analyzer][NFC] Use DynamicRecursiveASTVisitor in two WebKit
 checkers

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
---
 .../Checkers/WebKit/ForwardDeclChecker.cpp    | 27 +++++++--------
 .../WebKit/RetainPtrCtorAdoptChecker.cpp      | 33 +++++++++----------
 2 files changed, 27 insertions(+), 33 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
index 6fb775c878e73..f2166fa5f25a4 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 861ea9a6d52d3..3fd5e2c74d437 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;
       }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to