This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[Committed] Fix C++/29295, wrong code with typedef of Bools
- From: Andrew Pinski <pinskia at gmail dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sat, 28 Oct 2006 16:01:28 -0700
- Subject: [Committed] Fix C++/29295, wrong code with typedef of Bools
The problem here is that the C++ front-end is comparing the pointer
value of a type to boolean_type_node instead of using same_type_p so we
produce wrong code (and accept invalid code) with typedefs in some
places. Anyways this patch fixes the problem by replacing the pointer
comparision with a call to same_type_p.
Applied as obvious to the mainline, this is not a regression as far as I
can tell, it was broken in 2.95.3 and above and looks like the code has
been always wrong. I did bootstrap/test on i686-linux-gnu with no
regressions.
Thanks,
Andrew Pinski
cp/ChangeLog:
* typeck.c (build_unary_op): Use same_type_p when comparing to
boolean type.
testsuite/ChangeLog:
* g++.dg/expr/bool1.C: New test.
* g++.dg/expr/bool2.C: New test.
Index: testsuite/g++.dg/expr/bool1.C
===================================================================
--- testsuite/g++.dg/expr/bool1.C (revision 0)
+++ testsuite/g++.dg/expr/bool1.C (revision 0)
@@ -0,0 +1,21 @@
+// { dg-do run }
+// PR C++/29295
+// make sure that a typedef for a bool will have the
+// the same results as a bool itself.
+
+extern "C" void abort();
+typedef bool my_bool;
+int main()
+{
+ my_bool b = false;
+ int i;
+
+ b++;
+ b++;
+ i = b;
+ if (i != 1)
+ abort ();
+ return 0;
+}
+
+
Index: testsuite/g++.dg/expr/bool2.C
===================================================================
--- testsuite/g++.dg/expr/bool2.C (revision 0)
+++ testsuite/g++.dg/expr/bool2.C (revision 0)
@@ -0,0 +1,13 @@
+// { dg-do compile }
+// make sure that a typedef for a bool will have the
+// the same results as a bool itself.
+
+
+typedef bool my_bool;
+int main()
+{
+ my_bool b = false;
+ b--; // { dg-error "" }
+ return 0;
+}
+
Index: cp/typeck.c
===================================================================
--- cp/typeck.c (revision 118112)
+++ cp/typeck.c (working copy)
@@ -4192,7 +4192,7 @@ build_unary_op (enum tree_code code, tre
return error_mark_node;
/* Forbid using -- on `bool'. */
- if (TREE_TYPE (arg) == boolean_type_node)
+ if (same_type_p (TREE_TYPE (arg), boolean_type_node))
{
if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
{