1/*
2 * Copyright (C) 2010 Dan Carpenter.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 */
17
18/*
19 * Some macros don't return NULL pointers.  Complain if people
20 * check the results for NULL because obviously the programmers
21 * don't know what the pants they're doing.
22 */
23
24#include "smatch.h"
25
26static int my_id;
27
28STATE(non_null);
29
30static void is_ok(struct sm_state *sm, struct expression *mod_expr)
31{
32	set_state(my_id, sm->name, sm->sym, &undefined);
33}
34
35static void match_non_null(const char *fn, struct expression *expr, void *unused)
36{
37	set_state_expr(my_id, expr->left, &non_null);
38}
39
40static void match_condition(struct expression *expr)
41{
42	if (__in_pre_condition)
43		return;
44
45	if (get_macro_name(expr->pos))
46		return;
47
48	if (get_state_expr(my_id, expr) == &non_null) {
49		char *name;
50
51		name = expr_to_var(expr);
52		sm_warning("can '%s' even be NULL?", name);
53		set_state_expr(my_id, expr, &undefined);
54		free_string(name);
55	}
56}
57
58void check_container_of(int id)
59{
60	if (option_project != PROJ_KERNEL)
61		return;
62
63	my_id = id;
64	add_macro_assign_hook("container_of", &match_non_null, NULL);
65	add_macro_assign_hook("list_first_entry", &match_non_null, NULL);
66	add_function_assign_hook("nla_data", &match_non_null, NULL);
67	add_modification_hook(my_id, &is_ok);
68	add_hook(&match_condition, CONDITION_HOOK);
69}
70