Add a check for unused assignments.
[smatch.git] / check_deref_check.c
blob2e8866fa6e5bd934a73d56b744ed16a0aafb73d5
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 extern struct expression_list *big_expression_stack;
15 static int my_id;
17 STATE(derefed);
18 STATE(oktocheck);
20 static void underef(const char *name, struct symbol *sym, struct expression *expr, void *unused)
22 set_state(my_id, name, sym, &oktocheck);
25 static int is_not_really_dereference()
27 struct expression *tmp;
28 int i = 0;
29 int dot_ops = 0;
31 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
32 if (!i++)
33 continue;
34 if (tmp->op == '(')
35 continue;
36 if (tmp->op == '.' && !dot_ops++)
37 continue;
38 if (tmp->op == '&')
39 return 1;
40 return 0;
41 } END_FOR_EACH_PTR_REVERSE(tmp);
42 return 0;
45 static void match_dereference(struct expression *expr)
47 char *name;
49 if (expr->type != EXPR_PREOP)
50 return;
51 if (is_not_really_dereference())
52 return;
54 expr = strip_expr(expr->unop);
55 if (implied_not_equal(expr, 0))
56 return;
57 set_state_expr(my_id, expr, &derefed);
58 name = get_variable_from_expr(expr, NULL);
59 if (!name)
60 return;
61 add_modification_hook(name, &underef, NULL);
62 free_string(name);
65 static void match_condition(struct expression *expr)
67 if (get_state_expr(my_id, expr) == &derefed) {
68 char *name;
70 name = get_variable_from_expr(expr, NULL);
71 if (!implied_not_equal(expr, 0))
72 sm_msg("warn: variable dereferenced before check '%s'", name);
73 set_state_expr(my_id, expr, &oktocheck);
74 free_string(name);
78 void check_deref_check(int id)
80 my_id = id;
81 set_default_state(my_id, &oktocheck);
82 add_hook(&match_dereference, DEREF_HOOK);
83 add_hook(&match_condition, CONDITION_HOOK);