about_fn_ptr_arg: don't get into a recursive loop
[smatch.git] / smatch_flow.c
blob840e12fdd3efbe6c9c3ba9f6e1fb93edf3d002fa
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 void __split_expr(struct expression *expr)
291 if (!expr)
292 return;
294 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
296 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
297 return;
298 if (__in_fake_assign >= 4) /* don't allow too much nesting */
299 return;
301 push_expression(&big_expression_stack, expr);
302 set_position(expr->pos);
303 __pass_to_client(expr, EXPR_HOOK);
305 switch (expr->type) {
306 case EXPR_PREOP:
307 expr_set_parent_expr(expr->unop, expr);
309 if (expr->op == '*' &&
310 !prev_expression_is_getting_address(expr))
311 __pass_to_client(expr, DEREF_HOOK);
312 __split_expr(expr->unop);
313 __pass_to_client(expr, OP_HOOK);
314 break;
315 case EXPR_POSTOP:
316 expr_set_parent_expr(expr->unop, expr);
318 __split_expr(expr->unop);
319 push_expression(&post_op_stack, expr);
320 break;
321 case EXPR_STATEMENT:
322 __expr_stmt_count++;
323 if (expr->statement && !expr->statement) {
324 stmt_set_parent_stmt(expr->statement,
325 last_ptr_list((struct ptr_list *)big_statement_stack));
327 __split_stmt(expr->statement);
328 __expr_stmt_count--;
329 break;
330 case EXPR_LOGICAL:
331 case EXPR_COMPARE:
332 expr_set_parent_expr(expr->left, expr);
333 expr_set_parent_expr(expr->right, expr);
335 __pass_to_client(expr, LOGIC_HOOK);
336 __handle_logic(expr);
337 break;
338 case EXPR_BINOP:
339 expr_set_parent_expr(expr->left, expr);
340 expr_set_parent_expr(expr->right, expr);
342 __pass_to_client(expr, BINOP_HOOK);
343 case EXPR_COMMA:
344 expr_set_parent_expr(expr->left, expr);
345 expr_set_parent_expr(expr->right, expr);
347 __split_expr(expr->left);
348 __process_post_op_stack();
349 __split_expr(expr->right);
350 break;
351 case EXPR_ASSIGNMENT: {
352 struct expression *right;
354 expr_set_parent_expr(expr->left, expr);
355 expr_set_parent_expr(expr->right, expr);
357 right = strip_expr(expr->right);
358 if (!right)
359 break;
361 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
363 /* foo = !bar() */
364 if (__handle_condition_assigns(expr))
365 break;
366 /* foo = (x < 5 ? foo : 5); */
367 if (__handle_select_assigns(expr))
368 break;
369 /* foo = ({frob(); frob(); frob(); 1;}) */
370 if (__handle_expr_statement_assigns(expr))
371 break;
372 /* foo = (3, 4); */
373 if (handle_comma_assigns(expr))
374 break;
375 if (handle_postop_assigns(expr))
376 break;
378 __split_expr(expr->right);
379 if (outside_of_function())
380 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
381 else
382 __pass_to_client(expr, ASSIGNMENT_HOOK);
384 __fake_struct_member_assignments(expr);
386 if (expr->op == '=' && right->type == EXPR_CALL)
387 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
389 if (get_macro_name(right->pos) &&
390 get_macro_name(expr->pos) != get_macro_name(right->pos))
391 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
393 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
395 __split_expr(expr->left);
396 break;
398 case EXPR_DEREF:
399 expr_set_parent_expr(expr->deref, expr);
401 __pass_to_client(expr, DEREF_HOOK);
402 __split_expr(expr->deref);
403 break;
404 case EXPR_SLICE:
405 expr_set_parent_expr(expr->base, expr);
407 __split_expr(expr->base);
408 break;
409 case EXPR_CAST:
410 case EXPR_FORCE_CAST:
411 expr_set_parent_expr(expr->cast_expression, expr);
413 __pass_to_client(expr, CAST_HOOK);
414 __split_expr(expr->cast_expression);
415 break;
416 case EXPR_SIZEOF:
417 if (expr->cast_expression)
418 __pass_to_client(strip_parens(expr->cast_expression),
419 SIZEOF_HOOK);
420 break;
421 case EXPR_OFFSETOF:
422 case EXPR_ALIGNOF:
423 evaluate_expression(expr);
424 break;
425 case EXPR_CONDITIONAL:
426 case EXPR_SELECT:
427 expr_set_parent_expr(expr->conditional, expr);
428 expr_set_parent_expr(expr->cond_true, expr);
429 expr_set_parent_expr(expr->cond_false, expr);
431 if (known_condition_true(expr->conditional)) {
432 __split_expr(expr->cond_true);
433 break;
435 if (known_condition_false(expr->conditional)) {
436 __split_expr(expr->cond_false);
437 break;
439 __pass_to_client(expr, SELECT_HOOK);
440 __split_whole_condition(expr->conditional);
441 __split_expr(expr->cond_true);
442 __push_true_states();
443 __use_false_states();
444 __split_expr(expr->cond_false);
445 __merge_true_states();
446 break;
447 case EXPR_CALL:
448 expr_set_parent_expr(expr->fn, expr);
450 if (sym_name_is("__builtin_constant_p", expr->fn))
451 break;
452 split_expr_list(expr->args, expr);
453 __split_expr(expr->fn);
454 if (is_inline_func(expr->fn))
455 add_inline_function(expr->fn->symbol);
456 if (inlinable(expr->fn))
457 __inline_call = 1;
458 __process_post_op_stack();
459 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
460 __pass_to_client(expr, FUNCTION_CALL_HOOK);
461 __inline_call = 0;
462 if (inlinable(expr->fn)) {
463 parse_inline(expr);
465 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
466 if (is_noreturn_func(expr->fn))
467 nullify_path();
468 break;
469 case EXPR_INITIALIZER:
470 split_expr_list(expr->expr_list, expr);
471 break;
472 case EXPR_IDENTIFIER:
473 expr_set_parent_expr(expr->ident_expression, expr);
474 __split_expr(expr->ident_expression);
475 break;
476 case EXPR_INDEX:
477 expr_set_parent_expr(expr->idx_expression, expr);
478 __split_expr(expr->idx_expression);
479 break;
480 case EXPR_POS:
481 expr_set_parent_expr(expr->init_expr, expr);
482 __split_expr(expr->init_expr);
483 break;
484 case EXPR_SYMBOL:
485 __pass_to_client(expr, SYM_HOOK);
486 break;
487 case EXPR_STRING:
488 __pass_to_client(expr, STRING_HOOK);
489 break;
490 default:
491 break;
493 pop_expression(&big_expression_stack);
496 static int is_forever_loop(struct statement *stmt)
498 struct expression *expr;
499 sval_t sval;
501 expr = strip_expr(stmt->iterator_pre_condition);
502 if (!expr)
503 expr = stmt->iterator_post_condition;
504 if (!expr) {
505 /* this is a for(;;) loop... */
506 return 1;
509 if (get_value(expr, &sval) && sval.value != 0)
510 return 1;
512 return 0;
515 static int loop_num;
516 static char *get_loop_name(int num)
518 char buf[256];
520 snprintf(buf, 255, "-loop%d", num);
521 buf[255] = '\0';
522 return alloc_sname(buf);
526 * Pre Loops are while and for loops.
528 static void handle_pre_loop(struct statement *stmt)
530 int once_through; /* we go through the loop at least once */
531 struct sm_state *extra_sm = NULL;
532 int unchanged = 0;
533 char *loop_name;
534 struct stree *stree = NULL;
535 struct sm_state *sm = NULL;
537 loop_name = get_loop_name(loop_num);
538 loop_num++;
540 __split_stmt(stmt->iterator_pre_statement);
541 __prev_stmt = stmt->iterator_pre_statement;
543 once_through = implied_condition_true(stmt->iterator_pre_condition);
545 loop_count++;
546 __push_continues();
547 __push_breaks();
549 __merge_gotos(loop_name, NULL);
551 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
552 __in_pre_condition++;
553 __pass_to_client(stmt, PRELOOP_HOOK);
554 __split_whole_condition(stmt->iterator_pre_condition);
555 __in_pre_condition--;
556 FOR_EACH_SM(stree, sm) {
557 set_state(sm->owner, sm->name, sm->sym, sm->state);
558 } END_FOR_EACH_SM(sm);
559 free_stree(&stree);
560 if (extra_sm)
561 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
563 if (option_assume_loops)
564 once_through = 1;
566 __split_stmt(stmt->iterator_statement);
567 if (is_forever_loop(stmt)) {
568 __merge_continues();
569 __save_gotos(loop_name, NULL);
571 __push_fake_cur_stree();
572 __split_stmt(stmt->iterator_post_statement);
573 stree = __pop_fake_cur_stree();
575 __discard_false_states();
576 __use_breaks();
578 if (!__path_is_null())
579 __merge_stree_into_cur(stree);
580 free_stree(&stree);
581 } else {
582 __merge_continues();
583 unchanged = __iterator_unchanged(extra_sm);
584 __split_stmt(stmt->iterator_post_statement);
585 __prev_stmt = stmt->iterator_post_statement;
586 __cur_stmt = stmt;
588 __save_gotos(loop_name, NULL);
589 __in_pre_condition++;
590 __split_whole_condition(stmt->iterator_pre_condition);
591 __in_pre_condition--;
592 nullify_path();
593 __merge_false_states();
594 if (once_through)
595 __discard_false_states();
596 else
597 __merge_false_states();
599 if (extra_sm && unchanged)
600 __extra_pre_loop_hook_after(extra_sm,
601 stmt->iterator_post_statement,
602 stmt->iterator_pre_condition);
603 __merge_breaks();
605 loop_count--;
609 * Post loops are do {} while();
611 static void handle_post_loop(struct statement *stmt)
613 char *loop_name;
615 loop_name = get_loop_name(loop_num);
616 loop_num++;
617 loop_count++;
619 __push_continues();
620 __push_breaks();
621 __merge_gotos(loop_name, NULL);
622 __split_stmt(stmt->iterator_statement);
623 __merge_continues();
624 if (!is_zero(stmt->iterator_post_condition))
625 __save_gotos(loop_name, NULL);
627 if (is_forever_loop(stmt)) {
628 __use_breaks();
629 } else {
630 __split_whole_condition(stmt->iterator_post_condition);
631 __use_false_states();
632 __merge_breaks();
634 loop_count--;
637 static int empty_statement(struct statement *stmt)
639 if (!stmt)
640 return 0;
641 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
642 return 1;
643 return 0;
646 static int last_stmt_on_same_line(void)
648 struct statement *stmt;
649 int i = 0;
651 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
652 if (!i++)
653 continue;
654 if (stmt->pos.line == get_lineno())
655 return 1;
656 return 0;
657 } END_FOR_EACH_PTR_REVERSE(stmt);
658 return 0;
661 static void split_asm_constraints(struct expression_list *expr_list)
663 struct expression *expr;
664 int state = 0;
666 FOR_EACH_PTR(expr_list, expr) {
667 switch (state) {
668 case 0: /* identifier */
669 case 1: /* constraint */
670 state++;
671 continue;
672 case 2: /* expression */
673 state = 0;
674 __split_expr(expr);
675 continue;
677 } END_FOR_EACH_PTR(expr);
680 static int is_case_val(struct statement *stmt, sval_t sval)
682 sval_t case_sval;
684 if (stmt->type != STMT_CASE)
685 return 0;
686 if (!stmt->case_expression) {
687 __set_default();
688 return 1;
690 if (!get_value(stmt->case_expression, &case_sval))
691 return 0;
692 if (case_sval.value == sval.value)
693 return 1;
694 return 0;
697 static struct range_list *get_case_rl(struct expression *switch_expr,
698 struct expression *case_expr,
699 struct expression *case_to)
701 sval_t start, end;
702 struct range_list *rl = NULL;
703 struct symbol *switch_type;
705 switch_type = get_type(switch_expr);
706 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
707 start = sval_cast(switch_type, start);
708 end = sval_cast(switch_type, end);
709 add_range(&rl, start, end);
710 } else if (get_value(case_expr, &start)) {
711 start = sval_cast(switch_type, start);
712 add_range(&rl, start, start);
715 return rl;
718 static void split_known_switch(struct statement *stmt, sval_t sval)
720 struct statement *tmp;
721 struct range_list *rl;
723 __split_expr(stmt->switch_expression);
724 sval = sval_cast(get_type(stmt->switch_expression), sval);
726 push_expression(&switch_expr_stack, stmt->switch_expression);
727 __save_switch_states(top_expression(switch_expr_stack));
728 nullify_path();
729 __push_default();
730 __push_breaks();
732 stmt = stmt->switch_statement;
734 __push_scope_hooks();
735 FOR_EACH_PTR(stmt->stmts, tmp) {
736 __smatch_lineno = tmp->pos.line;
737 if (is_case_val(tmp, sval)) {
738 rl = alloc_rl(sval, sval);
739 __merge_switches(top_expression(switch_expr_stack), rl);
740 __pass_case_to_client(top_expression(switch_expr_stack), rl);
742 if (__path_is_null())
743 continue;
744 __split_stmt(tmp);
745 if (__path_is_null()) {
746 __set_default();
747 goto out;
749 } END_FOR_EACH_PTR(tmp);
750 out:
751 __call_scope_hooks();
752 if (!__pop_default())
753 __merge_switches(top_expression(switch_expr_stack), NULL);
754 __discard_switches();
755 __merge_breaks();
756 pop_expression(&switch_expr_stack);
759 static void split_case(struct statement *stmt)
761 struct range_list *rl = NULL;
763 expr_set_parent_stmt(stmt->case_expression, stmt);
764 expr_set_parent_stmt(stmt->case_to, stmt);
766 rl = get_case_rl(top_expression(switch_expr_stack),
767 stmt->case_expression, stmt->case_to);
768 while (stmt->case_statement->type == STMT_CASE) {
769 struct range_list *tmp;
771 tmp = get_case_rl(top_expression(switch_expr_stack),
772 stmt->case_statement->case_expression,
773 stmt->case_statement->case_to);
774 if (!tmp)
775 break;
776 rl = rl_union(rl, tmp);
777 if (!stmt->case_expression)
778 __set_default();
779 stmt = stmt->case_statement;
782 __merge_switches(top_expression(switch_expr_stack), rl);
784 if (!stmt->case_expression)
785 __set_default();
786 __split_stmt(stmt->case_statement);
789 int time_parsing_function(void)
791 return ms_since(&fn_start_time) / 1000;
794 static int taking_too_long(void)
796 if (time_parsing_function() > 60 * 5) /* five minutes */
797 return 1;
798 return 0;
801 static int is_last_stmt(struct statement *cur_stmt)
803 struct symbol *fn = get_base_type(cur_func_sym);
804 struct statement *stmt;
806 if (!fn)
807 return 0;
808 stmt = fn->stmt;
809 if (!stmt)
810 stmt = fn->inline_stmt;
811 if (!stmt || stmt->type != STMT_COMPOUND)
812 return 0;
813 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
814 if (stmt && stmt->type == STMT_LABEL)
815 stmt = stmt->label_statement;
816 if (stmt == cur_stmt)
817 return 1;
818 return 0;
821 static void handle_backward_goto(struct statement *goto_stmt)
823 const char *goto_name, *label_name;
824 struct statement *func_stmt;
825 struct symbol *base_type = get_base_type(cur_func_sym);
826 struct statement *tmp;
827 int found = 0;
829 if (!option_info)
830 return;
831 if (last_goto_statement_handled)
832 return;
833 last_goto_statement_handled = 1;
835 if (!goto_stmt->goto_label ||
836 goto_stmt->goto_label->type != SYM_LABEL ||
837 !goto_stmt->goto_label->ident)
838 return;
839 goto_name = goto_stmt->goto_label->ident->name;
841 func_stmt = base_type->stmt;
842 if (!func_stmt)
843 func_stmt = base_type->inline_stmt;
844 if (!func_stmt)
845 return;
846 if (func_stmt->type != STMT_COMPOUND)
847 return;
849 FOR_EACH_PTR(func_stmt->stmts, tmp) {
850 if (!found) {
851 if (tmp->type != STMT_LABEL)
852 continue;
853 if (!tmp->label_identifier ||
854 tmp->label_identifier->type != SYM_LABEL ||
855 !tmp->label_identifier->ident)
856 continue;
857 label_name = tmp->label_identifier->ident->name;
858 if (strcmp(goto_name, label_name) != 0)
859 continue;
860 found = 1;
862 __split_stmt(tmp);
863 } END_FOR_EACH_PTR(tmp);
866 static void fake_a_return(void)
868 struct symbol *return_type;
870 nullify_path();
871 __unnullify_path();
873 return_type = get_real_base_type(cur_func_sym);
874 return_type = get_real_base_type(return_type);
875 if (return_type != &void_ctype) {
876 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
877 nullify_path();
881 static void fake_an_empty_default(struct position pos)
883 static struct statement none = {};
885 none.pos = pos;
886 none.type = STMT_NONE;
887 __merge_switches(top_expression(switch_expr_stack), NULL);
888 __split_stmt(&none);
891 static void split_compound(struct statement *stmt)
893 struct statement *prev = NULL;
894 struct statement *cur = NULL;
895 struct statement *next;
897 __push_scope_hooks();
899 FOR_EACH_PTR(stmt->stmts, next) {
900 /* just set them all ahead of time */
901 stmt_set_parent_stmt(next, stmt);
903 if (cur) {
904 __prev_stmt = prev;
905 __next_stmt = next;
906 __cur_stmt = cur;
907 __split_stmt(cur);
909 prev = cur;
910 cur = next;
911 } END_FOR_EACH_PTR(next);
912 if (cur) {
913 __prev_stmt = prev;
914 __cur_stmt = cur;
915 __next_stmt = NULL;
916 __split_stmt(cur);
920 * For function scope, then delay calling the scope hooks until the
921 * end of function hooks can run. I'm not positive this is the right
922 * thing...
924 if (!is_last_stmt(cur))
925 __call_scope_hooks();
929 * This is a hack, work around for detecting empty functions.
931 static int need_delayed_scope_hooks(void)
933 struct symbol *fn = get_base_type(cur_func_sym);
934 struct statement *stmt;
936 if (!fn)
937 return 0;
938 stmt = fn->stmt;
939 if (!stmt)
940 stmt = fn->inline_stmt;
941 if (stmt && stmt->type == STMT_COMPOUND)
942 return 1;
943 return 0;
946 void __split_label_stmt(struct statement *stmt)
948 if (stmt->label_identifier &&
949 stmt->label_identifier->type == SYM_LABEL &&
950 stmt->label_identifier->ident) {
951 loop_count |= 0x0800000;
952 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
956 static void find_asm_gotos(struct statement *stmt)
958 struct symbol *sym;
960 FOR_EACH_PTR(stmt->asm_labels, sym) {
961 __save_gotos(sym->ident->name, sym);
962 } END_FOR_EACH_PTR(sym);
965 void __split_stmt(struct statement *stmt)
967 sval_t sval;
969 if (!stmt)
970 goto out;
972 if (!__in_fake_assign)
973 __silence_warnings_for_stmt = false;
975 if (__bail_on_rest_of_function)
976 return;
978 if (out_of_memory() || taking_too_long()) {
979 struct timeval stop;
981 gettimeofday(&stop, NULL);
983 __bail_on_rest_of_function = 1;
984 final_pass = 1;
985 sm_msg("Function too hairy. Giving up. %lu seconds",
986 stop.tv_sec - fn_start_time.tv_sec);
987 fake_a_return();
988 final_pass = 0; /* turn off sm_msg() from here */
989 return;
992 add_ptr_list(&big_statement_stack, stmt);
993 free_expression_stack(&big_expression_stack);
994 set_position(stmt->pos);
995 __pass_to_client(stmt, STMT_HOOK);
997 switch (stmt->type) {
998 case STMT_DECLARATION:
999 split_declaration(stmt->declaration);
1000 break;
1001 case STMT_RETURN:
1002 expr_set_parent_stmt(stmt->ret_value, stmt);
1004 __split_expr(stmt->ret_value);
1005 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1006 __process_post_op_stack();
1007 nullify_path();
1008 break;
1009 case STMT_EXPRESSION:
1010 expr_set_parent_stmt(stmt->expression, stmt);
1011 expr_set_parent_stmt(stmt->context, stmt);
1013 __split_expr(stmt->expression);
1014 break;
1015 case STMT_COMPOUND:
1016 split_compound(stmt);
1017 break;
1018 case STMT_IF:
1019 stmt_set_parent_stmt(stmt->if_true, stmt);
1020 stmt_set_parent_stmt(stmt->if_false, stmt);
1021 expr_set_parent_stmt(stmt->if_conditional, stmt);
1023 if (known_condition_true(stmt->if_conditional)) {
1024 __split_stmt(stmt->if_true);
1025 break;
1027 if (known_condition_false(stmt->if_conditional)) {
1028 __split_stmt(stmt->if_false);
1029 break;
1031 __split_whole_condition(stmt->if_conditional);
1032 __split_stmt(stmt->if_true);
1033 if (empty_statement(stmt->if_true) &&
1034 last_stmt_on_same_line() &&
1035 !get_macro_name(stmt->if_true->pos))
1036 sm_msg("warn: if();");
1037 __push_true_states();
1038 __use_false_states();
1039 __split_stmt(stmt->if_false);
1040 __merge_true_states();
1041 break;
1042 case STMT_ITERATOR:
1043 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1044 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1045 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1046 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1047 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1049 if (stmt->iterator_pre_condition)
1050 handle_pre_loop(stmt);
1051 else if (stmt->iterator_post_condition)
1052 handle_post_loop(stmt);
1053 else {
1054 // these are for(;;) type loops.
1055 handle_pre_loop(stmt);
1057 break;
1058 case STMT_SWITCH:
1059 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1060 expr_set_parent_stmt(stmt->switch_expression, stmt);
1062 if (get_value(stmt->switch_expression, &sval)) {
1063 split_known_switch(stmt, sval);
1064 break;
1066 __split_expr(stmt->switch_expression);
1067 push_expression(&switch_expr_stack, stmt->switch_expression);
1068 __save_switch_states(top_expression(switch_expr_stack));
1069 nullify_path();
1070 __push_default();
1071 __push_breaks();
1072 __split_stmt(stmt->switch_statement);
1073 if (!__pop_default() && have_remaining_cases())
1074 fake_an_empty_default(stmt->pos);
1075 __discard_switches();
1076 __merge_breaks();
1077 pop_expression(&switch_expr_stack);
1078 break;
1079 case STMT_CASE:
1080 split_case(stmt);
1081 break;
1082 case STMT_LABEL:
1083 __split_label_stmt(stmt);
1084 __split_stmt(stmt->label_statement);
1085 break;
1086 case STMT_GOTO:
1087 expr_set_parent_stmt(stmt->goto_expression, stmt);
1089 __split_expr(stmt->goto_expression);
1090 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1091 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1092 __process_breaks();
1093 } else if (!strcmp(stmt->goto_label->ident->name,
1094 "continue")) {
1095 __process_continues();
1097 } else if (stmt->goto_label &&
1098 stmt->goto_label->type == SYM_LABEL &&
1099 stmt->goto_label->ident) {
1100 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1102 nullify_path();
1103 if (is_last_stmt(stmt))
1104 handle_backward_goto(stmt);
1105 break;
1106 case STMT_NONE:
1107 break;
1108 case STMT_ASM:
1109 expr_set_parent_stmt(stmt->asm_string, stmt);
1111 find_asm_gotos(stmt);
1112 __pass_to_client(stmt, ASM_HOOK);
1113 __split_expr(stmt->asm_string);
1114 split_asm_constraints(stmt->asm_outputs);
1115 split_asm_constraints(stmt->asm_inputs);
1116 split_asm_constraints(stmt->asm_clobbers);
1117 break;
1118 case STMT_CONTEXT:
1119 break;
1120 case STMT_RANGE:
1121 __split_expr(stmt->range_expression);
1122 __split_expr(stmt->range_low);
1123 __split_expr(stmt->range_high);
1124 break;
1126 __pass_to_client(stmt, STMT_HOOK_AFTER);
1127 out:
1128 __process_post_op_stack();
1131 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1133 struct expression *expr;
1135 FOR_EACH_PTR(expr_list, expr) {
1136 expr_set_parent_expr(expr, parent);
1137 __split_expr(expr);
1138 __process_post_op_stack();
1139 } END_FOR_EACH_PTR(expr);
1142 static void split_sym(struct symbol *sym)
1144 if (!sym)
1145 return;
1146 if (!(sym->namespace & NS_SYMBOL))
1147 return;
1149 __split_stmt(sym->stmt);
1150 __split_expr(sym->array_size);
1151 split_symlist(sym->arguments);
1152 split_symlist(sym->symbol_list);
1153 __split_stmt(sym->inline_stmt);
1154 split_symlist(sym->inline_symbol_list);
1157 static void split_symlist(struct symbol_list *sym_list)
1159 struct symbol *sym;
1161 FOR_EACH_PTR(sym_list, sym) {
1162 split_sym(sym);
1163 } END_FOR_EACH_PTR(sym);
1166 typedef void (fake_cb)(struct expression *expr);
1168 static int member_to_number(struct expression *expr, struct ident *member)
1170 struct symbol *type, *tmp;
1171 char *name;
1172 int i;
1174 if (!member)
1175 return -1;
1176 name = member->name;
1178 type = get_type(expr);
1179 if (!type || type->type != SYM_STRUCT)
1180 return -1;
1182 i = -1;
1183 FOR_EACH_PTR(type->symbol_list, tmp) {
1184 i++;
1185 if (!tmp->ident)
1186 continue;
1187 if (strcmp(name, tmp->ident->name) == 0)
1188 return i;
1189 } END_FOR_EACH_PTR(tmp);
1190 return -1;
1193 static struct ident *number_to_member(struct expression *expr, int num)
1195 struct symbol *type, *member;
1196 int i = 0;
1198 type = get_type(expr);
1199 if (!type || type->type != SYM_STRUCT)
1200 return NULL;
1202 FOR_EACH_PTR(type->symbol_list, member) {
1203 if (i == num)
1204 return member->ident;
1205 i++;
1206 } END_FOR_EACH_PTR(member);
1207 return NULL;
1210 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1212 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1214 struct expression *edge_member, *assign;
1215 struct symbol *base = get_real_base_type(member);
1216 struct symbol *tmp;
1218 if (member->ident)
1219 expr = member_expression(expr, '.', member->ident);
1221 FOR_EACH_PTR(base->symbol_list, tmp) {
1222 struct symbol *type;
1224 type = get_real_base_type(tmp);
1225 if (!type)
1226 continue;
1228 edge_member = member_expression(expr, '.', tmp->ident);
1229 if (get_extra_state(edge_member))
1230 continue;
1232 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1233 set_inner_struct_members(expr, tmp);
1234 continue;
1237 if (!tmp->ident)
1238 continue;
1240 assign = assign_expression(edge_member, zero_expr());
1241 __split_expr(assign);
1242 } END_FOR_EACH_PTR(tmp);
1247 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1249 struct symbol *tmp;
1250 struct expression *member = NULL;
1251 struct expression *assign;
1252 int op = '*';
1254 if (expr->type == EXPR_PREOP && expr->op == '&') {
1255 expr = strip_expr(expr->unop);
1256 op = '.';
1259 FOR_EACH_PTR(type->symbol_list, tmp) {
1260 type = get_real_base_type(tmp);
1261 if (!type)
1262 continue;
1264 if (tmp->ident) {
1265 member = member_expression(expr, op, tmp->ident);
1266 if (get_extra_state(member))
1267 continue;
1270 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1271 set_inner_struct_members(expr, tmp);
1272 continue;
1274 if (type->type == SYM_ARRAY)
1275 continue;
1276 if (!tmp->ident)
1277 continue;
1279 assign = assign_expression(member, zero_expr());
1280 __split_expr(assign);
1281 } END_FOR_EACH_PTR(tmp);
1284 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1286 struct expression *deref, *assign, *tmp, *right;
1287 struct symbol *struct_type, *type;
1288 struct ident *member;
1289 int member_idx;
1291 struct_type = get_type(symbol);
1292 if (!struct_type ||
1293 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1294 return;
1297 * We're parsing an initializer that could look something like this:
1298 * struct foo foo = {
1299 * 42,
1300 * .whatever.xxx = 11,
1301 * .zzz = 12,
1302 * };
1304 * So what we have here is a list with 42, .whatever, and .zzz. We need
1305 * to break it up into left and right sides of the assignments.
1308 member_idx = 0;
1309 FOR_EACH_PTR(members, tmp) {
1310 deref = NULL;
1311 if (tmp->type == EXPR_IDENTIFIER) {
1312 member_idx = member_to_number(symbol, tmp->expr_ident);
1313 while (tmp->type == EXPR_IDENTIFIER) {
1314 member = tmp->expr_ident;
1315 tmp = tmp->ident_expression;
1316 if (deref)
1317 deref = member_expression(deref, '.', member);
1318 else
1319 deref = member_expression(symbol, '.', member);
1321 } else {
1322 member = number_to_member(symbol, member_idx);
1323 deref = member_expression(symbol, '.', member);
1325 right = tmp;
1326 member_idx++;
1327 if (right->type == EXPR_INITIALIZER) {
1328 type = get_type(deref);
1329 if (type && type->type == SYM_ARRAY)
1330 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1331 else
1332 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1333 } else {
1334 assign = assign_expression(deref, right);
1335 fake_cb(assign);
1337 } END_FOR_EACH_PTR(tmp);
1339 set_unset_to_zero(struct_type, symbol);
1342 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1344 fake_member_assigns_helper(symbol_expression(sym),
1345 sym->initializer->expr_list, fake_cb);
1348 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1350 struct expression *offset, *binop, *assign, *tmp;
1351 struct symbol *type;
1352 int idx;
1354 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1355 return;
1357 idx = 0;
1358 FOR_EACH_PTR(expr_list, tmp) {
1359 if (tmp->type == EXPR_INDEX) {
1360 if (tmp->idx_from != tmp->idx_to)
1361 return;
1362 idx = tmp->idx_from;
1363 if (!tmp->idx_expression)
1364 goto next;
1365 tmp = tmp->idx_expression;
1367 offset = value_expr(idx);
1368 binop = array_element_expression(array, offset);
1369 if (tmp->type == EXPR_INITIALIZER) {
1370 type = get_type(binop);
1371 if (type && type->type == SYM_ARRAY)
1372 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1373 else
1374 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1375 } else {
1376 assign = assign_expression(binop, tmp);
1377 fake_cb(assign);
1379 next:
1380 idx++;
1381 } END_FOR_EACH_PTR(tmp);
1384 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1386 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1389 static void fake_assign_expr(struct symbol *sym)
1391 struct expression *assign, *symbol;
1393 symbol = symbol_expression(sym);
1394 assign = assign_expression(symbol, sym->initializer);
1395 __split_expr(assign);
1398 static void call_split_expr(struct expression *expr)
1400 __split_expr(expr);
1403 static void do_initializer_stuff(struct symbol *sym)
1405 if (!sym->initializer)
1406 return;
1408 if (sym->initializer->type == EXPR_INITIALIZER) {
1409 if (get_real_base_type(sym)->type == SYM_ARRAY)
1410 fake_element_assigns(sym, call_split_expr);
1411 else
1412 fake_member_assigns(sym, call_split_expr);
1413 } else {
1414 fake_assign_expr(sym);
1418 static void split_declaration(struct symbol_list *sym_list)
1420 struct symbol *sym;
1422 FOR_EACH_PTR(sym_list, sym) {
1423 __pass_to_client(sym, DECLARATION_HOOK);
1424 do_initializer_stuff(sym);
1425 split_sym(sym);
1426 } END_FOR_EACH_PTR(sym);
1429 static void call_global_assign_hooks(struct expression *assign)
1431 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1434 static void fake_global_assign(struct symbol *sym)
1436 struct expression *assign, *symbol;
1438 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1439 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1440 fake_element_assigns(sym, call_global_assign_hooks);
1441 } else if (sym->initializer) {
1442 symbol = symbol_expression(sym);
1443 assign = assign_expression(symbol, sym->initializer);
1444 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1445 } else {
1446 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1448 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1449 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1450 fake_member_assigns(sym, call_global_assign_hooks);
1451 } else if (sym->initializer) {
1452 symbol = symbol_expression(sym);
1453 assign = assign_expression(symbol, sym->initializer);
1454 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1455 } else {
1456 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1458 } else {
1459 symbol = symbol_expression(sym);
1460 if (sym->initializer)
1461 assign = assign_expression(symbol, sym->initializer);
1462 else
1463 assign = assign_expression(symbol, zero_expr());
1464 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1468 static void start_function_definition(struct symbol *sym)
1470 __in_function_def = 1;
1471 __pass_to_client(sym, FUNC_DEF_HOOK);
1472 __in_function_def = 0;
1473 __pass_to_client(sym, AFTER_DEF_HOOK);
1477 static void split_function(struct symbol *sym)
1479 struct symbol *base_type = get_base_type(sym);
1481 if (!base_type->stmt && !base_type->inline_stmt)
1482 return;
1484 gettimeofday(&fn_start_time, NULL);
1485 cur_func_sym = sym;
1486 if (sym->ident)
1487 cur_func = sym->ident->name;
1488 set_position(sym->pos);
1489 loop_count = 0;
1490 last_goto_statement_handled = 0;
1491 sm_debug("new function: %s\n", cur_func);
1492 __stree_id = 0;
1493 if (option_two_passes) {
1494 __unnullify_path();
1495 loop_num = 0;
1496 final_pass = 0;
1497 start_function_definition(sym);
1498 __split_stmt(base_type->stmt);
1499 __split_stmt(base_type->inline_stmt);
1500 nullify_path();
1502 __unnullify_path();
1503 loop_num = 0;
1504 final_pass = 1;
1505 start_function_definition(sym);
1506 __split_stmt(base_type->stmt);
1507 __split_stmt(base_type->inline_stmt);
1508 __pass_to_client(sym, END_FUNC_HOOK);
1509 if (need_delayed_scope_hooks())
1510 __call_scope_hooks();
1511 __pass_to_client(sym, AFTER_FUNC_HOOK);
1513 clear_all_states();
1514 cur_func_sym = NULL;
1515 cur_func = NULL;
1516 free_data_info_allocs();
1517 free_expression_stack(&switch_expr_stack);
1518 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1519 __bail_on_rest_of_function = 0;
1522 static void save_flow_state(void)
1524 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1525 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1526 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1528 __add_ptr_list(&backup, big_statement_stack, 0);
1529 __add_ptr_list(&backup, big_expression_stack, 0);
1530 __add_ptr_list(&backup, big_condition_stack, 0);
1531 __add_ptr_list(&backup, switch_expr_stack, 0);
1533 __add_ptr_list(&backup, cur_func_sym, 0);
1535 __add_ptr_list(&backup, __prev_stmt, 0);
1536 __add_ptr_list(&backup, __cur_stmt, 0);
1537 __add_ptr_list(&backup, __next_stmt, 0);
1541 static void *pop_backup(void)
1543 void *ret;
1545 ret = last_ptr_list(backup);
1546 delete_ptr_list_last(&backup);
1547 return ret;
1550 static void restore_flow_state(void)
1552 __next_stmt = pop_backup();
1553 __cur_stmt = pop_backup();
1554 __prev_stmt = pop_backup();
1556 cur_func_sym = pop_backup();
1557 switch_expr_stack = pop_backup();
1558 big_condition_stack = pop_backup();
1559 big_expression_stack = pop_backup();
1560 big_statement_stack = pop_backup();
1561 final_pass = PTR_INT(pop_backup()) >> 2;
1562 loop_count = PTR_INT(pop_backup()) >> 2;
1563 loop_num = PTR_INT(pop_backup()) >> 2;
1566 static void parse_inline(struct expression *call)
1568 struct symbol *base_type;
1569 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1570 struct timeval time_backup = fn_start_time;
1572 save_flow_state();
1574 __pass_to_client(call, INLINE_FN_START);
1575 final_pass = 0; /* don't print anything */
1576 __inline_fn = call;
1578 base_type = get_base_type(call->fn->symbol);
1579 cur_func_sym = call->fn->symbol;
1580 if (call->fn->symbol->ident)
1581 cur_func = call->fn->symbol->ident->name;
1582 else
1583 cur_func = NULL;
1584 set_position(call->fn->symbol->pos);
1586 save_all_states();
1587 big_statement_stack = NULL;
1588 big_expression_stack = NULL;
1589 big_condition_stack = NULL;
1590 switch_expr_stack = NULL;
1592 sm_debug("inline function: %s\n", cur_func);
1593 __unnullify_path();
1594 loop_num = 0;
1595 loop_count = 0;
1596 start_function_definition(call->fn->symbol);
1597 __split_stmt(base_type->stmt);
1598 __split_stmt(base_type->inline_stmt);
1599 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1600 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1602 free_expression_stack(&switch_expr_stack);
1603 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1604 nullify_path();
1605 free_goto_stack();
1607 restore_flow_state();
1608 fn_start_time = time_backup;
1609 cur_func = cur_func_bak;
1611 restore_all_states();
1612 set_position(call->pos);
1613 __inline_fn = NULL;
1614 __pass_to_client(call, INLINE_FN_END);
1617 static struct symbol_list *inlines_called;
1618 static void add_inline_function(struct symbol *sym)
1620 static struct symbol_list *already_added;
1621 struct symbol *tmp;
1623 FOR_EACH_PTR(already_added, tmp) {
1624 if (tmp == sym)
1625 return;
1626 } END_FOR_EACH_PTR(tmp);
1628 add_ptr_list(&already_added, sym);
1629 add_ptr_list(&inlines_called, sym);
1632 static void process_inlines(void)
1634 struct symbol *tmp;
1636 FOR_EACH_PTR(inlines_called, tmp) {
1637 split_function(tmp);
1638 } END_FOR_EACH_PTR(tmp);
1639 free_ptr_list(&inlines_called);
1642 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1644 struct symbol *sym;
1646 FOR_EACH_PTR_REVERSE(big_list, sym) {
1647 if (!sym->scope)
1648 continue;
1649 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1650 return sym;
1651 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1652 return sym;
1653 } END_FOR_EACH_PTR_REVERSE(sym);
1655 return NULL;
1658 static void split_inlines_in_scope(struct symbol *sym)
1660 struct symbol *base;
1661 struct symbol_list *scope_list;
1662 int stream;
1664 scope_list = sym->scope->symbols;
1665 stream = sym->pos.stream;
1667 /* find the last static symbol in the file */
1668 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1669 if (sym->pos.stream != stream)
1670 continue;
1671 if (sym->type != SYM_NODE)
1672 continue;
1673 base = get_base_type(sym);
1674 if (!base)
1675 continue;
1676 if (base->type != SYM_FN)
1677 continue;
1678 if (!base->inline_stmt)
1679 continue;
1680 add_inline_function(sym);
1681 } END_FOR_EACH_PTR_REVERSE(sym);
1683 process_inlines();
1686 static void split_inlines(struct symbol_list *sym_list)
1688 struct symbol *sym;
1690 sym = get_last_scoped_symbol(sym_list, 0);
1691 if (sym)
1692 split_inlines_in_scope(sym);
1693 sym = get_last_scoped_symbol(sym_list, 1);
1694 if (sym)
1695 split_inlines_in_scope(sym);
1698 static struct stree *clone_estates_perm(struct stree *orig)
1700 struct stree *ret = NULL;
1701 struct sm_state *tmp;
1703 FOR_EACH_SM(orig, tmp) {
1704 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1705 } END_FOR_EACH_SM(tmp);
1707 return ret;
1710 static bool interesting_function(struct symbol *sym)
1712 static int prev_stream = -1;
1713 static bool prev_answer;
1714 const char *filename;
1715 int len;
1718 if (!(sym->ctype.modifiers & MOD_INLINE))
1719 return true;
1721 if (sym->pos.stream == prev_stream)
1722 return prev_answer;
1724 prev_stream = sym->pos.stream;
1725 prev_answer = false;
1727 filename = stream_name(sym->pos.stream);
1728 len = strlen(filename);
1729 if (len > 0 && filename[len - 1] == 'c')
1730 prev_answer = true;
1731 return prev_answer;
1734 struct position last_pos;
1735 static void split_c_file_functions(struct symbol_list *sym_list)
1737 struct symbol *sym;
1739 __unnullify_path();
1740 FOR_EACH_PTR(sym_list, sym) {
1741 set_position(sym->pos);
1742 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1743 __pass_to_client(sym, BASE_HOOK);
1744 fake_global_assign(sym);
1746 } END_FOR_EACH_PTR(sym);
1747 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1748 nullify_path();
1750 FOR_EACH_PTR(sym_list, sym) {
1751 set_position(sym->pos);
1752 last_pos = sym->pos;
1753 if (!interesting_function(sym))
1754 continue;
1755 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1756 split_function(sym);
1757 process_inlines();
1759 last_pos = sym->pos;
1760 } END_FOR_EACH_PTR(sym);
1761 split_inlines(sym_list);
1762 __pass_to_client(sym_list, END_FILE_HOOK);
1765 static int final_before_fake;
1766 void init_fake_env(void)
1768 if (!in_fake_env)
1769 final_before_fake = final_pass;
1770 in_fake_env++;
1771 __push_fake_cur_stree();
1772 final_pass = 0;
1775 void end_fake_env(void)
1777 __pop_fake_cur_stree();
1778 in_fake_env--;
1779 if (!in_fake_env)
1780 final_pass = final_before_fake;
1783 static void open_output_files(char *base_file)
1785 char buf[256];
1787 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1788 sm_outfd = fopen(buf, "w");
1789 if (!sm_outfd) {
1790 printf("Error: Cannot open %s\n", buf);
1791 exit(1);
1794 if (!option_info)
1795 return;
1797 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1798 sql_outfd = fopen(buf, "w");
1799 if (!sql_outfd) {
1800 printf("Error: Cannot open %s\n", buf);
1801 exit(1);
1804 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1805 caller_info_fd = fopen(buf, "w");
1806 if (!caller_info_fd) {
1807 printf("Error: Cannot open %s\n", buf);
1808 exit(1);
1812 void smatch(int argc, char **argv)
1814 struct string_list *filelist = NULL;
1815 struct symbol_list *sym_list;
1816 struct timeval stop, start;
1818 gettimeofday(&start, NULL);
1820 if (argc < 2) {
1821 printf("Usage: smatch [--debug] <filename.c>\n");
1822 exit(1);
1824 sparse_initialize(argc, argv, &filelist);
1825 set_valid_ptr_max();
1826 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1827 if (option_file_output)
1828 open_output_files(base_file);
1829 sym_list = sparse_keep_tokens(base_file);
1830 split_c_file_functions(sym_list);
1831 } END_FOR_EACH_PTR_NOTAG(base_file);
1833 gettimeofday(&stop, NULL);
1835 set_position(last_pos);
1836 if (option_time)
1837 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1838 if (option_mem)
1839 sm_msg("mem: %luKb", get_max_memory());