db: introduce the cache_db
[smatch.git] / smatch_flow.c
blobb0f091b859080450cf5e3d3b6108f200d2fe9dba
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 static int inline_budget = 20;
180 int inlinable(struct expression *expr)
182 struct symbol *sym;
183 struct statement *last_stmt = NULL;
185 if (__inline_fn) /* don't nest */
186 return 0;
188 if (expr->type != EXPR_SYMBOL || !expr->symbol)
189 return 0;
190 if (is_no_inline_function(expr->symbol->ident->name))
191 return 0;
192 sym = get_base_type(expr->symbol);
193 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
194 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
195 return 0;
196 if (sym->stmt->type != STMT_COMPOUND)
197 return 0;
198 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
200 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
201 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
202 return 0;
203 if (sym->inline_stmt->type != STMT_COMPOUND)
204 return 0;
205 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
208 if (!last_stmt)
209 return 0;
211 /* the magic numbers in this function are pulled out of my bum. */
212 if (last_stmt->pos.line > sym->pos.line + inline_budget)
213 return 0;
215 return 1;
218 void __process_post_op_stack(void)
220 struct expression *expr;
222 FOR_EACH_PTR(post_op_stack, expr) {
223 __pass_to_client(expr, OP_HOOK);
224 } END_FOR_EACH_PTR(expr);
226 __free_ptr_list((struct ptr_list **)&post_op_stack);
229 static int handle_comma_assigns(struct expression *expr)
231 struct expression *right;
232 struct expression *assign;
234 right = strip_expr(expr->right);
235 if (right->type != EXPR_COMMA)
236 return 0;
238 __split_expr(right->left);
239 __process_post_op_stack();
241 assign = assign_expression(expr->left, '=', right->right);
242 __split_expr(assign);
244 return 1;
247 /* This is to handle *p++ = foo; assignments */
248 static int handle_postop_assigns(struct expression *expr)
250 struct expression *left, *fake_left;
251 struct expression *assign;
253 left = strip_expr(expr->left);
254 if (left->type != EXPR_PREOP || left->op != '*')
255 return 0;
256 left = strip_expr(left->unop);
257 if (left->type != EXPR_POSTOP)
258 return 0;
260 fake_left = deref_expression(strip_expr(left->unop));
261 assign = assign_expression(fake_left, '=', expr->right);
263 __split_expr(assign);
264 __split_expr(expr->left);
266 return 1;
269 static int prev_expression_is_getting_address(struct expression *expr)
271 struct expression *parent;
273 do {
274 parent = expr_get_parent_expr(expr);
276 if (!parent)
277 return 0;
278 if (parent->type == EXPR_PREOP && parent->op == '&')
279 return 1;
280 if (parent->type == EXPR_PREOP && parent->op == '(')
281 goto next;
282 if (parent->type == EXPR_DEREF && parent->op == '.')
283 goto next;
285 return 0;
286 next:
287 expr = parent;
288 } while (1);
291 static int handle__builtin_choose_expr(struct expression *expr)
293 struct expression *const_expr, *expr1, *expr2;
294 sval_t sval;
296 if (!sym_name_is("__builtin_choose_expr", expr->fn))
297 return 0;
299 const_expr = get_argument_from_call_expr(expr->args, 0);
300 expr1 = get_argument_from_call_expr(expr->args, 1);
301 expr2 = get_argument_from_call_expr(expr->args, 2);
303 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
304 return 0;
305 if (sval.value)
306 __split_expr(expr1);
307 else
308 __split_expr(expr2);
309 return 1;
312 void __split_expr(struct expression *expr)
314 if (!expr)
315 return;
317 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
319 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
320 return;
321 if (__in_fake_assign >= 4) /* don't allow too much nesting */
322 return;
324 push_expression(&big_expression_stack, expr);
325 set_position(expr->pos);
326 __pass_to_client(expr, EXPR_HOOK);
328 switch (expr->type) {
329 case EXPR_PREOP:
330 expr_set_parent_expr(expr->unop, expr);
332 if (expr->op == '*' &&
333 !prev_expression_is_getting_address(expr))
334 __pass_to_client(expr, DEREF_HOOK);
335 __split_expr(expr->unop);
336 __pass_to_client(expr, OP_HOOK);
337 break;
338 case EXPR_POSTOP:
339 expr_set_parent_expr(expr->unop, expr);
341 __split_expr(expr->unop);
342 push_expression(&post_op_stack, expr);
343 break;
344 case EXPR_STATEMENT:
345 __expr_stmt_count++;
346 if (expr->statement && !expr->statement) {
347 stmt_set_parent_stmt(expr->statement,
348 last_ptr_list((struct ptr_list *)big_statement_stack));
350 __split_stmt(expr->statement);
351 __expr_stmt_count--;
352 break;
353 case EXPR_LOGICAL:
354 case EXPR_COMPARE:
355 expr_set_parent_expr(expr->left, expr);
356 expr_set_parent_expr(expr->right, expr);
358 __pass_to_client(expr, LOGIC_HOOK);
359 __handle_logic(expr);
360 break;
361 case EXPR_BINOP:
362 expr_set_parent_expr(expr->left, expr);
363 expr_set_parent_expr(expr->right, expr);
365 __pass_to_client(expr, BINOP_HOOK);
366 case EXPR_COMMA:
367 expr_set_parent_expr(expr->left, expr);
368 expr_set_parent_expr(expr->right, expr);
370 __split_expr(expr->left);
371 __process_post_op_stack();
372 __split_expr(expr->right);
373 break;
374 case EXPR_ASSIGNMENT: {
375 struct expression *right;
377 expr_set_parent_expr(expr->left, expr);
378 expr_set_parent_expr(expr->right, expr);
380 right = strip_expr(expr->right);
381 if (!right)
382 break;
384 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
386 /* foo = !bar() */
387 if (__handle_condition_assigns(expr))
388 break;
389 /* foo = (x < 5 ? foo : 5); */
390 if (__handle_select_assigns(expr))
391 break;
392 /* foo = ({frob(); frob(); frob(); 1;}) */
393 if (__handle_expr_statement_assigns(expr))
394 break;
395 /* foo = (3, 4); */
396 if (handle_comma_assigns(expr))
397 break;
398 if (handle_postop_assigns(expr))
399 break;
401 __split_expr(expr->right);
402 if (outside_of_function())
403 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
404 else
405 __pass_to_client(expr, ASSIGNMENT_HOOK);
407 __fake_struct_member_assignments(expr);
409 if (expr->op == '=' && right->type == EXPR_CALL)
410 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
412 if (get_macro_name(right->pos) &&
413 get_macro_name(expr->pos) != get_macro_name(right->pos))
414 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
416 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
418 __split_expr(expr->left);
419 break;
421 case EXPR_DEREF:
422 expr_set_parent_expr(expr->deref, expr);
424 __pass_to_client(expr, DEREF_HOOK);
425 __split_expr(expr->deref);
426 break;
427 case EXPR_SLICE:
428 expr_set_parent_expr(expr->base, expr);
430 __split_expr(expr->base);
431 break;
432 case EXPR_CAST:
433 case EXPR_FORCE_CAST:
434 expr_set_parent_expr(expr->cast_expression, expr);
436 __pass_to_client(expr, CAST_HOOK);
437 __split_expr(expr->cast_expression);
438 break;
439 case EXPR_SIZEOF:
440 if (expr->cast_expression)
441 __pass_to_client(strip_parens(expr->cast_expression),
442 SIZEOF_HOOK);
443 break;
444 case EXPR_OFFSETOF:
445 case EXPR_ALIGNOF:
446 evaluate_expression(expr);
447 break;
448 case EXPR_CONDITIONAL:
449 case EXPR_SELECT:
450 expr_set_parent_expr(expr->conditional, expr);
451 expr_set_parent_expr(expr->cond_true, expr);
452 expr_set_parent_expr(expr->cond_false, expr);
454 if (known_condition_true(expr->conditional)) {
455 __split_expr(expr->cond_true);
456 break;
458 if (known_condition_false(expr->conditional)) {
459 __split_expr(expr->cond_false);
460 break;
462 __pass_to_client(expr, SELECT_HOOK);
463 __split_whole_condition(expr->conditional);
464 __split_expr(expr->cond_true);
465 __push_true_states();
466 __use_false_states();
467 __split_expr(expr->cond_false);
468 __merge_true_states();
469 break;
470 case EXPR_CALL:
471 expr_set_parent_expr(expr->fn, expr);
473 if (sym_name_is("__builtin_constant_p", expr->fn))
474 break;
475 if (handle__builtin_choose_expr(expr))
476 break;
477 split_expr_list(expr->args, expr);
478 __split_expr(expr->fn);
479 if (is_inline_func(expr->fn))
480 add_inline_function(expr->fn->symbol);
481 if (inlinable(expr->fn))
482 __inline_call = 1;
483 __process_post_op_stack();
484 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
485 __pass_to_client(expr, FUNCTION_CALL_HOOK);
486 __inline_call = 0;
487 if (inlinable(expr->fn)) {
488 parse_inline(expr);
490 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
491 if (is_noreturn_func(expr->fn))
492 nullify_path();
493 break;
494 case EXPR_INITIALIZER:
495 split_expr_list(expr->expr_list, expr);
496 break;
497 case EXPR_IDENTIFIER:
498 expr_set_parent_expr(expr->ident_expression, expr);
499 __split_expr(expr->ident_expression);
500 break;
501 case EXPR_INDEX:
502 expr_set_parent_expr(expr->idx_expression, expr);
503 __split_expr(expr->idx_expression);
504 break;
505 case EXPR_POS:
506 expr_set_parent_expr(expr->init_expr, expr);
507 __split_expr(expr->init_expr);
508 break;
509 case EXPR_SYMBOL:
510 __pass_to_client(expr, SYM_HOOK);
511 break;
512 case EXPR_STRING:
513 __pass_to_client(expr, STRING_HOOK);
514 break;
515 default:
516 break;
518 pop_expression(&big_expression_stack);
521 static int is_forever_loop(struct statement *stmt)
523 struct expression *expr;
524 sval_t sval;
526 expr = strip_expr(stmt->iterator_pre_condition);
527 if (!expr)
528 expr = stmt->iterator_post_condition;
529 if (!expr) {
530 /* this is a for(;;) loop... */
531 return 1;
534 if (get_value(expr, &sval) && sval.value != 0)
535 return 1;
537 return 0;
540 static int loop_num;
541 static char *get_loop_name(int num)
543 char buf[256];
545 snprintf(buf, 255, "-loop%d", num);
546 buf[255] = '\0';
547 return alloc_sname(buf);
551 * Pre Loops are while and for loops.
553 static void handle_pre_loop(struct statement *stmt)
555 int once_through; /* we go through the loop at least once */
556 struct sm_state *extra_sm = NULL;
557 int unchanged = 0;
558 char *loop_name;
559 struct stree *stree = NULL;
560 struct sm_state *sm = NULL;
562 loop_name = get_loop_name(loop_num);
563 loop_num++;
565 __split_stmt(stmt->iterator_pre_statement);
566 __prev_stmt = stmt->iterator_pre_statement;
568 once_through = implied_condition_true(stmt->iterator_pre_condition);
570 loop_count++;
571 __push_continues();
572 __push_breaks();
574 __merge_gotos(loop_name, NULL);
576 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
577 __in_pre_condition++;
578 __pass_to_client(stmt, PRELOOP_HOOK);
579 __split_whole_condition(stmt->iterator_pre_condition);
580 __in_pre_condition--;
581 FOR_EACH_SM(stree, sm) {
582 set_state(sm->owner, sm->name, sm->sym, sm->state);
583 } END_FOR_EACH_SM(sm);
584 free_stree(&stree);
585 if (extra_sm)
586 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
588 if (option_assume_loops)
589 once_through = 1;
591 __split_stmt(stmt->iterator_statement);
592 if (is_forever_loop(stmt)) {
593 __merge_continues();
594 __save_gotos(loop_name, NULL);
596 __push_fake_cur_stree();
597 __split_stmt(stmt->iterator_post_statement);
598 stree = __pop_fake_cur_stree();
600 __discard_false_states();
601 __use_breaks();
603 if (!__path_is_null())
604 __merge_stree_into_cur(stree);
605 free_stree(&stree);
606 } else {
607 __merge_continues();
608 unchanged = __iterator_unchanged(extra_sm);
609 __split_stmt(stmt->iterator_post_statement);
610 __prev_stmt = stmt->iterator_post_statement;
611 __cur_stmt = stmt;
613 __save_gotos(loop_name, NULL);
614 __in_pre_condition++;
615 __split_whole_condition(stmt->iterator_pre_condition);
616 __in_pre_condition--;
617 nullify_path();
618 __merge_false_states();
619 if (once_through)
620 __discard_false_states();
621 else
622 __merge_false_states();
624 if (extra_sm && unchanged)
625 __extra_pre_loop_hook_after(extra_sm,
626 stmt->iterator_post_statement,
627 stmt->iterator_pre_condition);
628 __merge_breaks();
630 loop_count--;
634 * Post loops are do {} while();
636 static void handle_post_loop(struct statement *stmt)
638 char *loop_name;
640 loop_name = get_loop_name(loop_num);
641 loop_num++;
642 loop_count++;
644 __push_continues();
645 __push_breaks();
646 __merge_gotos(loop_name, NULL);
647 __split_stmt(stmt->iterator_statement);
648 __merge_continues();
649 if (!is_zero(stmt->iterator_post_condition))
650 __save_gotos(loop_name, NULL);
652 if (is_forever_loop(stmt)) {
653 __use_breaks();
654 } else {
655 __split_whole_condition(stmt->iterator_post_condition);
656 __use_false_states();
657 __merge_breaks();
659 loop_count--;
662 static int empty_statement(struct statement *stmt)
664 if (!stmt)
665 return 0;
666 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
667 return 1;
668 return 0;
671 static int last_stmt_on_same_line(void)
673 struct statement *stmt;
674 int i = 0;
676 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
677 if (!i++)
678 continue;
679 if (stmt->pos.line == get_lineno())
680 return 1;
681 return 0;
682 } END_FOR_EACH_PTR_REVERSE(stmt);
683 return 0;
686 static void split_asm_constraints(struct expression_list *expr_list)
688 struct expression *expr;
689 int state = 0;
691 FOR_EACH_PTR(expr_list, expr) {
692 switch (state) {
693 case 0: /* identifier */
694 case 1: /* constraint */
695 state++;
696 continue;
697 case 2: /* expression */
698 state = 0;
699 __split_expr(expr);
700 continue;
702 } END_FOR_EACH_PTR(expr);
705 static int is_case_val(struct statement *stmt, sval_t sval)
707 sval_t case_sval;
709 if (stmt->type != STMT_CASE)
710 return 0;
711 if (!stmt->case_expression) {
712 __set_default();
713 return 1;
715 if (!get_value(stmt->case_expression, &case_sval))
716 return 0;
717 if (case_sval.value == sval.value)
718 return 1;
719 return 0;
722 static struct range_list *get_case_rl(struct expression *switch_expr,
723 struct expression *case_expr,
724 struct expression *case_to)
726 sval_t start, end;
727 struct range_list *rl = NULL;
728 struct symbol *switch_type;
730 switch_type = get_type(switch_expr);
731 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
732 start = sval_cast(switch_type, start);
733 end = sval_cast(switch_type, end);
734 add_range(&rl, start, end);
735 } else if (get_value(case_expr, &start)) {
736 start = sval_cast(switch_type, start);
737 add_range(&rl, start, start);
740 return rl;
743 static void split_known_switch(struct statement *stmt, sval_t sval)
745 struct statement *tmp;
746 struct range_list *rl;
748 __split_expr(stmt->switch_expression);
749 sval = sval_cast(get_type(stmt->switch_expression), sval);
751 push_expression(&switch_expr_stack, stmt->switch_expression);
752 __save_switch_states(top_expression(switch_expr_stack));
753 nullify_path();
754 __push_default();
755 __push_breaks();
757 stmt = stmt->switch_statement;
759 __push_scope_hooks();
760 FOR_EACH_PTR(stmt->stmts, tmp) {
761 __smatch_lineno = tmp->pos.line;
762 if (is_case_val(tmp, sval)) {
763 rl = alloc_rl(sval, sval);
764 __merge_switches(top_expression(switch_expr_stack), rl);
765 __pass_case_to_client(top_expression(switch_expr_stack), rl);
767 if (__path_is_null())
768 continue;
769 __split_stmt(tmp);
770 if (__path_is_null()) {
771 __set_default();
772 goto out;
774 } END_FOR_EACH_PTR(tmp);
775 out:
776 __call_scope_hooks();
777 if (!__pop_default())
778 __merge_switches(top_expression(switch_expr_stack), NULL);
779 __discard_switches();
780 __merge_breaks();
781 pop_expression(&switch_expr_stack);
784 static void split_case(struct statement *stmt)
786 struct range_list *rl = NULL;
788 expr_set_parent_stmt(stmt->case_expression, stmt);
789 expr_set_parent_stmt(stmt->case_to, stmt);
791 rl = get_case_rl(top_expression(switch_expr_stack),
792 stmt->case_expression, stmt->case_to);
793 while (stmt->case_statement->type == STMT_CASE) {
794 struct range_list *tmp;
796 tmp = get_case_rl(top_expression(switch_expr_stack),
797 stmt->case_statement->case_expression,
798 stmt->case_statement->case_to);
799 if (!tmp)
800 break;
801 rl = rl_union(rl, tmp);
802 if (!stmt->case_expression)
803 __set_default();
804 stmt = stmt->case_statement;
807 __merge_switches(top_expression(switch_expr_stack), rl);
809 if (!stmt->case_expression)
810 __set_default();
811 __split_stmt(stmt->case_statement);
814 int time_parsing_function(void)
816 return ms_since(&fn_start_time) / 1000;
819 static int taking_too_long(void)
821 if (time_parsing_function() > 60 * 5) /* five minutes */
822 return 1;
823 return 0;
826 static int is_last_stmt(struct statement *cur_stmt)
828 struct symbol *fn = get_base_type(cur_func_sym);
829 struct statement *stmt;
831 if (!fn)
832 return 0;
833 stmt = fn->stmt;
834 if (!stmt)
835 stmt = fn->inline_stmt;
836 if (!stmt || stmt->type != STMT_COMPOUND)
837 return 0;
838 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
839 if (stmt && stmt->type == STMT_LABEL)
840 stmt = stmt->label_statement;
841 if (stmt == cur_stmt)
842 return 1;
843 return 0;
846 static void handle_backward_goto(struct statement *goto_stmt)
848 const char *goto_name, *label_name;
849 struct statement *func_stmt;
850 struct symbol *base_type = get_base_type(cur_func_sym);
851 struct statement *tmp;
852 int found = 0;
854 if (!option_info)
855 return;
856 if (last_goto_statement_handled)
857 return;
858 last_goto_statement_handled = 1;
860 if (!goto_stmt->goto_label ||
861 goto_stmt->goto_label->type != SYM_LABEL ||
862 !goto_stmt->goto_label->ident)
863 return;
864 goto_name = goto_stmt->goto_label->ident->name;
866 func_stmt = base_type->stmt;
867 if (!func_stmt)
868 func_stmt = base_type->inline_stmt;
869 if (!func_stmt)
870 return;
871 if (func_stmt->type != STMT_COMPOUND)
872 return;
874 FOR_EACH_PTR(func_stmt->stmts, tmp) {
875 if (!found) {
876 if (tmp->type != STMT_LABEL)
877 continue;
878 if (!tmp->label_identifier ||
879 tmp->label_identifier->type != SYM_LABEL ||
880 !tmp->label_identifier->ident)
881 continue;
882 label_name = tmp->label_identifier->ident->name;
883 if (strcmp(goto_name, label_name) != 0)
884 continue;
885 found = 1;
887 __split_stmt(tmp);
888 } END_FOR_EACH_PTR(tmp);
891 static void fake_a_return(void)
893 struct symbol *return_type;
895 nullify_path();
896 __unnullify_path();
898 return_type = get_real_base_type(cur_func_sym);
899 return_type = get_real_base_type(return_type);
900 if (return_type != &void_ctype) {
901 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
902 nullify_path();
906 static void fake_an_empty_default(struct position pos)
908 static struct statement none = {};
910 none.pos = pos;
911 none.type = STMT_NONE;
912 __merge_switches(top_expression(switch_expr_stack), NULL);
913 __split_stmt(&none);
916 static void split_compound(struct statement *stmt)
918 struct statement *prev = NULL;
919 struct statement *cur = NULL;
920 struct statement *next;
922 __push_scope_hooks();
924 FOR_EACH_PTR(stmt->stmts, next) {
925 /* just set them all ahead of time */
926 stmt_set_parent_stmt(next, stmt);
928 if (cur) {
929 __prev_stmt = prev;
930 __next_stmt = next;
931 __cur_stmt = cur;
932 __split_stmt(cur);
934 prev = cur;
935 cur = next;
936 } END_FOR_EACH_PTR(next);
937 if (cur) {
938 __prev_stmt = prev;
939 __cur_stmt = cur;
940 __next_stmt = NULL;
941 __split_stmt(cur);
945 * For function scope, then delay calling the scope hooks until the
946 * end of function hooks can run. I'm not positive this is the right
947 * thing...
949 if (!is_last_stmt(cur))
950 __call_scope_hooks();
954 * This is a hack, work around for detecting empty functions.
956 static int need_delayed_scope_hooks(void)
958 struct symbol *fn = get_base_type(cur_func_sym);
959 struct statement *stmt;
961 if (!fn)
962 return 0;
963 stmt = fn->stmt;
964 if (!stmt)
965 stmt = fn->inline_stmt;
966 if (stmt && stmt->type == STMT_COMPOUND)
967 return 1;
968 return 0;
971 void __split_label_stmt(struct statement *stmt)
973 if (stmt->label_identifier &&
974 stmt->label_identifier->type == SYM_LABEL &&
975 stmt->label_identifier->ident) {
976 loop_count |= 0x0800000;
977 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
981 static void find_asm_gotos(struct statement *stmt)
983 struct symbol *sym;
985 FOR_EACH_PTR(stmt->asm_labels, sym) {
986 __save_gotos(sym->ident->name, sym);
987 } END_FOR_EACH_PTR(sym);
990 void __split_stmt(struct statement *stmt)
992 sval_t sval;
994 if (!stmt)
995 goto out;
997 if (!__in_fake_assign)
998 __silence_warnings_for_stmt = false;
1000 if (__bail_on_rest_of_function)
1001 return;
1003 if (out_of_memory() || taking_too_long()) {
1004 struct timeval stop;
1006 gettimeofday(&stop, NULL);
1008 __bail_on_rest_of_function = 1;
1009 final_pass = 1;
1010 sm_msg("Function too hairy. Giving up. %lu seconds",
1011 stop.tv_sec - fn_start_time.tv_sec);
1012 fake_a_return();
1013 final_pass = 0; /* turn off sm_msg() from here */
1014 return;
1017 add_ptr_list(&big_statement_stack, stmt);
1018 free_expression_stack(&big_expression_stack);
1019 set_position(stmt->pos);
1020 __pass_to_client(stmt, STMT_HOOK);
1022 switch (stmt->type) {
1023 case STMT_DECLARATION:
1024 split_declaration(stmt->declaration);
1025 break;
1026 case STMT_RETURN:
1027 expr_set_parent_stmt(stmt->ret_value, stmt);
1029 __split_expr(stmt->ret_value);
1030 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1031 __process_post_op_stack();
1032 nullify_path();
1033 break;
1034 case STMT_EXPRESSION:
1035 expr_set_parent_stmt(stmt->expression, stmt);
1036 expr_set_parent_stmt(stmt->context, stmt);
1038 __split_expr(stmt->expression);
1039 break;
1040 case STMT_COMPOUND:
1041 split_compound(stmt);
1042 break;
1043 case STMT_IF:
1044 stmt_set_parent_stmt(stmt->if_true, stmt);
1045 stmt_set_parent_stmt(stmt->if_false, stmt);
1046 expr_set_parent_stmt(stmt->if_conditional, stmt);
1048 if (known_condition_true(stmt->if_conditional)) {
1049 __split_stmt(stmt->if_true);
1050 break;
1052 if (known_condition_false(stmt->if_conditional)) {
1053 __split_stmt(stmt->if_false);
1054 break;
1056 __split_whole_condition(stmt->if_conditional);
1057 __split_stmt(stmt->if_true);
1058 if (empty_statement(stmt->if_true) &&
1059 last_stmt_on_same_line() &&
1060 !get_macro_name(stmt->if_true->pos))
1061 sm_msg("warn: if();");
1062 __push_true_states();
1063 __use_false_states();
1064 __split_stmt(stmt->if_false);
1065 __merge_true_states();
1066 break;
1067 case STMT_ITERATOR:
1068 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1069 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1070 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1071 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1072 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1074 if (stmt->iterator_pre_condition)
1075 handle_pre_loop(stmt);
1076 else if (stmt->iterator_post_condition)
1077 handle_post_loop(stmt);
1078 else {
1079 // these are for(;;) type loops.
1080 handle_pre_loop(stmt);
1082 break;
1083 case STMT_SWITCH:
1084 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1085 expr_set_parent_stmt(stmt->switch_expression, stmt);
1087 if (get_value(stmt->switch_expression, &sval)) {
1088 split_known_switch(stmt, sval);
1089 break;
1091 __split_expr(stmt->switch_expression);
1092 push_expression(&switch_expr_stack, stmt->switch_expression);
1093 __save_switch_states(top_expression(switch_expr_stack));
1094 nullify_path();
1095 __push_default();
1096 __push_breaks();
1097 __split_stmt(stmt->switch_statement);
1098 if (!__pop_default() && have_remaining_cases())
1099 fake_an_empty_default(stmt->pos);
1100 __discard_switches();
1101 __merge_breaks();
1102 pop_expression(&switch_expr_stack);
1103 break;
1104 case STMT_CASE:
1105 split_case(stmt);
1106 break;
1107 case STMT_LABEL:
1108 __split_label_stmt(stmt);
1109 __split_stmt(stmt->label_statement);
1110 break;
1111 case STMT_GOTO:
1112 expr_set_parent_stmt(stmt->goto_expression, stmt);
1114 __split_expr(stmt->goto_expression);
1115 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1116 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1117 __process_breaks();
1118 } else if (!strcmp(stmt->goto_label->ident->name,
1119 "continue")) {
1120 __process_continues();
1122 } else if (stmt->goto_label &&
1123 stmt->goto_label->type == SYM_LABEL &&
1124 stmt->goto_label->ident) {
1125 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1127 nullify_path();
1128 if (is_last_stmt(stmt))
1129 handle_backward_goto(stmt);
1130 break;
1131 case STMT_NONE:
1132 break;
1133 case STMT_ASM:
1134 expr_set_parent_stmt(stmt->asm_string, stmt);
1136 find_asm_gotos(stmt);
1137 __pass_to_client(stmt, ASM_HOOK);
1138 __split_expr(stmt->asm_string);
1139 split_asm_constraints(stmt->asm_outputs);
1140 split_asm_constraints(stmt->asm_inputs);
1141 split_asm_constraints(stmt->asm_clobbers);
1142 break;
1143 case STMT_CONTEXT:
1144 break;
1145 case STMT_RANGE:
1146 __split_expr(stmt->range_expression);
1147 __split_expr(stmt->range_low);
1148 __split_expr(stmt->range_high);
1149 break;
1151 __pass_to_client(stmt, STMT_HOOK_AFTER);
1152 out:
1153 __process_post_op_stack();
1156 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1158 struct expression *expr;
1160 FOR_EACH_PTR(expr_list, expr) {
1161 expr_set_parent_expr(expr, parent);
1162 __split_expr(expr);
1163 __process_post_op_stack();
1164 } END_FOR_EACH_PTR(expr);
1167 static void split_sym(struct symbol *sym)
1169 if (!sym)
1170 return;
1171 if (!(sym->namespace & NS_SYMBOL))
1172 return;
1174 __split_stmt(sym->stmt);
1175 __split_expr(sym->array_size);
1176 split_symlist(sym->arguments);
1177 split_symlist(sym->symbol_list);
1178 __split_stmt(sym->inline_stmt);
1179 split_symlist(sym->inline_symbol_list);
1182 static void split_symlist(struct symbol_list *sym_list)
1184 struct symbol *sym;
1186 FOR_EACH_PTR(sym_list, sym) {
1187 split_sym(sym);
1188 } END_FOR_EACH_PTR(sym);
1191 typedef void (fake_cb)(struct expression *expr);
1193 static int member_to_number(struct expression *expr, struct ident *member)
1195 struct symbol *type, *tmp;
1196 char *name;
1197 int i;
1199 if (!member)
1200 return -1;
1201 name = member->name;
1203 type = get_type(expr);
1204 if (!type || type->type != SYM_STRUCT)
1205 return -1;
1207 i = -1;
1208 FOR_EACH_PTR(type->symbol_list, tmp) {
1209 i++;
1210 if (!tmp->ident)
1211 continue;
1212 if (strcmp(name, tmp->ident->name) == 0)
1213 return i;
1214 } END_FOR_EACH_PTR(tmp);
1215 return -1;
1218 static struct ident *number_to_member(struct expression *expr, int num)
1220 struct symbol *type, *member;
1221 int i = 0;
1223 type = get_type(expr);
1224 if (!type || type->type != SYM_STRUCT)
1225 return NULL;
1227 FOR_EACH_PTR(type->symbol_list, member) {
1228 if (i == num)
1229 return member->ident;
1230 i++;
1231 } END_FOR_EACH_PTR(member);
1232 return NULL;
1235 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1237 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1239 struct expression *edge_member, *assign;
1240 struct symbol *base = get_real_base_type(member);
1241 struct symbol *tmp;
1243 if (member->ident)
1244 expr = member_expression(expr, '.', member->ident);
1246 FOR_EACH_PTR(base->symbol_list, tmp) {
1247 struct symbol *type;
1249 type = get_real_base_type(tmp);
1250 if (!type)
1251 continue;
1253 edge_member = member_expression(expr, '.', tmp->ident);
1254 if (get_extra_state(edge_member))
1255 continue;
1257 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1258 set_inner_struct_members(expr, tmp);
1259 continue;
1262 if (!tmp->ident)
1263 continue;
1265 assign = assign_expression(edge_member, '=', zero_expr());
1266 __split_expr(assign);
1267 } END_FOR_EACH_PTR(tmp);
1272 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1274 struct symbol *tmp;
1275 struct expression *member = NULL;
1276 struct expression *assign;
1277 int op = '*';
1279 if (expr->type == EXPR_PREOP && expr->op == '&') {
1280 expr = strip_expr(expr->unop);
1281 op = '.';
1284 FOR_EACH_PTR(type->symbol_list, tmp) {
1285 type = get_real_base_type(tmp);
1286 if (!type)
1287 continue;
1289 if (tmp->ident) {
1290 member = member_expression(expr, op, tmp->ident);
1291 if (get_extra_state(member))
1292 continue;
1295 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1296 set_inner_struct_members(expr, tmp);
1297 continue;
1299 if (type->type == SYM_ARRAY)
1300 continue;
1301 if (!tmp->ident)
1302 continue;
1304 assign = assign_expression(member, '=', zero_expr());
1305 __split_expr(assign);
1306 } END_FOR_EACH_PTR(tmp);
1309 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1311 struct expression *deref, *assign, *tmp, *right;
1312 struct symbol *struct_type, *type;
1313 struct ident *member;
1314 int member_idx;
1316 struct_type = get_type(symbol);
1317 if (!struct_type ||
1318 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1319 return;
1322 * We're parsing an initializer that could look something like this:
1323 * struct foo foo = {
1324 * 42,
1325 * .whatever.xxx = 11,
1326 * .zzz = 12,
1327 * };
1329 * So what we have here is a list with 42, .whatever, and .zzz. We need
1330 * to break it up into left and right sides of the assignments.
1333 member_idx = 0;
1334 FOR_EACH_PTR(members, tmp) {
1335 deref = NULL;
1336 if (tmp->type == EXPR_IDENTIFIER) {
1337 member_idx = member_to_number(symbol, tmp->expr_ident);
1338 while (tmp->type == EXPR_IDENTIFIER) {
1339 member = tmp->expr_ident;
1340 tmp = tmp->ident_expression;
1341 if (deref)
1342 deref = member_expression(deref, '.', member);
1343 else
1344 deref = member_expression(symbol, '.', member);
1346 } else {
1347 member = number_to_member(symbol, member_idx);
1348 deref = member_expression(symbol, '.', member);
1350 right = tmp;
1351 member_idx++;
1352 if (right->type == EXPR_INITIALIZER) {
1353 type = get_type(deref);
1354 if (type && type->type == SYM_ARRAY)
1355 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1356 else
1357 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1358 } else {
1359 assign = assign_expression(deref, '=', right);
1360 fake_cb(assign);
1362 } END_FOR_EACH_PTR(tmp);
1364 set_unset_to_zero(struct_type, symbol);
1367 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1369 fake_member_assigns_helper(symbol_expression(sym),
1370 sym->initializer->expr_list, fake_cb);
1373 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1375 struct expression *offset, *binop, *assign, *tmp;
1376 struct symbol *type;
1377 int idx;
1379 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1380 return;
1382 idx = 0;
1383 FOR_EACH_PTR(expr_list, tmp) {
1384 if (tmp->type == EXPR_INDEX) {
1385 if (tmp->idx_from != tmp->idx_to)
1386 return;
1387 idx = tmp->idx_from;
1388 if (!tmp->idx_expression)
1389 goto next;
1390 tmp = tmp->idx_expression;
1392 offset = value_expr(idx);
1393 binop = array_element_expression(array, offset);
1394 if (tmp->type == EXPR_INITIALIZER) {
1395 type = get_type(binop);
1396 if (type && type->type == SYM_ARRAY)
1397 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1398 else
1399 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1400 } else {
1401 assign = assign_expression(binop, '=', tmp);
1402 fake_cb(assign);
1404 next:
1405 idx++;
1406 } END_FOR_EACH_PTR(tmp);
1409 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1411 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1414 static void fake_assign_expr(struct symbol *sym)
1416 struct expression *assign, *symbol;
1418 symbol = symbol_expression(sym);
1419 assign = assign_expression(symbol, '=', sym->initializer);
1420 __split_expr(assign);
1423 static void do_initializer_stuff(struct symbol *sym)
1425 if (!sym->initializer)
1426 return;
1428 if (sym->initializer->type == EXPR_INITIALIZER) {
1429 if (get_real_base_type(sym)->type == SYM_ARRAY)
1430 fake_element_assigns(sym, __split_expr);
1431 else
1432 fake_member_assigns(sym, __split_expr);
1433 } else {
1434 fake_assign_expr(sym);
1438 static void split_declaration(struct symbol_list *sym_list)
1440 struct symbol *sym;
1442 FOR_EACH_PTR(sym_list, sym) {
1443 __pass_to_client(sym, DECLARATION_HOOK);
1444 do_initializer_stuff(sym);
1445 split_sym(sym);
1446 } END_FOR_EACH_PTR(sym);
1449 static void call_global_assign_hooks(struct expression *assign)
1451 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1454 static void fake_global_assign(struct symbol *sym)
1456 struct expression *assign, *symbol;
1458 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1459 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1460 fake_element_assigns(sym, call_global_assign_hooks);
1461 } else if (sym->initializer) {
1462 symbol = symbol_expression(sym);
1463 assign = assign_expression(symbol, '=', sym->initializer);
1464 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1465 } else {
1466 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1468 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1469 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1470 fake_member_assigns(sym, call_global_assign_hooks);
1471 } else if (sym->initializer) {
1472 symbol = symbol_expression(sym);
1473 assign = assign_expression(symbol, '=', sym->initializer);
1474 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1475 } else {
1476 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1478 } else {
1479 symbol = symbol_expression(sym);
1480 if (sym->initializer)
1481 assign = assign_expression(symbol, '=', sym->initializer);
1482 else
1483 assign = assign_expression(symbol, '=', zero_expr());
1484 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1488 static void start_function_definition(struct symbol *sym)
1490 __in_function_def = 1;
1491 __pass_to_client(sym, FUNC_DEF_HOOK);
1492 __in_function_def = 0;
1493 __pass_to_client(sym, AFTER_DEF_HOOK);
1497 static void split_function(struct symbol *sym)
1499 struct symbol *base_type = get_base_type(sym);
1501 if (!base_type->stmt && !base_type->inline_stmt)
1502 return;
1504 gettimeofday(&fn_start_time, NULL);
1505 cur_func_sym = sym;
1506 if (sym->ident)
1507 cur_func = sym->ident->name;
1508 set_position(sym->pos);
1509 loop_count = 0;
1510 last_goto_statement_handled = 0;
1511 sm_debug("new function: %s\n", cur_func);
1512 __stree_id = 0;
1513 if (option_two_passes) {
1514 __unnullify_path();
1515 loop_num = 0;
1516 final_pass = 0;
1517 start_function_definition(sym);
1518 __split_stmt(base_type->stmt);
1519 __split_stmt(base_type->inline_stmt);
1520 nullify_path();
1522 __unnullify_path();
1523 loop_num = 0;
1524 final_pass = 1;
1525 start_function_definition(sym);
1526 __split_stmt(base_type->stmt);
1527 __split_stmt(base_type->inline_stmt);
1528 __pass_to_client(sym, END_FUNC_HOOK);
1529 if (need_delayed_scope_hooks())
1530 __call_scope_hooks();
1531 __pass_to_client(sym, AFTER_FUNC_HOOK);
1533 clear_all_states();
1534 cur_func_sym = NULL;
1535 cur_func = NULL;
1536 free_data_info_allocs();
1537 free_expression_stack(&switch_expr_stack);
1538 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1539 __bail_on_rest_of_function = 0;
1542 static void save_flow_state(void)
1544 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1545 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1546 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1548 __add_ptr_list(&backup, big_statement_stack, 0);
1549 __add_ptr_list(&backup, big_expression_stack, 0);
1550 __add_ptr_list(&backup, big_condition_stack, 0);
1551 __add_ptr_list(&backup, switch_expr_stack, 0);
1553 __add_ptr_list(&backup, cur_func_sym, 0);
1555 __add_ptr_list(&backup, __prev_stmt, 0);
1556 __add_ptr_list(&backup, __cur_stmt, 0);
1557 __add_ptr_list(&backup, __next_stmt, 0);
1561 static void *pop_backup(void)
1563 void *ret;
1565 ret = last_ptr_list(backup);
1566 delete_ptr_list_last(&backup);
1567 return ret;
1570 static void restore_flow_state(void)
1572 __next_stmt = pop_backup();
1573 __cur_stmt = pop_backup();
1574 __prev_stmt = pop_backup();
1576 cur_func_sym = pop_backup();
1577 switch_expr_stack = pop_backup();
1578 big_condition_stack = pop_backup();
1579 big_expression_stack = pop_backup();
1580 big_statement_stack = pop_backup();
1581 final_pass = PTR_INT(pop_backup()) >> 2;
1582 loop_count = PTR_INT(pop_backup()) >> 2;
1583 loop_num = PTR_INT(pop_backup()) >> 2;
1586 static void parse_inline(struct expression *call)
1588 struct symbol *base_type;
1589 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1590 struct timeval time_backup = fn_start_time;
1591 struct expression *orig_inline = __inline_fn;
1592 int orig_budget;
1594 save_flow_state();
1596 __pass_to_client(call, INLINE_FN_START);
1597 final_pass = 0; /* don't print anything */
1598 __inline_fn = call;
1599 orig_budget = inline_budget;
1600 inline_budget = inline_budget - 5;
1602 base_type = get_base_type(call->fn->symbol);
1603 cur_func_sym = call->fn->symbol;
1604 if (call->fn->symbol->ident)
1605 cur_func = call->fn->symbol->ident->name;
1606 else
1607 cur_func = NULL;
1608 set_position(call->fn->symbol->pos);
1610 save_all_states();
1611 big_statement_stack = NULL;
1612 big_expression_stack = NULL;
1613 big_condition_stack = NULL;
1614 switch_expr_stack = NULL;
1616 sm_debug("inline function: %s\n", cur_func);
1617 __unnullify_path();
1618 loop_num = 0;
1619 loop_count = 0;
1620 start_function_definition(call->fn->symbol);
1621 __split_stmt(base_type->stmt);
1622 __split_stmt(base_type->inline_stmt);
1623 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1624 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1626 free_expression_stack(&switch_expr_stack);
1627 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1628 nullify_path();
1629 free_goto_stack();
1631 restore_flow_state();
1632 fn_start_time = time_backup;
1633 cur_func = cur_func_bak;
1635 restore_all_states();
1636 set_position(call->pos);
1637 __inline_fn = orig_inline;
1638 inline_budget = orig_budget;
1639 __pass_to_client(call, INLINE_FN_END);
1642 static struct symbol_list *inlines_called;
1643 static void add_inline_function(struct symbol *sym)
1645 static struct symbol_list *already_added;
1646 struct symbol *tmp;
1648 FOR_EACH_PTR(already_added, tmp) {
1649 if (tmp == sym)
1650 return;
1651 } END_FOR_EACH_PTR(tmp);
1653 add_ptr_list(&already_added, sym);
1654 add_ptr_list(&inlines_called, sym);
1657 static void process_inlines(void)
1659 struct symbol *tmp;
1661 FOR_EACH_PTR(inlines_called, tmp) {
1662 split_function(tmp);
1663 } END_FOR_EACH_PTR(tmp);
1664 free_ptr_list(&inlines_called);
1667 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1669 struct symbol *sym;
1671 FOR_EACH_PTR_REVERSE(big_list, sym) {
1672 if (!sym->scope)
1673 continue;
1674 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1675 return sym;
1676 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1677 return sym;
1678 } END_FOR_EACH_PTR_REVERSE(sym);
1680 return NULL;
1683 static bool interesting_function(struct symbol *sym)
1685 static int prev_stream = -1;
1686 static bool prev_answer;
1687 const char *filename;
1688 int len;
1690 if (!(sym->ctype.modifiers & MOD_INLINE))
1691 return true;
1693 if (sym->pos.stream == prev_stream)
1694 return prev_answer;
1696 prev_stream = sym->pos.stream;
1697 prev_answer = false;
1699 filename = stream_name(sym->pos.stream);
1700 len = strlen(filename);
1701 if (len > 0 && filename[len - 1] == 'c')
1702 prev_answer = true;
1703 return prev_answer;
1706 static void split_inlines_in_scope(struct symbol *sym)
1708 struct symbol *base;
1709 struct symbol_list *scope_list;
1710 int stream;
1712 scope_list = sym->scope->symbols;
1713 stream = sym->pos.stream;
1715 /* find the last static symbol in the file */
1716 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1717 if (sym->pos.stream != stream)
1718 continue;
1719 if (sym->type != SYM_NODE)
1720 continue;
1721 base = get_base_type(sym);
1722 if (!base)
1723 continue;
1724 if (base->type != SYM_FN)
1725 continue;
1726 if (!base->inline_stmt)
1727 continue;
1728 if (!interesting_function(sym))
1729 continue;
1730 add_inline_function(sym);
1731 } END_FOR_EACH_PTR_REVERSE(sym);
1733 process_inlines();
1736 static void split_inlines(struct symbol_list *sym_list)
1738 struct symbol *sym;
1740 sym = get_last_scoped_symbol(sym_list, 0);
1741 if (sym)
1742 split_inlines_in_scope(sym);
1743 sym = get_last_scoped_symbol(sym_list, 1);
1744 if (sym)
1745 split_inlines_in_scope(sym);
1748 static struct stree *clone_estates_perm(struct stree *orig)
1750 struct stree *ret = NULL;
1751 struct sm_state *tmp;
1753 FOR_EACH_SM(orig, tmp) {
1754 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1755 } END_FOR_EACH_SM(tmp);
1757 return ret;
1760 struct position last_pos;
1761 static void split_c_file_functions(struct symbol_list *sym_list)
1763 struct symbol *sym;
1765 __unnullify_path();
1766 FOR_EACH_PTR(sym_list, sym) {
1767 set_position(sym->pos);
1768 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1769 __pass_to_client(sym, BASE_HOOK);
1770 fake_global_assign(sym);
1772 } END_FOR_EACH_PTR(sym);
1773 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1774 nullify_path();
1776 FOR_EACH_PTR(sym_list, sym) {
1777 set_position(sym->pos);
1778 last_pos = sym->pos;
1779 if (!interesting_function(sym))
1780 continue;
1781 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1782 split_function(sym);
1783 process_inlines();
1785 last_pos = sym->pos;
1786 } END_FOR_EACH_PTR(sym);
1787 split_inlines(sym_list);
1788 __pass_to_client(sym_list, END_FILE_HOOK);
1791 static int final_before_fake;
1792 void init_fake_env(void)
1794 if (!in_fake_env)
1795 final_before_fake = final_pass;
1796 in_fake_env++;
1797 __push_fake_cur_stree();
1798 final_pass = 0;
1801 void end_fake_env(void)
1803 __pop_fake_cur_stree();
1804 in_fake_env--;
1805 if (!in_fake_env)
1806 final_pass = final_before_fake;
1809 static void open_output_files(char *base_file)
1811 char buf[256];
1813 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1814 sm_outfd = fopen(buf, "w");
1815 if (!sm_outfd) {
1816 printf("Error: Cannot open %s\n", buf);
1817 exit(1);
1820 if (!option_info)
1821 return;
1823 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1824 sql_outfd = fopen(buf, "w");
1825 if (!sql_outfd) {
1826 printf("Error: Cannot open %s\n", buf);
1827 exit(1);
1830 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1831 caller_info_fd = fopen(buf, "w");
1832 if (!caller_info_fd) {
1833 printf("Error: Cannot open %s\n", buf);
1834 exit(1);
1838 void smatch(int argc, char **argv)
1840 struct string_list *filelist = NULL;
1841 struct symbol_list *sym_list;
1842 struct timeval stop, start;
1844 gettimeofday(&start, NULL);
1846 if (argc < 2) {
1847 printf("Usage: smatch [--debug] <filename.c>\n");
1848 exit(1);
1850 sparse_initialize(argc, argv, &filelist);
1851 set_valid_ptr_max();
1852 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1853 if (option_file_output)
1854 open_output_files(base_file);
1855 sym_list = sparse_keep_tokens(base_file);
1856 split_c_file_functions(sym_list);
1857 } END_FOR_EACH_PTR_NOTAG(base_file);
1859 gettimeofday(&stop, NULL);
1861 set_position(last_pos);
1862 if (option_time)
1863 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1864 if (option_mem)
1865 sm_msg("mem: %luKb", get_max_memory());