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 static struct timeval outer_fn_start_time
;
61 char *get_function(void) { return cur_func
; }
62 int get_lineno(void) { return __smatch_lineno
; }
63 int inside_loop(void) { return !!loop_count
; }
64 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
65 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
66 int in_expression_statement(void) { return !!__expr_stmt_count
; }
68 static void split_symlist(struct symbol_list
*sym_list
);
69 static void split_declaration(struct symbol_list
*sym_list
);
70 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
71 static void add_inline_function(struct symbol
*sym
);
72 static void parse_inline(struct expression
*expr
);
74 int option_assume_loops
= 0;
75 int option_two_passes
= 0;
76 struct symbol
*cur_func_sym
= NULL
;
77 struct stree
*global_states
;
79 const unsigned long valid_ptr_min
= 4096;
80 const unsigned long valid_ptr_max
= LONG_MAX
& ~(MTAG_OFFSET_MASK
);
81 sval_t valid_ptr_min_sval
= {
85 sval_t valid_ptr_max_sval
= {
87 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
89 struct range_list
*valid_ptr_rl
;
91 static void set_valid_ptr_max(void)
93 valid_ptr_max_sval
.value
= valid_ptr_max
;
96 static void alloc_valid_ptr_rl(void)
98 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
99 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
100 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
103 int outside_of_function(void)
105 return cur_func_sym
== NULL
;
108 const char *get_filename(void)
110 if (option_info
&& option_full_path
)
111 return full_base_file
;
114 if (option_full_path
)
115 return full_filename
;
119 const char *get_base_file(void)
121 if (option_full_path
)
122 return full_base_file
;
126 static void set_position(struct position pos
)
129 static int prev_stream
= -1;
134 if (pos
.stream
== 0 && pos
.line
== 0)
137 __smatch_lineno
= pos
.line
;
139 if (pos
.stream
== prev_stream
)
142 filename
= stream_name(pos
.stream
);
145 pathname
= getcwd(NULL
, 0);
147 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
148 full_filename
= malloc(len
);
149 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
151 full_filename
= alloc_string(filename
);
156 int is_assigned_call(struct expression
*expr
)
158 struct expression
*parent
= expr_get_parent_expr(expr
);
161 parent
->type
== EXPR_ASSIGNMENT
&&
163 strip_expr(parent
->right
) == expr
)
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 static int inline_budget
= 20;
189 int inlinable(struct expression
*expr
)
192 struct statement
*last_stmt
= NULL
;
194 if (__inline_fn
) /* don't nest */
197 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
199 if (is_no_inline_function(expr
->symbol
->ident
->name
))
201 sym
= get_base_type(expr
->symbol
);
202 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
203 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
205 if (sym
->stmt
->type
!= STMT_COMPOUND
)
207 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
209 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
210 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
212 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
214 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
220 /* the magic numbers in this function are pulled out of my bum. */
221 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
227 void __process_post_op_stack(void)
229 struct expression
*expr
;
231 FOR_EACH_PTR(post_op_stack
, expr
) {
232 __pass_to_client(expr
, OP_HOOK
);
233 } END_FOR_EACH_PTR(expr
);
235 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
238 static int handle_comma_assigns(struct expression
*expr
)
240 struct expression
*right
;
241 struct expression
*assign
;
243 right
= strip_expr(expr
->right
);
244 if (right
->type
!= EXPR_COMMA
)
247 __split_expr(right
->left
);
248 __process_post_op_stack();
250 assign
= assign_expression(expr
->left
, '=', right
->right
);
251 __split_expr(assign
);
256 /* This is to handle *p++ = foo; assignments */
257 static int handle_postop_assigns(struct expression
*expr
)
259 struct expression
*left
, *fake_left
;
260 struct expression
*assign
;
262 left
= strip_expr(expr
->left
);
263 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
265 left
= strip_expr(left
->unop
);
266 if (left
->type
!= EXPR_POSTOP
)
269 fake_left
= deref_expression(strip_expr(left
->unop
));
270 assign
= assign_expression(fake_left
, '=', expr
->right
);
272 __split_expr(assign
);
273 __split_expr(expr
->left
);
278 static int prev_expression_is_getting_address(struct expression
*expr
)
280 struct expression
*parent
;
283 parent
= expr_get_parent_expr(expr
);
287 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
289 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
291 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
300 static void handle_builtin_overflow_func(struct expression
*expr
)
302 struct expression
*a
, *b
, *res
, *assign
;
305 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
307 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
309 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
314 a
= get_argument_from_call_expr(expr
->args
, 0);
315 b
= get_argument_from_call_expr(expr
->args
, 1);
316 res
= get_argument_from_call_expr(expr
->args
, 2);
318 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
319 __split_expr(assign
);
322 static int handle__builtin_choose_expr(struct expression
*expr
)
324 struct expression
*const_expr
, *expr1
, *expr2
;
327 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
330 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
331 expr1
= get_argument_from_call_expr(expr
->args
, 1);
332 expr2
= get_argument_from_call_expr(expr
->args
, 2);
334 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
343 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
345 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
348 right
= strip_expr(expr
->right
);
349 if (right
->type
!= EXPR_CALL
)
351 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
354 const_expr
= get_argument_from_call_expr(right
->args
, 0);
355 expr1
= get_argument_from_call_expr(right
->args
, 1);
356 expr2
= get_argument_from_call_expr(right
->args
, 2);
358 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
361 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
366 void __split_expr(struct expression
*expr
)
371 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
373 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
375 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
378 push_expression(&big_expression_stack
, expr
);
379 set_position(expr
->pos
);
380 __pass_to_client(expr
, EXPR_HOOK
);
382 switch (expr
->type
) {
384 expr_set_parent_expr(expr
->unop
, expr
);
386 if (expr
->op
== '*' &&
387 !prev_expression_is_getting_address(expr
))
388 __pass_to_client(expr
, DEREF_HOOK
);
389 __split_expr(expr
->unop
);
390 __pass_to_client(expr
, OP_HOOK
);
393 expr_set_parent_expr(expr
->unop
, expr
);
395 __split_expr(expr
->unop
);
396 push_expression(&post_op_stack
, expr
);
400 if (expr
->statement
&& !expr
->statement
) {
401 stmt_set_parent_stmt(expr
->statement
,
402 last_ptr_list((struct ptr_list
*)big_statement_stack
));
404 __split_stmt(expr
->statement
);
409 expr_set_parent_expr(expr
->left
, expr
);
410 expr_set_parent_expr(expr
->right
, expr
);
412 __pass_to_client(expr
, LOGIC_HOOK
);
413 __handle_logic(expr
);
416 expr_set_parent_expr(expr
->left
, expr
);
417 expr_set_parent_expr(expr
->right
, expr
);
419 __pass_to_client(expr
, BINOP_HOOK
);
421 expr_set_parent_expr(expr
->left
, expr
);
422 expr_set_parent_expr(expr
->right
, expr
);
424 __split_expr(expr
->left
);
425 __process_post_op_stack();
426 __split_expr(expr
->right
);
428 case EXPR_ASSIGNMENT
: {
429 struct expression
*right
;
431 expr_set_parent_expr(expr
->left
, expr
);
432 expr_set_parent_expr(expr
->right
, expr
);
434 right
= strip_expr(expr
->right
);
438 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
441 if (__handle_condition_assigns(expr
))
443 /* foo = (x < 5 ? foo : 5); */
444 if (__handle_select_assigns(expr
))
446 /* foo = ({frob(); frob(); frob(); 1;}) */
447 if (__handle_expr_statement_assigns(expr
))
450 if (handle_comma_assigns(expr
))
452 if (handle_postop_assigns(expr
))
454 if (handle__builtin_choose_expr_assigns(expr
))
457 __split_expr(expr
->right
);
458 if (outside_of_function())
459 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
461 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
463 __fake_struct_member_assignments(expr
);
465 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
466 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
468 if (get_macro_name(right
->pos
) &&
469 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
470 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
472 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
474 __split_expr(expr
->left
);
478 expr_set_parent_expr(expr
->deref
, expr
);
480 __pass_to_client(expr
, DEREF_HOOK
);
481 __split_expr(expr
->deref
);
484 expr_set_parent_expr(expr
->base
, expr
);
486 __split_expr(expr
->base
);
489 case EXPR_FORCE_CAST
:
490 expr_set_parent_expr(expr
->cast_expression
, expr
);
492 __pass_to_client(expr
, CAST_HOOK
);
493 __split_expr(expr
->cast_expression
);
496 if (expr
->cast_expression
)
497 __pass_to_client(strip_parens(expr
->cast_expression
),
502 evaluate_expression(expr
);
504 case EXPR_CONDITIONAL
:
506 expr_set_parent_expr(expr
->conditional
, expr
);
507 expr_set_parent_expr(expr
->cond_true
, expr
);
508 expr_set_parent_expr(expr
->cond_false
, expr
);
510 if (known_condition_true(expr
->conditional
)) {
511 __split_expr(expr
->cond_true
);
514 if (known_condition_false(expr
->conditional
)) {
515 __split_expr(expr
->cond_false
);
518 __pass_to_client(expr
, SELECT_HOOK
);
519 __split_whole_condition(expr
->conditional
);
520 __split_expr(expr
->cond_true
);
521 __push_true_states();
522 __use_false_states();
523 __split_expr(expr
->cond_false
);
524 __merge_true_states();
527 expr_set_parent_expr(expr
->fn
, expr
);
529 if (sym_name_is("__builtin_constant_p", expr
->fn
))
531 if (handle__builtin_choose_expr(expr
))
533 split_expr_list(expr
->args
, expr
);
534 __split_expr(expr
->fn
);
535 if (is_inline_func(expr
->fn
))
536 add_inline_function(expr
->fn
->symbol
);
537 if (inlinable(expr
->fn
))
539 __process_post_op_stack();
540 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
541 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
543 if (inlinable(expr
->fn
)) {
546 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
547 if (is_noreturn_func(expr
->fn
))
549 handle_builtin_overflow_func(expr
);
551 case EXPR_INITIALIZER
:
552 split_expr_list(expr
->expr_list
, expr
);
554 case EXPR_IDENTIFIER
:
555 expr_set_parent_expr(expr
->ident_expression
, expr
);
556 __split_expr(expr
->ident_expression
);
559 expr_set_parent_expr(expr
->idx_expression
, expr
);
560 __split_expr(expr
->idx_expression
);
563 expr_set_parent_expr(expr
->init_expr
, expr
);
564 __split_expr(expr
->init_expr
);
567 __pass_to_client(expr
, SYM_HOOK
);
570 __pass_to_client(expr
, STRING_HOOK
);
575 pop_expression(&big_expression_stack
);
578 static int is_forever_loop(struct statement
*stmt
)
580 struct expression
*expr
;
583 expr
= strip_expr(stmt
->iterator_pre_condition
);
585 expr
= stmt
->iterator_post_condition
;
587 /* this is a for(;;) loop... */
591 if (get_value(expr
, &sval
) && sval
.value
!= 0)
598 static char *get_loop_name(int num
)
602 snprintf(buf
, 255, "-loop%d", num
);
604 return alloc_sname(buf
);
608 * Pre Loops are while and for loops.
610 static void handle_pre_loop(struct statement
*stmt
)
612 int once_through
; /* we go through the loop at least once */
613 struct sm_state
*extra_sm
= NULL
;
616 struct stree
*stree
= NULL
;
617 struct sm_state
*sm
= NULL
;
619 loop_name
= get_loop_name(loop_num
);
622 __split_stmt(stmt
->iterator_pre_statement
);
623 __prev_stmt
= stmt
->iterator_pre_statement
;
625 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
631 __merge_gotos(loop_name
, NULL
);
633 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
634 __in_pre_condition
++;
635 __pass_to_client(stmt
, PRELOOP_HOOK
);
636 __split_whole_condition(stmt
->iterator_pre_condition
);
637 __in_pre_condition
--;
638 FOR_EACH_SM(stree
, sm
) {
639 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
640 } END_FOR_EACH_SM(sm
);
643 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
645 if (option_assume_loops
)
648 __split_stmt(stmt
->iterator_statement
);
649 if (is_forever_loop(stmt
)) {
651 __save_gotos(loop_name
, NULL
);
653 __push_fake_cur_stree();
654 __split_stmt(stmt
->iterator_post_statement
);
655 stree
= __pop_fake_cur_stree();
657 __discard_false_states();
660 if (!__path_is_null())
661 __merge_stree_into_cur(stree
);
665 unchanged
= __iterator_unchanged(extra_sm
);
666 __split_stmt(stmt
->iterator_post_statement
);
667 __prev_stmt
= stmt
->iterator_post_statement
;
670 __save_gotos(loop_name
, NULL
);
671 __in_pre_condition
++;
672 __split_whole_condition(stmt
->iterator_pre_condition
);
673 __in_pre_condition
--;
675 __merge_false_states();
677 __discard_false_states();
679 __merge_false_states();
681 if (extra_sm
&& unchanged
)
682 __extra_pre_loop_hook_after(extra_sm
,
683 stmt
->iterator_post_statement
,
684 stmt
->iterator_pre_condition
);
691 * Post loops are do {} while();
693 static void handle_post_loop(struct statement
*stmt
)
697 loop_name
= get_loop_name(loop_num
);
703 __merge_gotos(loop_name
, NULL
);
704 __split_stmt(stmt
->iterator_statement
);
706 if (!is_zero(stmt
->iterator_post_condition
))
707 __save_gotos(loop_name
, NULL
);
709 if (is_forever_loop(stmt
)) {
712 __split_whole_condition(stmt
->iterator_post_condition
);
713 __use_false_states();
719 static int empty_statement(struct statement
*stmt
)
723 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
728 static int last_stmt_on_same_line(void)
730 struct statement
*stmt
;
733 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
736 if (stmt
->pos
.line
== get_lineno())
739 } END_FOR_EACH_PTR_REVERSE(stmt
);
743 static void split_asm_constraints(struct expression_list
*expr_list
)
745 struct expression
*expr
;
748 FOR_EACH_PTR(expr_list
, expr
) {
750 case 0: /* identifier */
751 case 1: /* constraint */
754 case 2: /* expression */
759 } END_FOR_EACH_PTR(expr
);
762 static int is_case_val(struct statement
*stmt
, sval_t sval
)
766 if (stmt
->type
!= STMT_CASE
)
768 if (!stmt
->case_expression
) {
772 if (!get_value(stmt
->case_expression
, &case_sval
))
774 if (case_sval
.value
== sval
.value
)
779 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
780 struct expression
*case_expr
,
781 struct expression
*case_to
)
784 struct range_list
*rl
= NULL
;
785 struct symbol
*switch_type
;
787 switch_type
= get_type(switch_expr
);
788 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
789 start
= sval_cast(switch_type
, start
);
790 end
= sval_cast(switch_type
, end
);
791 add_range(&rl
, start
, end
);
792 } else if (get_value(case_expr
, &start
)) {
793 start
= sval_cast(switch_type
, start
);
794 add_range(&rl
, start
, start
);
800 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
802 struct statement
*tmp
;
803 struct range_list
*rl
;
805 __split_expr(stmt
->switch_expression
);
806 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
808 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
809 __save_switch_states(top_expression(switch_expr_stack
));
814 stmt
= stmt
->switch_statement
;
816 __push_scope_hooks();
817 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
818 __smatch_lineno
= tmp
->pos
.line
;
819 if (is_case_val(tmp
, sval
)) {
820 rl
= alloc_rl(sval
, sval
);
821 __merge_switches(top_expression(switch_expr_stack
), rl
);
822 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
824 if (__path_is_null())
827 if (__path_is_null()) {
831 } END_FOR_EACH_PTR(tmp
);
833 __call_scope_hooks();
834 if (!__pop_default())
835 __merge_switches(top_expression(switch_expr_stack
), NULL
);
836 __discard_switches();
838 pop_expression(&switch_expr_stack
);
841 static void split_case(struct statement
*stmt
)
843 struct range_list
*rl
= NULL
;
845 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
846 expr_set_parent_stmt(stmt
->case_to
, stmt
);
848 rl
= get_case_rl(top_expression(switch_expr_stack
),
849 stmt
->case_expression
, stmt
->case_to
);
850 while (stmt
->case_statement
->type
== STMT_CASE
) {
851 struct range_list
*tmp
;
853 tmp
= get_case_rl(top_expression(switch_expr_stack
),
854 stmt
->case_statement
->case_expression
,
855 stmt
->case_statement
->case_to
);
858 rl
= rl_union(rl
, tmp
);
859 if (!stmt
->case_expression
)
861 stmt
= stmt
->case_statement
;
864 __merge_switches(top_expression(switch_expr_stack
), rl
);
866 if (!stmt
->case_expression
)
868 __split_stmt(stmt
->case_statement
);
871 int time_parsing_function(void)
873 return ms_since(&fn_start_time
) / 1000;
876 static int taking_too_long(void)
878 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
883 static int is_last_stmt(struct statement
*cur_stmt
)
886 struct statement
*stmt
;
890 fn
= get_base_type(cur_func_sym
);
895 stmt
= fn
->inline_stmt
;
896 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
898 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
899 if (stmt
&& stmt
->type
== STMT_LABEL
)
900 stmt
= stmt
->label_statement
;
901 if (stmt
== cur_stmt
)
906 static void handle_backward_goto(struct statement
*goto_stmt
)
908 const char *goto_name
, *label_name
;
909 struct statement
*func_stmt
;
910 struct symbol
*base_type
= get_base_type(cur_func_sym
);
911 struct statement
*tmp
;
916 if (last_goto_statement_handled
)
918 last_goto_statement_handled
= 1;
920 if (!goto_stmt
->goto_label
||
921 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
922 !goto_stmt
->goto_label
->ident
)
924 goto_name
= goto_stmt
->goto_label
->ident
->name
;
926 func_stmt
= base_type
->stmt
;
928 func_stmt
= base_type
->inline_stmt
;
931 if (func_stmt
->type
!= STMT_COMPOUND
)
934 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
936 if (tmp
->type
!= STMT_LABEL
)
938 if (!tmp
->label_identifier
||
939 tmp
->label_identifier
->type
!= SYM_LABEL
||
940 !tmp
->label_identifier
->ident
)
942 label_name
= tmp
->label_identifier
->ident
->name
;
943 if (strcmp(goto_name
, label_name
) != 0)
948 } END_FOR_EACH_PTR(tmp
);
951 static void fake_a_return(void)
953 struct symbol
*return_type
;
958 return_type
= get_real_base_type(cur_func_sym
);
959 return_type
= get_real_base_type(return_type
);
960 if (return_type
!= &void_ctype
) {
961 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
966 static void fake_an_empty_default(struct position pos
)
968 static struct statement none
= {};
971 none
.type
= STMT_NONE
;
972 __merge_switches(top_expression(switch_expr_stack
), NULL
);
976 static void split_compound(struct statement
*stmt
)
978 struct statement
*prev
= NULL
;
979 struct statement
*cur
= NULL
;
980 struct statement
*next
;
982 __push_scope_hooks();
984 FOR_EACH_PTR(stmt
->stmts
, next
) {
985 /* just set them all ahead of time */
986 stmt_set_parent_stmt(next
, stmt
);
996 } END_FOR_EACH_PTR(next
);
1005 * For function scope, then delay calling the scope hooks until the
1006 * end of function hooks can run. I'm not positive this is the right
1009 if (!is_last_stmt(cur
))
1010 __call_scope_hooks();
1014 * This is a hack, work around for detecting empty functions.
1016 static int need_delayed_scope_hooks(void)
1018 struct symbol
*fn
= get_base_type(cur_func_sym
);
1019 struct statement
*stmt
;
1025 stmt
= fn
->inline_stmt
;
1026 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1031 void __split_label_stmt(struct statement
*stmt
)
1033 if (stmt
->label_identifier
&&
1034 stmt
->label_identifier
->type
== SYM_LABEL
&&
1035 stmt
->label_identifier
->ident
) {
1036 loop_count
|= 0x0800000;
1037 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1041 static void find_asm_gotos(struct statement
*stmt
)
1045 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1046 __save_gotos(sym
->ident
->name
, sym
);
1047 } END_FOR_EACH_PTR(sym
);
1050 void __split_stmt(struct statement
*stmt
)
1057 if (!__in_fake_assign
)
1058 __silence_warnings_for_stmt
= false;
1060 if (__bail_on_rest_of_function
|| is_skipped_function())
1063 if (out_of_memory() || taking_too_long()) {
1064 struct timeval stop
;
1066 gettimeofday(&stop
, NULL
);
1068 __bail_on_rest_of_function
= 1;
1070 sm_perror("Function too hairy. Giving up. %lu seconds",
1071 stop
.tv_sec
- fn_start_time
.tv_sec
);
1073 final_pass
= 0; /* turn off sm_msg() from here */
1077 add_ptr_list(&big_statement_stack
, stmt
);
1078 free_expression_stack(&big_expression_stack
);
1079 set_position(stmt
->pos
);
1080 __pass_to_client(stmt
, STMT_HOOK
);
1082 switch (stmt
->type
) {
1083 case STMT_DECLARATION
:
1084 split_declaration(stmt
->declaration
);
1087 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1089 __split_expr(stmt
->ret_value
);
1090 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1091 __process_post_op_stack();
1094 case STMT_EXPRESSION
:
1095 expr_set_parent_stmt(stmt
->expression
, stmt
);
1096 expr_set_parent_stmt(stmt
->context
, stmt
);
1098 __split_expr(stmt
->expression
);
1101 split_compound(stmt
);
1104 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1105 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1106 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1108 if (known_condition_true(stmt
->if_conditional
)) {
1109 __split_stmt(stmt
->if_true
);
1112 if (known_condition_false(stmt
->if_conditional
)) {
1113 __split_stmt(stmt
->if_false
);
1116 __split_whole_condition(stmt
->if_conditional
);
1117 __split_stmt(stmt
->if_true
);
1118 if (empty_statement(stmt
->if_true
) &&
1119 last_stmt_on_same_line() &&
1120 !get_macro_name(stmt
->if_true
->pos
))
1121 sm_warning("if();");
1122 __push_true_states();
1123 __use_false_states();
1124 __split_stmt(stmt
->if_false
);
1125 __merge_true_states();
1128 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1129 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1130 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1131 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1132 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1134 if (stmt
->iterator_pre_condition
)
1135 handle_pre_loop(stmt
);
1136 else if (stmt
->iterator_post_condition
)
1137 handle_post_loop(stmt
);
1139 // these are for(;;) type loops.
1140 handle_pre_loop(stmt
);
1144 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1145 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1147 if (get_value(stmt
->switch_expression
, &sval
)) {
1148 split_known_switch(stmt
, sval
);
1151 __split_expr(stmt
->switch_expression
);
1152 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1153 __save_switch_states(top_expression(switch_expr_stack
));
1157 __split_stmt(stmt
->switch_statement
);
1158 if (!__pop_default() && have_remaining_cases())
1159 fake_an_empty_default(stmt
->pos
);
1160 __discard_switches();
1162 pop_expression(&switch_expr_stack
);
1168 __split_label_stmt(stmt
);
1169 __split_stmt(stmt
->label_statement
);
1172 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1174 __split_expr(stmt
->goto_expression
);
1175 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1176 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1178 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1180 __process_continues();
1182 } else if (stmt
->goto_label
&&
1183 stmt
->goto_label
->type
== SYM_LABEL
&&
1184 stmt
->goto_label
->ident
) {
1185 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1188 if (is_last_stmt(stmt
))
1189 handle_backward_goto(stmt
);
1194 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1196 find_asm_gotos(stmt
);
1197 __pass_to_client(stmt
, ASM_HOOK
);
1198 __split_expr(stmt
->asm_string
);
1199 split_asm_constraints(stmt
->asm_outputs
);
1200 split_asm_constraints(stmt
->asm_inputs
);
1201 split_asm_constraints(stmt
->asm_clobbers
);
1206 __split_expr(stmt
->range_expression
);
1207 __split_expr(stmt
->range_low
);
1208 __split_expr(stmt
->range_high
);
1211 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1213 __process_post_op_stack();
1216 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1218 struct expression
*expr
;
1220 FOR_EACH_PTR(expr_list
, expr
) {
1221 expr_set_parent_expr(expr
, parent
);
1223 __process_post_op_stack();
1224 } END_FOR_EACH_PTR(expr
);
1227 static void split_sym(struct symbol
*sym
)
1231 if (!(sym
->namespace & NS_SYMBOL
))
1234 __split_stmt(sym
->stmt
);
1235 __split_expr(sym
->array_size
);
1236 split_symlist(sym
->arguments
);
1237 split_symlist(sym
->symbol_list
);
1238 __split_stmt(sym
->inline_stmt
);
1239 split_symlist(sym
->inline_symbol_list
);
1242 static void split_symlist(struct symbol_list
*sym_list
)
1246 FOR_EACH_PTR(sym_list
, sym
) {
1248 } END_FOR_EACH_PTR(sym
);
1251 typedef void (fake_cb
)(struct expression
*expr
);
1253 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1255 struct symbol
*type
, *tmp
;
1261 name
= member
->name
;
1263 type
= get_type(expr
);
1264 if (!type
|| type
->type
!= SYM_STRUCT
)
1268 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1272 if (strcmp(name
, tmp
->ident
->name
) == 0)
1274 } END_FOR_EACH_PTR(tmp
);
1278 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1280 struct symbol
*type
, *member
;
1283 type
= get_type(expr
);
1284 if (!type
|| type
->type
!= SYM_STRUCT
)
1287 FOR_EACH_PTR(type
->symbol_list
, member
) {
1289 return member
->ident
;
1291 } END_FOR_EACH_PTR(member
);
1295 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1297 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1299 struct expression
*edge_member
, *assign
;
1300 struct symbol
*base
= get_real_base_type(member
);
1304 expr
= member_expression(expr
, '.', member
->ident
);
1306 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1307 struct symbol
*type
;
1309 type
= get_real_base_type(tmp
);
1313 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1314 if (get_extra_state(edge_member
))
1317 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1318 set_inner_struct_members(expr
, tmp
);
1325 assign
= assign_expression(edge_member
, '=', zero_expr());
1326 __split_expr(assign
);
1327 } END_FOR_EACH_PTR(tmp
);
1332 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1335 struct expression
*member
= NULL
;
1336 struct expression
*assign
;
1339 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1340 expr
= strip_expr(expr
->unop
);
1344 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1345 type
= get_real_base_type(tmp
);
1350 member
= member_expression(expr
, op
, tmp
->ident
);
1351 if (get_extra_state(member
))
1355 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1356 set_inner_struct_members(expr
, tmp
);
1359 if (type
->type
== SYM_ARRAY
)
1364 assign
= assign_expression(member
, '=', zero_expr());
1365 __split_expr(assign
);
1366 } END_FOR_EACH_PTR(tmp
);
1369 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1371 struct expression
*deref
, *assign
, *tmp
, *right
;
1372 struct symbol
*struct_type
, *type
;
1373 struct ident
*member
;
1376 struct_type
= get_type(symbol
);
1378 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1382 * We're parsing an initializer that could look something like this:
1383 * struct foo foo = {
1385 * .whatever.xxx = 11,
1389 * So what we have here is a list with 42, .whatever, and .zzz. We need
1390 * to break it up into left and right sides of the assignments.
1394 FOR_EACH_PTR(members
, tmp
) {
1396 if (tmp
->type
== EXPR_IDENTIFIER
) {
1397 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1398 while (tmp
->type
== EXPR_IDENTIFIER
) {
1399 member
= tmp
->expr_ident
;
1400 tmp
= tmp
->ident_expression
;
1402 deref
= member_expression(deref
, '.', member
);
1404 deref
= member_expression(symbol
, '.', member
);
1407 member
= number_to_member(symbol
, member_idx
);
1408 deref
= member_expression(symbol
, '.', member
);
1412 if (right
->type
== EXPR_INITIALIZER
) {
1413 type
= get_type(deref
);
1414 if (type
&& type
->type
== SYM_ARRAY
)
1415 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1417 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1419 assign
= assign_expression(deref
, '=', right
);
1422 } END_FOR_EACH_PTR(tmp
);
1424 set_unset_to_zero(struct_type
, symbol
);
1427 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1429 fake_member_assigns_helper(symbol_expression(sym
),
1430 sym
->initializer
->expr_list
, fake_cb
);
1433 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1435 struct expression
*offset
, *binop
, *assign
, *tmp
;
1436 struct symbol
*type
;
1439 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1443 FOR_EACH_PTR(expr_list
, tmp
) {
1444 if (tmp
->type
== EXPR_INDEX
) {
1445 if (tmp
->idx_from
!= tmp
->idx_to
)
1447 idx
= tmp
->idx_from
;
1448 if (!tmp
->idx_expression
)
1450 tmp
= tmp
->idx_expression
;
1452 offset
= value_expr(idx
);
1453 binop
= array_element_expression(array
, offset
);
1454 if (tmp
->type
== EXPR_INITIALIZER
) {
1455 type
= get_type(binop
);
1456 if (type
&& type
->type
== SYM_ARRAY
)
1457 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1459 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1461 assign
= assign_expression(binop
, '=', tmp
);
1466 } END_FOR_EACH_PTR(tmp
);
1469 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1471 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1474 static void fake_assign_expr(struct symbol
*sym
)
1476 struct expression
*assign
, *symbol
;
1478 symbol
= symbol_expression(sym
);
1479 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1480 __split_expr(assign
);
1483 static void do_initializer_stuff(struct symbol
*sym
)
1485 if (!sym
->initializer
)
1488 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1489 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1490 fake_element_assigns(sym
, __split_expr
);
1492 fake_member_assigns(sym
, __split_expr
);
1494 fake_assign_expr(sym
);
1498 static void split_declaration(struct symbol_list
*sym_list
)
1502 FOR_EACH_PTR(sym_list
, sym
) {
1503 __pass_to_client(sym
, DECLARATION_HOOK
);
1504 do_initializer_stuff(sym
);
1506 } END_FOR_EACH_PTR(sym
);
1509 static void call_global_assign_hooks(struct expression
*assign
)
1511 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1514 static void fake_global_assign(struct symbol
*sym
)
1516 struct expression
*assign
, *symbol
;
1518 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1519 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1520 fake_element_assigns(sym
, call_global_assign_hooks
);
1521 } else if (sym
->initializer
) {
1522 symbol
= symbol_expression(sym
);
1523 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1524 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1526 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1528 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1529 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1530 fake_member_assigns(sym
, call_global_assign_hooks
);
1531 } else if (sym
->initializer
) {
1532 symbol
= symbol_expression(sym
);
1533 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1534 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1536 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1539 symbol
= symbol_expression(sym
);
1540 if (sym
->initializer
) {
1541 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1542 __split_expr(assign
);
1544 assign
= assign_expression(symbol
, '=', zero_expr());
1546 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1550 static void start_function_definition(struct symbol
*sym
)
1552 __in_function_def
= 1;
1553 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1554 __in_function_def
= 0;
1555 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1559 static void split_function(struct symbol
*sym
)
1561 struct symbol
*base_type
= get_base_type(sym
);
1562 struct timeval stop
;
1564 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1567 gettimeofday(&outer_fn_start_time
, NULL
);
1568 gettimeofday(&fn_start_time
, NULL
);
1571 cur_func
= sym
->ident
->name
;
1572 set_position(sym
->pos
);
1574 last_goto_statement_handled
= 0;
1575 sm_debug("new function: %s\n", cur_func
);
1577 if (option_two_passes
) {
1581 start_function_definition(sym
);
1582 __split_stmt(base_type
->stmt
);
1583 __split_stmt(base_type
->inline_stmt
);
1589 start_function_definition(sym
);
1590 __split_stmt(base_type
->stmt
);
1591 __split_stmt(base_type
->inline_stmt
);
1592 __pass_to_client(sym
, END_FUNC_HOOK
);
1593 if (need_delayed_scope_hooks())
1594 __call_scope_hooks();
1595 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1599 gettimeofday(&stop
, NULL
);
1600 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1602 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1605 cur_func_sym
= NULL
;
1607 free_data_info_allocs();
1608 free_expression_stack(&switch_expr_stack
);
1609 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1610 __bail_on_rest_of_function
= 0;
1613 static void save_flow_state(void)
1615 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1616 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1617 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1619 __add_ptr_list(&backup
, big_statement_stack
, 0);
1620 __add_ptr_list(&backup
, big_expression_stack
, 0);
1621 __add_ptr_list(&backup
, big_condition_stack
, 0);
1622 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1624 __add_ptr_list(&backup
, cur_func_sym
, 0);
1626 __add_ptr_list(&backup
, __prev_stmt
, 0);
1627 __add_ptr_list(&backup
, __cur_stmt
, 0);
1628 __add_ptr_list(&backup
, __next_stmt
, 0);
1632 static void *pop_backup(void)
1636 ret
= last_ptr_list(backup
);
1637 delete_ptr_list_last(&backup
);
1641 static void restore_flow_state(void)
1643 __next_stmt
= pop_backup();
1644 __cur_stmt
= pop_backup();
1645 __prev_stmt
= pop_backup();
1647 cur_func_sym
= pop_backup();
1648 switch_expr_stack
= pop_backup();
1649 big_condition_stack
= pop_backup();
1650 big_expression_stack
= pop_backup();
1651 big_statement_stack
= pop_backup();
1652 final_pass
= PTR_INT(pop_backup()) >> 2;
1653 loop_count
= PTR_INT(pop_backup()) >> 2;
1654 loop_num
= PTR_INT(pop_backup()) >> 2;
1657 static void parse_inline(struct expression
*call
)
1659 struct symbol
*base_type
;
1660 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1661 struct timeval time_backup
= fn_start_time
;
1662 struct expression
*orig_inline
= __inline_fn
;
1665 if (out_of_memory() || taking_too_long())
1670 __pass_to_client(call
, INLINE_FN_START
);
1671 final_pass
= 0; /* don't print anything */
1673 orig_budget
= inline_budget
;
1674 inline_budget
= inline_budget
- 5;
1676 base_type
= get_base_type(call
->fn
->symbol
);
1677 cur_func_sym
= call
->fn
->symbol
;
1678 if (call
->fn
->symbol
->ident
)
1679 cur_func
= call
->fn
->symbol
->ident
->name
;
1682 set_position(call
->fn
->symbol
->pos
);
1685 big_statement_stack
= NULL
;
1686 big_expression_stack
= NULL
;
1687 big_condition_stack
= NULL
;
1688 switch_expr_stack
= NULL
;
1690 sm_debug("inline function: %s\n", cur_func
);
1694 start_function_definition(call
->fn
->symbol
);
1695 __split_stmt(base_type
->stmt
);
1696 __split_stmt(base_type
->inline_stmt
);
1697 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1698 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1700 free_expression_stack(&switch_expr_stack
);
1701 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1705 restore_flow_state();
1706 fn_start_time
= time_backup
;
1707 cur_func
= cur_func_bak
;
1709 restore_all_states();
1710 set_position(call
->pos
);
1711 __inline_fn
= orig_inline
;
1712 inline_budget
= orig_budget
;
1713 __pass_to_client(call
, INLINE_FN_END
);
1716 static struct symbol_list
*inlines_called
;
1717 static void add_inline_function(struct symbol
*sym
)
1719 static struct symbol_list
*already_added
;
1722 FOR_EACH_PTR(already_added
, tmp
) {
1725 } END_FOR_EACH_PTR(tmp
);
1727 add_ptr_list(&already_added
, sym
);
1728 add_ptr_list(&inlines_called
, sym
);
1731 static void process_inlines(void)
1735 FOR_EACH_PTR(inlines_called
, tmp
) {
1736 split_function(tmp
);
1737 } END_FOR_EACH_PTR(tmp
);
1738 free_ptr_list(&inlines_called
);
1741 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1745 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1748 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1750 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1752 } END_FOR_EACH_PTR_REVERSE(sym
);
1757 static bool interesting_function(struct symbol
*sym
)
1759 static int prev_stream
= -1;
1760 static bool prev_answer
;
1761 const char *filename
;
1764 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1767 if (sym
->pos
.stream
== prev_stream
)
1770 prev_stream
= sym
->pos
.stream
;
1771 prev_answer
= false;
1773 filename
= stream_name(sym
->pos
.stream
);
1774 len
= strlen(filename
);
1775 if (len
> 0 && filename
[len
- 1] == 'c')
1780 static void split_inlines_in_scope(struct symbol
*sym
)
1782 struct symbol
*base
;
1783 struct symbol_list
*scope_list
;
1786 scope_list
= sym
->scope
->symbols
;
1787 stream
= sym
->pos
.stream
;
1789 /* find the last static symbol in the file */
1790 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1791 if (sym
->pos
.stream
!= stream
)
1793 if (sym
->type
!= SYM_NODE
)
1795 base
= get_base_type(sym
);
1798 if (base
->type
!= SYM_FN
)
1800 if (!base
->inline_stmt
)
1802 if (!interesting_function(sym
))
1804 add_inline_function(sym
);
1805 } END_FOR_EACH_PTR_REVERSE(sym
);
1810 static void split_inlines(struct symbol_list
*sym_list
)
1814 sym
= get_last_scoped_symbol(sym_list
, 0);
1816 split_inlines_in_scope(sym
);
1817 sym
= get_last_scoped_symbol(sym_list
, 1);
1819 split_inlines_in_scope(sym
);
1822 static struct stree
*clone_estates_perm(struct stree
*orig
)
1824 struct stree
*ret
= NULL
;
1825 struct sm_state
*tmp
;
1827 FOR_EACH_SM(orig
, tmp
) {
1828 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1829 } END_FOR_EACH_SM(tmp
);
1834 struct position last_pos
;
1835 static void split_c_file_functions(struct symbol_list
*sym_list
)
1840 FOR_EACH_PTR(sym_list
, sym
) {
1841 set_position(sym
->pos
);
1842 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1843 __pass_to_client(sym
, BASE_HOOK
);
1844 fake_global_assign(sym
);
1846 } END_FOR_EACH_PTR(sym
);
1847 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1850 FOR_EACH_PTR(sym_list
, sym
) {
1851 set_position(sym
->pos
);
1852 last_pos
= sym
->pos
;
1853 if (!interesting_function(sym
))
1855 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1856 split_function(sym
);
1859 last_pos
= sym
->pos
;
1860 } END_FOR_EACH_PTR(sym
);
1861 split_inlines(sym_list
);
1862 __pass_to_client(sym_list
, END_FILE_HOOK
);
1865 static int final_before_fake
;
1866 void init_fake_env(void)
1869 final_before_fake
= final_pass
;
1871 __push_fake_cur_stree();
1875 void end_fake_env(void)
1877 __pop_fake_cur_stree();
1880 final_pass
= final_before_fake
;
1883 static void open_output_files(char *base_file
)
1887 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1888 sm_outfd
= fopen(buf
, "w");
1890 sm_fatal("Cannot open %s", buf
);
1895 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1896 sql_outfd
= fopen(buf
, "w");
1898 sm_fatal("Error: Cannot open %s", buf
);
1900 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1901 caller_info_fd
= fopen(buf
, "w");
1902 if (!caller_info_fd
)
1903 sm_fatal("Error: Cannot open %s", buf
);
1906 void smatch(int argc
, char **argv
)
1908 struct string_list
*filelist
= NULL
;
1909 struct symbol_list
*sym_list
;
1910 struct timeval stop
, start
;
1914 gettimeofday(&start
, NULL
);
1916 sparse_initialize(argc
, argv
, &filelist
);
1917 set_valid_ptr_max();
1918 alloc_valid_ptr_rl();
1919 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1920 path
= getcwd(NULL
, 0);
1921 free(full_base_file
);
1923 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
1924 full_base_file
= malloc(len
);
1925 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
1927 full_base_file
= alloc_string(base_file
);
1929 if (option_file_output
)
1930 open_output_files(base_file
);
1931 sym_list
= sparse_keep_tokens(base_file
);
1932 split_c_file_functions(sym_list
);
1933 } END_FOR_EACH_PTR_NOTAG(base_file
);
1935 gettimeofday(&stop
, NULL
);
1937 set_position(last_pos
);
1939 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1941 sm_msg("mem: %luKb", get_max_memory());