[clang] [analyzer] Improve solver (PR #112583)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 17 04:37:51 PDT 2024


================
@@ -2866,12 +2877,14 @@ ConditionTruthVal RangeConstraintManager::checkNull(ProgramStateRef State,
 
 const llvm::APSInt *RangeConstraintManager::getSymVal(ProgramStateRef St,
                                                       SymbolRef Sym) const {
-  const RangeSet *T = getConstraint(St, Sym);
-  return T ? T->getConcreteValue() : nullptr;
+  auto &MutableSelf = const_cast<RangeConstraintManager &>(*this);
+  return MutableSelf.getRange(St, Sym).getConcreteValue();
 }
 
 const llvm::APSInt *RangeConstraintManager::getSymMinVal(ProgramStateRef St,
                                                          SymbolRef Sym) const {
+  // TODO: Use `getRange()` like in `getSymVal()`, but that would make some
+  // of the reports of `BitwiseShiftChecker` look awkward.
----------------
NagyDonat wrote:

Just wonderful :upside_down_face: :heart:

These awkward messages demonstrate that `getRange()` _knows less_ than `evalBinOp()`: the code that creates the note calls `getMinValue()` and sees just `INT_MIN`, but when the actual check is performed with an `evalBinOp` call, it can prove that the value is actually at least 32.

As a quick workaround for the awkward reports I would be grateful if you tweaked `BitwiseShiftValidator::checkOvershift()` by extending the conditional 
```cpp
if (const llvm::APSInt *MinRight = SVB.getMinValue(FoldedState, Right)) {
  LowerBoundStr = formatv(" >= {0},", MinRight->getExtValue());
}
```
with a sanity check that only defines `LowerBoundStr` when `MinRight` is not smaller than `LHSBitWidth`.

On a longer term it would be nice if `getRange()` and `getMinValue` / `getMaxValue` would become more accurate and closer to `evalBinOp()`.

https://github.com/llvm/llvm-project/pull/112583


More information about the cfe-commits mailing list