From 1d9335766d86262f8335d63d99b328bb44543b01 Mon Sep 17 00:00:00 2001 From: Bernd Edlinger Date: Fri, 15 Sep 2017 17:21:50 +0000 Subject: common.opt (Wcast-align=strict): New warning option. 2017-09-15 Bernd Edlinger * common.opt (Wcast-align=strict): New warning option. * doc/invoke.texi: Document -Wcast-align=strict. c: 2017-09-15 Bernd Edlinger * c-typeck.c (build_c_cast): Implement -Wcast-align=strict. cp: 2017-09-15 Bernd Edlinger * typeck.c (build_reinterpret_cast_1, build_const_cast_1): Implement -Wcast-align=strict. testsuite: 2017-09-15 Bernd Edlinger * c-c++-common/Wcast-align.c: New test. From-SVN: r252832 --- gcc/cp/ChangeLog | 5 +++++ gcc/cp/typeck.c | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) (limited to 'gcc/cp') diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 0a3b80f..c013225 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2017-09-15 Bernd Edlinger + + * typeck.c (build_reinterpret_cast_1, + build_const_cast_1): Implement -Wcast-align=strict. + 2017-09-15 Jakub Jelinek * decl.c (redeclaration_error_message): Use cxx17 instead of cxx1z, diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index aa5d128..028d56f 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -7265,15 +7265,16 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, complain)) return error_mark_node; /* Warn about possible alignment problems. */ - if (STRICT_ALIGNMENT && warn_cast_align - && (complain & tf_warning) + if ((STRICT_ALIGNMENT || warn_cast_align == 2) + && (complain & tf_warning) && !VOID_TYPE_P (type) && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE && COMPLETE_TYPE_P (TREE_TYPE (type)) && COMPLETE_TYPE_P (TREE_TYPE (intype)) - && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype))) + && min_align_of_type (TREE_TYPE (type)) + > min_align_of_type (TREE_TYPE (intype))) warning (OPT_Wcast_align, "cast from %qH to %qI " - "increases required alignment of target type", intype, type); + "increases required alignment of target type", intype, type); /* We need to strip nops here, because the front end likes to create (int *)&a for array-to-pointer decay, instead of &a[0]. */ @@ -7447,6 +7448,14 @@ build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain, the user is making a potentially unsafe cast. */ check_for_casting_away_constness (src_type, dst_type, CAST_EXPR, complain); + /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */ + if ((STRICT_ALIGNMENT || warn_cast_align == 2) + && (complain & tf_warning) + && min_align_of_type (TREE_TYPE (dst_type)) + > min_align_of_type (TREE_TYPE (src_type))) + warning (OPT_Wcast_align, "cast from %qH to %qI " + "increases required alignment of target type", + src_type, dst_type); } if (reference_type) { -- cgit v1.1