From 2d8ca6673a8f66aa10b7acc7786e6331de3c9cc0 Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Wed, 26 May 2010 02:14:23 +0200
Subject: [PATCH] issue warnings for implied casts to bool [unstable]


Signed-off-by: Kamil Dudka <kdudka@redhat.com>
---
 evaluate.c |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/evaluate.c b/evaluate.c
index cdbd064..9532ab5 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -256,6 +256,38 @@ warn_for_different_enum_types (struct position pos,
 	}
 }
 
+static void
+warn_for_implied_cast_to_bool(struct expression *expr, struct symbol *typeb)
+{
+	struct symbol *typea = expr->ctype;
+	switch (expr->type) {
+	case EXPR_SELECT:
+	case EXPR_CONDITIONAL:
+		// FIXME: we are not ready for conditional expressions
+		return;
+
+	case EXPR_VALUE:
+		if (expr->value == 0 || expr->value == 1)
+			return;
+		// fall through!
+
+	default:
+		if (typea->type == SYM_NODE)
+			typea = typea->ctype.base_type;
+		if (typeb->type == SYM_NODE)
+			typeb = typeb->ctype.base_type;
+
+		// we are interested only in implied casts to bool
+		if (typea != &bool_ctype && typeb == &bool_ctype)
+		{
+			const char *a = strdup(show_typename(typea));
+			const char *b = show_typename(typeb);
+			warning(expr->pos, "implied cast from %s to %s", a, b);
+			free((char *) a);
+		}
+	}
+}
+
 /*
  * This gets called for implicit casts in assignments and
  * integer promotion. We often want to try to move the
@@ -305,6 +337,8 @@ static struct expression * cast_to(struct expression *old, struct symbol *type)
 		/* nothing */;
 	}
 
+	warn_for_implied_cast_to_bool(old, type);
+
 	expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
 	expr->flags = old->flags;
 	expr->ctype = type;
-- 
1.7.1


