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
;
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},
87 struct range_list
*valid_ptr_rl
;
89 static void set_valid_ptr_max(void)
91 if (type_bits(&ptr_ctype
) == 32)
92 valid_ptr_max
= 2117777777;
93 else if (type_bits(&ptr_ctype
) == 64)
94 valid_ptr_max
= 2117777777777777777LL;
96 valid_ptr_max_sval
.value
= valid_ptr_max
;
99 static void alloc_valid_ptr_rl(void)
101 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
102 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
103 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
106 int outside_of_function(void)
108 return cur_func_sym
== NULL
;
111 const char *get_filename(void)
115 if (option_full_path
)
116 return full_filename
;
120 const char *get_base_file(void)
125 static void set_position(struct position pos
)
128 static int prev_stream
= -1;
133 if (pos
.stream
== 0 && pos
.line
== 0)
136 __smatch_lineno
= pos
.line
;
138 if (pos
.stream
== prev_stream
)
141 filename
= stream_name(pos
.stream
);
144 pathname
= getcwd(NULL
, 0);
146 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
147 full_filename
= malloc(len
);
148 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
150 full_filename
= alloc_string(filename
);
155 int is_assigned_call(struct expression
*expr
)
157 struct expression
*parent
= expr_get_parent_expr(expr
);
160 parent
->type
== EXPR_ASSIGNMENT
&&
162 strip_expr(parent
->right
) == expr
)
168 static int is_inline_func(struct expression
*expr
)
170 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
172 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
177 static int is_noreturn_func(struct expression
*expr
)
179 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
181 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
186 static int inline_budget
= 20;
188 int inlinable(struct expression
*expr
)
191 struct statement
*last_stmt
= NULL
;
193 if (__inline_fn
) /* don't nest */
196 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
198 if (is_no_inline_function(expr
->symbol
->ident
->name
))
200 sym
= get_base_type(expr
->symbol
);
201 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
202 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
204 if (sym
->stmt
->type
!= STMT_COMPOUND
)
206 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
208 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
209 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
211 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
213 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
219 /* the magic numbers in this function are pulled out of my bum. */
220 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
226 void __process_post_op_stack(void)
228 struct expression
*expr
;
230 FOR_EACH_PTR(post_op_stack
, expr
) {
231 __pass_to_client(expr
, OP_HOOK
);
232 } END_FOR_EACH_PTR(expr
);
234 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
237 static int handle_comma_assigns(struct expression
*expr
)
239 struct expression
*right
;
240 struct expression
*assign
;
242 right
= strip_expr(expr
->right
);
243 if (right
->type
!= EXPR_COMMA
)
246 __split_expr(right
->left
);
247 __process_post_op_stack();
249 assign
= assign_expression(expr
->left
, '=', right
->right
);
250 __split_expr(assign
);
255 /* This is to handle *p++ = foo; assignments */
256 static int handle_postop_assigns(struct expression
*expr
)
258 struct expression
*left
, *fake_left
;
259 struct expression
*assign
;
261 left
= strip_expr(expr
->left
);
262 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
264 left
= strip_expr(left
->unop
);
265 if (left
->type
!= EXPR_POSTOP
)
268 fake_left
= deref_expression(strip_expr(left
->unop
));
269 assign
= assign_expression(fake_left
, '=', expr
->right
);
271 __split_expr(assign
);
272 __split_expr(expr
->left
);
277 static int prev_expression_is_getting_address(struct expression
*expr
)
279 struct expression
*parent
;
282 parent
= expr_get_parent_expr(expr
);
286 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
288 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
290 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
299 static int handle__builtin_choose_expr(struct expression
*expr
)
301 struct expression
*const_expr
, *expr1
, *expr2
;
304 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
307 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
308 expr1
= get_argument_from_call_expr(expr
->args
, 1);
309 expr2
= get_argument_from_call_expr(expr
->args
, 2);
311 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
320 void __split_expr(struct expression
*expr
)
325 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
327 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
329 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
332 push_expression(&big_expression_stack
, expr
);
333 set_position(expr
->pos
);
334 __pass_to_client(expr
, EXPR_HOOK
);
336 switch (expr
->type
) {
338 expr_set_parent_expr(expr
->unop
, expr
);
340 if (expr
->op
== '*' &&
341 !prev_expression_is_getting_address(expr
))
342 __pass_to_client(expr
, DEREF_HOOK
);
343 __split_expr(expr
->unop
);
344 __pass_to_client(expr
, OP_HOOK
);
347 expr_set_parent_expr(expr
->unop
, expr
);
349 __split_expr(expr
->unop
);
350 push_expression(&post_op_stack
, expr
);
354 if (expr
->statement
&& !expr
->statement
) {
355 stmt_set_parent_stmt(expr
->statement
,
356 last_ptr_list((struct ptr_list
*)big_statement_stack
));
358 __split_stmt(expr
->statement
);
363 expr_set_parent_expr(expr
->left
, expr
);
364 expr_set_parent_expr(expr
->right
, expr
);
366 __pass_to_client(expr
, LOGIC_HOOK
);
367 __handle_logic(expr
);
370 expr_set_parent_expr(expr
->left
, expr
);
371 expr_set_parent_expr(expr
->right
, expr
);
373 __pass_to_client(expr
, BINOP_HOOK
);
375 expr_set_parent_expr(expr
->left
, expr
);
376 expr_set_parent_expr(expr
->right
, expr
);
378 __split_expr(expr
->left
);
379 __process_post_op_stack();
380 __split_expr(expr
->right
);
382 case EXPR_ASSIGNMENT
: {
383 struct expression
*right
;
385 expr_set_parent_expr(expr
->left
, expr
);
386 expr_set_parent_expr(expr
->right
, expr
);
388 right
= strip_expr(expr
->right
);
392 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
395 if (__handle_condition_assigns(expr
))
397 /* foo = (x < 5 ? foo : 5); */
398 if (__handle_select_assigns(expr
))
400 /* foo = ({frob(); frob(); frob(); 1;}) */
401 if (__handle_expr_statement_assigns(expr
))
404 if (handle_comma_assigns(expr
))
406 if (handle_postop_assigns(expr
))
409 __split_expr(expr
->right
);
410 if (outside_of_function())
411 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
413 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
415 __fake_struct_member_assignments(expr
);
417 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
418 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
420 if (get_macro_name(right
->pos
) &&
421 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
422 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
424 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
426 __split_expr(expr
->left
);
430 expr_set_parent_expr(expr
->deref
, expr
);
432 __pass_to_client(expr
, DEREF_HOOK
);
433 __split_expr(expr
->deref
);
436 expr_set_parent_expr(expr
->base
, expr
);
438 __split_expr(expr
->base
);
441 case EXPR_FORCE_CAST
:
442 expr_set_parent_expr(expr
->cast_expression
, expr
);
444 __pass_to_client(expr
, CAST_HOOK
);
445 __split_expr(expr
->cast_expression
);
448 if (expr
->cast_expression
)
449 __pass_to_client(strip_parens(expr
->cast_expression
),
454 evaluate_expression(expr
);
456 case EXPR_CONDITIONAL
:
458 expr_set_parent_expr(expr
->conditional
, expr
);
459 expr_set_parent_expr(expr
->cond_true
, expr
);
460 expr_set_parent_expr(expr
->cond_false
, expr
);
462 if (known_condition_true(expr
->conditional
)) {
463 __split_expr(expr
->cond_true
);
466 if (known_condition_false(expr
->conditional
)) {
467 __split_expr(expr
->cond_false
);
470 __pass_to_client(expr
, SELECT_HOOK
);
471 __split_whole_condition(expr
->conditional
);
472 __split_expr(expr
->cond_true
);
473 __push_true_states();
474 __use_false_states();
475 __split_expr(expr
->cond_false
);
476 __merge_true_states();
479 expr_set_parent_expr(expr
->fn
, expr
);
481 if (sym_name_is("__builtin_constant_p", expr
->fn
))
483 if (handle__builtin_choose_expr(expr
))
485 split_expr_list(expr
->args
, expr
);
486 __split_expr(expr
->fn
);
487 if (is_inline_func(expr
->fn
))
488 add_inline_function(expr
->fn
->symbol
);
489 if (inlinable(expr
->fn
))
491 __process_post_op_stack();
492 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
493 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
495 if (inlinable(expr
->fn
)) {
498 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
499 if (is_noreturn_func(expr
->fn
))
502 case EXPR_INITIALIZER
:
503 split_expr_list(expr
->expr_list
, expr
);
505 case EXPR_IDENTIFIER
:
506 expr_set_parent_expr(expr
->ident_expression
, expr
);
507 __split_expr(expr
->ident_expression
);
510 expr_set_parent_expr(expr
->idx_expression
, expr
);
511 __split_expr(expr
->idx_expression
);
514 expr_set_parent_expr(expr
->init_expr
, expr
);
515 __split_expr(expr
->init_expr
);
518 __pass_to_client(expr
, SYM_HOOK
);
521 __pass_to_client(expr
, STRING_HOOK
);
526 pop_expression(&big_expression_stack
);
529 static int is_forever_loop(struct statement
*stmt
)
531 struct expression
*expr
;
534 expr
= strip_expr(stmt
->iterator_pre_condition
);
536 expr
= stmt
->iterator_post_condition
;
538 /* this is a for(;;) loop... */
542 if (get_value(expr
, &sval
) && sval
.value
!= 0)
549 static char *get_loop_name(int num
)
553 snprintf(buf
, 255, "-loop%d", num
);
555 return alloc_sname(buf
);
559 * Pre Loops are while and for loops.
561 static void handle_pre_loop(struct statement
*stmt
)
563 int once_through
; /* we go through the loop at least once */
564 struct sm_state
*extra_sm
= NULL
;
567 struct stree
*stree
= NULL
;
568 struct sm_state
*sm
= NULL
;
570 loop_name
= get_loop_name(loop_num
);
573 __split_stmt(stmt
->iterator_pre_statement
);
574 __prev_stmt
= stmt
->iterator_pre_statement
;
576 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
582 __merge_gotos(loop_name
, NULL
);
584 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
585 __in_pre_condition
++;
586 __pass_to_client(stmt
, PRELOOP_HOOK
);
587 __split_whole_condition(stmt
->iterator_pre_condition
);
588 __in_pre_condition
--;
589 FOR_EACH_SM(stree
, sm
) {
590 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
591 } END_FOR_EACH_SM(sm
);
594 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
596 if (option_assume_loops
)
599 __split_stmt(stmt
->iterator_statement
);
600 if (is_forever_loop(stmt
)) {
602 __save_gotos(loop_name
, NULL
);
604 __push_fake_cur_stree();
605 __split_stmt(stmt
->iterator_post_statement
);
606 stree
= __pop_fake_cur_stree();
608 __discard_false_states();
611 if (!__path_is_null())
612 __merge_stree_into_cur(stree
);
616 unchanged
= __iterator_unchanged(extra_sm
);
617 __split_stmt(stmt
->iterator_post_statement
);
618 __prev_stmt
= stmt
->iterator_post_statement
;
621 __save_gotos(loop_name
, NULL
);
622 __in_pre_condition
++;
623 __split_whole_condition(stmt
->iterator_pre_condition
);
624 __in_pre_condition
--;
626 __merge_false_states();
628 __discard_false_states();
630 __merge_false_states();
632 if (extra_sm
&& unchanged
)
633 __extra_pre_loop_hook_after(extra_sm
,
634 stmt
->iterator_post_statement
,
635 stmt
->iterator_pre_condition
);
642 * Post loops are do {} while();
644 static void handle_post_loop(struct statement
*stmt
)
648 loop_name
= get_loop_name(loop_num
);
654 __merge_gotos(loop_name
, NULL
);
655 __split_stmt(stmt
->iterator_statement
);
657 if (!is_zero(stmt
->iterator_post_condition
))
658 __save_gotos(loop_name
, NULL
);
660 if (is_forever_loop(stmt
)) {
663 __split_whole_condition(stmt
->iterator_post_condition
);
664 __use_false_states();
670 static int empty_statement(struct statement
*stmt
)
674 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
679 static int last_stmt_on_same_line(void)
681 struct statement
*stmt
;
684 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
687 if (stmt
->pos
.line
== get_lineno())
690 } END_FOR_EACH_PTR_REVERSE(stmt
);
694 static void split_asm_constraints(struct expression_list
*expr_list
)
696 struct expression
*expr
;
699 FOR_EACH_PTR(expr_list
, expr
) {
701 case 0: /* identifier */
702 case 1: /* constraint */
705 case 2: /* expression */
710 } END_FOR_EACH_PTR(expr
);
713 static int is_case_val(struct statement
*stmt
, sval_t sval
)
717 if (stmt
->type
!= STMT_CASE
)
719 if (!stmt
->case_expression
) {
723 if (!get_value(stmt
->case_expression
, &case_sval
))
725 if (case_sval
.value
== sval
.value
)
730 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
731 struct expression
*case_expr
,
732 struct expression
*case_to
)
735 struct range_list
*rl
= NULL
;
736 struct symbol
*switch_type
;
738 switch_type
= get_type(switch_expr
);
739 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
740 start
= sval_cast(switch_type
, start
);
741 end
= sval_cast(switch_type
, end
);
742 add_range(&rl
, start
, end
);
743 } else if (get_value(case_expr
, &start
)) {
744 start
= sval_cast(switch_type
, start
);
745 add_range(&rl
, start
, start
);
751 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
753 struct statement
*tmp
;
754 struct range_list
*rl
;
756 __split_expr(stmt
->switch_expression
);
757 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
759 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
760 __save_switch_states(top_expression(switch_expr_stack
));
765 stmt
= stmt
->switch_statement
;
767 __push_scope_hooks();
768 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
769 __smatch_lineno
= tmp
->pos
.line
;
770 if (is_case_val(tmp
, sval
)) {
771 rl
= alloc_rl(sval
, sval
);
772 __merge_switches(top_expression(switch_expr_stack
), rl
);
773 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
775 if (__path_is_null())
778 if (__path_is_null()) {
782 } END_FOR_EACH_PTR(tmp
);
784 __call_scope_hooks();
785 if (!__pop_default())
786 __merge_switches(top_expression(switch_expr_stack
), NULL
);
787 __discard_switches();
789 pop_expression(&switch_expr_stack
);
792 static void split_case(struct statement
*stmt
)
794 struct range_list
*rl
= NULL
;
796 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
797 expr_set_parent_stmt(stmt
->case_to
, stmt
);
799 rl
= get_case_rl(top_expression(switch_expr_stack
),
800 stmt
->case_expression
, stmt
->case_to
);
801 while (stmt
->case_statement
->type
== STMT_CASE
) {
802 struct range_list
*tmp
;
804 tmp
= get_case_rl(top_expression(switch_expr_stack
),
805 stmt
->case_statement
->case_expression
,
806 stmt
->case_statement
->case_to
);
809 rl
= rl_union(rl
, tmp
);
810 if (!stmt
->case_expression
)
812 stmt
= stmt
->case_statement
;
815 __merge_switches(top_expression(switch_expr_stack
), rl
);
817 if (!stmt
->case_expression
)
819 __split_stmt(stmt
->case_statement
);
822 int time_parsing_function(void)
824 return ms_since(&fn_start_time
) / 1000;
827 static int taking_too_long(void)
829 if (time_parsing_function() > 60 * 5) /* five minutes */
834 static int is_last_stmt(struct statement
*cur_stmt
)
836 struct symbol
*fn
= get_base_type(cur_func_sym
);
837 struct statement
*stmt
;
843 stmt
= fn
->inline_stmt
;
844 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
846 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
847 if (stmt
&& stmt
->type
== STMT_LABEL
)
848 stmt
= stmt
->label_statement
;
849 if (stmt
== cur_stmt
)
854 static void handle_backward_goto(struct statement
*goto_stmt
)
856 const char *goto_name
, *label_name
;
857 struct statement
*func_stmt
;
858 struct symbol
*base_type
= get_base_type(cur_func_sym
);
859 struct statement
*tmp
;
864 if (last_goto_statement_handled
)
866 last_goto_statement_handled
= 1;
868 if (!goto_stmt
->goto_label
||
869 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
870 !goto_stmt
->goto_label
->ident
)
872 goto_name
= goto_stmt
->goto_label
->ident
->name
;
874 func_stmt
= base_type
->stmt
;
876 func_stmt
= base_type
->inline_stmt
;
879 if (func_stmt
->type
!= STMT_COMPOUND
)
882 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
884 if (tmp
->type
!= STMT_LABEL
)
886 if (!tmp
->label_identifier
||
887 tmp
->label_identifier
->type
!= SYM_LABEL
||
888 !tmp
->label_identifier
->ident
)
890 label_name
= tmp
->label_identifier
->ident
->name
;
891 if (strcmp(goto_name
, label_name
) != 0)
896 } END_FOR_EACH_PTR(tmp
);
899 static void fake_a_return(void)
901 struct symbol
*return_type
;
906 return_type
= get_real_base_type(cur_func_sym
);
907 return_type
= get_real_base_type(return_type
);
908 if (return_type
!= &void_ctype
) {
909 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
914 static void fake_an_empty_default(struct position pos
)
916 static struct statement none
= {};
919 none
.type
= STMT_NONE
;
920 __merge_switches(top_expression(switch_expr_stack
), NULL
);
924 static void split_compound(struct statement
*stmt
)
926 struct statement
*prev
= NULL
;
927 struct statement
*cur
= NULL
;
928 struct statement
*next
;
930 __push_scope_hooks();
932 FOR_EACH_PTR(stmt
->stmts
, next
) {
933 /* just set them all ahead of time */
934 stmt_set_parent_stmt(next
, stmt
);
944 } END_FOR_EACH_PTR(next
);
953 * For function scope, then delay calling the scope hooks until the
954 * end of function hooks can run. I'm not positive this is the right
957 if (!is_last_stmt(cur
))
958 __call_scope_hooks();
962 * This is a hack, work around for detecting empty functions.
964 static int need_delayed_scope_hooks(void)
966 struct symbol
*fn
= get_base_type(cur_func_sym
);
967 struct statement
*stmt
;
973 stmt
= fn
->inline_stmt
;
974 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
979 void __split_label_stmt(struct statement
*stmt
)
981 if (stmt
->label_identifier
&&
982 stmt
->label_identifier
->type
== SYM_LABEL
&&
983 stmt
->label_identifier
->ident
) {
984 loop_count
|= 0x0800000;
985 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
989 static void find_asm_gotos(struct statement
*stmt
)
993 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
994 __save_gotos(sym
->ident
->name
, sym
);
995 } END_FOR_EACH_PTR(sym
);
998 void __split_stmt(struct statement
*stmt
)
1005 if (!__in_fake_assign
)
1006 __silence_warnings_for_stmt
= false;
1008 if (__bail_on_rest_of_function
)
1011 if (out_of_memory() || taking_too_long()) {
1012 struct timeval stop
;
1014 gettimeofday(&stop
, NULL
);
1016 __bail_on_rest_of_function
= 1;
1018 sm_msg("Function too hairy. Giving up. %lu seconds",
1019 stop
.tv_sec
- fn_start_time
.tv_sec
);
1021 final_pass
= 0; /* turn off sm_msg() from here */
1025 add_ptr_list(&big_statement_stack
, stmt
);
1026 free_expression_stack(&big_expression_stack
);
1027 set_position(stmt
->pos
);
1028 __pass_to_client(stmt
, STMT_HOOK
);
1030 switch (stmt
->type
) {
1031 case STMT_DECLARATION
:
1032 split_declaration(stmt
->declaration
);
1035 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1037 __split_expr(stmt
->ret_value
);
1038 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1039 __process_post_op_stack();
1042 case STMT_EXPRESSION
:
1043 expr_set_parent_stmt(stmt
->expression
, stmt
);
1044 expr_set_parent_stmt(stmt
->context
, stmt
);
1046 __split_expr(stmt
->expression
);
1049 split_compound(stmt
);
1052 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1053 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1054 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1056 if (known_condition_true(stmt
->if_conditional
)) {
1057 __split_stmt(stmt
->if_true
);
1060 if (known_condition_false(stmt
->if_conditional
)) {
1061 __split_stmt(stmt
->if_false
);
1064 __split_whole_condition(stmt
->if_conditional
);
1065 __split_stmt(stmt
->if_true
);
1066 if (empty_statement(stmt
->if_true
) &&
1067 last_stmt_on_same_line() &&
1068 !get_macro_name(stmt
->if_true
->pos
))
1069 sm_msg("warn: if();");
1070 __push_true_states();
1071 __use_false_states();
1072 __split_stmt(stmt
->if_false
);
1073 __merge_true_states();
1076 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1077 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1078 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1079 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1080 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1082 if (stmt
->iterator_pre_condition
)
1083 handle_pre_loop(stmt
);
1084 else if (stmt
->iterator_post_condition
)
1085 handle_post_loop(stmt
);
1087 // these are for(;;) type loops.
1088 handle_pre_loop(stmt
);
1092 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1093 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1095 if (get_value(stmt
->switch_expression
, &sval
)) {
1096 split_known_switch(stmt
, sval
);
1099 __split_expr(stmt
->switch_expression
);
1100 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1101 __save_switch_states(top_expression(switch_expr_stack
));
1105 __split_stmt(stmt
->switch_statement
);
1106 if (!__pop_default() && have_remaining_cases())
1107 fake_an_empty_default(stmt
->pos
);
1108 __discard_switches();
1110 pop_expression(&switch_expr_stack
);
1116 __split_label_stmt(stmt
);
1117 __split_stmt(stmt
->label_statement
);
1120 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1122 __split_expr(stmt
->goto_expression
);
1123 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1124 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1126 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1128 __process_continues();
1130 } else if (stmt
->goto_label
&&
1131 stmt
->goto_label
->type
== SYM_LABEL
&&
1132 stmt
->goto_label
->ident
) {
1133 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1136 if (is_last_stmt(stmt
))
1137 handle_backward_goto(stmt
);
1142 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1144 find_asm_gotos(stmt
);
1145 __pass_to_client(stmt
, ASM_HOOK
);
1146 __split_expr(stmt
->asm_string
);
1147 split_asm_constraints(stmt
->asm_outputs
);
1148 split_asm_constraints(stmt
->asm_inputs
);
1149 split_asm_constraints(stmt
->asm_clobbers
);
1154 __split_expr(stmt
->range_expression
);
1155 __split_expr(stmt
->range_low
);
1156 __split_expr(stmt
->range_high
);
1159 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1161 __process_post_op_stack();
1164 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1166 struct expression
*expr
;
1168 FOR_EACH_PTR(expr_list
, expr
) {
1169 expr_set_parent_expr(expr
, parent
);
1171 __process_post_op_stack();
1172 } END_FOR_EACH_PTR(expr
);
1175 static void split_sym(struct symbol
*sym
)
1179 if (!(sym
->namespace & NS_SYMBOL
))
1182 __split_stmt(sym
->stmt
);
1183 __split_expr(sym
->array_size
);
1184 split_symlist(sym
->arguments
);
1185 split_symlist(sym
->symbol_list
);
1186 __split_stmt(sym
->inline_stmt
);
1187 split_symlist(sym
->inline_symbol_list
);
1190 static void split_symlist(struct symbol_list
*sym_list
)
1194 FOR_EACH_PTR(sym_list
, sym
) {
1196 } END_FOR_EACH_PTR(sym
);
1199 typedef void (fake_cb
)(struct expression
*expr
);
1201 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1203 struct symbol
*type
, *tmp
;
1209 name
= member
->name
;
1211 type
= get_type(expr
);
1212 if (!type
|| type
->type
!= SYM_STRUCT
)
1216 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1220 if (strcmp(name
, tmp
->ident
->name
) == 0)
1222 } END_FOR_EACH_PTR(tmp
);
1226 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1228 struct symbol
*type
, *member
;
1231 type
= get_type(expr
);
1232 if (!type
|| type
->type
!= SYM_STRUCT
)
1235 FOR_EACH_PTR(type
->symbol_list
, member
) {
1237 return member
->ident
;
1239 } END_FOR_EACH_PTR(member
);
1243 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1245 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1247 struct expression
*edge_member
, *assign
;
1248 struct symbol
*base
= get_real_base_type(member
);
1252 expr
= member_expression(expr
, '.', member
->ident
);
1254 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1255 struct symbol
*type
;
1257 type
= get_real_base_type(tmp
);
1261 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1262 if (get_extra_state(edge_member
))
1265 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1266 set_inner_struct_members(expr
, tmp
);
1273 assign
= assign_expression(edge_member
, '=', zero_expr());
1274 __split_expr(assign
);
1275 } END_FOR_EACH_PTR(tmp
);
1280 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1283 struct expression
*member
= NULL
;
1284 struct expression
*assign
;
1287 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1288 expr
= strip_expr(expr
->unop
);
1292 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1293 type
= get_real_base_type(tmp
);
1298 member
= member_expression(expr
, op
, tmp
->ident
);
1299 if (get_extra_state(member
))
1303 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1304 set_inner_struct_members(expr
, tmp
);
1307 if (type
->type
== SYM_ARRAY
)
1312 assign
= assign_expression(member
, '=', zero_expr());
1313 __split_expr(assign
);
1314 } END_FOR_EACH_PTR(tmp
);
1317 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1319 struct expression
*deref
, *assign
, *tmp
, *right
;
1320 struct symbol
*struct_type
, *type
;
1321 struct ident
*member
;
1324 struct_type
= get_type(symbol
);
1326 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1330 * We're parsing an initializer that could look something like this:
1331 * struct foo foo = {
1333 * .whatever.xxx = 11,
1337 * So what we have here is a list with 42, .whatever, and .zzz. We need
1338 * to break it up into left and right sides of the assignments.
1342 FOR_EACH_PTR(members
, tmp
) {
1344 if (tmp
->type
== EXPR_IDENTIFIER
) {
1345 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1346 while (tmp
->type
== EXPR_IDENTIFIER
) {
1347 member
= tmp
->expr_ident
;
1348 tmp
= tmp
->ident_expression
;
1350 deref
= member_expression(deref
, '.', member
);
1352 deref
= member_expression(symbol
, '.', member
);
1355 member
= number_to_member(symbol
, member_idx
);
1356 deref
= member_expression(symbol
, '.', member
);
1360 if (right
->type
== EXPR_INITIALIZER
) {
1361 type
= get_type(deref
);
1362 if (type
&& type
->type
== SYM_ARRAY
)
1363 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1365 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1367 assign
= assign_expression(deref
, '=', right
);
1370 } END_FOR_EACH_PTR(tmp
);
1372 set_unset_to_zero(struct_type
, symbol
);
1375 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1377 fake_member_assigns_helper(symbol_expression(sym
),
1378 sym
->initializer
->expr_list
, fake_cb
);
1381 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1383 struct expression
*offset
, *binop
, *assign
, *tmp
;
1384 struct symbol
*type
;
1387 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1391 FOR_EACH_PTR(expr_list
, tmp
) {
1392 if (tmp
->type
== EXPR_INDEX
) {
1393 if (tmp
->idx_from
!= tmp
->idx_to
)
1395 idx
= tmp
->idx_from
;
1396 if (!tmp
->idx_expression
)
1398 tmp
= tmp
->idx_expression
;
1400 offset
= value_expr(idx
);
1401 binop
= array_element_expression(array
, offset
);
1402 if (tmp
->type
== EXPR_INITIALIZER
) {
1403 type
= get_type(binop
);
1404 if (type
&& type
->type
== SYM_ARRAY
)
1405 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1407 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1409 assign
= assign_expression(binop
, '=', tmp
);
1414 } END_FOR_EACH_PTR(tmp
);
1417 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1419 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1422 static void fake_assign_expr(struct symbol
*sym
)
1424 struct expression
*assign
, *symbol
;
1426 symbol
= symbol_expression(sym
);
1427 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1428 __split_expr(assign
);
1431 static void do_initializer_stuff(struct symbol
*sym
)
1433 if (!sym
->initializer
)
1436 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1437 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1438 fake_element_assigns(sym
, __split_expr
);
1440 fake_member_assigns(sym
, __split_expr
);
1442 fake_assign_expr(sym
);
1446 static void split_declaration(struct symbol_list
*sym_list
)
1450 FOR_EACH_PTR(sym_list
, sym
) {
1451 __pass_to_client(sym
, DECLARATION_HOOK
);
1452 do_initializer_stuff(sym
);
1454 } END_FOR_EACH_PTR(sym
);
1457 static void call_global_assign_hooks(struct expression
*assign
)
1459 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1462 static void fake_global_assign(struct symbol
*sym
)
1464 struct expression
*assign
, *symbol
;
1466 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1467 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1468 fake_element_assigns(sym
, call_global_assign_hooks
);
1469 } else if (sym
->initializer
) {
1470 symbol
= symbol_expression(sym
);
1471 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1472 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1474 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1476 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1477 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1478 fake_member_assigns(sym
, call_global_assign_hooks
);
1479 } else if (sym
->initializer
) {
1480 symbol
= symbol_expression(sym
);
1481 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1482 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1484 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1487 symbol
= symbol_expression(sym
);
1488 if (sym
->initializer
)
1489 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1491 assign
= assign_expression(symbol
, '=', zero_expr());
1492 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1496 static void start_function_definition(struct symbol
*sym
)
1498 __in_function_def
= 1;
1499 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1500 __in_function_def
= 0;
1501 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1505 static void split_function(struct symbol
*sym
)
1507 struct symbol
*base_type
= get_base_type(sym
);
1508 struct timeval stop
;
1510 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1513 gettimeofday(&fn_start_time
, NULL
);
1516 cur_func
= sym
->ident
->name
;
1517 set_position(sym
->pos
);
1519 last_goto_statement_handled
= 0;
1520 sm_debug("new function: %s\n", cur_func
);
1522 if (option_two_passes
) {
1526 start_function_definition(sym
);
1527 __split_stmt(base_type
->stmt
);
1528 __split_stmt(base_type
->inline_stmt
);
1534 start_function_definition(sym
);
1535 __split_stmt(base_type
->stmt
);
1536 __split_stmt(base_type
->inline_stmt
);
1537 __pass_to_client(sym
, END_FUNC_HOOK
);
1538 if (need_delayed_scope_hooks())
1539 __call_scope_hooks();
1540 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1544 gettimeofday(&stop
, NULL
);
1545 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1547 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1550 cur_func_sym
= NULL
;
1552 free_data_info_allocs();
1553 free_expression_stack(&switch_expr_stack
);
1554 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1555 __bail_on_rest_of_function
= 0;
1558 static void save_flow_state(void)
1560 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1561 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1562 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1564 __add_ptr_list(&backup
, big_statement_stack
, 0);
1565 __add_ptr_list(&backup
, big_expression_stack
, 0);
1566 __add_ptr_list(&backup
, big_condition_stack
, 0);
1567 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1569 __add_ptr_list(&backup
, cur_func_sym
, 0);
1571 __add_ptr_list(&backup
, __prev_stmt
, 0);
1572 __add_ptr_list(&backup
, __cur_stmt
, 0);
1573 __add_ptr_list(&backup
, __next_stmt
, 0);
1577 static void *pop_backup(void)
1581 ret
= last_ptr_list(backup
);
1582 delete_ptr_list_last(&backup
);
1586 static void restore_flow_state(void)
1588 __next_stmt
= pop_backup();
1589 __cur_stmt
= pop_backup();
1590 __prev_stmt
= pop_backup();
1592 cur_func_sym
= pop_backup();
1593 switch_expr_stack
= pop_backup();
1594 big_condition_stack
= pop_backup();
1595 big_expression_stack
= pop_backup();
1596 big_statement_stack
= pop_backup();
1597 final_pass
= PTR_INT(pop_backup()) >> 2;
1598 loop_count
= PTR_INT(pop_backup()) >> 2;
1599 loop_num
= PTR_INT(pop_backup()) >> 2;
1602 static void parse_inline(struct expression
*call
)
1604 struct symbol
*base_type
;
1605 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1606 struct timeval time_backup
= fn_start_time
;
1607 struct expression
*orig_inline
= __inline_fn
;
1612 __pass_to_client(call
, INLINE_FN_START
);
1613 final_pass
= 0; /* don't print anything */
1615 orig_budget
= inline_budget
;
1616 inline_budget
= inline_budget
- 5;
1618 base_type
= get_base_type(call
->fn
->symbol
);
1619 cur_func_sym
= call
->fn
->symbol
;
1620 if (call
->fn
->symbol
->ident
)
1621 cur_func
= call
->fn
->symbol
->ident
->name
;
1624 set_position(call
->fn
->symbol
->pos
);
1627 big_statement_stack
= NULL
;
1628 big_expression_stack
= NULL
;
1629 big_condition_stack
= NULL
;
1630 switch_expr_stack
= NULL
;
1632 sm_debug("inline function: %s\n", cur_func
);
1636 start_function_definition(call
->fn
->symbol
);
1637 __split_stmt(base_type
->stmt
);
1638 __split_stmt(base_type
->inline_stmt
);
1639 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1640 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1642 free_expression_stack(&switch_expr_stack
);
1643 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1647 restore_flow_state();
1648 fn_start_time
= time_backup
;
1649 cur_func
= cur_func_bak
;
1651 restore_all_states();
1652 set_position(call
->pos
);
1653 __inline_fn
= orig_inline
;
1654 inline_budget
= orig_budget
;
1655 __pass_to_client(call
, INLINE_FN_END
);
1658 static struct symbol_list
*inlines_called
;
1659 static void add_inline_function(struct symbol
*sym
)
1661 static struct symbol_list
*already_added
;
1664 FOR_EACH_PTR(already_added
, tmp
) {
1667 } END_FOR_EACH_PTR(tmp
);
1669 add_ptr_list(&already_added
, sym
);
1670 add_ptr_list(&inlines_called
, sym
);
1673 static void process_inlines(void)
1677 FOR_EACH_PTR(inlines_called
, tmp
) {
1678 split_function(tmp
);
1679 } END_FOR_EACH_PTR(tmp
);
1680 free_ptr_list(&inlines_called
);
1683 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1687 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1690 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1692 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1694 } END_FOR_EACH_PTR_REVERSE(sym
);
1699 static bool interesting_function(struct symbol
*sym
)
1701 static int prev_stream
= -1;
1702 static bool prev_answer
;
1703 const char *filename
;
1706 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1709 if (sym
->pos
.stream
== prev_stream
)
1712 prev_stream
= sym
->pos
.stream
;
1713 prev_answer
= false;
1715 filename
= stream_name(sym
->pos
.stream
);
1716 len
= strlen(filename
);
1717 if (len
> 0 && filename
[len
- 1] == 'c')
1722 static void split_inlines_in_scope(struct symbol
*sym
)
1724 struct symbol
*base
;
1725 struct symbol_list
*scope_list
;
1728 scope_list
= sym
->scope
->symbols
;
1729 stream
= sym
->pos
.stream
;
1731 /* find the last static symbol in the file */
1732 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1733 if (sym
->pos
.stream
!= stream
)
1735 if (sym
->type
!= SYM_NODE
)
1737 base
= get_base_type(sym
);
1740 if (base
->type
!= SYM_FN
)
1742 if (!base
->inline_stmt
)
1744 if (!interesting_function(sym
))
1746 add_inline_function(sym
);
1747 } END_FOR_EACH_PTR_REVERSE(sym
);
1752 static void split_inlines(struct symbol_list
*sym_list
)
1756 sym
= get_last_scoped_symbol(sym_list
, 0);
1758 split_inlines_in_scope(sym
);
1759 sym
= get_last_scoped_symbol(sym_list
, 1);
1761 split_inlines_in_scope(sym
);
1764 static struct stree
*clone_estates_perm(struct stree
*orig
)
1766 struct stree
*ret
= NULL
;
1767 struct sm_state
*tmp
;
1769 FOR_EACH_SM(orig
, tmp
) {
1770 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1771 } END_FOR_EACH_SM(tmp
);
1776 struct position last_pos
;
1777 static void split_c_file_functions(struct symbol_list
*sym_list
)
1782 FOR_EACH_PTR(sym_list
, sym
) {
1783 set_position(sym
->pos
);
1784 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1785 __pass_to_client(sym
, BASE_HOOK
);
1786 fake_global_assign(sym
);
1788 } END_FOR_EACH_PTR(sym
);
1789 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1792 FOR_EACH_PTR(sym_list
, sym
) {
1793 set_position(sym
->pos
);
1794 last_pos
= sym
->pos
;
1795 if (!interesting_function(sym
))
1797 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1798 split_function(sym
);
1801 last_pos
= sym
->pos
;
1802 } END_FOR_EACH_PTR(sym
);
1803 split_inlines(sym_list
);
1804 __pass_to_client(sym_list
, END_FILE_HOOK
);
1807 static int final_before_fake
;
1808 void init_fake_env(void)
1811 final_before_fake
= final_pass
;
1813 __push_fake_cur_stree();
1817 void end_fake_env(void)
1819 __pop_fake_cur_stree();
1822 final_pass
= final_before_fake
;
1825 static void open_output_files(char *base_file
)
1829 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1830 sm_outfd
= fopen(buf
, "w");
1832 printf("Error: Cannot open %s\n", buf
);
1839 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1840 sql_outfd
= fopen(buf
, "w");
1842 printf("Error: Cannot open %s\n", buf
);
1846 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1847 caller_info_fd
= fopen(buf
, "w");
1848 if (!caller_info_fd
) {
1849 printf("Error: Cannot open %s\n", buf
);
1854 void smatch(int argc
, char **argv
)
1856 struct string_list
*filelist
= NULL
;
1857 struct symbol_list
*sym_list
;
1858 struct timeval stop
, start
;
1860 gettimeofday(&start
, NULL
);
1863 printf("Usage: smatch [--debug] <filename.c>\n");
1866 sparse_initialize(argc
, argv
, &filelist
);
1867 set_valid_ptr_max();
1868 alloc_valid_ptr_rl();
1869 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1870 if (option_file_output
)
1871 open_output_files(base_file
);
1872 sym_list
= sparse_keep_tokens(base_file
);
1873 split_c_file_functions(sym_list
);
1874 } END_FOR_EACH_PTR_NOTAG(base_file
);
1876 gettimeofday(&stop
, NULL
);
1878 set_position(last_pos
);
1880 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1882 sm_msg("mem: %luKb", get_max_memory());