11f5207b7SJohn Levon /*
21f5207b7SJohn Levon * Copyright (C) 2010 Dan Carpenter.
31f5207b7SJohn Levon *
41f5207b7SJohn Levon * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon * of the License, or (at your option) any later version.
81f5207b7SJohn Levon *
91f5207b7SJohn Levon * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121f5207b7SJohn Levon * GNU General Public License for more details.
131f5207b7SJohn Levon *
141f5207b7SJohn Levon * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon */
171f5207b7SJohn Levon
181f5207b7SJohn Levon #include "smatch.h"
191f5207b7SJohn Levon #include "smatch_extra.h"
201f5207b7SJohn Levon
211f5207b7SJohn Levon static int my_id;
221f5207b7SJohn Levon
is_bool(struct expression * expr)231f5207b7SJohn Levon static int is_bool(struct expression *expr)
241f5207b7SJohn Levon {
251f5207b7SJohn Levon struct symbol *type;
261f5207b7SJohn Levon
271f5207b7SJohn Levon type = get_type(expr);
281f5207b7SJohn Levon if (!type)
291f5207b7SJohn Levon return 0;
301f5207b7SJohn Levon if (type_bits(type) == 1 && type->ctype.modifiers & MOD_UNSIGNED)
311f5207b7SJohn Levon return 1;
321f5207b7SJohn Levon return 0;
331f5207b7SJohn Levon }
341f5207b7SJohn Levon
is_bool_from_context(struct expression * expr)351f5207b7SJohn Levon static int is_bool_from_context(struct expression *expr)
361f5207b7SJohn Levon {
371f5207b7SJohn Levon sval_t sval;
381f5207b7SJohn Levon
391f5207b7SJohn Levon if (!get_implied_max(expr, &sval) || sval.uvalue > 1)
401f5207b7SJohn Levon return 0;
411f5207b7SJohn Levon if (!get_implied_min(expr, &sval) || sval.value < 0)
421f5207b7SJohn Levon return 0;
431f5207b7SJohn Levon return 1;
441f5207b7SJohn Levon }
451f5207b7SJohn Levon
is_bool_op(struct expression * expr)461f5207b7SJohn Levon static int is_bool_op(struct expression *expr)
471f5207b7SJohn Levon {
481f5207b7SJohn Levon expr = strip_expr(expr);
491f5207b7SJohn Levon
501f5207b7SJohn Levon if (expr->type == EXPR_PREOP && expr->op == '!')
511f5207b7SJohn Levon return 1;
521f5207b7SJohn Levon if (expr->type == EXPR_COMPARE)
531f5207b7SJohn Levon return 1;
541f5207b7SJohn Levon if (expr->type == EXPR_LOGICAL)
551f5207b7SJohn Levon return 1;
561f5207b7SJohn Levon return is_bool(expr);
571f5207b7SJohn Levon }
581f5207b7SJohn Levon
match_condition(struct expression * expr)591f5207b7SJohn Levon static void match_condition(struct expression *expr)
601f5207b7SJohn Levon {
611f5207b7SJohn Levon int print = 0;
621f5207b7SJohn Levon
631f5207b7SJohn Levon if (expr->type == EXPR_COMPARE) {
641f5207b7SJohn Levon if (expr->left->type == EXPR_COMPARE || expr->right->type == EXPR_COMPARE)
651f5207b7SJohn Levon print = 1;
661f5207b7SJohn Levon if (expr->left->type == EXPR_PREOP && expr->left->op == '!') {
671f5207b7SJohn Levon if (expr->left->unop->type == EXPR_PREOP && expr->left->unop->op == '!')
681f5207b7SJohn Levon return;
691f5207b7SJohn Levon if (expr->right->op == '!')
701f5207b7SJohn Levon return;
711f5207b7SJohn Levon if (is_bool(expr->right))
721f5207b7SJohn Levon return;
731f5207b7SJohn Levon if (is_bool(expr->left->unop))
741f5207b7SJohn Levon return;
751f5207b7SJohn Levon if (is_bool_from_context(expr->left->unop))
761f5207b7SJohn Levon return;
771f5207b7SJohn Levon print = 1;
781f5207b7SJohn Levon }
791f5207b7SJohn Levon }
801f5207b7SJohn Levon
811f5207b7SJohn Levon if (expr->type == EXPR_BINOP) {
821f5207b7SJohn Levon if (expr->left->type == EXPR_COMPARE || expr->right->type == EXPR_COMPARE)
831f5207b7SJohn Levon print = 1;
841f5207b7SJohn Levon }
851f5207b7SJohn Levon
861f5207b7SJohn Levon if (print) {
871f5207b7SJohn Levon sm_warning("add some parenthesis here?");
881f5207b7SJohn Levon return;
891f5207b7SJohn Levon }
901f5207b7SJohn Levon
911f5207b7SJohn Levon if (expr->type == EXPR_BINOP && expr->op == '&') {
921f5207b7SJohn Levon int i = 0;
931f5207b7SJohn Levon
941f5207b7SJohn Levon if (is_bool_op(expr->left))
951f5207b7SJohn Levon i++;
961f5207b7SJohn Levon if (is_bool_op(expr->right))
971f5207b7SJohn Levon i++;
981f5207b7SJohn Levon if (i == 1)
991f5207b7SJohn Levon sm_warning("maybe use && instead of &");
1001f5207b7SJohn Levon }
1011f5207b7SJohn Levon }
1021f5207b7SJohn Levon
match_binop(struct expression * expr)1031f5207b7SJohn Levon static void match_binop(struct expression *expr)
1041f5207b7SJohn Levon {
1051f5207b7SJohn Levon if (expr->op != '&')
1061f5207b7SJohn Levon return;
1071f5207b7SJohn Levon if (expr->left->op == '!')
1081f5207b7SJohn Levon sm_warning("add some parenthesis here?");
1091f5207b7SJohn Levon }
1101f5207b7SJohn Levon
match_mask(struct expression * expr)1111f5207b7SJohn Levon static void match_mask(struct expression *expr)
1121f5207b7SJohn Levon {
1131f5207b7SJohn Levon if (expr->op != '&')
1141f5207b7SJohn Levon return;
1151f5207b7SJohn Levon if (expr->right->type != EXPR_BINOP)
1161f5207b7SJohn Levon return;
1171f5207b7SJohn Levon if (expr->right->op != SPECIAL_RIGHTSHIFT)
1181f5207b7SJohn Levon return;
1191f5207b7SJohn Levon
1201f5207b7SJohn Levon sm_warning("shift has higher precedence than mask");
1211f5207b7SJohn Levon }
1221f5207b7SJohn Levon
match_mask_compare(struct expression * expr)123*efe51d0cSJohn Levon static void match_mask_compare(struct expression *expr)
124*efe51d0cSJohn Levon {
125*efe51d0cSJohn Levon if (expr->op != '&')
126*efe51d0cSJohn Levon return;
127*efe51d0cSJohn Levon if (expr->right->type != EXPR_COMPARE)
128*efe51d0cSJohn Levon return;
129*efe51d0cSJohn Levon
130*efe51d0cSJohn Levon sm_warning("compare has higher precedence than mask");
131*efe51d0cSJohn Levon }
132*efe51d0cSJohn Levon
match_subtract_shift(struct expression * expr)1331f5207b7SJohn Levon static void match_subtract_shift(struct expression *expr)
1341f5207b7SJohn Levon {
1351f5207b7SJohn Levon if (expr->op != SPECIAL_LEFTSHIFT)
1361f5207b7SJohn Levon return;
1371f5207b7SJohn Levon if (expr->right->type != EXPR_BINOP)
1381f5207b7SJohn Levon return;
1391f5207b7SJohn Levon if (expr->right->op != '-')
1401f5207b7SJohn Levon return;
1411f5207b7SJohn Levon sm_warning("subtract is higher precedence than shift");
1421f5207b7SJohn Levon }
1431f5207b7SJohn Levon
check_precedence(int id)1441f5207b7SJohn Levon void check_precedence(int id)
1451f5207b7SJohn Levon {
1461f5207b7SJohn Levon my_id = id;
1471f5207b7SJohn Levon
1481f5207b7SJohn Levon add_hook(&match_condition, CONDITION_HOOK);
1491f5207b7SJohn Levon add_hook(&match_binop, BINOP_HOOK);
1501f5207b7SJohn Levon add_hook(&match_mask, BINOP_HOOK);
151*efe51d0cSJohn Levon add_hook(&match_mask_compare, BINOP_HOOK);
1521f5207b7SJohn Levon add_hook(&match_subtract_shift, BINOP_HOOK);
1531f5207b7SJohn Levon }
154