new: smatch_scripts/test_kernel.sh
[smatch.git] / smatch_flow.c
blob761f5da32193c18061f19fef52bc6bca2c2a1c85
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"
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 int loop_count;
29 int __expr_stmt_count;
30 static struct expression_list *switch_expr_stack = NULL;
32 struct expression_list *big_expression_stack;
33 struct statement_list *big_statement_stack;
34 int __in_pre_condition = 0;
35 int __bail_on_rest_of_function = 0;
36 char *get_function(void) { return cur_func; }
37 int get_lineno(void) { return __smatch_lineno; }
38 int inside_loop(void) { return !!loop_count; }
39 int in_expression_statement(void) { return !!__expr_stmt_count; }
41 static void split_symlist(struct symbol_list *sym_list);
42 static void split_declaration(struct symbol_list *sym_list);
43 static void split_expr_list(struct expression_list *expr_list);
44 static void add_inline_function(struct symbol *sym);
46 int option_assume_loops = 0;
47 int option_known_conditions = 0;
48 int option_two_passes = 0;
49 struct symbol *cur_func_sym = NULL;
51 const char *get_filename(void)
53 if (option_full_path)
54 return full_filename;
55 return filename;
58 static void set_position(struct position pos)
60 int len;
61 static int prev_stream = -1;
63 __smatch_lineno = pos.line;
65 if (pos.stream == prev_stream)
66 return;
68 filename = stream_name(pos.stream);
70 free(full_filename);
71 pathname = getcwd(NULL, 0);
72 if (pathname) {
73 len = strlen(pathname) + 1 + strlen(filename) + 1;
74 full_filename = malloc(len);
75 snprintf(full_filename, len, "%s/%s", pathname, filename);
76 } else {
77 full_filename = alloc_string(filename);
79 free(pathname);
82 static int is_inline_func(struct expression *expr)
84 if (expr->type != EXPR_SYMBOL || !expr->symbol)
85 return 0;
86 if (expr->symbol->ctype.modifiers & MOD_INLINE)
87 return 1;
88 return 0;
91 void __split_expr(struct expression *expr)
93 if (!expr)
94 return;
96 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
98 push_expression(&big_expression_stack, expr);
99 set_position(expr->pos);
100 __pass_to_client(expr, EXPR_HOOK);
102 switch (expr->type) {
103 case EXPR_PREOP:
104 if (expr->op == '*')
105 __pass_to_client(expr, DEREF_HOOK);
106 case EXPR_POSTOP:
107 __pass_to_client(expr, OP_HOOK);
108 __split_expr(expr->unop);
109 break;
110 case EXPR_STATEMENT:
111 __expr_stmt_count++;
112 __split_stmt(expr->statement);
113 __expr_stmt_count--;
114 break;
115 case EXPR_LOGICAL:
116 case EXPR_COMPARE:
117 __pass_to_client(expr, LOGIC_HOOK);
118 __handle_logic(expr);
119 break;
120 case EXPR_BINOP:
121 __pass_to_client(expr, BINOP_HOOK);
122 case EXPR_COMMA:
123 __split_expr(expr->left);
124 __split_expr(expr->right);
125 break;
126 case EXPR_ASSIGNMENT: {
127 struct expression *tmp;
129 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
131 /* foo = !bar() */
132 if (__handle_condition_assigns(expr))
133 break;
134 /* foo = (x < 5 ? foo : 5); */
135 if (__handle_select_assigns(expr))
136 break;
137 /* foo = ({frob(); frob(); frob(); 1;}) */
138 if (__handle_expr_statement_assigns(expr))
139 break;
141 __split_expr(expr->right);
142 __pass_to_client(expr, ASSIGNMENT_HOOK);
143 tmp = strip_expr(expr->right);
144 if (tmp->type == EXPR_CALL)
145 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
146 if (get_macro_name(&tmp->pos))
147 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
148 __split_expr(expr->left);
149 break;
151 case EXPR_DEREF:
152 __pass_to_client(expr, DEREF_HOOK);
153 __split_expr(expr->deref);
154 break;
155 case EXPR_SLICE:
156 __split_expr(expr->base);
157 break;
158 case EXPR_CAST:
159 case EXPR_FORCE_CAST:
160 __split_expr(expr->cast_expression);
161 break;
162 case EXPR_SIZEOF:
163 /* there isn't anything to pass a client from inside a sizeof() */
164 break;
165 case EXPR_CONDITIONAL:
166 case EXPR_SELECT:
167 __pass_to_client(expr, SELECT_HOOK);
168 __split_whole_condition(expr->conditional);
169 __split_expr(expr->cond_true);
170 __push_true_states();
171 __use_false_states();
172 __split_expr(expr->cond_false);
173 __merge_true_states();
174 break;
175 case EXPR_CALL:
176 split_expr_list(expr->args);
177 __split_expr(expr->fn);
178 if (is_inline_func(expr->fn))
179 add_inline_function(expr->fn->symbol);
180 __pass_to_client(expr, FUNCTION_CALL_HOOK);
181 break;
182 case EXPR_INITIALIZER:
183 split_expr_list(expr->expr_list);
184 break;
185 case EXPR_IDENTIFIER:
186 __split_expr(expr->ident_expression);
187 break;
188 case EXPR_INDEX:
189 __split_expr(expr->idx_expression);
190 break;
191 case EXPR_POS:
192 __split_expr(expr->init_expr);
193 break;
194 case EXPR_SYMBOL:
195 __pass_to_client(expr, SYM_HOOK);
196 break;
197 case EXPR_STRING:
198 __pass_to_client(expr, STRING_HOOK);
199 break;
200 default:
201 break;
203 pop_expression(&big_expression_stack);
206 static int is_forever_loop(struct statement *stmt)
208 struct expression *expr;
210 expr = strip_expr(stmt->iterator_pre_condition);
211 if (!expr)
212 expr = stmt->iterator_post_condition;
213 if (!expr) {
214 /* this is a for(;;) loop... */
215 return 1;
218 if (expr->type == EXPR_VALUE && expr->value == 1)
219 return 1;
221 return 0;
224 static int loop_num;
225 static char *get_loop_name(int num)
227 char buf[256];
229 snprintf(buf, 255, "-loop%d", num);
230 buf[255] = '\0';
231 return alloc_sname(buf);
235 * Pre Loops are while and for loops.
237 static void handle_pre_loop(struct statement *stmt)
239 int once_through; /* we go through the loop at least once */
240 struct sm_state *extra_sm = NULL;
241 int unchanged = 0;
242 char *loop_name;
243 struct state_list *slist = NULL;
244 struct sm_state *sm = NULL;
246 loop_name = get_loop_name(loop_num);
247 loop_num++;
249 __split_stmt(stmt->iterator_pre_statement);
251 once_through = implied_condition_true(stmt->iterator_pre_condition);
253 loop_count++;
254 __push_continues();
255 __push_breaks();
257 __merge_gotos(loop_name);
259 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
260 __in_pre_condition++;
261 __pass_to_client(stmt, PRELOOP_HOOK);
262 __split_whole_condition(stmt->iterator_pre_condition);
263 __in_pre_condition--;
264 FOR_EACH_PTR(slist, sm) {
265 set_state(sm->owner, sm->name, sm->sym, sm->state);
266 } END_FOR_EACH_PTR(sm);
267 free_slist(&slist);
268 if (extra_sm)
269 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
271 if (option_assume_loops)
272 once_through = 1;
274 __split_stmt(stmt->iterator_statement);
275 __warn_on_silly_pre_loops();
276 if (is_forever_loop(stmt)) {
277 __save_gotos(loop_name);
278 /* forever loops don't have an iterator_post_statement */
279 __discard_continues();
280 __discard_false_states();
281 __use_breaks();
282 } else {
283 __merge_continues();
284 unchanged = __iterator_unchanged(extra_sm);
285 __split_stmt(stmt->iterator_post_statement);
286 __save_gotos(loop_name);
287 __split_whole_condition(stmt->iterator_pre_condition);
288 nullify_path();
289 __merge_false_states();
290 if (once_through)
291 __discard_false_states();
292 else
293 __merge_false_states();
295 if (extra_sm && unchanged)
296 __extra_pre_loop_hook_after(extra_sm,
297 stmt->iterator_post_statement,
298 stmt->iterator_pre_condition);
299 __merge_breaks();
301 loop_count--;
305 * Post loops are do {} while();
307 static void handle_post_loop(struct statement *stmt)
309 char *loop_name;
311 loop_name = get_loop_name(loop_num);
312 loop_num++;
313 loop_count++;
315 __push_continues();
316 __push_breaks();
317 __merge_gotos(loop_name);
318 __split_stmt(stmt->iterator_statement);
319 __merge_continues();
320 if (!is_zero(stmt->iterator_post_condition))
321 __save_gotos(loop_name);
323 if (is_forever_loop(stmt)) {
324 __use_breaks();
325 } else {
326 __split_whole_condition(stmt->iterator_post_condition);
327 __use_false_states();
328 __merge_breaks();
330 loop_count--;
333 static int empty_statement(struct statement *stmt)
335 if (!stmt)
336 return 0;
337 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
338 return 1;
339 return 0;
342 static int last_stmt_on_same_line()
344 struct statement *stmt;
345 int i = 0;
347 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
348 if (!i++)
349 continue;
350 if (stmt->pos.line == get_lineno())
351 return 1;
352 return 0;
353 } END_FOR_EACH_PTR_REVERSE(stmt);
354 return 0;
357 static struct statement *last_stmt;
358 static int is_last_stmt(struct statement *stmt)
360 if (stmt == last_stmt)
361 return 1;
362 return 0;
365 static void print_unreached_initializers(struct symbol_list *sym_list)
367 struct symbol *sym;
369 FOR_EACH_PTR(sym_list, sym) {
370 if (sym->initializer)
371 sm_msg("info: '%s' is not actually initialized (unreached code).",
372 (sym->ident ? sym->ident->name : "this variable"));
373 } END_FOR_EACH_PTR(sym);
376 static void print_unreached(struct statement *stmt)
378 static int print = 1;
380 if (!__path_is_null()) {
381 print = 1;
382 return;
384 if (!print)
385 return;
387 switch (stmt->type) {
388 case STMT_COMPOUND: /* after a switch before a case stmt */
389 case STMT_RANGE:
390 case STMT_CASE:
391 case STMT_LABEL:
392 return;
393 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
394 print_unreached_initializers(stmt->declaration);
395 return;
396 case STMT_RETURN: /* gcc complains if you don't have a return statement */
397 if (is_last_stmt(stmt))
398 return;
399 break;
400 case STMT_GOTO:
401 if (!option_spammy)
402 return;
403 break;
404 default:
405 break;
407 if (!option_spammy && empty_statement(stmt))
408 return;
409 sm_msg("info: ignoring unreachable code.");
410 print = 0;
413 static void split_asm_constraints(struct expression_list *expr_list)
415 struct expression *expr;
416 int state = 0;
418 FOR_EACH_PTR(expr_list, expr) {
419 switch (state) {
420 case 0: /* identifier */
421 case 1: /* constraint */
422 state++;
423 continue;
424 case 2: /* expression */
425 state = 0;
426 __split_expr(expr);
427 continue;
429 } END_FOR_EACH_PTR(expr);
432 static int is_case_val(struct statement *stmt, long long val)
434 long long case_val;
436 if (stmt->type != STMT_CASE)
437 return 0;
438 if (!stmt->case_expression) {
439 __set_default();
440 return 1;
442 if (!get_value(stmt->case_expression, &case_val))
443 return 0;
444 if (case_val == val)
445 return 1;
446 return 0;
449 static void split_known_switch(struct statement *stmt, long long val)
451 struct statement *tmp;
453 __split_expr(stmt->switch_expression);
455 push_expression(&switch_expr_stack, stmt->switch_expression);
456 __save_switch_states(top_expression(switch_expr_stack));
457 nullify_path();
458 __push_default();
459 __push_breaks();
461 stmt = stmt->switch_statement;
463 if (!last_stmt)
464 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
466 __push_scope_hooks();
467 FOR_EACH_PTR(stmt->stmts, tmp) {
468 __smatch_lineno = tmp->pos.line;
469 if (is_case_val(tmp, val)) {
470 __merge_switches(top_expression(switch_expr_stack),
471 stmt->case_expression);
472 __pass_case_to_client(top_expression(switch_expr_stack),
473 stmt->case_expression);
475 if (__path_is_null())
476 continue;
477 __split_stmt(tmp);
478 if (__path_is_null()) {
479 __set_default();
480 goto out;
482 } END_FOR_EACH_PTR(tmp);
483 out:
484 __call_scope_hooks();
485 if (!__pop_default())
486 __merge_switches(top_expression(switch_expr_stack),
487 NULL);
488 __discard_switches();
489 __merge_breaks();
490 pop_expression(&switch_expr_stack);
493 void __split_stmt(struct statement *stmt)
495 long long val;
497 if (!stmt)
498 return;
500 if (out_of_memory() || __bail_on_rest_of_function) {
501 static char *printed = NULL;
503 if (printed != cur_func)
504 sm_msg("Function too hairy. Giving up.");
505 printed = cur_func;
506 return;
509 add_ptr_list(&big_statement_stack, stmt);
510 free_expression_stack(&big_expression_stack);
511 set_position(stmt->pos);
512 print_unreached(stmt);
513 __pass_to_client(stmt, STMT_HOOK);
515 switch (stmt->type) {
516 case STMT_DECLARATION:
517 split_declaration(stmt->declaration);
518 return;
519 case STMT_RETURN:
520 __split_expr(stmt->ret_value);
521 __pass_to_client(stmt->ret_value, RETURN_HOOK);
522 nullify_path();
523 return;
524 case STMT_EXPRESSION:
525 __split_expr(stmt->expression);
526 return;
527 case STMT_COMPOUND: {
528 struct statement *tmp;
530 if (!last_stmt)
531 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
532 __push_scope_hooks();
533 FOR_EACH_PTR(stmt->stmts, tmp) {
534 __split_stmt(tmp);
535 } END_FOR_EACH_PTR(tmp);
536 __call_scope_hooks();
537 return;
539 case STMT_IF:
540 if (known_condition_true(stmt->if_conditional)) {
541 __split_stmt(stmt->if_true);
542 return;
544 if (known_condition_false(stmt->if_conditional)) {
545 __split_stmt(stmt->if_false);
546 return;
548 if (option_known_conditions &&
549 implied_condition_true(stmt->if_conditional)) {
550 sm_info("this condition is true.");
551 __split_stmt(stmt->if_true);
552 return;
554 if (option_known_conditions &&
555 implied_condition_false(stmt->if_conditional)) {
556 sm_info("this condition is false.");
557 __split_stmt(stmt->if_false);
558 return;
560 __split_whole_condition(stmt->if_conditional);
561 __split_stmt(stmt->if_true);
562 if (empty_statement(stmt->if_true) &&
563 last_stmt_on_same_line() &&
564 !get_macro_name(&stmt->if_true->pos))
565 sm_msg("warn: if();");
566 __push_true_states();
567 __use_false_states();
568 __split_stmt(stmt->if_false);
569 __merge_true_states();
570 return;
571 case STMT_ITERATOR:
572 if (stmt->iterator_pre_condition)
573 handle_pre_loop(stmt);
574 else if (stmt->iterator_post_condition)
575 handle_post_loop(stmt);
576 else {
577 // these are for(;;) type loops.
578 handle_pre_loop(stmt);
580 return;
581 case STMT_SWITCH:
582 if (get_value(stmt->switch_expression, &val)) {
583 split_known_switch(stmt, val);
584 return;
586 __split_expr(stmt->switch_expression);
587 push_expression(&switch_expr_stack, stmt->switch_expression);
588 __save_switch_states(top_expression(switch_expr_stack));
589 nullify_path();
590 __push_default();
591 __push_breaks();
592 __split_stmt(stmt->switch_statement);
593 if (!__pop_default())
594 __merge_switches(top_expression(switch_expr_stack),
595 NULL);
596 __discard_switches();
597 __merge_breaks();
598 pop_expression(&switch_expr_stack);
599 return;
600 case STMT_CASE:
601 __merge_switches(top_expression(switch_expr_stack),
602 stmt->case_expression);
603 __pass_case_to_client(top_expression(switch_expr_stack),
604 stmt->case_expression);
605 if (!stmt->case_expression)
606 __set_default();
607 __split_expr(stmt->case_expression);
608 __split_expr(stmt->case_to);
609 __split_stmt(stmt->case_statement);
610 return;
611 case STMT_LABEL:
612 if (stmt->label_identifier &&
613 stmt->label_identifier->type == SYM_LABEL &&
614 stmt->label_identifier->ident) {
615 loop_count = 1000000;
616 __merge_gotos(stmt->label_identifier->ident->name);
618 __split_stmt(stmt->label_statement);
619 return;
620 case STMT_GOTO:
621 __split_expr(stmt->goto_expression);
622 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
623 if (!strcmp(stmt->goto_label->ident->name, "break")) {
624 __process_breaks();
625 } else if (!strcmp(stmt->goto_label->ident->name,
626 "continue")) {
627 __process_continues();
629 } else if (stmt->goto_label &&
630 stmt->goto_label->type == SYM_LABEL &&
631 stmt->goto_label->ident) {
632 __save_gotos(stmt->goto_label->ident->name);
634 nullify_path();
635 return;
636 case STMT_NONE:
637 return;
638 case STMT_ASM:
639 __pass_to_client(stmt, ASM_HOOK);
640 __split_expr(stmt->asm_string);
641 split_asm_constraints(stmt->asm_outputs);
642 split_asm_constraints(stmt->asm_inputs);
643 split_asm_constraints(stmt->asm_clobbers);
644 return;
645 case STMT_CONTEXT:
646 return;
647 case STMT_RANGE:
648 __split_expr(stmt->range_expression);
649 __split_expr(stmt->range_low);
650 __split_expr(stmt->range_high);
651 return;
655 static void split_expr_list(struct expression_list *expr_list)
657 struct expression *expr;
659 FOR_EACH_PTR(expr_list, expr) {
660 __split_expr(expr);
661 } END_FOR_EACH_PTR(expr);
664 static void split_sym(struct symbol *sym)
666 if (!sym)
667 return;
668 if (!(sym->namespace & NS_SYMBOL))
669 return;
671 __split_stmt(sym->stmt);
672 __split_expr(sym->array_size);
673 split_symlist(sym->arguments);
674 split_symlist(sym->symbol_list);
675 __split_stmt(sym->inline_stmt);
676 split_symlist(sym->inline_symbol_list);
679 static void split_symlist(struct symbol_list *sym_list)
681 struct symbol *sym;
683 FOR_EACH_PTR(sym_list, sym) {
684 split_sym(sym);
685 } END_FOR_EACH_PTR(sym);
688 static struct expression *fake_assign_expr(struct symbol *sym)
690 struct expression *e_assign, *e_symbol;
692 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
693 e_symbol = alloc_expression(sym->pos, EXPR_SYMBOL);
694 e_assign->op = (int)'=';
695 e_symbol->symbol = sym;
696 e_symbol->symbol_name = sym->ident;
697 e_assign->left = e_symbol;
698 e_assign->right = sym->initializer;
699 return e_assign;
702 static void do_initializer_stuff(struct symbol *sym)
704 struct expression *assign;
706 if (!sym->initializer)
707 return;
708 assign = fake_assign_expr(sym);
709 __split_expr(assign);
712 static void split_declaration(struct symbol_list *sym_list)
714 struct symbol *sym;
716 FOR_EACH_PTR(sym_list, sym) {
717 __pass_to_client(sym, DECLARATION_HOOK);
718 do_initializer_stuff(sym);
719 split_sym(sym);
720 } END_FOR_EACH_PTR(sym);
723 static void split_function(struct symbol *sym)
725 struct symbol *base_type = get_base_type(sym);
727 cur_func_sym = sym;
728 if (base_type->stmt)
729 line_func_start = base_type->stmt->pos.line;
730 if (sym->ident)
731 cur_func = sym->ident->name;
732 __smatch_lineno = sym->pos.line;
733 last_stmt = NULL;
734 loop_count = 0;
735 sm_debug("new function: %s\n", cur_func);
736 if (option_two_passes) {
737 __unnullify_path();
738 loop_num = 0;
739 final_pass = 0;
740 __pass_to_client(sym, FUNC_DEF_HOOK);
741 __split_stmt(base_type->stmt);
742 __split_stmt(base_type->inline_stmt);
743 nullify_path();
745 __unnullify_path();
746 loop_num = 0;
747 final_pass = 1;
748 __pass_to_client(sym, FUNC_DEF_HOOK);
749 __split_stmt(base_type->stmt);
750 __split_stmt(base_type->inline_stmt);
751 __pass_to_client(sym, END_FUNC_HOOK);
752 cur_func = NULL;
753 line_func_start = 0;
754 clear_all_states();
755 free_data_info_allocs();
756 free_expression_stack(&switch_expr_stack);
757 __free_ptr_list((struct ptr_list **)&big_statement_stack);
758 __bail_on_rest_of_function = 0;
761 static struct symbol_list *inlines_called;
762 static void add_inline_function(struct symbol *sym)
764 static struct symbol_list *already_added;
765 struct symbol *tmp;
767 FOR_EACH_PTR(already_added, tmp) {
768 if (tmp == sym)
769 return;
770 } END_FOR_EACH_PTR(tmp);
772 add_ptr_list(&already_added, sym);
773 add_ptr_list(&inlines_called, sym);
776 static void process_inlines()
778 struct symbol *tmp;
780 FOR_EACH_PTR(inlines_called, tmp) {
781 split_function(tmp);
782 } END_FOR_EACH_PTR(tmp);
783 free_ptr_list(&inlines_called);
786 static void split_functions(struct symbol_list *sym_list)
788 struct symbol *sym;
790 FOR_EACH_PTR(sym_list, sym) {
791 set_position(sym->pos);
792 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
793 split_function(sym);
794 process_inlines();
795 } else {
796 __pass_to_client(sym, BASE_HOOK);
798 } END_FOR_EACH_PTR(sym);
799 __pass_to_client_no_data(END_FILE_HOOK);
802 void smatch(int argc, char **argv)
805 struct string_list *filelist = NULL;
806 struct symbol_list *sym_list;
807 char *file;
809 if (argc < 2) {
810 printf("Usage: smatch [--debug] <filename.c>\n");
811 exit(1);
813 sparse_initialize(argc, argv, &filelist);
814 FOR_EACH_PTR_NOTAG(filelist, file) {
815 if (option_file_output) {
816 char buf[256];
818 snprintf(buf, sizeof(buf), "%s.smatch", file);
819 sm_outfd = fopen(buf, "w");
820 if (!sm_outfd) {
821 printf("Error: Cannot open %s\n", file);
822 exit(1);
825 sym_list = sparse_keep_tokens(file);
826 split_functions(sym_list);
827 } END_FOR_EACH_PTR_NOTAG(file);