From patchwork Fri Oct 21 20:20:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 685263 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 3t0xrh00RXz9syB for ; Sat, 22 Oct 2016 07:20:47 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=urXuiu70; 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:subject:date:message-id:content-type:mime-version; q=dns; s= default; b=hbEEDtNLZYIGOFSSDFNwZDds/h7Tq8mLpJK0o62xGJzinRbZy4YhT wo+Ttyk3Tch4vZMtnHy9mlCAR77yw1bi6gismyJq0xjJZdSXFOhbjK/u3t4Lo6u1 n4hGCmi/N3LpSWHgNjC4nwwpFihcSkrAt6kLuZcsvrqCvoyA2ZrJrI= 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:content-type:mime-version; s= default; bh=u86bu0wboPziP4sNKX6ug/4GDxo=; b=urXuiu70+L6uLZiiVKVC gtsiIRfbCWtrw5tj8vwjYMuKN3ttVFqxoLgg2k+3LP4Os5kiSNvKdzy7zWMTDYVY 0HEvwYZyUeQFHrOb8BNUd3FWmmLogi7l30vnXCW2fwCLbdx4Xz40XdUMnjGwGpOw Urxvo901TudvJLvipFS/hno= Received: (qmail 56692 invoked by alias); 21 Oct 2016 20:20:39 -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 56640 invoked by uid 89); 21 Oct 2016 20:20:38 -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, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 spammy=multiplication, locatio, H*c:HHH X-HELO: SNT004-OMC3S49.hotmail.com Received: from snt004-omc3s49.hotmail.com (HELO SNT004-OMC3S49.hotmail.com) (65.54.51.86) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 21 Oct 2016 20:20:28 +0000 Received: from EUR02-AM5-obe.outbound.protection.outlook.com ([65.55.90.137]) by SNT004-OMC3S49.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Fri, 21 Oct 2016 13:20:27 -0700 Received: from AM5EUR02FT058.eop-EUR02.prod.protection.outlook.com (10.152.8.54) by AM5EUR02HT015.eop-EUR02.prod.protection.outlook.com (10.152.9.2) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.669.7; Fri, 21 Oct 2016 20:20:25 +0000 Received: from AM4PR0701MB2162.eurprd07.prod.outlook.com (10.152.8.55) by AM5EUR02FT058.mail.protection.outlook.com (10.152.9.140) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.669.7 via Frontend Transport; Fri, 21 Oct 2016 20:20: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.0679.006; Fri, 21 Oct 2016 20:20:25 +0000 From: Bernd Edlinger To: "gcc-patches@gcc.gnu.org" , Jeff Law , Jason Merrill , Joseph Myers Subject: [PATCH] Extend -Wint-in-bool-context to warn for multiplications Date: Fri, 21 Oct 2016 20:20:25 +0000 Message-ID: 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-ms-exchange-messagesentrepresentingtype: 1 x-eopattributedmessage: 0 x-microsoft-exchange-diagnostics: 1; AM5EUR02HT015; 6:DSlO1aifzfCHoB4e3oEgQF/51DSBqid+q2nzkRDrGSGC6z+nh+OietXIchGkDRf6/lpzPPuo3MwF6/jTPesD9X7Cy0FjxNVPATIGhSDSuykEgSbrXngwayFKOCnqiOGrdtfzLV8HTpfRgXoD0E2l+HBn6zKSMFj5DtYsz01T0BPLW5KhpLG0jLQ2OTi+jbOqvnO+s+zz8YAUFY+iN0/SdQu3yOiNAIfgb9bitHveuY7jN4vGsm4jGTqANIAYNvlluHm6ktYUXu5kxF2J+KUZrZpuVv7/Ba07hQ424PXtuyQ/njEb3M+dHPa8x3dxu4br; 5:XUkmObWZzv087IRTWr67OPsopdQlI5B4TNNmji3digXPlZ19jmDtusUo8n7tqq2wDGkPEphR8MyNK1H/kgYSwukFFXKWwuf1aHnOWIM+Jl0lCuZRxIP+Dof+Cn9iZmDLl086JnA84KwWPf+IZEqvOg==; 24:On/t6MG+TEJaa3XQENqaBTIEl4wxglYd8EQWV5DYeRL+f/gOlfPMX1PL6JZd6SEgENWkEN9RudRmZgWQGHc572Ioe/KIwOu6ZKo45BfxC40=; 7:QakqIg76rI7nLwe0EwXg35n7tHSwOY1ebc5UlYbgvwbPkobzNa3vcrq00FzAmYX718oIIN4X9+wBKYrv4Xwm3fsu2DXhf/IcenbHvmQ89Qplss+LcYNlStQWyu3Hq0RB28ITVCsxnLbHK0/FBRLUzFjFow8v93Jt7Dt8l+l7BJKEIx8DLHKwwTsIjgSRWmOdMD1j/QJooXuozHnCcuJ3lRXkXbZoJXqCzL4nehkldY+cVYJZ38DxFvmEtuCBNeqXhxH652ENzKzEMPZBLheG5VaHA26dyl2K2Yr0QrrH0Lgywn2XUp9LdNQ/d1D0/8cJ2m2HCijQhldeN43r07H8MT4nyiKlnIZVtO2xEs9pTJk= x-forefront-antispam-report: EFV:NLI; SFV:NSPM; SFS:(10019020)(98900003); DIR:OUT; SFP:1102; SCL:1; SRVR:AM5EUR02HT015; H:AM4PR0701MB2162.eurprd07.prod.outlook.com; FPR:; SPF:None; LANG:en; x-ms-office365-filtering-correlation-id: 29cb72c8-29b5-4889-ad15-08d3f9efb07a x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(1601124038)(1603103081)(1601125047); SRVR:AM5EUR02HT015; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(432015012)(102415321)(82015046); SRVR:AM5EUR02HT015; BCL:0; PCL:0; RULEID:; SRVR:AM5EUR02HT015; x-forefront-prvs: 01026E1310 spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Oct 2016 20:20:25.2679 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5EUR02HT015 Hi! This patch extends -Wint-in-bool-context to warn for multiplications if used in boolean context. This is rarely useful, and where used, could be easily replaced with && for instance. I think that multiplications in boolean context should be warned about, regardless of the used data type. This warning found already one bug in value-prof.c, at stringop_block_profile where an undefined overflow in a signed multiplication was used to terminate a loop. Fixed as well. Bootstrapped and reg-tested on x86_64-pc-linux-gnu. Is it OK for trunk? Thanks Bernd. c-family: 2016-10-21 Bernd Edlinger * c-common.c (c_common_truthvalue_conversion): Warn for multiplications in boolean context. gcc: 2016-10-21 Bernd Edlinger * doc/invoke.text (Wint-in-bool-context): Update documentation. * value-prof.c (stringop_block_profile): Fix a warning. testsuite: 2016-10-21 Bernd Edlinger * c-c++-common/Wint-in-bool-context-3.c: New test. Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c (revision 241400) +++ gcc/c-family/c-common.c (working copy) @@ -3327,6 +3327,11 @@ c_common_truthvalue_conversion (location_t locatio return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); + case MULT_EXPR: + warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context, + "* in boolean context, better use '&&' here"); + break; + case LSHIFT_EXPR: /* We will only warn on signed shifts here, because the majority of false positive warnings happen in code where unsigned arithmetic Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 241400) +++ gcc/doc/invoke.texi (working copy) @@ -6169,8 +6169,9 @@ of the C++ standard. @opindex Wno-int-in-bool-context Warn for suspicious use of integer values where boolean values are expected, such as conditional expressions (?:) using non-boolean integer constants in -boolean context, like @code{if (a <= b ? 2 : 3)}. Or left shifting in -boolean context, like @code{for (a = 0; 1 << a; a++);}. +boolean context, like @code{if (a <= b ? 2 : 3)}. Or left shifting of signed +integers in boolean context, like @code{for (a = 0; 1 << a; a++);}. Likewise +for all kinds of multiplications regardless of the data type. This warning is enabled by @option{-Wall}. @item -Wno-int-to-pointer-cast Index: gcc/value-prof.c =================================================================== --- gcc/value-prof.c (revision 241400) +++ gcc/value-prof.c (working copy) @@ -1878,12 +1878,12 @@ stringop_block_profile (gimple *stmt, unsigned int else { gcov_type count; - int alignment; + unsigned int alignment; count = histogram->hvalue.counters[0]; alignment = 1; while (!(count & alignment) - && (alignment * 2 * BITS_PER_UNIT)) + && (alignment <= UINT_MAX / 2 / BITS_PER_UNIT)) alignment <<= 1; *expected_align = alignment * BITS_PER_UNIT; gimple_remove_histogram_value (cfun, stmt, histogram); Index: gcc/testsuite/c-c++-common/Wint-in-bool-context-3.c =================================================================== --- gcc/testsuite/c-c++-common/Wint-in-bool-context-3.c (revision 0) +++ gcc/testsuite/c-c++-common/Wint-in-bool-context-3.c (working copy) @@ -0,0 +1,15 @@ +/* { dg-options "-Wint-in-bool-context" } */ +/* { dg-do compile } */ + +#define BITS_PER_UNIT 8 + +int foo (int count) +{ + int alignment; + + alignment = 1; + while (!(count & alignment) + && (alignment * 2 * BITS_PER_UNIT)) /* { dg-warning "boolean context" } */ + alignment <<= 1; + return alignment * BITS_PER_UNIT; +}