From patchwork Fri May 19 02:47:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9735687 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 576A5601C2 for ; Fri, 19 May 2017 02:47:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 409C9287C3 for ; Fri, 19 May 2017 02:47:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 358F2287DA; Fri, 19 May 2017 02:47:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A92F2287C3 for ; Fri, 19 May 2017 02:47:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755567AbdESCrO (ORCPT ); Thu, 18 May 2017 22:47:14 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:34473 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752301AbdESCrM (ORCPT ); Thu, 18 May 2017 22:47:12 -0400 Received: by mail-wm0-f65.google.com with SMTP id d127so15140450wmf.1 for ; Thu, 18 May 2017 19:47:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2PoZcjfUlkPzuLFUSnEzxD/rnZJEWudT8pvouGPO2m8=; b=CCqnnuEQhud0d0HmiQ3Nw+Gs0mj16Ylwzo8HUB9SU7+GE45zAjHhlirgWvp9aLXTMI dLkuuiMDB10vr7CZSSJcpAD2xT3A8g/X8GZkcdDpmjao5cU9uNDxuUR/BMbXwkdeaOOE zyGuPWND/ExmG9E41zbKTQk354icZvmXHbAyajyBGsVm0luqi/sB+nmTSJCO02Q9PafO dnWL1OphH4D65yVZPI8D4pNIW1nIGGxQTL5T9h+1fI6R1F63dlI3h7hEEcD2NNjP8dkL hYLErh2WnsDSFgyGpQnQQvmbwiMVsMCFh8zWORA7+PgmQ8O9/mu7H5FPNS+1LnqWgcwz +C1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=2PoZcjfUlkPzuLFUSnEzxD/rnZJEWudT8pvouGPO2m8=; b=VOYse++RRMHrd7Fde1EzKFJ3DIp9oSNc0kU4NnF22QQMqyIPAUu9nGEfVZBTB39vFv ZYerPLuW0PqBlqO+Wl2ZLplDZHLoQ54vWvIxHG6DpdlsjkSOQbqX5lMX1XvEIXniRKyY yqRAuBZx6Q+XcwAn9H15pmB7iUac/u2sty4pFjJX+e45d1IC0ek7J2Rs4B3I4t2mjtcn mLA17ww8sooOgbGTFJ7WWVA1NnshDfn1jhDvILDr8bY3DZHfrgibkLeq5wEtP2+OhsdG CTOLOdAXvLs1oiY58k/BRTSkDTNQjBROmSN5UIJpgQH8XzBulEYmImB9BJBwZ8kwVEZ6 /AZw== X-Gm-Message-State: AODbwcAJS6UmCNvSsKNJpwxCJUaKfhfakFU+Hhwc1eQoygJwhpl7RcOB Ilewza3fRKT+rAWyoBM= X-Received: by 10.80.165.194 with SMTP id b2mr5321436edc.84.1495162030788; Thu, 18 May 2017 19:47:10 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:82d:ce00:702e:d47b:4e6:a8c8]) by smtp.gmail.com with ESMTPSA id q42sm4306582edd.24.2017.05.18.19.47.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 May 2017 19:47:10 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Al Viro , Luc Van Oostenryck Subject: [PATCH v3 2/2] avoid warning on explicit 'bool <- restricted' casts Date: Fri, 19 May 2017 04:47:02 +0200 Message-Id: <20170519024702.31443-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170519024702.31443-1-luc.vanoostenryck@gmail.com> References: <20170519024702.31443-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Conversion to bool is special in C since this conversion is essentially the result of the comparison with zero. As such, some operations which are normally unsafe to do with restricted types, like casting to an unrestricted type, are in fact safe to do when converting to bool and issuing a warning in those case is useless, confusing and causes people to add useless casts in the code in order to shut up the warning. Fix this by catching explicit 'bool <- restricted type' casts and not emit a warning like for others casts of restricted type to non-restrictes ones. Based-on-patch-by: Al Viro Signed-off-by: Luc Van Oostenryck --- evaluate.c | 13 ++++++++++--- validation/bool-cast-bad.c | 1 - validation/bool-cast-explicit.c | 4 ---- validation/bool-cast-restricted.c | 13 +++++++++++-- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/evaluate.c b/evaluate.c index 3dc26fc09..6bcccde2a 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2821,9 +2821,16 @@ static struct symbol *evaluate_cast(struct expression *expr) if ((class1 & TYPE_RESTRICT) && restricted_value(target, t1)) warning(expr->pos, "cast to %s", show_typename(t1)); - if (class2 & TYPE_RESTRICT) - warning(expr->pos, "cast from %s", - show_typename(t2)); + if (class2 & TYPE_RESTRICT) { + if (t1 == &bool_ctype) { + if (class2 & TYPE_FOULED) + warning(expr->pos, "%s degrades to integer", + show_typename(t2)); + } else { + warning(expr->pos, "cast from %s", + show_typename(t2)); + } + } } if (t1 == &ulong_ctype) diff --git a/validation/bool-cast-bad.c b/validation/bool-cast-bad.c index a0b091e1c..ae8b96018 100644 --- a/validation/bool-cast-bad.c +++ b/validation/bool-cast-bad.c @@ -15,7 +15,6 @@ static _Bool fstse(struct s a) { return (_Bool)a; } * check-command: sparse $file * * check-error-start -bool-cast-bad.c:9:42: warning: cast from restricted le16 bool-cast-bad.c:10:41: warning: incorrect type in return expression (different base types) bool-cast-bad.c:10:41: expected bool bool-cast-bad.c:10:41: got struct s a diff --git a/validation/bool-cast-explicit.c b/validation/bool-cast-explicit.c index 4e3c2b7ce..dbb67cc42 100644 --- a/validation/bool-cast-explicit.c +++ b/validation/bool-cast-explicit.c @@ -19,8 +19,4 @@ static _Bool fres(le16 a) { return (_Bool)a; } * check-command: test-linearize -m64 $file * check-output-ignore * check-output-excludes: cast\\. - * - * check-error-start -bool-cast-explicit.c:15:37: warning: cast from restricted le16 - * check-error-end */ diff --git a/validation/bool-cast-restricted.c b/validation/bool-cast-restricted.c index 0aa9f35b4..9985d6e32 100644 --- a/validation/bool-cast-restricted.c +++ b/validation/bool-cast-restricted.c @@ -3,12 +3,16 @@ typedef unsigned int __attribute__((bitwise)) large_t; _Bool lfoo(large_t x) { return x; } _Bool qfoo(large_t x) { _Bool r = x; return r; } +_Bool xfoo(large_t x) { return (_Bool)x; } _Bool lbar(large_t x) { return ~x; } _Bool qbar(large_t x) { _Bool r = ~x; return r; } +_Bool xbar(large_t x) { return (_Bool)~x; } _Bool lbaz(large_t x) { return !x; } _Bool qbaz(large_t x) { _Bool r = !x; return r; } +_Bool xbaz(large_t x) { return (_Bool)!x; } _Bool lqux(large_t x) { return x & LBIT; } _Bool qqux(large_t x) { _Bool r = x & LBIT; return r; } +_Bool xqux(large_t x) { return (_Bool)(x & LBIT); } typedef unsigned short __attribute__((bitwise)) small_t; @@ -16,19 +20,24 @@ typedef unsigned short __attribute__((bitwise)) small_t; _Bool sfoo(small_t x) { return x; } _Bool tfoo(small_t x) { _Bool r = x; return r; } +_Bool zfoo(small_t x) { return (_Bool)x; } _Bool sbar(small_t x) { return ~x; } _Bool tbar(small_t x) { _Bool r = ~x; return r; } +_Bool zbar(small_t x) { return (_Bool)~x; } _Bool sbaz(small_t x) { return !x; } _Bool tbaz(small_t x) { _Bool r = !x; return r; } +_Bool zbaz(small_t x) { return (_Bool)!x; } _Bool squx(small_t x) { return x & SBIT; } _Bool tqux(small_t x) { _Bool r = x & SBIT; return r; } +_Bool zqux(small_t x) { return (_Bool)(x & SBIT); } /* * check-name: bool-cast-restricted.c * check-command: sparse -Wno-decl $file * * check-error-start -bool-cast-restricted.c:19:32: warning: restricted small_t degrades to integer -bool-cast-restricted.c:20:35: warning: restricted small_t degrades to integer +bool-cast-restricted.c:24:32: warning: restricted small_t degrades to integer +bool-cast-restricted.c:25:35: warning: restricted small_t degrades to integer +bool-cast-restricted.c:26:33: warning: restricted small_t degrades to integer * check-error-end */