[PATCH] D103440: [WIP][analyzer] Introduce range-based reasoning for addition operator

Manas Gupta via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 23 01:53:56 PDT 2021


manas added a comment.

Thanks @vsavchenko and everyone for helping! :)



================
Comment at: clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp:1075-1076
+  ///       LHSOpd binop RHSOpd == Result, where binop is any binary operation
+  bool hasOverflowed(llvm::APSInt LHSOpd, llvm::APSInt RHSOpd,
+                     llvm::APSInt &Result, QualType T) {
+    llvm::APSInt Zero = ValueFactory.getAPSIntType(T).getZeroValue();
----------------
vsavchenko wrote:
> manas wrote:
> > We should have these specific functions for other BO as well. Because they will lead us to reason about when `Operand1 binop Operand2` can overflow or not. I was thinking in the direction of having a simpler class which works for this.
> I searched through a codebase a bit and here are the couple of functions (from `APInt.h`) that can come in handy:
> ```
>   // Operations that return overflow indicators.
>   APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
>   APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
>   APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
>   APInt usub_ov(const APInt &RHS, bool &Overflow) const;
>   APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
>   APInt smul_ov(const APInt &RHS, bool &Overflow) const;
>   APInt umul_ov(const APInt &RHS, bool &Overflow) const;
>   APInt sshl_ov(const APInt &Amt, bool &Overflow) const;
>   APInt ushl_ov(const APInt &Amt, bool &Overflow) const;
> ```
> `APSInt` is derived from `APInt`, so we can totally use these.
That's great! I will incorporate those in the code instead of custom functions.


================
Comment at: clang/test/Analysis/constant-folding.c:269
+  // Checks for inclusive ranges for unsigned integers
+  if (a >= 0 && a <= 10 && b >= 0 && b <= 20) {
+    clang_analyzer_eval((a + b) >= 0); // expected-warning{{TRUE}}
----------------
vsavchenko wrote:
> They are already unsigned, we don't need `a >= 0` and `b >= 0`
Right! I will remove those constraints.


================
Comment at: clang/test/Analysis/constant-folding.c:330
+    clang_analyzer_eval((c + d) == INT_MAX - 22); // expected-warning{{FALSE}}
+  }
+}
----------------
vsavchenko wrote:
> I don't see the cases where we overflow on both ends and the case where we overflow on one end, but `Min > Max`.
My bad. I thought I added both overflows one. I will add them now.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103440/new/

https://reviews.llvm.org/D103440



More information about the cfe-commits mailing list