fix get_implied_max()
[smatch.git] / check_unused_ret.c
blob099a6d82c8600a84fc061b5ddade5efc9c9ab928
1 /*
2 * sparse/check_unused_ret.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This check is supposed to find places like this:
12 * err = foo();
13 * err = bar();
14 * if (err)
15 * return err;
16 * (the first assignment isn't used)
18 * How the check works is that every assignment gets an ID.
19 * We store that assignment ID in a list of assignments that
20 * haven't been used. We also set the state of 'err' from
21 * the example above to be. Then when we use 'err' we remove
22 * it from the list. At the end of the function we print
23 * a list of assignments that still haven't been used.
25 * Note that this check only works for assignments to
26 * EXPR_SYMBOL. Maybe it could be modified to cover other
27 * assignments later but then you would have to deal with
28 * scope issues.
30 * Also this state is quite tied to the order the callbacks
31 * are called in smatch_flow.c. (If the order changed it
32 * would break).
36 #include "smatch.h"
37 #include "smatch_slist.h"
38 #include "smatch_function_hashtable.h"
40 static int my_id;
42 struct assignment {
43 int assign_id;
44 char *name;
45 int line;
47 ALLOCATOR(assignment, "assignment id");
48 DECLARE_PTR_LIST(assignment_list, struct assignment);
49 static struct assignment_list *assignment_list;
51 static struct expression *skip_this;
52 static int assign_id;
54 static DEFINE_HASHTABLE_INSERT(insert_func, char, int);
55 static DEFINE_HASHTABLE_SEARCH(search_func, char, int);
56 static struct hashtable *ignored_funcs;
58 static const char *kernel_ignored[] = {
59 "inb",
60 "inl",
61 "inw",
64 static int ignored_function(struct expression *func)
66 return !!search_func(ignored_funcs, (char *)func);
69 static void match_assign_call(struct expression *expr)
71 struct expression *left;
72 struct assignment *assign;
74 if (final_pass)
75 return;
76 if (in_condition())
77 return;
78 if (expr->op != '=')
79 return;
80 if (unreachable())
81 return;
82 if (ignored_function(expr->right))
83 return;
84 left = strip_expr(expr->left);
85 if (!left || left->type != EXPR_SYMBOL)
86 return;
87 if (left->symbol->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC))
88 return;
90 skip_this = left;
92 set_state_expr(my_id, left, alloc_state_num(assign_id));
94 assign = __alloc_assignment(0);
95 assign->assign_id = assign_id++;
96 assign->name = get_variable_from_expr(left, NULL);
97 assign->line = get_lineno();
98 add_ptr_list(&assignment_list, assign);
101 static void match_assign(struct expression *expr)
103 struct expression *left;
105 if (expr->op != '=')
106 return;
107 left = strip_expr(expr->left);
108 if (!left || left->type != EXPR_SYMBOL)
109 return;
110 delete_state_expr(my_id, left);
113 static void delete_used(int assign_id)
115 struct assignment *tmp;
117 FOR_EACH_PTR(assignment_list, tmp) {
118 if (tmp->assign_id == assign_id) {
119 DELETE_CURRENT_PTR(tmp);
120 return;
122 } END_FOR_EACH_PTR(tmp);
125 static void delete_used_symbols(struct state_list *possible)
127 struct sm_state *tmp;
129 FOR_EACH_PTR(possible, tmp) {
130 delete_used((int)tmp->state->data);
131 } END_FOR_EACH_PTR(tmp);
134 static void match_symbol(struct expression *expr)
136 struct sm_state *sm;
138 expr = strip_expr(expr);
139 if (expr == skip_this)
140 return;
141 sm = get_sm_state_expr(my_id, expr);
142 if (!sm)
143 return;
144 delete_used_symbols(sm->possible);
145 delete_state_expr(my_id, expr);
148 static void match_end_func(struct symbol *sym)
150 struct assignment *tmp;
152 FOR_EACH_PTR(assignment_list, tmp) {
153 sm_printf("%s +%d %s ", get_filename(), tmp->line, get_function());
154 sm_printf("warn: assignment to '%s' was never used\n", tmp->name);
155 } END_FOR_EACH_PTR(tmp);
156 clear_assignment_alloc();
157 __free_ptr_list((struct ptr_list **)&assignment_list);
160 void check_unused_ret(int id)
162 my_id = id;
164 /* It turns out that this test is worthless unless you use --two-passes. */
165 if (!option_two_passes)
166 return;
167 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
168 add_hook(&match_assign, ASSIGNMENT_HOOK);
169 add_hook(&match_symbol, SYM_HOOK);
170 add_hook(&match_end_func, END_FUNC_HOOK);
171 ignored_funcs = create_function_hashtable(100);
172 if (option_project == PROJ_KERNEL) {
173 int i;
175 for (i = 0; i < ARRAY_SIZE(kernel_ignored); i++)
176 insert_func(ignored_funcs, (char *)kernel_ignored[i], (int *)1);