]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR preprocessor/55715 (bogus overflow warning for #if A-B when A<0 & B==minimum...
authorJoseph Myers <joseph@codesourcery.com>
Tue, 10 Dec 2013 01:23:37 +0000 (01:23 +0000)
committerJoseph Myers <jsm28@gcc.gnu.org>
Tue, 10 Dec 2013 01:23:37 +0000 (01:23 +0000)
PR preprocessor/55715
libcpp:
* expr.c (num_binary_op): Implement subtraction directly rather
than with negation and falling through into addition case.

gcc/testsuite:
* gcc.dg/cpp/expr-overflow-1.c: New test.

From-SVN: r205846

gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/cpp/expr-overflow-1.c [new file with mode: 0644]
libcpp/ChangeLog
libcpp/expr.c

index 27c81682364e58c0466089c134238507ffd1199e..7945eac39fb58e7c79dce02dcd7c5c0b8c986e6b 100644 (file)
@@ -1,3 +1,8 @@
+2013-12-09  Joseph Myers  <joseph@codesourcery.com>
+
+       PR preprocessor/55715
+       * gcc.dg/cpp/expr-overflow-1.c: New test.
+
 2013-12-10  Tobias Burnus  <burnus@net-b.de>
 
        PR fortran/59428
diff --git a/gcc/testsuite/gcc.dg/cpp/expr-overflow-1.c b/gcc/testsuite/gcc.dg/cpp/expr-overflow-1.c
new file mode 100644 (file)
index 0000000..8a67aaa
--- /dev/null
@@ -0,0 +1,44 @@
+/* Test overflow in preprocessor arithmetic.  PR 55715.  */
+/* { dg-do preprocess } */
+/* { dg-options "-std=c99" } */
+
+#include <stdint.h>
+
+#if -1 - INTMAX_MIN
+#endif
+
+#if 0 - INTMAX_MIN /* { dg-warning "overflow" } */
+#endif
+
+#if 1 * INTMAX_MIN
+#endif
+
+#if -1 * INTMAX_MIN /* { dg-warning "overflow" } */
+#endif
+
+#if 0 * INTMAX_MIN
+#endif
+
+#if -INTMAX_MIN /* { dg-warning "overflow" } */
+#endif
+
+#if +INTMAX_MIN
+#endif
+
+#if INTMAX_MIN / 1
+#endif
+
+#if INTMAX_MIN / -1 /* { dg-warning "overflow" } */
+#endif
+
+#if UINTMAX_MAX * UINTMAX_MAX
+#endif
+
+#if UINTMAX_MAX / -1
+#endif
+
+#if UINTMAX_MAX + INTMAX_MAX
+#endif
+
+#if UINTMAX_MAX - INTMAX_MIN
+#endif
index c3391b4959d66f56b25d60eb2e480a1c747067b9..4a06d0c3a63217dcded0830cd384db1650fe5aed 100644 (file)
@@ -1,3 +1,9 @@
+2013-12-09  Joseph Myers  <joseph@codesourcery.com>
+
+       PR preprocessor/55715
+       * expr.c (num_binary_op): Implement subtraction directly rather
+       than with negation and falling through into addition case.
+
 2013-11-18  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
 
        * lex.c (search_line_fast): Correct for little endian.
index c0098073ab89a70aac3bd391e035bfc8fc893068..1e17b005deea26ec37bf0c015b0a5fdae8d136b4 100644 (file)
@@ -1836,7 +1836,22 @@ num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
 
       /* Arithmetic.  */
     case CPP_MINUS:
-      rhs = num_negate (rhs, precision);
+      result.low = lhs.low - rhs.low;
+      result.high = lhs.high - rhs.high;
+      if (result.low > lhs.low)
+       result.high--;
+      result.unsignedp = lhs.unsignedp || rhs.unsignedp;
+      result.overflow = false;
+
+      result = num_trim (result, precision);
+      if (!result.unsignedp)
+       {
+         bool lhsp = num_positive (lhs, precision);
+         result.overflow = (lhsp != num_positive (rhs, precision)
+                            && lhsp != num_positive (result, precision));
+       }
+      return result;
+
     case CPP_PLUS:
       result.low = lhs.low + rhs.low;
       result.high = lhs.high + rhs.high;