revert part of f4a459292e339
[smatch.git] / check_deref_check.c
blobfb9a8b1ac2c570ec1adb1d7c7439dacea4135615
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);
16 STATE(oktocheck);
18 static void underef(const char *name, struct symbol *sym, struct expression *expr, void *unused)
20 set_state(my_id, name, sym, &oktocheck);
23 static void match_dereference(struct expression *expr)
25 char *name;
27 if (expr->type == EXPR_PREOP) {
28 expr = strip_expr(expr->unop);
29 } else {
30 if (expr->deref->op != '*')
31 return;
32 expr = expr->deref->unop;
33 expr = strip_expr(expr);
35 if (implied_not_equal(expr, 0))
36 return;
38 set_state_expr(my_id, expr, &derefed);
40 name = get_variable_from_expr(expr, NULL);
41 if (!name)
42 return;
43 add_modification_hook(name, &underef, NULL);
44 free_string(name);
48 static void match_condition(struct expression *expr)
50 if (get_state_expr(my_id, expr) == &derefed) {
51 char *name;
53 name = get_variable_from_expr(expr, NULL);
54 if (!implied_not_equal(expr, 0))
55 sm_msg("warn: variable derefenced before check '%s'", name);
56 set_state_expr(my_id, expr, &oktocheck);
57 free_string(name);
62 void check_deref_check(int id)
64 my_id = id;
65 set_default_state(my_id, &oktocheck);
66 add_hook(&match_dereference, DEREF_HOOK);
67 add_hook(&match_condition, CONDITION_HOOK);