math: remove the get_implied_value_low_overhead() function
[smatch.git] / smatch_flow.c
blob808a7bdb8281f1673b0a85a5185ca1cdab06eadc
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 __in_fake_struct_assign;
30 int in_fake_env;
31 int final_pass;
32 int __inline_call;
33 struct expression *__inline_fn;
35 static int __smatch_lineno = 0;
37 static char *base_file;
38 static const char *filename;
39 static char *pathname;
40 static char *full_filename;
41 static char *full_base_file;
42 static char *cur_func;
43 static unsigned int loop_count;
44 static int last_goto_statement_handled;
45 int __expr_stmt_count;
46 int __in_function_def;
47 static struct expression_list *switch_expr_stack = NULL;
48 static struct expression_list *post_op_stack = NULL;
50 static struct ptr_list *backup;
52 struct expression_list *big_expression_stack;
53 struct statement_list *big_statement_stack;
54 struct statement *__prev_stmt;
55 struct statement *__cur_stmt;
56 struct statement *__next_stmt;
57 int __in_pre_condition = 0;
58 int __bail_on_rest_of_function = 0;
59 static struct timeval fn_start_time;
60 static struct timeval outer_fn_start_time;
61 char *get_function(void) { return cur_func; }
62 int get_lineno(void) { return __smatch_lineno; }
63 int inside_loop(void) { return !!loop_count; }
64 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
65 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
66 int in_expression_statement(void) { return !!__expr_stmt_count; }
68 static void split_symlist(struct symbol_list *sym_list);
69 static void split_declaration(struct symbol_list *sym_list);
70 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
71 static void add_inline_function(struct symbol *sym);
72 static void parse_inline(struct expression *expr);
74 int option_assume_loops = 0;
75 int option_two_passes = 0;
76 struct symbol *cur_func_sym = NULL;
77 struct stree *global_states;
79 const unsigned long valid_ptr_min = 4096;
80 const unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
81 const sval_t valid_ptr_min_sval = {
82 .type = &ptr_ctype,
83 {.value = 4096},
85 const sval_t valid_ptr_max_sval = {
86 .type = &ptr_ctype,
87 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
89 struct range_list *valid_ptr_rl;
91 static void alloc_valid_ptr_rl(void)
93 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
94 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
95 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
98 int outside_of_function(void)
100 return cur_func_sym == NULL;
103 const char *get_filename(void)
105 if (option_info && option_full_path)
106 return full_base_file;
107 if (option_info)
108 return base_file;
109 if (option_full_path)
110 return full_filename;
111 return filename;
114 const char *get_base_file(void)
116 if (option_full_path)
117 return full_base_file;
118 return base_file;
121 static void set_position(struct position pos)
123 int len;
124 static int prev_stream = -1;
126 if (in_fake_env)
127 return;
129 if (pos.stream == 0 && pos.line == 0)
130 return;
132 __smatch_lineno = pos.line;
134 if (pos.stream == prev_stream)
135 return;
137 filename = stream_name(pos.stream);
139 free(full_filename);
140 pathname = getcwd(NULL, 0);
141 if (pathname) {
142 len = strlen(pathname) + 1 + strlen(filename) + 1;
143 full_filename = malloc(len);
144 snprintf(full_filename, len, "%s/%s", pathname, filename);
145 } else {
146 full_filename = alloc_string(filename);
148 free(pathname);
151 int is_assigned_call(struct expression *expr)
153 struct expression *parent = expr_get_parent_expr(expr);
155 if (parent &&
156 parent->type == EXPR_ASSIGNMENT &&
157 parent->op == '=' &&
158 strip_expr(parent->right) == expr)
159 return 1;
161 return 0;
164 static int is_inline_func(struct expression *expr)
166 if (expr->type != EXPR_SYMBOL || !expr->symbol)
167 return 0;
168 if (expr->symbol->ctype.modifiers & MOD_INLINE)
169 return 1;
170 return 0;
173 static int is_noreturn_func(struct expression *expr)
175 if (expr->type != EXPR_SYMBOL || !expr->symbol)
176 return 0;
177 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
178 return 1;
179 return 0;
182 static int inline_budget = 20;
184 int inlinable(struct expression *expr)
186 struct symbol *sym;
187 struct statement *last_stmt = NULL;
189 if (__inline_fn) /* don't nest */
190 return 0;
192 if (expr->type != EXPR_SYMBOL || !expr->symbol)
193 return 0;
194 if (is_no_inline_function(expr->symbol->ident->name))
195 return 0;
196 sym = get_base_type(expr->symbol);
197 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
198 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
199 return 0;
200 if (sym->stmt->type != STMT_COMPOUND)
201 return 0;
202 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
204 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
205 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
206 return 0;
207 if (sym->inline_stmt->type != STMT_COMPOUND)
208 return 0;
209 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
212 if (!last_stmt)
213 return 0;
215 /* the magic numbers in this function are pulled out of my bum. */
216 if (last_stmt->pos.line > sym->pos.line + inline_budget)
217 return 0;
219 return 1;
222 void __process_post_op_stack(void)
224 struct expression *expr;
226 FOR_EACH_PTR(post_op_stack, expr) {
227 __pass_to_client(expr, OP_HOOK);
228 } END_FOR_EACH_PTR(expr);
230 __free_ptr_list((struct ptr_list **)&post_op_stack);
233 static int handle_comma_assigns(struct expression *expr)
235 struct expression *right;
236 struct expression *assign;
238 right = strip_expr(expr->right);
239 if (right->type != EXPR_COMMA)
240 return 0;
242 __split_expr(right->left);
243 __process_post_op_stack();
245 assign = assign_expression(expr->left, '=', right->right);
246 __split_expr(assign);
248 return 1;
251 /* This is to handle *p++ = foo; assignments */
252 static int handle_postop_assigns(struct expression *expr)
254 struct expression *left, *fake_left;
255 struct expression *assign;
257 left = strip_expr(expr->left);
258 if (left->type != EXPR_PREOP || left->op != '*')
259 return 0;
260 left = strip_expr(left->unop);
261 if (left->type != EXPR_POSTOP)
262 return 0;
264 fake_left = deref_expression(strip_expr(left->unop));
265 assign = assign_expression(fake_left, '=', expr->right);
267 __split_expr(assign);
268 __split_expr(expr->left);
270 return 1;
273 static int prev_expression_is_getting_address(struct expression *expr)
275 struct expression *parent;
277 do {
278 parent = expr_get_parent_expr(expr);
280 if (!parent)
281 return 0;
282 if (parent->type == EXPR_PREOP && parent->op == '&')
283 return 1;
284 if (parent->type == EXPR_PREOP && parent->op == '(')
285 goto next;
286 if (parent->type == EXPR_DEREF && parent->op == '.')
287 goto next;
289 return 0;
290 next:
291 expr = parent;
292 } while (1);
295 static void handle_builtin_overflow_func(struct expression *expr)
297 struct expression *a, *b, *res, *assign;
298 int op;
300 if (sym_name_is("__builtin_add_overflow", expr->fn))
301 op = '+';
302 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
303 op = '-';
304 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
305 op = '*';
306 else
307 return;
309 a = get_argument_from_call_expr(expr->args, 0);
310 b = get_argument_from_call_expr(expr->args, 1);
311 res = get_argument_from_call_expr(expr->args, 2);
313 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
314 __split_expr(assign);
317 static int handle__builtin_choose_expr(struct expression *expr)
319 struct expression *const_expr, *expr1, *expr2;
320 sval_t sval;
322 if (!sym_name_is("__builtin_choose_expr", expr->fn))
323 return 0;
325 const_expr = get_argument_from_call_expr(expr->args, 0);
326 expr1 = get_argument_from_call_expr(expr->args, 1);
327 expr2 = get_argument_from_call_expr(expr->args, 2);
329 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
330 return 0;
331 if (sval.value)
332 __split_expr(expr1);
333 else
334 __split_expr(expr2);
335 return 1;
338 static int handle__builtin_choose_expr_assigns(struct expression *expr)
340 struct expression *const_expr, *right, *expr1, *expr2, *fake;
341 sval_t sval;
343 right = strip_expr(expr->right);
344 if (right->type != EXPR_CALL)
345 return 0;
346 if (!sym_name_is("__builtin_choose_expr", right->fn))
347 return 0;
349 const_expr = get_argument_from_call_expr(right->args, 0);
350 expr1 = get_argument_from_call_expr(right->args, 1);
351 expr2 = get_argument_from_call_expr(right->args, 2);
353 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
354 return 0;
356 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
357 __split_expr(fake);
358 return 1;
361 void __split_expr(struct expression *expr)
363 if (!expr)
364 return;
366 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
368 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
369 return;
370 if (__in_fake_assign >= 4) /* don't allow too much nesting */
371 return;
373 push_expression(&big_expression_stack, expr);
374 set_position(expr->pos);
375 __pass_to_client(expr, EXPR_HOOK);
377 switch (expr->type) {
378 case EXPR_PREOP:
379 expr_set_parent_expr(expr->unop, expr);
381 if (expr->op == '*' &&
382 !prev_expression_is_getting_address(expr))
383 __pass_to_client(expr, DEREF_HOOK);
384 __split_expr(expr->unop);
385 __pass_to_client(expr, OP_HOOK);
386 break;
387 case EXPR_POSTOP:
388 expr_set_parent_expr(expr->unop, expr);
390 __split_expr(expr->unop);
391 push_expression(&post_op_stack, expr);
392 break;
393 case EXPR_STATEMENT:
394 __expr_stmt_count++;
395 if (expr->statement && !expr->statement) {
396 stmt_set_parent_stmt(expr->statement,
397 last_ptr_list((struct ptr_list *)big_statement_stack));
399 __split_stmt(expr->statement);
400 __expr_stmt_count--;
401 break;
402 case EXPR_LOGICAL:
403 case EXPR_COMPARE:
404 expr_set_parent_expr(expr->left, expr);
405 expr_set_parent_expr(expr->right, expr);
407 __pass_to_client(expr, LOGIC_HOOK);
408 __handle_logic(expr);
409 break;
410 case EXPR_BINOP:
411 expr_set_parent_expr(expr->left, expr);
412 expr_set_parent_expr(expr->right, expr);
414 __pass_to_client(expr, BINOP_HOOK);
415 case EXPR_COMMA:
416 expr_set_parent_expr(expr->left, expr);
417 expr_set_parent_expr(expr->right, expr);
419 __split_expr(expr->left);
420 __process_post_op_stack();
421 __split_expr(expr->right);
422 break;
423 case EXPR_ASSIGNMENT: {
424 struct expression *right;
426 expr_set_parent_expr(expr->left, expr);
427 expr_set_parent_expr(expr->right, expr);
429 right = strip_expr(expr->right);
430 if (!right)
431 break;
433 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
435 /* foo = !bar() */
436 if (__handle_condition_assigns(expr))
437 break;
438 /* foo = (x < 5 ? foo : 5); */
439 if (__handle_select_assigns(expr))
440 break;
441 /* foo = ({frob(); frob(); frob(); 1;}) */
442 if (__handle_expr_statement_assigns(expr))
443 break;
444 /* foo = (3, 4); */
445 if (handle_comma_assigns(expr))
446 break;
447 if (handle_postop_assigns(expr))
448 break;
449 if (handle__builtin_choose_expr_assigns(expr))
450 break;
452 __split_expr(expr->right);
453 if (outside_of_function())
454 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
455 else
456 __pass_to_client(expr, ASSIGNMENT_HOOK);
458 __fake_struct_member_assignments(expr);
460 if (expr->op == '=' && right->type == EXPR_CALL)
461 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
463 if (get_macro_name(right->pos) &&
464 get_macro_name(expr->pos) != get_macro_name(right->pos))
465 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
467 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
469 __split_expr(expr->left);
470 break;
472 case EXPR_DEREF:
473 expr_set_parent_expr(expr->deref, expr);
475 __pass_to_client(expr, DEREF_HOOK);
476 __split_expr(expr->deref);
477 break;
478 case EXPR_SLICE:
479 expr_set_parent_expr(expr->base, expr);
481 __split_expr(expr->base);
482 break;
483 case EXPR_CAST:
484 case EXPR_FORCE_CAST:
485 expr_set_parent_expr(expr->cast_expression, expr);
487 __pass_to_client(expr, CAST_HOOK);
488 __split_expr(expr->cast_expression);
489 break;
490 case EXPR_SIZEOF:
491 if (expr->cast_expression)
492 __pass_to_client(strip_parens(expr->cast_expression),
493 SIZEOF_HOOK);
494 break;
495 case EXPR_OFFSETOF:
496 case EXPR_ALIGNOF:
497 break;
498 case EXPR_CONDITIONAL:
499 case EXPR_SELECT:
500 expr_set_parent_expr(expr->conditional, expr);
501 expr_set_parent_expr(expr->cond_true, expr);
502 expr_set_parent_expr(expr->cond_false, expr);
504 if (known_condition_true(expr->conditional)) {
505 __split_expr(expr->cond_true);
506 break;
508 if (known_condition_false(expr->conditional)) {
509 __split_expr(expr->cond_false);
510 break;
512 __pass_to_client(expr, SELECT_HOOK);
513 __split_whole_condition(expr->conditional);
514 __split_expr(expr->cond_true);
515 __push_true_states();
516 __use_false_states();
517 __split_expr(expr->cond_false);
518 __merge_true_states();
519 break;
520 case EXPR_CALL:
521 expr_set_parent_expr(expr->fn, expr);
523 if (sym_name_is("__builtin_constant_p", expr->fn))
524 break;
525 if (handle__builtin_choose_expr(expr))
526 break;
527 split_expr_list(expr->args, expr);
528 __split_expr(expr->fn);
529 if (is_inline_func(expr->fn))
530 add_inline_function(expr->fn->symbol);
531 if (inlinable(expr->fn))
532 __inline_call = 1;
533 __process_post_op_stack();
534 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
535 __pass_to_client(expr, FUNCTION_CALL_HOOK);
536 __inline_call = 0;
537 if (inlinable(expr->fn)) {
538 parse_inline(expr);
540 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
541 if (is_noreturn_func(expr->fn))
542 nullify_path();
543 handle_builtin_overflow_func(expr);
544 break;
545 case EXPR_INITIALIZER:
546 split_expr_list(expr->expr_list, expr);
547 break;
548 case EXPR_IDENTIFIER:
549 expr_set_parent_expr(expr->ident_expression, expr);
550 __split_expr(expr->ident_expression);
551 break;
552 case EXPR_INDEX:
553 expr_set_parent_expr(expr->idx_expression, expr);
554 __split_expr(expr->idx_expression);
555 break;
556 case EXPR_POS:
557 expr_set_parent_expr(expr->init_expr, expr);
558 __split_expr(expr->init_expr);
559 break;
560 case EXPR_SYMBOL:
561 __pass_to_client(expr, SYM_HOOK);
562 break;
563 case EXPR_STRING:
564 __pass_to_client(expr, STRING_HOOK);
565 break;
566 default:
567 break;
569 pop_expression(&big_expression_stack);
572 static int is_forever_loop(struct statement *stmt)
574 struct expression *expr;
575 sval_t sval;
577 expr = strip_expr(stmt->iterator_pre_condition);
578 if (!expr)
579 expr = stmt->iterator_post_condition;
580 if (!expr) {
581 /* this is a for(;;) loop... */
582 return 1;
585 if (get_value(expr, &sval) && sval.value != 0)
586 return 1;
588 return 0;
591 static int loop_num;
592 static char *get_loop_name(int num)
594 char buf[256];
596 snprintf(buf, 255, "-loop%d", num);
597 buf[255] = '\0';
598 return alloc_sname(buf);
602 * Pre Loops are while and for loops.
604 static void handle_pre_loop(struct statement *stmt)
606 int once_through; /* we go through the loop at least once */
607 struct sm_state *extra_sm = NULL;
608 int unchanged = 0;
609 char *loop_name;
610 struct stree *stree = NULL;
611 struct sm_state *sm = NULL;
613 loop_name = get_loop_name(loop_num);
614 loop_num++;
616 __split_stmt(stmt->iterator_pre_statement);
617 __prev_stmt = stmt->iterator_pre_statement;
619 once_through = implied_condition_true(stmt->iterator_pre_condition);
621 loop_count++;
622 __push_continues();
623 __push_breaks();
625 __merge_gotos(loop_name, NULL);
627 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
628 __in_pre_condition++;
629 __pass_to_client(stmt, PRELOOP_HOOK);
630 __split_whole_condition(stmt->iterator_pre_condition);
631 __in_pre_condition--;
632 FOR_EACH_SM(stree, sm) {
633 set_state(sm->owner, sm->name, sm->sym, sm->state);
634 } END_FOR_EACH_SM(sm);
635 free_stree(&stree);
636 if (extra_sm)
637 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
639 if (option_assume_loops)
640 once_through = 1;
642 __split_stmt(stmt->iterator_statement);
643 if (is_forever_loop(stmt)) {
644 __merge_continues();
645 __save_gotos(loop_name, NULL);
647 __push_fake_cur_stree();
648 __split_stmt(stmt->iterator_post_statement);
649 stree = __pop_fake_cur_stree();
651 __discard_false_states();
652 __use_breaks();
654 if (!__path_is_null())
655 __merge_stree_into_cur(stree);
656 free_stree(&stree);
657 } else {
658 __merge_continues();
659 unchanged = __iterator_unchanged(extra_sm);
660 __split_stmt(stmt->iterator_post_statement);
661 __prev_stmt = stmt->iterator_post_statement;
662 __cur_stmt = stmt;
664 __save_gotos(loop_name, NULL);
665 __in_pre_condition++;
666 __split_whole_condition(stmt->iterator_pre_condition);
667 __in_pre_condition--;
668 nullify_path();
669 __merge_false_states();
670 if (once_through)
671 __discard_false_states();
672 else
673 __merge_false_states();
675 if (extra_sm && unchanged)
676 __extra_pre_loop_hook_after(extra_sm,
677 stmt->iterator_post_statement,
678 stmt->iterator_pre_condition);
679 __merge_breaks();
681 loop_count--;
685 * Post loops are do {} while();
687 static void handle_post_loop(struct statement *stmt)
689 char *loop_name;
691 loop_name = get_loop_name(loop_num);
692 loop_num++;
693 loop_count++;
695 __push_continues();
696 __push_breaks();
697 __merge_gotos(loop_name, NULL);
698 __split_stmt(stmt->iterator_statement);
699 __merge_continues();
700 if (!is_zero(stmt->iterator_post_condition))
701 __save_gotos(loop_name, NULL);
703 if (is_forever_loop(stmt)) {
704 __use_breaks();
705 } else {
706 __split_whole_condition(stmt->iterator_post_condition);
707 __use_false_states();
708 __merge_breaks();
710 loop_count--;
713 static int empty_statement(struct statement *stmt)
715 if (!stmt)
716 return 0;
717 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
718 return 1;
719 return 0;
722 static int last_stmt_on_same_line(void)
724 struct statement *stmt;
725 int i = 0;
727 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
728 if (!i++)
729 continue;
730 if (stmt->pos.line == get_lineno())
731 return 1;
732 return 0;
733 } END_FOR_EACH_PTR_REVERSE(stmt);
734 return 0;
737 static void split_asm_constraints(struct expression_list *expr_list)
739 struct expression *expr;
740 int state = 0;
742 FOR_EACH_PTR(expr_list, expr) {
743 switch (state) {
744 case 0: /* identifier */
745 case 1: /* constraint */
746 state++;
747 continue;
748 case 2: /* expression */
749 state = 0;
750 __split_expr(expr);
751 continue;
753 } END_FOR_EACH_PTR(expr);
756 static int is_case_val(struct statement *stmt, sval_t sval)
758 sval_t case_sval;
760 if (stmt->type != STMT_CASE)
761 return 0;
762 if (!stmt->case_expression) {
763 __set_default();
764 return 1;
766 if (!get_value(stmt->case_expression, &case_sval))
767 return 0;
768 if (case_sval.value == sval.value)
769 return 1;
770 return 0;
773 static struct range_list *get_case_rl(struct expression *switch_expr,
774 struct expression *case_expr,
775 struct expression *case_to)
777 sval_t start, end;
778 struct range_list *rl = NULL;
779 struct symbol *switch_type;
781 switch_type = get_type(switch_expr);
782 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
783 start = sval_cast(switch_type, start);
784 end = sval_cast(switch_type, end);
785 add_range(&rl, start, end);
786 } else if (get_value(case_expr, &start)) {
787 start = sval_cast(switch_type, start);
788 add_range(&rl, start, start);
791 return rl;
794 static void split_known_switch(struct statement *stmt, sval_t sval)
796 struct statement *tmp;
797 struct range_list *rl;
799 __split_expr(stmt->switch_expression);
800 sval = sval_cast(get_type(stmt->switch_expression), sval);
802 push_expression(&switch_expr_stack, stmt->switch_expression);
803 __save_switch_states(top_expression(switch_expr_stack));
804 nullify_path();
805 __push_default();
806 __push_breaks();
808 stmt = stmt->switch_statement;
810 __push_scope_hooks();
811 FOR_EACH_PTR(stmt->stmts, tmp) {
812 __smatch_lineno = tmp->pos.line;
813 if (is_case_val(tmp, sval)) {
814 rl = alloc_rl(sval, sval);
815 __merge_switches(top_expression(switch_expr_stack), rl);
816 __pass_case_to_client(top_expression(switch_expr_stack), rl);
818 if (__path_is_null())
819 continue;
820 __split_stmt(tmp);
821 if (__path_is_null()) {
822 __set_default();
823 goto out;
825 } END_FOR_EACH_PTR(tmp);
826 out:
827 __call_scope_hooks();
828 if (!__pop_default())
829 __merge_switches(top_expression(switch_expr_stack), NULL);
830 __discard_switches();
831 __merge_breaks();
832 pop_expression(&switch_expr_stack);
835 static void split_case(struct statement *stmt)
837 struct range_list *rl = NULL;
839 expr_set_parent_stmt(stmt->case_expression, stmt);
840 expr_set_parent_stmt(stmt->case_to, stmt);
842 rl = get_case_rl(top_expression(switch_expr_stack),
843 stmt->case_expression, stmt->case_to);
844 while (stmt->case_statement->type == STMT_CASE) {
845 struct range_list *tmp;
847 tmp = get_case_rl(top_expression(switch_expr_stack),
848 stmt->case_statement->case_expression,
849 stmt->case_statement->case_to);
850 if (!tmp)
851 break;
852 rl = rl_union(rl, tmp);
853 if (!stmt->case_expression)
854 __set_default();
855 stmt = stmt->case_statement;
858 __merge_switches(top_expression(switch_expr_stack), rl);
860 if (!stmt->case_expression)
861 __set_default();
862 __split_stmt(stmt->case_statement);
865 int time_parsing_function(void)
867 return ms_since(&fn_start_time) / 1000;
870 static int taking_too_long(void)
872 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
873 return 1;
874 return 0;
877 static int is_last_stmt(struct statement *cur_stmt)
879 struct symbol *fn;
880 struct statement *stmt;
882 if (!cur_func_sym)
883 return 0;
884 fn = get_base_type(cur_func_sym);
885 if (!fn)
886 return 0;
887 stmt = fn->stmt;
888 if (!stmt)
889 stmt = fn->inline_stmt;
890 if (!stmt || stmt->type != STMT_COMPOUND)
891 return 0;
892 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
893 if (stmt && stmt->type == STMT_LABEL)
894 stmt = stmt->label_statement;
895 if (stmt == cur_stmt)
896 return 1;
897 return 0;
900 static void handle_backward_goto(struct statement *goto_stmt)
902 const char *goto_name, *label_name;
903 struct statement *func_stmt;
904 struct symbol *base_type = get_base_type(cur_func_sym);
905 struct statement *tmp;
906 int found = 0;
908 if (!option_info)
909 return;
910 if (last_goto_statement_handled)
911 return;
912 last_goto_statement_handled = 1;
914 if (!goto_stmt->goto_label ||
915 goto_stmt->goto_label->type != SYM_LABEL ||
916 !goto_stmt->goto_label->ident)
917 return;
918 goto_name = goto_stmt->goto_label->ident->name;
920 func_stmt = base_type->stmt;
921 if (!func_stmt)
922 func_stmt = base_type->inline_stmt;
923 if (!func_stmt)
924 return;
925 if (func_stmt->type != STMT_COMPOUND)
926 return;
928 FOR_EACH_PTR(func_stmt->stmts, tmp) {
929 if (!found) {
930 if (tmp->type != STMT_LABEL)
931 continue;
932 if (!tmp->label_identifier ||
933 tmp->label_identifier->type != SYM_LABEL ||
934 !tmp->label_identifier->ident)
935 continue;
936 label_name = tmp->label_identifier->ident->name;
937 if (strcmp(goto_name, label_name) != 0)
938 continue;
939 found = 1;
941 __split_stmt(tmp);
942 } END_FOR_EACH_PTR(tmp);
945 static void fake_a_return(void)
947 struct symbol *return_type;
949 nullify_path();
950 __unnullify_path();
952 return_type = get_real_base_type(cur_func_sym);
953 return_type = get_real_base_type(return_type);
954 if (return_type != &void_ctype) {
955 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
956 nullify_path();
960 static void fake_an_empty_default(struct position pos)
962 static struct statement none = {};
964 none.pos = pos;
965 none.type = STMT_NONE;
966 __merge_switches(top_expression(switch_expr_stack), NULL);
967 __split_stmt(&none);
970 static void split_compound(struct statement *stmt)
972 struct statement *prev = NULL;
973 struct statement *cur = NULL;
974 struct statement *next;
976 __push_scope_hooks();
978 FOR_EACH_PTR(stmt->stmts, next) {
979 /* just set them all ahead of time */
980 stmt_set_parent_stmt(next, stmt);
982 if (cur) {
983 __prev_stmt = prev;
984 __next_stmt = next;
985 __cur_stmt = cur;
986 __split_stmt(cur);
988 prev = cur;
989 cur = next;
990 } END_FOR_EACH_PTR(next);
991 if (cur) {
992 __prev_stmt = prev;
993 __cur_stmt = cur;
994 __next_stmt = NULL;
995 __split_stmt(cur);
999 * For function scope, then delay calling the scope hooks until the
1000 * end of function hooks can run. I'm not positive this is the right
1001 * thing...
1003 if (!is_last_stmt(cur))
1004 __call_scope_hooks();
1008 * This is a hack, work around for detecting empty functions.
1010 static int need_delayed_scope_hooks(void)
1012 struct symbol *fn = get_base_type(cur_func_sym);
1013 struct statement *stmt;
1015 if (!fn)
1016 return 0;
1017 stmt = fn->stmt;
1018 if (!stmt)
1019 stmt = fn->inline_stmt;
1020 if (stmt && stmt->type == STMT_COMPOUND)
1021 return 1;
1022 return 0;
1025 void __split_label_stmt(struct statement *stmt)
1027 if (stmt->label_identifier &&
1028 stmt->label_identifier->type == SYM_LABEL &&
1029 stmt->label_identifier->ident) {
1030 loop_count |= 0x0800000;
1031 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1035 static void find_asm_gotos(struct statement *stmt)
1037 struct symbol *sym;
1039 FOR_EACH_PTR(stmt->asm_labels, sym) {
1040 __save_gotos(sym->ident->name, sym);
1041 } END_FOR_EACH_PTR(sym);
1044 void __split_stmt(struct statement *stmt)
1046 sval_t sval;
1048 if (!stmt)
1049 goto out;
1051 if (!__in_fake_assign)
1052 __silence_warnings_for_stmt = false;
1054 if (__bail_on_rest_of_function || is_skipped_function())
1055 return;
1057 if (out_of_memory() || taking_too_long()) {
1058 struct timeval stop;
1060 gettimeofday(&stop, NULL);
1062 __bail_on_rest_of_function = 1;
1063 final_pass = 1;
1064 sm_perror("Function too hairy. Giving up. %lu seconds",
1065 stop.tv_sec - fn_start_time.tv_sec);
1066 fake_a_return();
1067 final_pass = 0; /* turn off sm_msg() from here */
1068 return;
1071 add_ptr_list(&big_statement_stack, stmt);
1072 free_expression_stack(&big_expression_stack);
1073 set_position(stmt->pos);
1074 __pass_to_client(stmt, STMT_HOOK);
1076 switch (stmt->type) {
1077 case STMT_DECLARATION:
1078 split_declaration(stmt->declaration);
1079 break;
1080 case STMT_RETURN:
1081 expr_set_parent_stmt(stmt->ret_value, stmt);
1083 __split_expr(stmt->ret_value);
1084 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1085 __process_post_op_stack();
1086 nullify_path();
1087 break;
1088 case STMT_EXPRESSION:
1089 expr_set_parent_stmt(stmt->expression, stmt);
1090 expr_set_parent_stmt(stmt->context, stmt);
1092 __split_expr(stmt->expression);
1093 break;
1094 case STMT_COMPOUND:
1095 split_compound(stmt);
1096 break;
1097 case STMT_IF:
1098 stmt_set_parent_stmt(stmt->if_true, stmt);
1099 stmt_set_parent_stmt(stmt->if_false, stmt);
1100 expr_set_parent_stmt(stmt->if_conditional, stmt);
1102 if (known_condition_true(stmt->if_conditional)) {
1103 __split_stmt(stmt->if_true);
1104 break;
1106 if (known_condition_false(stmt->if_conditional)) {
1107 __split_stmt(stmt->if_false);
1108 break;
1110 __split_whole_condition(stmt->if_conditional);
1111 __split_stmt(stmt->if_true);
1112 if (empty_statement(stmt->if_true) &&
1113 last_stmt_on_same_line() &&
1114 !get_macro_name(stmt->if_true->pos))
1115 sm_warning("if();");
1116 __push_true_states();
1117 __use_false_states();
1118 __split_stmt(stmt->if_false);
1119 __merge_true_states();
1120 break;
1121 case STMT_ITERATOR:
1122 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1123 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1124 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1125 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1126 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1128 if (stmt->iterator_pre_condition)
1129 handle_pre_loop(stmt);
1130 else if (stmt->iterator_post_condition)
1131 handle_post_loop(stmt);
1132 else {
1133 // these are for(;;) type loops.
1134 handle_pre_loop(stmt);
1136 break;
1137 case STMT_SWITCH:
1138 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1139 expr_set_parent_stmt(stmt->switch_expression, stmt);
1141 if (get_value(stmt->switch_expression, &sval)) {
1142 split_known_switch(stmt, sval);
1143 break;
1145 __split_expr(stmt->switch_expression);
1146 push_expression(&switch_expr_stack, stmt->switch_expression);
1147 __save_switch_states(top_expression(switch_expr_stack));
1148 nullify_path();
1149 __push_default();
1150 __push_breaks();
1151 __split_stmt(stmt->switch_statement);
1152 if (!__pop_default() && have_remaining_cases())
1153 fake_an_empty_default(stmt->pos);
1154 __discard_switches();
1155 __merge_breaks();
1156 pop_expression(&switch_expr_stack);
1157 break;
1158 case STMT_CASE:
1159 split_case(stmt);
1160 break;
1161 case STMT_LABEL:
1162 __split_label_stmt(stmt);
1163 __split_stmt(stmt->label_statement);
1164 break;
1165 case STMT_GOTO:
1166 expr_set_parent_stmt(stmt->goto_expression, stmt);
1168 __split_expr(stmt->goto_expression);
1169 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1170 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1171 __process_breaks();
1172 } else if (!strcmp(stmt->goto_label->ident->name,
1173 "continue")) {
1174 __process_continues();
1176 } else if (stmt->goto_label &&
1177 stmt->goto_label->type == SYM_LABEL &&
1178 stmt->goto_label->ident) {
1179 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1181 nullify_path();
1182 if (is_last_stmt(stmt))
1183 handle_backward_goto(stmt);
1184 break;
1185 case STMT_NONE:
1186 break;
1187 case STMT_ASM:
1188 expr_set_parent_stmt(stmt->asm_string, stmt);
1190 find_asm_gotos(stmt);
1191 __pass_to_client(stmt, ASM_HOOK);
1192 __split_expr(stmt->asm_string);
1193 split_asm_constraints(stmt->asm_outputs);
1194 split_asm_constraints(stmt->asm_inputs);
1195 split_asm_constraints(stmt->asm_clobbers);
1196 break;
1197 case STMT_CONTEXT:
1198 break;
1199 case STMT_RANGE:
1200 __split_expr(stmt->range_expression);
1201 __split_expr(stmt->range_low);
1202 __split_expr(stmt->range_high);
1203 break;
1205 __pass_to_client(stmt, STMT_HOOK_AFTER);
1206 out:
1207 __process_post_op_stack();
1210 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1212 struct expression *expr;
1214 FOR_EACH_PTR(expr_list, expr) {
1215 expr_set_parent_expr(expr, parent);
1216 __split_expr(expr);
1217 __process_post_op_stack();
1218 } END_FOR_EACH_PTR(expr);
1221 static void split_sym(struct symbol *sym)
1223 if (!sym)
1224 return;
1225 if (!(sym->namespace & NS_SYMBOL))
1226 return;
1228 __split_stmt(sym->stmt);
1229 __split_expr(sym->array_size);
1230 split_symlist(sym->arguments);
1231 split_symlist(sym->symbol_list);
1232 __split_stmt(sym->inline_stmt);
1233 split_symlist(sym->inline_symbol_list);
1236 static void split_symlist(struct symbol_list *sym_list)
1238 struct symbol *sym;
1240 FOR_EACH_PTR(sym_list, sym) {
1241 split_sym(sym);
1242 } END_FOR_EACH_PTR(sym);
1245 typedef void (fake_cb)(struct expression *expr);
1247 static int member_to_number(struct expression *expr, struct ident *member)
1249 struct symbol *type, *tmp;
1250 char *name;
1251 int i;
1253 if (!member)
1254 return -1;
1255 name = member->name;
1257 type = get_type(expr);
1258 if (!type || type->type != SYM_STRUCT)
1259 return -1;
1261 i = -1;
1262 FOR_EACH_PTR(type->symbol_list, tmp) {
1263 i++;
1264 if (!tmp->ident)
1265 continue;
1266 if (strcmp(name, tmp->ident->name) == 0)
1267 return i;
1268 } END_FOR_EACH_PTR(tmp);
1269 return -1;
1272 static struct ident *number_to_member(struct expression *expr, int num)
1274 struct symbol *type, *member;
1275 int i = 0;
1277 type = get_type(expr);
1278 if (!type || type->type != SYM_STRUCT)
1279 return NULL;
1281 FOR_EACH_PTR(type->symbol_list, member) {
1282 if (i == num)
1283 return member->ident;
1284 i++;
1285 } END_FOR_EACH_PTR(member);
1286 return NULL;
1289 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1291 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1293 struct expression *edge_member, *assign;
1294 struct symbol *base = get_real_base_type(member);
1295 struct symbol *tmp;
1297 if (member->ident)
1298 expr = member_expression(expr, '.', member->ident);
1300 FOR_EACH_PTR(base->symbol_list, tmp) {
1301 struct symbol *type;
1303 type = get_real_base_type(tmp);
1304 if (!type)
1305 continue;
1307 edge_member = member_expression(expr, '.', tmp->ident);
1308 if (get_extra_state(edge_member))
1309 continue;
1311 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1312 set_inner_struct_members(expr, tmp);
1313 continue;
1316 if (!tmp->ident)
1317 continue;
1319 assign = assign_expression(edge_member, '=', zero_expr());
1320 __split_expr(assign);
1321 } END_FOR_EACH_PTR(tmp);
1326 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1328 struct symbol *tmp;
1329 struct expression *member = NULL;
1330 struct expression *assign;
1331 int op = '*';
1333 if (expr->type == EXPR_PREOP && expr->op == '&') {
1334 expr = strip_expr(expr->unop);
1335 op = '.';
1338 FOR_EACH_PTR(type->symbol_list, tmp) {
1339 type = get_real_base_type(tmp);
1340 if (!type)
1341 continue;
1343 if (tmp->ident) {
1344 member = member_expression(expr, op, tmp->ident);
1345 if (get_extra_state(member))
1346 continue;
1349 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1350 set_inner_struct_members(expr, tmp);
1351 continue;
1353 if (type->type == SYM_ARRAY)
1354 continue;
1355 if (!tmp->ident)
1356 continue;
1358 assign = assign_expression(member, '=', zero_expr());
1359 __split_expr(assign);
1360 } END_FOR_EACH_PTR(tmp);
1363 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1365 struct expression *deref, *assign, *tmp, *right;
1366 struct symbol *struct_type, *type;
1367 struct ident *member;
1368 int member_idx;
1370 struct_type = get_type(symbol);
1371 if (!struct_type ||
1372 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1373 return;
1376 * We're parsing an initializer that could look something like this:
1377 * struct foo foo = {
1378 * 42,
1379 * .whatever.xxx = 11,
1380 * .zzz = 12,
1381 * };
1383 * So what we have here is a list with 42, .whatever, and .zzz. We need
1384 * to break it up into left and right sides of the assignments.
1387 member_idx = 0;
1388 FOR_EACH_PTR(members, tmp) {
1389 deref = NULL;
1390 if (tmp->type == EXPR_IDENTIFIER) {
1391 member_idx = member_to_number(symbol, tmp->expr_ident);
1392 while (tmp->type == EXPR_IDENTIFIER) {
1393 member = tmp->expr_ident;
1394 tmp = tmp->ident_expression;
1395 if (deref)
1396 deref = member_expression(deref, '.', member);
1397 else
1398 deref = member_expression(symbol, '.', member);
1400 } else {
1401 member = number_to_member(symbol, member_idx);
1402 deref = member_expression(symbol, '.', member);
1404 right = tmp;
1405 member_idx++;
1406 if (right->type == EXPR_INITIALIZER) {
1407 type = get_type(deref);
1408 if (type && type->type == SYM_ARRAY)
1409 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1410 else
1411 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1412 } else {
1413 assign = assign_expression(deref, '=', right);
1414 fake_cb(assign);
1416 } END_FOR_EACH_PTR(tmp);
1418 set_unset_to_zero(struct_type, symbol);
1421 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1423 fake_member_assigns_helper(symbol_expression(sym),
1424 sym->initializer->expr_list, fake_cb);
1427 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1429 struct expression *offset, *binop, *assign, *tmp;
1430 struct symbol *type;
1431 int idx;
1433 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1434 return;
1436 idx = 0;
1437 FOR_EACH_PTR(expr_list, tmp) {
1438 if (tmp->type == EXPR_INDEX) {
1439 if (tmp->idx_from != tmp->idx_to)
1440 return;
1441 idx = tmp->idx_from;
1442 if (!tmp->idx_expression)
1443 goto next;
1444 tmp = tmp->idx_expression;
1446 offset = value_expr(idx);
1447 binop = array_element_expression(array, offset);
1448 if (tmp->type == EXPR_INITIALIZER) {
1449 type = get_type(binop);
1450 if (type && type->type == SYM_ARRAY)
1451 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1452 else
1453 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1454 } else {
1455 assign = assign_expression(binop, '=', tmp);
1456 fake_cb(assign);
1458 next:
1459 idx++;
1460 } END_FOR_EACH_PTR(tmp);
1463 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1465 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1468 static void fake_assign_expr(struct symbol *sym)
1470 struct expression *assign, *symbol;
1472 symbol = symbol_expression(sym);
1473 assign = assign_expression(symbol, '=', sym->initializer);
1474 __split_expr(assign);
1477 static void do_initializer_stuff(struct symbol *sym)
1479 if (!sym->initializer)
1480 return;
1482 if (sym->initializer->type == EXPR_INITIALIZER) {
1483 if (get_real_base_type(sym)->type == SYM_ARRAY)
1484 fake_element_assigns(sym, __split_expr);
1485 else
1486 fake_member_assigns(sym, __split_expr);
1487 } else {
1488 fake_assign_expr(sym);
1492 static void split_declaration(struct symbol_list *sym_list)
1494 struct symbol *sym;
1496 FOR_EACH_PTR(sym_list, sym) {
1497 __pass_to_client(sym, DECLARATION_HOOK);
1498 do_initializer_stuff(sym);
1499 split_sym(sym);
1500 } END_FOR_EACH_PTR(sym);
1503 static void call_global_assign_hooks(struct expression *assign)
1505 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1508 static void fake_global_assign(struct symbol *sym)
1510 struct expression *assign, *symbol;
1512 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1513 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1514 fake_element_assigns(sym, call_global_assign_hooks);
1515 } else if (sym->initializer) {
1516 symbol = symbol_expression(sym);
1517 assign = assign_expression(symbol, '=', sym->initializer);
1518 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1519 } else {
1520 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1522 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1523 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1524 fake_member_assigns(sym, call_global_assign_hooks);
1525 } else if (sym->initializer) {
1526 symbol = symbol_expression(sym);
1527 assign = assign_expression(symbol, '=', sym->initializer);
1528 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1529 } else {
1530 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1532 } else {
1533 symbol = symbol_expression(sym);
1534 if (sym->initializer) {
1535 assign = assign_expression(symbol, '=', sym->initializer);
1536 __split_expr(assign);
1537 } else {
1538 assign = assign_expression(symbol, '=', zero_expr());
1540 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1544 static void start_function_definition(struct symbol *sym)
1546 __in_function_def = 1;
1547 __pass_to_client(sym, FUNC_DEF_HOOK);
1548 __in_function_def = 0;
1549 __pass_to_client(sym, AFTER_DEF_HOOK);
1553 static void split_function(struct symbol *sym)
1555 struct symbol *base_type = get_base_type(sym);
1556 struct timeval stop;
1558 if (!base_type->stmt && !base_type->inline_stmt)
1559 return;
1561 gettimeofday(&outer_fn_start_time, NULL);
1562 gettimeofday(&fn_start_time, NULL);
1563 cur_func_sym = sym;
1564 if (sym->ident)
1565 cur_func = sym->ident->name;
1566 set_position(sym->pos);
1567 loop_count = 0;
1568 last_goto_statement_handled = 0;
1569 sm_debug("new function: %s\n", cur_func);
1570 __stree_id = 0;
1571 if (option_two_passes) {
1572 __unnullify_path();
1573 loop_num = 0;
1574 final_pass = 0;
1575 start_function_definition(sym);
1576 __split_stmt(base_type->stmt);
1577 __split_stmt(base_type->inline_stmt);
1578 nullify_path();
1580 __unnullify_path();
1581 loop_num = 0;
1582 final_pass = 1;
1583 start_function_definition(sym);
1584 __split_stmt(base_type->stmt);
1585 __split_stmt(base_type->inline_stmt);
1586 __pass_to_client(sym, END_FUNC_HOOK);
1587 if (need_delayed_scope_hooks())
1588 __call_scope_hooks();
1589 __pass_to_client(sym, AFTER_FUNC_HOOK);
1591 clear_all_states();
1593 gettimeofday(&stop, NULL);
1594 if (option_time && stop.tv_sec - fn_start_time.tv_sec > 2) {
1595 final_pass++;
1596 sm_msg("func_time: %lu", stop.tv_sec - fn_start_time.tv_sec);
1597 final_pass--;
1599 cur_func_sym = NULL;
1600 cur_func = NULL;
1601 free_data_info_allocs();
1602 free_expression_stack(&switch_expr_stack);
1603 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1604 __bail_on_rest_of_function = 0;
1607 static void save_flow_state(void)
1609 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1610 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1611 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1613 __add_ptr_list(&backup, big_statement_stack, 0);
1614 __add_ptr_list(&backup, big_expression_stack, 0);
1615 __add_ptr_list(&backup, big_condition_stack, 0);
1616 __add_ptr_list(&backup, switch_expr_stack, 0);
1618 __add_ptr_list(&backup, cur_func_sym, 0);
1620 __add_ptr_list(&backup, __prev_stmt, 0);
1621 __add_ptr_list(&backup, __cur_stmt, 0);
1622 __add_ptr_list(&backup, __next_stmt, 0);
1626 static void *pop_backup(void)
1628 void *ret;
1630 ret = last_ptr_list(backup);
1631 delete_ptr_list_last(&backup);
1632 return ret;
1635 static void restore_flow_state(void)
1637 __next_stmt = pop_backup();
1638 __cur_stmt = pop_backup();
1639 __prev_stmt = pop_backup();
1641 cur_func_sym = pop_backup();
1642 switch_expr_stack = pop_backup();
1643 big_condition_stack = pop_backup();
1644 big_expression_stack = pop_backup();
1645 big_statement_stack = pop_backup();
1646 final_pass = PTR_INT(pop_backup()) >> 2;
1647 loop_count = PTR_INT(pop_backup()) >> 2;
1648 loop_num = PTR_INT(pop_backup()) >> 2;
1651 static void parse_inline(struct expression *call)
1653 struct symbol *base_type;
1654 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1655 struct timeval time_backup = fn_start_time;
1656 struct expression *orig_inline = __inline_fn;
1657 int orig_budget;
1659 if (out_of_memory() || taking_too_long())
1660 return;
1662 save_flow_state();
1664 __pass_to_client(call, INLINE_FN_START);
1665 final_pass = 0; /* don't print anything */
1666 __inline_fn = call;
1667 orig_budget = inline_budget;
1668 inline_budget = inline_budget - 5;
1670 base_type = get_base_type(call->fn->symbol);
1671 cur_func_sym = call->fn->symbol;
1672 if (call->fn->symbol->ident)
1673 cur_func = call->fn->symbol->ident->name;
1674 else
1675 cur_func = NULL;
1676 set_position(call->fn->symbol->pos);
1678 save_all_states();
1679 big_statement_stack = NULL;
1680 big_expression_stack = NULL;
1681 big_condition_stack = NULL;
1682 switch_expr_stack = NULL;
1684 sm_debug("inline function: %s\n", cur_func);
1685 __unnullify_path();
1686 loop_num = 0;
1687 loop_count = 0;
1688 start_function_definition(call->fn->symbol);
1689 __split_stmt(base_type->stmt);
1690 __split_stmt(base_type->inline_stmt);
1691 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1692 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1694 free_expression_stack(&switch_expr_stack);
1695 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1696 nullify_path();
1697 free_goto_stack();
1699 restore_flow_state();
1700 fn_start_time = time_backup;
1701 cur_func = cur_func_bak;
1703 restore_all_states();
1704 set_position(call->pos);
1705 __inline_fn = orig_inline;
1706 inline_budget = orig_budget;
1707 __pass_to_client(call, INLINE_FN_END);
1710 static struct symbol_list *inlines_called;
1711 static void add_inline_function(struct symbol *sym)
1713 static struct symbol_list *already_added;
1714 struct symbol *tmp;
1716 FOR_EACH_PTR(already_added, tmp) {
1717 if (tmp == sym)
1718 return;
1719 } END_FOR_EACH_PTR(tmp);
1721 add_ptr_list(&already_added, sym);
1722 add_ptr_list(&inlines_called, sym);
1725 static void process_inlines(void)
1727 struct symbol *tmp;
1729 FOR_EACH_PTR(inlines_called, tmp) {
1730 split_function(tmp);
1731 } END_FOR_EACH_PTR(tmp);
1732 free_ptr_list(&inlines_called);
1735 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1737 struct symbol *sym;
1739 FOR_EACH_PTR_REVERSE(big_list, sym) {
1740 if (!sym->scope)
1741 continue;
1742 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1743 return sym;
1744 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1745 return sym;
1746 } END_FOR_EACH_PTR_REVERSE(sym);
1748 return NULL;
1751 static bool interesting_function(struct symbol *sym)
1753 static int prev_stream = -1;
1754 static bool prev_answer;
1755 const char *filename;
1756 int len;
1758 if (!(sym->ctype.modifiers & MOD_INLINE))
1759 return true;
1761 if (sym->pos.stream == prev_stream)
1762 return prev_answer;
1764 prev_stream = sym->pos.stream;
1765 prev_answer = false;
1767 filename = stream_name(sym->pos.stream);
1768 len = strlen(filename);
1769 if (len > 0 && filename[len - 1] == 'c')
1770 prev_answer = true;
1771 return prev_answer;
1774 static void split_inlines_in_scope(struct symbol *sym)
1776 struct symbol *base;
1777 struct symbol_list *scope_list;
1778 int stream;
1780 scope_list = sym->scope->symbols;
1781 stream = sym->pos.stream;
1783 /* find the last static symbol in the file */
1784 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1785 if (sym->pos.stream != stream)
1786 continue;
1787 if (sym->type != SYM_NODE)
1788 continue;
1789 base = get_base_type(sym);
1790 if (!base)
1791 continue;
1792 if (base->type != SYM_FN)
1793 continue;
1794 if (!base->inline_stmt)
1795 continue;
1796 if (!interesting_function(sym))
1797 continue;
1798 add_inline_function(sym);
1799 } END_FOR_EACH_PTR_REVERSE(sym);
1801 process_inlines();
1804 static void split_inlines(struct symbol_list *sym_list)
1806 struct symbol *sym;
1808 sym = get_last_scoped_symbol(sym_list, 0);
1809 if (sym)
1810 split_inlines_in_scope(sym);
1811 sym = get_last_scoped_symbol(sym_list, 1);
1812 if (sym)
1813 split_inlines_in_scope(sym);
1816 static struct stree *clone_estates_perm(struct stree *orig)
1818 struct stree *ret = NULL;
1819 struct sm_state *tmp;
1821 FOR_EACH_SM(orig, tmp) {
1822 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1823 } END_FOR_EACH_SM(tmp);
1825 return ret;
1828 struct position last_pos;
1829 static void split_c_file_functions(struct symbol_list *sym_list)
1831 struct symbol *sym;
1833 __unnullify_path();
1834 FOR_EACH_PTR(sym_list, sym) {
1835 set_position(sym->pos);
1836 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1837 __pass_to_client(sym, BASE_HOOK);
1838 fake_global_assign(sym);
1840 } END_FOR_EACH_PTR(sym);
1841 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1842 nullify_path();
1844 FOR_EACH_PTR(sym_list, sym) {
1845 set_position(sym->pos);
1846 last_pos = sym->pos;
1847 if (!interesting_function(sym))
1848 continue;
1849 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1850 split_function(sym);
1851 process_inlines();
1853 last_pos = sym->pos;
1854 } END_FOR_EACH_PTR(sym);
1855 split_inlines(sym_list);
1856 __pass_to_client(sym_list, END_FILE_HOOK);
1859 static int final_before_fake;
1860 void init_fake_env(void)
1862 if (!in_fake_env)
1863 final_before_fake = final_pass;
1864 in_fake_env++;
1865 __push_fake_cur_stree();
1866 final_pass = 0;
1869 void end_fake_env(void)
1871 __pop_fake_cur_stree();
1872 in_fake_env--;
1873 if (!in_fake_env)
1874 final_pass = final_before_fake;
1877 static void open_output_files(char *base_file)
1879 char buf[256];
1881 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1882 sm_outfd = fopen(buf, "w");
1883 if (!sm_outfd)
1884 sm_fatal("Cannot open %s", buf);
1886 if (!option_info)
1887 return;
1889 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1890 sql_outfd = fopen(buf, "w");
1891 if (!sql_outfd)
1892 sm_fatal("Error: Cannot open %s", buf);
1894 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1895 caller_info_fd = fopen(buf, "w");
1896 if (!caller_info_fd)
1897 sm_fatal("Error: Cannot open %s", buf);
1900 void smatch(int argc, char **argv)
1902 struct string_list *filelist = NULL;
1903 struct symbol_list *sym_list;
1904 struct timeval stop, start;
1905 char *path;
1906 int len;
1908 gettimeofday(&start, NULL);
1910 sparse_initialize(argc, argv, &filelist);
1911 alloc_valid_ptr_rl();
1912 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1913 path = getcwd(NULL, 0);
1914 free(full_base_file);
1915 if (path) {
1916 len = strlen(path) + 1 + strlen(base_file) + 1;
1917 full_base_file = malloc(len);
1918 snprintf(full_base_file, len, "%s/%s", path, base_file);
1919 } else {
1920 full_base_file = alloc_string(base_file);
1922 if (option_file_output)
1923 open_output_files(base_file);
1924 sym_list = sparse_keep_tokens(base_file);
1925 split_c_file_functions(sym_list);
1926 } END_FOR_EACH_PTR_NOTAG(base_file);
1928 gettimeofday(&stop, NULL);
1930 set_position(last_pos);
1931 final_pass = 1;
1932 if (option_time)
1933 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1934 if (option_mem)
1935 sm_msg("mem: %luKb", get_max_memory());