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] fix 8083


Hi
this patch adds a warning about suspicious type punning casts which will
fall afoul of type based aliasing. The warning is only triggered when
you're doing type based alias analysis and asked for extra warnings. If
you cast an objects address to a type in a different alias set, you're told.

This triggers on the java front end - for which I have an obvious patch.
Also in the fortran front end, which I'll fix shortly.

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

nathan
--
Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
         'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org

2002-10-01  Nathan Sidwell  <nathan@codesourcery.com>

	* c-typeck.c (build_c_cast): Warn about type puning which breaks
	type based aliasing.

Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.208
diff -c -3 -p -r1.208 c-typeck.c
*** c-typeck.c	22 Sep 2002 02:03:15 -0000	1.208
--- c-typeck.c	1 Oct 2002 15:55:57 -0000
*************** build_c_cast (type, expr)
*** 3759,3764 ****
--- 3759,3781 ----
  	  && !TREE_CONSTANT (value))
  	warning ("cast to pointer from integer of different size");
  
+       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 && extra_warnings
+ 	  && !VOID_TYPE_P (TREE_TYPE (type)))
+ 	{
+  	  /* Casting the address of a decl to non void pointer. Warn
+ 	     if the cast breaks type based aliasing.  */
+ 	  if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
+ 	    warning ("type punning to incomplete type might not be type based aliasing safe");
+ 	  else if (!alias_sets_conflict_p
+ 		   (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
+ 		    get_alias_set (TREE_TYPE (type))))
+ 	    warning ("type punning cast is not type based aliasing safe");
+ 	}
+       
        ovalue = value;
        value = convert (type, value);
  
// { dg-do compile }
// { dg-options "-W -fstrict-aliasing" }

// Copyright (C) 2002 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 29 Sep 2002 <nathan@codesourcery.com>

// 8083. warn about odd casts

typedef int YYSTYPE;
typedef struct tDefEntry 
{
  unsigned t;
  
} tDefEntry;
struct incomplete;


YYSTYPE
 addSibMacro(
         YYSTYPE  def,
         YYSTYPE  list )
 {
     tDefEntry*  pDef  = (tDefEntry*)def;
     tDefEntry*  pScn  = (tDefEntry*)list;
     tDefEntry** ppT   = (tDefEntry**)&list; // { dg-warning "type punning cast" "" }
 
     struct incomplete *p = (struct incomplete *)&list; // { dg-warning "type punning to incomplete" "" }
     
     return list;
 }


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