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
)
158 * The problem is that the fake assignments we do in the initializer
159 * don't have a real assignment backing them so we don't set the
160 * ->parent pointer. This is a bit of an ugly hack, probably.
163 if (!parent
&& __cur_stmt
&& __cur_stmt
->type
== STMT_DECLARATION
)
169 static int is_inline_func(struct expression
*expr
)
171 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
173 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
178 static int is_noreturn_func(struct expression
*expr
)
180 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
182 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
187 int inlinable(struct expression
*expr
)
190 struct statement
*last_stmt
= NULL
;
192 if (__inline_fn
) /* don't nest */
195 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
197 if (is_no_inline_function(expr
->symbol
->ident
->name
))
199 sym
= get_base_type(expr
->symbol
);
200 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
201 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
203 if (sym
->stmt
->type
!= STMT_COMPOUND
)
205 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
207 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
208 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
210 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
212 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
218 /* the magic numbers in this function are pulled out of my bum. */
219 if (last_stmt
->pos
.line
> sym
->pos
.line
+ 20)
225 void __process_post_op_stack(void)
227 struct expression
*expr
;
229 FOR_EACH_PTR(post_op_stack
, expr
) {
230 __pass_to_client(expr
, OP_HOOK
);
231 } END_FOR_EACH_PTR(expr
);
233 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
236 static int handle_comma_assigns(struct expression
*expr
)
238 struct expression
*right
;
239 struct expression
*assign
;
241 right
= strip_expr(expr
->right
);
242 if (right
->type
!= EXPR_COMMA
)
245 __split_expr(right
->left
);
246 __process_post_op_stack();
248 assign
= assign_expression(expr
->left
, '=', right
->right
);
249 __split_expr(assign
);
254 /* This is to handle *p++ = foo; assignments */
255 static int handle_postop_assigns(struct expression
*expr
)
257 struct expression
*left
, *fake_left
;
258 struct expression
*assign
;
260 left
= strip_expr(expr
->left
);
261 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
263 left
= strip_expr(left
->unop
);
264 if (left
->type
!= EXPR_POSTOP
)
267 fake_left
= deref_expression(strip_expr(left
->unop
));
268 assign
= assign_expression(fake_left
, '=', expr
->right
);
270 __split_expr(assign
);
271 __split_expr(expr
->left
);
276 static int prev_expression_is_getting_address(struct expression
*expr
)
278 struct expression
*parent
;
281 parent
= expr_get_parent_expr(expr
);
285 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
287 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
289 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
298 static int handle__builtin_choose_expr(struct expression
*expr
)
300 struct expression
*const_expr
, *expr1
, *expr2
;
303 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
306 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
307 expr1
= get_argument_from_call_expr(expr
->args
, 1);
308 expr2
= get_argument_from_call_expr(expr
->args
, 2);
310 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
319 void __split_expr(struct expression
*expr
)
324 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
326 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
328 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
331 push_expression(&big_expression_stack
, expr
);
332 set_position(expr
->pos
);
333 __pass_to_client(expr
, EXPR_HOOK
);
335 switch (expr
->type
) {
337 expr_set_parent_expr(expr
->unop
, expr
);
339 if (expr
->op
== '*' &&
340 !prev_expression_is_getting_address(expr
))
341 __pass_to_client(expr
, DEREF_HOOK
);
342 __split_expr(expr
->unop
);
343 __pass_to_client(expr
, OP_HOOK
);
346 expr_set_parent_expr(expr
->unop
, expr
);
348 __split_expr(expr
->unop
);
349 push_expression(&post_op_stack
, expr
);
353 if (expr
->statement
&& !expr
->statement
) {
354 stmt_set_parent_stmt(expr
->statement
,
355 last_ptr_list((struct ptr_list
*)big_statement_stack
));
357 __split_stmt(expr
->statement
);
362 expr_set_parent_expr(expr
->left
, expr
);
363 expr_set_parent_expr(expr
->right
, expr
);
365 __pass_to_client(expr
, LOGIC_HOOK
);
366 __handle_logic(expr
);
369 expr_set_parent_expr(expr
->left
, expr
);
370 expr_set_parent_expr(expr
->right
, expr
);
372 __pass_to_client(expr
, BINOP_HOOK
);
374 expr_set_parent_expr(expr
->left
, expr
);
375 expr_set_parent_expr(expr
->right
, expr
);
377 __split_expr(expr
->left
);
378 __process_post_op_stack();
379 __split_expr(expr
->right
);
381 case EXPR_ASSIGNMENT
: {
382 struct expression
*right
;
384 expr_set_parent_expr(expr
->left
, expr
);
385 expr_set_parent_expr(expr
->right
, expr
);
387 right
= strip_expr(expr
->right
);
391 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
394 if (__handle_condition_assigns(expr
))
396 /* foo = (x < 5 ? foo : 5); */
397 if (__handle_select_assigns(expr
))
399 /* foo = ({frob(); frob(); frob(); 1;}) */
400 if (__handle_expr_statement_assigns(expr
))
403 if (handle_comma_assigns(expr
))
405 if (handle_postop_assigns(expr
))
408 __split_expr(expr
->right
);
409 if (outside_of_function())
410 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
412 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
414 __fake_struct_member_assignments(expr
);
416 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
417 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
419 if (get_macro_name(right
->pos
) &&
420 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
421 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
423 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
425 __split_expr(expr
->left
);
429 expr_set_parent_expr(expr
->deref
, expr
);
431 __pass_to_client(expr
, DEREF_HOOK
);
432 __split_expr(expr
->deref
);
435 expr_set_parent_expr(expr
->base
, expr
);
437 __split_expr(expr
->base
);
440 case EXPR_FORCE_CAST
:
441 expr_set_parent_expr(expr
->cast_expression
, expr
);
443 __pass_to_client(expr
, CAST_HOOK
);
444 __split_expr(expr
->cast_expression
);
447 if (expr
->cast_expression
)
448 __pass_to_client(strip_parens(expr
->cast_expression
),
453 evaluate_expression(expr
);
455 case EXPR_CONDITIONAL
:
457 expr_set_parent_expr(expr
->conditional
, expr
);
458 expr_set_parent_expr(expr
->cond_true
, expr
);
459 expr_set_parent_expr(expr
->cond_false
, expr
);
461 if (known_condition_true(expr
->conditional
)) {
462 __split_expr(expr
->cond_true
);
465 if (known_condition_false(expr
->conditional
)) {
466 __split_expr(expr
->cond_false
);
469 __pass_to_client(expr
, SELECT_HOOK
);
470 __split_whole_condition(expr
->conditional
);
471 __split_expr(expr
->cond_true
);
472 __push_true_states();
473 __use_false_states();
474 __split_expr(expr
->cond_false
);
475 __merge_true_states();
478 expr_set_parent_expr(expr
->fn
, expr
);
480 if (sym_name_is("__builtin_constant_p", expr
->fn
))
482 if (handle__builtin_choose_expr(expr
))
484 split_expr_list(expr
->args
, expr
);
485 __split_expr(expr
->fn
);
486 if (is_inline_func(expr
->fn
))
487 add_inline_function(expr
->fn
->symbol
);
488 if (inlinable(expr
->fn
))
490 __process_post_op_stack();
491 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
492 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
494 if (inlinable(expr
->fn
)) {
497 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
498 if (is_noreturn_func(expr
->fn
))
501 case EXPR_INITIALIZER
:
502 split_expr_list(expr
->expr_list
, expr
);
504 case EXPR_IDENTIFIER
:
505 expr_set_parent_expr(expr
->ident_expression
, expr
);
506 __split_expr(expr
->ident_expression
);
509 expr_set_parent_expr(expr
->idx_expression
, expr
);
510 __split_expr(expr
->idx_expression
);
513 expr_set_parent_expr(expr
->init_expr
, expr
);
514 __split_expr(expr
->init_expr
);
517 __pass_to_client(expr
, SYM_HOOK
);
520 __pass_to_client(expr
, STRING_HOOK
);
525 pop_expression(&big_expression_stack
);
528 static int is_forever_loop(struct statement
*stmt
)
530 struct expression
*expr
;
533 expr
= strip_expr(stmt
->iterator_pre_condition
);
535 expr
= stmt
->iterator_post_condition
;
537 /* this is a for(;;) loop... */
541 if (get_value(expr
, &sval
) && sval
.value
!= 0)
548 static char *get_loop_name(int num
)
552 snprintf(buf
, 255, "-loop%d", num
);
554 return alloc_sname(buf
);
558 * Pre Loops are while and for loops.
560 static void handle_pre_loop(struct statement
*stmt
)
562 int once_through
; /* we go through the loop at least once */
563 struct sm_state
*extra_sm
= NULL
;
566 struct stree
*stree
= NULL
;
567 struct sm_state
*sm
= NULL
;
569 loop_name
= get_loop_name(loop_num
);
572 __split_stmt(stmt
->iterator_pre_statement
);
573 __prev_stmt
= stmt
->iterator_pre_statement
;
575 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
581 __merge_gotos(loop_name
, NULL
);
583 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
584 __in_pre_condition
++;
585 __pass_to_client(stmt
, PRELOOP_HOOK
);
586 __split_whole_condition(stmt
->iterator_pre_condition
);
587 __in_pre_condition
--;
588 FOR_EACH_SM(stree
, sm
) {
589 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
590 } END_FOR_EACH_SM(sm
);
593 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
595 if (option_assume_loops
)
598 __split_stmt(stmt
->iterator_statement
);
599 if (is_forever_loop(stmt
)) {
601 __save_gotos(loop_name
, NULL
);
603 __push_fake_cur_stree();
604 __split_stmt(stmt
->iterator_post_statement
);
605 stree
= __pop_fake_cur_stree();
607 __discard_false_states();
610 if (!__path_is_null())
611 __merge_stree_into_cur(stree
);
615 unchanged
= __iterator_unchanged(extra_sm
);
616 __split_stmt(stmt
->iterator_post_statement
);
617 __prev_stmt
= stmt
->iterator_post_statement
;
620 __save_gotos(loop_name
, NULL
);
621 __in_pre_condition
++;
622 __split_whole_condition(stmt
->iterator_pre_condition
);
623 __in_pre_condition
--;
625 __merge_false_states();
627 __discard_false_states();
629 __merge_false_states();
631 if (extra_sm
&& unchanged
)
632 __extra_pre_loop_hook_after(extra_sm
,
633 stmt
->iterator_post_statement
,
634 stmt
->iterator_pre_condition
);
641 * Post loops are do {} while();
643 static void handle_post_loop(struct statement
*stmt
)
647 loop_name
= get_loop_name(loop_num
);
653 __merge_gotos(loop_name
, NULL
);
654 __split_stmt(stmt
->iterator_statement
);
656 if (!is_zero(stmt
->iterator_post_condition
))
657 __save_gotos(loop_name
, NULL
);
659 if (is_forever_loop(stmt
)) {
662 __split_whole_condition(stmt
->iterator_post_condition
);
663 __use_false_states();
669 static int empty_statement(struct statement
*stmt
)
673 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
678 static int last_stmt_on_same_line(void)
680 struct statement
*stmt
;
683 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
686 if (stmt
->pos
.line
== get_lineno())
689 } END_FOR_EACH_PTR_REVERSE(stmt
);
693 static void split_asm_constraints(struct expression_list
*expr_list
)
695 struct expression
*expr
;
698 FOR_EACH_PTR(expr_list
, expr
) {
700 case 0: /* identifier */
701 case 1: /* constraint */
704 case 2: /* expression */
709 } END_FOR_EACH_PTR(expr
);
712 static int is_case_val(struct statement
*stmt
, sval_t sval
)
716 if (stmt
->type
!= STMT_CASE
)
718 if (!stmt
->case_expression
) {
722 if (!get_value(stmt
->case_expression
, &case_sval
))
724 if (case_sval
.value
== sval
.value
)
729 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
730 struct expression
*case_expr
,
731 struct expression
*case_to
)
734 struct range_list
*rl
= NULL
;
735 struct symbol
*switch_type
;
737 switch_type
= get_type(switch_expr
);
738 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
739 start
= sval_cast(switch_type
, start
);
740 end
= sval_cast(switch_type
, end
);
741 add_range(&rl
, start
, end
);
742 } else if (get_value(case_expr
, &start
)) {
743 start
= sval_cast(switch_type
, start
);
744 add_range(&rl
, start
, start
);
750 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
752 struct statement
*tmp
;
753 struct range_list
*rl
;
755 __split_expr(stmt
->switch_expression
);
756 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
758 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
759 __save_switch_states(top_expression(switch_expr_stack
));
764 stmt
= stmt
->switch_statement
;
766 __push_scope_hooks();
767 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
768 __smatch_lineno
= tmp
->pos
.line
;
769 if (is_case_val(tmp
, sval
)) {
770 rl
= alloc_rl(sval
, sval
);
771 __merge_switches(top_expression(switch_expr_stack
), rl
);
772 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
774 if (__path_is_null())
777 if (__path_is_null()) {
781 } END_FOR_EACH_PTR(tmp
);
783 __call_scope_hooks();
784 if (!__pop_default())
785 __merge_switches(top_expression(switch_expr_stack
), NULL
);
786 __discard_switches();
788 pop_expression(&switch_expr_stack
);
791 static void split_case(struct statement
*stmt
)
793 struct range_list
*rl
= NULL
;
795 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
796 expr_set_parent_stmt(stmt
->case_to
, stmt
);
798 rl
= get_case_rl(top_expression(switch_expr_stack
),
799 stmt
->case_expression
, stmt
->case_to
);
800 while (stmt
->case_statement
->type
== STMT_CASE
) {
801 struct range_list
*tmp
;
803 tmp
= get_case_rl(top_expression(switch_expr_stack
),
804 stmt
->case_statement
->case_expression
,
805 stmt
->case_statement
->case_to
);
808 rl
= rl_union(rl
, tmp
);
809 if (!stmt
->case_expression
)
811 stmt
= stmt
->case_statement
;
814 __merge_switches(top_expression(switch_expr_stack
), rl
);
816 if (!stmt
->case_expression
)
818 __split_stmt(stmt
->case_statement
);
821 int time_parsing_function(void)
823 return ms_since(&fn_start_time
) / 1000;
826 static int taking_too_long(void)
828 if (time_parsing_function() > 60 * 5) /* five minutes */
833 static int is_last_stmt(struct statement
*cur_stmt
)
835 struct symbol
*fn
= get_base_type(cur_func_sym
);
836 struct statement
*stmt
;
842 stmt
= fn
->inline_stmt
;
843 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
845 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
846 if (stmt
&& stmt
->type
== STMT_LABEL
)
847 stmt
= stmt
->label_statement
;
848 if (stmt
== cur_stmt
)
853 static void handle_backward_goto(struct statement
*goto_stmt
)
855 const char *goto_name
, *label_name
;
856 struct statement
*func_stmt
;
857 struct symbol
*base_type
= get_base_type(cur_func_sym
);
858 struct statement
*tmp
;
863 if (last_goto_statement_handled
)
865 last_goto_statement_handled
= 1;
867 if (!goto_stmt
->goto_label
||
868 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
869 !goto_stmt
->goto_label
->ident
)
871 goto_name
= goto_stmt
->goto_label
->ident
->name
;
873 func_stmt
= base_type
->stmt
;
875 func_stmt
= base_type
->inline_stmt
;
878 if (func_stmt
->type
!= STMT_COMPOUND
)
881 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
883 if (tmp
->type
!= STMT_LABEL
)
885 if (!tmp
->label_identifier
||
886 tmp
->label_identifier
->type
!= SYM_LABEL
||
887 !tmp
->label_identifier
->ident
)
889 label_name
= tmp
->label_identifier
->ident
->name
;
890 if (strcmp(goto_name
, label_name
) != 0)
895 } END_FOR_EACH_PTR(tmp
);
898 static void fake_a_return(void)
900 struct symbol
*return_type
;
905 return_type
= get_real_base_type(cur_func_sym
);
906 return_type
= get_real_base_type(return_type
);
907 if (return_type
!= &void_ctype
) {
908 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
913 static void fake_an_empty_default(struct position pos
)
915 static struct statement none
= {};
918 none
.type
= STMT_NONE
;
919 __merge_switches(top_expression(switch_expr_stack
), NULL
);
923 static void split_compound(struct statement
*stmt
)
925 struct statement
*prev
= NULL
;
926 struct statement
*cur
= NULL
;
927 struct statement
*next
;
929 __push_scope_hooks();
931 FOR_EACH_PTR(stmt
->stmts
, next
) {
932 /* just set them all ahead of time */
933 stmt_set_parent_stmt(next
, stmt
);
943 } END_FOR_EACH_PTR(next
);
952 * For function scope, then delay calling the scope hooks until the
953 * end of function hooks can run. I'm not positive this is the right
956 if (!is_last_stmt(cur
))
957 __call_scope_hooks();
961 * This is a hack, work around for detecting empty functions.
963 static int need_delayed_scope_hooks(void)
965 struct symbol
*fn
= get_base_type(cur_func_sym
);
966 struct statement
*stmt
;
972 stmt
= fn
->inline_stmt
;
973 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
978 void __split_label_stmt(struct statement
*stmt
)
980 if (stmt
->label_identifier
&&
981 stmt
->label_identifier
->type
== SYM_LABEL
&&
982 stmt
->label_identifier
->ident
) {
983 loop_count
|= 0x0800000;
984 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
988 static void find_asm_gotos(struct statement
*stmt
)
992 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
993 __save_gotos(sym
->ident
->name
, sym
);
994 } END_FOR_EACH_PTR(sym
);
997 void __split_stmt(struct statement
*stmt
)
1004 if (!__in_fake_assign
)
1005 __silence_warnings_for_stmt
= false;
1007 if (__bail_on_rest_of_function
)
1010 if (out_of_memory() || taking_too_long()) {
1011 struct timeval stop
;
1013 gettimeofday(&stop
, NULL
);
1015 __bail_on_rest_of_function
= 1;
1017 sm_msg("Function too hairy. Giving up. %lu seconds",
1018 stop
.tv_sec
- fn_start_time
.tv_sec
);
1020 final_pass
= 0; /* turn off sm_msg() from here */
1024 add_ptr_list(&big_statement_stack
, stmt
);
1025 free_expression_stack(&big_expression_stack
);
1026 set_position(stmt
->pos
);
1027 __pass_to_client(stmt
, STMT_HOOK
);
1029 switch (stmt
->type
) {
1030 case STMT_DECLARATION
:
1031 split_declaration(stmt
->declaration
);
1034 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1036 __split_expr(stmt
->ret_value
);
1037 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1038 __process_post_op_stack();
1041 case STMT_EXPRESSION
:
1042 expr_set_parent_stmt(stmt
->expression
, stmt
);
1043 expr_set_parent_stmt(stmt
->context
, stmt
);
1045 __split_expr(stmt
->expression
);
1048 split_compound(stmt
);
1051 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1052 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1053 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1055 if (known_condition_true(stmt
->if_conditional
)) {
1056 __split_stmt(stmt
->if_true
);
1059 if (known_condition_false(stmt
->if_conditional
)) {
1060 __split_stmt(stmt
->if_false
);
1063 __split_whole_condition(stmt
->if_conditional
);
1064 __split_stmt(stmt
->if_true
);
1065 if (empty_statement(stmt
->if_true
) &&
1066 last_stmt_on_same_line() &&
1067 !get_macro_name(stmt
->if_true
->pos
))
1068 sm_msg("warn: if();");
1069 __push_true_states();
1070 __use_false_states();
1071 __split_stmt(stmt
->if_false
);
1072 __merge_true_states();
1075 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1076 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1077 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1078 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1079 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1081 if (stmt
->iterator_pre_condition
)
1082 handle_pre_loop(stmt
);
1083 else if (stmt
->iterator_post_condition
)
1084 handle_post_loop(stmt
);
1086 // these are for(;;) type loops.
1087 handle_pre_loop(stmt
);
1091 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1092 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1094 if (get_value(stmt
->switch_expression
, &sval
)) {
1095 split_known_switch(stmt
, sval
);
1098 __split_expr(stmt
->switch_expression
);
1099 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1100 __save_switch_states(top_expression(switch_expr_stack
));
1104 __split_stmt(stmt
->switch_statement
);
1105 if (!__pop_default() && have_remaining_cases())
1106 fake_an_empty_default(stmt
->pos
);
1107 __discard_switches();
1109 pop_expression(&switch_expr_stack
);
1115 __split_label_stmt(stmt
);
1116 __split_stmt(stmt
->label_statement
);
1119 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1121 __split_expr(stmt
->goto_expression
);
1122 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1123 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1125 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1127 __process_continues();
1129 } else if (stmt
->goto_label
&&
1130 stmt
->goto_label
->type
== SYM_LABEL
&&
1131 stmt
->goto_label
->ident
) {
1132 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1135 if (is_last_stmt(stmt
))
1136 handle_backward_goto(stmt
);
1141 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1143 find_asm_gotos(stmt
);
1144 __pass_to_client(stmt
, ASM_HOOK
);
1145 __split_expr(stmt
->asm_string
);
1146 split_asm_constraints(stmt
->asm_outputs
);
1147 split_asm_constraints(stmt
->asm_inputs
);
1148 split_asm_constraints(stmt
->asm_clobbers
);
1153 __split_expr(stmt
->range_expression
);
1154 __split_expr(stmt
->range_low
);
1155 __split_expr(stmt
->range_high
);
1158 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1160 __process_post_op_stack();
1163 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1165 struct expression
*expr
;
1167 FOR_EACH_PTR(expr_list
, expr
) {
1168 expr_set_parent_expr(expr
, parent
);
1170 __process_post_op_stack();
1171 } END_FOR_EACH_PTR(expr
);
1174 static void split_sym(struct symbol
*sym
)
1178 if (!(sym
->namespace & NS_SYMBOL
))
1181 __split_stmt(sym
->stmt
);
1182 __split_expr(sym
->array_size
);
1183 split_symlist(sym
->arguments
);
1184 split_symlist(sym
->symbol_list
);
1185 __split_stmt(sym
->inline_stmt
);
1186 split_symlist(sym
->inline_symbol_list
);
1189 static void split_symlist(struct symbol_list
*sym_list
)
1193 FOR_EACH_PTR(sym_list
, sym
) {
1195 } END_FOR_EACH_PTR(sym
);
1198 typedef void (fake_cb
)(struct expression
*expr
);
1200 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1202 struct symbol
*type
, *tmp
;
1208 name
= member
->name
;
1210 type
= get_type(expr
);
1211 if (!type
|| type
->type
!= SYM_STRUCT
)
1215 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1219 if (strcmp(name
, tmp
->ident
->name
) == 0)
1221 } END_FOR_EACH_PTR(tmp
);
1225 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1227 struct symbol
*type
, *member
;
1230 type
= get_type(expr
);
1231 if (!type
|| type
->type
!= SYM_STRUCT
)
1234 FOR_EACH_PTR(type
->symbol_list
, member
) {
1236 return member
->ident
;
1238 } END_FOR_EACH_PTR(member
);
1242 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1244 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1246 struct expression
*edge_member
, *assign
;
1247 struct symbol
*base
= get_real_base_type(member
);
1251 expr
= member_expression(expr
, '.', member
->ident
);
1253 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1254 struct symbol
*type
;
1256 type
= get_real_base_type(tmp
);
1260 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1261 if (get_extra_state(edge_member
))
1264 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1265 set_inner_struct_members(expr
, tmp
);
1272 assign
= assign_expression(edge_member
, '=', zero_expr());
1273 __split_expr(assign
);
1274 } END_FOR_EACH_PTR(tmp
);
1279 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1282 struct expression
*member
= NULL
;
1283 struct expression
*assign
;
1286 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1287 expr
= strip_expr(expr
->unop
);
1291 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1292 type
= get_real_base_type(tmp
);
1297 member
= member_expression(expr
, op
, tmp
->ident
);
1298 if (get_extra_state(member
))
1302 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1303 set_inner_struct_members(expr
, tmp
);
1306 if (type
->type
== SYM_ARRAY
)
1311 assign
= assign_expression(member
, '=', zero_expr());
1312 __split_expr(assign
);
1313 } END_FOR_EACH_PTR(tmp
);
1316 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1318 struct expression
*deref
, *assign
, *tmp
, *right
;
1319 struct symbol
*struct_type
, *type
;
1320 struct ident
*member
;
1323 struct_type
= get_type(symbol
);
1325 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1329 * We're parsing an initializer that could look something like this:
1330 * struct foo foo = {
1332 * .whatever.xxx = 11,
1336 * So what we have here is a list with 42, .whatever, and .zzz. We need
1337 * to break it up into left and right sides of the assignments.
1341 FOR_EACH_PTR(members
, tmp
) {
1343 if (tmp
->type
== EXPR_IDENTIFIER
) {
1344 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1345 while (tmp
->type
== EXPR_IDENTIFIER
) {
1346 member
= tmp
->expr_ident
;
1347 tmp
= tmp
->ident_expression
;
1349 deref
= member_expression(deref
, '.', member
);
1351 deref
= member_expression(symbol
, '.', member
);
1354 member
= number_to_member(symbol
, member_idx
);
1355 deref
= member_expression(symbol
, '.', member
);
1359 if (right
->type
== EXPR_INITIALIZER
) {
1360 type
= get_type(deref
);
1361 if (type
&& type
->type
== SYM_ARRAY
)
1362 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1364 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1366 assign
= assign_expression(deref
, '=', right
);
1369 } END_FOR_EACH_PTR(tmp
);
1371 set_unset_to_zero(struct_type
, symbol
);
1374 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1376 fake_member_assigns_helper(symbol_expression(sym
),
1377 sym
->initializer
->expr_list
, fake_cb
);
1380 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1382 struct expression
*offset
, *binop
, *assign
, *tmp
;
1383 struct symbol
*type
;
1386 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1390 FOR_EACH_PTR(expr_list
, tmp
) {
1391 if (tmp
->type
== EXPR_INDEX
) {
1392 if (tmp
->idx_from
!= tmp
->idx_to
)
1394 idx
= tmp
->idx_from
;
1395 if (!tmp
->idx_expression
)
1397 tmp
= tmp
->idx_expression
;
1399 offset
= value_expr(idx
);
1400 binop
= array_element_expression(array
, offset
);
1401 if (tmp
->type
== EXPR_INITIALIZER
) {
1402 type
= get_type(binop
);
1403 if (type
&& type
->type
== SYM_ARRAY
)
1404 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1406 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1408 assign
= assign_expression(binop
, '=', tmp
);
1413 } END_FOR_EACH_PTR(tmp
);
1416 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1418 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1421 static void fake_assign_expr(struct symbol
*sym
)
1423 struct expression
*assign
, *symbol
;
1425 symbol
= symbol_expression(sym
);
1426 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1427 __split_expr(assign
);
1430 static void call_split_expr(struct expression
*expr
)
1435 static void do_initializer_stuff(struct symbol
*sym
)
1437 if (!sym
->initializer
)
1440 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1441 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1442 fake_element_assigns(sym
, call_split_expr
);
1444 fake_member_assigns(sym
, call_split_expr
);
1446 fake_assign_expr(sym
);
1450 static void split_declaration(struct symbol_list
*sym_list
)
1454 FOR_EACH_PTR(sym_list
, sym
) {
1455 __pass_to_client(sym
, DECLARATION_HOOK
);
1456 do_initializer_stuff(sym
);
1458 } END_FOR_EACH_PTR(sym
);
1461 static void call_global_assign_hooks(struct expression
*assign
)
1463 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1466 static void fake_global_assign(struct symbol
*sym
)
1468 struct expression
*assign
, *symbol
;
1470 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1471 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1472 fake_element_assigns(sym
, call_global_assign_hooks
);
1473 } else if (sym
->initializer
) {
1474 symbol
= symbol_expression(sym
);
1475 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1476 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1478 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1480 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1481 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1482 fake_member_assigns(sym
, call_global_assign_hooks
);
1483 } else if (sym
->initializer
) {
1484 symbol
= symbol_expression(sym
);
1485 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1486 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1488 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1491 symbol
= symbol_expression(sym
);
1492 if (sym
->initializer
)
1493 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1495 assign
= assign_expression(symbol
, '=', zero_expr());
1496 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1500 static void start_function_definition(struct symbol
*sym
)
1502 __in_function_def
= 1;
1503 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1504 __in_function_def
= 0;
1505 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1509 static void split_function(struct symbol
*sym
)
1511 struct symbol
*base_type
= get_base_type(sym
);
1513 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1516 gettimeofday(&fn_start_time
, NULL
);
1519 cur_func
= sym
->ident
->name
;
1520 set_position(sym
->pos
);
1522 last_goto_statement_handled
= 0;
1523 sm_debug("new function: %s\n", cur_func
);
1525 if (option_two_passes
) {
1529 start_function_definition(sym
);
1530 __split_stmt(base_type
->stmt
);
1531 __split_stmt(base_type
->inline_stmt
);
1537 start_function_definition(sym
);
1538 __split_stmt(base_type
->stmt
);
1539 __split_stmt(base_type
->inline_stmt
);
1540 __pass_to_client(sym
, END_FUNC_HOOK
);
1541 if (need_delayed_scope_hooks())
1542 __call_scope_hooks();
1543 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1546 cur_func_sym
= NULL
;
1548 free_data_info_allocs();
1549 free_expression_stack(&switch_expr_stack
);
1550 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1551 __bail_on_rest_of_function
= 0;
1554 static void save_flow_state(void)
1556 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1557 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1558 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1560 __add_ptr_list(&backup
, big_statement_stack
, 0);
1561 __add_ptr_list(&backup
, big_expression_stack
, 0);
1562 __add_ptr_list(&backup
, big_condition_stack
, 0);
1563 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1565 __add_ptr_list(&backup
, cur_func_sym
, 0);
1567 __add_ptr_list(&backup
, __prev_stmt
, 0);
1568 __add_ptr_list(&backup
, __cur_stmt
, 0);
1569 __add_ptr_list(&backup
, __next_stmt
, 0);
1573 static void *pop_backup(void)
1577 ret
= last_ptr_list(backup
);
1578 delete_ptr_list_last(&backup
);
1582 static void restore_flow_state(void)
1584 __next_stmt
= pop_backup();
1585 __cur_stmt
= pop_backup();
1586 __prev_stmt
= pop_backup();
1588 cur_func_sym
= pop_backup();
1589 switch_expr_stack
= pop_backup();
1590 big_condition_stack
= pop_backup();
1591 big_expression_stack
= pop_backup();
1592 big_statement_stack
= pop_backup();
1593 final_pass
= PTR_INT(pop_backup()) >> 2;
1594 loop_count
= PTR_INT(pop_backup()) >> 2;
1595 loop_num
= PTR_INT(pop_backup()) >> 2;
1598 static void parse_inline(struct expression
*call
)
1600 struct symbol
*base_type
;
1601 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1602 struct timeval time_backup
= fn_start_time
;
1606 __pass_to_client(call
, INLINE_FN_START
);
1607 final_pass
= 0; /* don't print anything */
1610 base_type
= get_base_type(call
->fn
->symbol
);
1611 cur_func_sym
= call
->fn
->symbol
;
1612 if (call
->fn
->symbol
->ident
)
1613 cur_func
= call
->fn
->symbol
->ident
->name
;
1616 set_position(call
->fn
->symbol
->pos
);
1619 big_statement_stack
= NULL
;
1620 big_expression_stack
= NULL
;
1621 big_condition_stack
= NULL
;
1622 switch_expr_stack
= NULL
;
1624 sm_debug("inline function: %s\n", cur_func
);
1628 start_function_definition(call
->fn
->symbol
);
1629 __split_stmt(base_type
->stmt
);
1630 __split_stmt(base_type
->inline_stmt
);
1631 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1632 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1634 free_expression_stack(&switch_expr_stack
);
1635 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1639 restore_flow_state();
1640 fn_start_time
= time_backup
;
1641 cur_func
= cur_func_bak
;
1643 restore_all_states();
1644 set_position(call
->pos
);
1646 __pass_to_client(call
, INLINE_FN_END
);
1649 static struct symbol_list
*inlines_called
;
1650 static void add_inline_function(struct symbol
*sym
)
1652 static struct symbol_list
*already_added
;
1655 FOR_EACH_PTR(already_added
, tmp
) {
1658 } END_FOR_EACH_PTR(tmp
);
1660 add_ptr_list(&already_added
, sym
);
1661 add_ptr_list(&inlines_called
, sym
);
1664 static void process_inlines(void)
1668 FOR_EACH_PTR(inlines_called
, tmp
) {
1669 split_function(tmp
);
1670 } END_FOR_EACH_PTR(tmp
);
1671 free_ptr_list(&inlines_called
);
1674 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1678 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1681 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1683 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1685 } END_FOR_EACH_PTR_REVERSE(sym
);
1690 static void split_inlines_in_scope(struct symbol
*sym
)
1692 struct symbol
*base
;
1693 struct symbol_list
*scope_list
;
1696 scope_list
= sym
->scope
->symbols
;
1697 stream
= sym
->pos
.stream
;
1699 /* find the last static symbol in the file */
1700 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1701 if (sym
->pos
.stream
!= stream
)
1703 if (sym
->type
!= SYM_NODE
)
1705 base
= get_base_type(sym
);
1708 if (base
->type
!= SYM_FN
)
1710 if (!base
->inline_stmt
)
1712 add_inline_function(sym
);
1713 } END_FOR_EACH_PTR_REVERSE(sym
);
1718 static void split_inlines(struct symbol_list
*sym_list
)
1722 sym
= get_last_scoped_symbol(sym_list
, 0);
1724 split_inlines_in_scope(sym
);
1725 sym
= get_last_scoped_symbol(sym_list
, 1);
1727 split_inlines_in_scope(sym
);
1730 static struct stree
*clone_estates_perm(struct stree
*orig
)
1732 struct stree
*ret
= NULL
;
1733 struct sm_state
*tmp
;
1735 FOR_EACH_SM(orig
, tmp
) {
1736 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1737 } END_FOR_EACH_SM(tmp
);
1742 static bool interesting_function(struct symbol
*sym
)
1744 static int prev_stream
= -1;
1745 static bool prev_answer
;
1746 const char *filename
;
1750 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1753 if (sym
->pos
.stream
== prev_stream
)
1756 prev_stream
= sym
->pos
.stream
;
1757 prev_answer
= false;
1759 filename
= stream_name(sym
->pos
.stream
);
1760 len
= strlen(filename
);
1761 if (len
> 0 && filename
[len
- 1] == 'c')
1766 struct position last_pos
;
1767 static void split_c_file_functions(struct symbol_list
*sym_list
)
1772 FOR_EACH_PTR(sym_list
, sym
) {
1773 set_position(sym
->pos
);
1774 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1775 __pass_to_client(sym
, BASE_HOOK
);
1776 fake_global_assign(sym
);
1778 } END_FOR_EACH_PTR(sym
);
1779 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1782 FOR_EACH_PTR(sym_list
, sym
) {
1783 set_position(sym
->pos
);
1784 last_pos
= sym
->pos
;
1785 if (!interesting_function(sym
))
1787 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1788 split_function(sym
);
1791 last_pos
= sym
->pos
;
1792 } END_FOR_EACH_PTR(sym
);
1793 split_inlines(sym_list
);
1794 __pass_to_client(sym_list
, END_FILE_HOOK
);
1797 static int final_before_fake
;
1798 void init_fake_env(void)
1801 final_before_fake
= final_pass
;
1803 __push_fake_cur_stree();
1807 void end_fake_env(void)
1809 __pop_fake_cur_stree();
1812 final_pass
= final_before_fake
;
1815 static void open_output_files(char *base_file
)
1819 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1820 sm_outfd
= fopen(buf
, "w");
1822 printf("Error: Cannot open %s\n", buf
);
1829 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1830 sql_outfd
= fopen(buf
, "w");
1832 printf("Error: Cannot open %s\n", buf
);
1836 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1837 caller_info_fd
= fopen(buf
, "w");
1838 if (!caller_info_fd
) {
1839 printf("Error: Cannot open %s\n", buf
);
1844 void smatch(int argc
, char **argv
)
1846 struct string_list
*filelist
= NULL
;
1847 struct symbol_list
*sym_list
;
1848 struct timeval stop
, start
;
1850 gettimeofday(&start
, NULL
);
1853 printf("Usage: smatch [--debug] <filename.c>\n");
1856 sparse_initialize(argc
, argv
, &filelist
);
1857 set_valid_ptr_max();
1858 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1859 if (option_file_output
)
1860 open_output_files(base_file
);
1861 sym_list
= sparse_keep_tokens(base_file
);
1862 split_c_file_functions(sym_list
);
1863 } END_FOR_EACH_PTR_NOTAG(base_file
);
1865 gettimeofday(&stop
, NULL
);
1867 set_position(last_pos
);
1869 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1871 sm_msg("mem: %luKb", get_max_memory());