estate: introduce estate_ranges() helper function
[smatch.git] / smatch_flow.c
blob9dcf3077febfebd62d7a83d5f0cb530c6d7ab3ec
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 get_func_pos(void) { return __smatch_lineno - line_func_start; }
39 int inside_loop(void) { return !!loop_count; }
40 int in_expression_statement(void) { return !!__expr_stmt_count; }
42 static void split_symlist(struct symbol_list *sym_list);
43 static void split_declaration(struct symbol_list *sym_list);
44 static void split_expr_list(struct expression_list *expr_list);
45 static void add_inline_function(struct symbol *sym);
47 int option_assume_loops = 0;
48 int option_known_conditions = 0;
49 int option_two_passes = 0;
50 struct symbol *cur_func_sym = NULL;
52 const char *get_filename(void)
54 if (option_full_path)
55 return full_filename;
56 return filename;
59 static void set_position(struct position pos)
61 int len;
62 static int prev_stream = -1;
64 __smatch_lineno = pos.line;
66 if (pos.stream == prev_stream)
67 return;
69 filename = stream_name(pos.stream);
71 free(full_filename);
72 pathname = getcwd(NULL, 0);
73 if (pathname) {
74 len = strlen(pathname) + 1 + strlen(filename) + 1;
75 full_filename = malloc(len);
76 snprintf(full_filename, len, "%s/%s", pathname, filename);
77 } else {
78 full_filename = alloc_string(filename);
80 free(pathname);
83 static int is_inline_func(struct expression *expr)
85 if (expr->type != EXPR_SYMBOL || !expr->symbol)
86 return 0;
87 if (expr->symbol->ctype.modifiers & MOD_INLINE)
88 return 1;
89 return 0;
92 void __split_expr(struct expression *expr)
94 if (!expr)
95 return;
97 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
99 push_expression(&big_expression_stack, expr);
100 set_position(expr->pos);
101 __pass_to_client(expr, EXPR_HOOK);
103 switch (expr->type) {
104 case EXPR_PREOP:
105 if (expr->op == '*')
106 __pass_to_client(expr, DEREF_HOOK);
107 case EXPR_POSTOP:
108 __pass_to_client(expr, OP_HOOK);
109 __split_expr(expr->unop);
110 break;
111 case EXPR_STATEMENT:
112 __expr_stmt_count++;
113 __split_stmt(expr->statement);
114 __expr_stmt_count--;
115 break;
116 case EXPR_LOGICAL:
117 case EXPR_COMPARE:
118 __pass_to_client(expr, LOGIC_HOOK);
119 __handle_logic(expr);
120 break;
121 case EXPR_BINOP:
122 __pass_to_client(expr, BINOP_HOOK);
123 case EXPR_COMMA:
124 __split_expr(expr->left);
125 __split_expr(expr->right);
126 break;
127 case EXPR_ASSIGNMENT: {
128 struct expression *tmp;
130 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
132 /* foo = !bar() */
133 if (__handle_condition_assigns(expr))
134 break;
135 /* foo = (x < 5 ? foo : 5); */
136 if (__handle_select_assigns(expr))
137 break;
138 /* foo = ({frob(); frob(); frob(); 1;}) */
139 if (__handle_expr_statement_assigns(expr))
140 break;
142 __split_expr(expr->right);
143 __pass_to_client(expr, ASSIGNMENT_HOOK);
144 tmp = strip_expr(expr->right);
145 if (tmp->type == EXPR_CALL)
146 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
147 if (get_macro_name(&tmp->pos))
148 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
149 __split_expr(expr->left);
150 break;
152 case EXPR_DEREF:
153 __pass_to_client(expr, DEREF_HOOK);
154 __split_expr(expr->deref);
155 break;
156 case EXPR_SLICE:
157 __split_expr(expr->base);
158 break;
159 case EXPR_CAST:
160 case EXPR_FORCE_CAST:
161 __split_expr(expr->cast_expression);
162 break;
163 case EXPR_SIZEOF:
164 /* there isn't anything to pass a client from inside a sizeof() */
165 break;
166 case EXPR_CONDITIONAL:
167 case EXPR_SELECT:
168 __pass_to_client(expr, SELECT_HOOK);
169 __split_whole_condition(expr->conditional);
170 __split_expr(expr->cond_true);
171 __push_true_states();
172 __use_false_states();
173 __split_expr(expr->cond_false);
174 __merge_true_states();
175 break;
176 case EXPR_CALL:
177 split_expr_list(expr->args);
178 __split_expr(expr->fn);
179 if (is_inline_func(expr->fn))
180 add_inline_function(expr->fn->symbol);
181 __pass_to_client(expr, FUNCTION_CALL_HOOK);
182 break;
183 case EXPR_INITIALIZER:
184 split_expr_list(expr->expr_list);
185 break;
186 case EXPR_IDENTIFIER:
187 __split_expr(expr->ident_expression);
188 break;
189 case EXPR_INDEX:
190 __split_expr(expr->idx_expression);
191 break;
192 case EXPR_POS:
193 __split_expr(expr->init_expr);
194 break;
195 case EXPR_SYMBOL:
196 __pass_to_client(expr, SYM_HOOK);
197 break;
198 case EXPR_STRING:
199 __pass_to_client(expr, STRING_HOOK);
200 break;
201 default:
202 break;
204 pop_expression(&big_expression_stack);
207 static int is_forever_loop(struct statement *stmt)
209 struct expression *expr;
211 expr = strip_expr(stmt->iterator_pre_condition);
212 if (!expr)
213 expr = stmt->iterator_post_condition;
214 if (!expr) {
215 /* this is a for(;;) loop... */
216 return 1;
219 if (expr->type == EXPR_VALUE && expr->value == 1)
220 return 1;
222 return 0;
225 static int loop_num;
226 static char *get_loop_name(int num)
228 char buf[256];
230 snprintf(buf, 255, "-loop%d", num);
231 buf[255] = '\0';
232 return alloc_sname(buf);
236 * Pre Loops are while and for loops.
238 static void handle_pre_loop(struct statement *stmt)
240 int once_through; /* we go through the loop at least once */
241 struct sm_state *extra_sm = NULL;
242 int unchanged = 0;
243 char *loop_name;
244 struct state_list *slist = NULL;
245 struct sm_state *sm = NULL;
247 loop_name = get_loop_name(loop_num);
248 loop_num++;
250 __split_stmt(stmt->iterator_pre_statement);
252 once_through = implied_condition_true(stmt->iterator_pre_condition);
254 loop_count++;
255 __push_continues();
256 __push_breaks();
258 __merge_gotos(loop_name);
260 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
261 __in_pre_condition++;
262 __pass_to_client(stmt, PRELOOP_HOOK);
263 __split_whole_condition(stmt->iterator_pre_condition);
264 __in_pre_condition--;
265 FOR_EACH_PTR(slist, sm) {
266 set_state(sm->owner, sm->name, sm->sym, sm->state);
267 } END_FOR_EACH_PTR(sm);
268 free_slist(&slist);
269 if (extra_sm)
270 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
272 if (option_assume_loops)
273 once_through = 1;
275 __split_stmt(stmt->iterator_statement);
276 __warn_on_silly_pre_loops();
277 if (is_forever_loop(stmt)) {
278 __save_gotos(loop_name);
279 /* forever loops don't have an iterator_post_statement */
280 __discard_continues();
281 __discard_false_states();
282 __use_breaks();
283 } else {
284 __merge_continues();
285 unchanged = __iterator_unchanged(extra_sm);
286 __split_stmt(stmt->iterator_post_statement);
287 __save_gotos(loop_name);
288 __split_whole_condition(stmt->iterator_pre_condition);
289 nullify_path();
290 __merge_false_states();
291 if (once_through)
292 __discard_false_states();
293 else
294 __merge_false_states();
296 if (extra_sm && unchanged)
297 __extra_pre_loop_hook_after(extra_sm,
298 stmt->iterator_post_statement,
299 stmt->iterator_pre_condition);
300 __merge_breaks();
302 loop_count--;
306 * Post loops are do {} while();
308 static void handle_post_loop(struct statement *stmt)
310 char *loop_name;
312 loop_name = get_loop_name(loop_num);
313 loop_num++;
314 loop_count++;
316 __push_continues();
317 __push_breaks();
318 __merge_gotos(loop_name);
319 __split_stmt(stmt->iterator_statement);
320 __merge_continues();
321 if (!is_zero(stmt->iterator_post_condition))
322 __save_gotos(loop_name);
324 if (is_forever_loop(stmt)) {
325 __use_breaks();
326 } else {
327 __split_whole_condition(stmt->iterator_post_condition);
328 __use_false_states();
329 __merge_breaks();
331 loop_count--;
334 static int empty_statement(struct statement *stmt)
336 if (!stmt)
337 return 0;
338 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
339 return 1;
340 return 0;
343 static int last_stmt_on_same_line()
345 struct statement *stmt;
346 int i = 0;
348 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
349 if (!i++)
350 continue;
351 if (stmt->pos.line == get_lineno())
352 return 1;
353 return 0;
354 } END_FOR_EACH_PTR_REVERSE(stmt);
355 return 0;
358 static struct statement *last_stmt;
359 static int is_last_stmt(struct statement *stmt)
361 if (stmt == last_stmt)
362 return 1;
363 return 0;
366 static void print_unreached_initializers(struct symbol_list *sym_list)
368 struct symbol *sym;
370 FOR_EACH_PTR(sym_list, sym) {
371 if (sym->initializer)
372 sm_msg("info: '%s' is not actually initialized (unreached code).",
373 (sym->ident ? sym->ident->name : "this variable"));
374 } END_FOR_EACH_PTR(sym);
377 static void print_unreached(struct statement *stmt)
379 static int print = 1;
381 if (!__path_is_null()) {
382 print = 1;
383 return;
385 if (!print)
386 return;
388 switch (stmt->type) {
389 case STMT_COMPOUND: /* after a switch before a case stmt */
390 case STMT_RANGE:
391 case STMT_CASE:
392 case STMT_LABEL:
393 return;
394 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
395 print_unreached_initializers(stmt->declaration);
396 return;
397 case STMT_RETURN: /* gcc complains if you don't have a return statement */
398 if (is_last_stmt(stmt))
399 return;
400 break;
401 case STMT_GOTO:
402 if (!option_spammy)
403 return;
404 break;
405 default:
406 break;
408 if (!option_spammy && empty_statement(stmt))
409 return;
410 sm_msg("info: ignoring unreachable code.");
411 print = 0;
414 static void split_asm_constraints(struct expression_list *expr_list)
416 struct expression *expr;
417 int state = 0;
419 FOR_EACH_PTR(expr_list, expr) {
420 switch (state) {
421 case 0: /* identifier */
422 case 1: /* constraint */
423 state++;
424 continue;
425 case 2: /* expression */
426 state = 0;
427 __split_expr(expr);
428 continue;
430 } END_FOR_EACH_PTR(expr);
433 static int is_case_val(struct statement *stmt, long long val)
435 long long case_val;
437 if (stmt->type != STMT_CASE)
438 return 0;
439 if (!stmt->case_expression) {
440 __set_default();
441 return 1;
443 if (!get_value(stmt->case_expression, &case_val))
444 return 0;
445 if (case_val == val)
446 return 1;
447 return 0;
450 static void split_known_switch(struct statement *stmt, long long val)
452 struct statement *tmp;
454 __split_expr(stmt->switch_expression);
456 push_expression(&switch_expr_stack, stmt->switch_expression);
457 __save_switch_states(top_expression(switch_expr_stack));
458 nullify_path();
459 __push_default();
460 __push_breaks();
462 stmt = stmt->switch_statement;
464 if (!last_stmt)
465 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
467 __push_scope_hooks();
468 FOR_EACH_PTR(stmt->stmts, tmp) {
469 __smatch_lineno = tmp->pos.line;
470 if (is_case_val(tmp, val)) {
471 __merge_switches(top_expression(switch_expr_stack),
472 stmt->case_expression);
473 __pass_case_to_client(top_expression(switch_expr_stack),
474 stmt->case_expression);
476 if (__path_is_null())
477 continue;
478 __split_stmt(tmp);
479 if (__path_is_null()) {
480 __set_default();
481 goto out;
483 } END_FOR_EACH_PTR(tmp);
484 out:
485 __call_scope_hooks();
486 if (!__pop_default())
487 __merge_switches(top_expression(switch_expr_stack),
488 NULL);
489 __discard_switches();
490 __merge_breaks();
491 pop_expression(&switch_expr_stack);
494 void __split_stmt(struct statement *stmt)
496 long long val;
498 if (!stmt)
499 return;
501 if (out_of_memory() || __bail_on_rest_of_function) {
502 static char *printed = NULL;
504 if (printed != cur_func)
505 sm_msg("Function too hairy. Giving up.");
506 printed = cur_func;
507 return;
510 add_ptr_list(&big_statement_stack, stmt);
511 free_expression_stack(&big_expression_stack);
512 set_position(stmt->pos);
513 print_unreached(stmt);
514 __pass_to_client(stmt, STMT_HOOK);
516 switch (stmt->type) {
517 case STMT_DECLARATION:
518 split_declaration(stmt->declaration);
519 return;
520 case STMT_RETURN:
521 __split_expr(stmt->ret_value);
522 __pass_to_client(stmt->ret_value, RETURN_HOOK);
523 nullify_path();
524 return;
525 case STMT_EXPRESSION:
526 __split_expr(stmt->expression);
527 return;
528 case STMT_COMPOUND: {
529 struct statement *tmp;
531 if (!last_stmt)
532 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
533 __push_scope_hooks();
534 FOR_EACH_PTR(stmt->stmts, tmp) {
535 __split_stmt(tmp);
536 } END_FOR_EACH_PTR(tmp);
537 __call_scope_hooks();
538 return;
540 case STMT_IF:
541 if (known_condition_true(stmt->if_conditional)) {
542 __split_stmt(stmt->if_true);
543 return;
545 if (known_condition_false(stmt->if_conditional)) {
546 __split_stmt(stmt->if_false);
547 return;
549 if (option_known_conditions &&
550 implied_condition_true(stmt->if_conditional)) {
551 sm_info("this condition is true.");
552 __split_stmt(stmt->if_true);
553 return;
555 if (option_known_conditions &&
556 implied_condition_false(stmt->if_conditional)) {
557 sm_info("this condition is false.");
558 __split_stmt(stmt->if_false);
559 return;
561 __split_whole_condition(stmt->if_conditional);
562 __split_stmt(stmt->if_true);
563 if (empty_statement(stmt->if_true) &&
564 last_stmt_on_same_line() &&
565 !get_macro_name(&stmt->if_true->pos))
566 sm_msg("warn: if();");
567 __push_true_states();
568 __use_false_states();
569 __split_stmt(stmt->if_false);
570 __merge_true_states();
571 return;
572 case STMT_ITERATOR:
573 if (stmt->iterator_pre_condition)
574 handle_pre_loop(stmt);
575 else if (stmt->iterator_post_condition)
576 handle_post_loop(stmt);
577 else {
578 // these are for(;;) type loops.
579 handle_pre_loop(stmt);
581 return;
582 case STMT_SWITCH:
583 if (get_value(stmt->switch_expression, &val)) {
584 split_known_switch(stmt, val);
585 return;
587 __split_expr(stmt->switch_expression);
588 push_expression(&switch_expr_stack, stmt->switch_expression);
589 __save_switch_states(top_expression(switch_expr_stack));
590 nullify_path();
591 __push_default();
592 __push_breaks();
593 __split_stmt(stmt->switch_statement);
594 if (!__pop_default())
595 __merge_switches(top_expression(switch_expr_stack),
596 NULL);
597 __discard_switches();
598 __merge_breaks();
599 pop_expression(&switch_expr_stack);
600 return;
601 case STMT_CASE:
602 __merge_switches(top_expression(switch_expr_stack),
603 stmt->case_expression);
604 __pass_case_to_client(top_expression(switch_expr_stack),
605 stmt->case_expression);
606 if (!stmt->case_expression)
607 __set_default();
608 __split_expr(stmt->case_expression);
609 __split_expr(stmt->case_to);
610 __split_stmt(stmt->case_statement);
611 return;
612 case STMT_LABEL:
613 if (stmt->label_identifier &&
614 stmt->label_identifier->type == SYM_LABEL &&
615 stmt->label_identifier->ident) {
616 loop_count = 1000000;
617 __merge_gotos(stmt->label_identifier->ident->name);
619 __split_stmt(stmt->label_statement);
620 return;
621 case STMT_GOTO:
622 __split_expr(stmt->goto_expression);
623 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
624 if (!strcmp(stmt->goto_label->ident->name, "break")) {
625 __process_breaks();
626 } else if (!strcmp(stmt->goto_label->ident->name,
627 "continue")) {
628 __process_continues();
630 } else if (stmt->goto_label &&
631 stmt->goto_label->type == SYM_LABEL &&
632 stmt->goto_label->ident) {
633 __save_gotos(stmt->goto_label->ident->name);
635 nullify_path();
636 return;
637 case STMT_NONE:
638 return;
639 case STMT_ASM:
640 __pass_to_client(stmt, ASM_HOOK);
641 __split_expr(stmt->asm_string);
642 split_asm_constraints(stmt->asm_outputs);
643 split_asm_constraints(stmt->asm_inputs);
644 split_asm_constraints(stmt->asm_clobbers);
645 return;
646 case STMT_CONTEXT:
647 return;
648 case STMT_RANGE:
649 __split_expr(stmt->range_expression);
650 __split_expr(stmt->range_low);
651 __split_expr(stmt->range_high);
652 return;
656 static void split_expr_list(struct expression_list *expr_list)
658 struct expression *expr;
660 FOR_EACH_PTR(expr_list, expr) {
661 __split_expr(expr);
662 } END_FOR_EACH_PTR(expr);
665 static void split_sym(struct symbol *sym)
667 if (!sym)
668 return;
669 if (!(sym->namespace & NS_SYMBOL))
670 return;
672 __split_stmt(sym->stmt);
673 __split_expr(sym->array_size);
674 split_symlist(sym->arguments);
675 split_symlist(sym->symbol_list);
676 __split_stmt(sym->inline_stmt);
677 split_symlist(sym->inline_symbol_list);
680 static void split_symlist(struct symbol_list *sym_list)
682 struct symbol *sym;
684 FOR_EACH_PTR(sym_list, sym) {
685 split_sym(sym);
686 } END_FOR_EACH_PTR(sym);
689 static struct expression *fake_assign_expr(struct symbol *sym)
691 struct expression *e_assign, *e_symbol;
693 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
694 e_symbol = alloc_expression(sym->pos, EXPR_SYMBOL);
695 e_assign->op = (int)'=';
696 e_symbol->symbol = sym;
697 e_symbol->symbol_name = sym->ident;
698 e_assign->left = e_symbol;
699 e_assign->right = sym->initializer;
700 return e_assign;
703 static void do_initializer_stuff(struct symbol *sym)
705 struct expression *assign;
707 if (!sym->initializer)
708 return;
709 assign = fake_assign_expr(sym);
710 __split_expr(assign);
713 static void split_declaration(struct symbol_list *sym_list)
715 struct symbol *sym;
717 FOR_EACH_PTR(sym_list, sym) {
718 __pass_to_client(sym, DECLARATION_HOOK);
719 do_initializer_stuff(sym);
720 split_sym(sym);
721 } END_FOR_EACH_PTR(sym);
724 static void split_function(struct symbol *sym)
726 struct symbol *base_type = get_base_type(sym);
728 cur_func_sym = sym;
729 if (base_type->stmt)
730 line_func_start = base_type->stmt->pos.line;
731 if (sym->ident)
732 cur_func = sym->ident->name;
733 __smatch_lineno = sym->pos.line;
734 last_stmt = NULL;
735 loop_count = 0;
736 sm_debug("new function: %s\n", cur_func);
737 if (option_two_passes) {
738 __unnullify_path();
739 loop_num = 0;
740 final_pass = 0;
741 __pass_to_client(sym, FUNC_DEF_HOOK);
742 __split_stmt(base_type->stmt);
743 __split_stmt(base_type->inline_stmt);
744 nullify_path();
746 __unnullify_path();
747 loop_num = 0;
748 final_pass = 1;
749 __pass_to_client(sym, FUNC_DEF_HOOK);
750 __split_stmt(base_type->stmt);
751 __split_stmt(base_type->inline_stmt);
752 __pass_to_client(sym, END_FUNC_HOOK);
753 cur_func = NULL;
754 line_func_start = 0;
755 clear_all_states();
756 free_data_info_allocs();
757 free_expression_stack(&switch_expr_stack);
758 __free_ptr_list((struct ptr_list **)&big_statement_stack);
759 __bail_on_rest_of_function = 0;
762 static struct symbol_list *inlines_called;
763 static void add_inline_function(struct symbol *sym)
765 static struct symbol_list *already_added;
766 struct symbol *tmp;
768 FOR_EACH_PTR(already_added, tmp) {
769 if (tmp == sym)
770 return;
771 } END_FOR_EACH_PTR(tmp);
773 add_ptr_list(&already_added, sym);
774 add_ptr_list(&inlines_called, sym);
777 static void process_inlines()
779 struct symbol *tmp;
781 FOR_EACH_PTR(inlines_called, tmp) {
782 split_function(tmp);
783 } END_FOR_EACH_PTR(tmp);
784 free_ptr_list(&inlines_called);
787 static void split_functions(struct symbol_list *sym_list)
789 struct symbol *sym;
791 FOR_EACH_PTR(sym_list, sym) {
792 set_position(sym->pos);
793 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
794 split_function(sym);
795 process_inlines();
796 } else {
797 __pass_to_client(sym, BASE_HOOK);
799 } END_FOR_EACH_PTR(sym);
800 __pass_to_client_no_data(END_FILE_HOOK);
803 void smatch(int argc, char **argv)
806 struct string_list *filelist = NULL;
807 struct symbol_list *sym_list;
808 char *file;
810 if (argc < 2) {
811 printf("Usage: smatch [--debug] <filename.c>\n");
812 exit(1);
814 sparse_initialize(argc, argv, &filelist);
815 FOR_EACH_PTR_NOTAG(filelist, file) {
816 if (option_file_output) {
817 char buf[256];
819 snprintf(buf, sizeof(buf), "%s.smatch", file);
820 sm_outfd = fopen(buf, "w");
821 if (!sm_outfd) {
822 printf("Error: Cannot open %s\n", file);
823 exit(1);
826 sym_list = sparse_keep_tokens(file);
827 split_functions(sym_list);
828 } END_FOR_EACH_PTR_NOTAG(file);