From 5878b92f9edefa7c60d5508fbba8441dc1358df9 Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Thu, 9 Jun 2005 11:36:12 +0000 Subject: [PATCH] c-typeck.c (build_c_cast): Check type punning on COMPONENT_REF too. /: * c-typeck.c (build_c_cast): Check type punning on COMPONENT_REF too. testsuite: * gcc.dg/alias-8.c: New. From-SVN: r100799 --- gcc/ChangeLog | 5 +++++ gcc/c-typeck.c | 9 +++++---- gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.dg/alias-8.c | 12 ++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/alias-8.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 25004af66c3..b4195245fc0 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2005-06-09 Nathan Sidwell + + * c-typeck.c (build_c_cast): Check type punning on COMPONENT_REF + too. + 2005-06-09 Bernd Schmidt * config/bfin/bfin.c (enum bfin_builtins): Moved here from... diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index a1c7e56f76b..9429b73172c 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -3304,14 +3304,15 @@ build_c_cast (tree type, tree expr) && !TREE_CONSTANT (value)) warning (0, "cast to pointer from integer of different size"); - if (TREE_CODE (type) == POINTER_TYPE + if (flag_strict_aliasing && warn_strict_aliasing + && 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 + && (DECL_P (TREE_OPERAND (expr, 0)) + || TREE_CODE (TREE_OPERAND (expr, 0)) == COMPONENT_REF) && !VOID_TYPE_P (TREE_TYPE (type))) { - /* Casting the address of a decl to non void pointer. Warn + /* Casting the address of an object to non void pointer. Warn if the cast breaks type based aliasing. */ if (!COMPLETE_TYPE_P (TREE_TYPE (type))) warning (0, "type-punning to incomplete type might break strict-aliasing rules"); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0facf7fb1f7..fe3197c1e08 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2005-06-09 Nathan Sidwell + + * gcc.dg/alias-8.c: New. + 2005-06-08 Joseph S. Myers * gcc.dg/format/cmn_err-1.c: Update. diff --git a/gcc/testsuite/gcc.dg/alias-8.c b/gcc/testsuite/gcc.dg/alias-8.c new file mode 100644 index 00000000000..690f1b62da9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/alias-8.c @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-Wstrict-aliasing=2 -fstrict-aliasing" } + +struct s { + char *p; +}; + +void +func(struct s *ptr) +{ + *(void **)&ptr->p = 0; /* { dg-warning "type-punned pointer" } */ +} -- 2.30.2