From patchwork Sun Aug 28 03:14:20 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Forbes X-Patchwork-Id: 1105432 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.4) with ESMTP id p7S3EebH027794 for ; Sun, 28 Aug 2011 03:14:40 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751527Ab1H1DOj (ORCPT ); Sat, 27 Aug 2011 23:14:39 -0400 Received: from mail-pz0-f42.google.com ([209.85.210.42]:42937 "EHLO mail-pz0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751128Ab1H1DOj (ORCPT ); Sat, 27 Aug 2011 23:14:39 -0400 Received: by mail-pz0-f42.google.com with SMTP id 37so6479251pzk.1 for ; Sat, 27 Aug 2011 20:14:39 -0700 (PDT) Received: by 10.142.222.14 with SMTP id u14mr1488729wfg.309.1314501279260; Sat, 27 Aug 2011 20:14:39 -0700 (PDT) Received: from localhost.localdomain (121-73-230-53.broadband.telstraclear.net. [121.73.230.53]) by mx.google.com with ESMTPS id u2sm10485700pbq.9.2011.08.27.20.14.37 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 27 Aug 2011 20:14:38 -0700 (PDT) From: Chris Forbes To: linux-sparse@vger.kernel.org Cc: Chris Forbes Subject: [PATCH 3/3] evaluate: warn on identical exprs on ?: Date: Sun, 28 Aug 2011 15:14:20 +1200 Message-Id: <1314501260-27254-3-git-send-email-chrisf@ijw.co.nz> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1314501260-27254-1-git-send-email-chrisf@ijw.co.nz> References: <1314501260-27254-1-git-send-email-chrisf@ijw.co.nz> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Sun, 28 Aug 2011 03:14:40 +0000 (UTC) Adds a warning when identical expressions are found on both the true and false branches of ?:. This is another common copy-paste error. Signed-off-by: Chris Forbes --- evaluate.c | 6 ++++++ validation/check_identical_exprs_on_cond.c | 13 +++++++++++++ 2 files changed, 19 insertions(+), 0 deletions(-) create mode 100644 validation/check_identical_exprs_on_cond.c diff --git a/evaluate.c b/evaluate.c index 0e5d691..c339e63 100644 --- a/evaluate.c +++ b/evaluate.c @@ -1220,6 +1220,12 @@ static struct symbol *evaluate_conditional_expression(struct expression *expr) const char * typediff; int qual; + if (!preprocessing) { + if (expr_equiv(expr->cond_true, expr->cond_false)) + warning(expr->pos, "identical expressions on both " + "branches of '?:'"); + } + if (!evaluate_conditional(expr->conditional, 0)) return NULL; if (!evaluate_expression(expr->cond_false)) diff --git a/validation/check_identical_exprs_on_cond.c b/validation/check_identical_exprs_on_cond.c new file mode 100644 index 0000000..85a9938 --- /dev/null +++ b/validation/check_identical_exprs_on_cond.c @@ -0,0 +1,13 @@ +extern void bar(void); + +static void foo(void *a, void *b, void *c) +{ + void * d = a ? b : b; /* should warn */ +} +/* + * check-name: A warning should be issued for identical expressions on both the true and false branches of the '?:' operator. + * + * check-error-start +check_identical_exprs_on_cond.c:5:22: warning: identical expressions on both branches of '?:' + * check-error-end + */