db/untracked_param: mark parameters untracked if we have too much info
[smatch.git] / smatch_flow.c
blobe80d7986d6541a201fdbef4a5cfb44d1a476676c
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;
157 return 0;
160 static int is_inline_func(struct expression *expr)
162 if (expr->type != EXPR_SYMBOL || !expr->symbol)
163 return 0;
164 if (expr->symbol->ctype.modifiers & MOD_INLINE)
165 return 1;
166 return 0;
169 static int is_noreturn_func(struct expression *expr)
171 if (expr->type != EXPR_SYMBOL || !expr->symbol)
172 return 0;
173 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
174 return 1;
175 return 0;
178 int inlinable(struct expression *expr)
180 struct symbol *sym;
181 struct statement *last_stmt = NULL;
183 if (__inline_fn) /* don't nest */
184 return 0;
186 if (expr->type != EXPR_SYMBOL || !expr->symbol)
187 return 0;
188 if (is_no_inline_function(expr->symbol->ident->name))
189 return 0;
190 sym = get_base_type(expr->symbol);
191 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
192 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
193 return 0;
194 if (sym->stmt->type != STMT_COMPOUND)
195 return 0;
196 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
198 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
199 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
200 return 0;
201 if (sym->inline_stmt->type != STMT_COMPOUND)
202 return 0;
203 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
206 if (!last_stmt)
207 return 0;
209 /* the magic numbers in this function are pulled out of my bum. */
210 if (last_stmt->pos.line > sym->pos.line + 20)
211 return 0;
213 return 1;
216 void __process_post_op_stack(void)
218 struct expression *expr;
220 FOR_EACH_PTR(post_op_stack, expr) {
221 __pass_to_client(expr, OP_HOOK);
222 } END_FOR_EACH_PTR(expr);
224 __free_ptr_list((struct ptr_list **)&post_op_stack);
227 static int handle_comma_assigns(struct expression *expr)
229 struct expression *right;
230 struct expression *assign;
232 right = strip_expr(expr->right);
233 if (right->type != EXPR_COMMA)
234 return 0;
236 __split_expr(right->left);
237 __process_post_op_stack();
239 assign = assign_expression(expr->left, '=', right->right);
240 __split_expr(assign);
242 return 1;
245 /* This is to handle *p++ = foo; assignments */
246 static int handle_postop_assigns(struct expression *expr)
248 struct expression *left, *fake_left;
249 struct expression *assign;
251 left = strip_expr(expr->left);
252 if (left->type != EXPR_PREOP || left->op != '*')
253 return 0;
254 left = strip_expr(left->unop);
255 if (left->type != EXPR_POSTOP)
256 return 0;
258 fake_left = deref_expression(strip_expr(left->unop));
259 assign = assign_expression(fake_left, '=', expr->right);
261 __split_expr(assign);
262 __split_expr(expr->left);
264 return 1;
267 static int prev_expression_is_getting_address(struct expression *expr)
269 struct expression *parent;
271 do {
272 parent = expr_get_parent_expr(expr);
274 if (!parent)
275 return 0;
276 if (parent->type == EXPR_PREOP && parent->op == '&')
277 return 1;
278 if (parent->type == EXPR_PREOP && parent->op == '(')
279 goto next;
280 if (parent->type == EXPR_DEREF && parent->op == '.')
281 goto next;
283 return 0;
284 next:
285 expr = parent;
286 } while (1);
289 static int handle__builtin_choose_expr(struct expression *expr)
291 struct expression *const_expr, *expr1, *expr2;
292 sval_t sval;
294 if (!sym_name_is("__builtin_choose_expr", expr->fn))
295 return 0;
297 const_expr = get_argument_from_call_expr(expr->args, 0);
298 expr1 = get_argument_from_call_expr(expr->args, 1);
299 expr2 = get_argument_from_call_expr(expr->args, 2);
301 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
302 return 0;
303 if (sval.value)
304 __split_expr(expr1);
305 else
306 __split_expr(expr2);
307 return 1;
310 void __split_expr(struct expression *expr)
312 if (!expr)
313 return;
315 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
317 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
318 return;
319 if (__in_fake_assign >= 4) /* don't allow too much nesting */
320 return;
322 push_expression(&big_expression_stack, expr);
323 set_position(expr->pos);
324 __pass_to_client(expr, EXPR_HOOK);
326 switch (expr->type) {
327 case EXPR_PREOP:
328 expr_set_parent_expr(expr->unop, expr);
330 if (expr->op == '*' &&
331 !prev_expression_is_getting_address(expr))
332 __pass_to_client(expr, DEREF_HOOK);
333 __split_expr(expr->unop);
334 __pass_to_client(expr, OP_HOOK);
335 break;
336 case EXPR_POSTOP:
337 expr_set_parent_expr(expr->unop, expr);
339 __split_expr(expr->unop);
340 push_expression(&post_op_stack, expr);
341 break;
342 case EXPR_STATEMENT:
343 __expr_stmt_count++;
344 if (expr->statement && !expr->statement) {
345 stmt_set_parent_stmt(expr->statement,
346 last_ptr_list((struct ptr_list *)big_statement_stack));
348 __split_stmt(expr->statement);
349 __expr_stmt_count--;
350 break;
351 case EXPR_LOGICAL:
352 case EXPR_COMPARE:
353 expr_set_parent_expr(expr->left, expr);
354 expr_set_parent_expr(expr->right, expr);
356 __pass_to_client(expr, LOGIC_HOOK);
357 __handle_logic(expr);
358 break;
359 case EXPR_BINOP:
360 expr_set_parent_expr(expr->left, expr);
361 expr_set_parent_expr(expr->right, expr);
363 __pass_to_client(expr, BINOP_HOOK);
364 case EXPR_COMMA:
365 expr_set_parent_expr(expr->left, expr);
366 expr_set_parent_expr(expr->right, expr);
368 __split_expr(expr->left);
369 __process_post_op_stack();
370 __split_expr(expr->right);
371 break;
372 case EXPR_ASSIGNMENT: {
373 struct expression *right;
375 expr_set_parent_expr(expr->left, expr);
376 expr_set_parent_expr(expr->right, expr);
378 right = strip_expr(expr->right);
379 if (!right)
380 break;
382 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
384 /* foo = !bar() */
385 if (__handle_condition_assigns(expr))
386 break;
387 /* foo = (x < 5 ? foo : 5); */
388 if (__handle_select_assigns(expr))
389 break;
390 /* foo = ({frob(); frob(); frob(); 1;}) */
391 if (__handle_expr_statement_assigns(expr))
392 break;
393 /* foo = (3, 4); */
394 if (handle_comma_assigns(expr))
395 break;
396 if (handle_postop_assigns(expr))
397 break;
399 __split_expr(expr->right);
400 if (outside_of_function())
401 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
402 else
403 __pass_to_client(expr, ASSIGNMENT_HOOK);
405 __fake_struct_member_assignments(expr);
407 if (expr->op == '=' && right->type == EXPR_CALL)
408 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
410 if (get_macro_name(right->pos) &&
411 get_macro_name(expr->pos) != get_macro_name(right->pos))
412 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
414 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
416 __split_expr(expr->left);
417 break;
419 case EXPR_DEREF:
420 expr_set_parent_expr(expr->deref, expr);
422 __pass_to_client(expr, DEREF_HOOK);
423 __split_expr(expr->deref);
424 break;
425 case EXPR_SLICE:
426 expr_set_parent_expr(expr->base, expr);
428 __split_expr(expr->base);
429 break;
430 case EXPR_CAST:
431 case EXPR_FORCE_CAST:
432 expr_set_parent_expr(expr->cast_expression, expr);
434 __pass_to_client(expr, CAST_HOOK);
435 __split_expr(expr->cast_expression);
436 break;
437 case EXPR_SIZEOF:
438 if (expr->cast_expression)
439 __pass_to_client(strip_parens(expr->cast_expression),
440 SIZEOF_HOOK);
441 break;
442 case EXPR_OFFSETOF:
443 case EXPR_ALIGNOF:
444 evaluate_expression(expr);
445 break;
446 case EXPR_CONDITIONAL:
447 case EXPR_SELECT:
448 expr_set_parent_expr(expr->conditional, expr);
449 expr_set_parent_expr(expr->cond_true, expr);
450 expr_set_parent_expr(expr->cond_false, expr);
452 if (known_condition_true(expr->conditional)) {
453 __split_expr(expr->cond_true);
454 break;
456 if (known_condition_false(expr->conditional)) {
457 __split_expr(expr->cond_false);
458 break;
460 __pass_to_client(expr, SELECT_HOOK);
461 __split_whole_condition(expr->conditional);
462 __split_expr(expr->cond_true);
463 __push_true_states();
464 __use_false_states();
465 __split_expr(expr->cond_false);
466 __merge_true_states();
467 break;
468 case EXPR_CALL:
469 expr_set_parent_expr(expr->fn, expr);
471 if (sym_name_is("__builtin_constant_p", expr->fn))
472 break;
473 if (handle__builtin_choose_expr(expr))
474 break;
475 split_expr_list(expr->args, expr);
476 __split_expr(expr->fn);
477 if (is_inline_func(expr->fn))
478 add_inline_function(expr->fn->symbol);
479 if (inlinable(expr->fn))
480 __inline_call = 1;
481 __process_post_op_stack();
482 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
483 __pass_to_client(expr, FUNCTION_CALL_HOOK);
484 __inline_call = 0;
485 if (inlinable(expr->fn)) {
486 parse_inline(expr);
488 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
489 if (is_noreturn_func(expr->fn))
490 nullify_path();
491 break;
492 case EXPR_INITIALIZER:
493 split_expr_list(expr->expr_list, expr);
494 break;
495 case EXPR_IDENTIFIER:
496 expr_set_parent_expr(expr->ident_expression, expr);
497 __split_expr(expr->ident_expression);
498 break;
499 case EXPR_INDEX:
500 expr_set_parent_expr(expr->idx_expression, expr);
501 __split_expr(expr->idx_expression);
502 break;
503 case EXPR_POS:
504 expr_set_parent_expr(expr->init_expr, expr);
505 __split_expr(expr->init_expr);
506 break;
507 case EXPR_SYMBOL:
508 __pass_to_client(expr, SYM_HOOK);
509 break;
510 case EXPR_STRING:
511 __pass_to_client(expr, STRING_HOOK);
512 break;
513 default:
514 break;
516 pop_expression(&big_expression_stack);
519 static int is_forever_loop(struct statement *stmt)
521 struct expression *expr;
522 sval_t sval;
524 expr = strip_expr(stmt->iterator_pre_condition);
525 if (!expr)
526 expr = stmt->iterator_post_condition;
527 if (!expr) {
528 /* this is a for(;;) loop... */
529 return 1;
532 if (get_value(expr, &sval) && sval.value != 0)
533 return 1;
535 return 0;
538 static int loop_num;
539 static char *get_loop_name(int num)
541 char buf[256];
543 snprintf(buf, 255, "-loop%d", num);
544 buf[255] = '\0';
545 return alloc_sname(buf);
549 * Pre Loops are while and for loops.
551 static void handle_pre_loop(struct statement *stmt)
553 int once_through; /* we go through the loop at least once */
554 struct sm_state *extra_sm = NULL;
555 int unchanged = 0;
556 char *loop_name;
557 struct stree *stree = NULL;
558 struct sm_state *sm = NULL;
560 loop_name = get_loop_name(loop_num);
561 loop_num++;
563 __split_stmt(stmt->iterator_pre_statement);
564 __prev_stmt = stmt->iterator_pre_statement;
566 once_through = implied_condition_true(stmt->iterator_pre_condition);
568 loop_count++;
569 __push_continues();
570 __push_breaks();
572 __merge_gotos(loop_name, NULL);
574 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
575 __in_pre_condition++;
576 __pass_to_client(stmt, PRELOOP_HOOK);
577 __split_whole_condition(stmt->iterator_pre_condition);
578 __in_pre_condition--;
579 FOR_EACH_SM(stree, sm) {
580 set_state(sm->owner, sm->name, sm->sym, sm->state);
581 } END_FOR_EACH_SM(sm);
582 free_stree(&stree);
583 if (extra_sm)
584 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
586 if (option_assume_loops)
587 once_through = 1;
589 __split_stmt(stmt->iterator_statement);
590 if (is_forever_loop(stmt)) {
591 __merge_continues();
592 __save_gotos(loop_name, NULL);
594 __push_fake_cur_stree();
595 __split_stmt(stmt->iterator_post_statement);
596 stree = __pop_fake_cur_stree();
598 __discard_false_states();
599 __use_breaks();
601 if (!__path_is_null())
602 __merge_stree_into_cur(stree);
603 free_stree(&stree);
604 } else {
605 __merge_continues();
606 unchanged = __iterator_unchanged(extra_sm);
607 __split_stmt(stmt->iterator_post_statement);
608 __prev_stmt = stmt->iterator_post_statement;
609 __cur_stmt = stmt;
611 __save_gotos(loop_name, NULL);
612 __in_pre_condition++;
613 __split_whole_condition(stmt->iterator_pre_condition);
614 __in_pre_condition--;
615 nullify_path();
616 __merge_false_states();
617 if (once_through)
618 __discard_false_states();
619 else
620 __merge_false_states();
622 if (extra_sm && unchanged)
623 __extra_pre_loop_hook_after(extra_sm,
624 stmt->iterator_post_statement,
625 stmt->iterator_pre_condition);
626 __merge_breaks();
628 loop_count--;
632 * Post loops are do {} while();
634 static void handle_post_loop(struct statement *stmt)
636 char *loop_name;
638 loop_name = get_loop_name(loop_num);
639 loop_num++;
640 loop_count++;
642 __push_continues();
643 __push_breaks();
644 __merge_gotos(loop_name, NULL);
645 __split_stmt(stmt->iterator_statement);
646 __merge_continues();
647 if (!is_zero(stmt->iterator_post_condition))
648 __save_gotos(loop_name, NULL);
650 if (is_forever_loop(stmt)) {
651 __use_breaks();
652 } else {
653 __split_whole_condition(stmt->iterator_post_condition);
654 __use_false_states();
655 __merge_breaks();
657 loop_count--;
660 static int empty_statement(struct statement *stmt)
662 if (!stmt)
663 return 0;
664 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
665 return 1;
666 return 0;
669 static int last_stmt_on_same_line(void)
671 struct statement *stmt;
672 int i = 0;
674 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
675 if (!i++)
676 continue;
677 if (stmt->pos.line == get_lineno())
678 return 1;
679 return 0;
680 } END_FOR_EACH_PTR_REVERSE(stmt);
681 return 0;
684 static void split_asm_constraints(struct expression_list *expr_list)
686 struct expression *expr;
687 int state = 0;
689 FOR_EACH_PTR(expr_list, expr) {
690 switch (state) {
691 case 0: /* identifier */
692 case 1: /* constraint */
693 state++;
694 continue;
695 case 2: /* expression */
696 state = 0;
697 __split_expr(expr);
698 continue;
700 } END_FOR_EACH_PTR(expr);
703 static int is_case_val(struct statement *stmt, sval_t sval)
705 sval_t case_sval;
707 if (stmt->type != STMT_CASE)
708 return 0;
709 if (!stmt->case_expression) {
710 __set_default();
711 return 1;
713 if (!get_value(stmt->case_expression, &case_sval))
714 return 0;
715 if (case_sval.value == sval.value)
716 return 1;
717 return 0;
720 static struct range_list *get_case_rl(struct expression *switch_expr,
721 struct expression *case_expr,
722 struct expression *case_to)
724 sval_t start, end;
725 struct range_list *rl = NULL;
726 struct symbol *switch_type;
728 switch_type = get_type(switch_expr);
729 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
730 start = sval_cast(switch_type, start);
731 end = sval_cast(switch_type, end);
732 add_range(&rl, start, end);
733 } else if (get_value(case_expr, &start)) {
734 start = sval_cast(switch_type, start);
735 add_range(&rl, start, start);
738 return rl;
741 static void split_known_switch(struct statement *stmt, sval_t sval)
743 struct statement *tmp;
744 struct range_list *rl;
746 __split_expr(stmt->switch_expression);
747 sval = sval_cast(get_type(stmt->switch_expression), sval);
749 push_expression(&switch_expr_stack, stmt->switch_expression);
750 __save_switch_states(top_expression(switch_expr_stack));
751 nullify_path();
752 __push_default();
753 __push_breaks();
755 stmt = stmt->switch_statement;
757 __push_scope_hooks();
758 FOR_EACH_PTR(stmt->stmts, tmp) {
759 __smatch_lineno = tmp->pos.line;
760 if (is_case_val(tmp, sval)) {
761 rl = alloc_rl(sval, sval);
762 __merge_switches(top_expression(switch_expr_stack), rl);
763 __pass_case_to_client(top_expression(switch_expr_stack), rl);
765 if (__path_is_null())
766 continue;
767 __split_stmt(tmp);
768 if (__path_is_null()) {
769 __set_default();
770 goto out;
772 } END_FOR_EACH_PTR(tmp);
773 out:
774 __call_scope_hooks();
775 if (!__pop_default())
776 __merge_switches(top_expression(switch_expr_stack), NULL);
777 __discard_switches();
778 __merge_breaks();
779 pop_expression(&switch_expr_stack);
782 static void split_case(struct statement *stmt)
784 struct range_list *rl = NULL;
786 expr_set_parent_stmt(stmt->case_expression, stmt);
787 expr_set_parent_stmt(stmt->case_to, stmt);
789 rl = get_case_rl(top_expression(switch_expr_stack),
790 stmt->case_expression, stmt->case_to);
791 while (stmt->case_statement->type == STMT_CASE) {
792 struct range_list *tmp;
794 tmp = get_case_rl(top_expression(switch_expr_stack),
795 stmt->case_statement->case_expression,
796 stmt->case_statement->case_to);
797 if (!tmp)
798 break;
799 rl = rl_union(rl, tmp);
800 if (!stmt->case_expression)
801 __set_default();
802 stmt = stmt->case_statement;
805 __merge_switches(top_expression(switch_expr_stack), rl);
807 if (!stmt->case_expression)
808 __set_default();
809 __split_stmt(stmt->case_statement);
812 int time_parsing_function(void)
814 return ms_since(&fn_start_time) / 1000;
817 static int taking_too_long(void)
819 if (time_parsing_function() > 60 * 5) /* five minutes */
820 return 1;
821 return 0;
824 static int is_last_stmt(struct statement *cur_stmt)
826 struct symbol *fn = get_base_type(cur_func_sym);
827 struct statement *stmt;
829 if (!fn)
830 return 0;
831 stmt = fn->stmt;
832 if (!stmt)
833 stmt = fn->inline_stmt;
834 if (!stmt || stmt->type != STMT_COMPOUND)
835 return 0;
836 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
837 if (stmt && stmt->type == STMT_LABEL)
838 stmt = stmt->label_statement;
839 if (stmt == cur_stmt)
840 return 1;
841 return 0;
844 static void handle_backward_goto(struct statement *goto_stmt)
846 const char *goto_name, *label_name;
847 struct statement *func_stmt;
848 struct symbol *base_type = get_base_type(cur_func_sym);
849 struct statement *tmp;
850 int found = 0;
852 if (!option_info)
853 return;
854 if (last_goto_statement_handled)
855 return;
856 last_goto_statement_handled = 1;
858 if (!goto_stmt->goto_label ||
859 goto_stmt->goto_label->type != SYM_LABEL ||
860 !goto_stmt->goto_label->ident)
861 return;
862 goto_name = goto_stmt->goto_label->ident->name;
864 func_stmt = base_type->stmt;
865 if (!func_stmt)
866 func_stmt = base_type->inline_stmt;
867 if (!func_stmt)
868 return;
869 if (func_stmt->type != STMT_COMPOUND)
870 return;
872 FOR_EACH_PTR(func_stmt->stmts, tmp) {
873 if (!found) {
874 if (tmp->type != STMT_LABEL)
875 continue;
876 if (!tmp->label_identifier ||
877 tmp->label_identifier->type != SYM_LABEL ||
878 !tmp->label_identifier->ident)
879 continue;
880 label_name = tmp->label_identifier->ident->name;
881 if (strcmp(goto_name, label_name) != 0)
882 continue;
883 found = 1;
885 __split_stmt(tmp);
886 } END_FOR_EACH_PTR(tmp);
889 static void fake_a_return(void)
891 struct symbol *return_type;
893 nullify_path();
894 __unnullify_path();
896 return_type = get_real_base_type(cur_func_sym);
897 return_type = get_real_base_type(return_type);
898 if (return_type != &void_ctype) {
899 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
900 nullify_path();
904 static void fake_an_empty_default(struct position pos)
906 static struct statement none = {};
908 none.pos = pos;
909 none.type = STMT_NONE;
910 __merge_switches(top_expression(switch_expr_stack), NULL);
911 __split_stmt(&none);
914 static void split_compound(struct statement *stmt)
916 struct statement *prev = NULL;
917 struct statement *cur = NULL;
918 struct statement *next;
920 __push_scope_hooks();
922 FOR_EACH_PTR(stmt->stmts, next) {
923 /* just set them all ahead of time */
924 stmt_set_parent_stmt(next, stmt);
926 if (cur) {
927 __prev_stmt = prev;
928 __next_stmt = next;
929 __cur_stmt = cur;
930 __split_stmt(cur);
932 prev = cur;
933 cur = next;
934 } END_FOR_EACH_PTR(next);
935 if (cur) {
936 __prev_stmt = prev;
937 __cur_stmt = cur;
938 __next_stmt = NULL;
939 __split_stmt(cur);
943 * For function scope, then delay calling the scope hooks until the
944 * end of function hooks can run. I'm not positive this is the right
945 * thing...
947 if (!is_last_stmt(cur))
948 __call_scope_hooks();
952 * This is a hack, work around for detecting empty functions.
954 static int need_delayed_scope_hooks(void)
956 struct symbol *fn = get_base_type(cur_func_sym);
957 struct statement *stmt;
959 if (!fn)
960 return 0;
961 stmt = fn->stmt;
962 if (!stmt)
963 stmt = fn->inline_stmt;
964 if (stmt && stmt->type == STMT_COMPOUND)
965 return 1;
966 return 0;
969 void __split_label_stmt(struct statement *stmt)
971 if (stmt->label_identifier &&
972 stmt->label_identifier->type == SYM_LABEL &&
973 stmt->label_identifier->ident) {
974 loop_count |= 0x0800000;
975 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
979 static void find_asm_gotos(struct statement *stmt)
981 struct symbol *sym;
983 FOR_EACH_PTR(stmt->asm_labels, sym) {
984 __save_gotos(sym->ident->name, sym);
985 } END_FOR_EACH_PTR(sym);
988 void __split_stmt(struct statement *stmt)
990 sval_t sval;
992 if (!stmt)
993 goto out;
995 if (!__in_fake_assign)
996 __silence_warnings_for_stmt = false;
998 if (__bail_on_rest_of_function)
999 return;
1001 if (out_of_memory() || taking_too_long()) {
1002 struct timeval stop;
1004 gettimeofday(&stop, NULL);
1006 __bail_on_rest_of_function = 1;
1007 final_pass = 1;
1008 sm_msg("Function too hairy. Giving up. %lu seconds",
1009 stop.tv_sec - fn_start_time.tv_sec);
1010 fake_a_return();
1011 final_pass = 0; /* turn off sm_msg() from here */
1012 return;
1015 add_ptr_list(&big_statement_stack, stmt);
1016 free_expression_stack(&big_expression_stack);
1017 set_position(stmt->pos);
1018 __pass_to_client(stmt, STMT_HOOK);
1020 switch (stmt->type) {
1021 case STMT_DECLARATION:
1022 split_declaration(stmt->declaration);
1023 break;
1024 case STMT_RETURN:
1025 expr_set_parent_stmt(stmt->ret_value, stmt);
1027 __split_expr(stmt->ret_value);
1028 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1029 __process_post_op_stack();
1030 nullify_path();
1031 break;
1032 case STMT_EXPRESSION:
1033 expr_set_parent_stmt(stmt->expression, stmt);
1034 expr_set_parent_stmt(stmt->context, stmt);
1036 __split_expr(stmt->expression);
1037 break;
1038 case STMT_COMPOUND:
1039 split_compound(stmt);
1040 break;
1041 case STMT_IF:
1042 stmt_set_parent_stmt(stmt->if_true, stmt);
1043 stmt_set_parent_stmt(stmt->if_false, stmt);
1044 expr_set_parent_stmt(stmt->if_conditional, stmt);
1046 if (known_condition_true(stmt->if_conditional)) {
1047 __split_stmt(stmt->if_true);
1048 break;
1050 if (known_condition_false(stmt->if_conditional)) {
1051 __split_stmt(stmt->if_false);
1052 break;
1054 __split_whole_condition(stmt->if_conditional);
1055 __split_stmt(stmt->if_true);
1056 if (empty_statement(stmt->if_true) &&
1057 last_stmt_on_same_line() &&
1058 !get_macro_name(stmt->if_true->pos))
1059 sm_msg("warn: if();");
1060 __push_true_states();
1061 __use_false_states();
1062 __split_stmt(stmt->if_false);
1063 __merge_true_states();
1064 break;
1065 case STMT_ITERATOR:
1066 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1067 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1068 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1069 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1070 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1072 if (stmt->iterator_pre_condition)
1073 handle_pre_loop(stmt);
1074 else if (stmt->iterator_post_condition)
1075 handle_post_loop(stmt);
1076 else {
1077 // these are for(;;) type loops.
1078 handle_pre_loop(stmt);
1080 break;
1081 case STMT_SWITCH:
1082 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1083 expr_set_parent_stmt(stmt->switch_expression, stmt);
1085 if (get_value(stmt->switch_expression, &sval)) {
1086 split_known_switch(stmt, sval);
1087 break;
1089 __split_expr(stmt->switch_expression);
1090 push_expression(&switch_expr_stack, stmt->switch_expression);
1091 __save_switch_states(top_expression(switch_expr_stack));
1092 nullify_path();
1093 __push_default();
1094 __push_breaks();
1095 __split_stmt(stmt->switch_statement);
1096 if (!__pop_default() && have_remaining_cases())
1097 fake_an_empty_default(stmt->pos);
1098 __discard_switches();
1099 __merge_breaks();
1100 pop_expression(&switch_expr_stack);
1101 break;
1102 case STMT_CASE:
1103 split_case(stmt);
1104 break;
1105 case STMT_LABEL:
1106 __split_label_stmt(stmt);
1107 __split_stmt(stmt->label_statement);
1108 break;
1109 case STMT_GOTO:
1110 expr_set_parent_stmt(stmt->goto_expression, stmt);
1112 __split_expr(stmt->goto_expression);
1113 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1114 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1115 __process_breaks();
1116 } else if (!strcmp(stmt->goto_label->ident->name,
1117 "continue")) {
1118 __process_continues();
1120 } else if (stmt->goto_label &&
1121 stmt->goto_label->type == SYM_LABEL &&
1122 stmt->goto_label->ident) {
1123 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1125 nullify_path();
1126 if (is_last_stmt(stmt))
1127 handle_backward_goto(stmt);
1128 break;
1129 case STMT_NONE:
1130 break;
1131 case STMT_ASM:
1132 expr_set_parent_stmt(stmt->asm_string, stmt);
1134 find_asm_gotos(stmt);
1135 __pass_to_client(stmt, ASM_HOOK);
1136 __split_expr(stmt->asm_string);
1137 split_asm_constraints(stmt->asm_outputs);
1138 split_asm_constraints(stmt->asm_inputs);
1139 split_asm_constraints(stmt->asm_clobbers);
1140 break;
1141 case STMT_CONTEXT:
1142 break;
1143 case STMT_RANGE:
1144 __split_expr(stmt->range_expression);
1145 __split_expr(stmt->range_low);
1146 __split_expr(stmt->range_high);
1147 break;
1149 __pass_to_client(stmt, STMT_HOOK_AFTER);
1150 out:
1151 __process_post_op_stack();
1154 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1156 struct expression *expr;
1158 FOR_EACH_PTR(expr_list, expr) {
1159 expr_set_parent_expr(expr, parent);
1160 __split_expr(expr);
1161 __process_post_op_stack();
1162 } END_FOR_EACH_PTR(expr);
1165 static void split_sym(struct symbol *sym)
1167 if (!sym)
1168 return;
1169 if (!(sym->namespace & NS_SYMBOL))
1170 return;
1172 __split_stmt(sym->stmt);
1173 __split_expr(sym->array_size);
1174 split_symlist(sym->arguments);
1175 split_symlist(sym->symbol_list);
1176 __split_stmt(sym->inline_stmt);
1177 split_symlist(sym->inline_symbol_list);
1180 static void split_symlist(struct symbol_list *sym_list)
1182 struct symbol *sym;
1184 FOR_EACH_PTR(sym_list, sym) {
1185 split_sym(sym);
1186 } END_FOR_EACH_PTR(sym);
1189 typedef void (fake_cb)(struct expression *expr);
1191 static int member_to_number(struct expression *expr, struct ident *member)
1193 struct symbol *type, *tmp;
1194 char *name;
1195 int i;
1197 if (!member)
1198 return -1;
1199 name = member->name;
1201 type = get_type(expr);
1202 if (!type || type->type != SYM_STRUCT)
1203 return -1;
1205 i = -1;
1206 FOR_EACH_PTR(type->symbol_list, tmp) {
1207 i++;
1208 if (!tmp->ident)
1209 continue;
1210 if (strcmp(name, tmp->ident->name) == 0)
1211 return i;
1212 } END_FOR_EACH_PTR(tmp);
1213 return -1;
1216 static struct ident *number_to_member(struct expression *expr, int num)
1218 struct symbol *type, *member;
1219 int i = 0;
1221 type = get_type(expr);
1222 if (!type || type->type != SYM_STRUCT)
1223 return NULL;
1225 FOR_EACH_PTR(type->symbol_list, member) {
1226 if (i == num)
1227 return member->ident;
1228 i++;
1229 } END_FOR_EACH_PTR(member);
1230 return NULL;
1233 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1235 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1237 struct expression *edge_member, *assign;
1238 struct symbol *base = get_real_base_type(member);
1239 struct symbol *tmp;
1241 if (member->ident)
1242 expr = member_expression(expr, '.', member->ident);
1244 FOR_EACH_PTR(base->symbol_list, tmp) {
1245 struct symbol *type;
1247 type = get_real_base_type(tmp);
1248 if (!type)
1249 continue;
1251 edge_member = member_expression(expr, '.', tmp->ident);
1252 if (get_extra_state(edge_member))
1253 continue;
1255 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1256 set_inner_struct_members(expr, tmp);
1257 continue;
1260 if (!tmp->ident)
1261 continue;
1263 assign = assign_expression(edge_member, '=', zero_expr());
1264 __split_expr(assign);
1265 } END_FOR_EACH_PTR(tmp);
1270 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1272 struct symbol *tmp;
1273 struct expression *member = NULL;
1274 struct expression *assign;
1275 int op = '*';
1277 if (expr->type == EXPR_PREOP && expr->op == '&') {
1278 expr = strip_expr(expr->unop);
1279 op = '.';
1282 FOR_EACH_PTR(type->symbol_list, tmp) {
1283 type = get_real_base_type(tmp);
1284 if (!type)
1285 continue;
1287 if (tmp->ident) {
1288 member = member_expression(expr, op, tmp->ident);
1289 if (get_extra_state(member))
1290 continue;
1293 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1294 set_inner_struct_members(expr, tmp);
1295 continue;
1297 if (type->type == SYM_ARRAY)
1298 continue;
1299 if (!tmp->ident)
1300 continue;
1302 assign = assign_expression(member, '=', zero_expr());
1303 __split_expr(assign);
1304 } END_FOR_EACH_PTR(tmp);
1307 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1309 struct expression *deref, *assign, *tmp, *right;
1310 struct symbol *struct_type, *type;
1311 struct ident *member;
1312 int member_idx;
1314 struct_type = get_type(symbol);
1315 if (!struct_type ||
1316 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1317 return;
1320 * We're parsing an initializer that could look something like this:
1321 * struct foo foo = {
1322 * 42,
1323 * .whatever.xxx = 11,
1324 * .zzz = 12,
1325 * };
1327 * So what we have here is a list with 42, .whatever, and .zzz. We need
1328 * to break it up into left and right sides of the assignments.
1331 member_idx = 0;
1332 FOR_EACH_PTR(members, tmp) {
1333 deref = NULL;
1334 if (tmp->type == EXPR_IDENTIFIER) {
1335 member_idx = member_to_number(symbol, tmp->expr_ident);
1336 while (tmp->type == EXPR_IDENTIFIER) {
1337 member = tmp->expr_ident;
1338 tmp = tmp->ident_expression;
1339 if (deref)
1340 deref = member_expression(deref, '.', member);
1341 else
1342 deref = member_expression(symbol, '.', member);
1344 } else {
1345 member = number_to_member(symbol, member_idx);
1346 deref = member_expression(symbol, '.', member);
1348 right = tmp;
1349 member_idx++;
1350 if (right->type == EXPR_INITIALIZER) {
1351 type = get_type(deref);
1352 if (type && type->type == SYM_ARRAY)
1353 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1354 else
1355 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1356 } else {
1357 assign = assign_expression(deref, '=', right);
1358 fake_cb(assign);
1360 } END_FOR_EACH_PTR(tmp);
1362 set_unset_to_zero(struct_type, symbol);
1365 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1367 fake_member_assigns_helper(symbol_expression(sym),
1368 sym->initializer->expr_list, fake_cb);
1371 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1373 struct expression *offset, *binop, *assign, *tmp;
1374 struct symbol *type;
1375 int idx;
1377 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1378 return;
1380 idx = 0;
1381 FOR_EACH_PTR(expr_list, tmp) {
1382 if (tmp->type == EXPR_INDEX) {
1383 if (tmp->idx_from != tmp->idx_to)
1384 return;
1385 idx = tmp->idx_from;
1386 if (!tmp->idx_expression)
1387 goto next;
1388 tmp = tmp->idx_expression;
1390 offset = value_expr(idx);
1391 binop = array_element_expression(array, offset);
1392 if (tmp->type == EXPR_INITIALIZER) {
1393 type = get_type(binop);
1394 if (type && type->type == SYM_ARRAY)
1395 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1396 else
1397 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1398 } else {
1399 assign = assign_expression(binop, '=', tmp);
1400 fake_cb(assign);
1402 next:
1403 idx++;
1404 } END_FOR_EACH_PTR(tmp);
1407 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1409 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1412 static void fake_assign_expr(struct symbol *sym)
1414 struct expression *assign, *symbol;
1416 symbol = symbol_expression(sym);
1417 assign = assign_expression(symbol, '=', sym->initializer);
1418 __split_expr(assign);
1421 static void call_split_expr(struct expression *expr)
1423 __split_expr(expr);
1426 static void do_initializer_stuff(struct symbol *sym)
1428 if (!sym->initializer)
1429 return;
1431 if (sym->initializer->type == EXPR_INITIALIZER) {
1432 if (get_real_base_type(sym)->type == SYM_ARRAY)
1433 fake_element_assigns(sym, call_split_expr);
1434 else
1435 fake_member_assigns(sym, call_split_expr);
1436 } else {
1437 fake_assign_expr(sym);
1441 static void split_declaration(struct symbol_list *sym_list)
1443 struct symbol *sym;
1445 FOR_EACH_PTR(sym_list, sym) {
1446 __pass_to_client(sym, DECLARATION_HOOK);
1447 do_initializer_stuff(sym);
1448 split_sym(sym);
1449 } END_FOR_EACH_PTR(sym);
1452 static void call_global_assign_hooks(struct expression *assign)
1454 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1457 static void fake_global_assign(struct symbol *sym)
1459 struct expression *assign, *symbol;
1461 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1462 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1463 fake_element_assigns(sym, call_global_assign_hooks);
1464 } else if (sym->initializer) {
1465 symbol = symbol_expression(sym);
1466 assign = assign_expression(symbol, '=', sym->initializer);
1467 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1468 } else {
1469 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1471 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1472 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1473 fake_member_assigns(sym, call_global_assign_hooks);
1474 } else if (sym->initializer) {
1475 symbol = symbol_expression(sym);
1476 assign = assign_expression(symbol, '=', sym->initializer);
1477 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1478 } else {
1479 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1481 } else {
1482 symbol = symbol_expression(sym);
1483 if (sym->initializer)
1484 assign = assign_expression(symbol, '=', sym->initializer);
1485 else
1486 assign = assign_expression(symbol, '=', zero_expr());
1487 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1491 static void start_function_definition(struct symbol *sym)
1493 __in_function_def = 1;
1494 __pass_to_client(sym, FUNC_DEF_HOOK);
1495 __in_function_def = 0;
1496 __pass_to_client(sym, AFTER_DEF_HOOK);
1500 static void split_function(struct symbol *sym)
1502 struct symbol *base_type = get_base_type(sym);
1504 if (!base_type->stmt && !base_type->inline_stmt)
1505 return;
1507 gettimeofday(&fn_start_time, NULL);
1508 cur_func_sym = sym;
1509 if (sym->ident)
1510 cur_func = sym->ident->name;
1511 set_position(sym->pos);
1512 loop_count = 0;
1513 last_goto_statement_handled = 0;
1514 sm_debug("new function: %s\n", cur_func);
1515 __stree_id = 0;
1516 if (option_two_passes) {
1517 __unnullify_path();
1518 loop_num = 0;
1519 final_pass = 0;
1520 start_function_definition(sym);
1521 __split_stmt(base_type->stmt);
1522 __split_stmt(base_type->inline_stmt);
1523 nullify_path();
1525 __unnullify_path();
1526 loop_num = 0;
1527 final_pass = 1;
1528 start_function_definition(sym);
1529 __split_stmt(base_type->stmt);
1530 __split_stmt(base_type->inline_stmt);
1531 __pass_to_client(sym, END_FUNC_HOOK);
1532 if (need_delayed_scope_hooks())
1533 __call_scope_hooks();
1534 __pass_to_client(sym, AFTER_FUNC_HOOK);
1536 clear_all_states();
1537 cur_func_sym = NULL;
1538 cur_func = NULL;
1539 free_data_info_allocs();
1540 free_expression_stack(&switch_expr_stack);
1541 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1542 __bail_on_rest_of_function = 0;
1545 static void save_flow_state(void)
1547 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1548 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1549 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1551 __add_ptr_list(&backup, big_statement_stack, 0);
1552 __add_ptr_list(&backup, big_expression_stack, 0);
1553 __add_ptr_list(&backup, big_condition_stack, 0);
1554 __add_ptr_list(&backup, switch_expr_stack, 0);
1556 __add_ptr_list(&backup, cur_func_sym, 0);
1558 __add_ptr_list(&backup, __prev_stmt, 0);
1559 __add_ptr_list(&backup, __cur_stmt, 0);
1560 __add_ptr_list(&backup, __next_stmt, 0);
1564 static void *pop_backup(void)
1566 void *ret;
1568 ret = last_ptr_list(backup);
1569 delete_ptr_list_last(&backup);
1570 return ret;
1573 static void restore_flow_state(void)
1575 __next_stmt = pop_backup();
1576 __cur_stmt = pop_backup();
1577 __prev_stmt = pop_backup();
1579 cur_func_sym = pop_backup();
1580 switch_expr_stack = pop_backup();
1581 big_condition_stack = pop_backup();
1582 big_expression_stack = pop_backup();
1583 big_statement_stack = pop_backup();
1584 final_pass = PTR_INT(pop_backup()) >> 2;
1585 loop_count = PTR_INT(pop_backup()) >> 2;
1586 loop_num = PTR_INT(pop_backup()) >> 2;
1589 static void parse_inline(struct expression *call)
1591 struct symbol *base_type;
1592 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1593 struct timeval time_backup = fn_start_time;
1595 save_flow_state();
1597 __pass_to_client(call, INLINE_FN_START);
1598 final_pass = 0; /* don't print anything */
1599 __inline_fn = call;
1601 base_type = get_base_type(call->fn->symbol);
1602 cur_func_sym = call->fn->symbol;
1603 if (call->fn->symbol->ident)
1604 cur_func = call->fn->symbol->ident->name;
1605 else
1606 cur_func = NULL;
1607 set_position(call->fn->symbol->pos);
1609 save_all_states();
1610 big_statement_stack = NULL;
1611 big_expression_stack = NULL;
1612 big_condition_stack = NULL;
1613 switch_expr_stack = NULL;
1615 sm_debug("inline function: %s\n", cur_func);
1616 __unnullify_path();
1617 loop_num = 0;
1618 loop_count = 0;
1619 start_function_definition(call->fn->symbol);
1620 __split_stmt(base_type->stmt);
1621 __split_stmt(base_type->inline_stmt);
1622 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1623 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1625 free_expression_stack(&switch_expr_stack);
1626 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1627 nullify_path();
1628 free_goto_stack();
1630 restore_flow_state();
1631 fn_start_time = time_backup;
1632 cur_func = cur_func_bak;
1634 restore_all_states();
1635 set_position(call->pos);
1636 __inline_fn = NULL;
1637 __pass_to_client(call, INLINE_FN_END);
1640 static struct symbol_list *inlines_called;
1641 static void add_inline_function(struct symbol *sym)
1643 static struct symbol_list *already_added;
1644 struct symbol *tmp;
1646 FOR_EACH_PTR(already_added, tmp) {
1647 if (tmp == sym)
1648 return;
1649 } END_FOR_EACH_PTR(tmp);
1651 add_ptr_list(&already_added, sym);
1652 add_ptr_list(&inlines_called, sym);
1655 static void process_inlines(void)
1657 struct symbol *tmp;
1659 FOR_EACH_PTR(inlines_called, tmp) {
1660 split_function(tmp);
1661 } END_FOR_EACH_PTR(tmp);
1662 free_ptr_list(&inlines_called);
1665 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1667 struct symbol *sym;
1669 FOR_EACH_PTR_REVERSE(big_list, sym) {
1670 if (!sym->scope)
1671 continue;
1672 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1673 return sym;
1674 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1675 return sym;
1676 } END_FOR_EACH_PTR_REVERSE(sym);
1678 return NULL;
1681 static bool interesting_function(struct symbol *sym)
1683 static int prev_stream = -1;
1684 static bool prev_answer;
1685 const char *filename;
1686 int len;
1688 if (!(sym->ctype.modifiers & MOD_INLINE))
1689 return true;
1691 if (sym->pos.stream == prev_stream)
1692 return prev_answer;
1694 prev_stream = sym->pos.stream;
1695 prev_answer = false;
1697 filename = stream_name(sym->pos.stream);
1698 len = strlen(filename);
1699 if (len > 0 && filename[len - 1] == 'c')
1700 prev_answer = true;
1701 return prev_answer;
1704 static void split_inlines_in_scope(struct symbol *sym)
1706 struct symbol *base;
1707 struct symbol_list *scope_list;
1708 int stream;
1710 scope_list = sym->scope->symbols;
1711 stream = sym->pos.stream;
1713 /* find the last static symbol in the file */
1714 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1715 if (sym->pos.stream != stream)
1716 continue;
1717 if (sym->type != SYM_NODE)
1718 continue;
1719 base = get_base_type(sym);
1720 if (!base)
1721 continue;
1722 if (base->type != SYM_FN)
1723 continue;
1724 if (!base->inline_stmt)
1725 continue;
1726 if (!interesting_function(sym))
1727 continue;
1728 add_inline_function(sym);
1729 } END_FOR_EACH_PTR_REVERSE(sym);
1731 process_inlines();
1734 static void split_inlines(struct symbol_list *sym_list)
1736 struct symbol *sym;
1738 sym = get_last_scoped_symbol(sym_list, 0);
1739 if (sym)
1740 split_inlines_in_scope(sym);
1741 sym = get_last_scoped_symbol(sym_list, 1);
1742 if (sym)
1743 split_inlines_in_scope(sym);
1746 static struct stree *clone_estates_perm(struct stree *orig)
1748 struct stree *ret = NULL;
1749 struct sm_state *tmp;
1751 FOR_EACH_SM(orig, tmp) {
1752 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1753 } END_FOR_EACH_SM(tmp);
1755 return ret;
1758 struct position last_pos;
1759 static void split_c_file_functions(struct symbol_list *sym_list)
1761 struct symbol *sym;
1763 __unnullify_path();
1764 FOR_EACH_PTR(sym_list, sym) {
1765 set_position(sym->pos);
1766 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1767 __pass_to_client(sym, BASE_HOOK);
1768 fake_global_assign(sym);
1770 } END_FOR_EACH_PTR(sym);
1771 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1772 nullify_path();
1774 FOR_EACH_PTR(sym_list, sym) {
1775 set_position(sym->pos);
1776 last_pos = sym->pos;
1777 if (!interesting_function(sym))
1778 continue;
1779 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1780 split_function(sym);
1781 process_inlines();
1783 last_pos = sym->pos;
1784 } END_FOR_EACH_PTR(sym);
1785 split_inlines(sym_list);
1786 __pass_to_client(sym_list, END_FILE_HOOK);
1789 static int final_before_fake;
1790 void init_fake_env(void)
1792 if (!in_fake_env)
1793 final_before_fake = final_pass;
1794 in_fake_env++;
1795 __push_fake_cur_stree();
1796 final_pass = 0;
1799 void end_fake_env(void)
1801 __pop_fake_cur_stree();
1802 in_fake_env--;
1803 if (!in_fake_env)
1804 final_pass = final_before_fake;
1807 static void open_output_files(char *base_file)
1809 char buf[256];
1811 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1812 sm_outfd = fopen(buf, "w");
1813 if (!sm_outfd) {
1814 printf("Error: Cannot open %s\n", buf);
1815 exit(1);
1818 if (!option_info)
1819 return;
1821 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1822 sql_outfd = fopen(buf, "w");
1823 if (!sql_outfd) {
1824 printf("Error: Cannot open %s\n", buf);
1825 exit(1);
1828 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1829 caller_info_fd = fopen(buf, "w");
1830 if (!caller_info_fd) {
1831 printf("Error: Cannot open %s\n", buf);
1832 exit(1);
1836 void smatch(int argc, char **argv)
1838 struct string_list *filelist = NULL;
1839 struct symbol_list *sym_list;
1840 struct timeval stop, start;
1842 gettimeofday(&start, NULL);
1844 if (argc < 2) {
1845 printf("Usage: smatch [--debug] <filename.c>\n");
1846 exit(1);
1848 sparse_initialize(argc, argv, &filelist);
1849 set_valid_ptr_max();
1850 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1851 if (option_file_output)
1852 open_output_files(base_file);
1853 sym_list = sparse_keep_tokens(base_file);
1854 split_c_file_functions(sym_list);
1855 } END_FOR_EACH_PTR_NOTAG(base_file);
1857 gettimeofday(&stop, NULL);
1859 set_position(last_pos);
1860 if (option_time)
1861 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1862 if (option_mem)
1863 sm_msg("mem: %luKb", get_max_memory());