This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: More cases added to cast-qual-1.c test
- To: "Kaveh R. Ghazi" <ghazi at caip dot rutgers dot edu>
- Subject: Re: More cases added to cast-qual-1.c test
- From: Geoff Keating <geoffk at cygnus dot com>
- Date: 04 Apr 2000 14:53:24 -0700
- CC: gcc-patches at gcc dot gnu dot org
- References: <200004041515.LAA13898@caip.rutgers.edu>
"Kaveh R. Ghazi" <ghazi@caip.rutgers.edu> writes:
> I installed some more cases to the cast-qual-1.c test, they were
> inspired by bogus warnings from gcc sources when using -Wcast-qual.
I reverted this patch.
> +typedef struct rtx_def * rtx;
> +
> +void
> +typedef_cast(const void *bar)
> +{
> + (const rtx)bar; /* { dg-bogus "cast discards" "discarding `const' warning" } */
A 'const rtx' is a pointer which has constant value, not a pointer
which points to something which has constant value. Thus it is
perfectly correct to warn that a 'const void *' is being converted
into a 'struct rtx_def * const'.
The usual way to fix this is to do
typedef const struct rtx_def * const_rtx;
and to write
(const_rtx) bar;
> + (const rtx *)bar; /* { dg-warning "cast discards" "discarding `const' warning" } */
Here, it is not correct to warn, because we are casting a 'const void *'
to a 'struct rtx_def *const *', and both of these are pointers to things
declared 'const'.
> +}
> +
> +void
> +typedef_cast2(const rtx bar)
> +{
> + (const void *)bar; /* { dg-bogus "cast discards" "discarding `const' warning" } */
> + (const void **)bar; /* { dg-warning "cast discards" "discarding `const' warning" } */
> +}
Since the type pointed to by 'const rtx' is not constant, it's not
possible to discard any of its qualifiers by casting it :-).
So GCC is correct not to warn here.
> +void
> +typedef_assign(const void *bar)
> +{
> + rtx const *foo1 = bar; /* { dg-bogus "initialization discards" "discarding `const' warning" } */
> + const rtx *foo2 = bar; /* { dg-warning "initialization discards" "discarding `const' warning" } */
> +}
'rtx const' and 'const rtx' are the same type. It doesn't matter
which order you write declaration-specifiers (except that things like
'int extern' are obsolecent). Anyway, GCC is correct not to warn for
either of these, because 'bar' points to a 'const void' and 'foo1'
points to a 'const rtx'.
> +void
> +typdef_assign2(const rtx bar)
> +{
> + void *const *foo1 = bar; /* { dg-bogus "initialization discards" "discarding `const' warning" } */
> + const void **foo2 = bar; /* { dg-warning "initialization discards" "discarding `const' warning" } */
> +}
Here, GCC correctly complains that 'struct rtx_def' and 'void *' are
not compatible types, whether they are const or otherwise, which is
the more serious error.
--
- Geoffrey Keating <geoffk@cygnus.com>