[clang] [analyzer][NFC] Skip RetainSummaryManager's OSObject and ISL checks when they cannot match (PR #246458)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 09:55:06 PST 2026
https://github.com/Xazax-hun updated https://github.com/llvm/llvm-project/pull/246458
>From c6f7d7845fad4f931024d3e6e7218f13eb9987b4 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Thu, 26 Nov 2026 17:29:13 +0000
Subject: [PATCH 1/2] [analyzer][NFC] Skip RetainSummaryManager's OSObject and
ISL checks when they cannot match
For every call it summarizes, RetainSummaryManager runs AST matchers to
check whether a record derives from OSMetaClassBase, and prints parameter
types to check for an "isl_" prefix. Neither can match if no identifier
in the translation unit is named OSMetaClassBase or starts with "isl_",
which is the case for most translation units, so skip them then.
On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces
the instructions of the static analyzer by 1.5-2.4%.
Assisted-by: Claude Opus 5.5
---
.../clang/Analysis/RetainSummaryManager.h | 30 ++++++++++++++++++-
clang/lib/Analysis/RetainSummaryManager.cpp | 13 ++++----
2 files changed, 35 insertions(+), 8 deletions(-)
diff --git a/clang/include/clang/Analysis/RetainSummaryManager.h b/clang/include/clang/Analysis/RetainSummaryManager.h
index 797685ac7f2ef9..e2cf6f08599577 100644
--- a/clang/include/clang/Analysis/RetainSummaryManager.h
+++ b/clang/include/clang/Analysis/RetainSummaryManager.h
@@ -457,6 +457,14 @@ class RetainSummaryManager {
/// Track sublcasses of OSObject.
const bool TrackOSObjects;
+ /// Whether the translation unit has an identifier named OSMetaClassBase,
+ /// without which no class can be an OSObject.
+ const bool MayHaveOSObjects;
+
+ /// Whether the translation unit has an identifier starting with "isl_",
+ /// without which no type can be an ISL object.
+ const bool MayHaveISLTypes;
+
/// FuncSummaries - A map from FunctionDecls to summaries.
FuncSummariesTy FuncSummaries;
@@ -486,6 +494,19 @@ class RetainSummaryManager {
/// effects.
llvm::FoldingSet<CachedSummaryNode> SimpleSummaries;
+ /// Whether \p D is a subclass of OSObject.
+ bool isOSObjectSubclass(const Decl *D) const;
+
+ /// Whether the type is a pointer to a subclass of OSObject.
+ bool isOSObjectPtr(QualType QT) const;
+
+ /// Whether the method is declared on a subclass of OSObject, or any of its
+ /// parameters is a pointer to a subclass of OSObject.
+ bool isOSObjectRelated(const CXXMethodDecl *MD) const;
+
+ /// Whether the type is printed with an "isl_" prefix.
+ bool isISLObjectRef(QualType Ty) const;
+
/// Create an OS object at +1.
const RetainSummary *getOSSummaryCreateRule(const FunctionDecl *FD);
@@ -616,7 +637,14 @@ class RetainSummaryManager {
bool trackOSObjects)
: Ctx(ctx), ARCEnabled((bool)Ctx.getLangOpts().ObjCAutoRefCount),
TrackObjCAndCFObjects(trackObjCAndCFObjects),
- TrackOSObjects(trackOSObjects), AF(BPAlloc),
+ TrackOSObjects(trackOSObjects),
+ // is_contained uses find, so the lookup does not create the
+ // identifier.
+ MayHaveOSObjects(llvm::is_contained(ctx.Idents, "OSMetaClassBase")),
+ MayHaveISLTypes(llvm::any_of(
+ ctx.Idents,
+ [](const auto &E) { return E.getKey().starts_with("isl_"); })),
+ AF(BPAlloc),
ObjCAllocRetE(ARCEnabled ? RetEffect::MakeNotOwned(ObjKind::ObjC)
: RetEffect::MakeOwned(ObjKind::ObjC)),
ObjCInitRetE(ARCEnabled ? RetEffect::MakeNotOwned(ObjKind::ObjC)
diff --git a/clang/lib/Analysis/RetainSummaryManager.cpp b/clang/lib/Analysis/RetainSummaryManager.cpp
index 4235dfc747c6d2..5851e2364043fe 100644
--- a/clang/lib/Analysis/RetainSummaryManager.cpp
+++ b/clang/lib/Analysis/RetainSummaryManager.cpp
@@ -151,8 +151,8 @@ static bool isExactClass(const Decl *D, StringRef ClassName) {
return !(match(sameClassM, *D, D->getASTContext()).empty());
}
-static bool isOSObjectSubclass(const Decl *D) {
- return D && isSubclass(D, "OSMetaClassBase") &&
+bool RetainSummaryManager::isOSObjectSubclass(const Decl *D) const {
+ return MayHaveOSObjects && D && isSubclass(D, "OSMetaClassBase") &&
!isExactClass(D, "OSMetaClass");
}
@@ -166,13 +166,12 @@ static bool isOSObjectThisCast(StringRef S) {
return S == "metaCast";
}
-
-static bool isOSObjectPtr(QualType QT) {
+bool RetainSummaryManager::isOSObjectPtr(QualType QT) const {
return isOSObjectSubclass(QT->getPointeeCXXRecordDecl());
}
-static bool isISLObjectRef(QualType Ty) {
- return StringRef(Ty.getAsString()).starts_with("isl_");
+bool RetainSummaryManager::isISLObjectRef(QualType Ty) const {
+ return MayHaveISLTypes && StringRef(Ty.getAsString()).starts_with("isl_");
}
static bool isOSIteratorSubclass(const Decl *D) {
@@ -208,7 +207,7 @@ static bool isMakeCollectable(StringRef FName) {
/// A function is OSObject related if it is declared on a subclass
/// of OSObject, or any of the parameters is a subclass of an OSObject.
-static bool isOSObjectRelated(const CXXMethodDecl *MD) {
+bool RetainSummaryManager::isOSObjectRelated(const CXXMethodDecl *MD) const {
if (isOSObjectSubclass(MD->getParent()))
return true;
>From 813341a848d23fca3c23fb87b32f87dd72ca6931 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Thu, 26 Nov 2026 17:54:49 +0000
Subject: [PATCH 2/2] [analyzer][NFC] Address review comments
Assisted-by: Claude Opus 5.5
---
.../include/clang/Analysis/RetainSummaryManager.h | 15 +++++----------
clang/lib/Analysis/RetainSummaryManager.cpp | 6 ++++++
2 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/clang/include/clang/Analysis/RetainSummaryManager.h b/clang/include/clang/Analysis/RetainSummaryManager.h
index e2cf6f08599577..74eb87622528dd 100644
--- a/clang/include/clang/Analysis/RetainSummaryManager.h
+++ b/clang/include/clang/Analysis/RetainSummaryManager.h
@@ -457,14 +457,14 @@ class RetainSummaryManager {
/// Track sublcasses of OSObject.
const bool TrackOSObjects;
- /// Whether the translation unit has an identifier named OSMetaClassBase,
- /// without which no class can be an OSObject.
+ /// Whether the TU has an identifier named OSMetaClassBase.
const bool MayHaveOSObjects;
- /// Whether the translation unit has an identifier starting with "isl_",
- /// without which no type can be an ISL object.
+ /// Whether the TU has an identifier starting with "isl_".
const bool MayHaveISLTypes;
+ static bool hasISLIdentifiers(const IdentifierTable &Idents);
+
/// FuncSummaries - A map from FunctionDecls to summaries.
FuncSummariesTy FuncSummaries;
@@ -638,13 +638,8 @@ class RetainSummaryManager {
: Ctx(ctx), ARCEnabled((bool)Ctx.getLangOpts().ObjCAutoRefCount),
TrackObjCAndCFObjects(trackObjCAndCFObjects),
TrackOSObjects(trackOSObjects),
- // is_contained uses find, so the lookup does not create the
- // identifier.
MayHaveOSObjects(llvm::is_contained(ctx.Idents, "OSMetaClassBase")),
- MayHaveISLTypes(llvm::any_of(
- ctx.Idents,
- [](const auto &E) { return E.getKey().starts_with("isl_"); })),
- AF(BPAlloc),
+ MayHaveISLTypes(hasISLIdentifiers(ctx.Idents)), AF(BPAlloc),
ObjCAllocRetE(ARCEnabled ? RetEffect::MakeNotOwned(ObjKind::ObjC)
: RetEffect::MakeOwned(ObjKind::ObjC)),
ObjCInitRetE(ARCEnabled ? RetEffect::MakeNotOwned(ObjKind::ObjC)
diff --git a/clang/lib/Analysis/RetainSummaryManager.cpp b/clang/lib/Analysis/RetainSummaryManager.cpp
index 5851e2364043fe..ea082f1c1823fd 100644
--- a/clang/lib/Analysis/RetainSummaryManager.cpp
+++ b/clang/lib/Analysis/RetainSummaryManager.cpp
@@ -174,6 +174,12 @@ bool RetainSummaryManager::isISLObjectRef(QualType Ty) const {
return MayHaveISLTypes && StringRef(Ty.getAsString()).starts_with("isl_");
}
+bool RetainSummaryManager::hasISLIdentifiers(const IdentifierTable &Idents) {
+ return llvm::any_of(Idents, [](const auto &Entry) {
+ return Entry.getKey().starts_with("isl_");
+ });
+}
+
static bool isOSIteratorSubclass(const Decl *D) {
return isSubclass(D, "OSIterator");
}
More information about the cfe-commits
mailing list