From 73a737681502c4880bee7b1825a4ff0c30f97e79 Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Tue, 1 Oct 2002 19:11:07 +0000 Subject: [PATCH] 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 --- gcc/ChangeLog | 7 +++++++ gcc/c-typeck.c | 17 +++++++++++++++++ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.dg/alias-1.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/alias-1.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index a860318aa12..600b7ea4a6e 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2002-10-01 Nathan Sidwell + + PR c/8083 + * c-typeck.c (build_c_cast): Warn about type punning which breaks + type based aliasing. + 2002-10-01 Mark Mitchell * stor-layout.c (update_alignment_for_field): New function. @@ -6,6 +12,7 @@ 2002-10-01 Nathan Sidwell + PR other/8077 * gcc.c (cc1_options): Add space on -auxbase-strip. 2002-10-01 Jim Wilson diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 1bca1bfb858..6fd14e5e743 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 57d265fe7f6..ccfba4efc33 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2002-10-01 Nathan Sidwell + + * gcc.dg/alias-1.c: New test. + 2002-10-01 Mark Mitchell * 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 00000000000..71056e9b6d8 --- /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 + +// 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; + } + -- 2.30.2