[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 13:24:49 PST 2025


================
@@ -10069,6 +10069,17 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
     case UO_AddrOf: // should be impossible
       return IntRange::forValueOfType(C, GetExprType(E));
 
+    case UO_Not: {
+      std::optional<IntRange> SubRange = TryGetExprRange(
+          C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
+      if (!SubRange)
+        return std::nullopt;
+
+      // The width increments by 1 if the sub-expression cannot be negative
+      // since it now can be.
+      return IntRange(SubRange->Width + (int)SubRange->NonNegative, false);
----------------
zygoloid wrote:

Does this do the right thing for `~` applied to an unsigned integer? In that case I think we know nothing useful about the range beyond the range of the type, because `IntRange` always treats 0 as being representable, which means that `UINTn_MAX` is always a possible value of the result.

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


More information about the cfe-commits mailing list