expressions: make assign_expression() take an op argument
[smatch.git] / smatch_flow.c
blob3f72d160c27197830a272b2cab5d60553b6f412e
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 static 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},
88 static void set_valid_ptr_max(void)
90 if (type_bits(&ptr_ctype) == 32)
91 valid_ptr_max = 2117777777;
92 else if (type_bits(&ptr_ctype) == 64)
93 valid_ptr_max = 2117777777777777777LL;
95 valid_ptr_max_sval.value = valid_ptr_max;
98 int outside_of_function(void)
100 return cur_func_sym == NULL;
103 const char *get_filename(void)
105 if (option_info)
106 return base_file;
107 if (option_full_path)
108 return full_filename;
109 return filename;
112 const char *get_base_file(void)
114 return base_file;
117 static void set_position(struct position pos)
119 int len;
120 static int prev_stream = -1;
122 if (in_fake_env)
123 return;
125 if (pos.stream == 0 && pos.line == 0)
126 return;
128 __smatch_lineno = pos.line;
130 if (pos.stream == prev_stream)
131 return;
133 filename = stream_name(pos.stream);
135 free(full_filename);
136 pathname = getcwd(NULL, 0);
137 if (pathname) {
138 len = strlen(pathname) + 1 + strlen(filename) + 1;
139 full_filename = malloc(len);
140 snprintf(full_filename, len, "%s/%s", pathname, filename);
141 } else {
142 full_filename = alloc_string(filename);
144 free(pathname);
147 int is_assigned_call(struct expression *expr)
149 struct expression *parent = expr_get_parent_expr(expr);
151 if (parent &&
152 parent->type == EXPR_ASSIGNMENT &&
153 parent->op == '=' &&
154 strip_expr(parent->right) == expr)
155 return 1;
158 * The problem is that the fake assignments we do in the initializer
159 * don't have a real assignment backing them so we don't set the
160 * ->parent pointer. This is a bit of an ugly hack, probably.
163 if (!parent && __cur_stmt && __cur_stmt->type == STMT_DECLARATION)
164 return 1;
166 return 0;
169 static int is_inline_func(struct expression *expr)
171 if (expr->type != EXPR_SYMBOL || !expr->symbol)
172 return 0;
173 if (expr->symbol->ctype.modifiers & MOD_INLINE)
174 return 1;
175 return 0;
178 static int is_noreturn_func(struct expression *expr)
180 if (expr->type != EXPR_SYMBOL || !expr->symbol)
181 return 0;
182 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
183 return 1;
184 return 0;
187 int inlinable(struct expression *expr)
189 struct symbol *sym;
190 struct statement *last_stmt = NULL;
192 if (__inline_fn) /* don't nest */
193 return 0;
195 if (expr->type != EXPR_SYMBOL || !expr->symbol)
196 return 0;
197 if (is_no_inline_function(expr->symbol->ident->name))
198 return 0;
199 sym = get_base_type(expr->symbol);
200 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
201 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
202 return 0;
203 if (sym->stmt->type != STMT_COMPOUND)
204 return 0;
205 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
207 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
208 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
209 return 0;
210 if (sym->inline_stmt->type != STMT_COMPOUND)
211 return 0;
212 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
215 if (!last_stmt)
216 return 0;
218 /* the magic numbers in this function are pulled out of my bum. */
219 if (last_stmt->pos.line > sym->pos.line + 20)
220 return 0;
222 return 1;
225 void __process_post_op_stack(void)
227 struct expression *expr;
229 FOR_EACH_PTR(post_op_stack, expr) {
230 __pass_to_client(expr, OP_HOOK);
231 } END_FOR_EACH_PTR(expr);
233 __free_ptr_list((struct ptr_list **)&post_op_stack);
236 static int handle_comma_assigns(struct expression *expr)
238 struct expression *right;
239 struct expression *assign;
241 right = strip_expr(expr->right);
242 if (right->type != EXPR_COMMA)
243 return 0;
245 __split_expr(right->left);
246 __process_post_op_stack();
248 assign = assign_expression(expr->left, '=', right->right);
249 __split_expr(assign);
251 return 1;
254 /* This is to handle *p++ = foo; assignments */
255 static int handle_postop_assigns(struct expression *expr)
257 struct expression *left, *fake_left;
258 struct expression *assign;
260 left = strip_expr(expr->left);
261 if (left->type != EXPR_PREOP || left->op != '*')
262 return 0;
263 left = strip_expr(left->unop);
264 if (left->type != EXPR_POSTOP)
265 return 0;
267 fake_left = deref_expression(strip_expr(left->unop));
268 assign = assign_expression(fake_left, '=', expr->right);
270 __split_expr(assign);
271 __split_expr(expr->left);
273 return 1;
276 static int prev_expression_is_getting_address(struct expression *expr)
278 struct expression *parent;
280 do {
281 parent = expr_get_parent_expr(expr);
283 if (!parent)
284 return 0;
285 if (parent->type == EXPR_PREOP && parent->op == '&')
286 return 1;
287 if (parent->type == EXPR_PREOP && parent->op == '(')
288 goto next;
289 if (parent->type == EXPR_DEREF && parent->op == '.')
290 goto next;
292 return 0;
293 next:
294 expr = parent;
295 } while (1);
298 static int handle__builtin_choose_expr(struct expression *expr)
300 struct expression *const_expr, *expr1, *expr2;
301 sval_t sval;
303 if (!sym_name_is("__builtin_choose_expr", expr->fn))
304 return 0;
306 const_expr = get_argument_from_call_expr(expr->args, 0);
307 expr1 = get_argument_from_call_expr(expr->args, 1);
308 expr2 = get_argument_from_call_expr(expr->args, 2);
310 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
311 return 0;
312 if (sval.value)
313 __split_expr(expr1);
314 else
315 __split_expr(expr2);
316 return 1;
319 void __split_expr(struct expression *expr)
321 if (!expr)
322 return;
324 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
326 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
327 return;
328 if (__in_fake_assign >= 4) /* don't allow too much nesting */
329 return;
331 push_expression(&big_expression_stack, expr);
332 set_position(expr->pos);
333 __pass_to_client(expr, EXPR_HOOK);
335 switch (expr->type) {
336 case EXPR_PREOP:
337 expr_set_parent_expr(expr->unop, expr);
339 if (expr->op == '*' &&
340 !prev_expression_is_getting_address(expr))
341 __pass_to_client(expr, DEREF_HOOK);
342 __split_expr(expr->unop);
343 __pass_to_client(expr, OP_HOOK);
344 break;
345 case EXPR_POSTOP:
346 expr_set_parent_expr(expr->unop, expr);
348 __split_expr(expr->unop);
349 push_expression(&post_op_stack, expr);
350 break;
351 case EXPR_STATEMENT:
352 __expr_stmt_count++;
353 if (expr->statement && !expr->statement) {
354 stmt_set_parent_stmt(expr->statement,
355 last_ptr_list((struct ptr_list *)big_statement_stack));
357 __split_stmt(expr->statement);
358 __expr_stmt_count--;
359 break;
360 case EXPR_LOGICAL:
361 case EXPR_COMPARE:
362 expr_set_parent_expr(expr->left, expr);
363 expr_set_parent_expr(expr->right, expr);
365 __pass_to_client(expr, LOGIC_HOOK);
366 __handle_logic(expr);
367 break;
368 case EXPR_BINOP:
369 expr_set_parent_expr(expr->left, expr);
370 expr_set_parent_expr(expr->right, expr);
372 __pass_to_client(expr, BINOP_HOOK);
373 case EXPR_COMMA:
374 expr_set_parent_expr(expr->left, expr);
375 expr_set_parent_expr(expr->right, expr);
377 __split_expr(expr->left);
378 __process_post_op_stack();
379 __split_expr(expr->right);
380 break;
381 case EXPR_ASSIGNMENT: {
382 struct expression *right;
384 expr_set_parent_expr(expr->left, expr);
385 expr_set_parent_expr(expr->right, expr);
387 right = strip_expr(expr->right);
388 if (!right)
389 break;
391 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
393 /* foo = !bar() */
394 if (__handle_condition_assigns(expr))
395 break;
396 /* foo = (x < 5 ? foo : 5); */
397 if (__handle_select_assigns(expr))
398 break;
399 /* foo = ({frob(); frob(); frob(); 1;}) */
400 if (__handle_expr_statement_assigns(expr))
401 break;
402 /* foo = (3, 4); */
403 if (handle_comma_assigns(expr))
404 break;
405 if (handle_postop_assigns(expr))
406 break;
408 __split_expr(expr->right);
409 if (outside_of_function())
410 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
411 else
412 __pass_to_client(expr, ASSIGNMENT_HOOK);
414 __fake_struct_member_assignments(expr);
416 if (expr->op == '=' && right->type == EXPR_CALL)
417 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
419 if (get_macro_name(right->pos) &&
420 get_macro_name(expr->pos) != get_macro_name(right->pos))
421 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
423 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
425 __split_expr(expr->left);
426 break;
428 case EXPR_DEREF:
429 expr_set_parent_expr(expr->deref, expr);
431 __pass_to_client(expr, DEREF_HOOK);
432 __split_expr(expr->deref);
433 break;
434 case EXPR_SLICE:
435 expr_set_parent_expr(expr->base, expr);
437 __split_expr(expr->base);
438 break;
439 case EXPR_CAST:
440 case EXPR_FORCE_CAST:
441 expr_set_parent_expr(expr->cast_expression, expr);
443 __pass_to_client(expr, CAST_HOOK);
444 __split_expr(expr->cast_expression);
445 break;
446 case EXPR_SIZEOF:
447 if (expr->cast_expression)
448 __pass_to_client(strip_parens(expr->cast_expression),
449 SIZEOF_HOOK);
450 break;
451 case EXPR_OFFSETOF:
452 case EXPR_ALIGNOF:
453 evaluate_expression(expr);
454 break;
455 case EXPR_CONDITIONAL:
456 case EXPR_SELECT:
457 expr_set_parent_expr(expr->conditional, expr);
458 expr_set_parent_expr(expr->cond_true, expr);
459 expr_set_parent_expr(expr->cond_false, expr);
461 if (known_condition_true(expr->conditional)) {
462 __split_expr(expr->cond_true);
463 break;
465 if (known_condition_false(expr->conditional)) {
466 __split_expr(expr->cond_false);
467 break;
469 __pass_to_client(expr, SELECT_HOOK);
470 __split_whole_condition(expr->conditional);
471 __split_expr(expr->cond_true);
472 __push_true_states();
473 __use_false_states();
474 __split_expr(expr->cond_false);
475 __merge_true_states();
476 break;
477 case EXPR_CALL:
478 expr_set_parent_expr(expr->fn, expr);
480 if (sym_name_is("__builtin_constant_p", expr->fn))
481 break;
482 if (handle__builtin_choose_expr(expr))
483 break;
484 split_expr_list(expr->args, expr);
485 __split_expr(expr->fn);
486 if (is_inline_func(expr->fn))
487 add_inline_function(expr->fn->symbol);
488 if (inlinable(expr->fn))
489 __inline_call = 1;
490 __process_post_op_stack();
491 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
492 __pass_to_client(expr, FUNCTION_CALL_HOOK);
493 __inline_call = 0;
494 if (inlinable(expr->fn)) {
495 parse_inline(expr);
497 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
498 if (is_noreturn_func(expr->fn))
499 nullify_path();
500 break;
501 case EXPR_INITIALIZER:
502 split_expr_list(expr->expr_list, expr);
503 break;
504 case EXPR_IDENTIFIER:
505 expr_set_parent_expr(expr->ident_expression, expr);
506 __split_expr(expr->ident_expression);
507 break;
508 case EXPR_INDEX:
509 expr_set_parent_expr(expr->idx_expression, expr);
510 __split_expr(expr->idx_expression);
511 break;
512 case EXPR_POS:
513 expr_set_parent_expr(expr->init_expr, expr);
514 __split_expr(expr->init_expr);
515 break;
516 case EXPR_SYMBOL:
517 __pass_to_client(expr, SYM_HOOK);
518 break;
519 case EXPR_STRING:
520 __pass_to_client(expr, STRING_HOOK);
521 break;
522 default:
523 break;
525 pop_expression(&big_expression_stack);
528 static int is_forever_loop(struct statement *stmt)
530 struct expression *expr;
531 sval_t sval;
533 expr = strip_expr(stmt->iterator_pre_condition);
534 if (!expr)
535 expr = stmt->iterator_post_condition;
536 if (!expr) {
537 /* this is a for(;;) loop... */
538 return 1;
541 if (get_value(expr, &sval) && sval.value != 0)
542 return 1;
544 return 0;
547 static int loop_num;
548 static char *get_loop_name(int num)
550 char buf[256];
552 snprintf(buf, 255, "-loop%d", num);
553 buf[255] = '\0';
554 return alloc_sname(buf);
558 * Pre Loops are while and for loops.
560 static void handle_pre_loop(struct statement *stmt)
562 int once_through; /* we go through the loop at least once */
563 struct sm_state *extra_sm = NULL;
564 int unchanged = 0;
565 char *loop_name;
566 struct stree *stree = NULL;
567 struct sm_state *sm = NULL;
569 loop_name = get_loop_name(loop_num);
570 loop_num++;
572 __split_stmt(stmt->iterator_pre_statement);
573 __prev_stmt = stmt->iterator_pre_statement;
575 once_through = implied_condition_true(stmt->iterator_pre_condition);
577 loop_count++;
578 __push_continues();
579 __push_breaks();
581 __merge_gotos(loop_name, NULL);
583 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
584 __in_pre_condition++;
585 __pass_to_client(stmt, PRELOOP_HOOK);
586 __split_whole_condition(stmt->iterator_pre_condition);
587 __in_pre_condition--;
588 FOR_EACH_SM(stree, sm) {
589 set_state(sm->owner, sm->name, sm->sym, sm->state);
590 } END_FOR_EACH_SM(sm);
591 free_stree(&stree);
592 if (extra_sm)
593 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
595 if (option_assume_loops)
596 once_through = 1;
598 __split_stmt(stmt->iterator_statement);
599 if (is_forever_loop(stmt)) {
600 __merge_continues();
601 __save_gotos(loop_name, NULL);
603 __push_fake_cur_stree();
604 __split_stmt(stmt->iterator_post_statement);
605 stree = __pop_fake_cur_stree();
607 __discard_false_states();
608 __use_breaks();
610 if (!__path_is_null())
611 __merge_stree_into_cur(stree);
612 free_stree(&stree);
613 } else {
614 __merge_continues();
615 unchanged = __iterator_unchanged(extra_sm);
616 __split_stmt(stmt->iterator_post_statement);
617 __prev_stmt = stmt->iterator_post_statement;
618 __cur_stmt = stmt;
620 __save_gotos(loop_name, NULL);
621 __in_pre_condition++;
622 __split_whole_condition(stmt->iterator_pre_condition);
623 __in_pre_condition--;
624 nullify_path();
625 __merge_false_states();
626 if (once_through)
627 __discard_false_states();
628 else
629 __merge_false_states();
631 if (extra_sm && unchanged)
632 __extra_pre_loop_hook_after(extra_sm,
633 stmt->iterator_post_statement,
634 stmt->iterator_pre_condition);
635 __merge_breaks();
637 loop_count--;
641 * Post loops are do {} while();
643 static void handle_post_loop(struct statement *stmt)
645 char *loop_name;
647 loop_name = get_loop_name(loop_num);
648 loop_num++;
649 loop_count++;
651 __push_continues();
652 __push_breaks();
653 __merge_gotos(loop_name, NULL);
654 __split_stmt(stmt->iterator_statement);
655 __merge_continues();
656 if (!is_zero(stmt->iterator_post_condition))
657 __save_gotos(loop_name, NULL);
659 if (is_forever_loop(stmt)) {
660 __use_breaks();
661 } else {
662 __split_whole_condition(stmt->iterator_post_condition);
663 __use_false_states();
664 __merge_breaks();
666 loop_count--;
669 static int empty_statement(struct statement *stmt)
671 if (!stmt)
672 return 0;
673 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
674 return 1;
675 return 0;
678 static int last_stmt_on_same_line(void)
680 struct statement *stmt;
681 int i = 0;
683 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
684 if (!i++)
685 continue;
686 if (stmt->pos.line == get_lineno())
687 return 1;
688 return 0;
689 } END_FOR_EACH_PTR_REVERSE(stmt);
690 return 0;
693 static void split_asm_constraints(struct expression_list *expr_list)
695 struct expression *expr;
696 int state = 0;
698 FOR_EACH_PTR(expr_list, expr) {
699 switch (state) {
700 case 0: /* identifier */
701 case 1: /* constraint */
702 state++;
703 continue;
704 case 2: /* expression */
705 state = 0;
706 __split_expr(expr);
707 continue;
709 } END_FOR_EACH_PTR(expr);
712 static int is_case_val(struct statement *stmt, sval_t sval)
714 sval_t case_sval;
716 if (stmt->type != STMT_CASE)
717 return 0;
718 if (!stmt->case_expression) {
719 __set_default();
720 return 1;
722 if (!get_value(stmt->case_expression, &case_sval))
723 return 0;
724 if (case_sval.value == sval.value)
725 return 1;
726 return 0;
729 static struct range_list *get_case_rl(struct expression *switch_expr,
730 struct expression *case_expr,
731 struct expression *case_to)
733 sval_t start, end;
734 struct range_list *rl = NULL;
735 struct symbol *switch_type;
737 switch_type = get_type(switch_expr);
738 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
739 start = sval_cast(switch_type, start);
740 end = sval_cast(switch_type, end);
741 add_range(&rl, start, end);
742 } else if (get_value(case_expr, &start)) {
743 start = sval_cast(switch_type, start);
744 add_range(&rl, start, start);
747 return rl;
750 static void split_known_switch(struct statement *stmt, sval_t sval)
752 struct statement *tmp;
753 struct range_list *rl;
755 __split_expr(stmt->switch_expression);
756 sval = sval_cast(get_type(stmt->switch_expression), sval);
758 push_expression(&switch_expr_stack, stmt->switch_expression);
759 __save_switch_states(top_expression(switch_expr_stack));
760 nullify_path();
761 __push_default();
762 __push_breaks();
764 stmt = stmt->switch_statement;
766 __push_scope_hooks();
767 FOR_EACH_PTR(stmt->stmts, tmp) {
768 __smatch_lineno = tmp->pos.line;
769 if (is_case_val(tmp, sval)) {
770 rl = alloc_rl(sval, sval);
771 __merge_switches(top_expression(switch_expr_stack), rl);
772 __pass_case_to_client(top_expression(switch_expr_stack), rl);
774 if (__path_is_null())
775 continue;
776 __split_stmt(tmp);
777 if (__path_is_null()) {
778 __set_default();
779 goto out;
781 } END_FOR_EACH_PTR(tmp);
782 out:
783 __call_scope_hooks();
784 if (!__pop_default())
785 __merge_switches(top_expression(switch_expr_stack), NULL);
786 __discard_switches();
787 __merge_breaks();
788 pop_expression(&switch_expr_stack);
791 static void split_case(struct statement *stmt)
793 struct range_list *rl = NULL;
795 expr_set_parent_stmt(stmt->case_expression, stmt);
796 expr_set_parent_stmt(stmt->case_to, stmt);
798 rl = get_case_rl(top_expression(switch_expr_stack),
799 stmt->case_expression, stmt->case_to);
800 while (stmt->case_statement->type == STMT_CASE) {
801 struct range_list *tmp;
803 tmp = get_case_rl(top_expression(switch_expr_stack),
804 stmt->case_statement->case_expression,
805 stmt->case_statement->case_to);
806 if (!tmp)
807 break;
808 rl = rl_union(rl, tmp);
809 if (!stmt->case_expression)
810 __set_default();
811 stmt = stmt->case_statement;
814 __merge_switches(top_expression(switch_expr_stack), rl);
816 if (!stmt->case_expression)
817 __set_default();
818 __split_stmt(stmt->case_statement);
821 int time_parsing_function(void)
823 return ms_since(&fn_start_time) / 1000;
826 static int taking_too_long(void)
828 if (time_parsing_function() > 60 * 5) /* five minutes */
829 return 1;
830 return 0;
833 static int is_last_stmt(struct statement *cur_stmt)
835 struct symbol *fn = get_base_type(cur_func_sym);
836 struct statement *stmt;
838 if (!fn)
839 return 0;
840 stmt = fn->stmt;
841 if (!stmt)
842 stmt = fn->inline_stmt;
843 if (!stmt || stmt->type != STMT_COMPOUND)
844 return 0;
845 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
846 if (stmt && stmt->type == STMT_LABEL)
847 stmt = stmt->label_statement;
848 if (stmt == cur_stmt)
849 return 1;
850 return 0;
853 static void handle_backward_goto(struct statement *goto_stmt)
855 const char *goto_name, *label_name;
856 struct statement *func_stmt;
857 struct symbol *base_type = get_base_type(cur_func_sym);
858 struct statement *tmp;
859 int found = 0;
861 if (!option_info)
862 return;
863 if (last_goto_statement_handled)
864 return;
865 last_goto_statement_handled = 1;
867 if (!goto_stmt->goto_label ||
868 goto_stmt->goto_label->type != SYM_LABEL ||
869 !goto_stmt->goto_label->ident)
870 return;
871 goto_name = goto_stmt->goto_label->ident->name;
873 func_stmt = base_type->stmt;
874 if (!func_stmt)
875 func_stmt = base_type->inline_stmt;
876 if (!func_stmt)
877 return;
878 if (func_stmt->type != STMT_COMPOUND)
879 return;
881 FOR_EACH_PTR(func_stmt->stmts, tmp) {
882 if (!found) {
883 if (tmp->type != STMT_LABEL)
884 continue;
885 if (!tmp->label_identifier ||
886 tmp->label_identifier->type != SYM_LABEL ||
887 !tmp->label_identifier->ident)
888 continue;
889 label_name = tmp->label_identifier->ident->name;
890 if (strcmp(goto_name, label_name) != 0)
891 continue;
892 found = 1;
894 __split_stmt(tmp);
895 } END_FOR_EACH_PTR(tmp);
898 static void fake_a_return(void)
900 struct symbol *return_type;
902 nullify_path();
903 __unnullify_path();
905 return_type = get_real_base_type(cur_func_sym);
906 return_type = get_real_base_type(return_type);
907 if (return_type != &void_ctype) {
908 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
909 nullify_path();
913 static void fake_an_empty_default(struct position pos)
915 static struct statement none = {};
917 none.pos = pos;
918 none.type = STMT_NONE;
919 __merge_switches(top_expression(switch_expr_stack), NULL);
920 __split_stmt(&none);
923 static void split_compound(struct statement *stmt)
925 struct statement *prev = NULL;
926 struct statement *cur = NULL;
927 struct statement *next;
929 __push_scope_hooks();
931 FOR_EACH_PTR(stmt->stmts, next) {
932 /* just set them all ahead of time */
933 stmt_set_parent_stmt(next, stmt);
935 if (cur) {
936 __prev_stmt = prev;
937 __next_stmt = next;
938 __cur_stmt = cur;
939 __split_stmt(cur);
941 prev = cur;
942 cur = next;
943 } END_FOR_EACH_PTR(next);
944 if (cur) {
945 __prev_stmt = prev;
946 __cur_stmt = cur;
947 __next_stmt = NULL;
948 __split_stmt(cur);
952 * For function scope, then delay calling the scope hooks until the
953 * end of function hooks can run. I'm not positive this is the right
954 * thing...
956 if (!is_last_stmt(cur))
957 __call_scope_hooks();
961 * This is a hack, work around for detecting empty functions.
963 static int need_delayed_scope_hooks(void)
965 struct symbol *fn = get_base_type(cur_func_sym);
966 struct statement *stmt;
968 if (!fn)
969 return 0;
970 stmt = fn->stmt;
971 if (!stmt)
972 stmt = fn->inline_stmt;
973 if (stmt && stmt->type == STMT_COMPOUND)
974 return 1;
975 return 0;
978 void __split_label_stmt(struct statement *stmt)
980 if (stmt->label_identifier &&
981 stmt->label_identifier->type == SYM_LABEL &&
982 stmt->label_identifier->ident) {
983 loop_count |= 0x0800000;
984 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
988 static void find_asm_gotos(struct statement *stmt)
990 struct symbol *sym;
992 FOR_EACH_PTR(stmt->asm_labels, sym) {
993 __save_gotos(sym->ident->name, sym);
994 } END_FOR_EACH_PTR(sym);
997 void __split_stmt(struct statement *stmt)
999 sval_t sval;
1001 if (!stmt)
1002 goto out;
1004 if (!__in_fake_assign)
1005 __silence_warnings_for_stmt = false;
1007 if (__bail_on_rest_of_function)
1008 return;
1010 if (out_of_memory() || taking_too_long()) {
1011 struct timeval stop;
1013 gettimeofday(&stop, NULL);
1015 __bail_on_rest_of_function = 1;
1016 final_pass = 1;
1017 sm_msg("Function too hairy. Giving up. %lu seconds",
1018 stop.tv_sec - fn_start_time.tv_sec);
1019 fake_a_return();
1020 final_pass = 0; /* turn off sm_msg() from here */
1021 return;
1024 add_ptr_list(&big_statement_stack, stmt);
1025 free_expression_stack(&big_expression_stack);
1026 set_position(stmt->pos);
1027 __pass_to_client(stmt, STMT_HOOK);
1029 switch (stmt->type) {
1030 case STMT_DECLARATION:
1031 split_declaration(stmt->declaration);
1032 break;
1033 case STMT_RETURN:
1034 expr_set_parent_stmt(stmt->ret_value, stmt);
1036 __split_expr(stmt->ret_value);
1037 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1038 __process_post_op_stack();
1039 nullify_path();
1040 break;
1041 case STMT_EXPRESSION:
1042 expr_set_parent_stmt(stmt->expression, stmt);
1043 expr_set_parent_stmt(stmt->context, stmt);
1045 __split_expr(stmt->expression);
1046 break;
1047 case STMT_COMPOUND:
1048 split_compound(stmt);
1049 break;
1050 case STMT_IF:
1051 stmt_set_parent_stmt(stmt->if_true, stmt);
1052 stmt_set_parent_stmt(stmt->if_false, stmt);
1053 expr_set_parent_stmt(stmt->if_conditional, stmt);
1055 if (known_condition_true(stmt->if_conditional)) {
1056 __split_stmt(stmt->if_true);
1057 break;
1059 if (known_condition_false(stmt->if_conditional)) {
1060 __split_stmt(stmt->if_false);
1061 break;
1063 __split_whole_condition(stmt->if_conditional);
1064 __split_stmt(stmt->if_true);
1065 if (empty_statement(stmt->if_true) &&
1066 last_stmt_on_same_line() &&
1067 !get_macro_name(stmt->if_true->pos))
1068 sm_msg("warn: if();");
1069 __push_true_states();
1070 __use_false_states();
1071 __split_stmt(stmt->if_false);
1072 __merge_true_states();
1073 break;
1074 case STMT_ITERATOR:
1075 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1076 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1077 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1078 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1079 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1081 if (stmt->iterator_pre_condition)
1082 handle_pre_loop(stmt);
1083 else if (stmt->iterator_post_condition)
1084 handle_post_loop(stmt);
1085 else {
1086 // these are for(;;) type loops.
1087 handle_pre_loop(stmt);
1089 break;
1090 case STMT_SWITCH:
1091 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1092 expr_set_parent_stmt(stmt->switch_expression, stmt);
1094 if (get_value(stmt->switch_expression, &sval)) {
1095 split_known_switch(stmt, sval);
1096 break;
1098 __split_expr(stmt->switch_expression);
1099 push_expression(&switch_expr_stack, stmt->switch_expression);
1100 __save_switch_states(top_expression(switch_expr_stack));
1101 nullify_path();
1102 __push_default();
1103 __push_breaks();
1104 __split_stmt(stmt->switch_statement);
1105 if (!__pop_default() && have_remaining_cases())
1106 fake_an_empty_default(stmt->pos);
1107 __discard_switches();
1108 __merge_breaks();
1109 pop_expression(&switch_expr_stack);
1110 break;
1111 case STMT_CASE:
1112 split_case(stmt);
1113 break;
1114 case STMT_LABEL:
1115 __split_label_stmt(stmt);
1116 __split_stmt(stmt->label_statement);
1117 break;
1118 case STMT_GOTO:
1119 expr_set_parent_stmt(stmt->goto_expression, stmt);
1121 __split_expr(stmt->goto_expression);
1122 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1123 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1124 __process_breaks();
1125 } else if (!strcmp(stmt->goto_label->ident->name,
1126 "continue")) {
1127 __process_continues();
1129 } else if (stmt->goto_label &&
1130 stmt->goto_label->type == SYM_LABEL &&
1131 stmt->goto_label->ident) {
1132 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1134 nullify_path();
1135 if (is_last_stmt(stmt))
1136 handle_backward_goto(stmt);
1137 break;
1138 case STMT_NONE:
1139 break;
1140 case STMT_ASM:
1141 expr_set_parent_stmt(stmt->asm_string, stmt);
1143 find_asm_gotos(stmt);
1144 __pass_to_client(stmt, ASM_HOOK);
1145 __split_expr(stmt->asm_string);
1146 split_asm_constraints(stmt->asm_outputs);
1147 split_asm_constraints(stmt->asm_inputs);
1148 split_asm_constraints(stmt->asm_clobbers);
1149 break;
1150 case STMT_CONTEXT:
1151 break;
1152 case STMT_RANGE:
1153 __split_expr(stmt->range_expression);
1154 __split_expr(stmt->range_low);
1155 __split_expr(stmt->range_high);
1156 break;
1158 __pass_to_client(stmt, STMT_HOOK_AFTER);
1159 out:
1160 __process_post_op_stack();
1163 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1165 struct expression *expr;
1167 FOR_EACH_PTR(expr_list, expr) {
1168 expr_set_parent_expr(expr, parent);
1169 __split_expr(expr);
1170 __process_post_op_stack();
1171 } END_FOR_EACH_PTR(expr);
1174 static void split_sym(struct symbol *sym)
1176 if (!sym)
1177 return;
1178 if (!(sym->namespace & NS_SYMBOL))
1179 return;
1181 __split_stmt(sym->stmt);
1182 __split_expr(sym->array_size);
1183 split_symlist(sym->arguments);
1184 split_symlist(sym->symbol_list);
1185 __split_stmt(sym->inline_stmt);
1186 split_symlist(sym->inline_symbol_list);
1189 static void split_symlist(struct symbol_list *sym_list)
1191 struct symbol *sym;
1193 FOR_EACH_PTR(sym_list, sym) {
1194 split_sym(sym);
1195 } END_FOR_EACH_PTR(sym);
1198 typedef void (fake_cb)(struct expression *expr);
1200 static int member_to_number(struct expression *expr, struct ident *member)
1202 struct symbol *type, *tmp;
1203 char *name;
1204 int i;
1206 if (!member)
1207 return -1;
1208 name = member->name;
1210 type = get_type(expr);
1211 if (!type || type->type != SYM_STRUCT)
1212 return -1;
1214 i = -1;
1215 FOR_EACH_PTR(type->symbol_list, tmp) {
1216 i++;
1217 if (!tmp->ident)
1218 continue;
1219 if (strcmp(name, tmp->ident->name) == 0)
1220 return i;
1221 } END_FOR_EACH_PTR(tmp);
1222 return -1;
1225 static struct ident *number_to_member(struct expression *expr, int num)
1227 struct symbol *type, *member;
1228 int i = 0;
1230 type = get_type(expr);
1231 if (!type || type->type != SYM_STRUCT)
1232 return NULL;
1234 FOR_EACH_PTR(type->symbol_list, member) {
1235 if (i == num)
1236 return member->ident;
1237 i++;
1238 } END_FOR_EACH_PTR(member);
1239 return NULL;
1242 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1244 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1246 struct expression *edge_member, *assign;
1247 struct symbol *base = get_real_base_type(member);
1248 struct symbol *tmp;
1250 if (member->ident)
1251 expr = member_expression(expr, '.', member->ident);
1253 FOR_EACH_PTR(base->symbol_list, tmp) {
1254 struct symbol *type;
1256 type = get_real_base_type(tmp);
1257 if (!type)
1258 continue;
1260 edge_member = member_expression(expr, '.', tmp->ident);
1261 if (get_extra_state(edge_member))
1262 continue;
1264 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1265 set_inner_struct_members(expr, tmp);
1266 continue;
1269 if (!tmp->ident)
1270 continue;
1272 assign = assign_expression(edge_member, '=', zero_expr());
1273 __split_expr(assign);
1274 } END_FOR_EACH_PTR(tmp);
1279 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1281 struct symbol *tmp;
1282 struct expression *member = NULL;
1283 struct expression *assign;
1284 int op = '*';
1286 if (expr->type == EXPR_PREOP && expr->op == '&') {
1287 expr = strip_expr(expr->unop);
1288 op = '.';
1291 FOR_EACH_PTR(type->symbol_list, tmp) {
1292 type = get_real_base_type(tmp);
1293 if (!type)
1294 continue;
1296 if (tmp->ident) {
1297 member = member_expression(expr, op, tmp->ident);
1298 if (get_extra_state(member))
1299 continue;
1302 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1303 set_inner_struct_members(expr, tmp);
1304 continue;
1306 if (type->type == SYM_ARRAY)
1307 continue;
1308 if (!tmp->ident)
1309 continue;
1311 assign = assign_expression(member, '=', zero_expr());
1312 __split_expr(assign);
1313 } END_FOR_EACH_PTR(tmp);
1316 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1318 struct expression *deref, *assign, *tmp, *right;
1319 struct symbol *struct_type, *type;
1320 struct ident *member;
1321 int member_idx;
1323 struct_type = get_type(symbol);
1324 if (!struct_type ||
1325 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1326 return;
1329 * We're parsing an initializer that could look something like this:
1330 * struct foo foo = {
1331 * 42,
1332 * .whatever.xxx = 11,
1333 * .zzz = 12,
1334 * };
1336 * So what we have here is a list with 42, .whatever, and .zzz. We need
1337 * to break it up into left and right sides of the assignments.
1340 member_idx = 0;
1341 FOR_EACH_PTR(members, tmp) {
1342 deref = NULL;
1343 if (tmp->type == EXPR_IDENTIFIER) {
1344 member_idx = member_to_number(symbol, tmp->expr_ident);
1345 while (tmp->type == EXPR_IDENTIFIER) {
1346 member = tmp->expr_ident;
1347 tmp = tmp->ident_expression;
1348 if (deref)
1349 deref = member_expression(deref, '.', member);
1350 else
1351 deref = member_expression(symbol, '.', member);
1353 } else {
1354 member = number_to_member(symbol, member_idx);
1355 deref = member_expression(symbol, '.', member);
1357 right = tmp;
1358 member_idx++;
1359 if (right->type == EXPR_INITIALIZER) {
1360 type = get_type(deref);
1361 if (type && type->type == SYM_ARRAY)
1362 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1363 else
1364 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1365 } else {
1366 assign = assign_expression(deref, '=', right);
1367 fake_cb(assign);
1369 } END_FOR_EACH_PTR(tmp);
1371 set_unset_to_zero(struct_type, symbol);
1374 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1376 fake_member_assigns_helper(symbol_expression(sym),
1377 sym->initializer->expr_list, fake_cb);
1380 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1382 struct expression *offset, *binop, *assign, *tmp;
1383 struct symbol *type;
1384 int idx;
1386 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1387 return;
1389 idx = 0;
1390 FOR_EACH_PTR(expr_list, tmp) {
1391 if (tmp->type == EXPR_INDEX) {
1392 if (tmp->idx_from != tmp->idx_to)
1393 return;
1394 idx = tmp->idx_from;
1395 if (!tmp->idx_expression)
1396 goto next;
1397 tmp = tmp->idx_expression;
1399 offset = value_expr(idx);
1400 binop = array_element_expression(array, offset);
1401 if (tmp->type == EXPR_INITIALIZER) {
1402 type = get_type(binop);
1403 if (type && type->type == SYM_ARRAY)
1404 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1405 else
1406 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1407 } else {
1408 assign = assign_expression(binop, '=', tmp);
1409 fake_cb(assign);
1411 next:
1412 idx++;
1413 } END_FOR_EACH_PTR(tmp);
1416 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1418 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1421 static void fake_assign_expr(struct symbol *sym)
1423 struct expression *assign, *symbol;
1425 symbol = symbol_expression(sym);
1426 assign = assign_expression(symbol, '=', sym->initializer);
1427 __split_expr(assign);
1430 static void call_split_expr(struct expression *expr)
1432 __split_expr(expr);
1435 static void do_initializer_stuff(struct symbol *sym)
1437 if (!sym->initializer)
1438 return;
1440 if (sym->initializer->type == EXPR_INITIALIZER) {
1441 if (get_real_base_type(sym)->type == SYM_ARRAY)
1442 fake_element_assigns(sym, call_split_expr);
1443 else
1444 fake_member_assigns(sym, call_split_expr);
1445 } else {
1446 fake_assign_expr(sym);
1450 static void split_declaration(struct symbol_list *sym_list)
1452 struct symbol *sym;
1454 FOR_EACH_PTR(sym_list, sym) {
1455 __pass_to_client(sym, DECLARATION_HOOK);
1456 do_initializer_stuff(sym);
1457 split_sym(sym);
1458 } END_FOR_EACH_PTR(sym);
1461 static void call_global_assign_hooks(struct expression *assign)
1463 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1466 static void fake_global_assign(struct symbol *sym)
1468 struct expression *assign, *symbol;
1470 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1471 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1472 fake_element_assigns(sym, call_global_assign_hooks);
1473 } else if (sym->initializer) {
1474 symbol = symbol_expression(sym);
1475 assign = assign_expression(symbol, '=', sym->initializer);
1476 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1477 } else {
1478 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1480 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1481 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1482 fake_member_assigns(sym, call_global_assign_hooks);
1483 } else if (sym->initializer) {
1484 symbol = symbol_expression(sym);
1485 assign = assign_expression(symbol, '=', sym->initializer);
1486 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1487 } else {
1488 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1490 } else {
1491 symbol = symbol_expression(sym);
1492 if (sym->initializer)
1493 assign = assign_expression(symbol, '=', sym->initializer);
1494 else
1495 assign = assign_expression(symbol, '=', zero_expr());
1496 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1500 static void start_function_definition(struct symbol *sym)
1502 __in_function_def = 1;
1503 __pass_to_client(sym, FUNC_DEF_HOOK);
1504 __in_function_def = 0;
1505 __pass_to_client(sym, AFTER_DEF_HOOK);
1509 static void split_function(struct symbol *sym)
1511 struct symbol *base_type = get_base_type(sym);
1513 if (!base_type->stmt && !base_type->inline_stmt)
1514 return;
1516 gettimeofday(&fn_start_time, NULL);
1517 cur_func_sym = sym;
1518 if (sym->ident)
1519 cur_func = sym->ident->name;
1520 set_position(sym->pos);
1521 loop_count = 0;
1522 last_goto_statement_handled = 0;
1523 sm_debug("new function: %s\n", cur_func);
1524 __stree_id = 0;
1525 if (option_two_passes) {
1526 __unnullify_path();
1527 loop_num = 0;
1528 final_pass = 0;
1529 start_function_definition(sym);
1530 __split_stmt(base_type->stmt);
1531 __split_stmt(base_type->inline_stmt);
1532 nullify_path();
1534 __unnullify_path();
1535 loop_num = 0;
1536 final_pass = 1;
1537 start_function_definition(sym);
1538 __split_stmt(base_type->stmt);
1539 __split_stmt(base_type->inline_stmt);
1540 __pass_to_client(sym, END_FUNC_HOOK);
1541 if (need_delayed_scope_hooks())
1542 __call_scope_hooks();
1543 __pass_to_client(sym, AFTER_FUNC_HOOK);
1545 clear_all_states();
1546 cur_func_sym = NULL;
1547 cur_func = NULL;
1548 free_data_info_allocs();
1549 free_expression_stack(&switch_expr_stack);
1550 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1551 __bail_on_rest_of_function = 0;
1554 static void save_flow_state(void)
1556 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1557 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1558 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1560 __add_ptr_list(&backup, big_statement_stack, 0);
1561 __add_ptr_list(&backup, big_expression_stack, 0);
1562 __add_ptr_list(&backup, big_condition_stack, 0);
1563 __add_ptr_list(&backup, switch_expr_stack, 0);
1565 __add_ptr_list(&backup, cur_func_sym, 0);
1567 __add_ptr_list(&backup, __prev_stmt, 0);
1568 __add_ptr_list(&backup, __cur_stmt, 0);
1569 __add_ptr_list(&backup, __next_stmt, 0);
1573 static void *pop_backup(void)
1575 void *ret;
1577 ret = last_ptr_list(backup);
1578 delete_ptr_list_last(&backup);
1579 return ret;
1582 static void restore_flow_state(void)
1584 __next_stmt = pop_backup();
1585 __cur_stmt = pop_backup();
1586 __prev_stmt = pop_backup();
1588 cur_func_sym = pop_backup();
1589 switch_expr_stack = pop_backup();
1590 big_condition_stack = pop_backup();
1591 big_expression_stack = pop_backup();
1592 big_statement_stack = pop_backup();
1593 final_pass = PTR_INT(pop_backup()) >> 2;
1594 loop_count = PTR_INT(pop_backup()) >> 2;
1595 loop_num = PTR_INT(pop_backup()) >> 2;
1598 static void parse_inline(struct expression *call)
1600 struct symbol *base_type;
1601 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1602 struct timeval time_backup = fn_start_time;
1604 save_flow_state();
1606 __pass_to_client(call, INLINE_FN_START);
1607 final_pass = 0; /* don't print anything */
1608 __inline_fn = call;
1610 base_type = get_base_type(call->fn->symbol);
1611 cur_func_sym = call->fn->symbol;
1612 if (call->fn->symbol->ident)
1613 cur_func = call->fn->symbol->ident->name;
1614 else
1615 cur_func = NULL;
1616 set_position(call->fn->symbol->pos);
1618 save_all_states();
1619 big_statement_stack = NULL;
1620 big_expression_stack = NULL;
1621 big_condition_stack = NULL;
1622 switch_expr_stack = NULL;
1624 sm_debug("inline function: %s\n", cur_func);
1625 __unnullify_path();
1626 loop_num = 0;
1627 loop_count = 0;
1628 start_function_definition(call->fn->symbol);
1629 __split_stmt(base_type->stmt);
1630 __split_stmt(base_type->inline_stmt);
1631 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1632 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1634 free_expression_stack(&switch_expr_stack);
1635 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1636 nullify_path();
1637 free_goto_stack();
1639 restore_flow_state();
1640 fn_start_time = time_backup;
1641 cur_func = cur_func_bak;
1643 restore_all_states();
1644 set_position(call->pos);
1645 __inline_fn = NULL;
1646 __pass_to_client(call, INLINE_FN_END);
1649 static struct symbol_list *inlines_called;
1650 static void add_inline_function(struct symbol *sym)
1652 static struct symbol_list *already_added;
1653 struct symbol *tmp;
1655 FOR_EACH_PTR(already_added, tmp) {
1656 if (tmp == sym)
1657 return;
1658 } END_FOR_EACH_PTR(tmp);
1660 add_ptr_list(&already_added, sym);
1661 add_ptr_list(&inlines_called, sym);
1664 static void process_inlines(void)
1666 struct symbol *tmp;
1668 FOR_EACH_PTR(inlines_called, tmp) {
1669 split_function(tmp);
1670 } END_FOR_EACH_PTR(tmp);
1671 free_ptr_list(&inlines_called);
1674 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1676 struct symbol *sym;
1678 FOR_EACH_PTR_REVERSE(big_list, sym) {
1679 if (!sym->scope)
1680 continue;
1681 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1682 return sym;
1683 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1684 return sym;
1685 } END_FOR_EACH_PTR_REVERSE(sym);
1687 return NULL;
1690 static void split_inlines_in_scope(struct symbol *sym)
1692 struct symbol *base;
1693 struct symbol_list *scope_list;
1694 int stream;
1696 scope_list = sym->scope->symbols;
1697 stream = sym->pos.stream;
1699 /* find the last static symbol in the file */
1700 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1701 if (sym->pos.stream != stream)
1702 continue;
1703 if (sym->type != SYM_NODE)
1704 continue;
1705 base = get_base_type(sym);
1706 if (!base)
1707 continue;
1708 if (base->type != SYM_FN)
1709 continue;
1710 if (!base->inline_stmt)
1711 continue;
1712 add_inline_function(sym);
1713 } END_FOR_EACH_PTR_REVERSE(sym);
1715 process_inlines();
1718 static void split_inlines(struct symbol_list *sym_list)
1720 struct symbol *sym;
1722 sym = get_last_scoped_symbol(sym_list, 0);
1723 if (sym)
1724 split_inlines_in_scope(sym);
1725 sym = get_last_scoped_symbol(sym_list, 1);
1726 if (sym)
1727 split_inlines_in_scope(sym);
1730 static struct stree *clone_estates_perm(struct stree *orig)
1732 struct stree *ret = NULL;
1733 struct sm_state *tmp;
1735 FOR_EACH_SM(orig, tmp) {
1736 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1737 } END_FOR_EACH_SM(tmp);
1739 return ret;
1742 static bool interesting_function(struct symbol *sym)
1744 static int prev_stream = -1;
1745 static bool prev_answer;
1746 const char *filename;
1747 int len;
1750 if (!(sym->ctype.modifiers & MOD_INLINE))
1751 return true;
1753 if (sym->pos.stream == prev_stream)
1754 return prev_answer;
1756 prev_stream = sym->pos.stream;
1757 prev_answer = false;
1759 filename = stream_name(sym->pos.stream);
1760 len = strlen(filename);
1761 if (len > 0 && filename[len - 1] == 'c')
1762 prev_answer = true;
1763 return prev_answer;
1766 struct position last_pos;
1767 static void split_c_file_functions(struct symbol_list *sym_list)
1769 struct symbol *sym;
1771 __unnullify_path();
1772 FOR_EACH_PTR(sym_list, sym) {
1773 set_position(sym->pos);
1774 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1775 __pass_to_client(sym, BASE_HOOK);
1776 fake_global_assign(sym);
1778 } END_FOR_EACH_PTR(sym);
1779 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1780 nullify_path();
1782 FOR_EACH_PTR(sym_list, sym) {
1783 set_position(sym->pos);
1784 last_pos = sym->pos;
1785 if (!interesting_function(sym))
1786 continue;
1787 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1788 split_function(sym);
1789 process_inlines();
1791 last_pos = sym->pos;
1792 } END_FOR_EACH_PTR(sym);
1793 split_inlines(sym_list);
1794 __pass_to_client(sym_list, END_FILE_HOOK);
1797 static int final_before_fake;
1798 void init_fake_env(void)
1800 if (!in_fake_env)
1801 final_before_fake = final_pass;
1802 in_fake_env++;
1803 __push_fake_cur_stree();
1804 final_pass = 0;
1807 void end_fake_env(void)
1809 __pop_fake_cur_stree();
1810 in_fake_env--;
1811 if (!in_fake_env)
1812 final_pass = final_before_fake;
1815 static void open_output_files(char *base_file)
1817 char buf[256];
1819 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1820 sm_outfd = fopen(buf, "w");
1821 if (!sm_outfd) {
1822 printf("Error: Cannot open %s\n", buf);
1823 exit(1);
1826 if (!option_info)
1827 return;
1829 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1830 sql_outfd = fopen(buf, "w");
1831 if (!sql_outfd) {
1832 printf("Error: Cannot open %s\n", buf);
1833 exit(1);
1836 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1837 caller_info_fd = fopen(buf, "w");
1838 if (!caller_info_fd) {
1839 printf("Error: Cannot open %s\n", buf);
1840 exit(1);
1844 void smatch(int argc, char **argv)
1846 struct string_list *filelist = NULL;
1847 struct symbol_list *sym_list;
1848 struct timeval stop, start;
1850 gettimeofday(&start, NULL);
1852 if (argc < 2) {
1853 printf("Usage: smatch [--debug] <filename.c>\n");
1854 exit(1);
1856 sparse_initialize(argc, argv, &filelist);
1857 set_valid_ptr_max();
1858 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1859 if (option_file_output)
1860 open_output_files(base_file);
1861 sym_list = sparse_keep_tokens(base_file);
1862 split_c_file_functions(sym_list);
1863 } END_FOR_EACH_PTR_NOTAG(base_file);
1865 gettimeofday(&stop, NULL);
1867 set_position(last_pos);
1868 if (option_time)
1869 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1870 if (option_mem)
1871 sm_msg("mem: %luKb", get_max_memory());