db: boost the allowable return_states count to 3000 from 1000
[smatch.git] / smatch_flow.c
blob56ca216928e20c6ee5bf29cf30aff43c9a11eae4
1 /*
2 * Copyright (C) 2006,2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #define _GNU_SOURCE 1
19 #include <unistd.h>
20 #include <stdio.h>
21 #include "token.h"
22 #include "scope.h"
23 #include "smatch.h"
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 int __in_fake_assign;
29 int final_pass;
30 int __inline_call;
31 struct expression *__inline_fn;
33 static int __smatch_lineno = 0;
35 static char *base_file;
36 static const char *filename;
37 static char *pathname;
38 static char *full_filename;
39 static char *cur_func;
40 static unsigned int loop_count;
41 static int last_goto_statement_handled;
42 int __expr_stmt_count;
43 int __in_function_def;
44 static struct expression_list *switch_expr_stack = NULL;
45 static struct expression_list *post_op_stack = NULL;
47 struct expression_list *big_expression_stack;
48 struct statement_list *big_statement_stack;
49 int __in_pre_condition = 0;
50 int __bail_on_rest_of_function = 0;
51 static struct timeval fn_start_time;
52 char *get_function(void) { return cur_func; }
53 int get_lineno(void) { return __smatch_lineno; }
54 int inside_loop(void) { return !!loop_count; }
55 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
56 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
57 int in_expression_statement(void) { return !!__expr_stmt_count; }
59 static void split_symlist(struct symbol_list *sym_list);
60 static void split_declaration(struct symbol_list *sym_list);
61 static void split_expr_list(struct expression_list *expr_list);
62 static void add_inline_function(struct symbol *sym);
63 static void parse_inline(struct expression *expr);
65 int option_assume_loops = 0;
66 int option_known_conditions = 0;
67 int option_two_passes = 0;
68 struct symbol *cur_func_sym = NULL;
69 struct stree *global_states;
71 long long valid_ptr_min = 4096;
72 long long valid_ptr_max = 7777777777;
73 sval_t valid_ptr_min_sval = {
74 .type = &ptr_ctype,
75 {.value = 4096},
77 sval_t valid_ptr_max_sval = {
78 .type = &ptr_ctype,
79 {.value = LONG_MAX - 100000},
82 static void set_valid_ptr_max(void)
84 if (type_bits(&ptr_ctype) == 32)
85 valid_ptr_max = 7777777777;
86 else if (type_bits(&ptr_ctype) == 64)
87 valid_ptr_max = 7777777777777777777LL;
89 valid_ptr_max_sval.value = valid_ptr_max;
92 int outside_of_function(void)
94 return cur_func_sym == NULL;
97 const char *get_filename(void)
99 if (option_info)
100 return base_file;
101 if (option_full_path)
102 return full_filename;
103 return filename;
106 const char *get_base_file(void)
108 return base_file;
111 static void set_position(struct position pos)
113 int len;
114 static int prev_stream = -1;
116 if (pos.stream == 0 && pos.line == 0)
117 return;
119 __smatch_lineno = pos.line;
121 if (pos.stream == prev_stream)
122 return;
124 filename = stream_name(pos.stream);
126 free(full_filename);
127 pathname = getcwd(NULL, 0);
128 if (pathname) {
129 len = strlen(pathname) + 1 + strlen(filename) + 1;
130 full_filename = malloc(len);
131 snprintf(full_filename, len, "%s/%s", pathname, filename);
132 } else {
133 full_filename = alloc_string(filename);
135 free(pathname);
138 static int is_inline_func(struct expression *expr)
140 if (expr->type != EXPR_SYMBOL || !expr->symbol)
141 return 0;
142 if (expr->symbol->ctype.modifiers & MOD_INLINE)
143 return 1;
144 return 0;
147 static int is_noreturn_func(struct expression *expr)
149 if (expr->type != EXPR_SYMBOL || !expr->symbol)
150 return 0;
151 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
152 return 1;
153 return 0;
156 int inlinable(struct expression *expr)
158 struct symbol *sym;
160 if (__inline_fn) /* don't nest */
161 return 0;
163 if (expr->type != EXPR_SYMBOL || !expr->symbol)
164 return 0;
165 if (is_no_inline_function(expr->symbol->ident->name))
166 return 0;
167 sym = get_base_type(expr->symbol);
168 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
169 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
170 return 1;
171 return 0;
173 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
174 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
175 return 1;
176 return 0;
178 return 0;
181 void __process_post_op_stack(void)
183 struct expression *expr;
185 FOR_EACH_PTR(post_op_stack, expr) {
186 __pass_to_client(expr, OP_HOOK);
187 } END_FOR_EACH_PTR(expr);
189 __free_ptr_list((struct ptr_list **)&post_op_stack);
192 static int handle_comma_assigns(struct expression *expr)
194 struct expression *right;
195 struct expression *assign;
197 right = strip_expr(expr->right);
198 if (right->type != EXPR_COMMA)
199 return 0;
201 __split_expr(right->left);
202 __process_post_op_stack();
204 assign = assign_expression(expr->left, right->right);
205 __split_expr(assign);
207 return 1;
210 void __split_expr(struct expression *expr)
212 if (!expr)
213 return;
215 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
217 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
218 return;
219 if (__in_fake_assign >= 4) /* don't allow too much nesting */
220 return;
222 push_expression(&big_expression_stack, expr);
223 set_position(expr->pos);
224 __pass_to_client(expr, EXPR_HOOK);
226 switch (expr->type) {
227 case EXPR_PREOP:
228 if (expr->op == '*')
229 __pass_to_client(expr, DEREF_HOOK);
230 __split_expr(expr->unop);
231 __pass_to_client(expr, OP_HOOK);
232 break;
233 case EXPR_POSTOP:
234 __split_expr(expr->unop);
235 push_expression(&post_op_stack, expr);
236 break;
237 case EXPR_STATEMENT:
238 __expr_stmt_count++;
239 __split_stmt(expr->statement);
240 __expr_stmt_count--;
241 break;
242 case EXPR_LOGICAL:
243 case EXPR_COMPARE:
244 __pass_to_client(expr, LOGIC_HOOK);
245 __handle_logic(expr);
246 break;
247 case EXPR_BINOP:
248 __pass_to_client(expr, BINOP_HOOK);
249 case EXPR_COMMA:
250 __split_expr(expr->left);
251 __process_post_op_stack();
252 __split_expr(expr->right);
253 break;
254 case EXPR_ASSIGNMENT: {
255 struct expression *tmp;
257 if (!expr->right)
258 break;
260 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
262 /* foo = !bar() */
263 if (__handle_condition_assigns(expr))
264 break;
265 /* foo = (x < 5 ? foo : 5); */
266 if (__handle_select_assigns(expr))
267 break;
268 /* foo = ({frob(); frob(); frob(); 1;}) */
269 if (__handle_expr_statement_assigns(expr))
270 break;
271 /* foo = (3, 4); */
272 if (handle_comma_assigns(expr))
273 break;
275 __split_expr(expr->right);
276 if (outside_of_function())
277 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
278 else
279 __pass_to_client(expr, ASSIGNMENT_HOOK);
281 __fake_struct_member_assignments(expr);
283 tmp = strip_expr(expr->right);
284 if (tmp->type == EXPR_CALL)
285 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
286 if (get_macro_name(tmp->pos) &&
287 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
288 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
289 __split_expr(expr->left);
290 break;
292 case EXPR_DEREF:
293 __pass_to_client(expr, DEREF_HOOK);
294 __split_expr(expr->deref);
295 break;
296 case EXPR_SLICE:
297 __split_expr(expr->base);
298 break;
299 case EXPR_CAST:
300 case EXPR_FORCE_CAST:
301 __pass_to_client(expr, CAST_HOOK);
302 __split_expr(expr->cast_expression);
303 break;
304 case EXPR_SIZEOF:
305 if (expr->cast_expression)
306 __pass_to_client(strip_parens(expr->cast_expression),
307 SIZEOF_HOOK);
308 break;
309 case EXPR_OFFSETOF:
310 case EXPR_ALIGNOF:
311 evaluate_expression(expr);
312 break;
313 case EXPR_CONDITIONAL:
314 case EXPR_SELECT:
315 if (known_condition_true(expr->conditional)) {
316 __split_expr(expr->cond_true);
317 break;
319 if (known_condition_false(expr->conditional)) {
320 __split_expr(expr->cond_false);
321 break;
323 __pass_to_client(expr, SELECT_HOOK);
324 __split_whole_condition(expr->conditional);
325 __split_expr(expr->cond_true);
326 __push_true_states();
327 __use_false_states();
328 __split_expr(expr->cond_false);
329 __merge_true_states();
330 break;
331 case EXPR_CALL:
332 if (sym_name_is("__builtin_constant_p", expr->fn))
333 break;
334 split_expr_list(expr->args);
335 __split_expr(expr->fn);
336 if (is_inline_func(expr->fn))
337 add_inline_function(expr->fn->symbol);
338 if (inlinable(expr->fn))
339 __inline_call = 1;
340 __process_post_op_stack();
341 __pass_to_client(expr, FUNCTION_CALL_HOOK);
342 __inline_call = 0;
343 if (inlinable(expr->fn)) {
344 parse_inline(expr);
346 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
347 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER);
348 if (is_noreturn_func(expr->fn))
349 nullify_path();
350 break;
351 case EXPR_INITIALIZER:
352 split_expr_list(expr->expr_list);
353 break;
354 case EXPR_IDENTIFIER:
355 __split_expr(expr->ident_expression);
356 break;
357 case EXPR_INDEX:
358 __split_expr(expr->idx_expression);
359 break;
360 case EXPR_POS:
361 __split_expr(expr->init_expr);
362 break;
363 case EXPR_SYMBOL:
364 __pass_to_client(expr, SYM_HOOK);
365 break;
366 case EXPR_STRING:
367 __pass_to_client(expr, STRING_HOOK);
368 break;
369 default:
370 break;
372 pop_expression(&big_expression_stack);
375 static int is_forever_loop(struct statement *stmt)
377 struct expression *expr;
379 expr = strip_expr(stmt->iterator_pre_condition);
380 if (!expr)
381 expr = stmt->iterator_post_condition;
382 if (!expr) {
383 /* this is a for(;;) loop... */
384 return 1;
387 if (expr->type == EXPR_VALUE && expr->value == 1)
388 return 1;
390 return 0;
393 static int loop_num;
394 static char *get_loop_name(int num)
396 char buf[256];
398 snprintf(buf, 255, "-loop%d", num);
399 buf[255] = '\0';
400 return alloc_sname(buf);
404 * Pre Loops are while and for loops.
406 static void handle_pre_loop(struct statement *stmt)
408 int once_through; /* we go through the loop at least once */
409 struct sm_state *extra_sm = NULL;
410 int unchanged = 0;
411 char *loop_name;
412 struct stree *stree = NULL;
413 struct sm_state *sm = NULL;
415 loop_name = get_loop_name(loop_num);
416 loop_num++;
418 __split_stmt(stmt->iterator_pre_statement);
420 once_through = implied_condition_true(stmt->iterator_pre_condition);
422 loop_count++;
423 __push_continues();
424 __push_breaks();
426 __merge_gotos(loop_name);
428 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
429 __in_pre_condition++;
430 __pass_to_client(stmt, PRELOOP_HOOK);
431 __split_whole_condition(stmt->iterator_pre_condition);
432 __in_pre_condition--;
433 FOR_EACH_SM(stree, sm) {
434 set_state(sm->owner, sm->name, sm->sym, sm->state);
435 } END_FOR_EACH_SM(sm);
436 free_stree(&stree);
437 if (extra_sm)
438 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
440 if (option_assume_loops)
441 once_through = 1;
443 __split_stmt(stmt->iterator_statement);
444 __warn_on_silly_pre_loops();
445 if (is_forever_loop(stmt)) {
446 __merge_continues();
447 __save_gotos(loop_name);
449 __push_fake_cur_stree();
450 __split_stmt(stmt->iterator_post_statement);
451 stree = __pop_fake_cur_stree();
453 __discard_false_states();
454 __use_breaks();
456 if (!__path_is_null())
457 __merge_stree_into_cur(stree);
458 free_stree(&stree);
459 } else {
460 __merge_continues();
461 unchanged = __iterator_unchanged(extra_sm);
462 __split_stmt(stmt->iterator_post_statement);
463 __save_gotos(loop_name);
464 __in_pre_condition++;
465 __split_whole_condition(stmt->iterator_pre_condition);
466 __in_pre_condition--;
467 nullify_path();
468 __merge_false_states();
469 if (once_through)
470 __discard_false_states();
471 else
472 __merge_false_states();
474 if (extra_sm && unchanged)
475 __extra_pre_loop_hook_after(extra_sm,
476 stmt->iterator_post_statement,
477 stmt->iterator_pre_condition);
478 __merge_breaks();
480 loop_count--;
484 * Post loops are do {} while();
486 static void handle_post_loop(struct statement *stmt)
488 char *loop_name;
490 loop_name = get_loop_name(loop_num);
491 loop_num++;
492 loop_count++;
494 __push_continues();
495 __push_breaks();
496 __merge_gotos(loop_name);
497 __split_stmt(stmt->iterator_statement);
498 __merge_continues();
499 if (!is_zero(stmt->iterator_post_condition))
500 __save_gotos(loop_name);
502 if (is_forever_loop(stmt)) {
503 __use_breaks();
504 } else {
505 __split_whole_condition(stmt->iterator_post_condition);
506 __use_false_states();
507 __merge_breaks();
509 loop_count--;
512 static int empty_statement(struct statement *stmt)
514 if (!stmt)
515 return 0;
516 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
517 return 1;
518 return 0;
521 static int last_stmt_on_same_line(void)
523 struct statement *stmt;
524 int i = 0;
526 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
527 if (!i++)
528 continue;
529 if (stmt->pos.line == get_lineno())
530 return 1;
531 return 0;
532 } END_FOR_EACH_PTR_REVERSE(stmt);
533 return 0;
536 static void split_asm_constraints(struct expression_list *expr_list)
538 struct expression *expr;
539 int state = 0;
541 FOR_EACH_PTR(expr_list, expr) {
542 switch (state) {
543 case 0: /* identifier */
544 case 1: /* constraint */
545 state++;
546 continue;
547 case 2: /* expression */
548 state = 0;
549 __split_expr(expr);
550 continue;
552 } END_FOR_EACH_PTR(expr);
555 static int is_case_val(struct statement *stmt, sval_t sval)
557 sval_t case_sval;
559 if (stmt->type != STMT_CASE)
560 return 0;
561 if (!stmt->case_expression) {
562 __set_default();
563 return 1;
565 if (!get_value(stmt->case_expression, &case_sval))
566 return 0;
567 if (case_sval.value == sval.value)
568 return 1;
569 return 0;
572 static void split_known_switch(struct statement *stmt, sval_t sval)
574 struct statement *tmp;
576 __split_expr(stmt->switch_expression);
578 push_expression(&switch_expr_stack, stmt->switch_expression);
579 __save_switch_states(top_expression(switch_expr_stack));
580 nullify_path();
581 __push_default();
582 __push_breaks();
584 stmt = stmt->switch_statement;
586 __push_scope_hooks();
587 FOR_EACH_PTR(stmt->stmts, tmp) {
588 __smatch_lineno = tmp->pos.line;
589 if (is_case_val(tmp, sval)) {
590 __merge_switches(top_expression(switch_expr_stack),
591 stmt->case_expression);
592 __pass_case_to_client(top_expression(switch_expr_stack),
593 stmt->case_expression);
595 if (__path_is_null())
596 continue;
597 __split_stmt(tmp);
598 if (__path_is_null()) {
599 __set_default();
600 goto out;
602 } END_FOR_EACH_PTR(tmp);
603 out:
604 __call_scope_hooks();
605 if (!__pop_default())
606 __merge_switches(top_expression(switch_expr_stack),
607 NULL);
608 __discard_switches();
609 __merge_breaks();
610 pop_expression(&switch_expr_stack);
613 static int taking_too_long(void)
615 int ms;
617 ms = ms_since(&fn_start_time);
618 if (ms > 1000 * 60 * 5) /* five minutes */
619 return 1;
620 return 0;
623 static int is_last_stmt(struct statement *cur_stmt)
625 struct symbol *fn = get_base_type(cur_func_sym);
626 struct statement *stmt;
628 if (!fn)
629 return 0;
630 stmt = fn->stmt;
631 if (!stmt)
632 stmt = fn->inline_stmt;
633 if (!stmt || stmt->type != STMT_COMPOUND)
634 return 0;
635 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
636 if (stmt && stmt->type == STMT_LABEL)
637 stmt = stmt->label_statement;
638 if (stmt == cur_stmt)
639 return 1;
640 return 0;
643 static void handle_backward_goto(struct statement *goto_stmt)
645 const char *goto_name, *label_name;
646 struct statement *func_stmt;
647 struct symbol *base_type = get_base_type(cur_func_sym);
648 struct statement *tmp;
649 int found = 0;
651 if (!option_info)
652 return;
653 if (last_goto_statement_handled)
654 return;
655 last_goto_statement_handled = 1;
657 if (!goto_stmt->goto_label ||
658 goto_stmt->goto_label->type != SYM_LABEL ||
659 !goto_stmt->goto_label->ident)
660 return;
661 goto_name = goto_stmt->goto_label->ident->name;
663 func_stmt = base_type->stmt;
664 if (!func_stmt)
665 func_stmt = base_type->inline_stmt;
666 if (!func_stmt)
667 return;
668 if (func_stmt->type != STMT_COMPOUND)
669 return;
671 FOR_EACH_PTR(func_stmt->stmts, tmp) {
672 if (!found) {
673 if (tmp->type != STMT_LABEL)
674 continue;
675 if (!tmp->label_identifier ||
676 tmp->label_identifier->type != SYM_LABEL ||
677 !tmp->label_identifier->ident)
678 continue;
679 label_name = tmp->label_identifier->ident->name;
680 if (strcmp(goto_name, label_name) != 0)
681 continue;
682 found = 1;
684 __split_stmt(tmp);
685 } END_FOR_EACH_PTR(tmp);
688 static void fake_a_return(void)
690 struct symbol *return_type;
692 nullify_path();
693 __unnullify_path();
695 return_type = get_real_base_type(cur_func_sym);
696 return_type = get_real_base_type(return_type);
697 if (return_type != &void_ctype) {
698 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
699 nullify_path();
702 __pass_to_client(cur_func_sym, END_FUNC_HOOK);
703 __pass_to_client(cur_func_sym, AFTER_FUNC_HOOK);
706 void __split_stmt(struct statement *stmt)
708 sval_t sval;
710 if (!stmt)
711 goto out;
713 if (__bail_on_rest_of_function || out_of_memory() || taking_too_long()) {
714 static char *printed = NULL;
716 __bail_on_rest_of_function = 1;
717 if (printed != cur_func)
718 sm_msg("Function too hairy. Giving up.");
719 fake_a_return();
720 final_pass = 0; /* turn off sm_msg() from here */
721 printed = cur_func;
722 return;
725 add_ptr_list(&big_statement_stack, stmt);
726 free_expression_stack(&big_expression_stack);
727 set_position(stmt->pos);
728 __pass_to_client(stmt, STMT_HOOK);
730 switch (stmt->type) {
731 case STMT_DECLARATION:
732 split_declaration(stmt->declaration);
733 break;
734 case STMT_RETURN:
735 __split_expr(stmt->ret_value);
736 __pass_to_client(stmt->ret_value, RETURN_HOOK);
737 __process_post_op_stack();
738 nullify_path();
739 break;
740 case STMT_EXPRESSION:
741 __split_expr(stmt->expression);
742 break;
743 case STMT_COMPOUND: {
744 struct statement *tmp;
746 __push_scope_hooks();
747 FOR_EACH_PTR(stmt->stmts, tmp) {
748 __split_stmt(tmp);
749 } END_FOR_EACH_PTR(tmp);
750 __call_scope_hooks();
751 break;
753 case STMT_IF:
754 if (known_condition_true(stmt->if_conditional)) {
755 __split_stmt(stmt->if_true);
756 break;
758 if (known_condition_false(stmt->if_conditional)) {
759 __split_stmt(stmt->if_false);
760 break;
762 if (option_known_conditions &&
763 implied_condition_true(stmt->if_conditional)) {
764 sm_info("this condition is true.");
765 __split_stmt(stmt->if_true);
766 break;
768 if (option_known_conditions &&
769 implied_condition_false(stmt->if_conditional)) {
770 sm_info("this condition is false.");
771 __split_stmt(stmt->if_false);
772 break;
774 __split_whole_condition(stmt->if_conditional);
775 __split_stmt(stmt->if_true);
776 if (empty_statement(stmt->if_true) &&
777 last_stmt_on_same_line() &&
778 !get_macro_name(stmt->if_true->pos))
779 sm_msg("warn: if();");
780 __push_true_states();
781 __use_false_states();
782 __split_stmt(stmt->if_false);
783 __merge_true_states();
784 break;
785 case STMT_ITERATOR:
786 if (stmt->iterator_pre_condition)
787 handle_pre_loop(stmt);
788 else if (stmt->iterator_post_condition)
789 handle_post_loop(stmt);
790 else {
791 // these are for(;;) type loops.
792 handle_pre_loop(stmt);
794 break;
795 case STMT_SWITCH:
796 if (get_value(stmt->switch_expression, &sval)) {
797 split_known_switch(stmt, sval);
798 break;
800 __split_expr(stmt->switch_expression);
801 push_expression(&switch_expr_stack, stmt->switch_expression);
802 __save_switch_states(top_expression(switch_expr_stack));
803 nullify_path();
804 __push_default();
805 __push_breaks();
806 __split_stmt(stmt->switch_statement);
807 if (!__pop_default())
808 __merge_switches(top_expression(switch_expr_stack),
809 NULL);
810 __discard_switches();
811 __merge_breaks();
812 pop_expression(&switch_expr_stack);
813 break;
814 case STMT_CASE:
815 __merge_switches(top_expression(switch_expr_stack),
816 stmt->case_expression);
817 __pass_case_to_client(top_expression(switch_expr_stack),
818 stmt->case_expression);
819 if (!stmt->case_expression)
820 __set_default();
821 __split_expr(stmt->case_expression);
822 __split_expr(stmt->case_to);
823 __split_stmt(stmt->case_statement);
824 break;
825 case STMT_LABEL:
826 if (stmt->label_identifier &&
827 stmt->label_identifier->type == SYM_LABEL &&
828 stmt->label_identifier->ident) {
829 loop_count |= 0x80000000;
830 __merge_gotos(stmt->label_identifier->ident->name);
832 __split_stmt(stmt->label_statement);
833 break;
834 case STMT_GOTO:
835 __split_expr(stmt->goto_expression);
836 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
837 if (!strcmp(stmt->goto_label->ident->name, "break")) {
838 __process_breaks();
839 } else if (!strcmp(stmt->goto_label->ident->name,
840 "continue")) {
841 __process_continues();
843 } else if (stmt->goto_label &&
844 stmt->goto_label->type == SYM_LABEL &&
845 stmt->goto_label->ident) {
846 __save_gotos(stmt->goto_label->ident->name);
848 nullify_path();
849 if (is_last_stmt(stmt))
850 handle_backward_goto(stmt);
851 break;
852 case STMT_NONE:
853 break;
854 case STMT_ASM:
855 __pass_to_client(stmt, ASM_HOOK);
856 __split_expr(stmt->asm_string);
857 split_asm_constraints(stmt->asm_outputs);
858 split_asm_constraints(stmt->asm_inputs);
859 split_asm_constraints(stmt->asm_clobbers);
860 break;
861 case STMT_CONTEXT:
862 break;
863 case STMT_RANGE:
864 __split_expr(stmt->range_expression);
865 __split_expr(stmt->range_low);
866 __split_expr(stmt->range_high);
867 break;
869 __pass_to_client(stmt, STMT_HOOK_AFTER);
870 out:
871 __process_post_op_stack();
874 static void split_expr_list(struct expression_list *expr_list)
876 struct expression *expr;
878 FOR_EACH_PTR(expr_list, expr) {
879 __split_expr(expr);
880 __process_post_op_stack();
881 } END_FOR_EACH_PTR(expr);
884 static void split_sym(struct symbol *sym)
886 if (!sym)
887 return;
888 if (!(sym->namespace & NS_SYMBOL))
889 return;
891 __split_stmt(sym->stmt);
892 __split_expr(sym->array_size);
893 split_symlist(sym->arguments);
894 split_symlist(sym->symbol_list);
895 __split_stmt(sym->inline_stmt);
896 split_symlist(sym->inline_symbol_list);
899 static void split_symlist(struct symbol_list *sym_list)
901 struct symbol *sym;
903 FOR_EACH_PTR(sym_list, sym) {
904 split_sym(sym);
905 } END_FOR_EACH_PTR(sym);
908 typedef void (fake_cb)(struct expression *expr);
910 static int member_to_number(struct expression *expr, struct ident *member)
912 struct symbol *type, *tmp;
913 char *name;
914 int i;
916 if (!member)
917 return -1;
918 name = member->name;
920 type = get_type(expr);
921 if (!type || type->type != SYM_STRUCT)
922 return -1;
924 i = -1;
925 FOR_EACH_PTR(type->symbol_list, tmp) {
926 i++;
927 if (!tmp->ident)
928 continue;
929 if (strcmp(name, tmp->ident->name) == 0)
930 return i;
931 } END_FOR_EACH_PTR(tmp);
932 return -1;
935 static struct ident *number_to_member(struct expression *expr, int num)
937 struct symbol *type, *member;
938 int i = 0;
940 type = get_type(expr);
941 if (!type || type->type != SYM_STRUCT)
942 return NULL;
944 FOR_EACH_PTR(type->symbol_list, member) {
945 if (i == num)
946 return member->ident;
947 i++;
948 } END_FOR_EACH_PTR(member);
949 return NULL;
952 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
954 struct member_set {
955 struct ident *ident;
956 int set;
959 static struct member_set *alloc_member_set(struct symbol *type)
961 struct member_set *member_set;
962 struct symbol *member;
963 int member_count;
964 int member_idx;
966 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
967 member_set = malloc(member_count * sizeof(*member_set));
968 member_idx = 0;
969 FOR_EACH_PTR(type->symbol_list, member) {
970 member_set[member_idx].ident = member->ident;
971 member_set[member_idx].set = 0;
972 member_idx++;
973 } END_FOR_EACH_PTR(member);
975 return member_set;
978 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
980 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
981 int i;
983 for (i = 0; i < member_count; i++) {
984 if (member_set[i].ident == ident) {
985 member_set[i].set = 1;
986 return;
989 // crap. this is buggy.
990 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
993 static void set_unset_to_zero(struct expression *symbol, struct symbol *type, struct member_set *member_set)
995 struct expression *deref, *assign;
996 struct symbol *member, *member_type;
997 int member_idx;
999 member_idx = 0;
1000 FOR_EACH_PTR(type->symbol_list, member) {
1001 if (!member->ident || member_set[member_idx].set) {
1002 member_idx++;
1003 continue;
1005 member_type = get_real_base_type(member);
1006 if (!member_type || member_type->type == SYM_ARRAY) {
1007 member_idx++;
1008 continue;
1010 /* TODO: this should be handled recursively and not ignored */
1011 if (member_type->type == SYM_STRUCT || member_type->type == SYM_UNION) {
1012 member_idx++;
1013 continue;
1015 deref = member_expression(symbol, '.', member->ident);
1016 assign = assign_expression(deref, zero_expr());
1017 __split_expr(assign);
1018 member_idx++;
1019 } END_FOR_EACH_PTR(member);
1023 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1025 struct expression *deref, *assign, *tmp;
1026 struct symbol *struct_type, *type;
1027 struct ident *member;
1028 int member_idx;
1029 struct member_set *member_set;
1031 struct_type = get_type(symbol);
1032 if (!struct_type ||
1033 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1034 return;
1036 member_set = alloc_member_set(struct_type);
1038 member_idx = 0;
1039 FOR_EACH_PTR(members, tmp) {
1040 member = number_to_member(symbol, member_idx);
1041 while (tmp->type == EXPR_IDENTIFIER) {
1042 member = tmp->expr_ident;
1043 member_idx = member_to_number(symbol, member);
1044 tmp = tmp->ident_expression;
1046 mark_member_as_set(struct_type, member_set, member);
1047 member_idx++;
1048 deref = member_expression(symbol, '.', member);
1049 if (tmp->type == EXPR_INITIALIZER) {
1050 type = get_type(deref);
1051 if (type && type->type == SYM_ARRAY)
1052 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1053 else
1054 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1055 } else {
1056 assign = assign_expression(deref, tmp);
1057 fake_cb(assign);
1059 } END_FOR_EACH_PTR(tmp);
1061 set_unset_to_zero(symbol, struct_type, member_set);
1064 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1066 fake_member_assigns_helper(symbol_expression(sym),
1067 sym->initializer->expr_list, fake_cb);
1070 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1072 struct expression *offset, *binop, *assign, *tmp;
1073 struct symbol *type;
1074 int idx;
1076 idx = 0;
1077 FOR_EACH_PTR(expr_list, tmp) {
1078 if (tmp->type == EXPR_INDEX) {
1079 if (tmp->idx_from != tmp->idx_to)
1080 return;
1081 idx = tmp->idx_from;
1082 if (!tmp->idx_expression)
1083 goto next;
1084 tmp = tmp->idx_expression;
1086 offset = value_expr(idx);
1087 binop = array_element_expression(array, offset);
1088 if (tmp->type == EXPR_INITIALIZER) {
1089 type = get_type(binop);
1090 if (type && type->type == SYM_ARRAY)
1091 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1092 else
1093 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1094 } else {
1095 assign = assign_expression(binop, tmp);
1096 fake_cb(assign);
1098 next:
1099 idx++;
1100 } END_FOR_EACH_PTR(tmp);
1103 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1105 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1108 static void fake_assign_expr(struct symbol *sym)
1110 struct expression *assign, *symbol;
1112 symbol = symbol_expression(sym);
1113 assign = assign_expression(symbol, sym->initializer);
1114 __split_expr(assign);
1117 static void call_split_expr(struct expression *expr)
1119 __split_expr(expr);
1122 static void do_initializer_stuff(struct symbol *sym)
1124 if (!sym->initializer)
1125 return;
1127 if (sym->initializer->type == EXPR_INITIALIZER) {
1128 if (get_real_base_type(sym)->type == SYM_ARRAY)
1129 fake_element_assigns(sym, call_split_expr);
1130 else
1131 fake_member_assigns(sym, call_split_expr);
1132 } else {
1133 fake_assign_expr(sym);
1137 static void split_declaration(struct symbol_list *sym_list)
1139 struct symbol *sym;
1141 FOR_EACH_PTR(sym_list, sym) {
1142 __pass_to_client(sym, DECLARATION_HOOK);
1143 do_initializer_stuff(sym);
1144 split_sym(sym);
1145 } END_FOR_EACH_PTR(sym);
1148 static void call_global_assign_hooks(struct expression *assign)
1150 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1153 static void fake_global_assign(struct symbol *sym)
1155 struct expression *assign, *symbol;
1157 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1158 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1159 fake_element_assigns(sym, call_global_assign_hooks);
1160 } else if (sym->initializer) {
1161 symbol = symbol_expression(sym);
1162 assign = assign_expression(symbol, sym->initializer);
1163 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1164 } else {
1165 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1167 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1168 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1169 fake_member_assigns(sym, call_global_assign_hooks);
1170 } else if (sym->initializer) {
1171 symbol = symbol_expression(sym);
1172 assign = assign_expression(symbol, sym->initializer);
1173 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1174 } else {
1175 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1177 } else {
1178 symbol = symbol_expression(sym);
1179 if (sym->initializer)
1180 assign = assign_expression(symbol, sym->initializer);
1181 else
1182 assign = assign_expression(symbol, zero_expr());
1183 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1187 static void start_function_definition(struct symbol *sym)
1189 __in_function_def = 1;
1190 __pass_to_client(sym, FUNC_DEF_HOOK);
1191 __in_function_def = 0;
1192 __pass_to_client(sym, AFTER_DEF_HOOK);
1196 static void split_function(struct symbol *sym)
1198 struct symbol *base_type = get_base_type(sym);
1200 if (!base_type->stmt && !base_type->inline_stmt)
1201 return;
1203 gettimeofday(&fn_start_time, NULL);
1204 cur_func_sym = sym;
1205 if (sym->ident)
1206 cur_func = sym->ident->name;
1207 __smatch_lineno = sym->pos.line;
1208 loop_count = 0;
1209 last_goto_statement_handled = 0;
1210 sm_debug("new function: %s\n", cur_func);
1211 __stree_id = 0;
1212 if (option_two_passes) {
1213 __unnullify_path();
1214 loop_num = 0;
1215 final_pass = 0;
1216 start_function_definition(sym);
1217 __split_stmt(base_type->stmt);
1218 __split_stmt(base_type->inline_stmt);
1219 nullify_path();
1221 __unnullify_path();
1222 loop_num = 0;
1223 final_pass = 1;
1224 start_function_definition(sym);
1225 __split_stmt(base_type->stmt);
1226 __split_stmt(base_type->inline_stmt);
1227 __pass_to_client(sym, END_FUNC_HOOK);
1228 __pass_to_client(sym, AFTER_FUNC_HOOK);
1230 cur_func_sym = NULL;
1231 cur_func = NULL;
1232 clear_all_states();
1233 free_data_info_allocs();
1234 free_expression_stack(&switch_expr_stack);
1235 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1236 __bail_on_rest_of_function = 0;
1239 static void parse_inline(struct expression *call)
1241 struct symbol *base_type;
1242 int loop_num_bak = loop_num;
1243 int final_pass_bak = final_pass;
1244 char *cur_func_bak = cur_func;
1245 struct statement_list *big_statement_stack_bak = big_statement_stack;
1246 struct expression_list *big_expression_stack_bak = big_expression_stack;
1247 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1248 struct symbol *cur_func_sym_bak = cur_func_sym;
1250 __pass_to_client(call, INLINE_FN_START);
1251 final_pass = 0; /* don't print anything */
1252 __inline_fn = call;
1254 base_type = get_base_type(call->fn->symbol);
1255 cur_func_sym = call->fn->symbol;
1256 if (call->fn->symbol->ident)
1257 cur_func = call->fn->symbol->ident->name;
1258 else
1259 cur_func = NULL;
1260 set_position(call->fn->symbol->pos);
1262 save_all_states();
1263 big_statement_stack = NULL;
1264 big_expression_stack = NULL;
1265 switch_expr_stack = NULL;
1267 sm_debug("inline function: %s\n", cur_func);
1268 __unnullify_path();
1269 loop_num = 0;
1270 start_function_definition(call->fn->symbol);
1271 __split_stmt(base_type->stmt);
1272 __split_stmt(base_type->inline_stmt);
1273 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1274 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1276 free_expression_stack(&switch_expr_stack);
1277 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1278 nullify_path();
1279 free_goto_stack();
1281 loop_num = loop_num_bak;
1282 final_pass = final_pass_bak;
1283 cur_func_sym = cur_func_sym_bak;
1284 cur_func = cur_func_bak;
1285 big_statement_stack = big_statement_stack_bak;
1286 big_expression_stack = big_expression_stack_bak;
1287 switch_expr_stack = switch_expr_stack_bak;
1289 restore_all_states();
1290 set_position(call->pos);
1291 __inline_fn = NULL;
1292 __pass_to_client(call, INLINE_FN_END);
1295 static struct symbol_list *inlines_called;
1296 static void add_inline_function(struct symbol *sym)
1298 static struct symbol_list *already_added;
1299 struct symbol *tmp;
1301 FOR_EACH_PTR(already_added, tmp) {
1302 if (tmp == sym)
1303 return;
1304 } END_FOR_EACH_PTR(tmp);
1306 add_ptr_list(&already_added, sym);
1307 add_ptr_list(&inlines_called, sym);
1310 static void process_inlines(void)
1312 struct symbol *tmp;
1314 FOR_EACH_PTR(inlines_called, tmp) {
1315 split_function(tmp);
1316 } END_FOR_EACH_PTR(tmp);
1317 free_ptr_list(&inlines_called);
1320 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1322 struct symbol *sym;
1324 FOR_EACH_PTR_REVERSE(big_list, sym) {
1325 if (!sym->scope)
1326 continue;
1327 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1328 return sym;
1329 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1330 return sym;
1331 } END_FOR_EACH_PTR_REVERSE(sym);
1333 return NULL;
1336 static void split_inlines_in_scope(struct symbol *sym)
1338 struct symbol *base;
1339 struct symbol_list *scope_list;
1340 int stream;
1342 scope_list = sym->scope->symbols;
1343 stream = sym->pos.stream;
1345 /* find the last static symbol in the file */
1346 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1347 if (sym->pos.stream != stream)
1348 continue;
1349 if (sym->type != SYM_NODE)
1350 continue;
1351 base = get_base_type(sym);
1352 if (!base)
1353 continue;
1354 if (base->type != SYM_FN)
1355 continue;
1356 if (!base->inline_stmt)
1357 continue;
1358 add_inline_function(sym);
1359 } END_FOR_EACH_PTR_REVERSE(sym);
1361 process_inlines();
1364 static void split_inlines(struct symbol_list *sym_list)
1366 struct symbol *sym;
1368 sym = get_last_scoped_symbol(sym_list, 0);
1369 if (sym)
1370 split_inlines_in_scope(sym);
1371 sym = get_last_scoped_symbol(sym_list, 1);
1372 if (sym)
1373 split_inlines_in_scope(sym);
1376 static struct stree *clone_estates_perm(struct stree *orig)
1378 struct stree *ret = NULL;
1379 struct sm_state *tmp;
1381 FOR_EACH_SM(orig, tmp) {
1382 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1383 } END_FOR_EACH_SM(tmp);
1385 return ret;
1388 static void split_functions(struct symbol_list *sym_list)
1390 struct symbol *sym;
1392 __unnullify_path();
1393 FOR_EACH_PTR(sym_list, sym) {
1394 set_position(sym->pos);
1395 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1396 __pass_to_client(sym, BASE_HOOK);
1397 fake_global_assign(sym);
1399 } END_FOR_EACH_PTR(sym);
1400 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1401 nullify_path();
1403 FOR_EACH_PTR(sym_list, sym) {
1404 set_position(sym->pos);
1405 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1406 split_function(sym);
1407 process_inlines();
1409 } END_FOR_EACH_PTR(sym);
1410 split_inlines(sym_list);
1411 __pass_to_client(sym_list, END_FILE_HOOK);
1414 void smatch(int argc, char **argv)
1417 struct string_list *filelist = NULL;
1418 struct symbol_list *sym_list;
1420 if (argc < 2) {
1421 printf("Usage: smatch [--debug] <filename.c>\n");
1422 exit(1);
1424 sparse_initialize(argc, argv, &filelist);
1425 set_valid_ptr_max();
1426 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1427 if (option_file_output) {
1428 char buf[256];
1430 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1431 sm_outfd = fopen(buf, "w");
1432 if (!sm_outfd) {
1433 printf("Error: Cannot open %s\n", base_file);
1434 exit(1);
1437 sym_list = sparse_keep_tokens(base_file);
1438 split_functions(sym_list);
1439 } END_FOR_EACH_PTR_NOTAG(base_file);