OSDN Git Service

PR c/8083
authornathan <nathan@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 1 Oct 2002 19:11:07 +0000 (19:11 +0000)
committernathan <nathan@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 1 Oct 2002 19:11:07 +0000 (19:11 +0000)
* c-typeck.c (build_c_cast): Warn about type punning which breaks
type based aliasing.
testsuite:
* gcc.dg/alias-1.c: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@57698 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/alias-1.c [new file with mode: 0644]

index a860318..600b7ea 100644 (file)
@@ -1,3 +1,9 @@
+2002-10-01  Nathan Sidwell  <nathan@codesourcery.com>
+
+       PR c/8083
+       * c-typeck.c (build_c_cast): Warn about type punning which breaks
+       type based aliasing.
+
 2002-10-01  Mark Mitchell  <mark@codesourcery.com>
 
        * stor-layout.c (update_alignment_for_field): New function.
@@ -6,6 +12,7 @@
        
 2002-10-01  Nathan Sidwell  <nathan@codesourcery.com>
 
+       PR other/8077
        * gcc.c (cc1_options): Add space on -auxbase-strip.
 
 2002-10-01  Jim Wilson  <wilson@redhat.com>
index 1bca1bf..6fd14e5 100644 (file)
@@ -3759,6 +3759,23 @@ build_c_cast (type, expr)
          && !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);
 
index 57d265f..ccfba4e 100644 (file)
@@ -1,3 +1,7 @@
+2002-10-01  Nathan Sidwell  <nathan@codesourcery.com>
+
+       * gcc.dg/alias-1.c: New test.
+
 2002-10-01  Mark Mitchell  <mark@codesourcery.com>
 
        * gcc.dg/empty1.C: New test.
diff --git a/gcc/testsuite/gcc.dg/alias-1.c b/gcc/testsuite/gcc.dg/alias-1.c
new file mode 100644 (file)
index 0000000..71056e9
--- /dev/null
@@ -0,0 +1,28 @@
+// { 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  list )
+ {
+     tDefEntry** ppT   = (tDefEntry**)&list; // { dg-warning "type punning cast" "" }
+     struct incomplete *p = (struct incomplete *)&list; // { dg-warning "type punning to incomplete" "" }
+     
+     return list;
+ }
+