public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Eric Botcazou <ebotcazou@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-5814] Fix problematic conversion to boolean type
Date: Mon,  7 Dec 2020 09:18:27 +0000 (GMT)	[thread overview]
Message-ID: <20201207091827.97EBD385EC54@sourceware.org> (raw)

https://gcc.gnu.org/g:090abb0f51b5cf8ce46fb686bc4b968f6e2055be

commit r11-5814-g090abb0f51b5cf8ce46fb686bc4b968f6e2055be
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Mon Dec 7 10:17:20 2020 +0100

    Fix problematic conversion to boolean type
    
    The new ranger exposed a problematic conversion to boolean type.
    
    gcc/ada/ChangeLog:
            * gcc-interface/utils.c (convert) <INTEGER_TYPE>: Call fold_convert
            in the cases where convert_to_integer is not called.
            <BOOLEAN_TYPE>: Call fold_convert instead of convert_to_integer.

Diff:
---
 gcc/ada/gcc-interface/utils.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/gcc/ada/gcc-interface/utils.c b/gcc/ada/gcc-interface/utils.c
index 013fccdd657..1d49db9fb1b 100644
--- a/gcc/ada/gcc-interface/utils.c
+++ b/gcc/ada/gcc-interface/utils.c
@@ -4930,10 +4930,6 @@ convert (tree type, tree expr)
 					  convert (TREE_TYPE (type),
 						   TYPE_MIN_VALUE (type))));
 
-      /* ... fall through ... */
-
-    case ENUMERAL_TYPE:
-    case BOOLEAN_TYPE:
       /* If we are converting an additive expression to an integer type
 	 with lower precision, be wary of the optimization that can be
 	 applied by convert_to_integer.  There are 2 problematic cases:
@@ -4945,8 +4941,7 @@ convert (tree type, tree expr)
 	     intermediate conversion that changes the sign could
 	     be inserted and thus introduce an artificial overflow
 	     at compile time when the placeholder is substituted.  */
-      if (code == INTEGER_TYPE
-	  && ecode == INTEGER_TYPE
+      if (ecode == INTEGER_TYPE
 	  && TYPE_PRECISION (type) < TYPE_PRECISION (etype)
 	  && (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR))
 	{
@@ -4955,11 +4950,18 @@ convert (tree type, tree expr)
 	  if ((TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
 	       && TYPE_BIASED_REPRESENTATION_P (TREE_TYPE (op0)))
 	      || CONTAINS_PLACEHOLDER_P (expr))
-	    return build1 (NOP_EXPR, type, expr);
+	    return fold_convert (type, expr);
 	}
 
+      /* ... fall through ... */
+
+    case ENUMERAL_TYPE:
       return fold (convert_to_integer (type, expr));
 
+    case BOOLEAN_TYPE:
+      /* Do not use convert_to_integer with boolean types.  */
+      return fold_convert_loc (EXPR_LOCATION (expr), type, expr);
+
     case POINTER_TYPE:
     case REFERENCE_TYPE:
       /* If converting between two thin pointers, adjust if needed to account


                 reply	other threads:[~2020-12-07  9:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20201207091827.97EBD385EC54@sourceware.org \
    --to=ebotcazou@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).