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
),
497 evaluate_expression(expr
);
499 case EXPR_CONDITIONAL
:
501 expr_set_parent_expr(expr
->conditional
, expr
);
502 expr_set_parent_expr(expr
->cond_true
, expr
);
503 expr_set_parent_expr(expr
->cond_false
, expr
);
505 if (known_condition_true(expr
->conditional
)) {
506 __split_expr(expr
->cond_true
);
509 if (known_condition_false(expr
->conditional
)) {
510 __split_expr(expr
->cond_false
);
513 __pass_to_client(expr
, SELECT_HOOK
);
514 __split_whole_condition(expr
->conditional
);
515 __split_expr(expr
->cond_true
);
516 __push_true_states();
517 __use_false_states();
518 __split_expr(expr
->cond_false
);
519 __merge_true_states();
522 expr_set_parent_expr(expr
->fn
, expr
);
524 if (sym_name_is("__builtin_constant_p", expr
->fn
))
526 if (handle__builtin_choose_expr(expr
))
528 split_expr_list(expr
->args
, expr
);
529 __split_expr(expr
->fn
);
530 if (is_inline_func(expr
->fn
))
531 add_inline_function(expr
->fn
->symbol
);
532 if (inlinable(expr
->fn
))
534 __process_post_op_stack();
535 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
536 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
538 if (inlinable(expr
->fn
)) {
541 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
542 if (is_noreturn_func(expr
->fn
))
544 handle_builtin_overflow_func(expr
);
546 case EXPR_INITIALIZER
:
547 split_expr_list(expr
->expr_list
, expr
);
549 case EXPR_IDENTIFIER
:
550 expr_set_parent_expr(expr
->ident_expression
, expr
);
551 __split_expr(expr
->ident_expression
);
554 expr_set_parent_expr(expr
->idx_expression
, expr
);
555 __split_expr(expr
->idx_expression
);
558 expr_set_parent_expr(expr
->init_expr
, expr
);
559 __split_expr(expr
->init_expr
);
562 __pass_to_client(expr
, SYM_HOOK
);
565 __pass_to_client(expr
, STRING_HOOK
);
570 pop_expression(&big_expression_stack
);
573 static int is_forever_loop(struct statement
*stmt
)
575 struct expression
*expr
;
578 expr
= strip_expr(stmt
->iterator_pre_condition
);
580 expr
= stmt
->iterator_post_condition
;
582 /* this is a for(;;) loop... */
586 if (get_value(expr
, &sval
) && sval
.value
!= 0)
593 static char *get_loop_name(int num
)
597 snprintf(buf
, 255, "-loop%d", num
);
599 return alloc_sname(buf
);
603 * Pre Loops are while and for loops.
605 static void handle_pre_loop(struct statement
*stmt
)
607 int once_through
; /* we go through the loop at least once */
608 struct sm_state
*extra_sm
= NULL
;
611 struct stree
*stree
= NULL
;
612 struct sm_state
*sm
= NULL
;
614 loop_name
= get_loop_name(loop_num
);
617 __split_stmt(stmt
->iterator_pre_statement
);
618 __prev_stmt
= stmt
->iterator_pre_statement
;
620 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
626 __merge_gotos(loop_name
, NULL
);
628 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
629 __in_pre_condition
++;
630 __pass_to_client(stmt
, PRELOOP_HOOK
);
631 __split_whole_condition(stmt
->iterator_pre_condition
);
632 __in_pre_condition
--;
633 FOR_EACH_SM(stree
, sm
) {
634 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
635 } END_FOR_EACH_SM(sm
);
638 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
640 if (option_assume_loops
)
643 __split_stmt(stmt
->iterator_statement
);
644 if (is_forever_loop(stmt
)) {
646 __save_gotos(loop_name
, NULL
);
648 __push_fake_cur_stree();
649 __split_stmt(stmt
->iterator_post_statement
);
650 stree
= __pop_fake_cur_stree();
652 __discard_false_states();
655 if (!__path_is_null())
656 __merge_stree_into_cur(stree
);
660 unchanged
= __iterator_unchanged(extra_sm
);
661 __split_stmt(stmt
->iterator_post_statement
);
662 __prev_stmt
= stmt
->iterator_post_statement
;
665 __save_gotos(loop_name
, NULL
);
666 __in_pre_condition
++;
667 __split_whole_condition(stmt
->iterator_pre_condition
);
668 __in_pre_condition
--;
670 __merge_false_states();
672 __discard_false_states();
674 __merge_false_states();
676 if (extra_sm
&& unchanged
)
677 __extra_pre_loop_hook_after(extra_sm
,
678 stmt
->iterator_post_statement
,
679 stmt
->iterator_pre_condition
);
686 * Post loops are do {} while();
688 static void handle_post_loop(struct statement
*stmt
)
692 loop_name
= get_loop_name(loop_num
);
698 __merge_gotos(loop_name
, NULL
);
699 __split_stmt(stmt
->iterator_statement
);
701 if (!is_zero(stmt
->iterator_post_condition
))
702 __save_gotos(loop_name
, NULL
);
704 if (is_forever_loop(stmt
)) {
707 __split_whole_condition(stmt
->iterator_post_condition
);
708 __use_false_states();
714 static int empty_statement(struct statement
*stmt
)
718 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
723 static int last_stmt_on_same_line(void)
725 struct statement
*stmt
;
728 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
731 if (stmt
->pos
.line
== get_lineno())
734 } END_FOR_EACH_PTR_REVERSE(stmt
);
738 static void split_asm_constraints(struct expression_list
*expr_list
)
740 struct expression
*expr
;
743 FOR_EACH_PTR(expr_list
, expr
) {
745 case 0: /* identifier */
746 case 1: /* constraint */
749 case 2: /* expression */
754 } END_FOR_EACH_PTR(expr
);
757 static int is_case_val(struct statement
*stmt
, sval_t sval
)
761 if (stmt
->type
!= STMT_CASE
)
763 if (!stmt
->case_expression
) {
767 if (!get_value(stmt
->case_expression
, &case_sval
))
769 if (case_sval
.value
== sval
.value
)
774 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
775 struct expression
*case_expr
,
776 struct expression
*case_to
)
779 struct range_list
*rl
= NULL
;
780 struct symbol
*switch_type
;
782 switch_type
= get_type(switch_expr
);
783 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
784 start
= sval_cast(switch_type
, start
);
785 end
= sval_cast(switch_type
, end
);
786 add_range(&rl
, start
, end
);
787 } else if (get_value(case_expr
, &start
)) {
788 start
= sval_cast(switch_type
, start
);
789 add_range(&rl
, start
, start
);
795 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
797 struct statement
*tmp
;
798 struct range_list
*rl
;
800 __split_expr(stmt
->switch_expression
);
801 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
803 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
804 __save_switch_states(top_expression(switch_expr_stack
));
809 stmt
= stmt
->switch_statement
;
811 __push_scope_hooks();
812 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
813 __smatch_lineno
= tmp
->pos
.line
;
814 if (is_case_val(tmp
, sval
)) {
815 rl
= alloc_rl(sval
, sval
);
816 __merge_switches(top_expression(switch_expr_stack
), rl
);
817 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
819 if (__path_is_null())
822 if (__path_is_null()) {
826 } END_FOR_EACH_PTR(tmp
);
828 __call_scope_hooks();
829 if (!__pop_default())
830 __merge_switches(top_expression(switch_expr_stack
), NULL
);
831 __discard_switches();
833 pop_expression(&switch_expr_stack
);
836 static void split_case(struct statement
*stmt
)
838 struct range_list
*rl
= NULL
;
840 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
841 expr_set_parent_stmt(stmt
->case_to
, stmt
);
843 rl
= get_case_rl(top_expression(switch_expr_stack
),
844 stmt
->case_expression
, stmt
->case_to
);
845 while (stmt
->case_statement
->type
== STMT_CASE
) {
846 struct range_list
*tmp
;
848 tmp
= get_case_rl(top_expression(switch_expr_stack
),
849 stmt
->case_statement
->case_expression
,
850 stmt
->case_statement
->case_to
);
853 rl
= rl_union(rl
, tmp
);
854 if (!stmt
->case_expression
)
856 stmt
= stmt
->case_statement
;
859 __merge_switches(top_expression(switch_expr_stack
), rl
);
861 if (!stmt
->case_expression
)
863 __split_stmt(stmt
->case_statement
);
866 int time_parsing_function(void)
868 return ms_since(&fn_start_time
) / 1000;
871 static int taking_too_long(void)
873 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
878 static int is_last_stmt(struct statement
*cur_stmt
)
881 struct statement
*stmt
;
885 fn
= get_base_type(cur_func_sym
);
890 stmt
= fn
->inline_stmt
;
891 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
893 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
894 if (stmt
&& stmt
->type
== STMT_LABEL
)
895 stmt
= stmt
->label_statement
;
896 if (stmt
== cur_stmt
)
901 static void handle_backward_goto(struct statement
*goto_stmt
)
903 const char *goto_name
, *label_name
;
904 struct statement
*func_stmt
;
905 struct symbol
*base_type
= get_base_type(cur_func_sym
);
906 struct statement
*tmp
;
911 if (last_goto_statement_handled
)
913 last_goto_statement_handled
= 1;
915 if (!goto_stmt
->goto_label
||
916 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
917 !goto_stmt
->goto_label
->ident
)
919 goto_name
= goto_stmt
->goto_label
->ident
->name
;
921 func_stmt
= base_type
->stmt
;
923 func_stmt
= base_type
->inline_stmt
;
926 if (func_stmt
->type
!= STMT_COMPOUND
)
929 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
931 if (tmp
->type
!= STMT_LABEL
)
933 if (!tmp
->label_identifier
||
934 tmp
->label_identifier
->type
!= SYM_LABEL
||
935 !tmp
->label_identifier
->ident
)
937 label_name
= tmp
->label_identifier
->ident
->name
;
938 if (strcmp(goto_name
, label_name
) != 0)
943 } END_FOR_EACH_PTR(tmp
);
946 static void fake_a_return(void)
948 struct symbol
*return_type
;
953 return_type
= get_real_base_type(cur_func_sym
);
954 return_type
= get_real_base_type(return_type
);
955 if (return_type
!= &void_ctype
) {
956 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
961 static void fake_an_empty_default(struct position pos
)
963 static struct statement none
= {};
966 none
.type
= STMT_NONE
;
967 __merge_switches(top_expression(switch_expr_stack
), NULL
);
971 static void split_compound(struct statement
*stmt
)
973 struct statement
*prev
= NULL
;
974 struct statement
*cur
= NULL
;
975 struct statement
*next
;
977 __push_scope_hooks();
979 FOR_EACH_PTR(stmt
->stmts
, next
) {
980 /* just set them all ahead of time */
981 stmt_set_parent_stmt(next
, stmt
);
991 } END_FOR_EACH_PTR(next
);
1000 * For function scope, then delay calling the scope hooks until the
1001 * end of function hooks can run. I'm not positive this is the right
1004 if (!is_last_stmt(cur
))
1005 __call_scope_hooks();
1009 * This is a hack, work around for detecting empty functions.
1011 static int need_delayed_scope_hooks(void)
1013 struct symbol
*fn
= get_base_type(cur_func_sym
);
1014 struct statement
*stmt
;
1020 stmt
= fn
->inline_stmt
;
1021 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1026 void __split_label_stmt(struct statement
*stmt
)
1028 if (stmt
->label_identifier
&&
1029 stmt
->label_identifier
->type
== SYM_LABEL
&&
1030 stmt
->label_identifier
->ident
) {
1031 loop_count
|= 0x0800000;
1032 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1036 static void find_asm_gotos(struct statement
*stmt
)
1040 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1041 __save_gotos(sym
->ident
->name
, sym
);
1042 } END_FOR_EACH_PTR(sym
);
1045 void __split_stmt(struct statement
*stmt
)
1052 if (!__in_fake_assign
)
1053 __silence_warnings_for_stmt
= false;
1055 if (__bail_on_rest_of_function
|| is_skipped_function())
1058 if (out_of_memory() || taking_too_long()) {
1059 struct timeval stop
;
1061 gettimeofday(&stop
, NULL
);
1063 __bail_on_rest_of_function
= 1;
1065 sm_perror("Function too hairy. Giving up. %lu seconds",
1066 stop
.tv_sec
- fn_start_time
.tv_sec
);
1068 final_pass
= 0; /* turn off sm_msg() from here */
1072 add_ptr_list(&big_statement_stack
, stmt
);
1073 free_expression_stack(&big_expression_stack
);
1074 set_position(stmt
->pos
);
1075 __pass_to_client(stmt
, STMT_HOOK
);
1077 switch (stmt
->type
) {
1078 case STMT_DECLARATION
:
1079 split_declaration(stmt
->declaration
);
1082 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1084 __split_expr(stmt
->ret_value
);
1085 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1086 __process_post_op_stack();
1089 case STMT_EXPRESSION
:
1090 expr_set_parent_stmt(stmt
->expression
, stmt
);
1091 expr_set_parent_stmt(stmt
->context
, stmt
);
1093 __split_expr(stmt
->expression
);
1096 split_compound(stmt
);
1099 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1100 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1101 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1103 if (known_condition_true(stmt
->if_conditional
)) {
1104 __split_stmt(stmt
->if_true
);
1107 if (known_condition_false(stmt
->if_conditional
)) {
1108 __split_stmt(stmt
->if_false
);
1111 __split_whole_condition(stmt
->if_conditional
);
1112 __split_stmt(stmt
->if_true
);
1113 if (empty_statement(stmt
->if_true
) &&
1114 last_stmt_on_same_line() &&
1115 !get_macro_name(stmt
->if_true
->pos
))
1116 sm_warning("if();");
1117 __push_true_states();
1118 __use_false_states();
1119 __split_stmt(stmt
->if_false
);
1120 __merge_true_states();
1123 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1124 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1125 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1126 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1127 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1129 if (stmt
->iterator_pre_condition
)
1130 handle_pre_loop(stmt
);
1131 else if (stmt
->iterator_post_condition
)
1132 handle_post_loop(stmt
);
1134 // these are for(;;) type loops.
1135 handle_pre_loop(stmt
);
1139 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1140 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1142 if (get_value(stmt
->switch_expression
, &sval
)) {
1143 split_known_switch(stmt
, sval
);
1146 __split_expr(stmt
->switch_expression
);
1147 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1148 __save_switch_states(top_expression(switch_expr_stack
));
1152 __split_stmt(stmt
->switch_statement
);
1153 if (!__pop_default() && have_remaining_cases())
1154 fake_an_empty_default(stmt
->pos
);
1155 __discard_switches();
1157 pop_expression(&switch_expr_stack
);
1163 __split_label_stmt(stmt
);
1164 __split_stmt(stmt
->label_statement
);
1167 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1169 __split_expr(stmt
->goto_expression
);
1170 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1171 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1173 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1175 __process_continues();
1177 } else if (stmt
->goto_label
&&
1178 stmt
->goto_label
->type
== SYM_LABEL
&&
1179 stmt
->goto_label
->ident
) {
1180 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1183 if (is_last_stmt(stmt
))
1184 handle_backward_goto(stmt
);
1189 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1191 find_asm_gotos(stmt
);
1192 __pass_to_client(stmt
, ASM_HOOK
);
1193 __split_expr(stmt
->asm_string
);
1194 split_asm_constraints(stmt
->asm_outputs
);
1195 split_asm_constraints(stmt
->asm_inputs
);
1196 split_asm_constraints(stmt
->asm_clobbers
);
1201 __split_expr(stmt
->range_expression
);
1202 __split_expr(stmt
->range_low
);
1203 __split_expr(stmt
->range_high
);
1206 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1208 __process_post_op_stack();
1211 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1213 struct expression
*expr
;
1215 FOR_EACH_PTR(expr_list
, expr
) {
1216 expr_set_parent_expr(expr
, parent
);
1218 __process_post_op_stack();
1219 } END_FOR_EACH_PTR(expr
);
1222 static void split_sym(struct symbol
*sym
)
1226 if (!(sym
->namespace & NS_SYMBOL
))
1229 __split_stmt(sym
->stmt
);
1230 __split_expr(sym
->array_size
);
1231 split_symlist(sym
->arguments
);
1232 split_symlist(sym
->symbol_list
);
1233 __split_stmt(sym
->inline_stmt
);
1234 split_symlist(sym
->inline_symbol_list
);
1237 static void split_symlist(struct symbol_list
*sym_list
)
1241 FOR_EACH_PTR(sym_list
, sym
) {
1243 } END_FOR_EACH_PTR(sym
);
1246 typedef void (fake_cb
)(struct expression
*expr
);
1248 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1250 struct symbol
*type
, *tmp
;
1256 name
= member
->name
;
1258 type
= get_type(expr
);
1259 if (!type
|| type
->type
!= SYM_STRUCT
)
1263 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1267 if (strcmp(name
, tmp
->ident
->name
) == 0)
1269 } END_FOR_EACH_PTR(tmp
);
1273 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1275 struct symbol
*type
, *member
;
1278 type
= get_type(expr
);
1279 if (!type
|| type
->type
!= SYM_STRUCT
)
1282 FOR_EACH_PTR(type
->symbol_list
, member
) {
1284 return member
->ident
;
1286 } END_FOR_EACH_PTR(member
);
1290 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1292 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1294 struct expression
*edge_member
, *assign
;
1295 struct symbol
*base
= get_real_base_type(member
);
1299 expr
= member_expression(expr
, '.', member
->ident
);
1301 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1302 struct symbol
*type
;
1304 type
= get_real_base_type(tmp
);
1308 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1309 if (get_extra_state(edge_member
))
1312 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1313 set_inner_struct_members(expr
, tmp
);
1320 assign
= assign_expression(edge_member
, '=', zero_expr());
1321 __split_expr(assign
);
1322 } END_FOR_EACH_PTR(tmp
);
1327 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1330 struct expression
*member
= NULL
;
1331 struct expression
*assign
;
1334 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1335 expr
= strip_expr(expr
->unop
);
1339 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1340 type
= get_real_base_type(tmp
);
1345 member
= member_expression(expr
, op
, tmp
->ident
);
1346 if (get_extra_state(member
))
1350 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1351 set_inner_struct_members(expr
, tmp
);
1354 if (type
->type
== SYM_ARRAY
)
1359 assign
= assign_expression(member
, '=', zero_expr());
1360 __split_expr(assign
);
1361 } END_FOR_EACH_PTR(tmp
);
1364 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1366 struct expression
*deref
, *assign
, *tmp
, *right
;
1367 struct symbol
*struct_type
, *type
;
1368 struct ident
*member
;
1371 struct_type
= get_type(symbol
);
1373 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1377 * We're parsing an initializer that could look something like this:
1378 * struct foo foo = {
1380 * .whatever.xxx = 11,
1384 * So what we have here is a list with 42, .whatever, and .zzz. We need
1385 * to break it up into left and right sides of the assignments.
1389 FOR_EACH_PTR(members
, tmp
) {
1391 if (tmp
->type
== EXPR_IDENTIFIER
) {
1392 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1393 while (tmp
->type
== EXPR_IDENTIFIER
) {
1394 member
= tmp
->expr_ident
;
1395 tmp
= tmp
->ident_expression
;
1397 deref
= member_expression(deref
, '.', member
);
1399 deref
= member_expression(symbol
, '.', member
);
1402 member
= number_to_member(symbol
, member_idx
);
1403 deref
= member_expression(symbol
, '.', member
);
1407 if (right
->type
== EXPR_INITIALIZER
) {
1408 type
= get_type(deref
);
1409 if (type
&& type
->type
== SYM_ARRAY
)
1410 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1412 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1414 assign
= assign_expression(deref
, '=', right
);
1417 } END_FOR_EACH_PTR(tmp
);
1419 set_unset_to_zero(struct_type
, symbol
);
1422 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1424 fake_member_assigns_helper(symbol_expression(sym
),
1425 sym
->initializer
->expr_list
, fake_cb
);
1428 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1430 struct expression
*offset
, *binop
, *assign
, *tmp
;
1431 struct symbol
*type
;
1434 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1438 FOR_EACH_PTR(expr_list
, tmp
) {
1439 if (tmp
->type
== EXPR_INDEX
) {
1440 if (tmp
->idx_from
!= tmp
->idx_to
)
1442 idx
= tmp
->idx_from
;
1443 if (!tmp
->idx_expression
)
1445 tmp
= tmp
->idx_expression
;
1447 offset
= value_expr(idx
);
1448 binop
= array_element_expression(array
, offset
);
1449 if (tmp
->type
== EXPR_INITIALIZER
) {
1450 type
= get_type(binop
);
1451 if (type
&& type
->type
== SYM_ARRAY
)
1452 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1454 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1456 assign
= assign_expression(binop
, '=', tmp
);
1461 } END_FOR_EACH_PTR(tmp
);
1464 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1466 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1469 static void fake_assign_expr(struct symbol
*sym
)
1471 struct expression
*assign
, *symbol
;
1473 symbol
= symbol_expression(sym
);
1474 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1475 __split_expr(assign
);
1478 static void do_initializer_stuff(struct symbol
*sym
)
1480 if (!sym
->initializer
)
1483 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1484 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1485 fake_element_assigns(sym
, __split_expr
);
1487 fake_member_assigns(sym
, __split_expr
);
1489 fake_assign_expr(sym
);
1493 static void split_declaration(struct symbol_list
*sym_list
)
1497 FOR_EACH_PTR(sym_list
, sym
) {
1498 __pass_to_client(sym
, DECLARATION_HOOK
);
1499 do_initializer_stuff(sym
);
1501 } END_FOR_EACH_PTR(sym
);
1504 static void call_global_assign_hooks(struct expression
*assign
)
1506 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1509 static void fake_global_assign(struct symbol
*sym
)
1511 struct expression
*assign
, *symbol
;
1513 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1514 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1515 fake_element_assigns(sym
, call_global_assign_hooks
);
1516 } else if (sym
->initializer
) {
1517 symbol
= symbol_expression(sym
);
1518 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1519 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1521 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1523 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1524 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1525 fake_member_assigns(sym
, call_global_assign_hooks
);
1526 } else if (sym
->initializer
) {
1527 symbol
= symbol_expression(sym
);
1528 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1529 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1531 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1534 symbol
= symbol_expression(sym
);
1535 if (sym
->initializer
) {
1536 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1537 __split_expr(assign
);
1539 assign
= assign_expression(symbol
, '=', zero_expr());
1541 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1545 static void start_function_definition(struct symbol
*sym
)
1547 __in_function_def
= 1;
1548 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1549 __in_function_def
= 0;
1550 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1554 static void split_function(struct symbol
*sym
)
1556 struct symbol
*base_type
= get_base_type(sym
);
1557 struct timeval stop
;
1559 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1562 gettimeofday(&outer_fn_start_time
, NULL
);
1563 gettimeofday(&fn_start_time
, NULL
);
1566 cur_func
= sym
->ident
->name
;
1567 set_position(sym
->pos
);
1569 last_goto_statement_handled
= 0;
1570 sm_debug("new function: %s\n", cur_func
);
1572 if (option_two_passes
) {
1576 start_function_definition(sym
);
1577 __split_stmt(base_type
->stmt
);
1578 __split_stmt(base_type
->inline_stmt
);
1584 start_function_definition(sym
);
1585 __split_stmt(base_type
->stmt
);
1586 __split_stmt(base_type
->inline_stmt
);
1587 __pass_to_client(sym
, END_FUNC_HOOK
);
1588 if (need_delayed_scope_hooks())
1589 __call_scope_hooks();
1590 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1594 gettimeofday(&stop
, NULL
);
1595 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1597 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1600 cur_func_sym
= NULL
;
1602 free_data_info_allocs();
1603 free_expression_stack(&switch_expr_stack
);
1604 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1605 __bail_on_rest_of_function
= 0;
1608 static void save_flow_state(void)
1610 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1611 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1612 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1614 __add_ptr_list(&backup
, big_statement_stack
, 0);
1615 __add_ptr_list(&backup
, big_expression_stack
, 0);
1616 __add_ptr_list(&backup
, big_condition_stack
, 0);
1617 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1619 __add_ptr_list(&backup
, cur_func_sym
, 0);
1621 __add_ptr_list(&backup
, __prev_stmt
, 0);
1622 __add_ptr_list(&backup
, __cur_stmt
, 0);
1623 __add_ptr_list(&backup
, __next_stmt
, 0);
1627 static void *pop_backup(void)
1631 ret
= last_ptr_list(backup
);
1632 delete_ptr_list_last(&backup
);
1636 static void restore_flow_state(void)
1638 __next_stmt
= pop_backup();
1639 __cur_stmt
= pop_backup();
1640 __prev_stmt
= pop_backup();
1642 cur_func_sym
= pop_backup();
1643 switch_expr_stack
= pop_backup();
1644 big_condition_stack
= pop_backup();
1645 big_expression_stack
= pop_backup();
1646 big_statement_stack
= pop_backup();
1647 final_pass
= PTR_INT(pop_backup()) >> 2;
1648 loop_count
= PTR_INT(pop_backup()) >> 2;
1649 loop_num
= PTR_INT(pop_backup()) >> 2;
1652 static void parse_inline(struct expression
*call
)
1654 struct symbol
*base_type
;
1655 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1656 struct timeval time_backup
= fn_start_time
;
1657 struct expression
*orig_inline
= __inline_fn
;
1660 if (out_of_memory() || taking_too_long())
1665 __pass_to_client(call
, INLINE_FN_START
);
1666 final_pass
= 0; /* don't print anything */
1668 orig_budget
= inline_budget
;
1669 inline_budget
= inline_budget
- 5;
1671 base_type
= get_base_type(call
->fn
->symbol
);
1672 cur_func_sym
= call
->fn
->symbol
;
1673 if (call
->fn
->symbol
->ident
)
1674 cur_func
= call
->fn
->symbol
->ident
->name
;
1677 set_position(call
->fn
->symbol
->pos
);
1680 big_statement_stack
= NULL
;
1681 big_expression_stack
= NULL
;
1682 big_condition_stack
= NULL
;
1683 switch_expr_stack
= NULL
;
1685 sm_debug("inline function: %s\n", cur_func
);
1689 start_function_definition(call
->fn
->symbol
);
1690 __split_stmt(base_type
->stmt
);
1691 __split_stmt(base_type
->inline_stmt
);
1692 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1693 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1695 free_expression_stack(&switch_expr_stack
);
1696 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1700 restore_flow_state();
1701 fn_start_time
= time_backup
;
1702 cur_func
= cur_func_bak
;
1704 restore_all_states();
1705 set_position(call
->pos
);
1706 __inline_fn
= orig_inline
;
1707 inline_budget
= orig_budget
;
1708 __pass_to_client(call
, INLINE_FN_END
);
1711 static struct symbol_list
*inlines_called
;
1712 static void add_inline_function(struct symbol
*sym
)
1714 static struct symbol_list
*already_added
;
1717 FOR_EACH_PTR(already_added
, tmp
) {
1720 } END_FOR_EACH_PTR(tmp
);
1722 add_ptr_list(&already_added
, sym
);
1723 add_ptr_list(&inlines_called
, sym
);
1726 static void process_inlines(void)
1730 FOR_EACH_PTR(inlines_called
, tmp
) {
1731 split_function(tmp
);
1732 } END_FOR_EACH_PTR(tmp
);
1733 free_ptr_list(&inlines_called
);
1736 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1740 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1743 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1745 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1747 } END_FOR_EACH_PTR_REVERSE(sym
);
1752 static bool interesting_function(struct symbol
*sym
)
1754 static int prev_stream
= -1;
1755 static bool prev_answer
;
1756 const char *filename
;
1759 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1762 if (sym
->pos
.stream
== prev_stream
)
1765 prev_stream
= sym
->pos
.stream
;
1766 prev_answer
= false;
1768 filename
= stream_name(sym
->pos
.stream
);
1769 len
= strlen(filename
);
1770 if (len
> 0 && filename
[len
- 1] == 'c')
1775 static void split_inlines_in_scope(struct symbol
*sym
)
1777 struct symbol
*base
;
1778 struct symbol_list
*scope_list
;
1781 scope_list
= sym
->scope
->symbols
;
1782 stream
= sym
->pos
.stream
;
1784 /* find the last static symbol in the file */
1785 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1786 if (sym
->pos
.stream
!= stream
)
1788 if (sym
->type
!= SYM_NODE
)
1790 base
= get_base_type(sym
);
1793 if (base
->type
!= SYM_FN
)
1795 if (!base
->inline_stmt
)
1797 if (!interesting_function(sym
))
1799 add_inline_function(sym
);
1800 } END_FOR_EACH_PTR_REVERSE(sym
);
1805 static void split_inlines(struct symbol_list
*sym_list
)
1809 sym
= get_last_scoped_symbol(sym_list
, 0);
1811 split_inlines_in_scope(sym
);
1812 sym
= get_last_scoped_symbol(sym_list
, 1);
1814 split_inlines_in_scope(sym
);
1817 static struct stree
*clone_estates_perm(struct stree
*orig
)
1819 struct stree
*ret
= NULL
;
1820 struct sm_state
*tmp
;
1822 FOR_EACH_SM(orig
, tmp
) {
1823 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1824 } END_FOR_EACH_SM(tmp
);
1829 struct position last_pos
;
1830 static void split_c_file_functions(struct symbol_list
*sym_list
)
1835 FOR_EACH_PTR(sym_list
, sym
) {
1836 set_position(sym
->pos
);
1837 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1838 __pass_to_client(sym
, BASE_HOOK
);
1839 fake_global_assign(sym
);
1841 } END_FOR_EACH_PTR(sym
);
1842 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1845 FOR_EACH_PTR(sym_list
, sym
) {
1846 set_position(sym
->pos
);
1847 last_pos
= sym
->pos
;
1848 if (!interesting_function(sym
))
1850 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1851 split_function(sym
);
1854 last_pos
= sym
->pos
;
1855 } END_FOR_EACH_PTR(sym
);
1856 split_inlines(sym_list
);
1857 __pass_to_client(sym_list
, END_FILE_HOOK
);
1860 static int final_before_fake
;
1861 void init_fake_env(void)
1864 final_before_fake
= final_pass
;
1866 __push_fake_cur_stree();
1870 void end_fake_env(void)
1872 __pop_fake_cur_stree();
1875 final_pass
= final_before_fake
;
1878 static void open_output_files(char *base_file
)
1882 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1883 sm_outfd
= fopen(buf
, "w");
1885 sm_fatal("Cannot open %s", buf
);
1890 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1891 sql_outfd
= fopen(buf
, "w");
1893 sm_fatal("Error: Cannot open %s", buf
);
1895 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1896 caller_info_fd
= fopen(buf
, "w");
1897 if (!caller_info_fd
)
1898 sm_fatal("Error: Cannot open %s", buf
);
1901 void smatch(int argc
, char **argv
)
1903 struct string_list
*filelist
= NULL
;
1904 struct symbol_list
*sym_list
;
1905 struct timeval stop
, start
;
1909 gettimeofday(&start
, NULL
);
1911 sparse_initialize(argc
, argv
, &filelist
);
1912 alloc_valid_ptr_rl();
1913 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1914 path
= getcwd(NULL
, 0);
1915 free(full_base_file
);
1917 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
1918 full_base_file
= malloc(len
);
1919 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
1921 full_base_file
= alloc_string(base_file
);
1923 if (option_file_output
)
1924 open_output_files(base_file
);
1925 sym_list
= sparse_keep_tokens(base_file
);
1926 split_c_file_functions(sym_list
);
1927 } END_FOR_EACH_PTR_NOTAG(base_file
);
1929 gettimeofday(&stop
, NULL
);
1931 set_position(last_pos
);
1934 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1936 sm_msg("mem: %luKb", get_max_memory());