smatch_extra: I don't like empty ranges
[smatch.git] / check_unused_ret.c
blobd8c5bdb3c8e68b403310970cb05c5f11c4b1bf8d
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 #define _GNU_SOURCE
37 #include <search.h>
38 #include "smatch.h"
39 #include "smatch_slist.h"
41 static int my_id;
43 struct assignment {
44 int assign_id;
45 char *name;
46 int line;
48 ALLOCATOR(assignment, "assignment id");
49 DECLARE_PTR_LIST(assignment_list, struct assignment);
50 static struct assignment_list *assignment_list;
52 static struct expression *skip_this;
53 static int assign_id;
55 static struct hsearch_data ignored_funcs;
56 static const char *kernel_ignored[] = {
57 "inb",
58 "inl",
59 "inw",
62 static int ignored_function(struct expression *func)
64 ENTRY e, *ep;
66 func = strip_expr(func);
67 if (!func || func->type != EXPR_CALL)
68 return 0;
69 func = func->fn;
70 if (!func || func->type != EXPR_SYMBOL)
71 return 0;
73 e.key = func->symbol_name->name;
74 hsearch_r(e, FIND, &ep, &ignored_funcs);
75 if (!ep)
76 return 0;
77 return 1;
80 static void match_assign_call(struct expression *expr)
82 struct expression *left;
83 struct assignment *assign;
85 if (final_pass)
86 return;
87 if (in_condition())
88 return;
89 if (expr->op != '=')
90 return;
91 if (unreachable())
92 return;
93 if (ignored_function(expr->right))
94 return;
95 left = strip_expr(expr->left);
96 if (!left || left->type != EXPR_SYMBOL)
97 return;
98 if (left->symbol->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC))
99 return;
101 skip_this = left;
103 set_state_expr(my_id, left, alloc_state_num(assign_id));
105 assign = __alloc_assignment(0);
106 assign->assign_id = assign_id++;
107 assign->name = get_variable_from_expr(left, NULL);
108 assign->line = get_lineno();
109 add_ptr_list(&assignment_list, assign);
112 static void match_assign(struct expression *expr)
114 struct expression *left;
116 if (expr->op != '=')
117 return;
118 left = strip_expr(expr->left);
119 if (!left || left->type != EXPR_SYMBOL)
120 return;
121 delete_state_expr(my_id, left);
124 static void delete_used(int assign_id)
126 struct assignment *tmp;
128 FOR_EACH_PTR(assignment_list, tmp) {
129 if (tmp->assign_id == assign_id) {
130 DELETE_CURRENT_PTR(tmp);
131 return;
133 } END_FOR_EACH_PTR(tmp);
136 static void delete_used_symbols(struct state_list *possible)
138 struct sm_state *tmp;
140 FOR_EACH_PTR(possible, tmp) {
141 delete_used((int)tmp->state->data);
142 } END_FOR_EACH_PTR(tmp);
145 static void match_symbol(struct expression *expr)
147 struct sm_state *sm;
149 expr = strip_expr(expr);
150 if (expr == skip_this)
151 return;
152 sm = get_sm_state_expr(my_id, expr);
153 if (!sm)
154 return;
155 delete_used_symbols(sm->possible);
156 delete_state_expr(my_id, expr);
159 static void match_end_func(struct symbol *sym)
161 struct assignment *tmp;
163 FOR_EACH_PTR(assignment_list, tmp) {
164 sm_printf("%s +%d %s ", get_filename(), tmp->line, get_function());
165 sm_printf("warning: assignment to '%s' was never used\n", tmp->name);
166 } END_FOR_EACH_PTR(tmp);
167 clear_assignment_alloc();
168 __free_ptr_list((struct ptr_list **)&assignment_list);
171 void check_unused_ret(int id)
173 my_id = id;
175 /* It turns out that this test is worthless unless you use --two-passes. */
176 if (!option_two_passes)
177 return;
178 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
179 add_hook(&match_assign, ASSIGNMENT_HOOK);
180 add_hook(&match_symbol, SYM_HOOK);
181 add_hook(&match_end_func, END_FUNC_HOOK);
182 hcreate_r(1000, &ignored_funcs);
183 if (option_project == PROJ_KERNEL) {
184 int i;
186 for (i = 0; i < ARRAY_SIZE(kernel_ignored); i++) {
187 ENTRY e, *ep;
189 e.key = (char *)kernel_ignored[i];
190 e.data = (void *)1;
191 if (!hsearch_r(e, ENTER, &ep, &ignored_funcs)) {
192 printf("check_unused_ret.c broke.\n");
193 exit(1);