Use the most recent implications.
[smatch.git] / smatch_flow.c
blob1edaf210c54b932c680c6ce24eaa12ada2040517
1 /*
2 * sparse/smatch_flow.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdio.h>
11 #include "token.h"
12 #include "smatch.h"
14 int __smatch_lineno = 0;
16 static char *filename;
17 static char *cur_func;
18 static int line_func_start;
20 char *get_filename() { return filename; }
21 char *get_function() { return cur_func; }
22 int get_lineno() { return __smatch_lineno; }
23 int get_func_pos() { return __smatch_lineno - line_func_start; }
25 static void split_symlist(struct symbol_list *sym_list);
26 static void split_expr_list(struct expression_list *expr_list);
28 unsigned int __get_allocations();
30 int option_assume_loops = 0;
32 void __split_expr(struct expression *expr)
34 if (!expr)
35 return;
37 // printf("%d Debug expr_type %d\n", get_lineno(), expr->type);
39 __smatch_lineno = expr->pos.line;
40 __pass_to_client(expr, EXPR_HOOK);
42 switch (expr->type) {
43 case EXPR_PREOP:
44 case EXPR_POSTOP:
45 __pass_to_client(expr, OP_HOOK);
46 __split_expr(expr->unop);
47 return;
48 case EXPR_STATEMENT:
49 __split_statements(expr->statement);
50 return;
51 case EXPR_LOGICAL:
52 __split_whole_condition(expr);
53 __push_true_states();
54 __use_false_states();
55 __merge_true_states();
56 __pop_false_only_stack();
57 return;
59 return;
60 case EXPR_BINOP:
61 case EXPR_COMMA:
62 case EXPR_COMPARE:
63 case EXPR_ASSIGNMENT:
64 if (expr->type == EXPR_ASSIGNMENT)
65 __pass_to_client(expr, ASSIGNMENT_HOOK);
66 __split_expr(expr->left);
67 __split_expr(expr->right);
68 if (expr->type == EXPR_ASSIGNMENT)
69 __pass_to_client(expr, ASSIGNMENT_AFTER_HOOK);
70 return;
71 case EXPR_DEREF:
72 __pass_to_client(expr, DEREF_HOOK);
73 __split_expr(expr->deref);
74 return;
75 case EXPR_SLICE:
76 __split_expr(expr->base);
77 return;
78 case EXPR_CAST:
79 __split_expr(expr->cast_expression);
80 return;
81 case EXPR_SIZEOF:
82 /* there isn't anything to pass a client from inside a sizeof() */
83 return;
84 case EXPR_CONDITIONAL:
85 case EXPR_SELECT:
86 __split_whole_condition(expr->conditional);
87 __split_expr(expr->cond_true);
88 __push_true_states();
89 __use_false_states();
90 __split_expr(expr->cond_false);
91 __merge_true_states();
92 __pop_false_only_stack();
93 return;
94 case EXPR_CALL:
95 __pass_to_client(expr, FUNCTION_CALL_HOOK);
96 __split_expr(expr->fn);
97 split_expr_list(expr->args);
98 __pass_to_client(expr, FUNCTION_CALL_AFTER_HOOK);
99 #ifdef KERNEL
100 if (expr->fn->type == EXPR_SYMBOL &&
101 !strcmp(expr->fn->symbol_name->name, "panic"))
102 nullify_path();
103 #endif
104 return;
105 case EXPR_INITIALIZER:
106 split_expr_list(expr->expr_list);
107 return;
108 case EXPR_IDENTIFIER:
109 __split_expr(expr->ident_expression);
110 return;
111 case EXPR_INDEX:
112 __split_expr(expr->idx_expression);
113 return;
114 case EXPR_POS:
115 __split_expr(expr->init_expr);
116 return;
117 default:
118 return;
122 static int is_forever_loop(struct statement *stmt)
125 struct expression *expr;
127 expr = strip_expr(stmt->iterator_pre_condition);
128 if (!expr)
129 expr = stmt->iterator_post_condition;
130 if (!expr) {
131 /* this is a for(;;) loop... */
132 return 1;
135 if (expr->type == EXPR_VALUE && expr->value == 1) {
136 return 1;
139 return 0;
143 * Pre Loops are while and for loops.
146 static void handle_pre_loop(struct statement *stmt)
148 int once_through; /* we go through the loop at least once */
150 __split_statements(stmt->iterator_pre_statement);
152 once_through = known_condition_true(stmt->iterator_pre_condition);
153 if (option_assume_loops)
154 once_through = 1;
156 __push_continues();
157 __push_breaks();
159 __split_whole_condition(stmt->iterator_pre_condition);
161 __split_statements(stmt->iterator_statement);
162 if (is_forever_loop(stmt)) {
163 __pop_false_only_stack();
164 /* forever loops don't have an iterator_post_statement */
165 __pop_continues();
166 __pop_false_states();
167 __use_breaks();
168 } else if (once_through) {
169 __merge_continues();
170 __split_statements(stmt->iterator_post_statement);
171 __pop_false_states();
172 __use_false_only_stack();
173 __merge_breaks();
174 } else {
175 __merge_continues();
176 __split_statements(stmt->iterator_post_statement);
177 __merge_false_states();
178 __use_false_only_stack();
179 __merge_breaks();
184 * Post loops are do {} while();
186 static void handle_post_loop(struct statement *stmt)
188 __push_continues();
189 __push_breaks();
190 __split_statements(stmt->iterator_statement);
191 if (is_forever_loop(stmt)) {
192 __pop_continues();
193 __use_breaks();
195 } else {
196 __merge_continues();
197 __split_whole_condition(stmt->iterator_post_condition);
198 __use_false_states();
199 __merge_breaks();
201 __pop_false_only_stack();
204 static void print_unreached(struct statement *stmt)
208 * GCC insists on a return statement even where it is never
209 * reached. Also BUG() sometimes is a forever loop and
210 * sometimes not so people put code after a BUG(). There
211 * are way to many false positives.
213 #ifdef KERNEL
214 return;
215 #endif
216 if (__path_is_null()) {
217 switch(stmt->type) {
218 case STMT_COMPOUND: /* after a switch before a case stmt */
219 case STMT_CASE:
220 case STMT_LABEL:
221 case STMT_DECLARATION: /* switch(x) { int a; case foo: ... */
222 break;
223 default:
224 smatch_msg("unreachable code. %d", stmt->type);
229 void __split_statements(struct statement *stmt)
231 if (!stmt)
232 return;
234 if (__get_allocations() > MAXSMSTATES) {
235 static char *printed = NULL;
237 if (printed != cur_func)
238 smatch_msg("Function too big. Giving up.");
239 printed = cur_func;
240 return;
243 __smatch_lineno = stmt->pos.line;
244 print_unreached(stmt);
245 __pass_to_client(stmt, STMT_HOOK);
247 switch (stmt->type) {
248 case STMT_DECLARATION:
249 __pass_declarations_to_client(stmt->declaration);
250 split_symlist(stmt->declaration);
251 return;
252 case STMT_RETURN:
253 __pass_to_client(stmt, RETURN_HOOK);
254 __split_expr(stmt->ret_value);
255 nullify_path();
256 return;
257 case STMT_EXPRESSION:
258 __split_expr(stmt->expression);
259 return;
260 case STMT_COMPOUND: {
261 struct statement *s;
262 FOR_EACH_PTR(stmt->stmts, s) {
263 __split_statements(s);
264 } END_FOR_EACH_PTR(s);
265 return;
267 case STMT_IF:
268 __split_whole_condition(stmt->if_conditional);
269 __split_statements(stmt->if_true);
270 __push_true_states();
271 __use_false_states();
272 __split_statements(stmt->if_false);
273 __merge_true_states();
274 __pop_false_only_stack();
275 return;
276 case STMT_ITERATOR:
277 if (stmt->iterator_pre_condition)
278 handle_pre_loop(stmt);
279 else if (stmt->iterator_post_condition)
280 handle_post_loop(stmt);
281 else {
282 // these are for(;;) type loops.
283 handle_pre_loop(stmt);
285 return;
286 case STMT_SWITCH:
287 __split_expr(stmt->switch_expression);
288 __save_switch_states();
289 __push_default();
290 __push_breaks();
291 __split_statements(stmt->switch_statement);
292 if (!__pop_default())
293 __merge_switches();
294 __pop_switches();
295 __merge_breaks();
296 return;
297 case STMT_CASE:
298 __merge_switches();
299 if (!stmt->case_expression)
300 __set_default();
301 __split_expr(stmt->case_expression);
302 __split_expr(stmt->case_to);
303 __split_statements(stmt->case_statement);
304 return;
305 case STMT_LABEL:
306 if (stmt->label &&
307 stmt->label->type == SYM_LABEL &&
308 stmt->label->ident) {
309 __merge_gotos(stmt->label->ident->name);
311 __split_statements(stmt->label_statement);
312 return;
313 case STMT_GOTO:
314 __split_expr(stmt->goto_expression);
315 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
316 if (!strcmp(stmt->goto_label->ident->name, "break")) {
317 __process_breaks();
318 } else if (!strcmp(stmt->goto_label->ident->name,
319 "continue")) {
320 __process_continues();
322 } else if (stmt->goto_label &&
323 stmt->goto_label->type == SYM_LABEL &&
324 stmt->goto_label->ident) {
325 __save_gotos(stmt->goto_label->ident->name);
327 nullify_path();
328 return;
329 case STMT_NONE:
330 return;
331 case STMT_ASM:
332 __split_expr(stmt->asm_string);
333 //__split_expr(stmt->asm_outputs);
334 //__split_expr(stmt->asm_inputs);
335 //__split_expr(stmt->asm_clobbers);
336 return;
337 case STMT_CONTEXT:
338 return;
339 case STMT_RANGE:
340 __split_expr(stmt->range_expression);
341 __split_expr(stmt->range_low);
342 __split_expr(stmt->range_high);
343 return;
347 static void split_expr_list(struct expression_list *expr_list)
349 struct expression *expr;
350 FOR_EACH_PTR(expr_list, expr) {
351 __split_expr(expr);
352 } END_FOR_EACH_PTR(expr);
356 static void split_sym(struct symbol *sym)
358 if (!sym)
359 return;
360 if (!(sym->namespace & NS_SYMBOL))
361 return;
363 __pass_to_client(sym, SYM_HOOK);
364 __split_statements(sym->stmt);
365 __split_expr(sym->array_size);
366 split_symlist(sym->arguments);
367 split_symlist(sym->symbol_list);
368 __split_statements(sym->inline_stmt);
369 split_symlist(sym->inline_symbol_list);
370 __split_expr(sym->initializer);
373 static void split_symlist(struct symbol_list *sym_list)
375 struct symbol *sym;
377 FOR_EACH_PTR(sym_list, sym) {
378 split_sym(sym);
379 } END_FOR_EACH_PTR(sym);
382 static void split_functions(struct symbol_list *sym_list)
384 struct symbol *sym;
386 FOR_EACH_PTR(sym_list, sym) {
387 struct symbol *base_type;
388 base_type = get_base_type(sym);
389 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
390 if (base_type->stmt)
391 line_func_start = base_type->stmt->pos.line;
392 if (sym->ident)
393 cur_func = sym->ident->name;
394 __smatch_lineno = sym->pos.line;
395 SM_DEBUG("new function: %s\n", cur_func);
396 __unnullify_path();
397 __pass_to_client(sym, FUNC_DEF_HOOK);
398 __split_statements(base_type->stmt);
399 __pass_to_client(sym, END_FUNC_HOOK);
400 cur_func = NULL;
401 line_func_start = 0;
402 clear_all_states();
404 } else {
405 __pass_to_client(sym, BASE_HOOK);
407 } END_FOR_EACH_PTR(sym);
408 __pass_to_client_no_data(END_FILE_HOOK);
411 void smatch (int argc, char **argv)
414 struct string_list *filelist = NULL;
415 struct symbol_list *sym_list;
417 if (argc < 2) {
418 printf("Usage: smatch [--debug] <filename.c>\n");
419 exit(1);
421 sparse_initialize(argc, argv, &filelist);
422 FOR_EACH_PTR_NOTAG(filelist, filename) {
423 sym_list = __sparse(filename);
424 split_functions(sym_list);
425 } END_FOR_EACH_PTR_NOTAG(filename);