struct_assignment: add a comment explaining what the file does
[smatch.git] / check_deref_check.c
blobfa4102da8616709421b942fcbd39df91236bdf62
1 /*
2 * sparse/check_deref_check.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
11 #include "smatch_extra.h"
13 static int my_id;
15 STATE(derefed);
17 static void underef(struct sm_state *sm, struct expression *mod_expr)
19 set_state(my_id, sm->name, sm->sym, &undefined);
22 static void match_dereference(struct expression *expr)
24 if (expr->type != EXPR_PREOP)
25 return;
26 if (getting_address())
27 return;
28 expr = strip_expr(expr->unop);
29 if (implied_not_equal(expr, 0))
30 return;
32 set_state_expr(my_id, expr, &derefed);
35 static void set_param_dereferenced(struct expression *arg, char *unused)
37 if (implied_not_equal(arg, 0))
38 return;
39 set_state_expr(my_id, arg, &derefed);
42 static void match_condition(struct expression *expr)
44 struct sm_state *sm;
46 if (__in_pre_condition)
47 return;
49 if (get_macro_name(expr->pos))
50 return;
52 sm = get_sm_state_expr(my_id, expr);
53 if (!sm || sm->state != &derefed)
54 return;
56 sm_msg("warn: variable dereferenced before check '%s' (see line %d)", sm->name, sm->line);
57 set_state_expr(my_id, expr, &undefined);
60 void check_deref_check(int id)
62 my_id = id;
63 add_hook(&match_dereference, DEREF_HOOK);
64 add_hook(&match_condition, CONDITION_HOOK);
65 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
66 add_modification_hook(my_id, &underef);