public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Bernd Edlinger <bernd.edlinger@hotmail.de>
Cc: Florian Weimer <fw@deneb.enyo.de>,
	"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	Jeff Law <law@redhat.com>
Subject: Re: [PATCH] Make -Wint-in-bool-context warn on suspicious shift ops
Date: Sat, 08 Oct 2016 17:40:00 -0000	[thread overview]
Message-ID: <CADzB+2kx9NV0zCORYhG+Q36dZpbGd7uj2U7FYZinwhq8Vz9x4Q@mail.gmail.com> (raw)
In-Reply-To: <AM4PR0701MB216221175254CF5C35B6D372E4C10@AM4PR0701MB2162.eurprd07.prod.outlook.com>

[-- Attachment #1: Type: text/plain, Size: 1126 bytes --]

On Fri, Sep 30, 2016 at 1:07 AM, Bernd Edlinger
<bernd.edlinger@hotmail.de> wrote:
> On 09/29/16 22:38, Jason Merrill wrote:
>> On Thu, Sep 29, 2016 at 3:58 PM, Bernd Edlinger
>> <bernd.edlinger@hotmail.de> wrote:
>>> Unfortunately, without that exception there is a false positive:
>>>
>>> In file included from ../../gcc-trunk/gcc/ada/gcc-interface/decl.c:30:0:
>>> ../../gcc-trunk/gcc/ada/gcc-interface/decl.c: In function 'int
>>> adjust_packed(tree, tree, int)':
>>> ../../gcc-trunk/gcc/tree.h:1874:22: error: << on signed integer in
>>> boolean context [-Werror=int-in-bool-context]
>>>         ? ((unsigned)1) << ((NODE)->type_common.align - 1) : 0)
>>>           ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> Ah, this issue again: the shift isn't in boolean context, it's in
>> integer context.  I think we want to be a lot more conservative about
>> these warnings in the arms of a COND_EXPR.  In fact, I think the
>> entire
>>
>>        /* Distribute the conversion into the arms of a COND_EXPR.  */
>>
>> section is wrong now that we're doing delayed folding.
>
> Could you take care of this ?

Done thus:

[-- Attachment #2: cond-bool.diff --]
[-- Type: text/plain, Size: 2129 bytes --]

commit 0a124bbb6f0598345c98e3a91f8c69548518d4c3
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Sep 30 17:52:21 2016 -0400

            Delay folding of bool conversion into COND_EXPR.
    
    gcc/c-family/
            * c-common.c (c_common_truthvalue_conversion): Don't distribute
            into COND_EXPR in C++.
    gcc/cp/
            * cp-gimplify.c (cp_fold): Distribute cp_truthvalue_conversion
            into COND_EXPR.

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index f7a5d62..dbdb276 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -4694,21 +4694,8 @@ c_common_truthvalue_conversion (location_t location, tree expr)
 	}
       /* Distribute the conversion into the arms of a COND_EXPR.  */
       if (c_dialect_cxx ())
-	{
-	  tree op1 = TREE_OPERAND (expr, 1);
-	  tree op2 = TREE_OPERAND (expr, 2);
-	  int w = warn_int_in_bool_context;
-	  warn_int_in_bool_context = 0;
-	  /* In C++ one of the arms might have void type if it is throw.  */
-	  if (!VOID_TYPE_P (TREE_TYPE (op1)))
-	    op1 = c_common_truthvalue_conversion (location, op1);
-	  if (!VOID_TYPE_P (TREE_TYPE (op2)))
-	    op2 = c_common_truthvalue_conversion (location, op2);
-	  expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
-				  TREE_OPERAND (expr, 0), op1, op2);
-	  warn_int_in_bool_context = w;
-	  goto ret;
-	}
+	/* Avoid premature folding.  */
+	break;
       else
 	{
 	  int w = warn_int_in_bool_context;
diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 5aca8f2..4879632 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -2253,6 +2253,15 @@ cp_fold (tree x)
       op1 = cp_fold (TREE_OPERAND (x, 1));
       op2 = cp_fold (TREE_OPERAND (x, 2));
 
+      if (TREE_CODE (TREE_TYPE (x)) == BOOLEAN_TYPE)
+	{
+	  warning_sentinel (warn_int_in_bool_context);
+	  if (!VOID_TYPE_P (TREE_TYPE (op1)))
+	    op1 = cp_truthvalue_conversion (op1);
+	  if (!VOID_TYPE_P (TREE_TYPE (op2)))
+	    op2 = cp_truthvalue_conversion (op2);
+	}
+
       if (op0 != TREE_OPERAND (x, 0)
 	  || op1 != TREE_OPERAND (x, 1)
 	  || op2 != TREE_OPERAND (x, 2))

  parent reply	other threads:[~2016-10-08 17:40 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-25  9:14 Bernd Edlinger
2016-09-27 12:45 ` Jason Merrill
2016-09-27 12:58   ` Florian Weimer
2016-09-27 13:56     ` Bernd Edlinger
2016-09-27 14:34       ` Florian Weimer
2016-09-27 14:42         ` Bernd Edlinger
2016-09-27 14:51           ` Jason Merrill
2016-09-27 15:19             ` Bernd Edlinger
2016-09-28 14:44               ` Jason Merrill
2016-09-28 16:17                 ` Bernd Edlinger
2016-09-29 18:10                   ` Jason Merrill
2016-09-29 19:07                     ` Bernd Edlinger
2016-09-29 20:08                       ` Bernd Edlinger
2016-09-29 20:53                         ` Jason Merrill
2016-09-30  7:05                           ` Bernd Edlinger
2016-10-02 18:38                             ` Jason Merrill
2016-10-08 17:40                             ` Jason Merrill [this message]
2016-10-08 20:05                               ` Bernd Edlinger
2016-10-09  2:42                                 ` Jason Merrill
2016-10-17 15:23                       ` Markus Trippelsdorf
2016-10-17 16:51                         ` Bernd Edlinger
2016-10-17 17:11                           ` Markus Trippelsdorf
2016-10-17 17:30                             ` Bernd Edlinger
2016-10-17 17:44                               ` Markus Trippelsdorf
2016-10-18 17:04                               ` Bernd Edlinger
2016-10-18 17:05                                 ` Joseph Myers
2016-10-18 18:14                                   ` Bernd Edlinger
2016-10-19 20:13                                     ` Jeff Law
2016-10-20  8:05                                       ` Markus Trippelsdorf
2016-10-20 14:00                                         ` Bernd Edlinger
2016-09-27 13:48   ` Michael Matz

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=CADzB+2kx9NV0zCORYhG+Q36dZpbGd7uj2U7FYZinwhq8Vz9x4Q@mail.gmail.com \
    --to=jason@redhat.com \
    --cc=bernd.edlinger@hotmail.de \
    --cc=fw@deneb.enyo.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=law@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).