c-typeck.c (build_c_cast): Check type punning on COMPONENT_REF too.
authorNathan Sidwell <nathan@codesourcery.com>
Thu, 9 Jun 2005 11:36:12 +0000 (11:36 +0000)
committerNathan Sidwell <nathan@gcc.gnu.org>
Thu, 9 Jun 2005 11:36:12 +0000 (11:36 +0000)
/:
* 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
gcc/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/alias-8.c [new file with mode: 0644]

index 25004af66c36887150b895aec1771acde8342910..b4195245fc0190318d32b2a8de6d5e00612684cb 100644 (file)
@@ -1,3 +1,8 @@
+2005-06-09  Nathan Sidwell  <nathan@codesourcery.com>
+
+       * c-typeck.c (build_c_cast): Check type punning on COMPONENT_REF
+       too.
+
 2005-06-09  Bernd Schmidt  <bernd.schmidt@analog.com>
 
        * config/bfin/bfin.c (enum bfin_builtins): Moved here from...
index a1c7e56f76bb33404ea66c3ceace54bab1f9a668..9429b73172c448ae64827b6951edd2b3f2eb1220 100644 (file)
@@ -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");
index 0facf7fb1f7324775d63f4628bbdccd737ac897c..fe3197c1e0845ca077dd093b58191570c5c941f8 100644 (file)
@@ -1,3 +1,7 @@
+2005-06-09  Nathan Sidwell  <nathan@codesourcery.com>
+
+       * gcc.dg/alias-8.c: New.
+
 2005-06-08  Joseph S. Myers  <joseph@codesourcery.com>
 
        * 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 (file)
index 0000000..690f1b6
--- /dev/null
@@ -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" } */
+}