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
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
);
81 const sval_t valid_ptr_min_sval
= {
85 const sval_t valid_ptr_max_sval
= {
87 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
89 struct range_list
*valid_ptr_rl
;
91 static void alloc_valid_ptr_rl(void)
93 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
94 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
95 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
98 int outside_of_function(void)
100 return cur_func_sym
== NULL
;
103 const char *get_filename(void)
105 if (option_info
&& option_full_path
)
106 return full_base_file
;
109 if (option_full_path
)
110 return full_filename
;
114 const char *get_base_file(void)
116 if (option_full_path
)
117 return full_base_file
;
121 static void set_position(struct position pos
)
124 static int prev_stream
= -1;
129 if (pos
.stream
== 0 && pos
.line
== 0)
132 __smatch_lineno
= pos
.line
;
134 if (pos
.stream
== prev_stream
)
137 filename
= stream_name(pos
.stream
);
140 pathname
= getcwd(NULL
, 0);
142 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
143 full_filename
= malloc(len
);
144 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
146 full_filename
= alloc_string(filename
);
151 int is_assigned_call(struct expression
*expr
)
153 struct expression
*parent
= expr_get_parent_expr(expr
);
156 parent
->type
== EXPR_ASSIGNMENT
&&
158 strip_expr(parent
->right
) == expr
)
164 static int is_inline_func(struct expression
*expr
)
166 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
168 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
173 static int is_noreturn_func(struct expression
*expr
)
175 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
177 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
182 static int inline_budget
= 20;
184 int inlinable(struct expression
*expr
)
187 struct statement
*last_stmt
= NULL
;
189 if (__inline_fn
) /* don't nest */
192 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
194 if (is_no_inline_function(expr
->symbol
->ident
->name
))
196 sym
= get_base_type(expr
->symbol
);
197 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
198 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
200 if (sym
->stmt
->type
!= STMT_COMPOUND
)
202 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
204 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
205 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
207 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
209 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
215 /* the magic numbers in this function are pulled out of my bum. */
216 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
222 void __process_post_op_stack(void)
224 struct expression
*expr
;
226 FOR_EACH_PTR(post_op_stack
, expr
) {
227 __pass_to_client(expr
, OP_HOOK
);
228 } END_FOR_EACH_PTR(expr
);
230 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
233 static int handle_comma_assigns(struct expression
*expr
)
235 struct expression
*right
;
236 struct expression
*assign
;
238 right
= strip_expr(expr
->right
);
239 if (right
->type
!= EXPR_COMMA
)
242 __split_expr(right
->left
);
243 __process_post_op_stack();
245 assign
= assign_expression(expr
->left
, '=', right
->right
);
246 __split_expr(assign
);
251 /* This is to handle *p++ = foo; assignments */
252 static int handle_postop_assigns(struct expression
*expr
)
254 struct expression
*left
, *fake_left
;
255 struct expression
*assign
;
257 left
= strip_expr(expr
->left
);
258 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
260 left
= strip_expr(left
->unop
);
261 if (left
->type
!= EXPR_POSTOP
)
264 fake_left
= deref_expression(strip_expr(left
->unop
));
265 assign
= assign_expression(fake_left
, '=', expr
->right
);
267 __split_expr(assign
);
268 __split_expr(expr
->left
);
273 static int prev_expression_is_getting_address(struct expression
*expr
)
275 struct expression
*parent
;
278 parent
= expr_get_parent_expr(expr
);
282 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
284 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
286 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
295 static void handle_builtin_overflow_func(struct expression
*expr
)
297 struct expression
*a
, *b
, *res
, *assign
;
300 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
302 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
304 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
309 a
= get_argument_from_call_expr(expr
->args
, 0);
310 b
= get_argument_from_call_expr(expr
->args
, 1);
311 res
= get_argument_from_call_expr(expr
->args
, 2);
313 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
314 __split_expr(assign
);
317 static int handle__builtin_choose_expr(struct expression
*expr
)
319 struct expression
*const_expr
, *expr1
, *expr2
;
322 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
325 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
326 expr1
= get_argument_from_call_expr(expr
->args
, 1);
327 expr2
= get_argument_from_call_expr(expr
->args
, 2);
329 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
338 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
340 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
343 right
= strip_expr(expr
->right
);
344 if (right
->type
!= EXPR_CALL
)
346 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
349 const_expr
= get_argument_from_call_expr(right
->args
, 0);
350 expr1
= get_argument_from_call_expr(right
->args
, 1);
351 expr2
= get_argument_from_call_expr(right
->args
, 2);
353 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
356 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
361 void __split_expr(struct expression
*expr
)
366 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
368 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
370 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
373 push_expression(&big_expression_stack
, expr
);
374 set_position(expr
->pos
);
375 __pass_to_client(expr
, EXPR_HOOK
);
377 switch (expr
->type
) {
379 expr_set_parent_expr(expr
->unop
, expr
);
381 if (expr
->op
== '*' &&
382 !prev_expression_is_getting_address(expr
))
383 __pass_to_client(expr
, DEREF_HOOK
);
384 __split_expr(expr
->unop
);
385 __pass_to_client(expr
, OP_HOOK
);
388 expr_set_parent_expr(expr
->unop
, expr
);
390 __split_expr(expr
->unop
);
391 push_expression(&post_op_stack
, expr
);
395 if (expr
->statement
&& !expr
->statement
) {
396 stmt_set_parent_stmt(expr
->statement
,
397 last_ptr_list((struct ptr_list
*)big_statement_stack
));
399 __split_stmt(expr
->statement
);
404 expr_set_parent_expr(expr
->left
, expr
);
405 expr_set_parent_expr(expr
->right
, expr
);
407 __pass_to_client(expr
, LOGIC_HOOK
);
408 __handle_logic(expr
);
411 expr_set_parent_expr(expr
->left
, expr
);
412 expr_set_parent_expr(expr
->right
, expr
);
414 __pass_to_client(expr
, BINOP_HOOK
);
416 expr_set_parent_expr(expr
->left
, expr
);
417 expr_set_parent_expr(expr
->right
, expr
);
419 __split_expr(expr
->left
);
420 __process_post_op_stack();
421 __split_expr(expr
->right
);
423 case EXPR_ASSIGNMENT
: {
424 struct expression
*right
;
426 expr_set_parent_expr(expr
->left
, expr
);
427 expr_set_parent_expr(expr
->right
, expr
);
429 right
= strip_expr(expr
->right
);
433 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
436 if (__handle_condition_assigns(expr
))
438 /* foo = (x < 5 ? foo : 5); */
439 if (__handle_select_assigns(expr
))
441 /* foo = ({frob(); frob(); frob(); 1;}) */
442 if (__handle_expr_statement_assigns(expr
))
445 if (handle_comma_assigns(expr
))
447 if (handle_postop_assigns(expr
))
449 if (handle__builtin_choose_expr_assigns(expr
))
452 __split_expr(expr
->right
);
453 if (outside_of_function())
454 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
456 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
458 __fake_struct_member_assignments(expr
);
460 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
461 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
463 if (get_macro_name(right
->pos
) &&
464 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
465 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
467 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
469 __split_expr(expr
->left
);
473 expr_set_parent_expr(expr
->deref
, expr
);
475 __pass_to_client(expr
, DEREF_HOOK
);
476 __split_expr(expr
->deref
);
479 expr_set_parent_expr(expr
->base
, expr
);
481 __split_expr(expr
->base
);
484 case EXPR_FORCE_CAST
:
485 expr_set_parent_expr(expr
->cast_expression
, expr
);
487 __pass_to_client(expr
, CAST_HOOK
);
488 __split_expr(expr
->cast_expression
);
491 if (expr
->cast_expression
)
492 __pass_to_client(strip_parens(expr
->cast_expression
),
498 case EXPR_CONDITIONAL
:
500 expr_set_parent_expr(expr
->conditional
, expr
);
501 expr_set_parent_expr(expr
->cond_true
, expr
);
502 expr_set_parent_expr(expr
->cond_false
, expr
);
504 if (known_condition_true(expr
->conditional
)) {
505 __split_expr(expr
->cond_true
);
508 if (known_condition_false(expr
->conditional
)) {
509 __split_expr(expr
->cond_false
);
512 __pass_to_client(expr
, SELECT_HOOK
);
513 __split_whole_condition(expr
->conditional
);
514 __split_expr(expr
->cond_true
);
515 __push_true_states();
516 __use_false_states();
517 __split_expr(expr
->cond_false
);
518 __merge_true_states();
521 expr_set_parent_expr(expr
->fn
, expr
);
523 if (sym_name_is("__builtin_constant_p", expr
->fn
))
525 if (handle__builtin_choose_expr(expr
))
527 split_expr_list(expr
->args
, expr
);
528 __split_expr(expr
->fn
);
529 if (is_inline_func(expr
->fn
))
530 add_inline_function(expr
->fn
->symbol
);
531 if (inlinable(expr
->fn
))
533 __process_post_op_stack();
534 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
535 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
537 if (inlinable(expr
->fn
)) {
540 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
541 if (is_noreturn_func(expr
->fn
))
543 handle_builtin_overflow_func(expr
);
545 case EXPR_INITIALIZER
:
546 split_expr_list(expr
->expr_list
, expr
);
548 case EXPR_IDENTIFIER
:
549 expr_set_parent_expr(expr
->ident_expression
, expr
);
550 __split_expr(expr
->ident_expression
);
553 expr_set_parent_expr(expr
->idx_expression
, expr
);
554 __split_expr(expr
->idx_expression
);
557 expr_set_parent_expr(expr
->init_expr
, expr
);
558 __split_expr(expr
->init_expr
);
561 __pass_to_client(expr
, SYM_HOOK
);
564 __pass_to_client(expr
, STRING_HOOK
);
569 pop_expression(&big_expression_stack
);
572 static int is_forever_loop(struct statement
*stmt
)
574 struct expression
*expr
;
577 expr
= strip_expr(stmt
->iterator_pre_condition
);
579 expr
= stmt
->iterator_post_condition
;
581 /* this is a for(;;) loop... */
585 if (get_value(expr
, &sval
) && sval
.value
!= 0)
592 static char *get_loop_name(int num
)
596 snprintf(buf
, 255, "-loop%d", num
);
598 return alloc_sname(buf
);
602 * Pre Loops are while and for loops.
604 static void handle_pre_loop(struct statement
*stmt
)
606 int once_through
; /* we go through the loop at least once */
607 struct sm_state
*extra_sm
= NULL
;
610 struct stree
*stree
= NULL
;
611 struct sm_state
*sm
= NULL
;
613 loop_name
= get_loop_name(loop_num
);
616 __split_stmt(stmt
->iterator_pre_statement
);
617 __prev_stmt
= stmt
->iterator_pre_statement
;
619 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
625 __merge_gotos(loop_name
, NULL
);
627 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
628 __in_pre_condition
++;
629 __pass_to_client(stmt
, PRELOOP_HOOK
);
630 __split_whole_condition(stmt
->iterator_pre_condition
);
631 __in_pre_condition
--;
632 FOR_EACH_SM(stree
, sm
) {
633 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
634 } END_FOR_EACH_SM(sm
);
637 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
639 if (option_assume_loops
)
642 __split_stmt(stmt
->iterator_statement
);
643 if (is_forever_loop(stmt
)) {
645 __save_gotos(loop_name
, NULL
);
647 __push_fake_cur_stree();
648 __split_stmt(stmt
->iterator_post_statement
);
649 stree
= __pop_fake_cur_stree();
651 __discard_false_states();
654 if (!__path_is_null())
655 __merge_stree_into_cur(stree
);
659 unchanged
= __iterator_unchanged(extra_sm
);
660 __split_stmt(stmt
->iterator_post_statement
);
661 __prev_stmt
= stmt
->iterator_post_statement
;
664 __save_gotos(loop_name
, NULL
);
665 __in_pre_condition
++;
666 __split_whole_condition(stmt
->iterator_pre_condition
);
667 __in_pre_condition
--;
669 __merge_false_states();
671 __discard_false_states();
673 __merge_false_states();
675 if (extra_sm
&& unchanged
)
676 __extra_pre_loop_hook_after(extra_sm
,
677 stmt
->iterator_post_statement
,
678 stmt
->iterator_pre_condition
);
685 * Post loops are do {} while();
687 static void handle_post_loop(struct statement
*stmt
)
691 loop_name
= get_loop_name(loop_num
);
697 __merge_gotos(loop_name
, NULL
);
698 __split_stmt(stmt
->iterator_statement
);
700 if (!is_zero(stmt
->iterator_post_condition
))
701 __save_gotos(loop_name
, NULL
);
703 if (is_forever_loop(stmt
)) {
706 __split_whole_condition(stmt
->iterator_post_condition
);
707 __use_false_states();
713 static int empty_statement(struct statement
*stmt
)
717 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
722 static int last_stmt_on_same_line(void)
724 struct statement
*stmt
;
727 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
730 if (stmt
->pos
.line
== get_lineno())
733 } END_FOR_EACH_PTR_REVERSE(stmt
);
737 static void split_asm_constraints(struct expression_list
*expr_list
)
739 struct expression
*expr
;
742 FOR_EACH_PTR(expr_list
, expr
) {
744 case 0: /* identifier */
745 case 1: /* constraint */
748 case 2: /* expression */
753 } END_FOR_EACH_PTR(expr
);
756 static int is_case_val(struct statement
*stmt
, sval_t sval
)
760 if (stmt
->type
!= STMT_CASE
)
762 if (!stmt
->case_expression
) {
766 if (!get_value(stmt
->case_expression
, &case_sval
))
768 if (case_sval
.value
== sval
.value
)
773 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
774 struct expression
*case_expr
,
775 struct expression
*case_to
)
778 struct range_list
*rl
= NULL
;
779 struct symbol
*switch_type
;
781 switch_type
= get_type(switch_expr
);
782 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
783 start
= sval_cast(switch_type
, start
);
784 end
= sval_cast(switch_type
, end
);
785 add_range(&rl
, start
, end
);
786 } else if (get_value(case_expr
, &start
)) {
787 start
= sval_cast(switch_type
, start
);
788 add_range(&rl
, start
, start
);
794 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
796 struct statement
*tmp
;
797 struct range_list
*rl
;
799 __split_expr(stmt
->switch_expression
);
800 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
802 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
803 __save_switch_states(top_expression(switch_expr_stack
));
808 stmt
= stmt
->switch_statement
;
810 __push_scope_hooks();
811 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
812 __smatch_lineno
= tmp
->pos
.line
;
813 if (is_case_val(tmp
, sval
)) {
814 rl
= alloc_rl(sval
, sval
);
815 __merge_switches(top_expression(switch_expr_stack
), rl
);
816 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
818 if (__path_is_null())
821 if (__path_is_null()) {
825 } END_FOR_EACH_PTR(tmp
);
827 __call_scope_hooks();
828 if (!__pop_default())
829 __merge_switches(top_expression(switch_expr_stack
), NULL
);
830 __discard_switches();
832 pop_expression(&switch_expr_stack
);
835 static void split_case(struct statement
*stmt
)
837 struct range_list
*rl
= NULL
;
839 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
840 expr_set_parent_stmt(stmt
->case_to
, stmt
);
842 rl
= get_case_rl(top_expression(switch_expr_stack
),
843 stmt
->case_expression
, stmt
->case_to
);
844 while (stmt
->case_statement
->type
== STMT_CASE
) {
845 struct range_list
*tmp
;
847 tmp
= get_case_rl(top_expression(switch_expr_stack
),
848 stmt
->case_statement
->case_expression
,
849 stmt
->case_statement
->case_to
);
852 rl
= rl_union(rl
, tmp
);
853 if (!stmt
->case_expression
)
855 stmt
= stmt
->case_statement
;
858 __merge_switches(top_expression(switch_expr_stack
), rl
);
860 if (!stmt
->case_expression
)
862 __split_stmt(stmt
->case_statement
);
865 int time_parsing_function(void)
867 return ms_since(&fn_start_time
) / 1000;
870 static int taking_too_long(void)
872 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
877 static int is_last_stmt(struct statement
*cur_stmt
)
880 struct statement
*stmt
;
884 fn
= get_base_type(cur_func_sym
);
889 stmt
= fn
->inline_stmt
;
890 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
892 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
893 if (stmt
&& stmt
->type
== STMT_LABEL
)
894 stmt
= stmt
->label_statement
;
895 if (stmt
== cur_stmt
)
900 static void handle_backward_goto(struct statement
*goto_stmt
)
902 const char *goto_name
, *label_name
;
903 struct statement
*func_stmt
;
904 struct symbol
*base_type
= get_base_type(cur_func_sym
);
905 struct statement
*tmp
;
910 if (last_goto_statement_handled
)
912 last_goto_statement_handled
= 1;
914 if (!goto_stmt
->goto_label
||
915 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
916 !goto_stmt
->goto_label
->ident
)
918 goto_name
= goto_stmt
->goto_label
->ident
->name
;
920 func_stmt
= base_type
->stmt
;
922 func_stmt
= base_type
->inline_stmt
;
925 if (func_stmt
->type
!= STMT_COMPOUND
)
928 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
930 if (tmp
->type
!= STMT_LABEL
)
932 if (!tmp
->label_identifier
||
933 tmp
->label_identifier
->type
!= SYM_LABEL
||
934 !tmp
->label_identifier
->ident
)
936 label_name
= tmp
->label_identifier
->ident
->name
;
937 if (strcmp(goto_name
, label_name
) != 0)
942 } END_FOR_EACH_PTR(tmp
);
945 static void fake_a_return(void)
947 struct symbol
*return_type
;
952 return_type
= get_real_base_type(cur_func_sym
);
953 return_type
= get_real_base_type(return_type
);
954 if (return_type
!= &void_ctype
) {
955 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
960 static void fake_an_empty_default(struct position pos
)
962 static struct statement none
= {};
965 none
.type
= STMT_NONE
;
966 __merge_switches(top_expression(switch_expr_stack
), NULL
);
970 static void split_compound(struct statement
*stmt
)
972 struct statement
*prev
= NULL
;
973 struct statement
*cur
= NULL
;
974 struct statement
*next
;
976 __push_scope_hooks();
978 FOR_EACH_PTR(stmt
->stmts
, next
) {
979 /* just set them all ahead of time */
980 stmt_set_parent_stmt(next
, stmt
);
990 } END_FOR_EACH_PTR(next
);
999 * For function scope, then delay calling the scope hooks until the
1000 * end of function hooks can run. I'm not positive this is the right
1003 if (!is_last_stmt(cur
))
1004 __call_scope_hooks();
1008 * This is a hack, work around for detecting empty functions.
1010 static int need_delayed_scope_hooks(void)
1012 struct symbol
*fn
= get_base_type(cur_func_sym
);
1013 struct statement
*stmt
;
1019 stmt
= fn
->inline_stmt
;
1020 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1025 void __split_label_stmt(struct statement
*stmt
)
1027 if (stmt
->label_identifier
&&
1028 stmt
->label_identifier
->type
== SYM_LABEL
&&
1029 stmt
->label_identifier
->ident
) {
1030 loop_count
|= 0x0800000;
1031 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1035 static void find_asm_gotos(struct statement
*stmt
)
1039 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1040 __save_gotos(sym
->ident
->name
, sym
);
1041 } END_FOR_EACH_PTR(sym
);
1044 void __split_stmt(struct statement
*stmt
)
1051 if (!__in_fake_assign
)
1052 __silence_warnings_for_stmt
= false;
1054 if (__bail_on_rest_of_function
|| is_skipped_function())
1057 if (out_of_memory() || taking_too_long()) {
1058 struct timeval stop
;
1060 gettimeofday(&stop
, NULL
);
1062 __bail_on_rest_of_function
= 1;
1064 sm_perror("Function too hairy. Giving up. %lu seconds",
1065 stop
.tv_sec
- fn_start_time
.tv_sec
);
1067 final_pass
= 0; /* turn off sm_msg() from here */
1071 add_ptr_list(&big_statement_stack
, stmt
);
1072 free_expression_stack(&big_expression_stack
);
1073 set_position(stmt
->pos
);
1074 __pass_to_client(stmt
, STMT_HOOK
);
1076 switch (stmt
->type
) {
1077 case STMT_DECLARATION
:
1078 split_declaration(stmt
->declaration
);
1081 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1083 __split_expr(stmt
->ret_value
);
1084 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1085 __process_post_op_stack();
1088 case STMT_EXPRESSION
:
1089 expr_set_parent_stmt(stmt
->expression
, stmt
);
1090 expr_set_parent_stmt(stmt
->context
, stmt
);
1092 __split_expr(stmt
->expression
);
1095 split_compound(stmt
);
1098 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1099 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1100 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1102 if (known_condition_true(stmt
->if_conditional
)) {
1103 __split_stmt(stmt
->if_true
);
1106 if (known_condition_false(stmt
->if_conditional
)) {
1107 __split_stmt(stmt
->if_false
);
1110 __split_whole_condition(stmt
->if_conditional
);
1111 __split_stmt(stmt
->if_true
);
1112 if (empty_statement(stmt
->if_true
) &&
1113 last_stmt_on_same_line() &&
1114 !get_macro_name(stmt
->if_true
->pos
))
1115 sm_warning("if();");
1116 __push_true_states();
1117 __use_false_states();
1118 __split_stmt(stmt
->if_false
);
1119 __merge_true_states();
1122 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1123 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1124 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1125 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1126 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1128 if (stmt
->iterator_pre_condition
)
1129 handle_pre_loop(stmt
);
1130 else if (stmt
->iterator_post_condition
)
1131 handle_post_loop(stmt
);
1133 // these are for(;;) type loops.
1134 handle_pre_loop(stmt
);
1138 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1139 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1141 if (get_value(stmt
->switch_expression
, &sval
)) {
1142 split_known_switch(stmt
, sval
);
1145 __split_expr(stmt
->switch_expression
);
1146 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1147 __save_switch_states(top_expression(switch_expr_stack
));
1151 __split_stmt(stmt
->switch_statement
);
1152 if (!__pop_default() && have_remaining_cases())
1153 fake_an_empty_default(stmt
->pos
);
1154 __discard_switches();
1156 pop_expression(&switch_expr_stack
);
1162 __split_label_stmt(stmt
);
1163 __split_stmt(stmt
->label_statement
);
1166 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1168 __split_expr(stmt
->goto_expression
);
1169 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1170 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1172 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1174 __process_continues();
1176 } else if (stmt
->goto_label
&&
1177 stmt
->goto_label
->type
== SYM_LABEL
&&
1178 stmt
->goto_label
->ident
) {
1179 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1182 if (is_last_stmt(stmt
))
1183 handle_backward_goto(stmt
);
1188 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1190 find_asm_gotos(stmt
);
1191 __pass_to_client(stmt
, ASM_HOOK
);
1192 __split_expr(stmt
->asm_string
);
1193 split_asm_constraints(stmt
->asm_outputs
);
1194 split_asm_constraints(stmt
->asm_inputs
);
1195 split_asm_constraints(stmt
->asm_clobbers
);
1200 __split_expr(stmt
->range_expression
);
1201 __split_expr(stmt
->range_low
);
1202 __split_expr(stmt
->range_high
);
1205 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1207 __process_post_op_stack();
1210 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1212 struct expression
*expr
;
1214 FOR_EACH_PTR(expr_list
, expr
) {
1215 expr_set_parent_expr(expr
, parent
);
1217 __process_post_op_stack();
1218 } END_FOR_EACH_PTR(expr
);
1221 static void split_sym(struct symbol
*sym
)
1225 if (!(sym
->namespace & NS_SYMBOL
))
1228 __split_stmt(sym
->stmt
);
1229 __split_expr(sym
->array_size
);
1230 split_symlist(sym
->arguments
);
1231 split_symlist(sym
->symbol_list
);
1232 __split_stmt(sym
->inline_stmt
);
1233 split_symlist(sym
->inline_symbol_list
);
1236 static void split_symlist(struct symbol_list
*sym_list
)
1240 FOR_EACH_PTR(sym_list
, sym
) {
1242 } END_FOR_EACH_PTR(sym
);
1245 typedef void (fake_cb
)(struct expression
*expr
);
1247 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1249 struct symbol
*type
, *tmp
;
1255 name
= member
->name
;
1257 type
= get_type(expr
);
1258 if (!type
|| type
->type
!= SYM_STRUCT
)
1262 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1266 if (strcmp(name
, tmp
->ident
->name
) == 0)
1268 } END_FOR_EACH_PTR(tmp
);
1272 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1274 struct symbol
*type
, *member
;
1277 type
= get_type(expr
);
1278 if (!type
|| type
->type
!= SYM_STRUCT
)
1281 FOR_EACH_PTR(type
->symbol_list
, member
) {
1283 return member
->ident
;
1285 } END_FOR_EACH_PTR(member
);
1289 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1291 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1293 struct expression
*edge_member
, *assign
;
1294 struct symbol
*base
= get_real_base_type(member
);
1298 expr
= member_expression(expr
, '.', member
->ident
);
1300 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1301 struct symbol
*type
;
1303 type
= get_real_base_type(tmp
);
1307 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1308 if (get_extra_state(edge_member
))
1311 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1312 set_inner_struct_members(expr
, tmp
);
1319 assign
= assign_expression(edge_member
, '=', zero_expr());
1320 __split_expr(assign
);
1321 } END_FOR_EACH_PTR(tmp
);
1326 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1329 struct expression
*member
= NULL
;
1330 struct expression
*assign
;
1333 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1334 expr
= strip_expr(expr
->unop
);
1338 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1339 type
= get_real_base_type(tmp
);
1344 member
= member_expression(expr
, op
, tmp
->ident
);
1345 if (get_extra_state(member
))
1349 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1350 set_inner_struct_members(expr
, tmp
);
1353 if (type
->type
== SYM_ARRAY
)
1358 assign
= assign_expression(member
, '=', zero_expr());
1359 __split_expr(assign
);
1360 } END_FOR_EACH_PTR(tmp
);
1363 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1365 struct expression
*deref
, *assign
, *tmp
, *right
;
1366 struct symbol
*struct_type
, *type
;
1367 struct ident
*member
;
1370 struct_type
= get_type(symbol
);
1372 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1376 * We're parsing an initializer that could look something like this:
1377 * struct foo foo = {
1379 * .whatever.xxx = 11,
1383 * So what we have here is a list with 42, .whatever, and .zzz. We need
1384 * to break it up into left and right sides of the assignments.
1388 FOR_EACH_PTR(members
, tmp
) {
1390 if (tmp
->type
== EXPR_IDENTIFIER
) {
1391 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1392 while (tmp
->type
== EXPR_IDENTIFIER
) {
1393 member
= tmp
->expr_ident
;
1394 tmp
= tmp
->ident_expression
;
1396 deref
= member_expression(deref
, '.', member
);
1398 deref
= member_expression(symbol
, '.', member
);
1401 member
= number_to_member(symbol
, member_idx
);
1402 deref
= member_expression(symbol
, '.', member
);
1406 if (right
->type
== EXPR_INITIALIZER
) {
1407 type
= get_type(deref
);
1408 if (type
&& type
->type
== SYM_ARRAY
)
1409 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1411 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1413 assign
= assign_expression(deref
, '=', right
);
1416 } END_FOR_EACH_PTR(tmp
);
1418 set_unset_to_zero(struct_type
, symbol
);
1421 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1423 fake_member_assigns_helper(symbol_expression(sym
),
1424 sym
->initializer
->expr_list
, fake_cb
);
1427 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1429 struct expression
*offset
, *binop
, *assign
, *tmp
;
1430 struct symbol
*type
;
1433 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1437 FOR_EACH_PTR(expr_list
, tmp
) {
1438 if (tmp
->type
== EXPR_INDEX
) {
1439 if (tmp
->idx_from
!= tmp
->idx_to
)
1441 idx
= tmp
->idx_from
;
1442 if (!tmp
->idx_expression
)
1444 tmp
= tmp
->idx_expression
;
1446 offset
= value_expr(idx
);
1447 binop
= array_element_expression(array
, offset
);
1448 if (tmp
->type
== EXPR_INITIALIZER
) {
1449 type
= get_type(binop
);
1450 if (type
&& type
->type
== SYM_ARRAY
)
1451 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1453 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1455 assign
= assign_expression(binop
, '=', tmp
);
1460 } END_FOR_EACH_PTR(tmp
);
1463 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1465 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1468 static void fake_assign_expr(struct symbol
*sym
)
1470 struct expression
*assign
, *symbol
;
1472 symbol
= symbol_expression(sym
);
1473 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1474 __split_expr(assign
);
1477 static void do_initializer_stuff(struct symbol
*sym
)
1479 if (!sym
->initializer
)
1482 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1483 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1484 fake_element_assigns(sym
, __split_expr
);
1486 fake_member_assigns(sym
, __split_expr
);
1488 fake_assign_expr(sym
);
1492 static void split_declaration(struct symbol_list
*sym_list
)
1496 FOR_EACH_PTR(sym_list
, sym
) {
1497 __pass_to_client(sym
, DECLARATION_HOOK
);
1498 do_initializer_stuff(sym
);
1500 } END_FOR_EACH_PTR(sym
);
1503 static void call_global_assign_hooks(struct expression
*assign
)
1505 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1508 static void fake_global_assign(struct symbol
*sym
)
1510 struct expression
*assign
, *symbol
;
1512 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1513 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1514 fake_element_assigns(sym
, call_global_assign_hooks
);
1515 } else if (sym
->initializer
) {
1516 symbol
= symbol_expression(sym
);
1517 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1518 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1520 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1522 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1523 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1524 fake_member_assigns(sym
, call_global_assign_hooks
);
1525 } else if (sym
->initializer
) {
1526 symbol
= symbol_expression(sym
);
1527 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1528 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1530 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1533 symbol
= symbol_expression(sym
);
1534 if (sym
->initializer
) {
1535 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1536 __split_expr(assign
);
1538 assign
= assign_expression(symbol
, '=', zero_expr());
1540 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1544 static void start_function_definition(struct symbol
*sym
)
1546 __in_function_def
= 1;
1547 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1548 __in_function_def
= 0;
1549 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1553 static void split_function(struct symbol
*sym
)
1555 struct symbol
*base_type
= get_base_type(sym
);
1556 struct timeval stop
;
1558 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1561 gettimeofday(&outer_fn_start_time
, NULL
);
1562 gettimeofday(&fn_start_time
, NULL
);
1565 cur_func
= sym
->ident
->name
;
1566 set_position(sym
->pos
);
1568 last_goto_statement_handled
= 0;
1569 sm_debug("new function: %s\n", cur_func
);
1571 if (option_two_passes
) {
1575 start_function_definition(sym
);
1576 __split_stmt(base_type
->stmt
);
1577 __split_stmt(base_type
->inline_stmt
);
1583 start_function_definition(sym
);
1584 __split_stmt(base_type
->stmt
);
1585 __split_stmt(base_type
->inline_stmt
);
1586 __pass_to_client(sym
, END_FUNC_HOOK
);
1587 if (need_delayed_scope_hooks())
1588 __call_scope_hooks();
1589 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1593 gettimeofday(&stop
, NULL
);
1594 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1596 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1599 cur_func_sym
= NULL
;
1601 free_data_info_allocs();
1602 free_expression_stack(&switch_expr_stack
);
1603 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1604 __bail_on_rest_of_function
= 0;
1607 static void save_flow_state(void)
1609 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1610 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1611 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1613 __add_ptr_list(&backup
, big_statement_stack
, 0);
1614 __add_ptr_list(&backup
, big_expression_stack
, 0);
1615 __add_ptr_list(&backup
, big_condition_stack
, 0);
1616 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1618 __add_ptr_list(&backup
, cur_func_sym
, 0);
1620 __add_ptr_list(&backup
, __prev_stmt
, 0);
1621 __add_ptr_list(&backup
, __cur_stmt
, 0);
1622 __add_ptr_list(&backup
, __next_stmt
, 0);
1626 static void *pop_backup(void)
1630 ret
= last_ptr_list(backup
);
1631 delete_ptr_list_last(&backup
);
1635 static void restore_flow_state(void)
1637 __next_stmt
= pop_backup();
1638 __cur_stmt
= pop_backup();
1639 __prev_stmt
= pop_backup();
1641 cur_func_sym
= pop_backup();
1642 switch_expr_stack
= pop_backup();
1643 big_condition_stack
= pop_backup();
1644 big_expression_stack
= pop_backup();
1645 big_statement_stack
= pop_backup();
1646 final_pass
= PTR_INT(pop_backup()) >> 2;
1647 loop_count
= PTR_INT(pop_backup()) >> 2;
1648 loop_num
= PTR_INT(pop_backup()) >> 2;
1651 static void parse_inline(struct expression
*call
)
1653 struct symbol
*base_type
;
1654 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1655 struct timeval time_backup
= fn_start_time
;
1656 struct expression
*orig_inline
= __inline_fn
;
1659 if (out_of_memory() || taking_too_long())
1664 __pass_to_client(call
, INLINE_FN_START
);
1665 final_pass
= 0; /* don't print anything */
1667 orig_budget
= inline_budget
;
1668 inline_budget
= inline_budget
- 5;
1670 base_type
= get_base_type(call
->fn
->symbol
);
1671 cur_func_sym
= call
->fn
->symbol
;
1672 if (call
->fn
->symbol
->ident
)
1673 cur_func
= call
->fn
->symbol
->ident
->name
;
1676 set_position(call
->fn
->symbol
->pos
);
1679 big_statement_stack
= NULL
;
1680 big_expression_stack
= NULL
;
1681 big_condition_stack
= NULL
;
1682 switch_expr_stack
= NULL
;
1684 sm_debug("inline function: %s\n", cur_func
);
1688 start_function_definition(call
->fn
->symbol
);
1689 __split_stmt(base_type
->stmt
);
1690 __split_stmt(base_type
->inline_stmt
);
1691 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1692 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1694 free_expression_stack(&switch_expr_stack
);
1695 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1699 restore_flow_state();
1700 fn_start_time
= time_backup
;
1701 cur_func
= cur_func_bak
;
1703 restore_all_states();
1704 set_position(call
->pos
);
1705 __inline_fn
= orig_inline
;
1706 inline_budget
= orig_budget
;
1707 __pass_to_client(call
, INLINE_FN_END
);
1710 static struct symbol_list
*inlines_called
;
1711 static void add_inline_function(struct symbol
*sym
)
1713 static struct symbol_list
*already_added
;
1716 FOR_EACH_PTR(already_added
, tmp
) {
1719 } END_FOR_EACH_PTR(tmp
);
1721 add_ptr_list(&already_added
, sym
);
1722 add_ptr_list(&inlines_called
, sym
);
1725 static void process_inlines(void)
1729 FOR_EACH_PTR(inlines_called
, tmp
) {
1730 split_function(tmp
);
1731 } END_FOR_EACH_PTR(tmp
);
1732 free_ptr_list(&inlines_called
);
1735 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1739 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1742 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1744 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1746 } END_FOR_EACH_PTR_REVERSE(sym
);
1751 static bool interesting_function(struct symbol
*sym
)
1753 static int prev_stream
= -1;
1754 static bool prev_answer
;
1755 const char *filename
;
1758 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1761 if (sym
->pos
.stream
== prev_stream
)
1764 prev_stream
= sym
->pos
.stream
;
1765 prev_answer
= false;
1767 filename
= stream_name(sym
->pos
.stream
);
1768 len
= strlen(filename
);
1769 if (len
> 0 && filename
[len
- 1] == 'c')
1774 static void split_inlines_in_scope(struct symbol
*sym
)
1776 struct symbol
*base
;
1777 struct symbol_list
*scope_list
;
1780 scope_list
= sym
->scope
->symbols
;
1781 stream
= sym
->pos
.stream
;
1783 /* find the last static symbol in the file */
1784 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1785 if (sym
->pos
.stream
!= stream
)
1787 if (sym
->type
!= SYM_NODE
)
1789 base
= get_base_type(sym
);
1792 if (base
->type
!= SYM_FN
)
1794 if (!base
->inline_stmt
)
1796 if (!interesting_function(sym
))
1798 add_inline_function(sym
);
1799 } END_FOR_EACH_PTR_REVERSE(sym
);
1804 static void split_inlines(struct symbol_list
*sym_list
)
1808 sym
= get_last_scoped_symbol(sym_list
, 0);
1810 split_inlines_in_scope(sym
);
1811 sym
= get_last_scoped_symbol(sym_list
, 1);
1813 split_inlines_in_scope(sym
);
1816 static struct stree
*clone_estates_perm(struct stree
*orig
)
1818 struct stree
*ret
= NULL
;
1819 struct sm_state
*tmp
;
1821 FOR_EACH_SM(orig
, tmp
) {
1822 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1823 } END_FOR_EACH_SM(tmp
);
1828 struct position last_pos
;
1829 static void split_c_file_functions(struct symbol_list
*sym_list
)
1834 FOR_EACH_PTR(sym_list
, sym
) {
1835 set_position(sym
->pos
);
1836 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1837 __pass_to_client(sym
, BASE_HOOK
);
1838 fake_global_assign(sym
);
1840 } END_FOR_EACH_PTR(sym
);
1841 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1844 FOR_EACH_PTR(sym_list
, sym
) {
1845 set_position(sym
->pos
);
1846 last_pos
= sym
->pos
;
1847 if (!interesting_function(sym
))
1849 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1850 split_function(sym
);
1853 last_pos
= sym
->pos
;
1854 } END_FOR_EACH_PTR(sym
);
1855 split_inlines(sym_list
);
1856 __pass_to_client(sym_list
, END_FILE_HOOK
);
1859 static int final_before_fake
;
1860 void init_fake_env(void)
1863 final_before_fake
= final_pass
;
1865 __push_fake_cur_stree();
1869 void end_fake_env(void)
1871 __pop_fake_cur_stree();
1874 final_pass
= final_before_fake
;
1877 static void open_output_files(char *base_file
)
1881 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1882 sm_outfd
= fopen(buf
, "w");
1884 sm_fatal("Cannot open %s", buf
);
1889 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1890 sql_outfd
= fopen(buf
, "w");
1892 sm_fatal("Error: Cannot open %s", buf
);
1894 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1895 caller_info_fd
= fopen(buf
, "w");
1896 if (!caller_info_fd
)
1897 sm_fatal("Error: Cannot open %s", buf
);
1900 void smatch(int argc
, char **argv
)
1902 struct string_list
*filelist
= NULL
;
1903 struct symbol_list
*sym_list
;
1904 struct timeval stop
, start
;
1908 gettimeofday(&start
, NULL
);
1910 sparse_initialize(argc
, argv
, &filelist
);
1911 alloc_valid_ptr_rl();
1912 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1913 path
= getcwd(NULL
, 0);
1914 free(full_base_file
);
1916 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
1917 full_base_file
= malloc(len
);
1918 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
1920 full_base_file
= alloc_string(base_file
);
1922 if (option_file_output
)
1923 open_output_files(base_file
);
1924 sym_list
= sparse_keep_tokens(base_file
);
1925 split_c_file_functions(sym_list
);
1926 } END_FOR_EACH_PTR_NOTAG(base_file
);
1928 gettimeofday(&stop
, NULL
);
1930 set_position(last_pos
);
1933 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1935 sm_msg("mem: %luKb", get_max_memory());