locking: add some more locks
[smatch.git] / smatch_flow.c
blob6edeb736147822c59cdf96a16b7dc178d24be0ed
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 evaluate_expression(expr);
498 break;
499 case EXPR_CONDITIONAL:
500 case EXPR_SELECT:
501 expr_set_parent_expr(expr->conditional, expr);
502 expr_set_parent_expr(expr->cond_true, expr);
503 expr_set_parent_expr(expr->cond_false, expr);
505 if (known_condition_true(expr->conditional)) {
506 __split_expr(expr->cond_true);
507 break;
509 if (known_condition_false(expr->conditional)) {
510 __split_expr(expr->cond_false);
511 break;
513 __pass_to_client(expr, SELECT_HOOK);
514 __split_whole_condition(expr->conditional);
515 __split_expr(expr->cond_true);
516 __push_true_states();
517 __use_false_states();
518 __split_expr(expr->cond_false);
519 __merge_true_states();
520 break;
521 case EXPR_CALL:
522 expr_set_parent_expr(expr->fn, expr);
524 if (sym_name_is("__builtin_constant_p", expr->fn))
525 break;
526 if (handle__builtin_choose_expr(expr))
527 break;
528 split_expr_list(expr->args, expr);
529 __split_expr(expr->fn);
530 if (is_inline_func(expr->fn))
531 add_inline_function(expr->fn->symbol);
532 if (inlinable(expr->fn))
533 __inline_call = 1;
534 __process_post_op_stack();
535 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
536 __pass_to_client(expr, FUNCTION_CALL_HOOK);
537 __inline_call = 0;
538 if (inlinable(expr->fn)) {
539 parse_inline(expr);
541 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
542 if (is_noreturn_func(expr->fn))
543 nullify_path();
544 handle_builtin_overflow_func(expr);
545 break;
546 case EXPR_INITIALIZER:
547 split_expr_list(expr->expr_list, expr);
548 break;
549 case EXPR_IDENTIFIER:
550 expr_set_parent_expr(expr->ident_expression, expr);
551 __split_expr(expr->ident_expression);
552 break;
553 case EXPR_INDEX:
554 expr_set_parent_expr(expr->idx_expression, expr);
555 __split_expr(expr->idx_expression);
556 break;
557 case EXPR_POS:
558 expr_set_parent_expr(expr->init_expr, expr);
559 __split_expr(expr->init_expr);
560 break;
561 case EXPR_SYMBOL:
562 __pass_to_client(expr, SYM_HOOK);
563 break;
564 case EXPR_STRING:
565 __pass_to_client(expr, STRING_HOOK);
566 break;
567 default:
568 break;
570 pop_expression(&big_expression_stack);
573 static int is_forever_loop(struct statement *stmt)
575 struct expression *expr;
576 sval_t sval;
578 expr = strip_expr(stmt->iterator_pre_condition);
579 if (!expr)
580 expr = stmt->iterator_post_condition;
581 if (!expr) {
582 /* this is a for(;;) loop... */
583 return 1;
586 if (get_value(expr, &sval) && sval.value != 0)
587 return 1;
589 return 0;
592 static int loop_num;
593 static char *get_loop_name(int num)
595 char buf[256];
597 snprintf(buf, 255, "-loop%d", num);
598 buf[255] = '\0';
599 return alloc_sname(buf);
603 * Pre Loops are while and for loops.
605 static void handle_pre_loop(struct statement *stmt)
607 int once_through; /* we go through the loop at least once */
608 struct sm_state *extra_sm = NULL;
609 int unchanged = 0;
610 char *loop_name;
611 struct stree *stree = NULL;
612 struct sm_state *sm = NULL;
614 loop_name = get_loop_name(loop_num);
615 loop_num++;
617 __split_stmt(stmt->iterator_pre_statement);
618 __prev_stmt = stmt->iterator_pre_statement;
620 once_through = implied_condition_true(stmt->iterator_pre_condition);
622 loop_count++;
623 __push_continues();
624 __push_breaks();
626 __merge_gotos(loop_name, NULL);
628 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
629 __in_pre_condition++;
630 __pass_to_client(stmt, PRELOOP_HOOK);
631 __split_whole_condition(stmt->iterator_pre_condition);
632 __in_pre_condition--;
633 FOR_EACH_SM(stree, sm) {
634 set_state(sm->owner, sm->name, sm->sym, sm->state);
635 } END_FOR_EACH_SM(sm);
636 free_stree(&stree);
637 if (extra_sm)
638 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
640 if (option_assume_loops)
641 once_through = 1;
643 __split_stmt(stmt->iterator_statement);
644 if (is_forever_loop(stmt)) {
645 __merge_continues();
646 __save_gotos(loop_name, NULL);
648 __push_fake_cur_stree();
649 __split_stmt(stmt->iterator_post_statement);
650 stree = __pop_fake_cur_stree();
652 __discard_false_states();
653 __use_breaks();
655 if (!__path_is_null())
656 __merge_stree_into_cur(stree);
657 free_stree(&stree);
658 } else {
659 __merge_continues();
660 unchanged = __iterator_unchanged(extra_sm);
661 __split_stmt(stmt->iterator_post_statement);
662 __prev_stmt = stmt->iterator_post_statement;
663 __cur_stmt = stmt;
665 __save_gotos(loop_name, NULL);
666 __in_pre_condition++;
667 __split_whole_condition(stmt->iterator_pre_condition);
668 __in_pre_condition--;
669 nullify_path();
670 __merge_false_states();
671 if (once_through)
672 __discard_false_states();
673 else
674 __merge_false_states();
676 if (extra_sm && unchanged)
677 __extra_pre_loop_hook_after(extra_sm,
678 stmt->iterator_post_statement,
679 stmt->iterator_pre_condition);
680 __merge_breaks();
682 loop_count--;
686 * Post loops are do {} while();
688 static void handle_post_loop(struct statement *stmt)
690 char *loop_name;
692 loop_name = get_loop_name(loop_num);
693 loop_num++;
694 loop_count++;
696 __push_continues();
697 __push_breaks();
698 __merge_gotos(loop_name, NULL);
699 __split_stmt(stmt->iterator_statement);
700 __merge_continues();
701 if (!is_zero(stmt->iterator_post_condition))
702 __save_gotos(loop_name, NULL);
704 if (is_forever_loop(stmt)) {
705 __use_breaks();
706 } else {
707 __split_whole_condition(stmt->iterator_post_condition);
708 __use_false_states();
709 __merge_breaks();
711 loop_count--;
714 static int empty_statement(struct statement *stmt)
716 if (!stmt)
717 return 0;
718 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
719 return 1;
720 return 0;
723 static int last_stmt_on_same_line(void)
725 struct statement *stmt;
726 int i = 0;
728 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
729 if (!i++)
730 continue;
731 if (stmt->pos.line == get_lineno())
732 return 1;
733 return 0;
734 } END_FOR_EACH_PTR_REVERSE(stmt);
735 return 0;
738 static void split_asm_constraints(struct expression_list *expr_list)
740 struct expression *expr;
741 int state = 0;
743 FOR_EACH_PTR(expr_list, expr) {
744 switch (state) {
745 case 0: /* identifier */
746 case 1: /* constraint */
747 state++;
748 continue;
749 case 2: /* expression */
750 state = 0;
751 __split_expr(expr);
752 continue;
754 } END_FOR_EACH_PTR(expr);
757 static int is_case_val(struct statement *stmt, sval_t sval)
759 sval_t case_sval;
761 if (stmt->type != STMT_CASE)
762 return 0;
763 if (!stmt->case_expression) {
764 __set_default();
765 return 1;
767 if (!get_value(stmt->case_expression, &case_sval))
768 return 0;
769 if (case_sval.value == sval.value)
770 return 1;
771 return 0;
774 static struct range_list *get_case_rl(struct expression *switch_expr,
775 struct expression *case_expr,
776 struct expression *case_to)
778 sval_t start, end;
779 struct range_list *rl = NULL;
780 struct symbol *switch_type;
782 switch_type = get_type(switch_expr);
783 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
784 start = sval_cast(switch_type, start);
785 end = sval_cast(switch_type, end);
786 add_range(&rl, start, end);
787 } else if (get_value(case_expr, &start)) {
788 start = sval_cast(switch_type, start);
789 add_range(&rl, start, start);
792 return rl;
795 static void split_known_switch(struct statement *stmt, sval_t sval)
797 struct statement *tmp;
798 struct range_list *rl;
800 __split_expr(stmt->switch_expression);
801 sval = sval_cast(get_type(stmt->switch_expression), sval);
803 push_expression(&switch_expr_stack, stmt->switch_expression);
804 __save_switch_states(top_expression(switch_expr_stack));
805 nullify_path();
806 __push_default();
807 __push_breaks();
809 stmt = stmt->switch_statement;
811 __push_scope_hooks();
812 FOR_EACH_PTR(stmt->stmts, tmp) {
813 __smatch_lineno = tmp->pos.line;
814 if (is_case_val(tmp, sval)) {
815 rl = alloc_rl(sval, sval);
816 __merge_switches(top_expression(switch_expr_stack), rl);
817 __pass_case_to_client(top_expression(switch_expr_stack), rl);
819 if (__path_is_null())
820 continue;
821 __split_stmt(tmp);
822 if (__path_is_null()) {
823 __set_default();
824 goto out;
826 } END_FOR_EACH_PTR(tmp);
827 out:
828 __call_scope_hooks();
829 if (!__pop_default())
830 __merge_switches(top_expression(switch_expr_stack), NULL);
831 __discard_switches();
832 __merge_breaks();
833 pop_expression(&switch_expr_stack);
836 static void split_case(struct statement *stmt)
838 struct range_list *rl = NULL;
840 expr_set_parent_stmt(stmt->case_expression, stmt);
841 expr_set_parent_stmt(stmt->case_to, stmt);
843 rl = get_case_rl(top_expression(switch_expr_stack),
844 stmt->case_expression, stmt->case_to);
845 while (stmt->case_statement->type == STMT_CASE) {
846 struct range_list *tmp;
848 tmp = get_case_rl(top_expression(switch_expr_stack),
849 stmt->case_statement->case_expression,
850 stmt->case_statement->case_to);
851 if (!tmp)
852 break;
853 rl = rl_union(rl, tmp);
854 if (!stmt->case_expression)
855 __set_default();
856 stmt = stmt->case_statement;
859 __merge_switches(top_expression(switch_expr_stack), rl);
861 if (!stmt->case_expression)
862 __set_default();
863 __split_stmt(stmt->case_statement);
866 int time_parsing_function(void)
868 return ms_since(&fn_start_time) / 1000;
871 static int taking_too_long(void)
873 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
874 return 1;
875 return 0;
878 static int is_last_stmt(struct statement *cur_stmt)
880 struct symbol *fn;
881 struct statement *stmt;
883 if (!cur_func_sym)
884 return 0;
885 fn = get_base_type(cur_func_sym);
886 if (!fn)
887 return 0;
888 stmt = fn->stmt;
889 if (!stmt)
890 stmt = fn->inline_stmt;
891 if (!stmt || stmt->type != STMT_COMPOUND)
892 return 0;
893 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
894 if (stmt && stmt->type == STMT_LABEL)
895 stmt = stmt->label_statement;
896 if (stmt == cur_stmt)
897 return 1;
898 return 0;
901 static void handle_backward_goto(struct statement *goto_stmt)
903 const char *goto_name, *label_name;
904 struct statement *func_stmt;
905 struct symbol *base_type = get_base_type(cur_func_sym);
906 struct statement *tmp;
907 int found = 0;
909 if (!option_info)
910 return;
911 if (last_goto_statement_handled)
912 return;
913 last_goto_statement_handled = 1;
915 if (!goto_stmt->goto_label ||
916 goto_stmt->goto_label->type != SYM_LABEL ||
917 !goto_stmt->goto_label->ident)
918 return;
919 goto_name = goto_stmt->goto_label->ident->name;
921 func_stmt = base_type->stmt;
922 if (!func_stmt)
923 func_stmt = base_type->inline_stmt;
924 if (!func_stmt)
925 return;
926 if (func_stmt->type != STMT_COMPOUND)
927 return;
929 FOR_EACH_PTR(func_stmt->stmts, tmp) {
930 if (!found) {
931 if (tmp->type != STMT_LABEL)
932 continue;
933 if (!tmp->label_identifier ||
934 tmp->label_identifier->type != SYM_LABEL ||
935 !tmp->label_identifier->ident)
936 continue;
937 label_name = tmp->label_identifier->ident->name;
938 if (strcmp(goto_name, label_name) != 0)
939 continue;
940 found = 1;
942 __split_stmt(tmp);
943 } END_FOR_EACH_PTR(tmp);
946 static void fake_a_return(void)
948 struct symbol *return_type;
950 nullify_path();
951 __unnullify_path();
953 return_type = get_real_base_type(cur_func_sym);
954 return_type = get_real_base_type(return_type);
955 if (return_type != &void_ctype) {
956 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
957 nullify_path();
961 static void fake_an_empty_default(struct position pos)
963 static struct statement none = {};
965 none.pos = pos;
966 none.type = STMT_NONE;
967 __merge_switches(top_expression(switch_expr_stack), NULL);
968 __split_stmt(&none);
971 static void split_compound(struct statement *stmt)
973 struct statement *prev = NULL;
974 struct statement *cur = NULL;
975 struct statement *next;
977 __push_scope_hooks();
979 FOR_EACH_PTR(stmt->stmts, next) {
980 /* just set them all ahead of time */
981 stmt_set_parent_stmt(next, stmt);
983 if (cur) {
984 __prev_stmt = prev;
985 __next_stmt = next;
986 __cur_stmt = cur;
987 __split_stmt(cur);
989 prev = cur;
990 cur = next;
991 } END_FOR_EACH_PTR(next);
992 if (cur) {
993 __prev_stmt = prev;
994 __cur_stmt = cur;
995 __next_stmt = NULL;
996 __split_stmt(cur);
1000 * For function scope, then delay calling the scope hooks until the
1001 * end of function hooks can run. I'm not positive this is the right
1002 * thing...
1004 if (!is_last_stmt(cur))
1005 __call_scope_hooks();
1009 * This is a hack, work around for detecting empty functions.
1011 static int need_delayed_scope_hooks(void)
1013 struct symbol *fn = get_base_type(cur_func_sym);
1014 struct statement *stmt;
1016 if (!fn)
1017 return 0;
1018 stmt = fn->stmt;
1019 if (!stmt)
1020 stmt = fn->inline_stmt;
1021 if (stmt && stmt->type == STMT_COMPOUND)
1022 return 1;
1023 return 0;
1026 void __split_label_stmt(struct statement *stmt)
1028 if (stmt->label_identifier &&
1029 stmt->label_identifier->type == SYM_LABEL &&
1030 stmt->label_identifier->ident) {
1031 loop_count |= 0x0800000;
1032 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1036 static void find_asm_gotos(struct statement *stmt)
1038 struct symbol *sym;
1040 FOR_EACH_PTR(stmt->asm_labels, sym) {
1041 __save_gotos(sym->ident->name, sym);
1042 } END_FOR_EACH_PTR(sym);
1045 void __split_stmt(struct statement *stmt)
1047 sval_t sval;
1049 if (!stmt)
1050 goto out;
1052 if (!__in_fake_assign)
1053 __silence_warnings_for_stmt = false;
1055 if (__bail_on_rest_of_function || is_skipped_function())
1056 return;
1058 if (out_of_memory() || taking_too_long()) {
1059 struct timeval stop;
1061 gettimeofday(&stop, NULL);
1063 __bail_on_rest_of_function = 1;
1064 final_pass = 1;
1065 sm_perror("Function too hairy. Giving up. %lu seconds",
1066 stop.tv_sec - fn_start_time.tv_sec);
1067 fake_a_return();
1068 final_pass = 0; /* turn off sm_msg() from here */
1069 return;
1072 add_ptr_list(&big_statement_stack, stmt);
1073 free_expression_stack(&big_expression_stack);
1074 set_position(stmt->pos);
1075 __pass_to_client(stmt, STMT_HOOK);
1077 switch (stmt->type) {
1078 case STMT_DECLARATION:
1079 split_declaration(stmt->declaration);
1080 break;
1081 case STMT_RETURN:
1082 expr_set_parent_stmt(stmt->ret_value, stmt);
1084 __split_expr(stmt->ret_value);
1085 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1086 __process_post_op_stack();
1087 nullify_path();
1088 break;
1089 case STMT_EXPRESSION:
1090 expr_set_parent_stmt(stmt->expression, stmt);
1091 expr_set_parent_stmt(stmt->context, stmt);
1093 __split_expr(stmt->expression);
1094 break;
1095 case STMT_COMPOUND:
1096 split_compound(stmt);
1097 break;
1098 case STMT_IF:
1099 stmt_set_parent_stmt(stmt->if_true, stmt);
1100 stmt_set_parent_stmt(stmt->if_false, stmt);
1101 expr_set_parent_stmt(stmt->if_conditional, stmt);
1103 if (known_condition_true(stmt->if_conditional)) {
1104 __split_stmt(stmt->if_true);
1105 break;
1107 if (known_condition_false(stmt->if_conditional)) {
1108 __split_stmt(stmt->if_false);
1109 break;
1111 __split_whole_condition(stmt->if_conditional);
1112 __split_stmt(stmt->if_true);
1113 if (empty_statement(stmt->if_true) &&
1114 last_stmt_on_same_line() &&
1115 !get_macro_name(stmt->if_true->pos))
1116 sm_warning("if();");
1117 __push_true_states();
1118 __use_false_states();
1119 __split_stmt(stmt->if_false);
1120 __merge_true_states();
1121 break;
1122 case STMT_ITERATOR:
1123 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1124 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1125 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1126 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1127 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1129 if (stmt->iterator_pre_condition)
1130 handle_pre_loop(stmt);
1131 else if (stmt->iterator_post_condition)
1132 handle_post_loop(stmt);
1133 else {
1134 // these are for(;;) type loops.
1135 handle_pre_loop(stmt);
1137 break;
1138 case STMT_SWITCH:
1139 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1140 expr_set_parent_stmt(stmt->switch_expression, stmt);
1142 if (get_value(stmt->switch_expression, &sval)) {
1143 split_known_switch(stmt, sval);
1144 break;
1146 __split_expr(stmt->switch_expression);
1147 push_expression(&switch_expr_stack, stmt->switch_expression);
1148 __save_switch_states(top_expression(switch_expr_stack));
1149 nullify_path();
1150 __push_default();
1151 __push_breaks();
1152 __split_stmt(stmt->switch_statement);
1153 if (!__pop_default() && have_remaining_cases())
1154 fake_an_empty_default(stmt->pos);
1155 __discard_switches();
1156 __merge_breaks();
1157 pop_expression(&switch_expr_stack);
1158 break;
1159 case STMT_CASE:
1160 split_case(stmt);
1161 break;
1162 case STMT_LABEL:
1163 __split_label_stmt(stmt);
1164 __split_stmt(stmt->label_statement);
1165 break;
1166 case STMT_GOTO:
1167 expr_set_parent_stmt(stmt->goto_expression, stmt);
1169 __split_expr(stmt->goto_expression);
1170 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1171 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1172 __process_breaks();
1173 } else if (!strcmp(stmt->goto_label->ident->name,
1174 "continue")) {
1175 __process_continues();
1177 } else if (stmt->goto_label &&
1178 stmt->goto_label->type == SYM_LABEL &&
1179 stmt->goto_label->ident) {
1180 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1182 nullify_path();
1183 if (is_last_stmt(stmt))
1184 handle_backward_goto(stmt);
1185 break;
1186 case STMT_NONE:
1187 break;
1188 case STMT_ASM:
1189 expr_set_parent_stmt(stmt->asm_string, stmt);
1191 find_asm_gotos(stmt);
1192 __pass_to_client(stmt, ASM_HOOK);
1193 __split_expr(stmt->asm_string);
1194 split_asm_constraints(stmt->asm_outputs);
1195 split_asm_constraints(stmt->asm_inputs);
1196 split_asm_constraints(stmt->asm_clobbers);
1197 break;
1198 case STMT_CONTEXT:
1199 break;
1200 case STMT_RANGE:
1201 __split_expr(stmt->range_expression);
1202 __split_expr(stmt->range_low);
1203 __split_expr(stmt->range_high);
1204 break;
1206 __pass_to_client(stmt, STMT_HOOK_AFTER);
1207 out:
1208 __process_post_op_stack();
1211 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1213 struct expression *expr;
1215 FOR_EACH_PTR(expr_list, expr) {
1216 expr_set_parent_expr(expr, parent);
1217 __split_expr(expr);
1218 __process_post_op_stack();
1219 } END_FOR_EACH_PTR(expr);
1222 static void split_sym(struct symbol *sym)
1224 if (!sym)
1225 return;
1226 if (!(sym->namespace & NS_SYMBOL))
1227 return;
1229 __split_stmt(sym->stmt);
1230 __split_expr(sym->array_size);
1231 split_symlist(sym->arguments);
1232 split_symlist(sym->symbol_list);
1233 __split_stmt(sym->inline_stmt);
1234 split_symlist(sym->inline_symbol_list);
1237 static void split_symlist(struct symbol_list *sym_list)
1239 struct symbol *sym;
1241 FOR_EACH_PTR(sym_list, sym) {
1242 split_sym(sym);
1243 } END_FOR_EACH_PTR(sym);
1246 typedef void (fake_cb)(struct expression *expr);
1248 static int member_to_number(struct expression *expr, struct ident *member)
1250 struct symbol *type, *tmp;
1251 char *name;
1252 int i;
1254 if (!member)
1255 return -1;
1256 name = member->name;
1258 type = get_type(expr);
1259 if (!type || type->type != SYM_STRUCT)
1260 return -1;
1262 i = -1;
1263 FOR_EACH_PTR(type->symbol_list, tmp) {
1264 i++;
1265 if (!tmp->ident)
1266 continue;
1267 if (strcmp(name, tmp->ident->name) == 0)
1268 return i;
1269 } END_FOR_EACH_PTR(tmp);
1270 return -1;
1273 static struct ident *number_to_member(struct expression *expr, int num)
1275 struct symbol *type, *member;
1276 int i = 0;
1278 type = get_type(expr);
1279 if (!type || type->type != SYM_STRUCT)
1280 return NULL;
1282 FOR_EACH_PTR(type->symbol_list, member) {
1283 if (i == num)
1284 return member->ident;
1285 i++;
1286 } END_FOR_EACH_PTR(member);
1287 return NULL;
1290 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1292 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1294 struct expression *edge_member, *assign;
1295 struct symbol *base = get_real_base_type(member);
1296 struct symbol *tmp;
1298 if (member->ident)
1299 expr = member_expression(expr, '.', member->ident);
1301 FOR_EACH_PTR(base->symbol_list, tmp) {
1302 struct symbol *type;
1304 type = get_real_base_type(tmp);
1305 if (!type)
1306 continue;
1308 edge_member = member_expression(expr, '.', tmp->ident);
1309 if (get_extra_state(edge_member))
1310 continue;
1312 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1313 set_inner_struct_members(expr, tmp);
1314 continue;
1317 if (!tmp->ident)
1318 continue;
1320 assign = assign_expression(edge_member, '=', zero_expr());
1321 __split_expr(assign);
1322 } END_FOR_EACH_PTR(tmp);
1327 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1329 struct symbol *tmp;
1330 struct expression *member = NULL;
1331 struct expression *assign;
1332 int op = '*';
1334 if (expr->type == EXPR_PREOP && expr->op == '&') {
1335 expr = strip_expr(expr->unop);
1336 op = '.';
1339 FOR_EACH_PTR(type->symbol_list, tmp) {
1340 type = get_real_base_type(tmp);
1341 if (!type)
1342 continue;
1344 if (tmp->ident) {
1345 member = member_expression(expr, op, tmp->ident);
1346 if (get_extra_state(member))
1347 continue;
1350 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1351 set_inner_struct_members(expr, tmp);
1352 continue;
1354 if (type->type == SYM_ARRAY)
1355 continue;
1356 if (!tmp->ident)
1357 continue;
1359 assign = assign_expression(member, '=', zero_expr());
1360 __split_expr(assign);
1361 } END_FOR_EACH_PTR(tmp);
1364 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1366 struct expression *deref, *assign, *tmp, *right;
1367 struct symbol *struct_type, *type;
1368 struct ident *member;
1369 int member_idx;
1371 struct_type = get_type(symbol);
1372 if (!struct_type ||
1373 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1374 return;
1377 * We're parsing an initializer that could look something like this:
1378 * struct foo foo = {
1379 * 42,
1380 * .whatever.xxx = 11,
1381 * .zzz = 12,
1382 * };
1384 * So what we have here is a list with 42, .whatever, and .zzz. We need
1385 * to break it up into left and right sides of the assignments.
1388 member_idx = 0;
1389 FOR_EACH_PTR(members, tmp) {
1390 deref = NULL;
1391 if (tmp->type == EXPR_IDENTIFIER) {
1392 member_idx = member_to_number(symbol, tmp->expr_ident);
1393 while (tmp->type == EXPR_IDENTIFIER) {
1394 member = tmp->expr_ident;
1395 tmp = tmp->ident_expression;
1396 if (deref)
1397 deref = member_expression(deref, '.', member);
1398 else
1399 deref = member_expression(symbol, '.', member);
1401 } else {
1402 member = number_to_member(symbol, member_idx);
1403 deref = member_expression(symbol, '.', member);
1405 right = tmp;
1406 member_idx++;
1407 if (right->type == EXPR_INITIALIZER) {
1408 type = get_type(deref);
1409 if (type && type->type == SYM_ARRAY)
1410 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1411 else
1412 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1413 } else {
1414 assign = assign_expression(deref, '=', right);
1415 fake_cb(assign);
1417 } END_FOR_EACH_PTR(tmp);
1419 set_unset_to_zero(struct_type, symbol);
1422 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1424 fake_member_assigns_helper(symbol_expression(sym),
1425 sym->initializer->expr_list, fake_cb);
1428 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1430 struct expression *offset, *binop, *assign, *tmp;
1431 struct symbol *type;
1432 int idx;
1434 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1435 return;
1437 idx = 0;
1438 FOR_EACH_PTR(expr_list, tmp) {
1439 if (tmp->type == EXPR_INDEX) {
1440 if (tmp->idx_from != tmp->idx_to)
1441 return;
1442 idx = tmp->idx_from;
1443 if (!tmp->idx_expression)
1444 goto next;
1445 tmp = tmp->idx_expression;
1447 offset = value_expr(idx);
1448 binop = array_element_expression(array, offset);
1449 if (tmp->type == EXPR_INITIALIZER) {
1450 type = get_type(binop);
1451 if (type && type->type == SYM_ARRAY)
1452 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1453 else
1454 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1455 } else {
1456 assign = assign_expression(binop, '=', tmp);
1457 fake_cb(assign);
1459 next:
1460 idx++;
1461 } END_FOR_EACH_PTR(tmp);
1464 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1466 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1469 static void fake_assign_expr(struct symbol *sym)
1471 struct expression *assign, *symbol;
1473 symbol = symbol_expression(sym);
1474 assign = assign_expression(symbol, '=', sym->initializer);
1475 __split_expr(assign);
1478 static void do_initializer_stuff(struct symbol *sym)
1480 if (!sym->initializer)
1481 return;
1483 if (sym->initializer->type == EXPR_INITIALIZER) {
1484 if (get_real_base_type(sym)->type == SYM_ARRAY)
1485 fake_element_assigns(sym, __split_expr);
1486 else
1487 fake_member_assigns(sym, __split_expr);
1488 } else {
1489 fake_assign_expr(sym);
1493 static void split_declaration(struct symbol_list *sym_list)
1495 struct symbol *sym;
1497 FOR_EACH_PTR(sym_list, sym) {
1498 __pass_to_client(sym, DECLARATION_HOOK);
1499 do_initializer_stuff(sym);
1500 split_sym(sym);
1501 } END_FOR_EACH_PTR(sym);
1504 static void call_global_assign_hooks(struct expression *assign)
1506 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1509 static void fake_global_assign(struct symbol *sym)
1511 struct expression *assign, *symbol;
1513 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1514 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1515 fake_element_assigns(sym, call_global_assign_hooks);
1516 } else if (sym->initializer) {
1517 symbol = symbol_expression(sym);
1518 assign = assign_expression(symbol, '=', sym->initializer);
1519 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1520 } else {
1521 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1523 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1524 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1525 fake_member_assigns(sym, call_global_assign_hooks);
1526 } else if (sym->initializer) {
1527 symbol = symbol_expression(sym);
1528 assign = assign_expression(symbol, '=', sym->initializer);
1529 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1530 } else {
1531 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1533 } else {
1534 symbol = symbol_expression(sym);
1535 if (sym->initializer) {
1536 assign = assign_expression(symbol, '=', sym->initializer);
1537 __split_expr(assign);
1538 } else {
1539 assign = assign_expression(symbol, '=', zero_expr());
1541 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1545 static void start_function_definition(struct symbol *sym)
1547 __in_function_def = 1;
1548 __pass_to_client(sym, FUNC_DEF_HOOK);
1549 __in_function_def = 0;
1550 __pass_to_client(sym, AFTER_DEF_HOOK);
1554 static void split_function(struct symbol *sym)
1556 struct symbol *base_type = get_base_type(sym);
1557 struct timeval stop;
1559 if (!base_type->stmt && !base_type->inline_stmt)
1560 return;
1562 gettimeofday(&outer_fn_start_time, NULL);
1563 gettimeofday(&fn_start_time, NULL);
1564 cur_func_sym = sym;
1565 if (sym->ident)
1566 cur_func = sym->ident->name;
1567 set_position(sym->pos);
1568 loop_count = 0;
1569 last_goto_statement_handled = 0;
1570 sm_debug("new function: %s\n", cur_func);
1571 __stree_id = 0;
1572 if (option_two_passes) {
1573 __unnullify_path();
1574 loop_num = 0;
1575 final_pass = 0;
1576 start_function_definition(sym);
1577 __split_stmt(base_type->stmt);
1578 __split_stmt(base_type->inline_stmt);
1579 nullify_path();
1581 __unnullify_path();
1582 loop_num = 0;
1583 final_pass = 1;
1584 start_function_definition(sym);
1585 __split_stmt(base_type->stmt);
1586 __split_stmt(base_type->inline_stmt);
1587 __pass_to_client(sym, END_FUNC_HOOK);
1588 if (need_delayed_scope_hooks())
1589 __call_scope_hooks();
1590 __pass_to_client(sym, AFTER_FUNC_HOOK);
1592 clear_all_states();
1594 gettimeofday(&stop, NULL);
1595 if (option_time && stop.tv_sec - fn_start_time.tv_sec > 2) {
1596 final_pass++;
1597 sm_msg("func_time: %lu", stop.tv_sec - fn_start_time.tv_sec);
1598 final_pass--;
1600 cur_func_sym = NULL;
1601 cur_func = NULL;
1602 free_data_info_allocs();
1603 free_expression_stack(&switch_expr_stack);
1604 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1605 __bail_on_rest_of_function = 0;
1608 static void save_flow_state(void)
1610 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1611 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1612 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1614 __add_ptr_list(&backup, big_statement_stack, 0);
1615 __add_ptr_list(&backup, big_expression_stack, 0);
1616 __add_ptr_list(&backup, big_condition_stack, 0);
1617 __add_ptr_list(&backup, switch_expr_stack, 0);
1619 __add_ptr_list(&backup, cur_func_sym, 0);
1621 __add_ptr_list(&backup, __prev_stmt, 0);
1622 __add_ptr_list(&backup, __cur_stmt, 0);
1623 __add_ptr_list(&backup, __next_stmt, 0);
1627 static void *pop_backup(void)
1629 void *ret;
1631 ret = last_ptr_list(backup);
1632 delete_ptr_list_last(&backup);
1633 return ret;
1636 static void restore_flow_state(void)
1638 __next_stmt = pop_backup();
1639 __cur_stmt = pop_backup();
1640 __prev_stmt = pop_backup();
1642 cur_func_sym = pop_backup();
1643 switch_expr_stack = pop_backup();
1644 big_condition_stack = pop_backup();
1645 big_expression_stack = pop_backup();
1646 big_statement_stack = pop_backup();
1647 final_pass = PTR_INT(pop_backup()) >> 2;
1648 loop_count = PTR_INT(pop_backup()) >> 2;
1649 loop_num = PTR_INT(pop_backup()) >> 2;
1652 static void parse_inline(struct expression *call)
1654 struct symbol *base_type;
1655 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1656 struct timeval time_backup = fn_start_time;
1657 struct expression *orig_inline = __inline_fn;
1658 int orig_budget;
1660 if (out_of_memory() || taking_too_long())
1661 return;
1663 save_flow_state();
1665 __pass_to_client(call, INLINE_FN_START);
1666 final_pass = 0; /* don't print anything */
1667 __inline_fn = call;
1668 orig_budget = inline_budget;
1669 inline_budget = inline_budget - 5;
1671 base_type = get_base_type(call->fn->symbol);
1672 cur_func_sym = call->fn->symbol;
1673 if (call->fn->symbol->ident)
1674 cur_func = call->fn->symbol->ident->name;
1675 else
1676 cur_func = NULL;
1677 set_position(call->fn->symbol->pos);
1679 save_all_states();
1680 big_statement_stack = NULL;
1681 big_expression_stack = NULL;
1682 big_condition_stack = NULL;
1683 switch_expr_stack = NULL;
1685 sm_debug("inline function: %s\n", cur_func);
1686 __unnullify_path();
1687 loop_num = 0;
1688 loop_count = 0;
1689 start_function_definition(call->fn->symbol);
1690 __split_stmt(base_type->stmt);
1691 __split_stmt(base_type->inline_stmt);
1692 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1693 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1695 free_expression_stack(&switch_expr_stack);
1696 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1697 nullify_path();
1698 free_goto_stack();
1700 restore_flow_state();
1701 fn_start_time = time_backup;
1702 cur_func = cur_func_bak;
1704 restore_all_states();
1705 set_position(call->pos);
1706 __inline_fn = orig_inline;
1707 inline_budget = orig_budget;
1708 __pass_to_client(call, INLINE_FN_END);
1711 static struct symbol_list *inlines_called;
1712 static void add_inline_function(struct symbol *sym)
1714 static struct symbol_list *already_added;
1715 struct symbol *tmp;
1717 FOR_EACH_PTR(already_added, tmp) {
1718 if (tmp == sym)
1719 return;
1720 } END_FOR_EACH_PTR(tmp);
1722 add_ptr_list(&already_added, sym);
1723 add_ptr_list(&inlines_called, sym);
1726 static void process_inlines(void)
1728 struct symbol *tmp;
1730 FOR_EACH_PTR(inlines_called, tmp) {
1731 split_function(tmp);
1732 } END_FOR_EACH_PTR(tmp);
1733 free_ptr_list(&inlines_called);
1736 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1738 struct symbol *sym;
1740 FOR_EACH_PTR_REVERSE(big_list, sym) {
1741 if (!sym->scope)
1742 continue;
1743 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1744 return sym;
1745 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1746 return sym;
1747 } END_FOR_EACH_PTR_REVERSE(sym);
1749 return NULL;
1752 static bool interesting_function(struct symbol *sym)
1754 static int prev_stream = -1;
1755 static bool prev_answer;
1756 const char *filename;
1757 int len;
1759 if (!(sym->ctype.modifiers & MOD_INLINE))
1760 return true;
1762 if (sym->pos.stream == prev_stream)
1763 return prev_answer;
1765 prev_stream = sym->pos.stream;
1766 prev_answer = false;
1768 filename = stream_name(sym->pos.stream);
1769 len = strlen(filename);
1770 if (len > 0 && filename[len - 1] == 'c')
1771 prev_answer = true;
1772 return prev_answer;
1775 static void split_inlines_in_scope(struct symbol *sym)
1777 struct symbol *base;
1778 struct symbol_list *scope_list;
1779 int stream;
1781 scope_list = sym->scope->symbols;
1782 stream = sym->pos.stream;
1784 /* find the last static symbol in the file */
1785 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1786 if (sym->pos.stream != stream)
1787 continue;
1788 if (sym->type != SYM_NODE)
1789 continue;
1790 base = get_base_type(sym);
1791 if (!base)
1792 continue;
1793 if (base->type != SYM_FN)
1794 continue;
1795 if (!base->inline_stmt)
1796 continue;
1797 if (!interesting_function(sym))
1798 continue;
1799 add_inline_function(sym);
1800 } END_FOR_EACH_PTR_REVERSE(sym);
1802 process_inlines();
1805 static void split_inlines(struct symbol_list *sym_list)
1807 struct symbol *sym;
1809 sym = get_last_scoped_symbol(sym_list, 0);
1810 if (sym)
1811 split_inlines_in_scope(sym);
1812 sym = get_last_scoped_symbol(sym_list, 1);
1813 if (sym)
1814 split_inlines_in_scope(sym);
1817 static struct stree *clone_estates_perm(struct stree *orig)
1819 struct stree *ret = NULL;
1820 struct sm_state *tmp;
1822 FOR_EACH_SM(orig, tmp) {
1823 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1824 } END_FOR_EACH_SM(tmp);
1826 return ret;
1829 struct position last_pos;
1830 static void split_c_file_functions(struct symbol_list *sym_list)
1832 struct symbol *sym;
1834 __unnullify_path();
1835 FOR_EACH_PTR(sym_list, sym) {
1836 set_position(sym->pos);
1837 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1838 __pass_to_client(sym, BASE_HOOK);
1839 fake_global_assign(sym);
1841 } END_FOR_EACH_PTR(sym);
1842 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1843 nullify_path();
1845 FOR_EACH_PTR(sym_list, sym) {
1846 set_position(sym->pos);
1847 last_pos = sym->pos;
1848 if (!interesting_function(sym))
1849 continue;
1850 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1851 split_function(sym);
1852 process_inlines();
1854 last_pos = sym->pos;
1855 } END_FOR_EACH_PTR(sym);
1856 split_inlines(sym_list);
1857 __pass_to_client(sym_list, END_FILE_HOOK);
1860 static int final_before_fake;
1861 void init_fake_env(void)
1863 if (!in_fake_env)
1864 final_before_fake = final_pass;
1865 in_fake_env++;
1866 __push_fake_cur_stree();
1867 final_pass = 0;
1870 void end_fake_env(void)
1872 __pop_fake_cur_stree();
1873 in_fake_env--;
1874 if (!in_fake_env)
1875 final_pass = final_before_fake;
1878 static void open_output_files(char *base_file)
1880 char buf[256];
1882 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1883 sm_outfd = fopen(buf, "w");
1884 if (!sm_outfd)
1885 sm_fatal("Cannot open %s", buf);
1887 if (!option_info)
1888 return;
1890 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1891 sql_outfd = fopen(buf, "w");
1892 if (!sql_outfd)
1893 sm_fatal("Error: Cannot open %s", buf);
1895 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1896 caller_info_fd = fopen(buf, "w");
1897 if (!caller_info_fd)
1898 sm_fatal("Error: Cannot open %s", buf);
1901 void smatch(int argc, char **argv)
1903 struct string_list *filelist = NULL;
1904 struct symbol_list *sym_list;
1905 struct timeval stop, start;
1906 char *path;
1907 int len;
1909 gettimeofday(&start, NULL);
1911 sparse_initialize(argc, argv, &filelist);
1912 alloc_valid_ptr_rl();
1913 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1914 path = getcwd(NULL, 0);
1915 free(full_base_file);
1916 if (path) {
1917 len = strlen(path) + 1 + strlen(base_file) + 1;
1918 full_base_file = malloc(len);
1919 snprintf(full_base_file, len, "%s/%s", path, base_file);
1920 } else {
1921 full_base_file = alloc_string(base_file);
1923 if (option_file_output)
1924 open_output_files(base_file);
1925 sym_list = sparse_keep_tokens(base_file);
1926 split_c_file_functions(sym_list);
1927 } END_FOR_EACH_PTR_NOTAG(base_file);
1929 gettimeofday(&stop, NULL);
1931 set_position(last_pos);
1932 final_pass = 1;
1933 if (option_time)
1934 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1935 if (option_mem)
1936 sm_msg("mem: %luKb", get_max_memory());