From patchwork Wed Dec 7 16:32:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9464797 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 BAFC86022E for ; Wed, 7 Dec 2016 16:33:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A79EA28425 for ; Wed, 7 Dec 2016 16:33:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9C6C92846E; Wed, 7 Dec 2016 16:33:39 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, 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 388B128425 for ; Wed, 7 Dec 2016 16:33:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932511AbcLGQdi (ORCPT ); Wed, 7 Dec 2016 11:33:38 -0500 Received: from mail-wm0-f67.google.com ([74.125.82.67]:35257 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932495AbcLGQdg (ORCPT ); Wed, 7 Dec 2016 11:33:36 -0500 Received: by mail-wm0-f67.google.com with SMTP id a20so28989636wme.2 for ; Wed, 07 Dec 2016 08:33:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=sUurMKYUr98l7iKG0LyU6GdaZ/MH/sBhnxkPevBY6aw=; b=etYDQtwKsHo6avahxso5gl8hxSz/4Sbf49rE1sErk9uxy6NNlmamTXmOg8o5NrvUgV aGQeBRmgRhTQnTEcCAJ/QTSR+S7yUWJfA6kjWA03m3/4sFvHaDafP9cn004jg8oOYMT/ eNDm6AMrYz40HvMi837MaW8LLh2/ByMjgdod4x4UKp3ScNWHSsIRI4mCnUlZUZsY5bp9 4/p4Ec0nlE8ijuBajJtbC3QG1gqm0ukK9DevR50+a6KjtD5E0Uvmq067UxdNrnlD/Dkg QfpSbr8DwVYF0IEvOS9QoUJyUOMnc/nuNLWE0uLIi7uGLB5UQIUoymUx70Q4Cf7i4Fmi bOWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=sUurMKYUr98l7iKG0LyU6GdaZ/MH/sBhnxkPevBY6aw=; b=JMMsh1tc7vKK6U4UbHQ/eLxoih05UUav+a275oXNT7p5DEi2DUkGAHlS+ws9ND5v7X 5LeBYcPh+WrDeczmCUzPKHNkDXXzA57+kLx6JBgRDfXS4UzPDU6jRtrnv2xe3yj+uHyo yCAWukfIOmTGp7cUqHAVdRqNLnD7P37v+2JPansW+O2CdhCUFfYmpj3avz4uSRc1fdN7 J/1pH2CJ8sYAISr4/Kin8si2MMZuf11E5yVjTff/IhjRuzNUvRXk2NRgiNnZ07VEIeYt rAu4Th3wVzAkIWJ45gBhr3zAjo9YMiIPh41Qopodi99+O5UxY0MZMwedV9frV6cA8pPc s7yQ== X-Gm-Message-State: AKaTC00ygECqwuGFLNMHxd5uco132oihQDy6GQU1OI+ypLwQqC6Ov1T/XYf+soyKUO9chw== X-Received: by 10.28.24.67 with SMTP id 64mr3373962wmy.136.1481128387246; Wed, 07 Dec 2016 08:33:07 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:816:ae00:7dc3:2e5b:38fe:befd]) by smtp.gmail.com with ESMTPSA id j1sm32249854wjm.26.2016.12.07.08.33.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Dec 2016 08:33:06 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Luc Van Oostenryck Subject: [PATCH 2/2] add warning option '-Wtautological-compare' Date: Wed, 7 Dec 2016 17:32:57 +0100 Message-Id: <20161207163257.22287-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161207163257.22287-1-luc.vanoostenryck@gmail.com> References: <20161207163257.22287-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 Now that we optimize away expressions like 'x == x' or 'x < x', also add a warning for it (but disabled by default). Signed-off-by: Luc Van Oostenryck --- lib.c | 2 ++ lib.h | 1 + simplify.c | 4 ++++ validation/tautological-compare.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 validation/tautological-compare.c diff --git a/lib.c b/lib.c index e5b0bb63..6b7a0817 100644 --- a/lib.c +++ b/lib.c @@ -236,6 +236,7 @@ int Wptr_subtraction_blows = 0; int Wreturn_void = 0; int Wshadow = 0; int Wsizeof_bool = 0; +int Wtautological_compare = 0; int Wtransparent_union = 0; int Wtypesign = 0; int Wundef = 0; @@ -471,6 +472,7 @@ static const struct warning { { "return-void", &Wreturn_void }, { "shadow", &Wshadow }, { "sizeof-bool", &Wsizeof_bool }, + { "tautological-compare", &Wtautological_compare }, { "transparent-union", &Wtransparent_union }, { "typesign", &Wtypesign }, { "undef", &Wundef }, diff --git a/lib.h b/lib.h index 15b69fa2..130e5b6c 100644 --- a/lib.h +++ b/lib.h @@ -122,6 +122,7 @@ extern int Wptr_subtraction_blows; extern int Wreturn_void; extern int Wshadow; extern int Wsizeof_bool; +extern int Wtautological_compare; extern int Wtransparent_union; extern int Wtypesign; extern int Wundef; diff --git a/simplify.c b/simplify.c index fde72f0a..b29b3ebb 100644 --- a/simplify.c +++ b/simplify.c @@ -494,6 +494,8 @@ static int simplify_binop_same_args(struct instruction *insn, pseudo_t arg) case OP_SET_NE: case OP_SET_LT: case OP_SET_GT: case OP_SET_B: case OP_SET_A: + if (Wtautological_compare) + warning(insn->pos, "self-comparison always evaluates to false"); case OP_SUB: case OP_XOR: return replace_with_pseudo(insn, value_pseudo(0)); @@ -501,6 +503,8 @@ static int simplify_binop_same_args(struct instruction *insn, pseudo_t arg) case OP_SET_EQ: case OP_SET_LE: case OP_SET_GE: case OP_SET_BE: case OP_SET_AE: + if (Wtautological_compare) + warning(insn->pos, "self-comparison always evaluates to true"); return replace_with_pseudo(insn, value_pseudo(1)); case OP_AND: diff --git a/validation/tautological-compare.c b/validation/tautological-compare.c new file mode 100644 index 00000000..55a2b463 --- /dev/null +++ b/validation/tautological-compare.c @@ -0,0 +1,35 @@ +typedef unsigned int u32; + +int seq(int a) { return a == a; } +int sne(int a) { return a != a; } +int slt(int a) { return a < a; } +int sgt(int a) { return a > a; } +int sle(int a) { return a <= a; } +int sge(int a) { return a >= a; } + +u32 ueq(u32 a) { return a == a; } +u32 une(u32 a) { return a != a; } +u32 ult(u32 a) { return a < a; } +u32 ugt(u32 a) { return a > a; } +u32 ule(u32 a) { return a <= a; } +u32 uge(u32 a) { return a >= a; } + +/* + * check-name: tautological-compare + * check-command: sparse -Wno-decl -Wtautological-compare $file + * + * check-error-start +tautological-compare.c:3:30: warning: self-comparison always evaluates to true +tautological-compare.c:4:30: warning: self-comparison always evaluates to false +tautological-compare.c:5:29: warning: self-comparison always evaluates to false +tautological-compare.c:6:29: warning: self-comparison always evaluates to false +tautological-compare.c:7:30: warning: self-comparison always evaluates to true +tautological-compare.c:8:30: warning: self-comparison always evaluates to true +tautological-compare.c:10:30: warning: self-comparison always evaluates to true +tautological-compare.c:11:30: warning: self-comparison always evaluates to false +tautological-compare.c:12:29: warning: self-comparison always evaluates to false +tautological-compare.c:13:29: warning: self-comparison always evaluates to false +tautological-compare.c:14:30: warning: self-comparison always evaluates to true +tautological-compare.c:15:30: warning: self-comparison always evaluates to true + * check-error-end + */