handle stuff like the kernel's WARN_ON() macro
[smatch.git] / smatch_flow.c
blobd557e342a461ff749d92a86c4af3ea839a3e8e79
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 #define _GNU_SOURCE 1
11 #include <unistd.h>
12 #include <stdio.h>
13 #include "token.h"
14 #include "smatch.h"
15 #include "smatch_expression_stacks.h"
16 #include "smatch_extra.h"
17 #include "smatch_slist.h" // just for sname.
19 int final_pass;
21 static int __smatch_lineno = 0;
23 static const char *filename;
24 static char *pathname;
25 static char *full_filename;
26 static char *cur_func;
27 static int line_func_start;
28 static struct expression_list *switch_expr_stack = NULL;
29 struct expression_list *big_expression_stack;
30 struct statement_list *big_statement_stack;
31 int __in_pre_condition = 0;
32 int __bail_on_rest_of_function = 0;
33 char *get_function(void) { return cur_func; }
34 int get_lineno(void) { return __smatch_lineno; }
35 int get_func_pos(void) { return __smatch_lineno - line_func_start; }
37 static void split_symlist(struct symbol_list *sym_list);
38 static void split_declaration(struct symbol_list *sym_list);
39 static void split_expr_list(struct expression_list *expr_list);
41 int option_assume_loops = 0;
42 int option_known_conditions = 0;
43 int option_two_passes = 0;
44 struct symbol *cur_func_sym = NULL;
46 const char *get_filename(void)
48 if (option_full_path)
49 return full_filename;
50 return filename;
53 static void set_position(struct expression *expr)
55 int len;
56 static int prev_stream = -1;
58 __smatch_lineno = expr->pos.line;
60 if (expr->pos.stream == prev_stream)
61 return;
63 filename = stream_name(expr->pos.stream);
65 free(full_filename);
66 pathname = getcwd(NULL, 0);
67 if (pathname) {
68 len = strlen(pathname) + 1 + strlen(filename) + 1;
69 full_filename = malloc(len);
70 snprintf(full_filename, len, "%s/%s", pathname, filename);
71 } else {
72 full_filename = alloc_string(filename);
74 free(pathname);
77 void __split_expr(struct expression *expr)
79 if (!expr)
80 return;
82 // printf("%d Debug expr_type %d %s\n", get_lineno(), expr->type, show_special(expr->op));
84 push_expression(&big_expression_stack, expr);
85 set_position(expr);
86 __pass_to_client(expr, EXPR_HOOK);
88 switch (expr->type) {
89 case EXPR_PREOP:
90 if (expr->op == '*')
91 __pass_to_client(expr, DEREF_HOOK);
92 case EXPR_POSTOP:
93 __pass_to_client(expr, OP_HOOK);
94 __split_expr(expr->unop);
95 break;
96 case EXPR_STATEMENT:
97 __split_statements(expr->statement);
98 break;
99 case EXPR_LOGICAL:
100 __split_whole_condition(expr);
101 __push_true_states();
102 __use_false_states();
103 __merge_true_states();
104 break;
105 case EXPR_BINOP:
106 __pass_to_client(expr, BINOP_HOOK);
107 case EXPR_COMMA:
108 case EXPR_COMPARE:
109 __split_expr(expr->left);
110 __split_expr(expr->right);
111 break;
112 case EXPR_ASSIGNMENT: {
113 struct expression *tmp;
115 /* foo = !bar() */
116 if (__handle_condition_assigns(expr))
117 break;
119 __split_expr(expr->right);
120 __pass_to_client(expr, ASSIGNMENT_HOOK);
121 tmp = strip_expr(expr->right);
122 if (tmp->type == EXPR_CALL)
123 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
124 __split_expr(expr->left);
125 break;
127 case EXPR_DEREF:
128 __pass_to_client(expr, DEREF_HOOK);
129 __split_expr(expr->deref);
130 break;
131 case EXPR_SLICE:
132 __split_expr(expr->base);
133 break;
134 case EXPR_CAST:
135 case EXPR_FORCE_CAST:
136 __split_expr(expr->cast_expression);
137 break;
138 case EXPR_SIZEOF:
139 /* there isn't anything to pass a client from inside a sizeof() */
140 break;
141 case EXPR_CONDITIONAL:
142 case EXPR_SELECT:
143 __split_whole_condition(expr->conditional);
144 __split_expr(expr->cond_true);
145 __push_true_states();
146 __use_false_states();
147 __split_expr(expr->cond_false);
148 __merge_true_states();
149 break;
150 case EXPR_CALL:
151 split_expr_list(expr->args);
152 __split_expr(expr->fn);
153 __pass_to_client(expr, FUNCTION_CALL_HOOK);
154 break;
155 case EXPR_INITIALIZER:
156 split_expr_list(expr->expr_list);
157 break;
158 case EXPR_IDENTIFIER:
159 __split_expr(expr->ident_expression);
160 break;
161 case EXPR_INDEX:
162 __split_expr(expr->idx_expression);
163 break;
164 case EXPR_POS:
165 __split_expr(expr->init_expr);
166 break;
167 case EXPR_SYMBOL:
168 __pass_to_client(expr, SYM_HOOK);
169 break;
170 default:
171 break;
173 pop_expression(&big_expression_stack);
176 static int is_forever_loop(struct statement *stmt)
179 struct expression *expr;
181 expr = strip_expr(stmt->iterator_pre_condition);
182 if (!expr)
183 expr = stmt->iterator_post_condition;
184 if (!expr) {
185 /* this is a for(;;) loop... */
186 return 1;
189 if (expr->type == EXPR_VALUE && expr->value == 1) {
190 return 1;
193 return 0;
196 static int loop_num;
197 static char *get_loop_name(int num)
199 char buf[256];
201 snprintf(buf, 255, "-loop%d", num);
202 buf[255] = '\0';
203 return alloc_sname(buf);;
207 * Pre Loops are while and for loops.
210 static void handle_pre_loop(struct statement *stmt)
212 int once_through; /* we go through the loop at least once */
213 struct sm_state *extra_state = NULL;
214 int unchanged = 0;
215 char *loop_name;
217 loop_name = get_loop_name(loop_num);
218 loop_num++;
220 __split_statements(stmt->iterator_pre_statement);
222 once_through = implied_condition_true(stmt->iterator_pre_condition);
224 __push_continues();
225 __push_breaks();
227 __merge_gotos(loop_name);
229 __in_pre_condition++;
230 __split_whole_condition(stmt->iterator_pre_condition);
231 __in_pre_condition--;
233 if (once_through)
234 extra_state = __extra_pre_loop_hook_before(stmt->iterator_pre_statement);
235 if (option_assume_loops)
236 once_through = 1;
237 __split_statements(stmt->iterator_statement);
239 __warn_on_silly_pre_loops();
240 if (is_forever_loop(stmt)) {
241 __save_gotos(loop_name);
242 /* forever loops don't have an iterator_post_statement */
243 __pop_continues();
244 __pop_false_states();
245 __use_breaks();
246 } else if (once_through) {
247 __merge_continues();
248 if (extra_state)
249 unchanged = __iterator_unchanged(extra_state, stmt->iterator_post_statement);
250 __split_statements(stmt->iterator_post_statement);
251 __save_gotos(loop_name);
252 __split_whole_condition(stmt->iterator_pre_condition);
253 nullify_path();
254 __merge_false_states();
255 if (extra_state && unchanged)
256 __extra_pre_loop_hook_after(extra_state,
257 stmt->iterator_post_statement, stmt->iterator_pre_condition);
258 __pop_false_states();
259 __merge_breaks();
260 } else {
261 __merge_continues();
262 __split_statements(stmt->iterator_post_statement);
263 __save_gotos(loop_name);
264 __split_whole_condition(stmt->iterator_pre_condition);
265 nullify_path();
266 __merge_false_states();
267 __merge_false_states();
268 __merge_breaks();
273 * Post loops are do {} while();
275 static void handle_post_loop(struct statement *stmt)
277 char *loop_name;
279 loop_name = get_loop_name(loop_num);
280 loop_num++;
282 __push_continues();
283 __push_breaks();
284 __merge_gotos(loop_name);
285 __split_statements(stmt->iterator_statement);
286 __merge_continues();
287 if (!is_zero(stmt->iterator_post_condition))
288 __save_gotos(loop_name);
290 if (is_forever_loop(stmt)) {
291 __use_breaks();
292 } else {
293 __split_whole_condition(stmt->iterator_post_condition);
294 __use_false_states();
295 __merge_breaks();
299 static struct statement *last_stmt;
300 static int is_last_stmt(struct statement *stmt)
302 if (stmt == last_stmt)
303 return 1;
304 return 0;
307 static void print_unreached_initializers(struct symbol_list *sym_list)
309 struct symbol *sym;
311 FOR_EACH_PTR(sym_list, sym) {
312 if(sym->initializer)
313 sm_msg("info: '%s' is not actually initialized (unreached code).",
314 (sym->ident ? sym->ident->name : "this variable"));
315 } END_FOR_EACH_PTR(sym);
318 static void print_unreached(struct statement *stmt)
321 static int print = 1;
323 if (__path_is_null()) {
324 switch (stmt->type) {
325 case STMT_COMPOUND: /* after a switch before a case stmt */
326 case STMT_CASE:
327 case STMT_LABEL:
328 break;
329 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
330 print_unreached_initializers(stmt->declaration);
331 break;
332 case STMT_RETURN: /* gcc complains if you don't have a return statement */
333 if (is_last_stmt(stmt))
334 break;
335 default:
336 if (print)
337 sm_msg("info: ignoring unreachable code.");
338 print = 0;
340 } else {
341 print = 1;
346 static int empty_statement(struct statement *stmt)
348 if (!stmt)
349 return 0;
350 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
351 return 1;
352 return 0;
355 void __split_statements(struct statement *stmt)
357 if (!stmt)
358 return;
360 if (out_of_memory() || __bail_on_rest_of_function) {
361 static char *printed = NULL;
363 if (printed != cur_func)
364 sm_msg("Function too hairy. Giving up.");
365 printed = cur_func;
366 return;
369 add_ptr_list(&big_statement_stack, stmt);
370 free_expression_stack(&big_expression_stack);
371 __smatch_lineno = stmt->pos.line;
372 print_unreached(stmt);
373 __pass_to_client(stmt, STMT_HOOK);
375 switch (stmt->type) {
376 case STMT_DECLARATION:
377 split_declaration(stmt->declaration);
378 return;
379 case STMT_RETURN:
380 __split_expr(stmt->ret_value);
381 __pass_to_client(stmt->ret_value, RETURN_HOOK);
382 nullify_path();
383 return;
384 case STMT_EXPRESSION:
385 __split_expr(stmt->expression);
386 return;
387 case STMT_COMPOUND: {
388 struct statement *s;
390 if (!last_stmt)
391 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
392 __push_scope_hooks();
393 FOR_EACH_PTR(stmt->stmts, s) {
394 __split_statements(s);
395 } END_FOR_EACH_PTR(s);
396 __call_scope_hooks();
397 return;
399 case STMT_IF:
400 if (known_condition_true(stmt->if_conditional)) {
401 __split_statements(stmt->if_true);
402 return;
404 if (known_condition_false(stmt->if_conditional)) {
405 __split_statements(stmt->if_false);
406 return;
408 if (option_known_conditions &&
409 implied_condition_true(stmt->if_conditional)) {
410 sm_info("this condition is true.");
411 __split_statements(stmt->if_true);
412 return;
414 if (option_known_conditions &&
415 implied_condition_false(stmt->if_conditional)) {
416 sm_info("this condition is false.");
417 __split_statements(stmt->if_false);
418 return;
420 __split_whole_condition(stmt->if_conditional);
421 __split_statements(stmt->if_true);
422 if (empty_statement(stmt->if_true))
423 sm_msg("warn: if();");
424 __push_true_states();
425 __use_false_states();
426 __split_statements(stmt->if_false);
427 __merge_true_states();
428 return;
429 case STMT_ITERATOR:
430 if (stmt->iterator_pre_condition)
431 handle_pre_loop(stmt);
432 else if (stmt->iterator_post_condition)
433 handle_post_loop(stmt);
434 else {
435 // these are for(;;) type loops.
436 handle_pre_loop(stmt);
438 return;
439 case STMT_SWITCH:
440 __split_expr(stmt->switch_expression);
441 push_expression(&switch_expr_stack, stmt->switch_expression);
442 __save_switch_states(top_expression(switch_expr_stack));
443 nullify_path();
444 __push_default();
445 __push_breaks();
446 __split_statements(stmt->switch_statement);
447 if (!__pop_default())
448 __merge_switches(top_expression(switch_expr_stack),
449 NULL);
450 __pop_switches();
451 __merge_breaks();
452 pop_expression(&switch_expr_stack);
453 return;
454 case STMT_CASE:
455 __merge_switches(top_expression(switch_expr_stack),
456 stmt->case_expression);
457 __pass_case_to_client(top_expression(switch_expr_stack),
458 stmt->case_expression);
459 if (!stmt->case_expression)
460 __set_default();
461 __split_expr(stmt->case_expression);
462 __split_expr(stmt->case_to);
463 __split_statements(stmt->case_statement);
464 return;
465 case STMT_LABEL:
466 if (stmt->label &&
467 stmt->label->type == SYM_LABEL &&
468 stmt->label->ident) {
469 __merge_gotos(stmt->label->ident->name);
471 __split_statements(stmt->label_statement);
472 return;
473 case STMT_GOTO:
474 __split_expr(stmt->goto_expression);
475 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
476 if (!strcmp(stmt->goto_label->ident->name, "break")) {
477 __process_breaks();
478 } else if (!strcmp(stmt->goto_label->ident->name,
479 "continue")) {
480 __process_continues();
482 } else if (stmt->goto_label &&
483 stmt->goto_label->type == SYM_LABEL &&
484 stmt->goto_label->ident) {
485 __save_gotos(stmt->goto_label->ident->name);
487 nullify_path();
488 return;
489 case STMT_NONE:
490 return;
491 case STMT_ASM:
492 __split_expr(stmt->asm_string);
493 //__split_expr(stmt->asm_outputs);
494 //__split_expr(stmt->asm_inputs);
495 //__split_expr(stmt->asm_clobbers);
496 return;
497 case STMT_CONTEXT:
498 return;
499 case STMT_RANGE:
500 __split_expr(stmt->range_expression);
501 __split_expr(stmt->range_low);
502 __split_expr(stmt->range_high);
503 return;
507 static void split_expr_list(struct expression_list *expr_list)
509 struct expression *expr;
510 FOR_EACH_PTR(expr_list, expr) {
511 __split_expr(expr);
512 } END_FOR_EACH_PTR(expr);
516 static void split_sym(struct symbol *sym)
518 if (!sym)
519 return;
520 if (!(sym->namespace & NS_SYMBOL))
521 return;
523 __split_statements(sym->stmt);
524 __split_expr(sym->array_size);
525 split_symlist(sym->arguments);
526 split_symlist(sym->symbol_list);
527 __split_statements(sym->inline_stmt);
528 split_symlist(sym->inline_symbol_list);
529 __split_expr(sym->initializer);
532 static void split_symlist(struct symbol_list *sym_list)
534 struct symbol *sym;
536 FOR_EACH_PTR(sym_list, sym) {
537 split_sym(sym);
538 } END_FOR_EACH_PTR(sym);
541 static struct expression *fake_assign_expr(struct symbol *sym)
543 struct expression *e_assign, *e_symbol;
545 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
546 e_symbol = alloc_expression(sym->initializer->pos, EXPR_SYMBOL);
547 e_assign->op = (int)'=';
548 e_symbol->symbol = sym;
549 e_symbol->symbol_name = sym->ident;
550 e_assign->left = e_symbol;
551 e_assign->right = sym->initializer;
552 return e_assign;
555 static void do_initializer_stuff(struct symbol *sym)
557 struct expression *assign;
558 struct expression *tmp;
560 if(!sym->initializer)
561 return;
562 __split_expr(sym->initializer);
563 assign = fake_assign_expr(sym);
564 if (__handle_condition_assigns(assign))
565 return;
566 __pass_to_client(assign, ASSIGNMENT_HOOK);
567 tmp = strip_expr(assign->right);
568 if (tmp->type == EXPR_CALL)
569 __pass_to_client(assign, CALL_ASSIGNMENT_HOOK);
572 static void split_declaration(struct symbol_list *sym_list)
574 struct symbol *sym;
576 FOR_EACH_PTR(sym_list, sym) {
577 __pass_to_client(sym, DECLARATION_HOOK);
578 do_initializer_stuff(sym);
579 split_sym(sym);
580 } END_FOR_EACH_PTR(sym);
583 static void split_functions(struct symbol_list *sym_list)
585 struct symbol *sym;
587 FOR_EACH_PTR(sym_list, sym) {
588 struct symbol *base_type;
589 base_type = get_base_type(sym);
590 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
591 cur_func_sym = sym;
592 if (base_type->stmt)
593 line_func_start = base_type->stmt->pos.line;
594 if (sym->ident)
595 cur_func = sym->ident->name;
596 __smatch_lineno = sym->pos.line;
597 last_stmt = NULL;
598 sm_debug("new function: %s\n", cur_func);
599 if (option_two_passes) {
600 __unnullify_path();
601 loop_num = 0;
602 final_pass = 0;
603 __pass_to_client(sym, FUNC_DEF_HOOK);
604 __split_statements(base_type->stmt);
605 nullify_path();
607 __unnullify_path();
608 loop_num = 0;
609 final_pass = 1;
610 __pass_to_client(sym, FUNC_DEF_HOOK);
611 __split_statements(base_type->stmt);
612 __pass_to_client(sym, END_FUNC_HOOK);
613 cur_func = NULL;
614 line_func_start = 0;
615 clear_all_states();
616 free_data_info_allocs();
617 free_expression_stack(&switch_expr_stack);
618 __free_ptr_list((struct ptr_list **)&big_statement_stack);
619 __bail_on_rest_of_function = 0;
620 } else {
621 __pass_to_client(sym, BASE_HOOK);
623 } END_FOR_EACH_PTR(sym);
624 __pass_to_client_no_data(END_FILE_HOOK);
627 void smatch (int argc, char **argv)
630 struct string_list *filelist = NULL;
631 struct symbol_list *sym_list;
632 char *file;
634 if (argc < 2) {
635 printf("Usage: smatch [--debug] <filename.c>\n");
636 exit(1);
638 sparse_initialize(argc, argv, &filelist);
639 FOR_EACH_PTR_NOTAG(filelist, file) {
640 sym_list = __sparse(file);
641 split_functions(sym_list);
642 } END_FOR_EACH_PTR_NOTAG(file);