mtag_data: split get_mtag_rl() into two functions
[smatch.git] / smatch_flow.c
blob3d235d8adb684cfc17e1ff30563f79ef50f80a3a
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 *cur_func;
42 static unsigned int loop_count;
43 static int last_goto_statement_handled;
44 int __expr_stmt_count;
45 int __in_function_def;
46 static struct expression_list *switch_expr_stack = NULL;
47 static struct expression_list *post_op_stack = NULL;
49 static struct ptr_list *backup;
51 struct expression_list *big_expression_stack;
52 struct statement_list *big_statement_stack;
53 struct statement *__prev_stmt;
54 struct statement *__cur_stmt;
55 struct statement *__next_stmt;
56 int __in_pre_condition = 0;
57 int __bail_on_rest_of_function = 0;
58 static struct timeval fn_start_time;
59 char *get_function(void) { return cur_func; }
60 int get_lineno(void) { return __smatch_lineno; }
61 int inside_loop(void) { return !!loop_count; }
62 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
63 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
64 int in_expression_statement(void) { return !!__expr_stmt_count; }
66 static void split_symlist(struct symbol_list *sym_list);
67 static void split_declaration(struct symbol_list *sym_list);
68 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
69 static void add_inline_function(struct symbol *sym);
70 static void parse_inline(struct expression *expr);
72 int option_assume_loops = 0;
73 int option_two_passes = 0;
74 struct symbol *cur_func_sym = NULL;
75 struct stree *global_states;
77 long long valid_ptr_min = 4096;
78 long long valid_ptr_max = 2117777777;
79 sval_t valid_ptr_min_sval = {
80 .type = &ptr_ctype,
81 {.value = 4096},
83 sval_t valid_ptr_max_sval = {
84 .type = &ptr_ctype,
85 {.value = LONG_MAX - 100000},
87 struct range_list *valid_ptr_rl;
89 static void set_valid_ptr_max(void)
91 if (type_bits(&ptr_ctype) == 32)
92 valid_ptr_max = 2117777777;
93 else if (type_bits(&ptr_ctype) == 64)
94 valid_ptr_max = 2117777777777777777LL;
96 valid_ptr_max_sval.value = valid_ptr_max;
99 static void alloc_valid_ptr_rl(void)
101 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
102 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
103 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
106 int outside_of_function(void)
108 return cur_func_sym == NULL;
111 const char *get_filename(void)
113 if (option_info)
114 return base_file;
115 if (option_full_path)
116 return full_filename;
117 return filename;
120 const char *get_base_file(void)
122 return base_file;
125 static void set_position(struct position pos)
127 int len;
128 static int prev_stream = -1;
130 if (in_fake_env)
131 return;
133 if (pos.stream == 0 && pos.line == 0)
134 return;
136 __smatch_lineno = pos.line;
138 if (pos.stream == prev_stream)
139 return;
141 filename = stream_name(pos.stream);
143 free(full_filename);
144 pathname = getcwd(NULL, 0);
145 if (pathname) {
146 len = strlen(pathname) + 1 + strlen(filename) + 1;
147 full_filename = malloc(len);
148 snprintf(full_filename, len, "%s/%s", pathname, filename);
149 } else {
150 full_filename = alloc_string(filename);
152 free(pathname);
155 int is_assigned_call(struct expression *expr)
157 struct expression *parent = expr_get_parent_expr(expr);
159 if (parent &&
160 parent->type == EXPR_ASSIGNMENT &&
161 parent->op == '=' &&
162 strip_expr(parent->right) == expr)
163 return 1;
165 return 0;
168 static int is_inline_func(struct expression *expr)
170 if (expr->type != EXPR_SYMBOL || !expr->symbol)
171 return 0;
172 if (expr->symbol->ctype.modifiers & MOD_INLINE)
173 return 1;
174 return 0;
177 static int is_noreturn_func(struct expression *expr)
179 if (expr->type != EXPR_SYMBOL || !expr->symbol)
180 return 0;
181 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
182 return 1;
183 return 0;
186 static int inline_budget = 20;
188 int inlinable(struct expression *expr)
190 struct symbol *sym;
191 struct statement *last_stmt = NULL;
193 if (__inline_fn) /* don't nest */
194 return 0;
196 if (expr->type != EXPR_SYMBOL || !expr->symbol)
197 return 0;
198 if (is_no_inline_function(expr->symbol->ident->name))
199 return 0;
200 sym = get_base_type(expr->symbol);
201 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
202 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
203 return 0;
204 if (sym->stmt->type != STMT_COMPOUND)
205 return 0;
206 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
208 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
209 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
210 return 0;
211 if (sym->inline_stmt->type != STMT_COMPOUND)
212 return 0;
213 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
216 if (!last_stmt)
217 return 0;
219 /* the magic numbers in this function are pulled out of my bum. */
220 if (last_stmt->pos.line > sym->pos.line + inline_budget)
221 return 0;
223 return 1;
226 void __process_post_op_stack(void)
228 struct expression *expr;
230 FOR_EACH_PTR(post_op_stack, expr) {
231 __pass_to_client(expr, OP_HOOK);
232 } END_FOR_EACH_PTR(expr);
234 __free_ptr_list((struct ptr_list **)&post_op_stack);
237 static int handle_comma_assigns(struct expression *expr)
239 struct expression *right;
240 struct expression *assign;
242 right = strip_expr(expr->right);
243 if (right->type != EXPR_COMMA)
244 return 0;
246 __split_expr(right->left);
247 __process_post_op_stack();
249 assign = assign_expression(expr->left, '=', right->right);
250 __split_expr(assign);
252 return 1;
255 /* This is to handle *p++ = foo; assignments */
256 static int handle_postop_assigns(struct expression *expr)
258 struct expression *left, *fake_left;
259 struct expression *assign;
261 left = strip_expr(expr->left);
262 if (left->type != EXPR_PREOP || left->op != '*')
263 return 0;
264 left = strip_expr(left->unop);
265 if (left->type != EXPR_POSTOP)
266 return 0;
268 fake_left = deref_expression(strip_expr(left->unop));
269 assign = assign_expression(fake_left, '=', expr->right);
271 __split_expr(assign);
272 __split_expr(expr->left);
274 return 1;
277 static int prev_expression_is_getting_address(struct expression *expr)
279 struct expression *parent;
281 do {
282 parent = expr_get_parent_expr(expr);
284 if (!parent)
285 return 0;
286 if (parent->type == EXPR_PREOP && parent->op == '&')
287 return 1;
288 if (parent->type == EXPR_PREOP && parent->op == '(')
289 goto next;
290 if (parent->type == EXPR_DEREF && parent->op == '.')
291 goto next;
293 return 0;
294 next:
295 expr = parent;
296 } while (1);
299 static int handle__builtin_choose_expr(struct expression *expr)
301 struct expression *const_expr, *expr1, *expr2;
302 sval_t sval;
304 if (!sym_name_is("__builtin_choose_expr", expr->fn))
305 return 0;
307 const_expr = get_argument_from_call_expr(expr->args, 0);
308 expr1 = get_argument_from_call_expr(expr->args, 1);
309 expr2 = get_argument_from_call_expr(expr->args, 2);
311 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
312 return 0;
313 if (sval.value)
314 __split_expr(expr1);
315 else
316 __split_expr(expr2);
317 return 1;
320 void __split_expr(struct expression *expr)
322 if (!expr)
323 return;
325 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
327 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
328 return;
329 if (__in_fake_assign >= 4) /* don't allow too much nesting */
330 return;
332 push_expression(&big_expression_stack, expr);
333 set_position(expr->pos);
334 __pass_to_client(expr, EXPR_HOOK);
336 switch (expr->type) {
337 case EXPR_PREOP:
338 expr_set_parent_expr(expr->unop, expr);
340 if (expr->op == '*' &&
341 !prev_expression_is_getting_address(expr))
342 __pass_to_client(expr, DEREF_HOOK);
343 __split_expr(expr->unop);
344 __pass_to_client(expr, OP_HOOK);
345 break;
346 case EXPR_POSTOP:
347 expr_set_parent_expr(expr->unop, expr);
349 __split_expr(expr->unop);
350 push_expression(&post_op_stack, expr);
351 break;
352 case EXPR_STATEMENT:
353 __expr_stmt_count++;
354 if (expr->statement && !expr->statement) {
355 stmt_set_parent_stmt(expr->statement,
356 last_ptr_list((struct ptr_list *)big_statement_stack));
358 __split_stmt(expr->statement);
359 __expr_stmt_count--;
360 break;
361 case EXPR_LOGICAL:
362 case EXPR_COMPARE:
363 expr_set_parent_expr(expr->left, expr);
364 expr_set_parent_expr(expr->right, expr);
366 __pass_to_client(expr, LOGIC_HOOK);
367 __handle_logic(expr);
368 break;
369 case EXPR_BINOP:
370 expr_set_parent_expr(expr->left, expr);
371 expr_set_parent_expr(expr->right, expr);
373 __pass_to_client(expr, BINOP_HOOK);
374 case EXPR_COMMA:
375 expr_set_parent_expr(expr->left, expr);
376 expr_set_parent_expr(expr->right, expr);
378 __split_expr(expr->left);
379 __process_post_op_stack();
380 __split_expr(expr->right);
381 break;
382 case EXPR_ASSIGNMENT: {
383 struct expression *right;
385 expr_set_parent_expr(expr->left, expr);
386 expr_set_parent_expr(expr->right, expr);
388 right = strip_expr(expr->right);
389 if (!right)
390 break;
392 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
394 /* foo = !bar() */
395 if (__handle_condition_assigns(expr))
396 break;
397 /* foo = (x < 5 ? foo : 5); */
398 if (__handle_select_assigns(expr))
399 break;
400 /* foo = ({frob(); frob(); frob(); 1;}) */
401 if (__handle_expr_statement_assigns(expr))
402 break;
403 /* foo = (3, 4); */
404 if (handle_comma_assigns(expr))
405 break;
406 if (handle_postop_assigns(expr))
407 break;
409 __split_expr(expr->right);
410 if (outside_of_function())
411 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
412 else
413 __pass_to_client(expr, ASSIGNMENT_HOOK);
415 __fake_struct_member_assignments(expr);
417 if (expr->op == '=' && right->type == EXPR_CALL)
418 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
420 if (get_macro_name(right->pos) &&
421 get_macro_name(expr->pos) != get_macro_name(right->pos))
422 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
424 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
426 __split_expr(expr->left);
427 break;
429 case EXPR_DEREF:
430 expr_set_parent_expr(expr->deref, expr);
432 __pass_to_client(expr, DEREF_HOOK);
433 __split_expr(expr->deref);
434 break;
435 case EXPR_SLICE:
436 expr_set_parent_expr(expr->base, expr);
438 __split_expr(expr->base);
439 break;
440 case EXPR_CAST:
441 case EXPR_FORCE_CAST:
442 expr_set_parent_expr(expr->cast_expression, expr);
444 __pass_to_client(expr, CAST_HOOK);
445 __split_expr(expr->cast_expression);
446 break;
447 case EXPR_SIZEOF:
448 if (expr->cast_expression)
449 __pass_to_client(strip_parens(expr->cast_expression),
450 SIZEOF_HOOK);
451 break;
452 case EXPR_OFFSETOF:
453 case EXPR_ALIGNOF:
454 evaluate_expression(expr);
455 break;
456 case EXPR_CONDITIONAL:
457 case EXPR_SELECT:
458 expr_set_parent_expr(expr->conditional, expr);
459 expr_set_parent_expr(expr->cond_true, expr);
460 expr_set_parent_expr(expr->cond_false, expr);
462 if (known_condition_true(expr->conditional)) {
463 __split_expr(expr->cond_true);
464 break;
466 if (known_condition_false(expr->conditional)) {
467 __split_expr(expr->cond_false);
468 break;
470 __pass_to_client(expr, SELECT_HOOK);
471 __split_whole_condition(expr->conditional);
472 __split_expr(expr->cond_true);
473 __push_true_states();
474 __use_false_states();
475 __split_expr(expr->cond_false);
476 __merge_true_states();
477 break;
478 case EXPR_CALL:
479 expr_set_parent_expr(expr->fn, expr);
481 if (sym_name_is("__builtin_constant_p", expr->fn))
482 break;
483 if (handle__builtin_choose_expr(expr))
484 break;
485 split_expr_list(expr->args, expr);
486 __split_expr(expr->fn);
487 if (is_inline_func(expr->fn))
488 add_inline_function(expr->fn->symbol);
489 if (inlinable(expr->fn))
490 __inline_call = 1;
491 __process_post_op_stack();
492 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
493 __pass_to_client(expr, FUNCTION_CALL_HOOK);
494 __inline_call = 0;
495 if (inlinable(expr->fn)) {
496 parse_inline(expr);
498 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
499 if (is_noreturn_func(expr->fn))
500 nullify_path();
501 break;
502 case EXPR_INITIALIZER:
503 split_expr_list(expr->expr_list, expr);
504 break;
505 case EXPR_IDENTIFIER:
506 expr_set_parent_expr(expr->ident_expression, expr);
507 __split_expr(expr->ident_expression);
508 break;
509 case EXPR_INDEX:
510 expr_set_parent_expr(expr->idx_expression, expr);
511 __split_expr(expr->idx_expression);
512 break;
513 case EXPR_POS:
514 expr_set_parent_expr(expr->init_expr, expr);
515 __split_expr(expr->init_expr);
516 break;
517 case EXPR_SYMBOL:
518 __pass_to_client(expr, SYM_HOOK);
519 break;
520 case EXPR_STRING:
521 __pass_to_client(expr, STRING_HOOK);
522 break;
523 default:
524 break;
526 pop_expression(&big_expression_stack);
529 static int is_forever_loop(struct statement *stmt)
531 struct expression *expr;
532 sval_t sval;
534 expr = strip_expr(stmt->iterator_pre_condition);
535 if (!expr)
536 expr = stmt->iterator_post_condition;
537 if (!expr) {
538 /* this is a for(;;) loop... */
539 return 1;
542 if (get_value(expr, &sval) && sval.value != 0)
543 return 1;
545 return 0;
548 static int loop_num;
549 static char *get_loop_name(int num)
551 char buf[256];
553 snprintf(buf, 255, "-loop%d", num);
554 buf[255] = '\0';
555 return alloc_sname(buf);
559 * Pre Loops are while and for loops.
561 static void handle_pre_loop(struct statement *stmt)
563 int once_through; /* we go through the loop at least once */
564 struct sm_state *extra_sm = NULL;
565 int unchanged = 0;
566 char *loop_name;
567 struct stree *stree = NULL;
568 struct sm_state *sm = NULL;
570 loop_name = get_loop_name(loop_num);
571 loop_num++;
573 __split_stmt(stmt->iterator_pre_statement);
574 __prev_stmt = stmt->iterator_pre_statement;
576 once_through = implied_condition_true(stmt->iterator_pre_condition);
578 loop_count++;
579 __push_continues();
580 __push_breaks();
582 __merge_gotos(loop_name, NULL);
584 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
585 __in_pre_condition++;
586 __pass_to_client(stmt, PRELOOP_HOOK);
587 __split_whole_condition(stmt->iterator_pre_condition);
588 __in_pre_condition--;
589 FOR_EACH_SM(stree, sm) {
590 set_state(sm->owner, sm->name, sm->sym, sm->state);
591 } END_FOR_EACH_SM(sm);
592 free_stree(&stree);
593 if (extra_sm)
594 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
596 if (option_assume_loops)
597 once_through = 1;
599 __split_stmt(stmt->iterator_statement);
600 if (is_forever_loop(stmt)) {
601 __merge_continues();
602 __save_gotos(loop_name, NULL);
604 __push_fake_cur_stree();
605 __split_stmt(stmt->iterator_post_statement);
606 stree = __pop_fake_cur_stree();
608 __discard_false_states();
609 __use_breaks();
611 if (!__path_is_null())
612 __merge_stree_into_cur(stree);
613 free_stree(&stree);
614 } else {
615 __merge_continues();
616 unchanged = __iterator_unchanged(extra_sm);
617 __split_stmt(stmt->iterator_post_statement);
618 __prev_stmt = stmt->iterator_post_statement;
619 __cur_stmt = stmt;
621 __save_gotos(loop_name, NULL);
622 __in_pre_condition++;
623 __split_whole_condition(stmt->iterator_pre_condition);
624 __in_pre_condition--;
625 nullify_path();
626 __merge_false_states();
627 if (once_through)
628 __discard_false_states();
629 else
630 __merge_false_states();
632 if (extra_sm && unchanged)
633 __extra_pre_loop_hook_after(extra_sm,
634 stmt->iterator_post_statement,
635 stmt->iterator_pre_condition);
636 __merge_breaks();
638 loop_count--;
642 * Post loops are do {} while();
644 static void handle_post_loop(struct statement *stmt)
646 char *loop_name;
648 loop_name = get_loop_name(loop_num);
649 loop_num++;
650 loop_count++;
652 __push_continues();
653 __push_breaks();
654 __merge_gotos(loop_name, NULL);
655 __split_stmt(stmt->iterator_statement);
656 __merge_continues();
657 if (!is_zero(stmt->iterator_post_condition))
658 __save_gotos(loop_name, NULL);
660 if (is_forever_loop(stmt)) {
661 __use_breaks();
662 } else {
663 __split_whole_condition(stmt->iterator_post_condition);
664 __use_false_states();
665 __merge_breaks();
667 loop_count--;
670 static int empty_statement(struct statement *stmt)
672 if (!stmt)
673 return 0;
674 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
675 return 1;
676 return 0;
679 static int last_stmt_on_same_line(void)
681 struct statement *stmt;
682 int i = 0;
684 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
685 if (!i++)
686 continue;
687 if (stmt->pos.line == get_lineno())
688 return 1;
689 return 0;
690 } END_FOR_EACH_PTR_REVERSE(stmt);
691 return 0;
694 static void split_asm_constraints(struct expression_list *expr_list)
696 struct expression *expr;
697 int state = 0;
699 FOR_EACH_PTR(expr_list, expr) {
700 switch (state) {
701 case 0: /* identifier */
702 case 1: /* constraint */
703 state++;
704 continue;
705 case 2: /* expression */
706 state = 0;
707 __split_expr(expr);
708 continue;
710 } END_FOR_EACH_PTR(expr);
713 static int is_case_val(struct statement *stmt, sval_t sval)
715 sval_t case_sval;
717 if (stmt->type != STMT_CASE)
718 return 0;
719 if (!stmt->case_expression) {
720 __set_default();
721 return 1;
723 if (!get_value(stmt->case_expression, &case_sval))
724 return 0;
725 if (case_sval.value == sval.value)
726 return 1;
727 return 0;
730 static struct range_list *get_case_rl(struct expression *switch_expr,
731 struct expression *case_expr,
732 struct expression *case_to)
734 sval_t start, end;
735 struct range_list *rl = NULL;
736 struct symbol *switch_type;
738 switch_type = get_type(switch_expr);
739 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
740 start = sval_cast(switch_type, start);
741 end = sval_cast(switch_type, end);
742 add_range(&rl, start, end);
743 } else if (get_value(case_expr, &start)) {
744 start = sval_cast(switch_type, start);
745 add_range(&rl, start, start);
748 return rl;
751 static void split_known_switch(struct statement *stmt, sval_t sval)
753 struct statement *tmp;
754 struct range_list *rl;
756 __split_expr(stmt->switch_expression);
757 sval = sval_cast(get_type(stmt->switch_expression), sval);
759 push_expression(&switch_expr_stack, stmt->switch_expression);
760 __save_switch_states(top_expression(switch_expr_stack));
761 nullify_path();
762 __push_default();
763 __push_breaks();
765 stmt = stmt->switch_statement;
767 __push_scope_hooks();
768 FOR_EACH_PTR(stmt->stmts, tmp) {
769 __smatch_lineno = tmp->pos.line;
770 if (is_case_val(tmp, sval)) {
771 rl = alloc_rl(sval, sval);
772 __merge_switches(top_expression(switch_expr_stack), rl);
773 __pass_case_to_client(top_expression(switch_expr_stack), rl);
775 if (__path_is_null())
776 continue;
777 __split_stmt(tmp);
778 if (__path_is_null()) {
779 __set_default();
780 goto out;
782 } END_FOR_EACH_PTR(tmp);
783 out:
784 __call_scope_hooks();
785 if (!__pop_default())
786 __merge_switches(top_expression(switch_expr_stack), NULL);
787 __discard_switches();
788 __merge_breaks();
789 pop_expression(&switch_expr_stack);
792 static void split_case(struct statement *stmt)
794 struct range_list *rl = NULL;
796 expr_set_parent_stmt(stmt->case_expression, stmt);
797 expr_set_parent_stmt(stmt->case_to, stmt);
799 rl = get_case_rl(top_expression(switch_expr_stack),
800 stmt->case_expression, stmt->case_to);
801 while (stmt->case_statement->type == STMT_CASE) {
802 struct range_list *tmp;
804 tmp = get_case_rl(top_expression(switch_expr_stack),
805 stmt->case_statement->case_expression,
806 stmt->case_statement->case_to);
807 if (!tmp)
808 break;
809 rl = rl_union(rl, tmp);
810 if (!stmt->case_expression)
811 __set_default();
812 stmt = stmt->case_statement;
815 __merge_switches(top_expression(switch_expr_stack), rl);
817 if (!stmt->case_expression)
818 __set_default();
819 __split_stmt(stmt->case_statement);
822 int time_parsing_function(void)
824 return ms_since(&fn_start_time) / 1000;
827 static int taking_too_long(void)
829 if (time_parsing_function() > 60 * 5) /* five minutes */
830 return 1;
831 return 0;
834 static int is_last_stmt(struct statement *cur_stmt)
836 struct symbol *fn = get_base_type(cur_func_sym);
837 struct statement *stmt;
839 if (!fn)
840 return 0;
841 stmt = fn->stmt;
842 if (!stmt)
843 stmt = fn->inline_stmt;
844 if (!stmt || stmt->type != STMT_COMPOUND)
845 return 0;
846 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
847 if (stmt && stmt->type == STMT_LABEL)
848 stmt = stmt->label_statement;
849 if (stmt == cur_stmt)
850 return 1;
851 return 0;
854 static void handle_backward_goto(struct statement *goto_stmt)
856 const char *goto_name, *label_name;
857 struct statement *func_stmt;
858 struct symbol *base_type = get_base_type(cur_func_sym);
859 struct statement *tmp;
860 int found = 0;
862 if (!option_info)
863 return;
864 if (last_goto_statement_handled)
865 return;
866 last_goto_statement_handled = 1;
868 if (!goto_stmt->goto_label ||
869 goto_stmt->goto_label->type != SYM_LABEL ||
870 !goto_stmt->goto_label->ident)
871 return;
872 goto_name = goto_stmt->goto_label->ident->name;
874 func_stmt = base_type->stmt;
875 if (!func_stmt)
876 func_stmt = base_type->inline_stmt;
877 if (!func_stmt)
878 return;
879 if (func_stmt->type != STMT_COMPOUND)
880 return;
882 FOR_EACH_PTR(func_stmt->stmts, tmp) {
883 if (!found) {
884 if (tmp->type != STMT_LABEL)
885 continue;
886 if (!tmp->label_identifier ||
887 tmp->label_identifier->type != SYM_LABEL ||
888 !tmp->label_identifier->ident)
889 continue;
890 label_name = tmp->label_identifier->ident->name;
891 if (strcmp(goto_name, label_name) != 0)
892 continue;
893 found = 1;
895 __split_stmt(tmp);
896 } END_FOR_EACH_PTR(tmp);
899 static void fake_a_return(void)
901 struct symbol *return_type;
903 nullify_path();
904 __unnullify_path();
906 return_type = get_real_base_type(cur_func_sym);
907 return_type = get_real_base_type(return_type);
908 if (return_type != &void_ctype) {
909 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
910 nullify_path();
914 static void fake_an_empty_default(struct position pos)
916 static struct statement none = {};
918 none.pos = pos;
919 none.type = STMT_NONE;
920 __merge_switches(top_expression(switch_expr_stack), NULL);
921 __split_stmt(&none);
924 static void split_compound(struct statement *stmt)
926 struct statement *prev = NULL;
927 struct statement *cur = NULL;
928 struct statement *next;
930 __push_scope_hooks();
932 FOR_EACH_PTR(stmt->stmts, next) {
933 /* just set them all ahead of time */
934 stmt_set_parent_stmt(next, stmt);
936 if (cur) {
937 __prev_stmt = prev;
938 __next_stmt = next;
939 __cur_stmt = cur;
940 __split_stmt(cur);
942 prev = cur;
943 cur = next;
944 } END_FOR_EACH_PTR(next);
945 if (cur) {
946 __prev_stmt = prev;
947 __cur_stmt = cur;
948 __next_stmt = NULL;
949 __split_stmt(cur);
953 * For function scope, then delay calling the scope hooks until the
954 * end of function hooks can run. I'm not positive this is the right
955 * thing...
957 if (!is_last_stmt(cur))
958 __call_scope_hooks();
962 * This is a hack, work around for detecting empty functions.
964 static int need_delayed_scope_hooks(void)
966 struct symbol *fn = get_base_type(cur_func_sym);
967 struct statement *stmt;
969 if (!fn)
970 return 0;
971 stmt = fn->stmt;
972 if (!stmt)
973 stmt = fn->inline_stmt;
974 if (stmt && stmt->type == STMT_COMPOUND)
975 return 1;
976 return 0;
979 void __split_label_stmt(struct statement *stmt)
981 if (stmt->label_identifier &&
982 stmt->label_identifier->type == SYM_LABEL &&
983 stmt->label_identifier->ident) {
984 loop_count |= 0x0800000;
985 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
989 static void find_asm_gotos(struct statement *stmt)
991 struct symbol *sym;
993 FOR_EACH_PTR(stmt->asm_labels, sym) {
994 __save_gotos(sym->ident->name, sym);
995 } END_FOR_EACH_PTR(sym);
998 void __split_stmt(struct statement *stmt)
1000 sval_t sval;
1002 if (!stmt)
1003 goto out;
1005 if (!__in_fake_assign)
1006 __silence_warnings_for_stmt = false;
1008 if (__bail_on_rest_of_function)
1009 return;
1011 if (out_of_memory() || taking_too_long()) {
1012 struct timeval stop;
1014 gettimeofday(&stop, NULL);
1016 __bail_on_rest_of_function = 1;
1017 final_pass = 1;
1018 sm_msg("Function too hairy. Giving up. %lu seconds",
1019 stop.tv_sec - fn_start_time.tv_sec);
1020 fake_a_return();
1021 final_pass = 0; /* turn off sm_msg() from here */
1022 return;
1025 add_ptr_list(&big_statement_stack, stmt);
1026 free_expression_stack(&big_expression_stack);
1027 set_position(stmt->pos);
1028 __pass_to_client(stmt, STMT_HOOK);
1030 switch (stmt->type) {
1031 case STMT_DECLARATION:
1032 split_declaration(stmt->declaration);
1033 break;
1034 case STMT_RETURN:
1035 expr_set_parent_stmt(stmt->ret_value, stmt);
1037 __split_expr(stmt->ret_value);
1038 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1039 __process_post_op_stack();
1040 nullify_path();
1041 break;
1042 case STMT_EXPRESSION:
1043 expr_set_parent_stmt(stmt->expression, stmt);
1044 expr_set_parent_stmt(stmt->context, stmt);
1046 __split_expr(stmt->expression);
1047 break;
1048 case STMT_COMPOUND:
1049 split_compound(stmt);
1050 break;
1051 case STMT_IF:
1052 stmt_set_parent_stmt(stmt->if_true, stmt);
1053 stmt_set_parent_stmt(stmt->if_false, stmt);
1054 expr_set_parent_stmt(stmt->if_conditional, stmt);
1056 if (known_condition_true(stmt->if_conditional)) {
1057 __split_stmt(stmt->if_true);
1058 break;
1060 if (known_condition_false(stmt->if_conditional)) {
1061 __split_stmt(stmt->if_false);
1062 break;
1064 __split_whole_condition(stmt->if_conditional);
1065 __split_stmt(stmt->if_true);
1066 if (empty_statement(stmt->if_true) &&
1067 last_stmt_on_same_line() &&
1068 !get_macro_name(stmt->if_true->pos))
1069 sm_msg("warn: if();");
1070 __push_true_states();
1071 __use_false_states();
1072 __split_stmt(stmt->if_false);
1073 __merge_true_states();
1074 break;
1075 case STMT_ITERATOR:
1076 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1077 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1078 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1079 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1080 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1082 if (stmt->iterator_pre_condition)
1083 handle_pre_loop(stmt);
1084 else if (stmt->iterator_post_condition)
1085 handle_post_loop(stmt);
1086 else {
1087 // these are for(;;) type loops.
1088 handle_pre_loop(stmt);
1090 break;
1091 case STMT_SWITCH:
1092 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1093 expr_set_parent_stmt(stmt->switch_expression, stmt);
1095 if (get_value(stmt->switch_expression, &sval)) {
1096 split_known_switch(stmt, sval);
1097 break;
1099 __split_expr(stmt->switch_expression);
1100 push_expression(&switch_expr_stack, stmt->switch_expression);
1101 __save_switch_states(top_expression(switch_expr_stack));
1102 nullify_path();
1103 __push_default();
1104 __push_breaks();
1105 __split_stmt(stmt->switch_statement);
1106 if (!__pop_default() && have_remaining_cases())
1107 fake_an_empty_default(stmt->pos);
1108 __discard_switches();
1109 __merge_breaks();
1110 pop_expression(&switch_expr_stack);
1111 break;
1112 case STMT_CASE:
1113 split_case(stmt);
1114 break;
1115 case STMT_LABEL:
1116 __split_label_stmt(stmt);
1117 __split_stmt(stmt->label_statement);
1118 break;
1119 case STMT_GOTO:
1120 expr_set_parent_stmt(stmt->goto_expression, stmt);
1122 __split_expr(stmt->goto_expression);
1123 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1124 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1125 __process_breaks();
1126 } else if (!strcmp(stmt->goto_label->ident->name,
1127 "continue")) {
1128 __process_continues();
1130 } else if (stmt->goto_label &&
1131 stmt->goto_label->type == SYM_LABEL &&
1132 stmt->goto_label->ident) {
1133 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1135 nullify_path();
1136 if (is_last_stmt(stmt))
1137 handle_backward_goto(stmt);
1138 break;
1139 case STMT_NONE:
1140 break;
1141 case STMT_ASM:
1142 expr_set_parent_stmt(stmt->asm_string, stmt);
1144 find_asm_gotos(stmt);
1145 __pass_to_client(stmt, ASM_HOOK);
1146 __split_expr(stmt->asm_string);
1147 split_asm_constraints(stmt->asm_outputs);
1148 split_asm_constraints(stmt->asm_inputs);
1149 split_asm_constraints(stmt->asm_clobbers);
1150 break;
1151 case STMT_CONTEXT:
1152 break;
1153 case STMT_RANGE:
1154 __split_expr(stmt->range_expression);
1155 __split_expr(stmt->range_low);
1156 __split_expr(stmt->range_high);
1157 break;
1159 __pass_to_client(stmt, STMT_HOOK_AFTER);
1160 out:
1161 __process_post_op_stack();
1164 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1166 struct expression *expr;
1168 FOR_EACH_PTR(expr_list, expr) {
1169 expr_set_parent_expr(expr, parent);
1170 __split_expr(expr);
1171 __process_post_op_stack();
1172 } END_FOR_EACH_PTR(expr);
1175 static void split_sym(struct symbol *sym)
1177 if (!sym)
1178 return;
1179 if (!(sym->namespace & NS_SYMBOL))
1180 return;
1182 __split_stmt(sym->stmt);
1183 __split_expr(sym->array_size);
1184 split_symlist(sym->arguments);
1185 split_symlist(sym->symbol_list);
1186 __split_stmt(sym->inline_stmt);
1187 split_symlist(sym->inline_symbol_list);
1190 static void split_symlist(struct symbol_list *sym_list)
1192 struct symbol *sym;
1194 FOR_EACH_PTR(sym_list, sym) {
1195 split_sym(sym);
1196 } END_FOR_EACH_PTR(sym);
1199 typedef void (fake_cb)(struct expression *expr);
1201 static int member_to_number(struct expression *expr, struct ident *member)
1203 struct symbol *type, *tmp;
1204 char *name;
1205 int i;
1207 if (!member)
1208 return -1;
1209 name = member->name;
1211 type = get_type(expr);
1212 if (!type || type->type != SYM_STRUCT)
1213 return -1;
1215 i = -1;
1216 FOR_EACH_PTR(type->symbol_list, tmp) {
1217 i++;
1218 if (!tmp->ident)
1219 continue;
1220 if (strcmp(name, tmp->ident->name) == 0)
1221 return i;
1222 } END_FOR_EACH_PTR(tmp);
1223 return -1;
1226 static struct ident *number_to_member(struct expression *expr, int num)
1228 struct symbol *type, *member;
1229 int i = 0;
1231 type = get_type(expr);
1232 if (!type || type->type != SYM_STRUCT)
1233 return NULL;
1235 FOR_EACH_PTR(type->symbol_list, member) {
1236 if (i == num)
1237 return member->ident;
1238 i++;
1239 } END_FOR_EACH_PTR(member);
1240 return NULL;
1243 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1245 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1247 struct expression *edge_member, *assign;
1248 struct symbol *base = get_real_base_type(member);
1249 struct symbol *tmp;
1251 if (member->ident)
1252 expr = member_expression(expr, '.', member->ident);
1254 FOR_EACH_PTR(base->symbol_list, tmp) {
1255 struct symbol *type;
1257 type = get_real_base_type(tmp);
1258 if (!type)
1259 continue;
1261 edge_member = member_expression(expr, '.', tmp->ident);
1262 if (get_extra_state(edge_member))
1263 continue;
1265 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1266 set_inner_struct_members(expr, tmp);
1267 continue;
1270 if (!tmp->ident)
1271 continue;
1273 assign = assign_expression(edge_member, '=', zero_expr());
1274 __split_expr(assign);
1275 } END_FOR_EACH_PTR(tmp);
1280 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1282 struct symbol *tmp;
1283 struct expression *member = NULL;
1284 struct expression *assign;
1285 int op = '*';
1287 if (expr->type == EXPR_PREOP && expr->op == '&') {
1288 expr = strip_expr(expr->unop);
1289 op = '.';
1292 FOR_EACH_PTR(type->symbol_list, tmp) {
1293 type = get_real_base_type(tmp);
1294 if (!type)
1295 continue;
1297 if (tmp->ident) {
1298 member = member_expression(expr, op, tmp->ident);
1299 if (get_extra_state(member))
1300 continue;
1303 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1304 set_inner_struct_members(expr, tmp);
1305 continue;
1307 if (type->type == SYM_ARRAY)
1308 continue;
1309 if (!tmp->ident)
1310 continue;
1312 assign = assign_expression(member, '=', zero_expr());
1313 __split_expr(assign);
1314 } END_FOR_EACH_PTR(tmp);
1317 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1319 struct expression *deref, *assign, *tmp, *right;
1320 struct symbol *struct_type, *type;
1321 struct ident *member;
1322 int member_idx;
1324 struct_type = get_type(symbol);
1325 if (!struct_type ||
1326 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1327 return;
1330 * We're parsing an initializer that could look something like this:
1331 * struct foo foo = {
1332 * 42,
1333 * .whatever.xxx = 11,
1334 * .zzz = 12,
1335 * };
1337 * So what we have here is a list with 42, .whatever, and .zzz. We need
1338 * to break it up into left and right sides of the assignments.
1341 member_idx = 0;
1342 FOR_EACH_PTR(members, tmp) {
1343 deref = NULL;
1344 if (tmp->type == EXPR_IDENTIFIER) {
1345 member_idx = member_to_number(symbol, tmp->expr_ident);
1346 while (tmp->type == EXPR_IDENTIFIER) {
1347 member = tmp->expr_ident;
1348 tmp = tmp->ident_expression;
1349 if (deref)
1350 deref = member_expression(deref, '.', member);
1351 else
1352 deref = member_expression(symbol, '.', member);
1354 } else {
1355 member = number_to_member(symbol, member_idx);
1356 deref = member_expression(symbol, '.', member);
1358 right = tmp;
1359 member_idx++;
1360 if (right->type == EXPR_INITIALIZER) {
1361 type = get_type(deref);
1362 if (type && type->type == SYM_ARRAY)
1363 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1364 else
1365 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1366 } else {
1367 assign = assign_expression(deref, '=', right);
1368 fake_cb(assign);
1370 } END_FOR_EACH_PTR(tmp);
1372 set_unset_to_zero(struct_type, symbol);
1375 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1377 fake_member_assigns_helper(symbol_expression(sym),
1378 sym->initializer->expr_list, fake_cb);
1381 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1383 struct expression *offset, *binop, *assign, *tmp;
1384 struct symbol *type;
1385 int idx;
1387 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1388 return;
1390 idx = 0;
1391 FOR_EACH_PTR(expr_list, tmp) {
1392 if (tmp->type == EXPR_INDEX) {
1393 if (tmp->idx_from != tmp->idx_to)
1394 return;
1395 idx = tmp->idx_from;
1396 if (!tmp->idx_expression)
1397 goto next;
1398 tmp = tmp->idx_expression;
1400 offset = value_expr(idx);
1401 binop = array_element_expression(array, offset);
1402 if (tmp->type == EXPR_INITIALIZER) {
1403 type = get_type(binop);
1404 if (type && type->type == SYM_ARRAY)
1405 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1406 else
1407 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1408 } else {
1409 assign = assign_expression(binop, '=', tmp);
1410 fake_cb(assign);
1412 next:
1413 idx++;
1414 } END_FOR_EACH_PTR(tmp);
1417 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1419 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1422 static void fake_assign_expr(struct symbol *sym)
1424 struct expression *assign, *symbol;
1426 symbol = symbol_expression(sym);
1427 assign = assign_expression(symbol, '=', sym->initializer);
1428 __split_expr(assign);
1431 static void do_initializer_stuff(struct symbol *sym)
1433 if (!sym->initializer)
1434 return;
1436 if (sym->initializer->type == EXPR_INITIALIZER) {
1437 if (get_real_base_type(sym)->type == SYM_ARRAY)
1438 fake_element_assigns(sym, __split_expr);
1439 else
1440 fake_member_assigns(sym, __split_expr);
1441 } else {
1442 fake_assign_expr(sym);
1446 static void split_declaration(struct symbol_list *sym_list)
1448 struct symbol *sym;
1450 FOR_EACH_PTR(sym_list, sym) {
1451 __pass_to_client(sym, DECLARATION_HOOK);
1452 do_initializer_stuff(sym);
1453 split_sym(sym);
1454 } END_FOR_EACH_PTR(sym);
1457 static void call_global_assign_hooks(struct expression *assign)
1459 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1462 static void fake_global_assign(struct symbol *sym)
1464 struct expression *assign, *symbol;
1466 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1467 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1468 fake_element_assigns(sym, call_global_assign_hooks);
1469 } else if (sym->initializer) {
1470 symbol = symbol_expression(sym);
1471 assign = assign_expression(symbol, '=', sym->initializer);
1472 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1473 } else {
1474 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1476 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1477 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1478 fake_member_assigns(sym, call_global_assign_hooks);
1479 } else if (sym->initializer) {
1480 symbol = symbol_expression(sym);
1481 assign = assign_expression(symbol, '=', sym->initializer);
1482 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1483 } else {
1484 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1486 } else {
1487 symbol = symbol_expression(sym);
1488 if (sym->initializer)
1489 assign = assign_expression(symbol, '=', sym->initializer);
1490 else
1491 assign = assign_expression(symbol, '=', zero_expr());
1492 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1496 static void start_function_definition(struct symbol *sym)
1498 __in_function_def = 1;
1499 __pass_to_client(sym, FUNC_DEF_HOOK);
1500 __in_function_def = 0;
1501 __pass_to_client(sym, AFTER_DEF_HOOK);
1505 static void split_function(struct symbol *sym)
1507 struct symbol *base_type = get_base_type(sym);
1508 struct timeval stop;
1510 if (!base_type->stmt && !base_type->inline_stmt)
1511 return;
1513 gettimeofday(&fn_start_time, NULL);
1514 cur_func_sym = sym;
1515 if (sym->ident)
1516 cur_func = sym->ident->name;
1517 set_position(sym->pos);
1518 loop_count = 0;
1519 last_goto_statement_handled = 0;
1520 sm_debug("new function: %s\n", cur_func);
1521 __stree_id = 0;
1522 if (option_two_passes) {
1523 __unnullify_path();
1524 loop_num = 0;
1525 final_pass = 0;
1526 start_function_definition(sym);
1527 __split_stmt(base_type->stmt);
1528 __split_stmt(base_type->inline_stmt);
1529 nullify_path();
1531 __unnullify_path();
1532 loop_num = 0;
1533 final_pass = 1;
1534 start_function_definition(sym);
1535 __split_stmt(base_type->stmt);
1536 __split_stmt(base_type->inline_stmt);
1537 __pass_to_client(sym, END_FUNC_HOOK);
1538 if (need_delayed_scope_hooks())
1539 __call_scope_hooks();
1540 __pass_to_client(sym, AFTER_FUNC_HOOK);
1542 clear_all_states();
1544 gettimeofday(&stop, NULL);
1545 if (option_time && stop.tv_sec - fn_start_time.tv_sec > 2) {
1546 final_pass++;
1547 sm_msg("func_time: %lu", stop.tv_sec - fn_start_time.tv_sec);
1548 final_pass--;
1550 cur_func_sym = NULL;
1551 cur_func = NULL;
1552 free_data_info_allocs();
1553 free_expression_stack(&switch_expr_stack);
1554 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1555 __bail_on_rest_of_function = 0;
1558 static void save_flow_state(void)
1560 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1561 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1562 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1564 __add_ptr_list(&backup, big_statement_stack, 0);
1565 __add_ptr_list(&backup, big_expression_stack, 0);
1566 __add_ptr_list(&backup, big_condition_stack, 0);
1567 __add_ptr_list(&backup, switch_expr_stack, 0);
1569 __add_ptr_list(&backup, cur_func_sym, 0);
1571 __add_ptr_list(&backup, __prev_stmt, 0);
1572 __add_ptr_list(&backup, __cur_stmt, 0);
1573 __add_ptr_list(&backup, __next_stmt, 0);
1577 static void *pop_backup(void)
1579 void *ret;
1581 ret = last_ptr_list(backup);
1582 delete_ptr_list_last(&backup);
1583 return ret;
1586 static void restore_flow_state(void)
1588 __next_stmt = pop_backup();
1589 __cur_stmt = pop_backup();
1590 __prev_stmt = pop_backup();
1592 cur_func_sym = pop_backup();
1593 switch_expr_stack = pop_backup();
1594 big_condition_stack = pop_backup();
1595 big_expression_stack = pop_backup();
1596 big_statement_stack = pop_backup();
1597 final_pass = PTR_INT(pop_backup()) >> 2;
1598 loop_count = PTR_INT(pop_backup()) >> 2;
1599 loop_num = PTR_INT(pop_backup()) >> 2;
1602 static void parse_inline(struct expression *call)
1604 struct symbol *base_type;
1605 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1606 struct timeval time_backup = fn_start_time;
1607 struct expression *orig_inline = __inline_fn;
1608 int orig_budget;
1610 save_flow_state();
1612 __pass_to_client(call, INLINE_FN_START);
1613 final_pass = 0; /* don't print anything */
1614 __inline_fn = call;
1615 orig_budget = inline_budget;
1616 inline_budget = inline_budget - 5;
1618 base_type = get_base_type(call->fn->symbol);
1619 cur_func_sym = call->fn->symbol;
1620 if (call->fn->symbol->ident)
1621 cur_func = call->fn->symbol->ident->name;
1622 else
1623 cur_func = NULL;
1624 set_position(call->fn->symbol->pos);
1626 save_all_states();
1627 big_statement_stack = NULL;
1628 big_expression_stack = NULL;
1629 big_condition_stack = NULL;
1630 switch_expr_stack = NULL;
1632 sm_debug("inline function: %s\n", cur_func);
1633 __unnullify_path();
1634 loop_num = 0;
1635 loop_count = 0;
1636 start_function_definition(call->fn->symbol);
1637 __split_stmt(base_type->stmt);
1638 __split_stmt(base_type->inline_stmt);
1639 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1640 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1642 free_expression_stack(&switch_expr_stack);
1643 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1644 nullify_path();
1645 free_goto_stack();
1647 restore_flow_state();
1648 fn_start_time = time_backup;
1649 cur_func = cur_func_bak;
1651 restore_all_states();
1652 set_position(call->pos);
1653 __inline_fn = orig_inline;
1654 inline_budget = orig_budget;
1655 __pass_to_client(call, INLINE_FN_END);
1658 static struct symbol_list *inlines_called;
1659 static void add_inline_function(struct symbol *sym)
1661 static struct symbol_list *already_added;
1662 struct symbol *tmp;
1664 FOR_EACH_PTR(already_added, tmp) {
1665 if (tmp == sym)
1666 return;
1667 } END_FOR_EACH_PTR(tmp);
1669 add_ptr_list(&already_added, sym);
1670 add_ptr_list(&inlines_called, sym);
1673 static void process_inlines(void)
1675 struct symbol *tmp;
1677 FOR_EACH_PTR(inlines_called, tmp) {
1678 split_function(tmp);
1679 } END_FOR_EACH_PTR(tmp);
1680 free_ptr_list(&inlines_called);
1683 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1685 struct symbol *sym;
1687 FOR_EACH_PTR_REVERSE(big_list, sym) {
1688 if (!sym->scope)
1689 continue;
1690 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1691 return sym;
1692 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1693 return sym;
1694 } END_FOR_EACH_PTR_REVERSE(sym);
1696 return NULL;
1699 static bool interesting_function(struct symbol *sym)
1701 static int prev_stream = -1;
1702 static bool prev_answer;
1703 const char *filename;
1704 int len;
1706 if (!(sym->ctype.modifiers & MOD_INLINE))
1707 return true;
1709 if (sym->pos.stream == prev_stream)
1710 return prev_answer;
1712 prev_stream = sym->pos.stream;
1713 prev_answer = false;
1715 filename = stream_name(sym->pos.stream);
1716 len = strlen(filename);
1717 if (len > 0 && filename[len - 1] == 'c')
1718 prev_answer = true;
1719 return prev_answer;
1722 static void split_inlines_in_scope(struct symbol *sym)
1724 struct symbol *base;
1725 struct symbol_list *scope_list;
1726 int stream;
1728 scope_list = sym->scope->symbols;
1729 stream = sym->pos.stream;
1731 /* find the last static symbol in the file */
1732 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1733 if (sym->pos.stream != stream)
1734 continue;
1735 if (sym->type != SYM_NODE)
1736 continue;
1737 base = get_base_type(sym);
1738 if (!base)
1739 continue;
1740 if (base->type != SYM_FN)
1741 continue;
1742 if (!base->inline_stmt)
1743 continue;
1744 if (!interesting_function(sym))
1745 continue;
1746 add_inline_function(sym);
1747 } END_FOR_EACH_PTR_REVERSE(sym);
1749 process_inlines();
1752 static void split_inlines(struct symbol_list *sym_list)
1754 struct symbol *sym;
1756 sym = get_last_scoped_symbol(sym_list, 0);
1757 if (sym)
1758 split_inlines_in_scope(sym);
1759 sym = get_last_scoped_symbol(sym_list, 1);
1760 if (sym)
1761 split_inlines_in_scope(sym);
1764 static struct stree *clone_estates_perm(struct stree *orig)
1766 struct stree *ret = NULL;
1767 struct sm_state *tmp;
1769 FOR_EACH_SM(orig, tmp) {
1770 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1771 } END_FOR_EACH_SM(tmp);
1773 return ret;
1776 struct position last_pos;
1777 static void split_c_file_functions(struct symbol_list *sym_list)
1779 struct symbol *sym;
1781 __unnullify_path();
1782 FOR_EACH_PTR(sym_list, sym) {
1783 set_position(sym->pos);
1784 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1785 __pass_to_client(sym, BASE_HOOK);
1786 fake_global_assign(sym);
1788 } END_FOR_EACH_PTR(sym);
1789 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1790 nullify_path();
1792 FOR_EACH_PTR(sym_list, sym) {
1793 set_position(sym->pos);
1794 last_pos = sym->pos;
1795 if (!interesting_function(sym))
1796 continue;
1797 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1798 split_function(sym);
1799 process_inlines();
1801 last_pos = sym->pos;
1802 } END_FOR_EACH_PTR(sym);
1803 split_inlines(sym_list);
1804 __pass_to_client(sym_list, END_FILE_HOOK);
1807 static int final_before_fake;
1808 void init_fake_env(void)
1810 if (!in_fake_env)
1811 final_before_fake = final_pass;
1812 in_fake_env++;
1813 __push_fake_cur_stree();
1814 final_pass = 0;
1817 void end_fake_env(void)
1819 __pop_fake_cur_stree();
1820 in_fake_env--;
1821 if (!in_fake_env)
1822 final_pass = final_before_fake;
1825 static void open_output_files(char *base_file)
1827 char buf[256];
1829 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1830 sm_outfd = fopen(buf, "w");
1831 if (!sm_outfd) {
1832 printf("Error: Cannot open %s\n", buf);
1833 exit(1);
1836 if (!option_info)
1837 return;
1839 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1840 sql_outfd = fopen(buf, "w");
1841 if (!sql_outfd) {
1842 printf("Error: Cannot open %s\n", buf);
1843 exit(1);
1846 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1847 caller_info_fd = fopen(buf, "w");
1848 if (!caller_info_fd) {
1849 printf("Error: Cannot open %s\n", buf);
1850 exit(1);
1854 void smatch(int argc, char **argv)
1856 struct string_list *filelist = NULL;
1857 struct symbol_list *sym_list;
1858 struct timeval stop, start;
1860 gettimeofday(&start, NULL);
1862 if (argc < 2) {
1863 printf("Usage: smatch [--debug] <filename.c>\n");
1864 exit(1);
1866 sparse_initialize(argc, argv, &filelist);
1867 set_valid_ptr_max();
1868 alloc_valid_ptr_rl();
1869 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1870 if (option_file_output)
1871 open_output_files(base_file);
1872 sym_list = sparse_keep_tokens(base_file);
1873 split_c_file_functions(sym_list);
1874 } END_FOR_EACH_PTR_NOTAG(base_file);
1876 gettimeofday(&stop, NULL);
1878 set_position(last_pos);
1879 if (option_time)
1880 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1881 if (option_mem)
1882 sm_msg("mem: %luKb", get_max_memory());