From patchwork Thu Sep 29 19:58:24 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 676737 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3slQPG2qzxz9sC3 for ; Fri, 30 Sep 2016 05:58:41 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=oh2Hdh2l; dkim-atps=neutral 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:cc:subject:date:message-id:references:in-reply-to :content-type:content-id:content-transfer-encoding:mime-version; q=dns; s=default; b=eAIwnuWh7y7Iri6T0umwJfUYryumINv+ahJBjRfLPwX JVKGS0GnswvLgsCYsf9KFJySJAAoFAPvd5s+3ZD07QxEyaNOOVCmh+IvFscza54z hymTzy/hTsbBz6CAnGH7PVf5bRZxwqRHyB2p06cWBumle1TZXpg6LGBhOW7dp7AQ = 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:cc:subject:date:message-id:references:in-reply-to :content-type:content-id:content-transfer-encoding:mime-version; s=default; bh=aH0/7HLZ+CJI9R6NROePOhFYZpc=; b=oh2Hdh2lcLPSmiFCQ IAF0oNRi8CHz3VI7KslcGM+zOXg53BzBIG4euB1iLBI2+dHYzQTdA4ROSD+5Ftab owKGBnEs7P6zAfjvSLAj9MLQniBXUpcXI2VVqw6JsL/3mwh1lDIAUAuSIDdSdSlX 0WigbMGUdaYo8ZdKll0zn5oZBo= Received: (qmail 5530 invoked by alias); 29 Sep 2016 19:58:32 -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 5490 invoked by uid 89); 29 Sep 2016 19:58:31 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, MIME_BASE64_BLANKS, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 spammy=H*MI:sk:AM4PR07, H*i:sk:AM4PR07 X-HELO: SNT004-OMC4S9.hotmail.com Received: from snt004-omc4s9.hotmail.com (HELO SNT004-OMC4S9.hotmail.com) (65.55.90.212) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 29 Sep 2016 19:58:30 +0000 Received: from EUR03-AM5-obe.outbound.protection.outlook.com ([65.55.90.199]) by SNT004-OMC4S9.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Thu, 29 Sep 2016 12:58:28 -0700 Received: from DB5EUR03FT041.eop-EUR03.prod.protection.outlook.com (10.152.20.54) by DB5EUR03HT190.eop-EUR03.prod.protection.outlook.com (10.152.21.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.629.5; Thu, 29 Sep 2016 19:58:25 +0000 Received: from AM4PR0701MB2162.eurprd07.prod.outlook.com (10.152.20.57) by DB5EUR03FT041.mail.protection.outlook.com (10.152.21.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.629.5 via Frontend Transport; Thu, 29 Sep 2016 19:58:25 +0000 Received: from AM4PR0701MB2162.eurprd07.prod.outlook.com ([10.167.132.147]) by AM4PR0701MB2162.eurprd07.prod.outlook.com ([10.167.132.147]) with mapi id 15.01.0639.011; Thu, 29 Sep 2016 19:58:24 +0000 From: Bernd Edlinger To: Jason Merrill CC: Florian Weimer , "gcc-patches@gcc.gnu.org" , Jeff Law Subject: Re: [PATCH] Make -Wint-in-bool-context warn on suspicious shift ops Date: Thu, 29 Sep 2016 19:58:24 +0000 Message-ID: References: <87shslv769.fsf@mid.deneb.enyo.de> <87fuol77ri.fsf@mid.deneb.enyo.de> In-Reply-To: authentication-results: spf=softfail (sender IP is 10.152.20.57) smtp.mailfrom=hotmail.de; redhat.com; dkim=none (message not signed) header.d=none; redhat.com; dmarc=none action=none header.from=hotmail.de; received-spf: SoftFail (protection.outlook.com: domain of transitioning hotmail.de discourages use of 10.152.20.57 as permitted sender) x-ms-exchange-messagesentrepresentingtype: 1 x-eopattributedmessage: 0 x-microsoft-exchange-diagnostics: 1; DB5EUR03HT190; 6:OBXCIOzU0beWeNdRGNjH0F5ujmS9AFPsibr1FGV8TdvUSQeQ3dWAMFXgieCFo7hx8i7h3rHdQOlSFnzA9IL5awZZydz9pOklybwQkCerdLTnJ0j+OyOLcFcGZ6ubKfQdOwTLCMlmUWq5eJ8CbtCm4stNmVt1VUR3CGGKJ4c0t/B79F8bS2zal+KJbYPdoo+5nM9CpdDzqzECXKRq49CXzEhoEXgffIeJRx5qDtGaO/Ep+gSGqavVVNPd9tCuGIAaV31P9uoX7/LI15wDMKHizuf/0CZZzalYFTEP0A0nn+M=; 5:BEUQ25ISaEbtBK68G0evCgmklBeqENjwucJ4ajcL5iPiUeZMvik2zd/+ujyCEspRb+G9zy0NA90AfpKLViWWw5k8huF47jrxYj4y3f+KXuoMclQpnppNoYn77Djz3mtDpCk8RheRegJUEmxyDTCA7Q==; 24:BHoMMb1GuYO+urZTfjGGS0qOlplr+alrs7KGK/ayrTXl0ZjlGy0ZddSJ/gOE6M8V3G2JGSkYIE9WzkNX71jofC1xgAt8I6oqIhGK9DUer2E=; 7:8j7V1vU551hqZDNYmHF/kP+GwFWy1tYh9+XDVfYotC18wafr+hZK/huVZPTthOpXuuv5CFFNmaHENs0Bn0BcRaf5bCuVMTgPtR1tA4+r5JOZF3ylqvXyy/HfVbJmJQQusQge/PkCy2kXVjg42D8xTqvv19QFSCANGqB4PfS2ZUcNOzZQxZTUC+rl7V4EzeUMKRaxim7YYzlMMuchIMep3aKLtS4AJJEmvAyrkOxOHp72l9Sns3fz4KsiS7BlYSeedDiPXduv1h8vw4C7ie0uRK2YHaozL/zg993v+46yaq24HFKTXjb/WkOvg4d6ksL+ x-forefront-antispam-report: EFV:NLI; SFV:NSPM; SFS:(10019020)(98900003); DIR:OUT; SFP:1102; SCL:1; SRVR:DB5EUR03HT190; H:AM4PR0701MB2162.eurprd07.prod.outlook.com; FPR:; SPF:None; LANG:en; x-ms-office365-filtering-correlation-id: 7da21ab4-9372-4fb3-36b7-08d3e8a2f7fc x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(1601124038)(1603103081)(1601125047); SRVR:DB5EUR03HT190; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(432015012)(82015046); SRVR:DB5EUR03HT190; BCL:0; PCL:0; RULEID:; SRVR:DB5EUR03HT190; x-forefront-prvs: 00808B16F3 spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-ID: <83A2B6B005A090409CDD08DB26324750@eurprd07.prod.outlook.com> MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Sep 2016 19:58:24.2978 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB5EUR03HT190 On 09/29/16 20:52, Bernd Edlinger wrote: > On 09/29/16 20:03, Jason Merrill wrote: >> >> What do you think about dropping the TYPE_UNSIGNED exception as well? >> I don't see what difference that makes. >> > > > If I drop that exception, then I could also drop the check for > INTEGER_TYPE and the whole if, because I think other types can not > happen, but if they are allowed they are as well bogus here. > > I can try a bootstrap and see if there are false positives. > > But I can do that as well in a follow-up patch, this should probably > be done step by step, especially when it may trigger some false > positives. > > I think I could also add more stuff, like unary + or - ? > or maybe also binary +, -, * and / ? > > We already discussed making this a multi-level option, > and maybe enabling the higher level explicitly in the > boot-strap. > > As long as the warning continues to find more bugs than false > positives, it is probably worth extending it to more cases. > > However unsigned integer shift are not undefined if they overflow. > > It is possible that this warning will then trigger also on valid > code that does loop termination with unsigned int left shifting. > I dont have a real example, but maybe like this hypothetical C-code: > > unsigned int x=1, bits=0; > while (x << bits) bits++; > printf("bits=%d\n", bits); > > > Is it OK for everybody to warn for this on -Wall, or maybe only > when -Wextra or for instance -Wint-in-bool-context=2 is used ? > > Unfortunately, without that exception there is a false positive: In file included from ../../gcc-trunk/gcc/ada/gcc-interface/decl.c:30:0: ../../gcc-trunk/gcc/ada/gcc-interface/decl.c: In function 'int adjust_packed(tree, tree, int)': ../../gcc-trunk/gcc/tree.h:1874:22: error: << on signed integer in boolean context [-Werror=int-in-bool-context] ? ((unsigned)1) << ((NODE)->type_common.align - 1) : 0) ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../../gcc-trunk/gcc/ada/gcc-interface/decl.c:6928:7: note: in expansion of macro 'TYPE_ALIGN' if (TYPE_ALIGN (record_type) ^~~~~~~~~~ But that did not happen with this version: Is that version OK for you? Bernd. Index: c-common.c =================================================================== --- c-common.c (revision 240571) +++ c-common.c (working copy) @@ -4655,6 +4655,14 @@ c_common_truthvalue_conversion (location_t locatio return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); + case LSHIFT_EXPR: + /* Warn on signed integer left shift. */ + if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE + && !TYPE_UNSIGNED (TREE_TYPE (expr))) + warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context, + "<< on signed integer in boolean context"); + break; + case COND_EXPR: if (warn_int_in_bool_context && !from_macro_definition_at (EXPR_LOCATION (expr)))