[clang] [analyzer][NFC] Skip NumberObjectConversionChecker in TUs without number objects (PR #246446)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 10:04:50 PST 2026


https://github.com/Xazax-hun updated https://github.com/llvm/llvm-project/pull/246446

>From 8be208b978db7dfd9f8f6c0ad4ee59cd334af72b Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Thu, 26 Nov 2026 16:40:45 +0000
Subject: [PATCH 1/3] [analyzer] Skip NumberObjectConversionChecker in TUs
 without number objects

NumberObjectConversionChecker runs its AST matcher on every function
body. Every match involves a declaration named CFNumberRef,
CFBooleanRef, OSBoolean, OSNumber or NSNumber, so nothing can match if
none of these identifiers is in the identifier table, which is the case
for most C++ translation units. Skip the matching then. The lookups use
IdentifierTable::find, so that they do not create the identifiers.
Declarations deserialized from a PCH or module add their identifiers to
the table, so this works with those too.

The matcher itself is already built only once per checker; this avoids
running it. On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr)
this reduces the instructions of the static analyzer by 0.6-2.2% and the
cycles by 0.9-2.4%. Peak memory is unchanged within noise. The
diagnostics and the analyzer statistics are unchanged.

Assisted-by: Claude Opus 5.5
---
 .../Checkers/NumberObjectConversionChecker.cpp        | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
index a05f204253b20b..742b94d95711eb 100644
--- a/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
@@ -345,6 +345,17 @@ NumberObjectConversionChecker::NumberObjectConversionChecker()
 void NumberObjectConversionChecker::checkASTCodeBody(const Decl *D,
                                                      AnalysisManager &AM,
                                                      BugReporter &BR) const {
+  // Every match involves a declaration with one of these names, so there is
+  // nothing to match if the translation unit has no such identifier. Use find
+  // so that the lookups do not create the identifiers.
+  static constexpr llvm::StringLiteral Names[] = {
+      "CFNumberRef", "CFBooleanRef", "OSBoolean", "OSNumber", "NSNumber"};
+  const IdentifierTable &Idents = AM.getASTContext().Idents;
+  if (llvm::none_of(Names, [&Idents](StringRef Name) {
+        return Idents.find(Name) != Idents.end();
+      }))
+    return;
+
   MatchFinder F;
   Callback CB(this, BR, AM.getAnalysisDeclContext(D));
 

>From 8c0fca130b0911595d4c991653de558b5bee5d63 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Thu, 26 Nov 2026 17:16:22 +0000
Subject: [PATCH 2/3] [analyzer][NFC] Address review comments

Assisted-by: Claude Opus 5.5
---
 .../Checkers/NumberObjectConversionChecker.cpp              | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
index 742b94d95711eb..72ae64db1d4d77 100644
--- a/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
@@ -346,13 +346,13 @@ void NumberObjectConversionChecker::checkASTCodeBody(const Decl *D,
                                                      AnalysisManager &AM,
                                                      BugReporter &BR) const {
   // Every match involves a declaration with one of these names, so there is
-  // nothing to match if the translation unit has no such identifier. Use find
-  // so that the lookups do not create the identifiers.
+  // nothing to match if the translation unit has no such identifier.
+  // is_contained uses find, so the lookups do not create the identifiers.
   static constexpr llvm::StringLiteral Names[] = {
       "CFNumberRef", "CFBooleanRef", "OSBoolean", "OSNumber", "NSNumber"};
   const IdentifierTable &Idents = AM.getASTContext().Idents;
   if (llvm::none_of(Names, [&Idents](StringRef Name) {
-        return Idents.find(Name) != Idents.end();
+        return llvm::is_contained(Idents, Name);
       }))
     return;
 

>From 60079674800e1dbf0c77c985152ad18bac56eae0 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Thu, 26 Nov 2026 18:04:31 +0000
Subject: [PATCH 3/3] [analyzer][NFC] Address review comments

Assisted-by: Claude Opus 5.5
---
 .../Checkers/NumberObjectConversionChecker.cpp         | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
index 72ae64db1d4d77..aee94b8eea4e6c 100644
--- a/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
@@ -345,15 +345,13 @@ NumberObjectConversionChecker::NumberObjectConversionChecker()
 void NumberObjectConversionChecker::checkASTCodeBody(const Decl *D,
                                                      AnalysisManager &AM,
                                                      BugReporter &BR) const {
-  // Every match involves a declaration with one of these names, so there is
-  // nothing to match if the translation unit has no such identifier.
-  // is_contained uses find, so the lookups do not create the identifiers.
   static constexpr llvm::StringLiteral Names[] = {
       "CFNumberRef", "CFBooleanRef", "OSBoolean", "OSNumber", "NSNumber"};
   const IdentifierTable &Idents = AM.getASTContext().Idents;
-  if (llvm::none_of(Names, [&Idents](StringRef Name) {
-        return llvm::is_contained(Idents, Name);
-      }))
+  auto IsDeclared = [&Idents](StringRef Name) {
+    return llvm::is_contained(Idents, Name);
+  };
+  if (llvm::none_of(Names, IsDeclared))
     return;
 
   MatchFinder F;



More information about the cfe-commits mailing list