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