@@ -1401,6 +1401,18 @@ c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
? G_("left shift count >= width of type")
: G_("right shift count >= width of type")));
}
+ if (code == LSHIFT_EXPR
+ /* If either OP0 has been folded to INTEGER_CST... */
+ && ((TREE_CODE (orig_op0) != INTEGER_CST
+ && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
+ && TREE_CODE (op0) == INTEGER_CST)
+ /* ...or if OP1 has been folded to INTEGER_CST... */
+ || (TREE_CODE (orig_op1) != INTEGER_CST
+ && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
+ && TREE_CODE (op1) == INTEGER_CST))
+ && c_inhibit_evaluation_warnings == 0)
+ /* ...then maybe we can detect an overflow. */
+ maybe_warn_shift_overflow (loc, op0, op1);
if ((code == TRUNC_DIV_EXPR
|| code == CEIL_DIV_EXPR
|| code == FLOOR_DIV_EXPR
@@ -12226,6 +12238,44 @@ maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
}
}
+/* Warn if signed left shift overflows. Note that we don't warn
+ about left-shifting 1 into the sign bit; cf.
+ <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
+ for C++ and <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1817.htm>
+ for C. LOC is a location of the shift; OP0 and OP1 are the operands.
+ Return true if an overflow is detected, false otherwise. */
+
+bool
+maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
+{
+ if (TREE_CODE (op0) != INTEGER_CST
+ || TREE_CODE (op1) != INTEGER_CST)
+ return false;
+
+ tree type0 = TREE_TYPE (op0);
+
+ /* Left-hand operand must be signed. */
+ if (TYPE_UNSIGNED (type0))
+ return false;
+
+ /* Compute the result in infinite precision math (sort of). */
+ widest_int w = wi::lshift (wi::to_widest (op0), wi::to_widest (op1));
+ unsigned int min_prec = wi::min_precision (w, SIGNED);
+ /* Watch out for shifting a negative value. */
+ tree r = wide_int_to_tree (tree_int_cst_sgn (op0) >= 0
+ ? unsigned_type_for (type0)
+ : type0, w);
+ bool overflowed = wi::cmps (w, wi::to_widest (r));
+ if (overflowed && c_inhibit_evaluation_warnings == 0)
+ warning_at (loc, OPT_Wshift_overflow,
+ "result of %qE requires %u bits to represent, "
+ "but %qT only has %u bits",
+ build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
+ min_prec, type0, TYPE_PRECISION (type0));
+
+ return overflowed;
+}
+
/* The C and C++ parsers both use vectors to hold function arguments.
For efficiency, we keep a cache of unused vectors. This is the
cache. */
@@ -1061,6 +1061,7 @@ extern void record_locally_defined_typedef (tree);
extern void maybe_record_typedef_use (tree);
extern void maybe_warn_unused_local_typedefs (void);
extern void maybe_warn_bool_compare (location_t, enum tree_code, tree, tree);
+extern bool maybe_warn_shift_overflow (location_t, tree, tree);
extern vec<tree, va_gc> *make_tree_vector (void);
extern void release_tree_vector (vec<tree, va_gc> *);
extern vec<tree, va_gc> *make_tree_vector_single (tree);
@@ -866,6 +866,10 @@ c_common_post_options (const char **pfilename)
if (warn_implicit_int == -1)
warn_implicit_int = flag_isoc99;
+ /* -Wshift-overflow is enabled by default in C99 and C++11 modes. */
+ if (warn_shift_overflow == -1)
+ warn_shift_overflow = cxx_dialect >= cxx11 || flag_isoc99;
+
/* -Wshift-negative-value is enabled by -Wextra in C99 and C++11 modes. */
if (warn_shift_negative_value == -1)
warn_shift_negative_value = (extra_warnings
@@ -780,6 +780,10 @@ Wshadow-ivar
ObjC ObjC++ Var(warn_shadow_ivar) EnabledBy(Wshadow) Init(1) Warning
Warn if a local declaration hides an instance variable
+Wshift-overflow
+C ObjC C++ ObjC++ Var(warn_shift_overflow) Init(-1) Warning
+Warn if left shift of a signed value overflows
+
Wshift-count-negative
C ObjC C++ ObjC++ Var(warn_shift_count_negative) Init(1) Warning
Warn if shift count is negative
@@ -6909,7 +6909,7 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype,
inside_init = error_mark_node;
}
else if (require_constant && !maybe_const)
- pedwarn_init (init_loc, 0,
+ pedwarn_init (init_loc, OPT_Wpedantic,
"initializer element is not a constant expression");
return inside_init;
@@ -10630,15 +10630,15 @@ build_binary_op (location_t location, enum tree_code code,
converted = 1;
}
else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
- && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
- && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
- && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
+ && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
+ && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
+ && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
{
result_type = type0;
converted = 1;
}
else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
- && code1 == INTEGER_TYPE)
+ && code1 == INTEGER_TYPE)
{
doing_shift = true;
if (TREE_CODE (op1) == INTEGER_CST)
@@ -10680,15 +10680,15 @@ build_binary_op (location_t location, enum tree_code code,
converted = 1;
}
else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
- && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
- && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
- && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
+ && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
+ && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
+ && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
{
result_type = type0;
converted = 1;
}
else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
- && code1 == INTEGER_TYPE)
+ && code1 == INTEGER_TYPE)
{
doing_shift = true;
if (TREE_CODE (op0) == INTEGER_CST
@@ -10711,7 +10711,6 @@ build_binary_op (location_t location, enum tree_code code,
warning_at (location, OPT_Wshift_count_negative,
"left shift count is negative");
}
-
else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
{
int_const = false;
@@ -10719,6 +10718,10 @@ build_binary_op (location_t location, enum tree_code code,
warning_at (location, OPT_Wshift_count_overflow,
"left shift count >= width of type");
}
+ else if (TREE_CODE (op0) == INTEGER_CST
+ && maybe_warn_shift_overflow (location, op0, op1)
+ && flag_isoc99)
+ int_const = false;
}
/* Use the type of the value to be shifted. */
@@ -4360,6 +4360,9 @@ cp_build_binary_op (location_t location,
warning (OPT_Wshift_count_overflow,
"left shift count >= width of type");
}
+ else if (TREE_CODE (const_op0) == INTEGER_CST
+ && (complain & tf_warning))
+ maybe_warn_shift_overflow (location, const_op0, const_op1);
}
/* Avoid converting op1 to result_type later. */
converted = 1;
@@ -273,6 +273,7 @@ Objective-C and Objective-C++ Dialects}.
-Wpointer-arith -Wno-pointer-to-int-cast @gol
-Wredundant-decls -Wno-return-local-addr @gol
-Wreturn-type -Wsequence-point -Wshadow -Wno-shadow-ivar @gol
+-Wshift-overflow @gol
-Wshift-count-negative -Wshift-count-overflow -Wshift-negative-value @gol
-Wsign-compare -Wsign-conversion -Wfloat-conversion @gol
-Wsizeof-pointer-memaccess -Wsizeof-array-argument @gol
@@ -3981,6 +3982,13 @@ Warn if shift count >= width of type. This warning is enabled by default.
Warn if left shifting a negative value. This warning is enabled by
@option{-Wextra} in C99 and C++11 modes (and newer).
+@item -Wshift-overflow
+@opindex Wshift-overflow
+@opindex Wno-shift-overflow
+Warn if left shift overflows. This warning is enabled by
+default in C99 and C++11 modes (and newer). This warning does not warn
+about left-shifting 1 into the sign bit.
+
@item -Wswitch
@opindex Wswitch
@opindex Wno-switch
@@ -0,0 +1,58 @@
+/* PR c++/55095 */
+/* { dg-do compile { target int32 } } */
+/* { dg-options "-O" } */
+/* { dg-additional-options "-std=c++11" { target c++ } } */
+
+#define INTM1 (sizeof (int) * __CHAR_BIT__ - 1)
+#define INTM2 (sizeof (int) * __CHAR_BIT__ - 2)
+#define LLONGM1 (sizeof (long long) * __CHAR_BIT__ - 1)
+#define LLONGM2 (sizeof (long long) * __CHAR_BIT__ - 2)
+
+int i1 = 1 << INTM1;
+int i2 = 9 << INTM1; /* { dg-warning "requires 36 bits to represent" } */
+int i3 = 10 << INTM2; /* { dg-warning "requires 35 bits to represent" } */
+int i4 = __INT_MAX__ << 2; /* { dg-warning "requires 34 bits to represent" } */
+int i5 = __INT_MAX__ << INTM1; /* { dg-warning "requires 63 bits to represent" } */
+int i6 = -1 << INTM1;
+int i7 = -9 << INTM1; /* { dg-warning "requires 36 bits to represent" } */
+int i8 = -10 << INTM2; /* { dg-warning "requires 35 bits to represent" } */
+int i9 = -__INT_MAX__ << 2; /* { dg-warning "requires 34 bits to represent" } */
+int i10 = -__INT_MAX__ << INTM1; /* { dg-warning "requires 63 bits to represent" } */
+
+int r1 = 1 >> INTM1;
+int r2 = 9 >> INTM1;
+int r3 = 10 >> INTM2;
+int r4 = __INT_MAX__ >> 2;
+int r5 = __INT_MAX__ >> INTM1;
+int r6 = -1 >> INTM1;
+int r7 = -9 >> INTM1;
+int r8 = -10 >> INTM2;
+int r9 = -__INT_MAX__ >> 2;
+int r10 = -__INT_MAX__ >> INTM1;
+
+unsigned u1 = 1 << INTM1;
+unsigned u2 = 9 << INTM1; /* { dg-warning "requires 36 bits to represent" } */
+unsigned u3 = 2U << INTM1;
+unsigned u4 = 9U << INTM1;
+unsigned u5 = 10U << INTM2;
+
+long long int l1 = 1LL << LLONGM1;
+long long int l2 = 9LL << LLONGM1; /* { dg-warning "requires 68 bits to represent" } */
+long long int l3 = 10LL << LLONGM2; /* { dg-warning "requires 67 bits to represent" } */
+long long int l4 = __LONG_LONG_MAX__ << 2; /* { dg-warning "requires 66 bits to represent" } */
+long long int l5 = __LONG_LONG_MAX__ << LLONGM1; /* { dg-warning "requires 127 bits to represent" } */
+long long int l6 = -1LL << LLONGM1;
+long long int l7 = -9LL << LLONGM1; /* { dg-warning "requires 68 bits to represent" } */
+long long int l8 = -10LL << LLONGM2; /* { dg-warning "requires 67 bits to represent" } */
+long long int l9 = -__LONG_LONG_MAX__ << 2; /* { dg-warning "requires 66 bits to represent" } */
+long long int l10 = -__LONG_LONG_MAX__ << LLONGM1; /* { dg-warning "requires 127 bits to represent" } */
+
+void
+fn (void)
+{
+ const int a = 10;
+ const __SIZE_TYPE__ b = INTM1;
+ int k1 = a << b; /* { dg-warning "requires 36 bits to represent" } */
+ int k2 = 10 << b; /* { dg-warning "requires 36 bits to represent" } */
+ int k3 = a << INTM1; /* { dg-warning "requires 36 bits to represent" } */
+}
@@ -0,0 +1,58 @@
+/* PR c++/55095 */
+/* { dg-do compile { target int32 } } */
+/* { dg-options "-O -Wno-shift-overflow" } */
+/* { dg-additional-options "-std=c++11" { target c++ } } */
+
+#define INTM1 (sizeof (int) * __CHAR_BIT__ - 1)
+#define INTM2 (sizeof (int) * __CHAR_BIT__ - 2)
+#define LLONGM1 (sizeof (long long) * __CHAR_BIT__ - 1)
+#define LLONGM2 (sizeof (long long) * __CHAR_BIT__ - 2)
+
+int i1 = 1 << INTM1;
+int i2 = 9 << INTM1;
+int i3 = 10 << INTM2;
+int i4 = __INT_MAX__ << 2;
+int i5 = __INT_MAX__ << INTM1;
+int i6 = -1 << INTM1;
+int i7 = -9 << INTM1;
+int i8 = -10 << INTM2;
+int i9 = -__INT_MAX__ << 2;
+int i10 = -__INT_MAX__ << INTM1;
+
+int r1 = 1 >> INTM1;
+int r2 = 9 >> INTM1;
+int r3 = 10 >> INTM2;
+int r4 = __INT_MAX__ >> 2;
+int r5 = __INT_MAX__ >> INTM1;
+int r6 = -1 >> INTM1;
+int r7 = -9 >> INTM1;
+int r8 = -10 >> INTM2;
+int r9 = -__INT_MAX__ >> 2;
+int r10 = -__INT_MAX__ >> INTM1;
+
+unsigned u1 = 1 << INTM1;
+unsigned u2 = 9 << INTM1;
+unsigned u3 = 2U << INTM1;
+unsigned u4 = 9U << INTM1;
+unsigned u5 = 10U << INTM2;
+
+long long int l1 = 1LL << LLONGM1;
+long long int l2 = 9LL << LLONGM1;
+long long int l3 = 10LL << LLONGM2;
+long long int l4 = __LONG_LONG_MAX__ << 2;
+long long int l5 = __LONG_LONG_MAX__ << LLONGM1;
+long long int l6 = -1LL << LLONGM1;
+long long int l7 = -9LL << LLONGM1;
+long long int l8 = -10LL << LLONGM2;
+long long int l9 = -__LONG_LONG_MAX__ << 2;
+long long int l10 = -__LONG_LONG_MAX__ << LLONGM1;
+
+void
+fn (void)
+{
+ const int a = 10;
+ const __SIZE_TYPE__ b = INTM1;
+ int k1 = a << b;
+ int k2 = 10 << b;
+ int k3 = a << INTM1;
+}
@@ -0,0 +1,59 @@
+/* PR c++/55095 */
+/* { dg-do compile { target int32 } } */
+/* { dg-options "-O -Wshift-overflow" } */
+/* { dg-additional-options "-std=gnu90" { target c } } */
+/* { dg-additional-options "-std=c++03" { target c++ } } */
+
+#define INTM1 (sizeof (int) * __CHAR_BIT__ - 1)
+#define INTM2 (sizeof (int) * __CHAR_BIT__ - 2)
+#define LLONGM1 (sizeof (long long) * __CHAR_BIT__ - 1)
+#define LLONGM2 (sizeof (long long) * __CHAR_BIT__ - 2)
+
+int i1 = 1 << INTM1;
+int i2 = 9 << INTM1; /* { dg-warning "requires 36 bits to represent" } */
+int i3 = 10 << INTM2; /* { dg-warning "requires 35 bits to represent" } */
+int i4 = __INT_MAX__ << 2; /* { dg-warning "requires 34 bits to represent" } */
+int i5 = __INT_MAX__ << INTM1; /* { dg-warning "requires 63 bits to represent" } */
+int i6 = -1 << INTM1;
+int i7 = -9 << INTM1; /* { dg-warning "requires 36 bits to represent" } */
+int i8 = -10 << INTM2; /* { dg-warning "requires 35 bits to represent" } */
+int i9 = -__INT_MAX__ << 2; /* { dg-warning "requires 34 bits to represent" } */
+int i10 = -__INT_MAX__ << INTM1; /* { dg-warning "requires 63 bits to represent" } */
+
+int r1 = 1 >> INTM1;
+int r2 = 9 >> INTM1;
+int r3 = 10 >> INTM2;
+int r4 = __INT_MAX__ >> 2;
+int r5 = __INT_MAX__ >> INTM1;
+int r6 = -1 >> INTM1;
+int r7 = -9 >> INTM1;
+int r8 = -10 >> INTM2;
+int r9 = -__INT_MAX__ >> 2;
+int r10 = -__INT_MAX__ >> INTM1;
+
+unsigned u1 = 1 << INTM1;
+unsigned u2 = 9 << INTM1; /* { dg-warning "requires 36 bits to represent" } */
+unsigned u3 = 2U << INTM1;
+unsigned u4 = 9U << INTM1;
+unsigned u5 = 10U << INTM2;
+
+long long int l1 = 1LL << LLONGM1;
+long long int l2 = 9LL << LLONGM1; /* { dg-warning "requires 68 bits to represent" } */
+long long int l3 = 10LL << LLONGM2; /* { dg-warning "requires 67 bits to represent" } */
+long long int l4 = __LONG_LONG_MAX__ << 2; /* { dg-warning "requires 66 bits to represent" } */
+long long int l5 = __LONG_LONG_MAX__ << LLONGM1; /* { dg-warning "requires 127 bits to represent" } */
+long long int l6 = -1LL << LLONGM1;
+long long int l7 = -9LL << LLONGM1; /* { dg-warning "requires 68 bits to represent" } */
+long long int l8 = -10LL << LLONGM2; /* { dg-warning "requires 67 bits to represent" } */
+long long int l9 = -__LONG_LONG_MAX__ << 2; /* { dg-warning "requires 66 bits to represent" } */
+long long int l10 = -__LONG_LONG_MAX__ << LLONGM1; /* { dg-warning "requires 127 bits to represent" } */
+
+void
+fn (void)
+{
+ const int a = 10;
+ const __SIZE_TYPE__ b = INTM1;
+ int k1 = a << b; /* { dg-warning "requires 36 bits to represent" } */
+ int k2 = 10 << b; /* { dg-warning "requires 36 bits to represent" } */
+ int k3 = a << INTM1; /* { dg-warning "requires 36 bits to represent" } */
+}
@@ -0,0 +1,59 @@
+/* PR c++/55095 */
+/* { dg-do compile { target int32 } } */
+/* { dg-options "-O" } */
+/* { dg-additional-options "-std=gnu90" { target c } } */
+/* { dg-additional-options "-std=c++03" { target c++ } } */
+
+#define INTM1 (sizeof (int) * __CHAR_BIT__ - 1)
+#define INTM2 (sizeof (int) * __CHAR_BIT__ - 2)
+#define LLONGM1 (sizeof (long long) * __CHAR_BIT__ - 1)
+#define LLONGM2 (sizeof (long long) * __CHAR_BIT__ - 2)
+
+int i1 = 1 << INTM1;
+int i2 = 9 << INTM1;
+int i3 = 10 << INTM2;
+int i4 = __INT_MAX__ << 2;
+int i5 = __INT_MAX__ << INTM1;
+int i6 = -1 << INTM1;
+int i7 = -9 << INTM1;
+int i8 = -10 << INTM2;
+int i9 = -__INT_MAX__ << 2;
+int i10 = -__INT_MAX__ << INTM1;
+
+int r1 = 1 >> INTM1;
+int r2 = 9 >> INTM1;
+int r3 = 10 >> INTM2;
+int r4 = __INT_MAX__ >> 2;
+int r5 = __INT_MAX__ >> INTM1;
+int r6 = -1 >> INTM1;
+int r7 = -9 >> INTM1;
+int r8 = -10 >> INTM2;
+int r9 = -__INT_MAX__ >> 2;
+int r10 = -__INT_MAX__ >> INTM1;
+
+unsigned u1 = 1 << INTM1;
+unsigned u2 = 9 << INTM1;
+unsigned u3 = 2U << INTM1;
+unsigned u4 = 9U << INTM1;
+unsigned u5 = 10U << INTM2;
+
+long long int l1 = 1LL << LLONGM1;
+long long int l2 = 9LL << LLONGM1;
+long long int l3 = 10LL << LLONGM2;
+long long int l4 = __LONG_LONG_MAX__ << 2;
+long long int l5 = __LONG_LONG_MAX__ << LLONGM1;
+long long int l6 = -1LL << LLONGM1;
+long long int l7 = -9LL << LLONGM1;
+long long int l8 = -10LL << LLONGM2;
+long long int l9 = -__LONG_LONG_MAX__ << 2;
+long long int l10 = -__LONG_LONG_MAX__ << LLONGM1;
+
+void
+fn (void)
+{
+ const int a = 10;
+ const __SIZE_TYPE__ b = INTM1;
+ int k1 = a << b;
+ int k2 = 10 << b;
+ int k3 = a << INTM1;
+}
@@ -1,4 +1,4 @@
-/* { dg-options "-fno-ira-share-spill-slots" } */
+/* { dg-options "-fno-ira-share-spill-slots -Wno-shift-overflow" } */
extern void abort (void);
extern void exit (int);
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1990 -pedantic-errors" } */
+
+#define INTM2 (sizeof (int) * __CHAR_BIT__ - 2)
+
+enum { A = 10 << INTM2 };
+int k = 10 << INTM2;
+
+void
+fn (int i)
+{
+ switch (i)
+ case 10 << INTM2: break;
+}
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors -Wno-shift-overflow" } */
+
+#define INTM2 (sizeof (int) * __CHAR_BIT__ - 2)
+
+enum { A = 10 << INTM2 }; /* { dg-error "constant expression" } */
+int k = 10 << INTM2; /* { dg-error "constant expression" } */
+
+void
+fn (int i)
+{
+ switch (i)
+ case 10 << INTM2: break; /* { dg-error "constant expression" } */
+}