From 93561a28b91c00d70ec92f28fdcc7c7c83625bcd Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Thu, 5 Mar 2015 15:50:07 +0300 Subject: [PATCH] precedence: complain about "foo << bar - baz" The negate operation has higher precedence then the shift so "foo << bar - baz" can be re-written as "foo << (bar - baz)". When you see these in the code, it's normally a bug. There are some macros like Dbl_right_align() in parisc where people use "foo << bar - baz" and it's not a bug, but that's ugly code so I don't feel bad complaining about it. Signed-off-by: Dan Carpenter --- check_precedence.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/check_precedence.c b/check_precedence.c index 2d3537fb..5af19ad9 100644 --- a/check_precedence.c +++ b/check_precedence.c @@ -119,6 +119,17 @@ static void match_mask(struct expression *expr) sm_msg("warn: shift has higher precedence than mask"); } +static void match_subtract_shift(struct expression *expr) +{ + if (expr->op != SPECIAL_LEFTSHIFT) + return; + if (expr->right->type != EXPR_BINOP) + return; + if (expr->right->op != '-') + return; + sm_msg("warn: subtract is higher precedence than shift"); +} + void check_precedence(int id) { my_id = id; @@ -126,4 +137,5 @@ void check_precedence(int id) add_hook(&match_condition, CONDITION_HOOK); add_hook(&match_binop, BINOP_HOOK); add_hook(&match_mask, BINOP_HOOK); + add_hook(&match_subtract_shift, BINOP_HOOK); } -- 2.11.4.GIT