summaryrefslogtreecommitdiff
path: root/gcc/c
diff options
context:
space:
mode:
authormpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2015-05-07 19:36:31 +0000
committermpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2015-05-07 19:36:31 +0000
commitcdc64059c5405c2b3c489b071edfe2127cc45333 (patch)
tree12367850915ce482968be36f7c2f4344d74b5ed5 /gcc/c
parente58b5f40ac976b65acb3827b16afa10ab7a11eb6 (diff)
downloadgcc-cdc64059c5405c2b3c489b071edfe2127cc45333.tar.gz
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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@222889 138bc75d-0d04-0410-961f-82ee72b054a4
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 2ef895a639c..ccd1e7291cf 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 328f294899b..73275aa106b 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)