From: Christoph Mallon Date: Tue, 9 Dec 2008 10:23:32 +0000 (+0000) Subject: Add one more case to -Wparentheses: x + y << z. X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=26073a8608126e36f18bd77c0f5ab9aa18d2d955;p=cparser Add one more case to -Wparentheses: x + y << z. [r24444] --- diff --git a/cparser.1 b/cparser.1 index c16c232..767f76d 100644 --- a/cparser.1 +++ b/cparser.1 @@ -1,5 +1,5 @@ .\" Please adjust this date whenever revising the manpage. -.Dd December 7, 2008 +.Dd December 9, 2008 .Dt CPARSER 1 .Sh NAME .Nm cparser @@ -185,6 +185,7 @@ Warn if an assignment is used as condition, e.g. if\ (x\ =\ 23). Warn if && without parentheses is used within ||, e.g. if\ (x\ ||\ y\ &&\ z). Warn if it there may be confusion which 'if'-statement an 'else'-branch belongs to, e.g. if\ (x)\ if\ (y)\ {}\ else\ {}. Warn if cascaded comparisons appear which do not have their mathematical meaning, e.g. if\ (23\ <=\ x\ <\ 42). +Warn if + or - is used as operand of << or >>, e.g. x\ +\ y\ <<\ z. .It Fl Wredundant-decls Warn about redundant declarations, i.e. multiple declarations of the same object or static forward declarations which have no use before their definition. .It Fl Wunreachable-code diff --git a/parser.c b/parser.c index 3d0e465..d2ce0cd 100644 --- a/parser.c +++ b/parser.c @@ -8833,6 +8833,19 @@ static void semantic_divmod_arithmetic(binary_expression_t *expression) { warn_div_by_zero(expression); } +static void warn_addsub_in_shift(const expression_t *const expr) +{ + char op; + switch (expr->kind) { + case EXPR_BINARY_ADD: op = '+'; break; + case EXPR_BINARY_SUB: op = '-'; break; + default: return; + } + + warningf(&expr->base.source_position, + "suggest parentheses around '%c' inside shift", op); +} + static void semantic_shift_op(binary_expression_t *expression) { expression_t *const left = expression->left; @@ -8851,6 +8864,11 @@ static void semantic_shift_op(binary_expression_t *expression) return; } + if (warning.parentheses) { + warn_addsub_in_shift(left); + warn_addsub_in_shift(right); + } + type_left = promote_integer(type_left); type_right = promote_integer(type_right);