From patchwork Mon Oct 3 17:09:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 677769 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 3snpT32V4Nz9s5g for ; Tue, 4 Oct 2016 04:10:13 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=WP4NdyuC; 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:content-type:mime-version; q=dns; s=default; b=T3P8BdIvOFfz1XqEwRZQENOvWEkOwoxSJ3zycbbedzefBQL7We LJDQBPMCmgjh9XPepXNPxPWq81rtqHLxdF/vbMHC/jNpwIxCGN/AVuK1fEwbsiBZ AKiyq7AJHzVJFFg9UbnpFMyRYhZRWPRJhUKCEApp7IrmmsnMiDD0n2xNE= 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:content-type:mime-version; s= default; bh=oIa86sFKuLkvJ4xMzCEuutH+ZyU=; b=WP4NdyuCy6jAUEkt44gN HT8Jh2b3GsPU2AvCywJx+lftnS48aJOW9sie8ELegfOFYtW0zOOolOhvhinCKGCt SLqeFTUXgODXF8QWhLl5rAcG+E1jtckcyOcv/0b9RttfShg85GU9rRC44h1KxN5V xY8BlA4GmTGRbtLpL44K9aQ= Received: (qmail 68421 invoked by alias); 3 Oct 2016 17:09:59 -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 68316 invoked by uid 89); 3 Oct 2016 17:09:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=no version=3.3.2 spammy=dg-bogus, dgbogus, warning_at, HContent-Language:de-DE X-HELO: SNT004-OMC3S47.hotmail.com Received: from snt004-omc3s47.hotmail.com (HELO SNT004-OMC3S47.hotmail.com) (65.54.51.84) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 03 Oct 2016 17:09:48 +0000 Received: from EUR01-VE1-obe.outbound.protection.outlook.com ([65.55.90.137]) by SNT004-OMC3S47.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Mon, 3 Oct 2016 10:09:47 -0700 Received: from DB5EUR01FT019.eop-EUR01.prod.protection.outlook.com (10.152.4.56) by DB5EUR01HT037.eop-EUR01.prod.protection.outlook.com (10.152.5.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.629.5; Mon, 3 Oct 2016 17:09:45 +0000 Received: from AM4PR0701MB2162.eurprd07.prod.outlook.com (10.152.4.60) by DB5EUR01FT019.mail.protection.outlook.com (10.152.4.249) 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; Mon, 3 Oct 2016 17:09:45 +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.015; Mon, 3 Oct 2016 17:09:45 +0000 From: Bernd Edlinger To: GCC Patches CC: Jason Merrill , Jeff Law Subject: [PATCH] Extend -Wint-in-bool-context to more conditional expressions Date: Mon, 3 Oct 2016 17:09:45 +0000 Message-ID: authentication-results: spf=softfail (sender IP is 10.152.4.60) 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.4.60 as permitted sender) x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [0iIotdaGWk+ikiWStLJGI3tU7dTtr6QW] x-eopattributedmessage: 0 x-microsoft-exchange-diagnostics: 1; DB5EUR01HT037; 6:ez0O2Z9t7FrRiTmFOBj295A44wcn5MAY6IoXdWeapLtpkk2RoHEQFuxJ6LJDwb5ax62gCdq6hdsGzP/+0zMs9d6aNOKsU0nztjMlWEuNyu8twgGkuPNhRwa+f4R6Tc5GBkWas2k1jrwomGxJIvdbnZKKrhpuGei4weTgHJEZNQTpBefT2BxtLUQar9WwcpgCvLC3fSEtHXKVAvlJoTgukjdOFdPjjw0H5cUleXCAt/prMPdFLLapbdfvbQZD/RUFsk/nc3UZlmawQPZ48Ps+R3FOQz7398VHMQhemLu8eCs=; 5:/VbrwUWjoATFfSPE67yTDkXPtxl+vjIfOghozUgV+lp6Nr/ZlpP5nGGYBnqOEMcFzFJdphlMcdB4jYghfNT6PR8eYcSrcJRjiqxHYHhsfuQNLjoTeftQ4KaX0ja04ThLfwTIoXDfh16LfKRXuqiJCQ==; 24:MBps9tGBNV4/cHBgKmuKbdsSprbNABvOuJURhC2/c1ZTPdrhoRQ+hpW201YYawK6mpTIn+na3mkef3laDempvuT0ZGThNqzh/FBV5EiKmvI=; 7:4iqAU+8LZSEc8+NhyuLVRE5Z8NqgCSeIWDN/uhEI0N32hXrli/TtDPvyIJz7LsitHh0I0Pv03+/6OW6nw6fncTtvwZ0ivGGMTnTQBMohYiTr7AqbAuIKHEELqZ1CcAWFhIfDkYJeBhyLOGmaF592ygr/T1UWglf/8/fMgMhKAuN6FrVf5K68LpIdczw8i8sShByncI0WcNhZotje8miPYGgxsag6h+81DL7ysfrWPYx1b6gTOWV0mvj/okZHCOw7QYMil3BU0z5U4HuQablnqEJt3YeWngjQ6t37TINIpqlafJDz96GnhrM1H4gUkFvt x-forefront-antispam-report: EFV:NLI; SFV:NSPM; SFS:(10019020)(98900003); DIR:OUT; SFP:1102; SCL:1; SRVR:DB5EUR01HT037; H:AM4PR0701MB2162.eurprd07.prod.outlook.com; FPR:; SPF:None; LANG:en; x-ms-office365-filtering-correlation-id: c1719894-649f-4716-c37f-08d3ebb012cc x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(1601124038)(1603103081)(1601125047); SRVR:DB5EUR01HT037; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(432015012)(102415321)(82015046); SRVR:DB5EUR01HT037; BCL:0; PCL:0; RULEID:; SRVR:DB5EUR01HT037; x-forefront-prvs: 008421A8FF spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Oct 2016 17:09:45.0551 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB5EUR01HT037 Hi! This is a next step in extending the -Wint-in-bool-context warning to cover the case when a conditional expression has only one arm which evaluates to a non-boolean integer value. With a previous version of this warning, we found PR 77574, among with several more or less false positives, but meanwhile, mostly due to excluding conditional expressions that originate from macro expansion, there are no false positives any more, so I think this is fine now with -Wall. Bootstrapped and reg-tested on x86_64-pc-linux-gnu. Is it OK for trunk? Thanks Bernd. c-family: 2016-10-03 Bernd Edlinger * c-common.c (c_common_truthvalue_conversion): Warn also for suspicious conditional expression in boolean context when only one arm is non-boolean. testsuite: 2016-10-03 Bernd Edlinger * c-c++-common/Wint-in-bool-context.c: Update test. Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c (revision 240713) +++ gcc/c-family/c-common.c (working copy) @@ -4675,6 +4675,14 @@ c_common_truthvalue_conversion (location_t locatio warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context, "?: using integer constants in boolean context, " "the expression will always evaluate to %"); + else if ((TREE_CODE (val1) == INTEGER_CST + && !integer_zerop (val1) + && !integer_onep (val1)) + || (TREE_CODE (val2) == INTEGER_CST + && !integer_zerop (val2) + && !integer_onep (val2))) + warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context, + "?: using integer constants in boolean context"); } /* Distribute the conversion into the arms of a COND_EXPR. */ if (c_dialect_cxx ()) Index: gcc/testsuite/c-c++-common/Wint-in-bool-context.c =================================================================== --- gcc/testsuite/c-c++-common/Wint-in-bool-context.c (revision 240713) +++ gcc/testsuite/c-c++-common/Wint-in-bool-context.c (working copy) @@ -10,7 +10,7 @@ int foo (int a, int b) if (a > 0 && a <= (b == 2) ? 1 : 1) /* { dg-bogus "boolean context" } */ return 2; - if (a > 0 && a <= (b == 3) ? 0 : 2) /* { dg-bogus "boolean context" } */ + if (a > 0 && a <= (b == 3) ? 0 : 2) /* { dg-warning "boolean context" } */ return 3; if (a == b ? 0 : 0) /* { dg-bogus "boolean context" } */