This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: No warning when breaking strict-aliasing rules despite -Wstrict-aliasing
- From: "Giovanni Bajo" <giovannibajo at libero dot it>
- To: "Anders Torger" <torger at ludd dot luth dot se>
- Cc: <gcc-bugs at gcc dot gnu dot org>
- Date: Wed, 10 Nov 2004 03:43:08 +0100
- Subject: Re: No warning when breaking strict-aliasing rules despite -Wstrict-aliasing
- References: <200411092023.46209.torger@ludd.luth.se>
Anders Torger <torger@ludd.luth.se> wrote:
> I found a case where GCC does not warn even with
> -Wstrict-aliasing, but makes incorrect asm code anyway.
> unsigned long
> hash(void *key)
> {
> unsigned long long u[1];
> u[0] = *(unsigned long long *)key;
> return ((unsigned long *)u)[0] ^ ((unsigned long *)u)[1];
> }
There is absolutely no guarantee that we always generate a warning for every
possible type-aliasing violation. For instance, if you passed the pointer to a
double as "key" to this function, we could not possibly find it out and emit a
warning.
Nonetheless, there is a problem here: we could easily emit a warning on this
line:
> return ((unsigned long *)u)[0] ^ ((unsigned long *)u)[1];
but we do not. The problem appears to be in c-typeck.c, in build_c_cast():
if (TREE_CODE (type) == POINTER_TYPE
&& TREE_CODE (otype) == POINTER_TYPE
&& TREE_CODE (expr) == ADDR_EXPR
&& DECL_P (TREE_OPERAND (expr, 0))
&& flag_strict_aliasing && warn_strict_aliasing
&& !VOID_TYPE_P (TREE_TYPE (type)))
The code only warns for expressions like "(cast*)&decl", but
"(cast*)array_decl" is also something to warn about.
This is a possible enhancement. Would you please file a bug report in Bugzilla
about this?
Thanks,
Giovanni Bajo