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
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
29 int __in_fake_struct_assign
;
30 static int in_fake_env
;
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
= {
83 sval_t valid_ptr_max_sval
= {
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)
107 if (option_full_path
)
108 return full_filename
;
112 const char *get_base_file(void)
117 static void set_position(struct position pos
)
120 static int prev_stream
= -1;
125 if (pos
.stream
== 0 && pos
.line
== 0)
128 __smatch_lineno
= pos
.line
;
130 if (pos
.stream
== prev_stream
)
133 filename
= stream_name(pos
.stream
);
136 pathname
= getcwd(NULL
, 0);
138 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
139 full_filename
= malloc(len
);
140 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
142 full_filename
= alloc_string(filename
);
147 int is_assigned_call(struct expression
*expr
)
149 struct expression
*parent
= expr_get_parent_expr(expr
);
152 parent
->type
== EXPR_ASSIGNMENT
&&
154 strip_expr(parent
->right
) == expr
)
160 static int is_inline_func(struct expression
*expr
)
162 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
164 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
169 static int is_noreturn_func(struct expression
*expr
)
171 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
173 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
178 static int inline_budget
= 20;
180 int inlinable(struct expression
*expr
)
183 struct statement
*last_stmt
= NULL
;
185 if (__inline_fn
) /* don't nest */
188 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
190 if (is_no_inline_function(expr
->symbol
->ident
->name
))
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)
196 if (sym
->stmt
->type
!= STMT_COMPOUND
)
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)
203 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
205 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
211 /* the magic numbers in this function are pulled out of my bum. */
212 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
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
)
238 __split_expr(right
->left
);
239 __process_post_op_stack();
241 assign
= assign_expression(expr
->left
, '=', right
->right
);
242 __split_expr(assign
);
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
!= '*')
256 left
= strip_expr(left
->unop
);
257 if (left
->type
!= EXPR_POSTOP
)
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
);
269 static int prev_expression_is_getting_address(struct expression
*expr
)
271 struct expression
*parent
;
274 parent
= expr_get_parent_expr(expr
);
278 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
280 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
282 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
291 static int handle__builtin_choose_expr(struct expression
*expr
)
293 struct expression
*const_expr
, *expr1
, *expr2
;
296 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
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
)
312 void __split_expr(struct expression
*expr
)
317 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
319 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
321 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
324 push_expression(&big_expression_stack
, expr
);
325 set_position(expr
->pos
);
326 __pass_to_client(expr
, EXPR_HOOK
);
328 switch (expr
->type
) {
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
);
339 expr_set_parent_expr(expr
->unop
, expr
);
341 __split_expr(expr
->unop
);
342 push_expression(&post_op_stack
, expr
);
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
);
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
);
362 expr_set_parent_expr(expr
->left
, expr
);
363 expr_set_parent_expr(expr
->right
, expr
);
365 __pass_to_client(expr
, BINOP_HOOK
);
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
);
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
);
384 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
387 if (__handle_condition_assigns(expr
))
389 /* foo = (x < 5 ? foo : 5); */
390 if (__handle_select_assigns(expr
))
392 /* foo = ({frob(); frob(); frob(); 1;}) */
393 if (__handle_expr_statement_assigns(expr
))
396 if (handle_comma_assigns(expr
))
398 if (handle_postop_assigns(expr
))
401 __split_expr(expr
->right
);
402 if (outside_of_function())
403 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
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
);
422 expr_set_parent_expr(expr
->deref
, expr
);
424 __pass_to_client(expr
, DEREF_HOOK
);
425 __split_expr(expr
->deref
);
428 expr_set_parent_expr(expr
->base
, expr
);
430 __split_expr(expr
->base
);
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
);
440 if (expr
->cast_expression
)
441 __pass_to_client(strip_parens(expr
->cast_expression
),
446 evaluate_expression(expr
);
448 case EXPR_CONDITIONAL
:
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
);
458 if (known_condition_false(expr
->conditional
)) {
459 __split_expr(expr
->cond_false
);
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();
471 expr_set_parent_expr(expr
->fn
, expr
);
473 if (sym_name_is("__builtin_constant_p", expr
->fn
))
475 if (handle__builtin_choose_expr(expr
))
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
))
483 __process_post_op_stack();
484 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
485 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
487 if (inlinable(expr
->fn
)) {
490 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
491 if (is_noreturn_func(expr
->fn
))
494 case EXPR_INITIALIZER
:
495 split_expr_list(expr
->expr_list
, expr
);
497 case EXPR_IDENTIFIER
:
498 expr_set_parent_expr(expr
->ident_expression
, expr
);
499 __split_expr(expr
->ident_expression
);
502 expr_set_parent_expr(expr
->idx_expression
, expr
);
503 __split_expr(expr
->idx_expression
);
506 expr_set_parent_expr(expr
->init_expr
, expr
);
507 __split_expr(expr
->init_expr
);
510 __pass_to_client(expr
, SYM_HOOK
);
513 __pass_to_client(expr
, STRING_HOOK
);
518 pop_expression(&big_expression_stack
);
521 static int is_forever_loop(struct statement
*stmt
)
523 struct expression
*expr
;
526 expr
= strip_expr(stmt
->iterator_pre_condition
);
528 expr
= stmt
->iterator_post_condition
;
530 /* this is a for(;;) loop... */
534 if (get_value(expr
, &sval
) && sval
.value
!= 0)
541 static char *get_loop_name(int num
)
545 snprintf(buf
, 255, "-loop%d", num
);
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
;
559 struct stree
*stree
= NULL
;
560 struct sm_state
*sm
= NULL
;
562 loop_name
= get_loop_name(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
);
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
);
586 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
588 if (option_assume_loops
)
591 __split_stmt(stmt
->iterator_statement
);
592 if (is_forever_loop(stmt
)) {
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();
603 if (!__path_is_null())
604 __merge_stree_into_cur(stree
);
608 unchanged
= __iterator_unchanged(extra_sm
);
609 __split_stmt(stmt
->iterator_post_statement
);
610 __prev_stmt
= stmt
->iterator_post_statement
;
613 __save_gotos(loop_name
, NULL
);
614 __in_pre_condition
++;
615 __split_whole_condition(stmt
->iterator_pre_condition
);
616 __in_pre_condition
--;
618 __merge_false_states();
620 __discard_false_states();
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
);
634 * Post loops are do {} while();
636 static void handle_post_loop(struct statement
*stmt
)
640 loop_name
= get_loop_name(loop_num
);
646 __merge_gotos(loop_name
, NULL
);
647 __split_stmt(stmt
->iterator_statement
);
649 if (!is_zero(stmt
->iterator_post_condition
))
650 __save_gotos(loop_name
, NULL
);
652 if (is_forever_loop(stmt
)) {
655 __split_whole_condition(stmt
->iterator_post_condition
);
656 __use_false_states();
662 static int empty_statement(struct statement
*stmt
)
666 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
671 static int last_stmt_on_same_line(void)
673 struct statement
*stmt
;
676 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
679 if (stmt
->pos
.line
== get_lineno())
682 } END_FOR_EACH_PTR_REVERSE(stmt
);
686 static void split_asm_constraints(struct expression_list
*expr_list
)
688 struct expression
*expr
;
691 FOR_EACH_PTR(expr_list
, expr
) {
693 case 0: /* identifier */
694 case 1: /* constraint */
697 case 2: /* expression */
702 } END_FOR_EACH_PTR(expr
);
705 static int is_case_val(struct statement
*stmt
, sval_t sval
)
709 if (stmt
->type
!= STMT_CASE
)
711 if (!stmt
->case_expression
) {
715 if (!get_value(stmt
->case_expression
, &case_sval
))
717 if (case_sval
.value
== sval
.value
)
722 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
723 struct expression
*case_expr
,
724 struct expression
*case_to
)
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
);
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
));
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())
770 if (__path_is_null()) {
774 } END_FOR_EACH_PTR(tmp
);
776 __call_scope_hooks();
777 if (!__pop_default())
778 __merge_switches(top_expression(switch_expr_stack
), NULL
);
779 __discard_switches();
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
);
801 rl
= rl_union(rl
, tmp
);
802 if (!stmt
->case_expression
)
804 stmt
= stmt
->case_statement
;
807 __merge_switches(top_expression(switch_expr_stack
), rl
);
809 if (!stmt
->case_expression
)
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 */
826 static int is_last_stmt(struct statement
*cur_stmt
)
828 struct symbol
*fn
= get_base_type(cur_func_sym
);
829 struct statement
*stmt
;
835 stmt
= fn
->inline_stmt
;
836 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
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
)
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
;
856 if (last_goto_statement_handled
)
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
)
864 goto_name
= goto_stmt
->goto_label
->ident
->name
;
866 func_stmt
= base_type
->stmt
;
868 func_stmt
= base_type
->inline_stmt
;
871 if (func_stmt
->type
!= STMT_COMPOUND
)
874 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
876 if (tmp
->type
!= STMT_LABEL
)
878 if (!tmp
->label_identifier
||
879 tmp
->label_identifier
->type
!= SYM_LABEL
||
880 !tmp
->label_identifier
->ident
)
882 label_name
= tmp
->label_identifier
->ident
->name
;
883 if (strcmp(goto_name
, label_name
) != 0)
888 } END_FOR_EACH_PTR(tmp
);
891 static void fake_a_return(void)
893 struct symbol
*return_type
;
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
);
906 static void fake_an_empty_default(struct position pos
)
908 static struct statement none
= {};
911 none
.type
= STMT_NONE
;
912 __merge_switches(top_expression(switch_expr_stack
), NULL
);
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
);
936 } END_FOR_EACH_PTR(next
);
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
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
;
965 stmt
= fn
->inline_stmt
;
966 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
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
)
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
)
997 if (!__in_fake_assign
)
998 __silence_warnings_for_stmt
= false;
1000 if (__bail_on_rest_of_function
)
1003 if (out_of_memory() || taking_too_long()) {
1004 struct timeval stop
;
1006 gettimeofday(&stop
, NULL
);
1008 __bail_on_rest_of_function
= 1;
1010 sm_msg("Function too hairy. Giving up. %lu seconds",
1011 stop
.tv_sec
- fn_start_time
.tv_sec
);
1013 final_pass
= 0; /* turn off sm_msg() from here */
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
);
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();
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
);
1041 split_compound(stmt
);
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
);
1052 if (known_condition_false(stmt
->if_conditional
)) {
1053 __split_stmt(stmt
->if_false
);
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();
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
);
1079 // these are for(;;) type loops.
1080 handle_pre_loop(stmt
);
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
);
1091 __split_expr(stmt
->switch_expression
);
1092 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1093 __save_switch_states(top_expression(switch_expr_stack
));
1097 __split_stmt(stmt
->switch_statement
);
1098 if (!__pop_default() && have_remaining_cases())
1099 fake_an_empty_default(stmt
->pos
);
1100 __discard_switches();
1102 pop_expression(&switch_expr_stack
);
1108 __split_label_stmt(stmt
);
1109 __split_stmt(stmt
->label_statement
);
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")) {
1118 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
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
);
1128 if (is_last_stmt(stmt
))
1129 handle_backward_goto(stmt
);
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
);
1146 __split_expr(stmt
->range_expression
);
1147 __split_expr(stmt
->range_low
);
1148 __split_expr(stmt
->range_high
);
1151 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
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
);
1163 __process_post_op_stack();
1164 } END_FOR_EACH_PTR(expr
);
1167 static void split_sym(struct symbol
*sym
)
1171 if (!(sym
->namespace & NS_SYMBOL
))
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
)
1186 FOR_EACH_PTR(sym_list
, 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
;
1201 name
= member
->name
;
1203 type
= get_type(expr
);
1204 if (!type
|| type
->type
!= SYM_STRUCT
)
1208 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1212 if (strcmp(name
, tmp
->ident
->name
) == 0)
1214 } END_FOR_EACH_PTR(tmp
);
1218 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1220 struct symbol
*type
, *member
;
1223 type
= get_type(expr
);
1224 if (!type
|| type
->type
!= SYM_STRUCT
)
1227 FOR_EACH_PTR(type
->symbol_list
, member
) {
1229 return member
->ident
;
1231 } END_FOR_EACH_PTR(member
);
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
);
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
);
1253 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1254 if (get_extra_state(edge_member
))
1257 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1258 set_inner_struct_members(expr
, tmp
);
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
)
1275 struct expression
*member
= NULL
;
1276 struct expression
*assign
;
1279 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1280 expr
= strip_expr(expr
->unop
);
1284 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1285 type
= get_real_base_type(tmp
);
1290 member
= member_expression(expr
, op
, tmp
->ident
);
1291 if (get_extra_state(member
))
1295 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1296 set_inner_struct_members(expr
, tmp
);
1299 if (type
->type
== SYM_ARRAY
)
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
;
1316 struct_type
= get_type(symbol
);
1318 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1322 * We're parsing an initializer that could look something like this:
1323 * struct foo foo = {
1325 * .whatever.xxx = 11,
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.
1334 FOR_EACH_PTR(members
, tmp
) {
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
;
1342 deref
= member_expression(deref
, '.', member
);
1344 deref
= member_expression(symbol
, '.', member
);
1347 member
= number_to_member(symbol
, member_idx
);
1348 deref
= member_expression(symbol
, '.', member
);
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
);
1357 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1359 assign
= assign_expression(deref
, '=', right
);
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
;
1379 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1383 FOR_EACH_PTR(expr_list
, tmp
) {
1384 if (tmp
->type
== EXPR_INDEX
) {
1385 if (tmp
->idx_from
!= tmp
->idx_to
)
1387 idx
= tmp
->idx_from
;
1388 if (!tmp
->idx_expression
)
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
);
1399 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1401 assign
= assign_expression(binop
, '=', tmp
);
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
)
1428 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1429 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1430 fake_element_assigns(sym
, __split_expr
);
1432 fake_member_assigns(sym
, __split_expr
);
1434 fake_assign_expr(sym
);
1438 static void split_declaration(struct symbol_list
*sym_list
)
1442 FOR_EACH_PTR(sym_list
, sym
) {
1443 __pass_to_client(sym
, DECLARATION_HOOK
);
1444 do_initializer_stuff(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
);
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
);
1476 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1479 symbol
= symbol_expression(sym
);
1480 if (sym
->initializer
)
1481 assign
= assign_expression(symbol
, '=', sym
->initializer
);
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
)
1504 gettimeofday(&fn_start_time
, NULL
);
1507 cur_func
= sym
->ident
->name
;
1508 set_position(sym
->pos
);
1510 last_goto_statement_handled
= 0;
1511 sm_debug("new function: %s\n", cur_func
);
1513 if (option_two_passes
) {
1517 start_function_definition(sym
);
1518 __split_stmt(base_type
->stmt
);
1519 __split_stmt(base_type
->inline_stmt
);
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
);
1534 cur_func_sym
= 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)
1565 ret
= last_ptr_list(backup
);
1566 delete_ptr_list_last(&backup
);
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
;
1596 __pass_to_client(call
, INLINE_FN_START
);
1597 final_pass
= 0; /* don't print anything */
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
;
1608 set_position(call
->fn
->symbol
->pos
);
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
);
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
);
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
;
1648 FOR_EACH_PTR(already_added
, tmp
) {
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)
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
)
1671 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1674 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1676 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1678 } END_FOR_EACH_PTR_REVERSE(sym
);
1683 static bool interesting_function(struct symbol
*sym
)
1685 static int prev_stream
= -1;
1686 static bool prev_answer
;
1687 const char *filename
;
1690 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1693 if (sym
->pos
.stream
== prev_stream
)
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')
1706 static void split_inlines_in_scope(struct symbol
*sym
)
1708 struct symbol
*base
;
1709 struct symbol_list
*scope_list
;
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
)
1719 if (sym
->type
!= SYM_NODE
)
1721 base
= get_base_type(sym
);
1724 if (base
->type
!= SYM_FN
)
1726 if (!base
->inline_stmt
)
1728 if (!interesting_function(sym
))
1730 add_inline_function(sym
);
1731 } END_FOR_EACH_PTR_REVERSE(sym
);
1736 static void split_inlines(struct symbol_list
*sym_list
)
1740 sym
= get_last_scoped_symbol(sym_list
, 0);
1742 split_inlines_in_scope(sym
);
1743 sym
= get_last_scoped_symbol(sym_list
, 1);
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
);
1760 struct position last_pos
;
1761 static void split_c_file_functions(struct symbol_list
*sym_list
)
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
));
1776 FOR_EACH_PTR(sym_list
, sym
) {
1777 set_position(sym
->pos
);
1778 last_pos
= sym
->pos
;
1779 if (!interesting_function(sym
))
1781 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1782 split_function(sym
);
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)
1795 final_before_fake
= final_pass
;
1797 __push_fake_cur_stree();
1801 void end_fake_env(void)
1803 __pop_fake_cur_stree();
1806 final_pass
= final_before_fake
;
1809 static void open_output_files(char *base_file
)
1813 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1814 sm_outfd
= fopen(buf
, "w");
1816 printf("Error: Cannot open %s\n", buf
);
1823 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1824 sql_outfd
= fopen(buf
, "w");
1826 printf("Error: Cannot open %s\n", buf
);
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
);
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
);
1847 printf("Usage: smatch [--debug] <filename.c>\n");
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
);
1863 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1865 sm_msg("mem: %luKb", get_max_memory());