This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C PATCH]: Additional alias warning


Hi,
a customer complained that -Wstrict-aliasing did not warn about
	(T *)&obj->member
when T and obj->member are in different alias sets.

That does seem like a sensible thing to warn about, and the attached patch augments the existing code. I reorded the checks to early out when the warning is not requested.

booted & tested on i686-pc-linux-gnu, ok?

nathan
--
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk

2005-06-08  Nathan Sidwell  <nathan@codesourcery.com>

	* c-typeck.c (build_c_cast): Check type punning on COMPONENT_REF
	too.

2005-06-08  Nathan Sidwell  <nathan@codesourcery.com>

	* gcc.dg/alias-8.c: New.

Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.450
diff -c -1 -p -r1.450 c-typeck.c
*** c-typeck.c	7 Jun 2005 10:10:22 -0000	1.450
--- c-typeck.c	8 Jun 2005 15:15:06 -0000
*************** build_c_cast (tree type, tree expr)
*** 3306,3315 ****
  
!       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)))
  	{
! 	  /* Casting the address of a decl to non void pointer. Warn
  	     if the cast breaks type based aliasing.  */
--- 3306,3316 ----
  
!       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))
! 	      || TREE_CODE (TREE_OPERAND (expr, 0)) == COMPONENT_REF)
  	  && !VOID_TYPE_P (TREE_TYPE (type)))
  	{
! 	  /* Casting the address of an object to non void pointer. Warn
  	     if the cast breaks type based aliasing.  */
Index: testsuite/gcc.dg/alias-8.c
===================================================================
RCS file: testsuite/gcc.dg/alias-8.c
diff -N testsuite/gcc.dg/alias-8.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/gcc.dg/alias-8.c	8 Jun 2005 14:59:04 -0000
***************
*** 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" } */
+ }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]