From patchwork Thu Sep 14 16:33:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 813896 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-462152-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="vRhLzSdM"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xtPGr508Zz9s7g for ; Fri, 15 Sep 2017 02:33:23 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:references:in-reply-to:content-type :mime-version; q=dns; s=default; b=U0z0Ym/XSKP2yfXKasXyuHzDxVWds iPwNmdUSQ/yS5S/yPyUFIYSnScnaTxzDxlQlwrqpe8RAqMs98YivUmGSsR1MLqev DReRkecYyxqmbtF9IpEWSMl1lf0IEOqxFNLNAL+ZBwl2RS32ZXpoBGKwzj7OMOqo /W43ULNNhXogcc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:references:in-reply-to:content-type :mime-version; s=default; bh=Dr226OcUhFY1rvQsVzXSC0G5/g8=; b=vRh LzSdMOMkcKTdZYwIJNzuY9y7MFjxwusNeepivK69S8kOWQgvkYF9PwvuY+F1JhbU CeJLLPWdUtcM9pp9378Ou85v8SCFn2EoWlmkouTqxn3r3DVv6NdKNW4RjyVhEUhA btE0G6dmXh734njRgoRJiLXZZjHugkIe58h9NlGc= Received: (qmail 42118 invoked by alias); 14 Sep 2017 16:33:15 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 42105 invoked by uid 89); 14 Sep 2017 16:33:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-15.2 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=H*c:sk:HMHPRHH, gol X-HELO: EUR02-VE1-obe.outbound.protection.outlook.com Received: from mail-oln040092069040.outbound.protection.outlook.com (HELO EUR02-VE1-obe.outbound.protection.outlook.com) (40.92.69.40) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 14 Sep 2017 16:33:13 +0000 Received: from VE1EUR02FT045.eop-EUR02.prod.protection.outlook.com (10.152.12.52) by VE1EUR02HT108.eop-EUR02.prod.protection.outlook.com (10.152.13.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.20.13.11; Thu, 14 Sep 2017 16:33:10 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com (10.152.12.57) by VE1EUR02FT045.mail.protection.outlook.com (10.152.12.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.20.35.14 via Frontend Transport; Thu, 14 Sep 2017 16:33:10 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::8c96:1341:5db1:7f8c]) by AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::8c96:1341:5db1:7f8c%18]) with mapi id 15.20.0056.010; Thu, 14 Sep 2017 16:33:10 +0000 From: Bernd Edlinger To: "gcc-patches@gcc.gnu.org" , Nathan Sidwell , Jason Merrill , Joseph Myers , Marek Polacek Subject: [PATCHv2] Add a -Wcast-align=strict warning Date: Thu, 14 Sep 2017 16:33:09 +0000 Message-ID: References: In-Reply-To: authentication-results: gcc.gnu.org; dkim=none (message not signed) header.d=none; gcc.gnu.org; dmarc=none action=none header.from=hotmail.de; x-incomingtopheadermarker: OriginalChecksum:21FE42B3341AEB55FB78A2590ADF915D1DC83C3D992911ABE488C1EA73BEF739; UpperCasedChecksum:36D2E736CC4A3F8DFD3131BDEA8724314D3BD27C8031D0B132F21C8329D5F135; SizeAsReceived:7297; Count:46 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [5bowgFl9rWHxsdneIws8JacJeXaNXlNh] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; VE1EUR02HT108; 6:1eJUnj/wqfA5NonmVhMvzKO53HEHpJz/LqKGOvgq6XOq8fTlJmmZ2ILL5GeLb2XS4h2XuF2vXDtIdItJHD89nCOiWWy06nMxodv3LPbKEln2VehmuWfO++Z8E7A2z6QhjfdnEPyJxuK/IqfiCz1OV5RrjZCT4C8DHhli3sZdgRIuu//RJIAkVv+3kIc0AbYQH1j1kZj1hbom2KQVQn/Jw+0Zw88XZs+YXkZpsRTzRiAxEpezFc84ORgOZNuMzwp8YAu0egHvlAJ5PGKPF7DKROzf3acfGldUpEY3kXGIPo/pHsc6NP2PolOM4CcBdAmL02Ry+mK669Hm9I1nn+MJPw==; 5:OKk8j7Gp8od4gPq09zD0f7Wg8gUTxqqpQwVVdHSZqTPUjdu0uY7yaD3OXu0uiLstT+joDEMQQorUd5y6ctcnQteLjA5DixpTJow5uIbj6bDn0lAJOUZz7clefi4CykHd8eF/e1q/auXE29CK92v6ig==; 24:QPuxoV7Sr4gn9+ChxudTEX5ftIM6M+y2d+Sm9RZ7JgRczAE4uEj9EvoW5SOXycUmlOB7DLBgTOKbqQKGzmDPEpqu8lnwy43s+PGxwg0d2fs=; 7:PeWQKefPf1umb+DlSRqINXsccpYXthwPxVx8Tw2yZktEoSJnAzFoI0QPUBEs7KU6xmLQI+HGR7o1sc/2XHVFpj8lMgQMz85EbgLDQYm0y5kD+wl8AYusGm7GVb9DWe9WikpGWpABglCy6VOipXJGjh+OglNiqbIVv59ErBYjiT5yKt01X6INNJu10wJoG658A3xMQl37gbtGRrevhx5x9AVtSykWBPKqs6yE3+ysBqw= x-incomingheadercount: 46 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 0cbd1266-f007-4f7e-414a-08d4fb8e48fc x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(300000503095)(300135400095)(201702061074)(5061506573)(5061507331)(1603103135)(2017031320274)(2017031324274)(2017031323274)(2017031322404)(1603101448)(1601125374)(1701031045)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:VE1EUR02HT108; x-ms-traffictypediagnostic: VE1EUR02HT108: x-exchange-antispam-report-test: UriScan:; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(444000031); SRVR:VE1EUR02HT108; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:VE1EUR02HT108; x-forefront-prvs: 0430FA5CB7 x-forefront-antispam-report: SFV:NSPM; SFS:(7070007)(98901004); DIR:OUT; SFP:1901; SCL:1; SRVR:VE1EUR02HT108; H:AM5PR0701MB2657.eurprd07.prod.outlook.com; FPR:; SPF:None; LANG:; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Sep 2017 16:33:10.0927 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1EUR02HT108 On 09/04/17 10:07, Bernd Edlinger wrote: > Hi, > > as you know we have a -Wcast-align warning which works only for > STRICT_ALIGNMENT targets. But occasionally it would be nice to be > able to switch this warning on even for other targets. > > Therefore I would like to add a strict version of this option > which can be invoked with -Wcast-align=strict. With the only > difference that it does not depend on STRICT_ALIGNMENT. > > I used the code from check_effective_target_non_strict_align > in target-supports.exp for the first version of the test case, > where we have this: > > return [check_no_compiler_messages non_strict_align assembly { > char *y; > typedef char __attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))) c; > c *z; > void foo(void) { z = (c *) y; } > } "-Wcast-align"] > > ... and to my big surprise it did _not_ work for C++ as-is, > because same_type_p considers differently aligned types identical, > and therefore cp_build_c_cast tries the conversion first via a > const_cast which succeeds, but did not emit the cast-align warning > in this case. > > As a work-around I had to check the alignment in build_const_cast_1 > as well. > > > Bootstrapped and reg-tested on x86_64-pc-linux-gnu. > Is it OK for trunk? > Hi, as suggested by Joseph, here is an updated patch that uses min_align_of_type instead of TYPE_ALIGN. Is it OK? Thanks, Bernd. gcc: 2017-09-03 Bernd Edlinger * common.opt (Wcast-align=strict): New warning option. * doc/invoke.texi: Document -Wcast-align=strict. c: 2017-09-03 Bernd Edlinger * c-typeck.c (build_c_cast): Implement -Wcast-align=strict. cp: 2017-09-03 Bernd Edlinger * typeck.c (build_reinterpret_cast_1, build_const_cast_1): Implement -Wcast-align=strict. testsuite: 2017-09-03 Bernd Edlinger * c-c++-common/Wcast-align.c: New test. Index: gcc/c/c-typeck.c =================================================================== --- gcc/c/c-typeck.c (revision 251617) +++ gcc/c/c-typeck.c (working copy) @@ -5578,7 +5578,7 @@ build_c_cast (location_t loc, tree type, } /* Warn about possible alignment problems. */ - if (STRICT_ALIGNMENT + if ((STRICT_ALIGNMENT || warn_cast_align == 2) && TREE_CODE (type) == POINTER_TYPE && TREE_CODE (otype) == POINTER_TYPE && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE @@ -5587,7 +5587,8 @@ build_c_cast (location_t loc, tree type, restriction is unknown. */ && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype)) && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) - && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) + && min_align_of_type (TREE_TYPE (type)) + > min_align_of_type (TREE_TYPE (otype))) warning_at (loc, OPT_Wcast_align, "cast increases required alignment of target type"); Index: gcc/common.opt =================================================================== --- gcc/common.opt (revision 251617) +++ gcc/common.opt (working copy) @@ -564,6 +564,10 @@ Wcast-align Common Var(warn_cast_align) Warning Warn about pointer casts which increase alignment. +Wcast-align=strict +Common Var(warn_cast_align,2) Warning +Warn about pointer casts which increase alignment. + Wcpp Common Var(warn_cpp) Init(1) Warning Warn when a #warning directive is encountered. Index: gcc/cp/typeck.c =================================================================== --- gcc/cp/typeck.c (revision 251617) +++ gcc/cp/typeck.c (working copy) @@ -7265,15 +7265,16 @@ build_reinterpret_cast_1 (tree type, tre 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 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) { Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 251617) +++ gcc/doc/invoke.texi (working copy) @@ -266,7 +266,8 @@ Objective-C and Objective-C++ Dialects}. -Wno-attributes -Wbool-compare -Wbool-operation @gol -Wno-builtin-declaration-mismatch @gol -Wno-builtin-macro-redefined -Wc90-c99-compat -Wc99-c11-compat @gol --Wc++-compat -Wc++11-compat -Wc++14-compat -Wcast-align -Wcast-qual @gol +-Wc++-compat -Wc++11-compat -Wc++14-compat @gol +-Wcast-align -Wcast-align=strict -Wcast-qual @gol -Wchar-subscripts -Wchkp -Wcatch-value -Wcatch-value=@var{n} @gol -Wclobbered -Wcomment -Wconditionally-supported @gol -Wconversion -Wcoverage-mismatch -Wno-cpp -Wdangling-else -Wdate-time @gol @@ -5923,6 +5924,12 @@ target is increased. For example, warn if a @code an @code{int *} on machines where integers can only be accessed at two- or four-byte boundaries. +@item -Wcast-align=strict +@opindex Wcast-align=strict +Warn whenever a pointer is cast such that the required alignment of the +target is increased. For example, warn if a @code{char *} is cast to +an @code{int *} regardless of the target machine. + @item -Wwrite-strings @opindex Wwrite-strings @opindex Wno-write-strings Index: gcc/testsuite/c-c++-common/Wcast-align.c =================================================================== --- gcc/testsuite/c-c++-common/Wcast-align.c (revision 0) +++ gcc/testsuite/c-c++-common/Wcast-align.c (working copy) @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-options "-Wcast-align=strict" } */ + +typedef char __attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))) c; +typedef struct __attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))) +{ + char x; +} d; + +char *x; +c *y; +d *z; +struct s { long long x; } *p; +struct t { double x; } *q; + +void +foo (void) +{ + y = (c *) x; /* { dg-warning "alignment" } */ + z = (d *) x; /* { dg-warning "alignment" } */ + (long long *) p; /* { dg-bogus "alignment" } */ + (double *) q; /* { dg-bogus "alignment" } */ +}