This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C/C++ PATCH] Implement -Wshift-negative-value (PR c/65179)


On 05/06/2015 05:37 AM, Marek Polacek wrote:
On Wed, Apr 29, 2015 at 10:54:58PM +0000, Joseph Myers wrote:
On Mon, 27 Apr 2015, Marek Polacek wrote:

trigger by default.  One change is that we reject programs that use shift with
undefined behavior in a context where a constant expression is required, thus
e.g. enum E { A = -1 << 0 };
But I hope that's reasonable.

That seems appropriate (for C99 and above).

But if someone explicitly uses -Wshift-negative-value, I'd expect that to
produce the warnings (as opposed to the rejections where a constant
expression is required) even in C90 mode.  That is, for the warnings, I
think flag_isoc99 should maybe affect the default (whether -Wextra enables
the warning, or whatever such approach gets taken), but not whether
-Wshift-negative-value, given that the option has been enabled, produces
warnings.

Ah, indeed.  The following patch hopefully addresses those defects.  The
tests show when the warning triggers and when not as well as when we reject
invalid shifts and when not.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-05-06  Marek Polacek  <polacek@redhat.com>

	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.
OK.  Please install if you haven't already.

jeff


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]