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 *full_base_file
;
42 static char *cur_func
;
43 static unsigned int loop_count
;
44 static int last_goto_statement_handled
;
45 int __expr_stmt_count
;
46 int __in_function_def
;
47 static struct expression_list
*switch_expr_stack
= NULL
;
48 static struct expression_list
*post_op_stack
= NULL
;
50 static struct ptr_list
*backup
;
52 struct expression_list
*big_expression_stack
;
53 struct statement_list
*big_statement_stack
;
54 struct statement
*__prev_stmt
;
55 struct statement
*__cur_stmt
;
56 struct statement
*__next_stmt
;
57 int __in_pre_condition
= 0;
58 int __bail_on_rest_of_function
= 0;
59 static struct timeval fn_start_time
;
60 char *get_function(void) { return cur_func
; }
61 int get_lineno(void) { return __smatch_lineno
; }
62 int inside_loop(void) { return !!loop_count
; }
63 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
64 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
65 int in_expression_statement(void) { return !!__expr_stmt_count
; }
67 static void split_symlist(struct symbol_list
*sym_list
);
68 static void split_declaration(struct symbol_list
*sym_list
);
69 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
70 static void add_inline_function(struct symbol
*sym
);
71 static void parse_inline(struct expression
*expr
);
73 int option_assume_loops
= 0;
74 int option_two_passes
= 0;
75 struct symbol
*cur_func_sym
= NULL
;
76 struct stree
*global_states
;
78 long long valid_ptr_min
= 4096;
79 long long valid_ptr_max
= 2117777777;
80 sval_t valid_ptr_min_sval
= {
84 sval_t valid_ptr_max_sval
= {
86 {.value
= LONG_MAX
- 100000},
88 struct range_list
*valid_ptr_rl
;
90 static void set_valid_ptr_max(void)
92 if (type_bits(&ptr_ctype
) == 32)
93 valid_ptr_max
= 2117777777;
94 else if (type_bits(&ptr_ctype
) == 64)
95 valid_ptr_max
= 2117777777777777777LL;
97 valid_ptr_max_sval
.value
= valid_ptr_max
;
100 static void alloc_valid_ptr_rl(void)
102 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
103 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
104 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
107 int outside_of_function(void)
109 return cur_func_sym
== NULL
;
112 const char *get_filename(void)
114 if (option_info
&& option_full_path
)
115 return full_base_file
;
118 if (option_full_path
)
119 return full_filename
;
123 const char *get_base_file(void)
125 if (option_full_path
)
126 return full_base_file
;
130 static void set_position(struct position pos
)
133 static int prev_stream
= -1;
138 if (pos
.stream
== 0 && pos
.line
== 0)
141 __smatch_lineno
= pos
.line
;
143 if (pos
.stream
== prev_stream
)
146 filename
= stream_name(pos
.stream
);
149 pathname
= getcwd(NULL
, 0);
151 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
152 full_filename
= malloc(len
);
153 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
155 full_filename
= alloc_string(filename
);
160 int is_assigned_call(struct expression
*expr
)
162 struct expression
*parent
= expr_get_parent_expr(expr
);
165 parent
->type
== EXPR_ASSIGNMENT
&&
167 strip_expr(parent
->right
) == expr
)
173 static int is_inline_func(struct expression
*expr
)
175 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
177 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
182 static int is_noreturn_func(struct expression
*expr
)
184 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
186 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
191 static int inline_budget
= 20;
193 int inlinable(struct expression
*expr
)
196 struct statement
*last_stmt
= NULL
;
198 if (__inline_fn
) /* don't nest */
201 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
203 if (is_no_inline_function(expr
->symbol
->ident
->name
))
205 sym
= get_base_type(expr
->symbol
);
206 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
207 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
209 if (sym
->stmt
->type
!= STMT_COMPOUND
)
211 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
213 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
214 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
216 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
218 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
224 /* the magic numbers in this function are pulled out of my bum. */
225 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
231 void __process_post_op_stack(void)
233 struct expression
*expr
;
235 FOR_EACH_PTR(post_op_stack
, expr
) {
236 __pass_to_client(expr
, OP_HOOK
);
237 } END_FOR_EACH_PTR(expr
);
239 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
242 static int handle_comma_assigns(struct expression
*expr
)
244 struct expression
*right
;
245 struct expression
*assign
;
247 right
= strip_expr(expr
->right
);
248 if (right
->type
!= EXPR_COMMA
)
251 __split_expr(right
->left
);
252 __process_post_op_stack();
254 assign
= assign_expression(expr
->left
, '=', right
->right
);
255 __split_expr(assign
);
260 /* This is to handle *p++ = foo; assignments */
261 static int handle_postop_assigns(struct expression
*expr
)
263 struct expression
*left
, *fake_left
;
264 struct expression
*assign
;
266 left
= strip_expr(expr
->left
);
267 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
269 left
= strip_expr(left
->unop
);
270 if (left
->type
!= EXPR_POSTOP
)
273 fake_left
= deref_expression(strip_expr(left
->unop
));
274 assign
= assign_expression(fake_left
, '=', expr
->right
);
276 __split_expr(assign
);
277 __split_expr(expr
->left
);
282 static int prev_expression_is_getting_address(struct expression
*expr
)
284 struct expression
*parent
;
287 parent
= expr_get_parent_expr(expr
);
291 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
293 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
295 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
304 static int handle__builtin_choose_expr(struct expression
*expr
)
306 struct expression
*const_expr
, *expr1
, *expr2
;
309 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
312 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
313 expr1
= get_argument_from_call_expr(expr
->args
, 1);
314 expr2
= get_argument_from_call_expr(expr
->args
, 2);
316 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
325 void __split_expr(struct expression
*expr
)
330 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
332 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
334 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
337 push_expression(&big_expression_stack
, expr
);
338 set_position(expr
->pos
);
339 __pass_to_client(expr
, EXPR_HOOK
);
341 switch (expr
->type
) {
343 expr_set_parent_expr(expr
->unop
, expr
);
345 if (expr
->op
== '*' &&
346 !prev_expression_is_getting_address(expr
))
347 __pass_to_client(expr
, DEREF_HOOK
);
348 __split_expr(expr
->unop
);
349 __pass_to_client(expr
, OP_HOOK
);
352 expr_set_parent_expr(expr
->unop
, expr
);
354 __split_expr(expr
->unop
);
355 push_expression(&post_op_stack
, expr
);
359 if (expr
->statement
&& !expr
->statement
) {
360 stmt_set_parent_stmt(expr
->statement
,
361 last_ptr_list((struct ptr_list
*)big_statement_stack
));
363 __split_stmt(expr
->statement
);
368 expr_set_parent_expr(expr
->left
, expr
);
369 expr_set_parent_expr(expr
->right
, expr
);
371 __pass_to_client(expr
, LOGIC_HOOK
);
372 __handle_logic(expr
);
375 expr_set_parent_expr(expr
->left
, expr
);
376 expr_set_parent_expr(expr
->right
, expr
);
378 __pass_to_client(expr
, BINOP_HOOK
);
380 expr_set_parent_expr(expr
->left
, expr
);
381 expr_set_parent_expr(expr
->right
, expr
);
383 __split_expr(expr
->left
);
384 __process_post_op_stack();
385 __split_expr(expr
->right
);
387 case EXPR_ASSIGNMENT
: {
388 struct expression
*right
;
390 expr_set_parent_expr(expr
->left
, expr
);
391 expr_set_parent_expr(expr
->right
, expr
);
393 right
= strip_expr(expr
->right
);
397 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
400 if (__handle_condition_assigns(expr
))
402 /* foo = (x < 5 ? foo : 5); */
403 if (__handle_select_assigns(expr
))
405 /* foo = ({frob(); frob(); frob(); 1;}) */
406 if (__handle_expr_statement_assigns(expr
))
409 if (handle_comma_assigns(expr
))
411 if (handle_postop_assigns(expr
))
414 __split_expr(expr
->right
);
415 if (outside_of_function())
416 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
418 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
420 __fake_struct_member_assignments(expr
);
422 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
423 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
425 if (get_macro_name(right
->pos
) &&
426 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
427 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
429 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
431 __split_expr(expr
->left
);
435 expr_set_parent_expr(expr
->deref
, expr
);
437 __pass_to_client(expr
, DEREF_HOOK
);
438 __split_expr(expr
->deref
);
441 expr_set_parent_expr(expr
->base
, expr
);
443 __split_expr(expr
->base
);
446 case EXPR_FORCE_CAST
:
447 expr_set_parent_expr(expr
->cast_expression
, expr
);
449 __pass_to_client(expr
, CAST_HOOK
);
450 __split_expr(expr
->cast_expression
);
453 if (expr
->cast_expression
)
454 __pass_to_client(strip_parens(expr
->cast_expression
),
459 evaluate_expression(expr
);
461 case EXPR_CONDITIONAL
:
463 expr_set_parent_expr(expr
->conditional
, expr
);
464 expr_set_parent_expr(expr
->cond_true
, expr
);
465 expr_set_parent_expr(expr
->cond_false
, expr
);
467 if (known_condition_true(expr
->conditional
)) {
468 __split_expr(expr
->cond_true
);
471 if (known_condition_false(expr
->conditional
)) {
472 __split_expr(expr
->cond_false
);
475 __pass_to_client(expr
, SELECT_HOOK
);
476 __split_whole_condition(expr
->conditional
);
477 __split_expr(expr
->cond_true
);
478 __push_true_states();
479 __use_false_states();
480 __split_expr(expr
->cond_false
);
481 __merge_true_states();
484 expr_set_parent_expr(expr
->fn
, expr
);
486 if (sym_name_is("__builtin_constant_p", expr
->fn
))
488 if (handle__builtin_choose_expr(expr
))
490 split_expr_list(expr
->args
, expr
);
491 __split_expr(expr
->fn
);
492 if (is_inline_func(expr
->fn
))
493 add_inline_function(expr
->fn
->symbol
);
494 if (inlinable(expr
->fn
))
496 __process_post_op_stack();
497 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
498 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
500 if (inlinable(expr
->fn
)) {
503 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
504 if (is_noreturn_func(expr
->fn
))
507 case EXPR_INITIALIZER
:
508 split_expr_list(expr
->expr_list
, expr
);
510 case EXPR_IDENTIFIER
:
511 expr_set_parent_expr(expr
->ident_expression
, expr
);
512 __split_expr(expr
->ident_expression
);
515 expr_set_parent_expr(expr
->idx_expression
, expr
);
516 __split_expr(expr
->idx_expression
);
519 expr_set_parent_expr(expr
->init_expr
, expr
);
520 __split_expr(expr
->init_expr
);
523 __pass_to_client(expr
, SYM_HOOK
);
526 __pass_to_client(expr
, STRING_HOOK
);
531 pop_expression(&big_expression_stack
);
534 static int is_forever_loop(struct statement
*stmt
)
536 struct expression
*expr
;
539 expr
= strip_expr(stmt
->iterator_pre_condition
);
541 expr
= stmt
->iterator_post_condition
;
543 /* this is a for(;;) loop... */
547 if (get_value(expr
, &sval
) && sval
.value
!= 0)
554 static char *get_loop_name(int num
)
558 snprintf(buf
, 255, "-loop%d", num
);
560 return alloc_sname(buf
);
564 * Pre Loops are while and for loops.
566 static void handle_pre_loop(struct statement
*stmt
)
568 int once_through
; /* we go through the loop at least once */
569 struct sm_state
*extra_sm
= NULL
;
572 struct stree
*stree
= NULL
;
573 struct sm_state
*sm
= NULL
;
575 loop_name
= get_loop_name(loop_num
);
578 __split_stmt(stmt
->iterator_pre_statement
);
579 __prev_stmt
= stmt
->iterator_pre_statement
;
581 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
587 __merge_gotos(loop_name
, NULL
);
589 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
590 __in_pre_condition
++;
591 __pass_to_client(stmt
, PRELOOP_HOOK
);
592 __split_whole_condition(stmt
->iterator_pre_condition
);
593 __in_pre_condition
--;
594 FOR_EACH_SM(stree
, sm
) {
595 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
596 } END_FOR_EACH_SM(sm
);
599 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
601 if (option_assume_loops
)
604 __split_stmt(stmt
->iterator_statement
);
605 if (is_forever_loop(stmt
)) {
607 __save_gotos(loop_name
, NULL
);
609 __push_fake_cur_stree();
610 __split_stmt(stmt
->iterator_post_statement
);
611 stree
= __pop_fake_cur_stree();
613 __discard_false_states();
616 if (!__path_is_null())
617 __merge_stree_into_cur(stree
);
621 unchanged
= __iterator_unchanged(extra_sm
);
622 __split_stmt(stmt
->iterator_post_statement
);
623 __prev_stmt
= stmt
->iterator_post_statement
;
626 __save_gotos(loop_name
, NULL
);
627 __in_pre_condition
++;
628 __split_whole_condition(stmt
->iterator_pre_condition
);
629 __in_pre_condition
--;
631 __merge_false_states();
633 __discard_false_states();
635 __merge_false_states();
637 if (extra_sm
&& unchanged
)
638 __extra_pre_loop_hook_after(extra_sm
,
639 stmt
->iterator_post_statement
,
640 stmt
->iterator_pre_condition
);
647 * Post loops are do {} while();
649 static void handle_post_loop(struct statement
*stmt
)
653 loop_name
= get_loop_name(loop_num
);
659 __merge_gotos(loop_name
, NULL
);
660 __split_stmt(stmt
->iterator_statement
);
662 if (!is_zero(stmt
->iterator_post_condition
))
663 __save_gotos(loop_name
, NULL
);
665 if (is_forever_loop(stmt
)) {
668 __split_whole_condition(stmt
->iterator_post_condition
);
669 __use_false_states();
675 static int empty_statement(struct statement
*stmt
)
679 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
684 static int last_stmt_on_same_line(void)
686 struct statement
*stmt
;
689 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
692 if (stmt
->pos
.line
== get_lineno())
695 } END_FOR_EACH_PTR_REVERSE(stmt
);
699 static void split_asm_constraints(struct expression_list
*expr_list
)
701 struct expression
*expr
;
704 FOR_EACH_PTR(expr_list
, expr
) {
706 case 0: /* identifier */
707 case 1: /* constraint */
710 case 2: /* expression */
715 } END_FOR_EACH_PTR(expr
);
718 static int is_case_val(struct statement
*stmt
, sval_t sval
)
722 if (stmt
->type
!= STMT_CASE
)
724 if (!stmt
->case_expression
) {
728 if (!get_value(stmt
->case_expression
, &case_sval
))
730 if (case_sval
.value
== sval
.value
)
735 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
736 struct expression
*case_expr
,
737 struct expression
*case_to
)
740 struct range_list
*rl
= NULL
;
741 struct symbol
*switch_type
;
743 switch_type
= get_type(switch_expr
);
744 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
745 start
= sval_cast(switch_type
, start
);
746 end
= sval_cast(switch_type
, end
);
747 add_range(&rl
, start
, end
);
748 } else if (get_value(case_expr
, &start
)) {
749 start
= sval_cast(switch_type
, start
);
750 add_range(&rl
, start
, start
);
756 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
758 struct statement
*tmp
;
759 struct range_list
*rl
;
761 __split_expr(stmt
->switch_expression
);
762 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
764 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
765 __save_switch_states(top_expression(switch_expr_stack
));
770 stmt
= stmt
->switch_statement
;
772 __push_scope_hooks();
773 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
774 __smatch_lineno
= tmp
->pos
.line
;
775 if (is_case_val(tmp
, sval
)) {
776 rl
= alloc_rl(sval
, sval
);
777 __merge_switches(top_expression(switch_expr_stack
), rl
);
778 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
780 if (__path_is_null())
783 if (__path_is_null()) {
787 } END_FOR_EACH_PTR(tmp
);
789 __call_scope_hooks();
790 if (!__pop_default())
791 __merge_switches(top_expression(switch_expr_stack
), NULL
);
792 __discard_switches();
794 pop_expression(&switch_expr_stack
);
797 static void split_case(struct statement
*stmt
)
799 struct range_list
*rl
= NULL
;
801 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
802 expr_set_parent_stmt(stmt
->case_to
, stmt
);
804 rl
= get_case_rl(top_expression(switch_expr_stack
),
805 stmt
->case_expression
, stmt
->case_to
);
806 while (stmt
->case_statement
->type
== STMT_CASE
) {
807 struct range_list
*tmp
;
809 tmp
= get_case_rl(top_expression(switch_expr_stack
),
810 stmt
->case_statement
->case_expression
,
811 stmt
->case_statement
->case_to
);
814 rl
= rl_union(rl
, tmp
);
815 if (!stmt
->case_expression
)
817 stmt
= stmt
->case_statement
;
820 __merge_switches(top_expression(switch_expr_stack
), rl
);
822 if (!stmt
->case_expression
)
824 __split_stmt(stmt
->case_statement
);
827 int time_parsing_function(void)
829 return ms_since(&fn_start_time
) / 1000;
832 static int taking_too_long(void)
834 if (time_parsing_function() > 60 * 5) /* five minutes */
839 static int is_last_stmt(struct statement
*cur_stmt
)
841 struct symbol
*fn
= get_base_type(cur_func_sym
);
842 struct statement
*stmt
;
848 stmt
= fn
->inline_stmt
;
849 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
851 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
852 if (stmt
&& stmt
->type
== STMT_LABEL
)
853 stmt
= stmt
->label_statement
;
854 if (stmt
== cur_stmt
)
859 static void handle_backward_goto(struct statement
*goto_stmt
)
861 const char *goto_name
, *label_name
;
862 struct statement
*func_stmt
;
863 struct symbol
*base_type
= get_base_type(cur_func_sym
);
864 struct statement
*tmp
;
869 if (last_goto_statement_handled
)
871 last_goto_statement_handled
= 1;
873 if (!goto_stmt
->goto_label
||
874 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
875 !goto_stmt
->goto_label
->ident
)
877 goto_name
= goto_stmt
->goto_label
->ident
->name
;
879 func_stmt
= base_type
->stmt
;
881 func_stmt
= base_type
->inline_stmt
;
884 if (func_stmt
->type
!= STMT_COMPOUND
)
887 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
889 if (tmp
->type
!= STMT_LABEL
)
891 if (!tmp
->label_identifier
||
892 tmp
->label_identifier
->type
!= SYM_LABEL
||
893 !tmp
->label_identifier
->ident
)
895 label_name
= tmp
->label_identifier
->ident
->name
;
896 if (strcmp(goto_name
, label_name
) != 0)
901 } END_FOR_EACH_PTR(tmp
);
904 static void fake_a_return(void)
906 struct symbol
*return_type
;
911 return_type
= get_real_base_type(cur_func_sym
);
912 return_type
= get_real_base_type(return_type
);
913 if (return_type
!= &void_ctype
) {
914 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
919 static void fake_an_empty_default(struct position pos
)
921 static struct statement none
= {};
924 none
.type
= STMT_NONE
;
925 __merge_switches(top_expression(switch_expr_stack
), NULL
);
929 static void split_compound(struct statement
*stmt
)
931 struct statement
*prev
= NULL
;
932 struct statement
*cur
= NULL
;
933 struct statement
*next
;
935 __push_scope_hooks();
937 FOR_EACH_PTR(stmt
->stmts
, next
) {
938 /* just set them all ahead of time */
939 stmt_set_parent_stmt(next
, stmt
);
949 } END_FOR_EACH_PTR(next
);
958 * For function scope, then delay calling the scope hooks until the
959 * end of function hooks can run. I'm not positive this is the right
962 if (!is_last_stmt(cur
))
963 __call_scope_hooks();
967 * This is a hack, work around for detecting empty functions.
969 static int need_delayed_scope_hooks(void)
971 struct symbol
*fn
= get_base_type(cur_func_sym
);
972 struct statement
*stmt
;
978 stmt
= fn
->inline_stmt
;
979 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
984 void __split_label_stmt(struct statement
*stmt
)
986 if (stmt
->label_identifier
&&
987 stmt
->label_identifier
->type
== SYM_LABEL
&&
988 stmt
->label_identifier
->ident
) {
989 loop_count
|= 0x0800000;
990 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
994 static void find_asm_gotos(struct statement
*stmt
)
998 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
999 __save_gotos(sym
->ident
->name
, sym
);
1000 } END_FOR_EACH_PTR(sym
);
1003 void __split_stmt(struct statement
*stmt
)
1010 if (!__in_fake_assign
)
1011 __silence_warnings_for_stmt
= false;
1013 if (__bail_on_rest_of_function
)
1016 if (out_of_memory() || taking_too_long()) {
1017 struct timeval stop
;
1019 gettimeofday(&stop
, NULL
);
1021 __bail_on_rest_of_function
= 1;
1023 sm_msg("Function too hairy. Giving up. %lu seconds",
1024 stop
.tv_sec
- fn_start_time
.tv_sec
);
1026 final_pass
= 0; /* turn off sm_msg() from here */
1030 add_ptr_list(&big_statement_stack
, stmt
);
1031 free_expression_stack(&big_expression_stack
);
1032 set_position(stmt
->pos
);
1033 __pass_to_client(stmt
, STMT_HOOK
);
1035 switch (stmt
->type
) {
1036 case STMT_DECLARATION
:
1037 split_declaration(stmt
->declaration
);
1040 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1042 __split_expr(stmt
->ret_value
);
1043 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1044 __process_post_op_stack();
1047 case STMT_EXPRESSION
:
1048 expr_set_parent_stmt(stmt
->expression
, stmt
);
1049 expr_set_parent_stmt(stmt
->context
, stmt
);
1051 __split_expr(stmt
->expression
);
1054 split_compound(stmt
);
1057 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1058 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1059 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1061 if (known_condition_true(stmt
->if_conditional
)) {
1062 __split_stmt(stmt
->if_true
);
1065 if (known_condition_false(stmt
->if_conditional
)) {
1066 __split_stmt(stmt
->if_false
);
1069 __split_whole_condition(stmt
->if_conditional
);
1070 __split_stmt(stmt
->if_true
);
1071 if (empty_statement(stmt
->if_true
) &&
1072 last_stmt_on_same_line() &&
1073 !get_macro_name(stmt
->if_true
->pos
))
1074 sm_msg("warn: if();");
1075 __push_true_states();
1076 __use_false_states();
1077 __split_stmt(stmt
->if_false
);
1078 __merge_true_states();
1081 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1082 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1083 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1084 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1085 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1087 if (stmt
->iterator_pre_condition
)
1088 handle_pre_loop(stmt
);
1089 else if (stmt
->iterator_post_condition
)
1090 handle_post_loop(stmt
);
1092 // these are for(;;) type loops.
1093 handle_pre_loop(stmt
);
1097 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1098 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1100 if (get_value(stmt
->switch_expression
, &sval
)) {
1101 split_known_switch(stmt
, sval
);
1104 __split_expr(stmt
->switch_expression
);
1105 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1106 __save_switch_states(top_expression(switch_expr_stack
));
1110 __split_stmt(stmt
->switch_statement
);
1111 if (!__pop_default() && have_remaining_cases())
1112 fake_an_empty_default(stmt
->pos
);
1113 __discard_switches();
1115 pop_expression(&switch_expr_stack
);
1121 __split_label_stmt(stmt
);
1122 __split_stmt(stmt
->label_statement
);
1125 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1127 __split_expr(stmt
->goto_expression
);
1128 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1129 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1131 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1133 __process_continues();
1135 } else if (stmt
->goto_label
&&
1136 stmt
->goto_label
->type
== SYM_LABEL
&&
1137 stmt
->goto_label
->ident
) {
1138 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1141 if (is_last_stmt(stmt
))
1142 handle_backward_goto(stmt
);
1147 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1149 find_asm_gotos(stmt
);
1150 __pass_to_client(stmt
, ASM_HOOK
);
1151 __split_expr(stmt
->asm_string
);
1152 split_asm_constraints(stmt
->asm_outputs
);
1153 split_asm_constraints(stmt
->asm_inputs
);
1154 split_asm_constraints(stmt
->asm_clobbers
);
1159 __split_expr(stmt
->range_expression
);
1160 __split_expr(stmt
->range_low
);
1161 __split_expr(stmt
->range_high
);
1164 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1166 __process_post_op_stack();
1169 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1171 struct expression
*expr
;
1173 FOR_EACH_PTR(expr_list
, expr
) {
1174 expr_set_parent_expr(expr
, parent
);
1176 __process_post_op_stack();
1177 } END_FOR_EACH_PTR(expr
);
1180 static void split_sym(struct symbol
*sym
)
1184 if (!(sym
->namespace & NS_SYMBOL
))
1187 __split_stmt(sym
->stmt
);
1188 __split_expr(sym
->array_size
);
1189 split_symlist(sym
->arguments
);
1190 split_symlist(sym
->symbol_list
);
1191 __split_stmt(sym
->inline_stmt
);
1192 split_symlist(sym
->inline_symbol_list
);
1195 static void split_symlist(struct symbol_list
*sym_list
)
1199 FOR_EACH_PTR(sym_list
, sym
) {
1201 } END_FOR_EACH_PTR(sym
);
1204 typedef void (fake_cb
)(struct expression
*expr
);
1206 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1208 struct symbol
*type
, *tmp
;
1214 name
= member
->name
;
1216 type
= get_type(expr
);
1217 if (!type
|| type
->type
!= SYM_STRUCT
)
1221 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1225 if (strcmp(name
, tmp
->ident
->name
) == 0)
1227 } END_FOR_EACH_PTR(tmp
);
1231 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1233 struct symbol
*type
, *member
;
1236 type
= get_type(expr
);
1237 if (!type
|| type
->type
!= SYM_STRUCT
)
1240 FOR_EACH_PTR(type
->symbol_list
, member
) {
1242 return member
->ident
;
1244 } END_FOR_EACH_PTR(member
);
1248 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1250 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1252 struct expression
*edge_member
, *assign
;
1253 struct symbol
*base
= get_real_base_type(member
);
1257 expr
= member_expression(expr
, '.', member
->ident
);
1259 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1260 struct symbol
*type
;
1262 type
= get_real_base_type(tmp
);
1266 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1267 if (get_extra_state(edge_member
))
1270 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1271 set_inner_struct_members(expr
, tmp
);
1278 assign
= assign_expression(edge_member
, '=', zero_expr());
1279 __split_expr(assign
);
1280 } END_FOR_EACH_PTR(tmp
);
1285 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1288 struct expression
*member
= NULL
;
1289 struct expression
*assign
;
1292 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1293 expr
= strip_expr(expr
->unop
);
1297 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1298 type
= get_real_base_type(tmp
);
1303 member
= member_expression(expr
, op
, tmp
->ident
);
1304 if (get_extra_state(member
))
1308 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1309 set_inner_struct_members(expr
, tmp
);
1312 if (type
->type
== SYM_ARRAY
)
1317 assign
= assign_expression(member
, '=', zero_expr());
1318 __split_expr(assign
);
1319 } END_FOR_EACH_PTR(tmp
);
1322 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1324 struct expression
*deref
, *assign
, *tmp
, *right
;
1325 struct symbol
*struct_type
, *type
;
1326 struct ident
*member
;
1329 struct_type
= get_type(symbol
);
1331 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1335 * We're parsing an initializer that could look something like this:
1336 * struct foo foo = {
1338 * .whatever.xxx = 11,
1342 * So what we have here is a list with 42, .whatever, and .zzz. We need
1343 * to break it up into left and right sides of the assignments.
1347 FOR_EACH_PTR(members
, tmp
) {
1349 if (tmp
->type
== EXPR_IDENTIFIER
) {
1350 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1351 while (tmp
->type
== EXPR_IDENTIFIER
) {
1352 member
= tmp
->expr_ident
;
1353 tmp
= tmp
->ident_expression
;
1355 deref
= member_expression(deref
, '.', member
);
1357 deref
= member_expression(symbol
, '.', member
);
1360 member
= number_to_member(symbol
, member_idx
);
1361 deref
= member_expression(symbol
, '.', member
);
1365 if (right
->type
== EXPR_INITIALIZER
) {
1366 type
= get_type(deref
);
1367 if (type
&& type
->type
== SYM_ARRAY
)
1368 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1370 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1372 assign
= assign_expression(deref
, '=', right
);
1375 } END_FOR_EACH_PTR(tmp
);
1377 set_unset_to_zero(struct_type
, symbol
);
1380 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1382 fake_member_assigns_helper(symbol_expression(sym
),
1383 sym
->initializer
->expr_list
, fake_cb
);
1386 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1388 struct expression
*offset
, *binop
, *assign
, *tmp
;
1389 struct symbol
*type
;
1392 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1396 FOR_EACH_PTR(expr_list
, tmp
) {
1397 if (tmp
->type
== EXPR_INDEX
) {
1398 if (tmp
->idx_from
!= tmp
->idx_to
)
1400 idx
= tmp
->idx_from
;
1401 if (!tmp
->idx_expression
)
1403 tmp
= tmp
->idx_expression
;
1405 offset
= value_expr(idx
);
1406 binop
= array_element_expression(array
, offset
);
1407 if (tmp
->type
== EXPR_INITIALIZER
) {
1408 type
= get_type(binop
);
1409 if (type
&& type
->type
== SYM_ARRAY
)
1410 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1412 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1414 assign
= assign_expression(binop
, '=', tmp
);
1419 } END_FOR_EACH_PTR(tmp
);
1422 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1424 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1427 static void fake_assign_expr(struct symbol
*sym
)
1429 struct expression
*assign
, *symbol
;
1431 symbol
= symbol_expression(sym
);
1432 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1433 __split_expr(assign
);
1436 static void do_initializer_stuff(struct symbol
*sym
)
1438 if (!sym
->initializer
)
1441 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1442 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1443 fake_element_assigns(sym
, __split_expr
);
1445 fake_member_assigns(sym
, __split_expr
);
1447 fake_assign_expr(sym
);
1451 static void split_declaration(struct symbol_list
*sym_list
)
1455 FOR_EACH_PTR(sym_list
, sym
) {
1456 __pass_to_client(sym
, DECLARATION_HOOK
);
1457 do_initializer_stuff(sym
);
1459 } END_FOR_EACH_PTR(sym
);
1462 static void call_global_assign_hooks(struct expression
*assign
)
1464 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1467 static void fake_global_assign(struct symbol
*sym
)
1469 struct expression
*assign
, *symbol
;
1471 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1472 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1473 fake_element_assigns(sym
, call_global_assign_hooks
);
1474 } else if (sym
->initializer
) {
1475 symbol
= symbol_expression(sym
);
1476 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1477 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1479 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1481 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1482 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1483 fake_member_assigns(sym
, call_global_assign_hooks
);
1484 } else if (sym
->initializer
) {
1485 symbol
= symbol_expression(sym
);
1486 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1487 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1489 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1492 symbol
= symbol_expression(sym
);
1493 if (sym
->initializer
)
1494 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1496 assign
= assign_expression(symbol
, '=', zero_expr());
1497 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1501 static void start_function_definition(struct symbol
*sym
)
1503 __in_function_def
= 1;
1504 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1505 __in_function_def
= 0;
1506 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1510 static void split_function(struct symbol
*sym
)
1512 struct symbol
*base_type
= get_base_type(sym
);
1513 struct timeval stop
;
1515 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1518 gettimeofday(&fn_start_time
, NULL
);
1521 cur_func
= sym
->ident
->name
;
1522 set_position(sym
->pos
);
1524 last_goto_statement_handled
= 0;
1525 sm_debug("new function: %s\n", cur_func
);
1527 if (option_two_passes
) {
1531 start_function_definition(sym
);
1532 __split_stmt(base_type
->stmt
);
1533 __split_stmt(base_type
->inline_stmt
);
1539 start_function_definition(sym
);
1540 __split_stmt(base_type
->stmt
);
1541 __split_stmt(base_type
->inline_stmt
);
1542 __pass_to_client(sym
, END_FUNC_HOOK
);
1543 if (need_delayed_scope_hooks())
1544 __call_scope_hooks();
1545 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1549 gettimeofday(&stop
, NULL
);
1550 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1552 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1555 cur_func_sym
= NULL
;
1557 free_data_info_allocs();
1558 free_expression_stack(&switch_expr_stack
);
1559 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1560 __bail_on_rest_of_function
= 0;
1563 static void save_flow_state(void)
1565 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1566 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1567 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1569 __add_ptr_list(&backup
, big_statement_stack
, 0);
1570 __add_ptr_list(&backup
, big_expression_stack
, 0);
1571 __add_ptr_list(&backup
, big_condition_stack
, 0);
1572 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1574 __add_ptr_list(&backup
, cur_func_sym
, 0);
1576 __add_ptr_list(&backup
, __prev_stmt
, 0);
1577 __add_ptr_list(&backup
, __cur_stmt
, 0);
1578 __add_ptr_list(&backup
, __next_stmt
, 0);
1582 static void *pop_backup(void)
1586 ret
= last_ptr_list(backup
);
1587 delete_ptr_list_last(&backup
);
1591 static void restore_flow_state(void)
1593 __next_stmt
= pop_backup();
1594 __cur_stmt
= pop_backup();
1595 __prev_stmt
= pop_backup();
1597 cur_func_sym
= pop_backup();
1598 switch_expr_stack
= pop_backup();
1599 big_condition_stack
= pop_backup();
1600 big_expression_stack
= pop_backup();
1601 big_statement_stack
= pop_backup();
1602 final_pass
= PTR_INT(pop_backup()) >> 2;
1603 loop_count
= PTR_INT(pop_backup()) >> 2;
1604 loop_num
= PTR_INT(pop_backup()) >> 2;
1607 static void parse_inline(struct expression
*call
)
1609 struct symbol
*base_type
;
1610 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1611 struct timeval time_backup
= fn_start_time
;
1612 struct expression
*orig_inline
= __inline_fn
;
1617 __pass_to_client(call
, INLINE_FN_START
);
1618 final_pass
= 0; /* don't print anything */
1620 orig_budget
= inline_budget
;
1621 inline_budget
= inline_budget
- 5;
1623 base_type
= get_base_type(call
->fn
->symbol
);
1624 cur_func_sym
= call
->fn
->symbol
;
1625 if (call
->fn
->symbol
->ident
)
1626 cur_func
= call
->fn
->symbol
->ident
->name
;
1629 set_position(call
->fn
->symbol
->pos
);
1632 big_statement_stack
= NULL
;
1633 big_expression_stack
= NULL
;
1634 big_condition_stack
= NULL
;
1635 switch_expr_stack
= NULL
;
1637 sm_debug("inline function: %s\n", cur_func
);
1641 start_function_definition(call
->fn
->symbol
);
1642 __split_stmt(base_type
->stmt
);
1643 __split_stmt(base_type
->inline_stmt
);
1644 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1645 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1647 free_expression_stack(&switch_expr_stack
);
1648 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1652 restore_flow_state();
1653 fn_start_time
= time_backup
;
1654 cur_func
= cur_func_bak
;
1656 restore_all_states();
1657 set_position(call
->pos
);
1658 __inline_fn
= orig_inline
;
1659 inline_budget
= orig_budget
;
1660 __pass_to_client(call
, INLINE_FN_END
);
1663 static struct symbol_list
*inlines_called
;
1664 static void add_inline_function(struct symbol
*sym
)
1666 static struct symbol_list
*already_added
;
1669 FOR_EACH_PTR(already_added
, tmp
) {
1672 } END_FOR_EACH_PTR(tmp
);
1674 add_ptr_list(&already_added
, sym
);
1675 add_ptr_list(&inlines_called
, sym
);
1678 static void process_inlines(void)
1682 FOR_EACH_PTR(inlines_called
, tmp
) {
1683 split_function(tmp
);
1684 } END_FOR_EACH_PTR(tmp
);
1685 free_ptr_list(&inlines_called
);
1688 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1692 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1695 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1697 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1699 } END_FOR_EACH_PTR_REVERSE(sym
);
1704 static bool interesting_function(struct symbol
*sym
)
1706 static int prev_stream
= -1;
1707 static bool prev_answer
;
1708 const char *filename
;
1711 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1714 if (sym
->pos
.stream
== prev_stream
)
1717 prev_stream
= sym
->pos
.stream
;
1718 prev_answer
= false;
1720 filename
= stream_name(sym
->pos
.stream
);
1721 len
= strlen(filename
);
1722 if (len
> 0 && filename
[len
- 1] == 'c')
1727 static void split_inlines_in_scope(struct symbol
*sym
)
1729 struct symbol
*base
;
1730 struct symbol_list
*scope_list
;
1733 scope_list
= sym
->scope
->symbols
;
1734 stream
= sym
->pos
.stream
;
1736 /* find the last static symbol in the file */
1737 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1738 if (sym
->pos
.stream
!= stream
)
1740 if (sym
->type
!= SYM_NODE
)
1742 base
= get_base_type(sym
);
1745 if (base
->type
!= SYM_FN
)
1747 if (!base
->inline_stmt
)
1749 if (!interesting_function(sym
))
1751 add_inline_function(sym
);
1752 } END_FOR_EACH_PTR_REVERSE(sym
);
1757 static void split_inlines(struct symbol_list
*sym_list
)
1761 sym
= get_last_scoped_symbol(sym_list
, 0);
1763 split_inlines_in_scope(sym
);
1764 sym
= get_last_scoped_symbol(sym_list
, 1);
1766 split_inlines_in_scope(sym
);
1769 static struct stree
*clone_estates_perm(struct stree
*orig
)
1771 struct stree
*ret
= NULL
;
1772 struct sm_state
*tmp
;
1774 FOR_EACH_SM(orig
, tmp
) {
1775 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1776 } END_FOR_EACH_SM(tmp
);
1781 struct position last_pos
;
1782 static void split_c_file_functions(struct symbol_list
*sym_list
)
1787 FOR_EACH_PTR(sym_list
, sym
) {
1788 set_position(sym
->pos
);
1789 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1790 __pass_to_client(sym
, BASE_HOOK
);
1791 fake_global_assign(sym
);
1793 } END_FOR_EACH_PTR(sym
);
1794 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1797 FOR_EACH_PTR(sym_list
, sym
) {
1798 set_position(sym
->pos
);
1799 last_pos
= sym
->pos
;
1800 if (!interesting_function(sym
))
1802 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1803 split_function(sym
);
1806 last_pos
= sym
->pos
;
1807 } END_FOR_EACH_PTR(sym
);
1808 split_inlines(sym_list
);
1809 __pass_to_client(sym_list
, END_FILE_HOOK
);
1812 static int final_before_fake
;
1813 void init_fake_env(void)
1816 final_before_fake
= final_pass
;
1818 __push_fake_cur_stree();
1822 void end_fake_env(void)
1824 __pop_fake_cur_stree();
1827 final_pass
= final_before_fake
;
1830 static void open_output_files(char *base_file
)
1834 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1835 sm_outfd
= fopen(buf
, "w");
1837 printf("Error: Cannot open %s\n", buf
);
1844 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1845 sql_outfd
= fopen(buf
, "w");
1847 printf("Error: Cannot open %s\n", buf
);
1851 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1852 caller_info_fd
= fopen(buf
, "w");
1853 if (!caller_info_fd
) {
1854 printf("Error: Cannot open %s\n", buf
);
1859 void smatch(int argc
, char **argv
)
1861 struct string_list
*filelist
= NULL
;
1862 struct symbol_list
*sym_list
;
1863 struct timeval stop
, start
;
1867 gettimeofday(&start
, NULL
);
1870 printf("Usage: smatch [--debug] <filename.c>\n");
1873 sparse_initialize(argc
, argv
, &filelist
);
1874 set_valid_ptr_max();
1875 alloc_valid_ptr_rl();
1876 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1877 path
= getcwd(NULL
, 0);
1878 free(full_base_file
);
1880 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
1881 full_base_file
= malloc(len
);
1882 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
1884 full_base_file
= alloc_string(base_file
);
1886 if (option_file_output
)
1887 open_output_files(base_file
);
1888 sym_list
= sparse_keep_tokens(base_file
);
1889 split_c_file_functions(sym_list
);
1890 } END_FOR_EACH_PTR_NOTAG(base_file
);
1892 gettimeofday(&stop
, NULL
);
1894 set_position(last_pos
);
1896 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1898 sm_msg("mem: %luKb", get_max_memory());