[clang] [analyzer] Check the nonnull annotation first for parameters in NullabilityChecker (PR #245167)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 01:21:56 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Gábor Horváth (Xazax-hun)
<details>
<summary>Changes</summary>
NullabilityChecker checks on every dead-symbol callback (and before reporting) whether the nonnull preconditions of the current function are violated. checkParamsForPreconditionViolation computed the lvalue of every parameter, i.e. looked up its VarRegion, before checking whether the parameter is annotated _Nonnull, which it rarely is. Check the annotation first.
On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces the instructions of the static analyzer by 0.3-0.4% and the cycles by 0.7-1.2%.
Assisted-by: Claude Opus 5.5
---
Full diff: https://github.com/llvm/llvm-project/pull/245167.diff
1 Files Affected:
- (modified) clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp (+5)
``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
index fd82fa2ff92a58..828107186b19fd 100644
--- a/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
@@ -405,6 +405,11 @@ static bool checkParamsForPreconditionViolation(ArrayRef<ParmVarDecl *> Params,
if (ParamDecl->isParameterPack())
break;
+ // Only nonnull parameters can violate the precondition; check this before
+ // looking up the region of the parameter.
+ if (getNullabilityAnnotation(ParamDecl->getType()) != Nullability::Nonnull)
+ continue;
+
SVal LV = State->getLValue(ParamDecl, SF);
if (checkValueAtLValForInvariantViolation(State, LV,
ParamDecl->getType())) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/245167
More information about the cfe-commits
mailing list