From 1d55aba65f56e111f90716f7489ed02ff8a8860a Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Tue, 22 Dec 2009 19:34:14 +0200 Subject: [PATCH] precedence: warn about: if (!a & b) {... It is easy to forget that the ! is done first. It would be better to say if ((!a) & b) or if (!(a & b)) { so it's either clear or a bug fix. Signed-off-by: Dan Carpenter --- check_precedence.c | 10 ++++++++++ smatch.h | 1 + smatch_flow.c | 3 +-- smatch_hooks.c | 3 +++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/check_precedence.c b/check_precedence.c index e59a3b33..ad4ada64 100644 --- a/check_precedence.c +++ b/check_precedence.c @@ -20,9 +20,19 @@ static void match_condition(struct expression *expr) sm_msg("warning: do you want parens here?"); } +static void match_binop(struct expression *expr) +{ + if (expr->op != '&') + return; + if (expr->left->op == '!') + sm_msg("warning: you probably want parens here."); +} + + void check_precedence(int id) { my_id = id; add_hook(&match_condition, CONDITION_HOOK); + add_hook(&match_binop, BINOP_HOOK); } diff --git a/smatch.h b/smatch.h index a0ce86ea..e65874ae 100644 --- a/smatch.h +++ b/smatch.h @@ -72,6 +72,7 @@ enum hook_type { WHOLE_CONDITION_HOOK, FUNCTION_CALL_HOOK, CALL_ASSIGNMENT_HOOK, + BINOP_HOOK, OP_HOOK, DEREF_HOOK, CASE_HOOK, diff --git a/smatch_flow.c b/smatch_flow.c index 6847269d..e9542e81 100644 --- a/smatch_flow.c +++ b/smatch_flow.c @@ -77,9 +77,8 @@ void __split_expr(struct expression *expr) __merge_true_states(); __pop_false_only_stack(); return; - - return; case EXPR_BINOP: + __pass_to_client(expr, BINOP_HOOK); case EXPR_COMMA: case EXPR_COMPARE: __split_expr(expr->left); diff --git a/smatch_hooks.c b/smatch_hooks.c index 71ab7e4c..9c85020e 100644 --- a/smatch_hooks.c +++ b/smatch_hooks.c @@ -59,6 +59,9 @@ void add_hook(void *func, enum hook_type type) case CALL_ASSIGNMENT_HOOK: container->data_type = EXPR_PTR; break; + case BINOP_HOOK: + container->data_type = EXPR_PTR; + break; case OP_HOOK: container->data_type = EXPR_PTR; break; -- 2.11.4.GIT