From: Chris Forbes <chrisf@ijw.co.nz>
To: linux-sparse@vger.kernel.org
Cc: Chris Forbes <chrisf@ijw.co.nz>
Subject: [PATCH 3/3] evaluate: warn on identical exprs on ?:
Date: Sun, 28 Aug 2011 15:14:20 +1200 [thread overview]
Message-ID: <1314501260-27254-3-git-send-email-chrisf@ijw.co.nz> (raw)
In-Reply-To: <1314501260-27254-1-git-send-email-chrisf@ijw.co.nz>
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 <chrisf@ijw.co.nz>
---
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
+ */
--
1.7.4.1
next prev parent reply other threads:[~2011-08-28 3:14 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-28 3:14 [PATCH 1/3] evaluate: warn on identical exprs around '&&' Chris Forbes
2011-08-28 3:14 ` [PATCH 2/3] evaluate: warn on identical exprs around '||' Chris Forbes
2011-08-28 3:14 ` Chris Forbes [this message]
2011-08-29 10:01 ` [PATCH 1/3] evaluate: warn on identical exprs around '&&' Jonathan Neuschäfer
2011-08-29 10:25 ` Josh Triplett
-- strict thread matches above, loose matches on Subject: below --
2011-08-27 22:26 Chris Forbes
2011-08-27 22:26 ` [PATCH 3/3] evaluate: warn on identical exprs on ?: Chris Forbes
2011-08-28 2:46 ` Josh Triplett
2011-08-28 2:51 ` Chris Forbes
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1314501260-27254-3-git-send-email-chrisf@ijw.co.nz \
--to=chrisf@ijw.co.nz \
--cc=linux-sparse@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).