[clang] 6ad01ad - [analyzer][NFC] Check the nonnull annotation first in NullabilityChecker::checkLocation (#245166)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 11:47:22 PST 2026


Author: Gábor Horváth
Date: 2026-11-23T19:47:17Z
New Revision: 6ad01ad74707ab9422b610b76f250f4966e96fa2

URL: https://github.com/llvm/llvm-project/commit/6ad01ad74707ab9422b610b76f250f4966e96fa2
DIFF: https://github.com/llvm/llvm-project/commit/6ad01ad74707ab9422b610b76f250f4966e96fa2.diff

LOG: [analyzer][NFC] Check the nonnull annotation first in NullabilityChecker::checkLocation (#245166)

NullabilityChecker::checkLocation is called for every load from a typed
region. It looked up the stored value in the store before checking
whether the region's type is annotated _Nonnull, which it rarely is.
Check the annotation first, so that the store lookup only happens for
annotated locations.

On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this
reduces the instructions of the static analyzer by 0.1-0.2%.

Assisted-by: Claude Opus 5.5

Co-authored-by: Gabor Horvath <gaborh at apple.com>

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
index 828107186b19f..2ac75f63d3b56 100644
--- a/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
@@ -626,21 +626,19 @@ void NullabilityChecker::checkLocation(SVal Location, bool IsLoad,
   if (!Region)
     return;
 
+  if (getNullabilityAnnotation(Region->getValueType()) != Nullability::Nonnull)
+    return;
+
   ProgramStateRef State = Context.getState();
 
   auto StoredVal = State->getSVal(Region).getAs<loc::MemRegionVal>();
   if (!StoredVal)
     return;
 
-  Nullability NullabilityOfTheLoadedValue =
-      getNullabilityAnnotation(Region->getValueType());
-
-  if (NullabilityOfTheLoadedValue == Nullability::Nonnull) {
-    // It doesn't matter what we think about this particular pointer, it should
-    // be considered non-null as annotated by the developer.
-    if (ProgramStateRef NewState = State->assume(*StoredVal, true)) {
-      Context.addTransition(NewState);
-    }
+  // It doesn't matter what we think about this particular pointer, it should
+  // be considered non-null as annotated by the developer.
+  if (ProgramStateRef NewState = State->assume(*StoredVal, true)) {
+    Context.addTransition(NewState);
   }
 }
 


        


More information about the cfe-commits mailing list