Add one more case to -Wparentheses: x + y << z.
authorChristoph Mallon <christoph.mallon@gmx.de>
Tue, 9 Dec 2008 10:23:32 +0000 (10:23 +0000)
committerChristoph Mallon <christoph.mallon@gmx.de>
Tue, 9 Dec 2008 10:23:32 +0000 (10:23 +0000)
[r24444]

cparser.1
parser.c

index c16c232..767f76d 100644 (file)
--- 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
index 3d0e465..d2ce0cd 100644 (file)
--- 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);