This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Constraints on unary '&' (PR 22367)


This patch fixes PR 22367, a bug where the C front end failed to check
the exact constraints on unary '&' operators (which differ between C90
and C99).

Bootstrapped with no regressions on x86_64-unknown-linux-gnu.  Applied
to mainline.

2009-04-18  Joseph Myers  <joseph@codesourcery.com>

	PR c/22367
	* c-typeck.c (build_unary_op): Check for taking address of
	expression of type void.

testsuite:
2009-04-18  Joseph Myers  <joseph@codesourcery.com>

	PR c/22367
	* gcc.dg/lvalue-6.c, gcc.dg/lvalue-7.c: New tests.

Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 146291)
+++ gcc/c-typeck.c	(working copy)
@@ -3335,6 +3335,15 @@ build_unary_op (location_t location,
     case ADDR_EXPR:
       /* Note that this operation never does default_conversion.  */
 
+      /* The operand of unary '&' must be an lvalue (which excludes
+	 expressions of type void), or, in C99, the result of a [] or
+	 unary '*' operator.  */
+      if (VOID_TYPE_P (TREE_TYPE (arg))
+	  && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
+	  && (TREE_CODE (arg) != INDIRECT_REF
+	      || !flag_isoc99))
+	pedwarn (location, 0, "taking address of expression of type %<void%>");
+
       /* Let &* cancel out to simplify resulting code.  */
       if (TREE_CODE (arg) == INDIRECT_REF)
 	{
Index: gcc/testsuite/gcc.dg/lvalue-6.c
===================================================================
--- gcc/testsuite/gcc.dg/lvalue-6.c	(revision 0)
+++ gcc/testsuite/gcc.dg/lvalue-6.c	(revision 0)
@@ -0,0 +1,17 @@
+/* Test constraints on unary '&': PR 22367.  */
+
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1990 -pedantic-errors" } */
+
+extern void v;
+void f1 (void) { &v; } /* { dg-error "taking address of expression of type 'void'" } */
+
+extern void *pv;
+void f2 (void) { &*pv; } /* { dg-warning "dereferencing" } */
+/* { dg-error "taking address of expression of type 'void'" "C90 only error" { target *-*-* } 10 } */
+
+extern const void cv;
+void f3 (void) { &cv; }
+
+extern const void *pcv;
+void f4 (void) { &*pcv; } /* { dg-warning "dereferencing" } */
Index: gcc/testsuite/gcc.dg/lvalue-7.c
===================================================================
--- gcc/testsuite/gcc.dg/lvalue-7.c	(revision 0)
+++ gcc/testsuite/gcc.dg/lvalue-7.c	(revision 0)
@@ -0,0 +1,16 @@
+/* Test constraints on unary '&': PR 22367.  */
+
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
+
+extern void v;
+void f1 (void) { &v; } /* { dg-error "taking address of expression of type 'void'" } */
+
+extern void *pv;
+void f2 (void) { &*pv; } /* { dg-warning "dereferencing" } */
+
+extern const void cv;
+void f3 (void) { &cv; }
+
+extern const void *pcv;
+void f4 (void) { &*pcv; } /* { dg-warning "dereferencing" } */

-- 
Joseph S. Myers
joseph@codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]