public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Sandiford <rdsandiford@googlemail.com>
To: Marek Polacek <polacek@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>,
	 Jason Merrill <jason@redhat.com>,
	 Joseph Myers <joseph@codesourcery.com>
Subject: Re: [C/C++ PATCH] Implement -Wshift-overflow (PR c++/55095)
Date: Mon, 01 Jun 2015 20:06:00 -0000	[thread overview]
Message-ID: <877frnchxt.fsf@googlemail.com> (raw)
In-Reply-To: <20150525194816.GX27320@redhat.com> (Marek Polacek's message of	"Mon, 25 May 2015 21:48:16 +0200")

Marek Polacek <polacek@redhat.com> writes:
> +  /* 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;

Yeah, this "sort of" is a bit worrying :-)  Especially as the number
of bits in a widest_int depends on the number of bits in the target's
widest integer mode.  E.g. for i386 it's forced to 128, but for ARM
it's 512 (IIRC).

Could you do the check based on the wi::min_precision of the unshifted
value?  I.e. see whether adding the shift amount to that gives a value
greater than type's precision.

Thanks,
Richard

  parent reply	other threads:[~2015-06-01 20:06 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-25 20:12 Marek Polacek
2015-05-26 11:07 ` Marek Polacek
2015-05-29 21:25 ` Joseph Myers
2015-06-02 19:26   ` [C/C++ PATCH] Implement -Wshift-overflow (PR c++/55095) (take 2) Marek Polacek
2015-06-03 13:07     ` [C/C++ PATCH] Implement -Wshift-overflow (PR c++/55095) (take 3) Marek Polacek
2015-06-04 21:05       ` Joseph Myers
2015-06-05  9:42         ` Marek Polacek
2015-06-12  9:08           ` Marek Polacek
2015-06-19 10:51             ` Marek Polacek
2015-06-26  8:36               ` Marek Polacek
2015-07-03  7:42                 ` Marek Polacek
2015-07-10 13:23                   ` Marek Polacek
2015-07-17  8:32                     ` Marek Polacek
2015-07-17 23:01                       ` Jeff Law
2015-07-20 14:19                         ` Marek Polacek
2015-06-01 20:06 ` Richard Sandiford [this message]
2015-06-02  8:16   ` [C/C++ PATCH] Implement -Wshift-overflow (PR c++/55095) Richard Biener
2015-06-02  9:01     ` Richard Sandiford
2015-06-02 15:05       ` Marek Polacek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877frnchxt.fsf@googlemail.com \
    --to=rdsandiford@googlemail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).