PATCH RFC: -Wstrict-overflow, take 2

Richard Guenther richard.guenther@gmail.com
Fri Feb 9 10:26:00 GMT 2007


On 08 Feb 2007 18:51:26 -0800, Ian Lance Taylor <iant@google.com> wrote:
> Here is a new version of my -Wstrict-overflow patch.  This
> incorporates all the comments made on the first version.
>
>
> -Wstrict-overflow is a new warning about cases where the compiler
> optimizes based on the assumption that signed overflow is undefined.
> This warning can produce a number of false positives, because the
> values which actually appear in the program may be such that overflow
> never does, in fact, occur.  Therefore, the warning comes in five
> levels.
>
> -Wstrict-overflow is equivalent to -Wstrict-overflow=1.  This level is
> automatically enabled by -Wall.  It warns about questionable cases
> which are easy to avoid.  For example, it will warn about optimizing
> (x + 1 > x) to 1 (this optimization will yield an unexpected result if
> X == INT_MAX).
>
> -Wstrict-overflow=2 additionally warns about cases where a conditional
> expression is folded to a constant.  A typical example would be
> (abs (x) >= 0), which will be folded to 1 (this optimization will yield
> an unexpected result if x == INT_MIN).
>
> -Wstrict-overflow=3 additionally warns about cases where a comparison
> is changed in some way other than folding it to a constant.  A typical
> example would be converting (x + 1 > 1) to (x > 0) (this optimization
> will yield an unexpected result if x == INT_MAX).
>
> -Wstrict-overflow=4 additionally warns about warnings not covered by
> the above cases (or the case below).  A typical example would be
> converting ((x * 10) / 5) to (x * 2) (this optimization will yield an
> unexpected result if x == ((INT_MAX / 2) - 1)).
>
> -Wstrict-overflow=5 additionally warns about reducing the magnitude of
> a constant involved in a comparison.  This is a particularly fertile
> source of false positives, as it is a canonicalization rather than an
> optimization per se.  That is why it is segregated from the other
> options.  For example: changing (x + 2 > y) to (x + 1 >= y) (this
> optimization will yield an unexpected result if x == INT_MAX - 1).

Are levels 4 and 5 useful at all?  I wonder what would be a good level
to turn on (apart from -Wall) for a distributor builting random packages.
Did you try building emacs? ;)

Does -Wstrict-overflow=3 -Werror=strict-overflow=1 work?  Hopefully
at least -Wno-error=strict-overflow does.

          if (negate_expr_p (tem))
-            return fold_build2 (TREE_CODE (t), type,
-                               TREE_OPERAND (t, 0), negate_expr (tem));
+           {
+             if (INTEGRAL_TYPE_P (type)
+                 && (TREE_CODE (tem) != INTEGER_CST
+                     || tree_int_cst_equal (tem,
+                                            build_int_cst (type, 1))))

  integer_onep (tem)

+               fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
+             return fold_build2 (TREE_CODE (t), type,
+                                 TREE_OPERAND (t, 0), negate_expr (tem));
+           }


Thanks,
Richard.



More information about the Gcc-patches mailing list