aboutsummaryrefslogtreecommitdiff
path: root/gcc/c
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/c
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/c')
-rw-r--r--gcc/c/ChangeLog6
-rw-r--r--gcc/c/c-typeck.c11
2 files changed, 17 insertions, 0 deletions
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index 2ef895a..ccd1e72 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,3 +1,9 @@
+2015-05-07 Marek Polacek <polacek@redhat.com>
+
+ PR c/65179
+ * c-typeck.c (build_binary_op): Warn when left shifting a negative
+ value.
+
2015-04-30 Marek Polacek <polacek@redhat.com>
* c-typeck.c (set_init_label): Call error_at instead of error and
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 328f294..73275aa 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -10697,6 +10697,17 @@ build_binary_op (location_t location, enum tree_code code,
&& code1 == INTEGER_TYPE)
{
doing_shift = true;
+ if (TREE_CODE (op0) == INTEGER_CST
+ && tree_int_cst_sgn (op0) < 0)
+ {
+ /* Don't reject a left shift of a negative value in a context
+ where a constant expression is needed in C90. */
+ if (flag_isoc99)
+ int_const = false;
+ if (c_inhibit_evaluation_warnings == 0)
+ warning_at (location, OPT_Wshift_negative_value,
+ "left shift of negative value");
+ }
if (TREE_CODE (op1) == INTEGER_CST)
{
if (tree_int_cst_sgn (op1) < 0)