1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2012 Oracle.
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 
check_constant(struct expression * expr)22*1f5207b7SJohn Levon static void check_constant(struct expression *expr)
23*1f5207b7SJohn Levon {
24*1f5207b7SJohn Levon 	sval_t val;
25*1f5207b7SJohn Levon 
26*1f5207b7SJohn Levon 	if (!get_value(expr->right, &val))
27*1f5207b7SJohn Levon 		return;
28*1f5207b7SJohn Levon 	sm_warning("was '== %s' instead of '='", sval_to_str(val));
29*1f5207b7SJohn Levon }
30*1f5207b7SJohn Levon 
check_address(struct expression * expr)31*1f5207b7SJohn Levon static void check_address(struct expression *expr)
32*1f5207b7SJohn Levon {
33*1f5207b7SJohn Levon 	char *str;
34*1f5207b7SJohn Levon 	struct expression *right = strip_expr(expr->right);
35*1f5207b7SJohn Levon 
36*1f5207b7SJohn Levon 	if (!__cur_stmt || __cur_stmt->type != STMT_IF)
37*1f5207b7SJohn Levon 		return;
38*1f5207b7SJohn Levon 
39*1f5207b7SJohn Levon 	if (right->type != EXPR_PREOP ||
40*1f5207b7SJohn Levon 	    right->op != '&')
41*1f5207b7SJohn Levon 		return;
42*1f5207b7SJohn Levon 
43*1f5207b7SJohn Levon 	if (get_macro_name(expr->pos))
44*1f5207b7SJohn Levon 		return;
45*1f5207b7SJohn Levon 
46*1f5207b7SJohn Levon 	str = expr_to_str(right);
47*1f5207b7SJohn Levon 	sm_warning("was '== %s' instead of '='", str);
48*1f5207b7SJohn Levon 	free_string(str);
49*1f5207b7SJohn Levon }
50*1f5207b7SJohn Levon 
match_condition(struct expression * expr)51*1f5207b7SJohn Levon static void match_condition(struct expression *expr)
52*1f5207b7SJohn Levon {
53*1f5207b7SJohn Levon 	if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
54*1f5207b7SJohn Levon 		return;
55*1f5207b7SJohn Levon 
56*1f5207b7SJohn Levon 	check_constant(expr);
57*1f5207b7SJohn Levon 	check_address(expr);
58*1f5207b7SJohn Levon }
59*1f5207b7SJohn Levon 
check_assign_vs_compare(int id)60*1f5207b7SJohn Levon void check_assign_vs_compare(int id)
61*1f5207b7SJohn Levon {
62*1f5207b7SJohn Levon 	my_id = id;
63*1f5207b7SJohn Levon 	add_hook(&match_condition, CONDITION_HOOK);
64*1f5207b7SJohn Levon }
65