From patchwork Thu May 16 20:56:06 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Wang X-Patchwork-Id: 2580271 Return-Path: X-Original-To: patchwork-linux-sparse@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 0E1F83FE1F for ; Thu, 16 May 2013 21:05:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754093Ab3EPVFC (ORCPT ); Thu, 16 May 2013 17:05:02 -0400 Received: from mail-ye0-f180.google.com ([209.85.213.180]:44305 "EHLO mail-ye0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753346Ab3EPVFA (ORCPT ); Thu, 16 May 2013 17:05:00 -0400 X-Greylist: delayed 448 seconds by postgrey-1.27 at vger.kernel.org; Thu, 16 May 2013 17:05:00 EDT Received: by mail-ye0-f180.google.com with SMTP id r11so708655yen.39 for ; Thu, 16 May 2013 14:05:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer; bh=GuT7AQICm2H/Xh3YcnUidk6iIGCCS+Q7Bc2rpji8H7w=; b=BvDNjQvVMtCOwQFh9R02Q0N02qgjeQga8YuZSlYg9wqYkikIgTKyyk6kiIkpGSr3a8 /7KolOVmsfNLM9YWNA440dRYEb4mJOnVpTBsNFcsWDHgWMbVq1mMclD/ysilmVR5Yxoo EJ5Q7wwaPLVjG24SGqOc2/SQLLUroYPniiYVs1JaPjUMWAy9L3qE0ZTuYupelGl+auTF IqtqLCxf8jv2o6rcVMn4vy095wLpygGEhYHoT4sG+Ux48+zRKfrNIYxytstRn2hYy640 8L0pKr0R/u+KMOC+FklLcUGUpUKE2qukWHPPw0wOqyt/L7w9/7BHb29l5JSmmfR1e1up bbnw== X-Received: by 10.236.116.164 with SMTP id g24mr23345486yhh.178.1368737852194; Thu, 16 May 2013 13:57:32 -0700 (PDT) Received: from hchen.csail.mit.edu (hchen.csail.mit.edu. [18.26.5.5]) by mx.google.com with ESMTPSA id a24sm11835081yhj.23.2013.05.16.13.57.31 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 16 May 2013 13:57:31 -0700 (PDT) From: Xi Wang To: linux-sparse@vger.kernel.org Cc: sparse@chrisli.org, Xi Wang Subject: [PATCH] fix casting to bool Date: Thu, 16 May 2013 16:56:06 -0400 Message-Id: <1368737766-6517-1-git-send-email-xi.wang@gmail.com> X-Mailer: git-send-email 1.8.1.2 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Consider the following function. static _Bool foo(int x) { return x; } Currently sparse emits: scast.1 %r2 <- (32) %arg1 ret.1 %r2 This is incorrect since bool requires a zero test. setne.32 %r2 <- %arg1, $0 ret.1 %r2 This patch adds zero testing for bool in cast_to(). Signed-off-by: Xi Wang Acked-by: Pekka Enberg --- evaluate.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/evaluate.c b/evaluate.c index d9c767f..a090028 100644 --- a/evaluate.c +++ b/evaluate.c @@ -272,6 +272,18 @@ static struct expression * cast_to(struct expression *old, struct symbol *type) if (old->ctype != &null_ctype && is_same_type(old, type)) return old; + /* bool requires a zero test */ + if (is_bool_type(type)) { + expr = alloc_expression(old->pos, EXPR_COMPARE); + expr->op = SPECIAL_NOTEQUAL; + expr->ctype = type; + expr->left = old; + expr->right = alloc_expression(old->pos, EXPR_VALUE); + expr->right->ctype = old->ctype; + expr->right->value = 0; + return expr; + } + /* * See if we can simplify the op. Move the cast down. */