public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [C++ PATCH] Fix another y && (x ? 1 : throw 1) issue (PR c++/49165)
Date: Fri, 27 May 2011 18:14:00 -0000	[thread overview]
Message-ID: <20110527164457.GD17079@tyan-ft48-01.lab.bos.redhat.com> (raw)

Hi!

As the testcase shows (it is eh/cond5.C just with s/true :/1 :/g),
c_common_truthvalue_conversion needs to be aware of void type arms
too, otherwise it errors on valid source.

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

2011-05-27  Jakub Jelinek  <jakub@redhat.com>

	PR c++/49165
	* c-common.c (c_common_truthvalue_conversion) <case COND_EXPR>: For
	C++ don't call c_common_truthvalue_conversion on void type arms.

	* g++.dg/eh/cond6.C: New test.

--- gcc/c-family/c-common.c.jj	2011-05-25 16:50:57.000000000 +0200
+++ gcc/c-family/c-common.c	2011-05-27 14:22:06.000000000 +0200
@@ -3942,14 +3942,15 @@ c_common_truthvalue_conversion (location
       /* 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);
+	  /* 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),
-			      c_common_truthvalue_conversion (location,
-							      TREE_OPERAND (expr,
-									    1)),
-			      c_common_truthvalue_conversion (location,
-							      TREE_OPERAND (expr,
-									    2)));
+				  TREE_OPERAND (expr, 0), op1, op2);
 	  goto ret;
 	}
       else
--- gcc/testsuite/g++.dg/eh/cond6.C.jj	2011-05-27 14:25:02.000000000 +0200
+++ gcc/testsuite/g++.dg/eh/cond6.C	2011-05-27 14:13:17.000000000 +0200
@@ -0,0 +1,43 @@
+// PR c++/49165
+// { dg-do run }
+
+extern "C" void abort ();
+
+int
+foo (bool x, int y)
+{
+  if (y < 10 && (x ? 1 : throw 1))
+    y++;
+  if (y > 20 || (x ? 1 : throw 2))
+    y++;
+  return y;
+}
+
+int
+main ()
+{
+  if (foo (true, 0) != 2
+      || foo (true, 10) != 11
+      || foo (false, 30) != 31)
+    abort ();
+  try
+    {
+      foo (false, 0);
+      abort ();
+    }
+  catch (int i)
+    {
+      if (i != 1)
+	abort ();
+    }
+  try
+    {
+      foo (false, 10);
+      abort ();
+    }
+  catch (int i)
+    {
+      if (i != 2)
+	abort ();
+    }
+}

	Jakub

             reply	other threads:[~2011-05-27 16:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-27 18:14 Jakub Jelinek [this message]
2011-05-27 18:34 ` Jason Merrill

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=20110527164457.GD17079@tyan-ft48-01.lab.bos.redhat.com \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@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).