aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2015-05-07 19:36:31 +0000
committerMarek Polacek <mpolacek@gcc.gnu.org>2015-05-07 19:36:31 +0000
commit0173bd2a038cbeef871b22b312a6856ab1dcda2a (patch)
tree12367850915ce482968be36f7c2f4344d74b5ed5 /gcc/cp
parentd57c99458933a21fdf94f508191f145ad8d5ec58 (diff)
downloadgcc-0173bd2a038cbeef871b22b312a6856ab1dcda2a.zip
gcc-0173bd2a038cbeef871b22b312a6856ab1dcda2a.tar.gz
gcc-0173bd2a038cbeef871b22b312a6856ab1dcda2a.tar.bz2
re PR c/65179 (Introduce new C warning: -Wshift-negative-value)
PR c/65179 * c-common.c (c_fully_fold_internal): Warn when left shifting a negative value. * c.opt (Wshift-negative-value): New option. * c-opts.c (c_common_post_options): Set warn_shift_negative_value when -Wextra and C99/C++11 mode. * c-typeck.c (build_binary_op): Warn when left shifting a negative value. * typeck.c (cp_build_binary_op): Warn when left shifting a negative value. * doc/invoke.texi: Document -Wshift-negative-value. * c-c++-common/Wshift-negative-value-1.c: New test. * testsuite/c-c++-common/Wshift-negative-value-2.c: New test. * testsuite/c-c++-common/Wshift-negative-value-3.c: New test. * testsuite/c-c++-common/Wshift-negative-value-4.c: New test. * testsuite/c-c++-common/Wshift-negative-value-5.c: New test. * testsuite/c-c++-common/Wshift-negative-value-6.c: New test. * testsuite/gcc.dg/c90-left-shift-1.c: New test. * testsuite/gcc.dg/c99-const-expr-7.c: Add dg-error. * testsuite/gcc.dg/c99-left-shift-1.c: New test. From-SVN: r222889
Diffstat (limited to 'gcc/cp')
-rw-r--r--gcc/cp/ChangeLog6
-rw-r--r--gcc/cp/typeck.c9
2 files changed, 15 insertions, 0 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 4929faa..3c32e6f 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2015-05-07 Marek Polacek <polacek@redhat.com>
+
+ PR c/65179
+ * typeck.c (cp_build_binary_op): Warn when left shifting a negative
+ value.
+
2015-05-07 Jason Merrill <jason@redhat.com>
DR 1467
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 91db32a..549e4b1 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -4326,11 +4326,20 @@ cp_build_binary_op (location_t location,
}
else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
{
+ tree const_op0 = fold_non_dependent_expr (op0);
+ if (TREE_CODE (const_op0) != INTEGER_CST)
+ const_op0 = op0;
tree const_op1 = fold_non_dependent_expr (op1);
if (TREE_CODE (const_op1) != INTEGER_CST)
const_op1 = op1;
result_type = type0;
doing_shift = true;
+ if (TREE_CODE (const_op0) == INTEGER_CST
+ && tree_int_cst_sgn (const_op0) < 0
+ && (complain & tf_warning)
+ && c_inhibit_evaluation_warnings == 0)
+ warning (OPT_Wshift_negative_value,
+ "left shift of negative value");
if (TREE_CODE (const_op1) == INTEGER_CST)
{
if (tree_int_cst_lt (const_op1, integer_zero_node))