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 int __in_unmatched_hook
;
48 static struct expression_list
*switch_expr_stack
= NULL
;
49 static struct expression_list
*post_op_stack
= NULL
;
51 static struct ptr_list
*backup
;
53 struct expression_list
*big_expression_stack
;
54 struct statement_list
*big_statement_stack
;
55 struct statement
*__prev_stmt
;
56 struct statement
*__cur_stmt
;
57 struct statement
*__next_stmt
;
58 int __in_pre_condition
= 0;
59 int __bail_on_rest_of_function
= 0;
60 static struct timeval fn_start_time
;
61 static struct timeval outer_fn_start_time
;
62 char *get_function(void) { return cur_func
; }
63 int get_lineno(void) { return __smatch_lineno
; }
64 int inside_loop(void) { return !!loop_count
; }
65 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
66 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
67 int in_expression_statement(void) { return !!__expr_stmt_count
; }
69 static void split_symlist(struct symbol_list
*sym_list
);
70 static void split_declaration(struct symbol_list
*sym_list
);
71 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
72 static void add_inline_function(struct symbol
*sym
);
73 static void parse_inline(struct expression
*expr
);
75 int option_assume_loops
= 0;
76 int option_two_passes
= 0;
77 struct symbol
*cur_func_sym
= NULL
;
78 struct stree
*global_states
;
80 const unsigned long valid_ptr_min
= 4096;
81 unsigned long valid_ptr_max
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
);
82 const sval_t valid_ptr_min_sval
= {
86 sval_t valid_ptr_max_sval
= {
88 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
90 struct range_list
*valid_ptr_rl
;
92 void alloc_valid_ptr_rl(void)
94 valid_ptr_max
= sval_type_max(&ulong_ctype
).value
& ~(MTAG_OFFSET_MASK
);
95 valid_ptr_max_sval
.value
= valid_ptr_max
;
97 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
98 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
99 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
102 int outside_of_function(void)
104 return cur_func_sym
== NULL
;
107 const char *get_filename(void)
109 if (option_info
&& option_full_path
)
110 return full_base_file
;
113 if (option_full_path
)
114 return full_filename
;
118 const char *get_base_file(void)
120 if (option_full_path
)
121 return full_base_file
;
125 static void set_position(struct position pos
)
128 static int prev_stream
= -1;
133 if (pos
.stream
== 0 && pos
.line
== 0)
136 __smatch_lineno
= pos
.line
;
138 if (pos
.stream
== prev_stream
)
141 filename
= stream_name(pos
.stream
);
144 pathname
= getcwd(NULL
, 0);
146 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
147 full_filename
= malloc(len
);
148 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
150 full_filename
= alloc_string(filename
);
155 int is_assigned_call(struct expression
*expr
)
157 struct expression
*parent
= expr_get_parent_expr(expr
);
160 parent
->type
== EXPR_ASSIGNMENT
&&
162 strip_expr(parent
->right
) == expr
)
168 static int is_inline_func(struct expression
*expr
)
170 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
172 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
177 static int is_noreturn_func(struct expression
*expr
)
179 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
181 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
186 static int inline_budget
= 20;
188 int inlinable(struct expression
*expr
)
191 struct statement
*last_stmt
= NULL
;
193 if (__inline_fn
) /* don't nest */
196 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
198 if (is_no_inline_function(expr
->symbol
->ident
->name
))
200 sym
= get_base_type(expr
->symbol
);
201 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
202 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
204 if (sym
->stmt
->type
!= STMT_COMPOUND
)
206 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
208 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
209 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
211 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
213 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
219 /* the magic numbers in this function are pulled out of my bum. */
220 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
226 void __process_post_op_stack(void)
228 struct expression
*expr
;
230 FOR_EACH_PTR(post_op_stack
, expr
) {
231 __pass_to_client(expr
, OP_HOOK
);
232 } END_FOR_EACH_PTR(expr
);
234 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
237 static int handle_comma_assigns(struct expression
*expr
)
239 struct expression
*right
;
240 struct expression
*assign
;
242 right
= strip_expr(expr
->right
);
243 if (right
->type
!= EXPR_COMMA
)
246 __split_expr(right
->left
);
247 __process_post_op_stack();
249 assign
= assign_expression(expr
->left
, '=', right
->right
);
250 __split_expr(assign
);
255 /* This is to handle *p++ = foo; assignments */
256 static int handle_postop_assigns(struct expression
*expr
)
258 struct expression
*left
, *fake_left
;
259 struct expression
*assign
;
261 left
= strip_expr(expr
->left
);
262 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
264 left
= strip_expr(left
->unop
);
265 if (left
->type
!= EXPR_POSTOP
)
268 fake_left
= deref_expression(strip_expr(left
->unop
));
269 assign
= assign_expression(fake_left
, '=', expr
->right
);
271 __split_expr(assign
);
272 __split_expr(expr
->left
);
277 static int prev_expression_is_getting_address(struct expression
*expr
)
279 struct expression
*parent
;
282 parent
= expr_get_parent_expr(expr
);
286 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
288 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
290 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
299 static void handle_builtin_overflow_func(struct expression
*expr
)
301 struct expression
*a
, *b
, *res
, *assign
;
304 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
306 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
308 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
313 a
= get_argument_from_call_expr(expr
->args
, 0);
314 b
= get_argument_from_call_expr(expr
->args
, 1);
315 res
= get_argument_from_call_expr(expr
->args
, 2);
317 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
318 __split_expr(assign
);
321 static int handle__builtin_choose_expr(struct expression
*expr
)
323 struct expression
*const_expr
, *expr1
, *expr2
;
326 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
329 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
330 expr1
= get_argument_from_call_expr(expr
->args
, 1);
331 expr2
= get_argument_from_call_expr(expr
->args
, 2);
333 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
342 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
344 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
347 right
= strip_expr(expr
->right
);
348 if (right
->type
!= EXPR_CALL
)
350 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
353 const_expr
= get_argument_from_call_expr(right
->args
, 0);
354 expr1
= get_argument_from_call_expr(right
->args
, 1);
355 expr2
= get_argument_from_call_expr(right
->args
, 2);
357 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
360 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
365 void __split_expr(struct expression
*expr
)
370 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
372 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
374 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
377 push_expression(&big_expression_stack
, expr
);
378 set_position(expr
->pos
);
379 __pass_to_client(expr
, EXPR_HOOK
);
381 switch (expr
->type
) {
383 expr_set_parent_expr(expr
->unop
, expr
);
385 if (expr
->op
== '*' &&
386 !prev_expression_is_getting_address(expr
))
387 __pass_to_client(expr
, DEREF_HOOK
);
388 __split_expr(expr
->unop
);
389 __pass_to_client(expr
, OP_HOOK
);
392 expr_set_parent_expr(expr
->unop
, expr
);
394 __split_expr(expr
->unop
);
395 push_expression(&post_op_stack
, expr
);
399 if (expr
->statement
&& !expr
->statement
) {
400 stmt_set_parent_stmt(expr
->statement
,
401 last_ptr_list((struct ptr_list
*)big_statement_stack
));
403 __split_stmt(expr
->statement
);
408 expr_set_parent_expr(expr
->left
, expr
);
409 expr_set_parent_expr(expr
->right
, expr
);
411 __pass_to_client(expr
, LOGIC_HOOK
);
412 __handle_logic(expr
);
415 expr_set_parent_expr(expr
->left
, expr
);
416 expr_set_parent_expr(expr
->right
, expr
);
418 __pass_to_client(expr
, BINOP_HOOK
);
420 expr_set_parent_expr(expr
->left
, expr
);
421 expr_set_parent_expr(expr
->right
, expr
);
423 __split_expr(expr
->left
);
424 __process_post_op_stack();
425 __split_expr(expr
->right
);
427 case EXPR_ASSIGNMENT
: {
428 struct expression
*right
;
430 expr_set_parent_expr(expr
->left
, expr
);
431 expr_set_parent_expr(expr
->right
, expr
);
433 right
= strip_expr(expr
->right
);
437 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
440 if (__handle_condition_assigns(expr
))
442 /* foo = (x < 5 ? foo : 5); */
443 if (__handle_select_assigns(expr
))
445 /* foo = ({frob(); frob(); frob(); 1;}) */
446 if (__handle_expr_statement_assigns(expr
))
447 break; // FIXME: got after
449 if (handle_comma_assigns(expr
))
451 if (handle__builtin_choose_expr_assigns(expr
))
453 if (handle_postop_assigns(expr
))
454 break; /* no need to goto after_assign */
456 __split_expr(expr
->right
);
457 if (outside_of_function())
458 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
460 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
462 __fake_struct_member_assignments(expr
);
464 /* Re-examine ->right for inlines. See the commit message */
465 right
= strip_expr(expr
->right
);
466 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
467 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
469 if (get_macro_name(right
->pos
) &&
470 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
471 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
474 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
475 __split_expr(expr
->left
);
479 expr_set_parent_expr(expr
->deref
, expr
);
481 __pass_to_client(expr
, DEREF_HOOK
);
482 __split_expr(expr
->deref
);
485 expr_set_parent_expr(expr
->base
, expr
);
487 __split_expr(expr
->base
);
490 case EXPR_FORCE_CAST
:
491 expr_set_parent_expr(expr
->cast_expression
, expr
);
493 __pass_to_client(expr
, CAST_HOOK
);
494 __split_expr(expr
->cast_expression
);
497 if (expr
->cast_expression
)
498 __pass_to_client(strip_parens(expr
->cast_expression
),
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(expr
->fn
);
534 split_expr_list(expr
->args
, expr
);
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 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
576 pop_expression(&big_expression_stack
);
579 static int is_forever_loop(struct statement
*stmt
)
581 struct expression
*expr
;
584 expr
= strip_expr(stmt
->iterator_pre_condition
);
586 expr
= stmt
->iterator_post_condition
;
588 /* this is a for(;;) loop... */
592 if (get_value(expr
, &sval
) && sval
.value
!= 0)
599 static char *get_loop_name(int num
)
603 snprintf(buf
, 255, "-loop%d", num
);
605 return alloc_sname(buf
);
609 * Pre Loops are while and for loops.
611 static void handle_pre_loop(struct statement
*stmt
)
613 int once_through
; /* we go through the loop at least once */
614 struct sm_state
*extra_sm
= NULL
;
617 struct stree
*stree
= NULL
;
618 struct sm_state
*sm
= NULL
;
620 loop_name
= get_loop_name(loop_num
);
623 __split_stmt(stmt
->iterator_pre_statement
);
624 __prev_stmt
= stmt
->iterator_pre_statement
;
626 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
632 __merge_gotos(loop_name
, NULL
);
634 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
635 __in_pre_condition
++;
636 __pass_to_client(stmt
, PRELOOP_HOOK
);
637 __split_whole_condition(stmt
->iterator_pre_condition
);
638 __in_pre_condition
--;
639 FOR_EACH_SM(stree
, sm
) {
640 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
641 } END_FOR_EACH_SM(sm
);
644 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
646 if (option_assume_loops
)
649 __split_stmt(stmt
->iterator_statement
);
650 if (is_forever_loop(stmt
)) {
652 __save_gotos(loop_name
, NULL
);
654 __push_fake_cur_stree();
655 __split_stmt(stmt
->iterator_post_statement
);
656 stree
= __pop_fake_cur_stree();
658 __discard_false_states();
661 if (!__path_is_null())
662 __merge_stree_into_cur(stree
);
666 unchanged
= __iterator_unchanged(extra_sm
);
667 __split_stmt(stmt
->iterator_post_statement
);
668 __prev_stmt
= stmt
->iterator_post_statement
;
671 __save_gotos(loop_name
, NULL
);
672 __in_pre_condition
++;
673 __split_whole_condition(stmt
->iterator_pre_condition
);
674 __in_pre_condition
--;
676 __merge_false_states();
678 __discard_false_states();
680 __merge_false_states();
682 if (extra_sm
&& unchanged
)
683 __extra_pre_loop_hook_after(extra_sm
,
684 stmt
->iterator_post_statement
,
685 stmt
->iterator_pre_condition
);
692 * Post loops are do {} while();
694 static void handle_post_loop(struct statement
*stmt
)
698 loop_name
= get_loop_name(loop_num
);
704 __merge_gotos(loop_name
, NULL
);
705 __split_stmt(stmt
->iterator_statement
);
707 if (!expr_is_zero(stmt
->iterator_post_condition
))
708 __save_gotos(loop_name
, NULL
);
710 if (is_forever_loop(stmt
)) {
713 __split_whole_condition(stmt
->iterator_post_condition
);
714 __use_false_states();
720 static int empty_statement(struct statement
*stmt
)
724 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
729 static int last_stmt_on_same_line(void)
731 struct statement
*stmt
;
734 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
737 if (stmt
->pos
.line
== get_lineno())
740 } END_FOR_EACH_PTR_REVERSE(stmt
);
744 static void split_asm_constraints(struct expression_list
*expr_list
)
746 struct expression
*expr
;
749 FOR_EACH_PTR(expr_list
, expr
) {
751 case 0: /* identifier */
752 case 1: /* constraint */
755 case 2: /* expression */
760 } END_FOR_EACH_PTR(expr
);
763 static int is_case_val(struct statement
*stmt
, sval_t sval
)
767 if (stmt
->type
!= STMT_CASE
)
769 if (!stmt
->case_expression
) {
773 if (!get_value(stmt
->case_expression
, &case_sval
))
775 if (case_sval
.value
== sval
.value
)
780 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
781 struct expression
*case_expr
,
782 struct expression
*case_to
)
785 struct range_list
*rl
= NULL
;
786 struct symbol
*switch_type
;
788 switch_type
= get_type(switch_expr
);
789 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
790 start
= sval_cast(switch_type
, start
);
791 end
= sval_cast(switch_type
, end
);
792 add_range(&rl
, start
, end
);
793 } else if (get_value(case_expr
, &start
)) {
794 start
= sval_cast(switch_type
, start
);
795 add_range(&rl
, start
, start
);
801 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
803 struct statement
*tmp
;
804 struct range_list
*rl
;
806 __split_expr(stmt
->switch_expression
);
807 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
809 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
810 __save_switch_states(top_expression(switch_expr_stack
));
815 stmt
= stmt
->switch_statement
;
817 __push_scope_hooks();
818 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
819 __smatch_lineno
= tmp
->pos
.line
;
820 if (is_case_val(tmp
, sval
)) {
821 rl
= alloc_rl(sval
, sval
);
822 __merge_switches(top_expression(switch_expr_stack
), rl
);
823 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
825 if (__path_is_null())
828 if (__path_is_null()) {
832 } END_FOR_EACH_PTR(tmp
);
834 __call_scope_hooks();
835 if (!__pop_default())
836 __merge_switches(top_expression(switch_expr_stack
), NULL
);
837 __discard_switches();
839 pop_expression(&switch_expr_stack
);
842 static void split_case(struct statement
*stmt
)
844 struct range_list
*rl
= NULL
;
846 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
847 expr_set_parent_stmt(stmt
->case_to
, stmt
);
849 rl
= get_case_rl(top_expression(switch_expr_stack
),
850 stmt
->case_expression
, stmt
->case_to
);
851 while (stmt
->case_statement
->type
== STMT_CASE
) {
852 struct range_list
*tmp
;
854 tmp
= get_case_rl(top_expression(switch_expr_stack
),
855 stmt
->case_statement
->case_expression
,
856 stmt
->case_statement
->case_to
);
859 rl
= rl_union(rl
, tmp
);
860 if (!stmt
->case_expression
)
862 stmt
= stmt
->case_statement
;
865 __merge_switches(top_expression(switch_expr_stack
), rl
);
867 if (!stmt
->case_expression
)
869 __split_stmt(stmt
->case_statement
);
872 int time_parsing_function(void)
874 return ms_since(&fn_start_time
) / 1000;
877 bool taking_too_long(void)
879 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
884 static int is_last_stmt(struct statement
*cur_stmt
)
887 struct statement
*stmt
;
891 fn
= get_base_type(cur_func_sym
);
896 stmt
= fn
->inline_stmt
;
897 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
899 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
900 if (stmt
&& stmt
->type
== STMT_LABEL
)
901 stmt
= stmt
->label_statement
;
902 if (stmt
== cur_stmt
)
907 static void handle_backward_goto(struct statement
*goto_stmt
)
909 const char *goto_name
, *label_name
;
910 struct statement
*func_stmt
;
911 struct symbol
*base_type
= get_base_type(cur_func_sym
);
912 struct statement
*tmp
;
917 if (last_goto_statement_handled
)
919 last_goto_statement_handled
= 1;
921 if (!goto_stmt
->goto_label
||
922 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
923 !goto_stmt
->goto_label
->ident
)
925 goto_name
= goto_stmt
->goto_label
->ident
->name
;
927 func_stmt
= base_type
->stmt
;
929 func_stmt
= base_type
->inline_stmt
;
932 if (func_stmt
->type
!= STMT_COMPOUND
)
935 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
937 if (tmp
->type
!= STMT_LABEL
)
939 if (!tmp
->label_identifier
||
940 tmp
->label_identifier
->type
!= SYM_LABEL
||
941 !tmp
->label_identifier
->ident
)
943 label_name
= tmp
->label_identifier
->ident
->name
;
944 if (strcmp(goto_name
, label_name
) != 0)
949 } END_FOR_EACH_PTR(tmp
);
952 static void fake_a_return(void)
954 struct symbol
*return_type
;
959 return_type
= get_real_base_type(cur_func_sym
);
960 return_type
= get_real_base_type(return_type
);
961 if (return_type
!= &void_ctype
) {
962 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
967 static void fake_an_empty_default(struct position pos
)
969 static struct statement none
= {};
972 none
.type
= STMT_NONE
;
973 __merge_switches(top_expression(switch_expr_stack
), NULL
);
977 static void split_compound(struct statement
*stmt
)
979 struct statement
*prev
= NULL
;
980 struct statement
*cur
= NULL
;
981 struct statement
*next
;
983 __push_scope_hooks();
985 FOR_EACH_PTR(stmt
->stmts
, next
) {
986 /* just set them all ahead of time */
987 stmt_set_parent_stmt(next
, stmt
);
997 } END_FOR_EACH_PTR(next
);
1006 * For function scope, then delay calling the scope hooks until the
1007 * end of function hooks can run. I'm not positive this is the right
1010 if (!is_last_stmt(cur
))
1011 __call_scope_hooks();
1015 * This is a hack, work around for detecting empty functions.
1017 static int need_delayed_scope_hooks(void)
1019 struct symbol
*fn
= get_base_type(cur_func_sym
);
1020 struct statement
*stmt
;
1026 stmt
= fn
->inline_stmt
;
1027 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1032 void __split_label_stmt(struct statement
*stmt
)
1034 if (stmt
->label_identifier
&&
1035 stmt
->label_identifier
->type
== SYM_LABEL
&&
1036 stmt
->label_identifier
->ident
) {
1037 loop_count
|= 0x0800000;
1038 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1042 static void find_asm_gotos(struct statement
*stmt
)
1046 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1047 __save_gotos(sym
->ident
->name
, sym
);
1048 } END_FOR_EACH_PTR(sym
);
1051 void __split_stmt(struct statement
*stmt
)
1058 if (!__in_fake_assign
)
1059 __silence_warnings_for_stmt
= false;
1061 if (__bail_on_rest_of_function
|| is_skipped_function())
1064 if (out_of_memory() || taking_too_long()) {
1065 struct timeval stop
;
1067 gettimeofday(&stop
, NULL
);
1069 __bail_on_rest_of_function
= 1;
1071 sm_perror("Function too hairy. Giving up. %lu seconds",
1072 stop
.tv_sec
- fn_start_time
.tv_sec
);
1074 final_pass
= 0; /* turn off sm_msg() from here */
1078 add_ptr_list(&big_statement_stack
, stmt
);
1079 free_expression_stack(&big_expression_stack
);
1080 set_position(stmt
->pos
);
1081 __pass_to_client(stmt
, STMT_HOOK
);
1083 switch (stmt
->type
) {
1084 case STMT_DECLARATION
:
1085 split_declaration(stmt
->declaration
);
1088 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1090 __split_expr(stmt
->ret_value
);
1091 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1092 __process_post_op_stack();
1095 case STMT_EXPRESSION
:
1096 expr_set_parent_stmt(stmt
->expression
, stmt
);
1097 expr_set_parent_stmt(stmt
->context
, stmt
);
1099 __split_expr(stmt
->expression
);
1102 split_compound(stmt
);
1105 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1106 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1107 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1109 if (known_condition_true(stmt
->if_conditional
)) {
1110 __split_stmt(stmt
->if_true
);
1113 if (known_condition_false(stmt
->if_conditional
)) {
1114 __split_stmt(stmt
->if_false
);
1117 __split_whole_condition(stmt
->if_conditional
);
1118 __split_stmt(stmt
->if_true
);
1119 if (empty_statement(stmt
->if_true
) &&
1120 last_stmt_on_same_line() &&
1121 !get_macro_name(stmt
->if_true
->pos
))
1122 sm_warning("if();");
1123 __push_true_states();
1124 __use_false_states();
1125 __split_stmt(stmt
->if_false
);
1126 __merge_true_states();
1129 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1130 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1131 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1132 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1133 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1135 if (stmt
->iterator_pre_condition
)
1136 handle_pre_loop(stmt
);
1137 else if (stmt
->iterator_post_condition
)
1138 handle_post_loop(stmt
);
1140 // these are for(;;) type loops.
1141 handle_pre_loop(stmt
);
1145 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1146 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1148 if (get_value(stmt
->switch_expression
, &sval
)) {
1149 split_known_switch(stmt
, sval
);
1152 __split_expr(stmt
->switch_expression
);
1153 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1154 __save_switch_states(top_expression(switch_expr_stack
));
1158 __split_stmt(stmt
->switch_statement
);
1159 if (!__pop_default() && have_remaining_cases())
1160 fake_an_empty_default(stmt
->pos
);
1161 __discard_switches();
1163 pop_expression(&switch_expr_stack
);
1169 __split_label_stmt(stmt
);
1170 __split_stmt(stmt
->label_statement
);
1173 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1175 __split_expr(stmt
->goto_expression
);
1176 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1177 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1179 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1181 __process_continues();
1183 } else if (stmt
->goto_label
&&
1184 stmt
->goto_label
->type
== SYM_LABEL
&&
1185 stmt
->goto_label
->ident
) {
1186 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1189 if (is_last_stmt(stmt
))
1190 handle_backward_goto(stmt
);
1195 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1197 find_asm_gotos(stmt
);
1198 __pass_to_client(stmt
, ASM_HOOK
);
1199 __split_expr(stmt
->asm_string
);
1200 split_asm_constraints(stmt
->asm_outputs
);
1201 split_asm_constraints(stmt
->asm_inputs
);
1202 split_asm_constraints(stmt
->asm_clobbers
);
1207 __split_expr(stmt
->range_expression
);
1208 __split_expr(stmt
->range_low
);
1209 __split_expr(stmt
->range_high
);
1212 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1214 __process_post_op_stack();
1217 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1219 struct expression
*expr
;
1221 FOR_EACH_PTR(expr_list
, expr
) {
1222 expr_set_parent_expr(expr
, parent
);
1224 __process_post_op_stack();
1225 } END_FOR_EACH_PTR(expr
);
1228 static void split_sym(struct symbol
*sym
)
1232 if (!(sym
->namespace & NS_SYMBOL
))
1235 __split_stmt(sym
->stmt
);
1236 __split_expr(sym
->array_size
);
1237 split_symlist(sym
->arguments
);
1238 split_symlist(sym
->symbol_list
);
1239 __split_stmt(sym
->inline_stmt
);
1240 split_symlist(sym
->inline_symbol_list
);
1243 static void split_symlist(struct symbol_list
*sym_list
)
1247 FOR_EACH_PTR(sym_list
, sym
) {
1249 } END_FOR_EACH_PTR(sym
);
1252 typedef void (fake_cb
)(struct expression
*expr
);
1254 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1256 struct symbol
*type
, *tmp
;
1262 name
= member
->name
;
1264 type
= get_type(expr
);
1265 if (!type
|| type
->type
!= SYM_STRUCT
)
1269 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1273 if (strcmp(name
, tmp
->ident
->name
) == 0)
1275 } END_FOR_EACH_PTR(tmp
);
1279 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1281 struct symbol
*type
, *member
;
1284 type
= get_type(expr
);
1285 if (!type
|| type
->type
!= SYM_STRUCT
)
1288 FOR_EACH_PTR(type
->symbol_list
, member
) {
1290 return member
->ident
;
1292 } END_FOR_EACH_PTR(member
);
1296 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1298 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1300 struct expression
*edge_member
, *assign
;
1301 struct symbol
*base
= get_real_base_type(member
);
1305 expr
= member_expression(expr
, '.', member
->ident
);
1307 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1308 struct symbol
*type
;
1310 type
= get_real_base_type(tmp
);
1314 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1315 if (get_extra_state(edge_member
))
1318 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1319 set_inner_struct_members(expr
, tmp
);
1326 assign
= assign_expression(edge_member
, '=', zero_expr());
1327 __split_expr(assign
);
1328 } END_FOR_EACH_PTR(tmp
);
1333 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1336 struct expression
*member
= NULL
;
1337 struct expression
*assign
;
1340 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1341 expr
= strip_expr(expr
->unop
);
1345 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1346 type
= get_real_base_type(tmp
);
1351 member
= member_expression(expr
, op
, tmp
->ident
);
1352 if (get_extra_state(member
))
1356 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1357 set_inner_struct_members(expr
, tmp
);
1360 if (type
->type
== SYM_ARRAY
)
1365 assign
= assign_expression(member
, '=', zero_expr());
1366 __split_expr(assign
);
1367 } END_FOR_EACH_PTR(tmp
);
1370 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1372 struct expression
*deref
, *assign
, *tmp
, *right
;
1373 struct symbol
*struct_type
, *type
;
1374 struct ident
*member
;
1377 struct_type
= get_type(symbol
);
1379 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1383 * We're parsing an initializer that could look something like this:
1384 * struct foo foo = {
1386 * .whatever.xxx = 11,
1390 * So what we have here is a list with 42, .whatever, and .zzz. We need
1391 * to break it up into left and right sides of the assignments.
1395 FOR_EACH_PTR(members
, tmp
) {
1397 if (tmp
->type
== EXPR_IDENTIFIER
) {
1398 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1399 while (tmp
->type
== EXPR_IDENTIFIER
) {
1400 member
= tmp
->expr_ident
;
1401 tmp
= tmp
->ident_expression
;
1403 deref
= member_expression(deref
, '.', member
);
1405 deref
= member_expression(symbol
, '.', member
);
1408 member
= number_to_member(symbol
, member_idx
);
1409 deref
= member_expression(symbol
, '.', member
);
1413 if (right
->type
== EXPR_INITIALIZER
) {
1414 type
= get_type(deref
);
1415 if (type
&& type
->type
== SYM_ARRAY
)
1416 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1418 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1420 assign
= assign_expression(deref
, '=', right
);
1423 } END_FOR_EACH_PTR(tmp
);
1425 set_unset_to_zero(struct_type
, symbol
);
1428 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1430 fake_member_assigns_helper(symbol_expression(sym
),
1431 sym
->initializer
->expr_list
, fake_cb
);
1434 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1436 struct expression
*offset
, *binop
, *assign
, *tmp
;
1437 struct symbol
*type
;
1440 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1444 FOR_EACH_PTR(expr_list
, tmp
) {
1445 if (tmp
->type
== EXPR_INDEX
) {
1446 if (tmp
->idx_from
!= tmp
->idx_to
)
1448 idx
= tmp
->idx_from
;
1449 if (!tmp
->idx_expression
)
1451 tmp
= tmp
->idx_expression
;
1453 offset
= value_expr(idx
);
1454 binop
= array_element_expression(array
, offset
);
1455 if (tmp
->type
== EXPR_INITIALIZER
) {
1456 type
= get_type(binop
);
1457 if (type
&& type
->type
== SYM_ARRAY
)
1458 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1460 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1462 assign
= assign_expression(binop
, '=', tmp
);
1467 } END_FOR_EACH_PTR(tmp
);
1470 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1472 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1475 static void fake_assign_expr(struct symbol
*sym
)
1477 struct expression
*assign
, *symbol
;
1479 symbol
= symbol_expression(sym
);
1480 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1481 __split_expr(assign
);
1484 static void do_initializer_stuff(struct symbol
*sym
)
1486 if (!sym
->initializer
)
1489 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1490 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1491 fake_element_assigns(sym
, __split_expr
);
1493 fake_member_assigns(sym
, __split_expr
);
1495 fake_assign_expr(sym
);
1499 static void split_declaration(struct symbol_list
*sym_list
)
1503 FOR_EACH_PTR(sym_list
, sym
) {
1504 __pass_to_client(sym
, DECLARATION_HOOK
);
1505 do_initializer_stuff(sym
);
1507 } END_FOR_EACH_PTR(sym
);
1510 static void call_global_assign_hooks(struct expression
*assign
)
1512 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1515 static void fake_global_assign(struct symbol
*sym
)
1517 struct expression
*assign
, *symbol
;
1519 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1520 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1521 fake_element_assigns(sym
, call_global_assign_hooks
);
1522 } else if (sym
->initializer
) {
1523 symbol
= symbol_expression(sym
);
1524 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1525 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1527 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1529 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1530 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1531 fake_member_assigns(sym
, call_global_assign_hooks
);
1532 } else if (sym
->initializer
) {
1533 symbol
= symbol_expression(sym
);
1534 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1535 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1537 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1540 symbol
= symbol_expression(sym
);
1541 if (sym
->initializer
) {
1542 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1543 __split_expr(assign
);
1545 assign
= assign_expression(symbol
, '=', zero_expr());
1547 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1551 static void start_function_definition(struct symbol
*sym
)
1553 __in_function_def
= 1;
1554 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1555 __in_function_def
= 0;
1556 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1560 static void split_function(struct symbol
*sym
)
1562 struct symbol
*base_type
= get_base_type(sym
);
1563 struct timeval stop
;
1565 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1568 gettimeofday(&outer_fn_start_time
, NULL
);
1569 gettimeofday(&fn_start_time
, NULL
);
1572 cur_func
= sym
->ident
->name
;
1573 set_position(sym
->pos
);
1575 last_goto_statement_handled
= 0;
1576 sm_debug("new function: %s\n", cur_func
);
1578 if (option_two_passes
) {
1582 start_function_definition(sym
);
1583 __split_stmt(base_type
->stmt
);
1584 __split_stmt(base_type
->inline_stmt
);
1590 start_function_definition(sym
);
1591 __split_stmt(base_type
->stmt
);
1592 __split_stmt(base_type
->inline_stmt
);
1593 __pass_to_client(sym
, END_FUNC_HOOK
);
1594 if (need_delayed_scope_hooks())
1595 __call_scope_hooks();
1596 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1600 gettimeofday(&stop
, NULL
);
1601 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1603 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1606 cur_func_sym
= NULL
;
1608 free_data_info_allocs();
1609 free_expression_stack(&switch_expr_stack
);
1610 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1611 __bail_on_rest_of_function
= 0;
1614 static void save_flow_state(void)
1616 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
1617 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
1618 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
1620 __add_ptr_list(&backup
, big_statement_stack
);
1621 __add_ptr_list(&backup
, big_expression_stack
);
1622 __add_ptr_list(&backup
, big_condition_stack
);
1623 __add_ptr_list(&backup
, switch_expr_stack
);
1625 __add_ptr_list(&backup
, cur_func_sym
);
1627 __add_ptr_list(&backup
, __prev_stmt
);
1628 __add_ptr_list(&backup
, __cur_stmt
);
1629 __add_ptr_list(&backup
, __next_stmt
);
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(struct string_list
*filelist
)
1908 struct symbol_list
*sym_list
;
1909 struct timeval stop
, start
;
1913 gettimeofday(&start
, NULL
);
1915 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1916 path
= getcwd(NULL
, 0);
1917 free(full_base_file
);
1919 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
1920 full_base_file
= malloc(len
);
1921 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
1923 full_base_file
= alloc_string(base_file
);
1925 if (option_file_output
)
1926 open_output_files(base_file
);
1927 sym_list
= sparse_keep_tokens(base_file
);
1928 split_c_file_functions(sym_list
);
1929 } END_FOR_EACH_PTR_NOTAG(base_file
);
1931 gettimeofday(&stop
, NULL
);
1933 set_position(last_pos
);
1936 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1938 sm_msg("mem: %luKb", get_max_memory());