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
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 /*
19*1f5207b7SJohn Levon  * Some macros don't return NULL pointers.  Complain if people
20*1f5207b7SJohn Levon  * check the results for NULL because obviously the programmers
21*1f5207b7SJohn Levon  * don't know what the pants they're doing.
22*1f5207b7SJohn Levon  */
23*1f5207b7SJohn Levon 
24*1f5207b7SJohn Levon #include "smatch.h"
25*1f5207b7SJohn Levon 
26*1f5207b7SJohn Levon static int my_id;
27*1f5207b7SJohn Levon 
28*1f5207b7SJohn Levon STATE(non_null);
29*1f5207b7SJohn Levon 
is_ok(struct sm_state * sm,struct expression * mod_expr)30*1f5207b7SJohn Levon static void is_ok(struct sm_state *sm, struct expression *mod_expr)
31*1f5207b7SJohn Levon {
32*1f5207b7SJohn Levon 	set_state(my_id, sm->name, sm->sym, &undefined);
33*1f5207b7SJohn Levon }
34*1f5207b7SJohn Levon 
match_non_null(const char * fn,struct expression * expr,void * unused)35*1f5207b7SJohn Levon static void match_non_null(const char *fn, struct expression *expr, void *unused)
36*1f5207b7SJohn Levon {
37*1f5207b7SJohn Levon 	set_state_expr(my_id, expr->left, &non_null);
38*1f5207b7SJohn Levon }
39*1f5207b7SJohn Levon 
match_condition(struct expression * expr)40*1f5207b7SJohn Levon static void match_condition(struct expression *expr)
41*1f5207b7SJohn Levon {
42*1f5207b7SJohn Levon 	if (__in_pre_condition)
43*1f5207b7SJohn Levon 		return;
44*1f5207b7SJohn Levon 
45*1f5207b7SJohn Levon 	if (get_macro_name(expr->pos))
46*1f5207b7SJohn Levon 		return;
47*1f5207b7SJohn Levon 
48*1f5207b7SJohn Levon 	if (get_state_expr(my_id, expr) == &non_null) {
49*1f5207b7SJohn Levon 		char *name;
50*1f5207b7SJohn Levon 
51*1f5207b7SJohn Levon 		name = expr_to_var(expr);
52*1f5207b7SJohn Levon 		sm_warning("can '%s' even be NULL?", name);
53*1f5207b7SJohn Levon 		set_state_expr(my_id, expr, &undefined);
54*1f5207b7SJohn Levon 		free_string(name);
55*1f5207b7SJohn Levon 	}
56*1f5207b7SJohn Levon }
57*1f5207b7SJohn Levon 
check_container_of(int id)58*1f5207b7SJohn Levon void check_container_of(int id)
59*1f5207b7SJohn Levon {
60*1f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
61*1f5207b7SJohn Levon 		return;
62*1f5207b7SJohn Levon 
63*1f5207b7SJohn Levon 	my_id = id;
64*1f5207b7SJohn Levon 	add_macro_assign_hook("container_of", &match_non_null, NULL);
65*1f5207b7SJohn Levon 	add_macro_assign_hook("list_first_entry", &match_non_null, NULL);
66*1f5207b7SJohn Levon 	add_function_assign_hook("nla_data", &match_non_null, NULL);
67*1f5207b7SJohn Levon 	add_modification_hook(my_id, &is_ok);
68*1f5207b7SJohn Levon 	add_hook(&match_condition, CONDITION_HOOK);
69*1f5207b7SJohn Levon }