[clang] 263f825 - [analyzer][NFC] Build the AST matchers of GCDAntipattern and OSObjectCStyleCast once (#246981)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 29 08:34:47 PST 2026
Author: Balázs Benics
Date: 2026-11-29T16:34:41Z
New Revision: 263f825f85d58652bc14ea53d2b85ae84f00e557
URL: https://github.com/llvm/llvm-project/commit/263f825f85d58652bc14ea53d2b85ae84f00e557
DIFF: https://github.com/llvm/llvm-project/commit/263f825f85d58652bc14ea53d2b85ae84f00e557.diff
LOG: [analyzer][NFC] Build the AST matchers of GCDAntipattern and OSObjectCStyleCast once (#246981)
- `GCDAntipatternChecker` and `OSObjectCStyleCastChecker` rebuilt their
large, heap-allocating matcher trees in `checkASTCodeBody` for every
function body.
- The matchers don't depend on the body, so build them once in the
checker constructors and keep them as type-erased checker fields.
- Fields rather than function-local statics, so no state is shared
between analysis runs in one process (e.g. unit tests).
- The checkers are in `optin.performance` and `optin.osx`, so only users
enabling them benefit; the win scales with the number of function
bodies.
Analysis-time impact (instructions retired in millions,
`unroll-loops=true`; inputs are the reproducers from
llvm/llvm-project#105512; ON = default graph trimming, OFF =
`graph-trim-interval=0`):
file mode before after change
clang-MSP430ISelDAGToDAG.cpp ON 26295 25523 -2.9%
clang-MSP430ISelDAGToDAG.cpp OFF 26809 25920 -3.3%
linux-topro.c ON 61158 60739 -0.7%
linux-topro.c OFF 61628 61289 -0.6%
ruby-unicode.c ON 22710 22687 -0.1% (noise)
ruby-unicode.c OFF 24059 24029 -0.1% (noise)
20 clang/lib TUs analyzed with their own compile flags and the same
config (instructions retired in billions; user CPU seconds):
| TU | instr before (G) | instr after (G) | change | spread b/a | user s
|
|---|---|---|---|---|---|
| CodeGen/CGBuiltin.cpp | 137.0 | 129.5 | -5.50% | 2.86% / 2.19% | 16.9
-> 12.1 |
| CodeGen/CGStmt.cpp | 89.1 | 86.6 | -2.80% | 0.23% / 0.07% | 8.0 -> 7.8
|
| Parse/ParseExpr.cpp | 65.6 | 63.8 | -2.69% | 0.01% / 0.06% | 5.8 ->
5.8 |
| CodeGen/CGCall.cpp | 236.6 | 232.4 | -1.76% | 3.57% / 0.69% | 24.2 ->
21.5 |
| Parse/ParseDecl.cpp | 172.9 | 171.2 | -0.96% | 0.10% / 0.03% | 15.7 ->
15.6 |
| Sema/SemaExprCXX.cpp | 282.4 | 280.1 | -0.82% | 0.17% / 0.26% | 26.0
-> 26.1 |
| Sema/SemaStmt.cpp | 240.9 | 239.0 | -0.78% | 0.06% / 0.10% | 21.4 ->
21.3 |
| Sema/SemaType.cpp | 253.0 | 251.2 | -0.72% | 0.03% / 0.10% | 22.7 ->
22.3 |
| CodeGen/CGExpr.cpp | 382.6 | 380.0 | -0.68% | 0.02% / 0.07% | 35.0 ->
34.0 |
| Sema/SemaLookup.cpp | 269.4 | 267.6 | -0.66% | 0.22% / 0.12% | 23.2 ->
23.2 |
| Sema/SemaInit.cpp | 226.9 | 225.5 | -0.64% | 0.05% / 0.05% | 19.7 ->
19.9 |
| Sema/SemaTemplate.cpp | 369.6 | 367.4 | -0.58% | 0.09% / 0.11% | 33.2
-> 32.5 |
| Sema/SemaDecl.cpp | 508.7 | 506.3 | -0.47% | 0.05% / 0.01% | 45.8 ->
45.8 |
| Sema/SemaExpr.cpp | 693.1 | 690.3 | -0.40% | 0.23% / 0.04% | 63.7 ->
63.3 |
| Sema/SemaDeclAttr.cpp | 508.2 | 506.2 | -0.39% | 0.15% / 0.04% | 47.1
-> 46.6 |
| Sema/SemaOverload.cpp | 542.3 | 540.4 | -0.34% | 0.02% / 0.05% | 47.4
-> 46.9 |
| Sema/SemaChecking.cpp | 649.6 | 647.6 | -0.31% | 0.34% / 0.04% | 58.9
-> 59.4 |
| Sema/SemaDeclCXX.cpp | 880.0 | 877.8 | -0.25% | 0.04% / 0.04% | 77.2
-> 77.0 |
| AST/ASTContext.cpp | 1003.8 | 1001.9 | -0.19% | 1.98% / 0.11% | 92.1
-> 90.4 |
| AST/ExprConstant.cpp | 1354.0 | 1351.7 | -0.17% | 1.39% / 0.05% |
117.6 -> 117.9 |
| **Total (20 TUs)** | 8865.7 | 8816.7 | -0.55% | | 801 -> 789 (-1.5%) |
- Diagnostics are byte-identical on all inputs.
Assisted-by: Claude
Added:
Modified:
clang/lib/StaticAnalyzer/Checkers/GCDAntipatternChecker.cpp
clang/lib/StaticAnalyzer/Checkers/OSObjectCStyleCast.cpp
Removed:
################################################################################
diff --git a/clang/lib/StaticAnalyzer/Checkers/GCDAntipatternChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/GCDAntipatternChecker.cpp
index 5637941a58f02..08bbb2af194d0 100644
--- a/clang/lib/StaticAnalyzer/Checkers/GCDAntipatternChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/GCDAntipatternChecker.cpp
@@ -45,7 +45,15 @@ namespace {
// ID of a node at which the diagnostic would be emitted.
const char *WarnAtNode = "waitcall";
+static auto findGCDAntiPatternWithSemaphore() -> decltype(compoundStmt());
+static auto findGCDAntiPatternWithGroup() -> decltype(compoundStmt());
+
class GCDAntipatternChecker : public Checker<check::ASTCodeBody> {
+ // Building the matchers is expensive, so build them once per checker instead
+ // of once per function body.
+ const StatementMatcher SemaphoreMatcherM = findGCDAntiPatternWithSemaphore();
+ const StatementMatcher GroupMatcherM = findGCDAntiPatternWithGroup();
+
public:
void checkASTCodeBody(const Decl *D,
AnalysisManager &AM,
@@ -208,12 +216,10 @@ void GCDAntipatternChecker::checkASTCodeBody(const Decl *D,
AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
- auto SemaphoreMatcherM = findGCDAntiPatternWithSemaphore();
auto Matches = match(SemaphoreMatcherM, *D->getBody(), AM.getASTContext());
for (BoundNodes Match : Matches)
emitDiagnostics(Match, "semaphore", BR, ADC, this);
- auto GroupMatcherM = findGCDAntiPatternWithGroup();
Matches = match(GroupMatcherM, *D->getBody(), AM.getASTContext());
for (BoundNodes Match : Matches)
emitDiagnostics(Match, "group", BR, ADC, this);
diff --git a/clang/lib/StaticAnalyzer/Checkers/OSObjectCStyleCast.cpp b/clang/lib/StaticAnalyzer/Checkers/OSObjectCStyleCast.cpp
index 46690dd886b2b..bc447820ed405 100644
--- a/clang/lib/StaticAnalyzer/Checkers/OSObjectCStyleCast.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/OSObjectCStyleCast.cpp
@@ -23,11 +23,17 @@ using namespace clang;
using namespace ento;
using namespace ast_matchers;
+static StatementMatcher makeCastsMatcher();
+
namespace {
static constexpr const char *const WarnAtNode = "WarnAtNode";
static constexpr const char *const WarnRecordDecl = "WarnRecordDecl";
class OSObjectCStyleCastChecker : public Checker<check::ASTCodeBody> {
+ // Building the matcher is expensive, so build it once per checker instead of
+ // once per function body.
+ const StatementMatcher CastsM = makeCastsMatcher();
+
public:
void checkASTCodeBody(const Decl *D, AnalysisManager &AM,
BugReporter &BR) const;
@@ -77,12 +83,7 @@ static decltype(auto) hasTypePointingTo(DeclarationMatcher DeclM) {
return hasType(pointerType(pointee(hasDeclaration(DeclM))));
}
-void OSObjectCStyleCastChecker::checkASTCodeBody(const Decl *D,
- AnalysisManager &AM,
- BugReporter &BR) const {
-
- AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
-
+static StatementMatcher makeCastsMatcher() {
auto DynamicCastM = callExpr(callee(functionDecl(hasName("safeMetaCast"))));
// 'allocClassWithName' allocates an object with the given type.
// The type is actually provided as a string argument (type's name).
@@ -110,8 +111,15 @@ void OSObjectCStyleCastChecker::checkASTCodeBody(const Decl *D,
unless(anyOf(DynamicCastM, AllocClassWithNameM))))))
.bind(WarnAtNode);
- auto Matches =
- match(stmt(forEachDescendant(CastM)), *D->getBody(), AM.getASTContext());
+ return stmt(forEachDescendant(CastM));
+}
+
+void OSObjectCStyleCastChecker::checkASTCodeBody(const Decl *D,
+ AnalysisManager &AM,
+ BugReporter &BR) const {
+ AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
+
+ auto Matches = match(CastsM, *D->getBody(), AM.getASTContext());
for (BoundNodes Match : Matches)
emitDiagnostics(Match, BR, ADC, this);
}
More information about the cfe-commits
mailing list