https://github.com/Xazax-hun created https://github.com/llvm/llvm-project/pull/244843
NumberObjectConversionChecker built its whole AST matcher tree, including a regex for matchesName, for every analyzed function body. The matcher does not depend on the function or on the Pedantic option, so build it once when the checker is constructed. ObjCAutoreleaseWriteChecker and RunLoopAutoreleaseLeakChecker built and ran Objective-C only matchers (they require Objective-C object pointer types, message sends or @autoreleasepool) on every function body, even in non-Objective-C translation units. Skip them when the language is not Objective-C. On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces the instructions of the static analyzer by 0.9-5.1% and the cycles by 1.0-4.6% on darwin, where these checkers are enabled by default. Peak memory is unchanged. The diagnostics are unchanged. Assisted-by: Claude Opus 5.5 From d645461908c542d4a2a85ed07b0fa7f0dd3e30e1 Mon Sep 17 00:00:00 2001 From: Gabor Horvath <[email protected]> Date: Sat, 21 Nov 2026 02:18:36 +0000 Subject: [PATCH] [analyzer] Avoid rebuilding AST matchers for every function NumberObjectConversionChecker built its whole AST matcher tree, including a regex for matchesName, for every analyzed function body. The matcher does not depend on the function or on the Pedantic option, so build it once when the checker is constructed. ObjCAutoreleaseWriteChecker and RunLoopAutoreleaseLeakChecker built and ran Objective-C only matchers (they require Objective-C object pointer types, message sends or @autoreleasepool) on every function body, even in non-Objective-C translation units. Skip them when the language is not Objective-C. On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces the instructions of the static analyzer by 0.9-5.1% and the cycles by 1.0-4.6% on darwin, where these checkers are enabled by default. Peak memory is unchanged. The diagnostics are unchanged. Assisted-by: Claude Opus 5.5 --- .../NumberObjectConversionChecker.cpp | 20 +++++++++++++++---- .../Checkers/ObjCAutoreleaseWriteChecker.cpp | 3 +++ .../RunLoopAutoreleaseLeakChecker.cpp | 3 +++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp index 68ab22a44e7b65..a05f204253b20b 100644 --- a/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp @@ -44,8 +44,13 @@ class NumberObjectConversionChecker : public Checker<check::ASTCodeBody> { public: bool Pedantic; + NumberObjectConversionChecker(); + void checkASTCodeBody(const Decl *D, AnalysisManager &AM, BugReporter &BR) const; + +private: + StatementMatcher Matcher; }; class Callback : public MatchFinder::MatchCallback { @@ -192,9 +197,7 @@ void Callback::run(const MatchFinder::MatchResult &Result) { Conv->getSourceRange()); } -void NumberObjectConversionChecker::checkASTCodeBody(const Decl *D, - AnalysisManager &AM, - BugReporter &BR) const { +static StatementMatcher buildMatcher() { // Currently this matches CoreFoundation opaque pointer typedefs. auto CSuspiciousNumberObjectExprM = expr(ignoringParenImpCasts( expr(hasType(typedefType( @@ -333,10 +336,19 @@ void NumberObjectConversionChecker::checkASTCodeBody(const Decl *D, ConversionThroughExplicitNumberCastM, ConversionThroughInitializerM)).bind("conv"); + return traverse(TK_AsIs, stmt(forEachDescendant(FinalM))); +} + +NumberObjectConversionChecker::NumberObjectConversionChecker() + : Matcher(buildMatcher()) {} + +void NumberObjectConversionChecker::checkASTCodeBody(const Decl *D, + AnalysisManager &AM, + BugReporter &BR) const { MatchFinder F; Callback CB(this, BR, AM.getAnalysisDeclContext(D)); - F.addMatcher(traverse(TK_AsIs, stmt(forEachDescendant(FinalM))), &CB); + F.addMatcher(Matcher, &CB); F.match(*D->getBody(), AM.getASTContext()); } diff --git a/clang/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp index e7fd14d4558b2b..4064effea4556c 100644 --- a/clang/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp @@ -170,6 +170,9 @@ static void emitDiagnostics(BoundNodes &Match, const Decl *D, BugReporter &BR, void ObjCAutoreleaseWriteChecker::checkASTCodeBody(const Decl *D, AnalysisManager &AM, BugReporter &BR) const { + // The matchers below can only match Objective-C code. + if (!AM.getLangOpts().ObjC) + return; auto DoublePointerParamM = parmVarDecl(hasType(hasCanonicalType(pointerType( diff --git a/clang/lib/StaticAnalyzer/Checkers/RunLoopAutoreleaseLeakChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/RunLoopAutoreleaseLeakChecker.cpp index 8233398e84d55b..47a247d84ce488 100644 --- a/clang/lib/StaticAnalyzer/Checkers/RunLoopAutoreleaseLeakChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/RunLoopAutoreleaseLeakChecker.cpp @@ -194,6 +194,9 @@ checkTempObjectsInNoPool(const Decl *D, AnalysisManager &AM, BugReporter &BR, void RunLoopAutoreleaseLeakChecker::checkASTCodeBody(const Decl *D, AnalysisManager &AM, BugReporter &BR) const { + // The matchers below can only match Objective-C code. + if (!AM.getLangOpts().ObjC) + return; checkTempObjectsInSamePool(D, AM, BR, this); checkTempObjectsInNoPool(D, AM, BR, this); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
