1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2010 Dan Carpenter.
3*1f5207b7SJohn Levon  *
4*1f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon  * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*1f5207b7SJohn Levon  * GNU General Public License for more details.
13*1f5207b7SJohn Levon  *
14*1f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon  */
17*1f5207b7SJohn Levon 
18*1f5207b7SJohn Levon #include "smatch.h"
19*1f5207b7SJohn Levon 
20*1f5207b7SJohn Levon static int my_id;
21*1f5207b7SJohn Levon 
match_inside(struct expression * expr,struct position pos)22*1f5207b7SJohn Levon static void match_inside(struct expression *expr, struct position pos)
23*1f5207b7SJohn Levon {
24*1f5207b7SJohn Levon 	char *name;
25*1f5207b7SJohn Levon 	int matched = 0;
26*1f5207b7SJohn Levon 
27*1f5207b7SJohn Levon 	if (positions_eq(expr->pos, pos))
28*1f5207b7SJohn Levon 		matched++;
29*1f5207b7SJohn Levon 	if (positions_eq(expr->unop->pos, pos))
30*1f5207b7SJohn Levon 		matched++;
31*1f5207b7SJohn Levon 	if (matched != 1)
32*1f5207b7SJohn Levon 		return;
33*1f5207b7SJohn Levon 	name = get_macro_name(pos);
34*1f5207b7SJohn Levon 	if (!name)
35*1f5207b7SJohn Levon 		return;
36*1f5207b7SJohn Levon 	sm_warning("the '%s' macro might need parens", name);
37*1f5207b7SJohn Levon }
38*1f5207b7SJohn Levon 
match_one_side(struct expression * expr,struct position pos,int op)39*1f5207b7SJohn Levon static void match_one_side(struct expression *expr, struct position pos, int op)
40*1f5207b7SJohn Levon {
41*1f5207b7SJohn Levon 	char *name;
42*1f5207b7SJohn Levon 	int matched = 0;
43*1f5207b7SJohn Levon 
44*1f5207b7SJohn Levon 	if ((op == '+' || op == '*' || op == '|' || op == '&') && expr->op == op)
45*1f5207b7SJohn Levon 		return;
46*1f5207b7SJohn Levon 	if (positions_eq(expr->right->pos, pos))
47*1f5207b7SJohn Levon 		matched++;
48*1f5207b7SJohn Levon 	if (positions_eq(expr->left->pos, pos))
49*1f5207b7SJohn Levon 		matched++;
50*1f5207b7SJohn Levon 	if (matched != 1)
51*1f5207b7SJohn Levon 		return;
52*1f5207b7SJohn Levon 	name = get_macro_name(pos);
53*1f5207b7SJohn Levon 	if (!name)
54*1f5207b7SJohn Levon 		return;
55*1f5207b7SJohn Levon 	if (option_project == PROJ_WINE && !strcmp("BEGIN", name))
56*1f5207b7SJohn Levon 		return;
57*1f5207b7SJohn Levon 	sm_warning("the '%s' macro might need parens", name);
58*1f5207b7SJohn Levon }
59*1f5207b7SJohn Levon 
match_join(struct expression * expr)60*1f5207b7SJohn Levon static void match_join(struct expression *expr)
61*1f5207b7SJohn Levon {
62*1f5207b7SJohn Levon 	if (expr->left->type == EXPR_PREOP)
63*1f5207b7SJohn Levon 		match_inside(expr->left, expr->pos);
64*1f5207b7SJohn Levon 	if (expr->right->type == EXPR_POSTOP)
65*1f5207b7SJohn Levon 		match_inside(expr->right, expr->pos);
66*1f5207b7SJohn Levon 
67*1f5207b7SJohn Levon 	if (expr->left->type == EXPR_BINOP)
68*1f5207b7SJohn Levon 		match_one_side(expr->left, expr->pos, expr->op);
69*1f5207b7SJohn Levon 	if (expr->right->type == EXPR_BINOP)
70*1f5207b7SJohn Levon 		match_one_side(expr->right, expr->pos, expr->op);
71*1f5207b7SJohn Levon }
72*1f5207b7SJohn Levon 
check_macros(int id)73*1f5207b7SJohn Levon void check_macros(int id)
74*1f5207b7SJohn Levon {
75*1f5207b7SJohn Levon 	my_id = id;
76*1f5207b7SJohn Levon 	add_hook(&match_join, BINOP_HOOK);
77*1f5207b7SJohn Levon 	add_hook(&match_join, LOGIC_HOOK);
78*1f5207b7SJohn Levon }
79