unreachable: silence "not actually initialized" false positives
[smatch.git] / check_deref_check.c
blobd2cd1c04e341d89f87523d78ad7fed3dd1fec331
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
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.
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.
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
18 #include "smatch.h"
19 #include "smatch_extra.h"
21 static int my_id;
23 STATE(derefed);
25 static void underef(struct sm_state *sm, struct expression *mod_expr)
27 set_state(my_id, sm->name, sm->sym, &undefined);
30 static void match_dereference(struct expression *expr)
32 if (expr->type != EXPR_PREOP)
33 return;
34 if (getting_address())
35 return;
36 expr = strip_expr(expr->unop);
37 if (implied_not_equal(expr, 0))
38 return;
40 set_state_expr(my_id, expr, &derefed);
43 static void set_param_dereferenced(struct expression *arg, char *unused)
45 if (implied_not_equal(arg, 0))
46 return;
47 set_state_expr(my_id, arg, &derefed);
50 static void match_condition(struct expression *expr)
52 struct sm_state *sm;
54 if (__in_pre_condition)
55 return;
57 if (get_macro_name(expr->pos))
58 return;
60 sm = get_sm_state_expr(my_id, expr);
61 if (!sm || sm->state != &derefed)
62 return;
64 sm_msg("warn: variable dereferenced before check '%s' (see line %d)", sm->name, sm->line);
65 set_state_expr(my_id, expr, &undefined);
68 void check_deref_check(int id)
70 my_id = id;
71 add_hook(&match_dereference, DEREF_HOOK);
72 add_hook(&match_condition, CONDITION_HOOK);
73 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
74 add_modification_hook(my_id, &underef);