diff options
author | Nathan Sidwell <nathan@codesourcery.com> | 2002-10-01 19:11:07 +0000 |
---|---|---|
committer | Nathan Sidwell <nathan@gcc.gnu.org> | 2002-10-01 19:11:07 +0000 |
commit | 73a737681502c4880bee7b1825a4ff0c30f97e79 (patch) | |
tree | 4d8138cc11bddd48062039b3be6a93ad7ecb8eff | |
parent | 0645ba8f0b34dbbce7a57dcf712cd0cb261edcc2 (diff) | |
download | gcc-73a737681502c4880bee7b1825a4ff0c30f97e79.zip gcc-73a737681502c4880bee7b1825a4ff0c30f97e79.tar.gz gcc-73a737681502c4880bee7b1825a4ff0c30f97e79.tar.bz2 |
re PR c/8083 (GCC does not warn for aliasing violations)
PR c/8083
* c-typeck.c (build_c_cast): Warn about type punning which breaks
type based aliasing.
testsuite:
* gcc.dg/alias-1.c: New test.
From-SVN: r57698
-rw-r--r-- | gcc/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/c-typeck.c | 17 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 4 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/alias-1.c | 28 |
4 files changed, 56 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index a860318..600b7ea 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -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> diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 1bca1bf..6fd14e5 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -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); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 57d265f..ccfba4e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -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 index 0000000..71056e9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/alias-1.c @@ -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; + } + |