OSDN Git Service

PR c/22367
authorjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 18 Apr 2009 22:34:10 +0000 (22:34 +0000)
committerjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 18 Apr 2009 22:34:10 +0000 (22:34 +0000)
* c-typeck.c (build_unary_op): Check for taking address of
expression of type void.

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

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@146332 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/lvalue-6.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/lvalue-7.c [new file with mode: 0644]

index e150765..4035b2c 100644 (file)
@@ -1,5 +1,11 @@
 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.
+
+2009-04-18  Joseph Myers  <joseph@codesourcery.com>
+
        PR c/35210
        * c-typeck.c (build_function_call): Check for calling a function
        with qualified void return types.  Call require_complete_type when
index 001ea1a..bacfdc0 100644 (file)
@@ -3346,6 +3346,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 0ccc20a..a2b0bb0 100644 (file)
@@ -1,5 +1,10 @@
 2009-04-18  Joseph Myers  <joseph@codesourcery.com>
 
+       PR c/22367
+       * gcc.dg/lvalue-6.c, gcc.dg/lvalue-7.c: New tests.
+
+2009-04-18  Joseph Myers  <joseph@codesourcery.com>
+
        * gcc.dg/cpp/include5.c: New test.
 
 2009-04-18  Joseph Myers  <joseph@codesourcery.com>
diff --git a/gcc/testsuite/gcc.dg/lvalue-6.c b/gcc/testsuite/gcc.dg/lvalue-6.c
new file mode 100644 (file)
index 0000000..af69de4
--- /dev/null
@@ -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" } */
diff --git a/gcc/testsuite/gcc.dg/lvalue-7.c b/gcc/testsuite/gcc.dg/lvalue-7.c
new file mode 100644 (file)
index 0000000..37964e1
--- /dev/null
@@ -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" } */