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 long long valid_ptr_min
= 4096;
80 long long valid_ptr_max
= 2117777777;
81 sval_t valid_ptr_min_sval
= {
85 sval_t valid_ptr_max_sval
= {
87 {.value
= LONG_MAX
- 100000},
89 struct range_list
*valid_ptr_rl
;
91 static void set_valid_ptr_max(void)
93 if (type_bits(&ptr_ctype
) == 32)
94 valid_ptr_max
= 2117777777;
95 else if (type_bits(&ptr_ctype
) == 64)
96 valid_ptr_max
= 2117777777777777777LL;
98 valid_ptr_max_sval
.value
= valid_ptr_max
;
101 static void alloc_valid_ptr_rl(void)
103 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
104 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
105 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
108 int outside_of_function(void)
110 return cur_func_sym
== NULL
;
113 const char *get_filename(void)
115 if (option_info
&& option_full_path
)
116 return full_base_file
;
119 if (option_full_path
)
120 return full_filename
;
124 const char *get_base_file(void)
126 if (option_full_path
)
127 return full_base_file
;
131 static void set_position(struct position pos
)
134 static int prev_stream
= -1;
139 if (pos
.stream
== 0 && pos
.line
== 0)
142 __smatch_lineno
= pos
.line
;
144 if (pos
.stream
== prev_stream
)
147 filename
= stream_name(pos
.stream
);
150 pathname
= getcwd(NULL
, 0);
152 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
153 full_filename
= malloc(len
);
154 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
156 full_filename
= alloc_string(filename
);
161 int is_assigned_call(struct expression
*expr
)
163 struct expression
*parent
= expr_get_parent_expr(expr
);
166 parent
->type
== EXPR_ASSIGNMENT
&&
168 strip_expr(parent
->right
) == expr
)
174 static int is_inline_func(struct expression
*expr
)
176 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
178 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
183 static int is_noreturn_func(struct expression
*expr
)
185 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
187 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
192 static int inline_budget
= 20;
194 int inlinable(struct expression
*expr
)
197 struct statement
*last_stmt
= NULL
;
199 if (__inline_fn
) /* don't nest */
202 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
204 if (is_no_inline_function(expr
->symbol
->ident
->name
))
206 sym
= get_base_type(expr
->symbol
);
207 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
208 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
210 if (sym
->stmt
->type
!= STMT_COMPOUND
)
212 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
214 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
215 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
217 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
219 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
225 /* the magic numbers in this function are pulled out of my bum. */
226 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
232 void __process_post_op_stack(void)
234 struct expression
*expr
;
236 FOR_EACH_PTR(post_op_stack
, expr
) {
237 __pass_to_client(expr
, OP_HOOK
);
238 } END_FOR_EACH_PTR(expr
);
240 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
243 static int handle_comma_assigns(struct expression
*expr
)
245 struct expression
*right
;
246 struct expression
*assign
;
248 right
= strip_expr(expr
->right
);
249 if (right
->type
!= EXPR_COMMA
)
252 __split_expr(right
->left
);
253 __process_post_op_stack();
255 assign
= assign_expression(expr
->left
, '=', right
->right
);
256 __split_expr(assign
);
261 /* This is to handle *p++ = foo; assignments */
262 static int handle_postop_assigns(struct expression
*expr
)
264 struct expression
*left
, *fake_left
;
265 struct expression
*assign
;
267 left
= strip_expr(expr
->left
);
268 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
270 left
= strip_expr(left
->unop
);
271 if (left
->type
!= EXPR_POSTOP
)
274 fake_left
= deref_expression(strip_expr(left
->unop
));
275 assign
= assign_expression(fake_left
, '=', expr
->right
);
277 __split_expr(assign
);
278 __split_expr(expr
->left
);
283 static int prev_expression_is_getting_address(struct expression
*expr
)
285 struct expression
*parent
;
288 parent
= expr_get_parent_expr(expr
);
292 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
294 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
296 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
305 static void handle_builtin_overflow_func(struct expression
*expr
)
307 struct expression
*a
, *b
, *res
, *assign
;
310 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
312 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
314 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
319 a
= get_argument_from_call_expr(expr
->args
, 0);
320 b
= get_argument_from_call_expr(expr
->args
, 1);
321 res
= get_argument_from_call_expr(expr
->args
, 2);
323 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
324 __split_expr(assign
);
327 static int handle__builtin_choose_expr(struct expression
*expr
)
329 struct expression
*const_expr
, *expr1
, *expr2
;
332 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
335 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
336 expr1
= get_argument_from_call_expr(expr
->args
, 1);
337 expr2
= get_argument_from_call_expr(expr
->args
, 2);
339 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
348 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
350 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
353 right
= strip_expr(expr
->right
);
354 if (right
->type
!= EXPR_CALL
)
356 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
359 const_expr
= get_argument_from_call_expr(right
->args
, 0);
360 expr1
= get_argument_from_call_expr(right
->args
, 1);
361 expr2
= get_argument_from_call_expr(right
->args
, 2);
363 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
366 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
371 void __split_expr(struct expression
*expr
)
376 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
378 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
380 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
383 push_expression(&big_expression_stack
, expr
);
384 set_position(expr
->pos
);
385 __pass_to_client(expr
, EXPR_HOOK
);
387 switch (expr
->type
) {
389 expr_set_parent_expr(expr
->unop
, expr
);
391 if (expr
->op
== '*' &&
392 !prev_expression_is_getting_address(expr
))
393 __pass_to_client(expr
, DEREF_HOOK
);
394 __split_expr(expr
->unop
);
395 __pass_to_client(expr
, OP_HOOK
);
398 expr_set_parent_expr(expr
->unop
, expr
);
400 __split_expr(expr
->unop
);
401 push_expression(&post_op_stack
, expr
);
405 if (expr
->statement
&& !expr
->statement
) {
406 stmt_set_parent_stmt(expr
->statement
,
407 last_ptr_list((struct ptr_list
*)big_statement_stack
));
409 __split_stmt(expr
->statement
);
414 expr_set_parent_expr(expr
->left
, expr
);
415 expr_set_parent_expr(expr
->right
, expr
);
417 __pass_to_client(expr
, LOGIC_HOOK
);
418 __handle_logic(expr
);
421 expr_set_parent_expr(expr
->left
, expr
);
422 expr_set_parent_expr(expr
->right
, expr
);
424 __pass_to_client(expr
, BINOP_HOOK
);
426 expr_set_parent_expr(expr
->left
, expr
);
427 expr_set_parent_expr(expr
->right
, expr
);
429 __split_expr(expr
->left
);
430 __process_post_op_stack();
431 __split_expr(expr
->right
);
433 case EXPR_ASSIGNMENT
: {
434 struct expression
*right
;
436 expr_set_parent_expr(expr
->left
, expr
);
437 expr_set_parent_expr(expr
->right
, expr
);
439 right
= strip_expr(expr
->right
);
443 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
446 if (__handle_condition_assigns(expr
))
448 /* foo = (x < 5 ? foo : 5); */
449 if (__handle_select_assigns(expr
))
451 /* foo = ({frob(); frob(); frob(); 1;}) */
452 if (__handle_expr_statement_assigns(expr
))
455 if (handle_comma_assigns(expr
))
457 if (handle_postop_assigns(expr
))
459 if (handle__builtin_choose_expr_assigns(expr
))
462 __split_expr(expr
->right
);
463 if (outside_of_function())
464 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
466 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
468 __fake_struct_member_assignments(expr
);
470 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
471 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
473 if (get_macro_name(right
->pos
) &&
474 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
475 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
477 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
479 __split_expr(expr
->left
);
483 expr_set_parent_expr(expr
->deref
, expr
);
485 __pass_to_client(expr
, DEREF_HOOK
);
486 __split_expr(expr
->deref
);
489 expr_set_parent_expr(expr
->base
, expr
);
491 __split_expr(expr
->base
);
494 case EXPR_FORCE_CAST
:
495 expr_set_parent_expr(expr
->cast_expression
, expr
);
497 __pass_to_client(expr
, CAST_HOOK
);
498 __split_expr(expr
->cast_expression
);
501 if (expr
->cast_expression
)
502 __pass_to_client(strip_parens(expr
->cast_expression
),
507 evaluate_expression(expr
);
509 case EXPR_CONDITIONAL
:
511 expr_set_parent_expr(expr
->conditional
, expr
);
512 expr_set_parent_expr(expr
->cond_true
, expr
);
513 expr_set_parent_expr(expr
->cond_false
, expr
);
515 if (known_condition_true(expr
->conditional
)) {
516 __split_expr(expr
->cond_true
);
519 if (known_condition_false(expr
->conditional
)) {
520 __split_expr(expr
->cond_false
);
523 __pass_to_client(expr
, SELECT_HOOK
);
524 __split_whole_condition(expr
->conditional
);
525 __split_expr(expr
->cond_true
);
526 __push_true_states();
527 __use_false_states();
528 __split_expr(expr
->cond_false
);
529 __merge_true_states();
532 expr_set_parent_expr(expr
->fn
, expr
);
534 if (sym_name_is("__builtin_constant_p", expr
->fn
))
536 if (handle__builtin_choose_expr(expr
))
538 split_expr_list(expr
->args
, expr
);
539 __split_expr(expr
->fn
);
540 if (is_inline_func(expr
->fn
))
541 add_inline_function(expr
->fn
->symbol
);
542 if (inlinable(expr
->fn
))
544 __process_post_op_stack();
545 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
546 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
548 if (inlinable(expr
->fn
)) {
551 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
552 if (is_noreturn_func(expr
->fn
))
554 handle_builtin_overflow_func(expr
);
556 case EXPR_INITIALIZER
:
557 split_expr_list(expr
->expr_list
, expr
);
559 case EXPR_IDENTIFIER
:
560 expr_set_parent_expr(expr
->ident_expression
, expr
);
561 __split_expr(expr
->ident_expression
);
564 expr_set_parent_expr(expr
->idx_expression
, expr
);
565 __split_expr(expr
->idx_expression
);
568 expr_set_parent_expr(expr
->init_expr
, expr
);
569 __split_expr(expr
->init_expr
);
572 __pass_to_client(expr
, SYM_HOOK
);
575 __pass_to_client(expr
, STRING_HOOK
);
580 pop_expression(&big_expression_stack
);
583 static int is_forever_loop(struct statement
*stmt
)
585 struct expression
*expr
;
588 expr
= strip_expr(stmt
->iterator_pre_condition
);
590 expr
= stmt
->iterator_post_condition
;
592 /* this is a for(;;) loop... */
596 if (get_value(expr
, &sval
) && sval
.value
!= 0)
603 static char *get_loop_name(int num
)
607 snprintf(buf
, 255, "-loop%d", num
);
609 return alloc_sname(buf
);
613 * Pre Loops are while and for loops.
615 static void handle_pre_loop(struct statement
*stmt
)
617 int once_through
; /* we go through the loop at least once */
618 struct sm_state
*extra_sm
= NULL
;
621 struct stree
*stree
= NULL
;
622 struct sm_state
*sm
= NULL
;
624 loop_name
= get_loop_name(loop_num
);
627 __split_stmt(stmt
->iterator_pre_statement
);
628 __prev_stmt
= stmt
->iterator_pre_statement
;
630 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
636 __merge_gotos(loop_name
, NULL
);
638 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
639 __in_pre_condition
++;
640 __pass_to_client(stmt
, PRELOOP_HOOK
);
641 __split_whole_condition(stmt
->iterator_pre_condition
);
642 __in_pre_condition
--;
643 FOR_EACH_SM(stree
, sm
) {
644 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
645 } END_FOR_EACH_SM(sm
);
648 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
650 if (option_assume_loops
)
653 __split_stmt(stmt
->iterator_statement
);
654 if (is_forever_loop(stmt
)) {
656 __save_gotos(loop_name
, NULL
);
658 __push_fake_cur_stree();
659 __split_stmt(stmt
->iterator_post_statement
);
660 stree
= __pop_fake_cur_stree();
662 __discard_false_states();
665 if (!__path_is_null())
666 __merge_stree_into_cur(stree
);
670 unchanged
= __iterator_unchanged(extra_sm
);
671 __split_stmt(stmt
->iterator_post_statement
);
672 __prev_stmt
= stmt
->iterator_post_statement
;
675 __save_gotos(loop_name
, NULL
);
676 __in_pre_condition
++;
677 __split_whole_condition(stmt
->iterator_pre_condition
);
678 __in_pre_condition
--;
680 __merge_false_states();
682 __discard_false_states();
684 __merge_false_states();
686 if (extra_sm
&& unchanged
)
687 __extra_pre_loop_hook_after(extra_sm
,
688 stmt
->iterator_post_statement
,
689 stmt
->iterator_pre_condition
);
696 * Post loops are do {} while();
698 static void handle_post_loop(struct statement
*stmt
)
702 loop_name
= get_loop_name(loop_num
);
708 __merge_gotos(loop_name
, NULL
);
709 __split_stmt(stmt
->iterator_statement
);
711 if (!is_zero(stmt
->iterator_post_condition
))
712 __save_gotos(loop_name
, NULL
);
714 if (is_forever_loop(stmt
)) {
717 __split_whole_condition(stmt
->iterator_post_condition
);
718 __use_false_states();
724 static int empty_statement(struct statement
*stmt
)
728 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
733 static int last_stmt_on_same_line(void)
735 struct statement
*stmt
;
738 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
741 if (stmt
->pos
.line
== get_lineno())
744 } END_FOR_EACH_PTR_REVERSE(stmt
);
748 static void split_asm_constraints(struct expression_list
*expr_list
)
750 struct expression
*expr
;
753 FOR_EACH_PTR(expr_list
, expr
) {
755 case 0: /* identifier */
756 case 1: /* constraint */
759 case 2: /* expression */
764 } END_FOR_EACH_PTR(expr
);
767 static int is_case_val(struct statement
*stmt
, sval_t sval
)
771 if (stmt
->type
!= STMT_CASE
)
773 if (!stmt
->case_expression
) {
777 if (!get_value(stmt
->case_expression
, &case_sval
))
779 if (case_sval
.value
== sval
.value
)
784 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
785 struct expression
*case_expr
,
786 struct expression
*case_to
)
789 struct range_list
*rl
= NULL
;
790 struct symbol
*switch_type
;
792 switch_type
= get_type(switch_expr
);
793 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
794 start
= sval_cast(switch_type
, start
);
795 end
= sval_cast(switch_type
, end
);
796 add_range(&rl
, start
, end
);
797 } else if (get_value(case_expr
, &start
)) {
798 start
= sval_cast(switch_type
, start
);
799 add_range(&rl
, start
, start
);
805 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
807 struct statement
*tmp
;
808 struct range_list
*rl
;
810 __split_expr(stmt
->switch_expression
);
811 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
813 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
814 __save_switch_states(top_expression(switch_expr_stack
));
819 stmt
= stmt
->switch_statement
;
821 __push_scope_hooks();
822 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
823 __smatch_lineno
= tmp
->pos
.line
;
824 if (is_case_val(tmp
, sval
)) {
825 rl
= alloc_rl(sval
, sval
);
826 __merge_switches(top_expression(switch_expr_stack
), rl
);
827 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
829 if (__path_is_null())
832 if (__path_is_null()) {
836 } END_FOR_EACH_PTR(tmp
);
838 __call_scope_hooks();
839 if (!__pop_default())
840 __merge_switches(top_expression(switch_expr_stack
), NULL
);
841 __discard_switches();
843 pop_expression(&switch_expr_stack
);
846 static void split_case(struct statement
*stmt
)
848 struct range_list
*rl
= NULL
;
850 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
851 expr_set_parent_stmt(stmt
->case_to
, stmt
);
853 rl
= get_case_rl(top_expression(switch_expr_stack
),
854 stmt
->case_expression
, stmt
->case_to
);
855 while (stmt
->case_statement
->type
== STMT_CASE
) {
856 struct range_list
*tmp
;
858 tmp
= get_case_rl(top_expression(switch_expr_stack
),
859 stmt
->case_statement
->case_expression
,
860 stmt
->case_statement
->case_to
);
863 rl
= rl_union(rl
, tmp
);
864 if (!stmt
->case_expression
)
866 stmt
= stmt
->case_statement
;
869 __merge_switches(top_expression(switch_expr_stack
), rl
);
871 if (!stmt
->case_expression
)
873 __split_stmt(stmt
->case_statement
);
876 int time_parsing_function(void)
878 return ms_since(&fn_start_time
) / 1000;
881 static int taking_too_long(void)
883 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
888 static int is_last_stmt(struct statement
*cur_stmt
)
890 struct symbol
*fn
= get_base_type(cur_func_sym
);
891 struct statement
*stmt
;
897 stmt
= fn
->inline_stmt
;
898 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
900 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
901 if (stmt
&& stmt
->type
== STMT_LABEL
)
902 stmt
= stmt
->label_statement
;
903 if (stmt
== cur_stmt
)
908 static void handle_backward_goto(struct statement
*goto_stmt
)
910 const char *goto_name
, *label_name
;
911 struct statement
*func_stmt
;
912 struct symbol
*base_type
= get_base_type(cur_func_sym
);
913 struct statement
*tmp
;
918 if (last_goto_statement_handled
)
920 last_goto_statement_handled
= 1;
922 if (!goto_stmt
->goto_label
||
923 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
924 !goto_stmt
->goto_label
->ident
)
926 goto_name
= goto_stmt
->goto_label
->ident
->name
;
928 func_stmt
= base_type
->stmt
;
930 func_stmt
= base_type
->inline_stmt
;
933 if (func_stmt
->type
!= STMT_COMPOUND
)
936 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
938 if (tmp
->type
!= STMT_LABEL
)
940 if (!tmp
->label_identifier
||
941 tmp
->label_identifier
->type
!= SYM_LABEL
||
942 !tmp
->label_identifier
->ident
)
944 label_name
= tmp
->label_identifier
->ident
->name
;
945 if (strcmp(goto_name
, label_name
) != 0)
950 } END_FOR_EACH_PTR(tmp
);
953 static void fake_a_return(void)
955 struct symbol
*return_type
;
960 return_type
= get_real_base_type(cur_func_sym
);
961 return_type
= get_real_base_type(return_type
);
962 if (return_type
!= &void_ctype
) {
963 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
968 static void fake_an_empty_default(struct position pos
)
970 static struct statement none
= {};
973 none
.type
= STMT_NONE
;
974 __merge_switches(top_expression(switch_expr_stack
), NULL
);
978 static void split_compound(struct statement
*stmt
)
980 struct statement
*prev
= NULL
;
981 struct statement
*cur
= NULL
;
982 struct statement
*next
;
984 __push_scope_hooks();
986 FOR_EACH_PTR(stmt
->stmts
, next
) {
987 /* just set them all ahead of time */
988 stmt_set_parent_stmt(next
, stmt
);
998 } END_FOR_EACH_PTR(next
);
1007 * For function scope, then delay calling the scope hooks until the
1008 * end of function hooks can run. I'm not positive this is the right
1011 if (!is_last_stmt(cur
))
1012 __call_scope_hooks();
1016 * This is a hack, work around for detecting empty functions.
1018 static int need_delayed_scope_hooks(void)
1020 struct symbol
*fn
= get_base_type(cur_func_sym
);
1021 struct statement
*stmt
;
1027 stmt
= fn
->inline_stmt
;
1028 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1033 void __split_label_stmt(struct statement
*stmt
)
1035 if (stmt
->label_identifier
&&
1036 stmt
->label_identifier
->type
== SYM_LABEL
&&
1037 stmt
->label_identifier
->ident
) {
1038 loop_count
|= 0x0800000;
1039 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1043 static void find_asm_gotos(struct statement
*stmt
)
1047 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1048 __save_gotos(sym
->ident
->name
, sym
);
1049 } END_FOR_EACH_PTR(sym
);
1052 void __split_stmt(struct statement
*stmt
)
1059 if (!__in_fake_assign
)
1060 __silence_warnings_for_stmt
= false;
1062 if (__bail_on_rest_of_function
)
1065 if (out_of_memory() || taking_too_long()) {
1066 struct timeval stop
;
1068 gettimeofday(&stop
, NULL
);
1070 __bail_on_rest_of_function
= 1;
1072 sm_msg("Function too hairy. Giving up. %lu seconds",
1073 stop
.tv_sec
- fn_start_time
.tv_sec
);
1075 final_pass
= 0; /* turn off sm_msg() from here */
1079 add_ptr_list(&big_statement_stack
, stmt
);
1080 free_expression_stack(&big_expression_stack
);
1081 set_position(stmt
->pos
);
1082 __pass_to_client(stmt
, STMT_HOOK
);
1084 switch (stmt
->type
) {
1085 case STMT_DECLARATION
:
1086 split_declaration(stmt
->declaration
);
1089 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1091 __split_expr(stmt
->ret_value
);
1092 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1093 __process_post_op_stack();
1096 case STMT_EXPRESSION
:
1097 expr_set_parent_stmt(stmt
->expression
, stmt
);
1098 expr_set_parent_stmt(stmt
->context
, stmt
);
1100 __split_expr(stmt
->expression
);
1103 split_compound(stmt
);
1106 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1107 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1108 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1110 if (known_condition_true(stmt
->if_conditional
)) {
1111 __split_stmt(stmt
->if_true
);
1114 if (known_condition_false(stmt
->if_conditional
)) {
1115 __split_stmt(stmt
->if_false
);
1118 __split_whole_condition(stmt
->if_conditional
);
1119 __split_stmt(stmt
->if_true
);
1120 if (empty_statement(stmt
->if_true
) &&
1121 last_stmt_on_same_line() &&
1122 !get_macro_name(stmt
->if_true
->pos
))
1123 sm_msg("warn: if();");
1124 __push_true_states();
1125 __use_false_states();
1126 __split_stmt(stmt
->if_false
);
1127 __merge_true_states();
1130 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1131 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1132 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1133 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1134 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1136 if (stmt
->iterator_pre_condition
)
1137 handle_pre_loop(stmt
);
1138 else if (stmt
->iterator_post_condition
)
1139 handle_post_loop(stmt
);
1141 // these are for(;;) type loops.
1142 handle_pre_loop(stmt
);
1146 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1147 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1149 if (get_value(stmt
->switch_expression
, &sval
)) {
1150 split_known_switch(stmt
, sval
);
1153 __split_expr(stmt
->switch_expression
);
1154 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1155 __save_switch_states(top_expression(switch_expr_stack
));
1159 __split_stmt(stmt
->switch_statement
);
1160 if (!__pop_default() && have_remaining_cases())
1161 fake_an_empty_default(stmt
->pos
);
1162 __discard_switches();
1164 pop_expression(&switch_expr_stack
);
1170 __split_label_stmt(stmt
);
1171 __split_stmt(stmt
->label_statement
);
1174 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1176 __split_expr(stmt
->goto_expression
);
1177 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1178 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1180 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1182 __process_continues();
1184 } else if (stmt
->goto_label
&&
1185 stmt
->goto_label
->type
== SYM_LABEL
&&
1186 stmt
->goto_label
->ident
) {
1187 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1190 if (is_last_stmt(stmt
))
1191 handle_backward_goto(stmt
);
1196 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1198 find_asm_gotos(stmt
);
1199 __pass_to_client(stmt
, ASM_HOOK
);
1200 __split_expr(stmt
->asm_string
);
1201 split_asm_constraints(stmt
->asm_outputs
);
1202 split_asm_constraints(stmt
->asm_inputs
);
1203 split_asm_constraints(stmt
->asm_clobbers
);
1208 __split_expr(stmt
->range_expression
);
1209 __split_expr(stmt
->range_low
);
1210 __split_expr(stmt
->range_high
);
1213 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1215 __process_post_op_stack();
1218 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1220 struct expression
*expr
;
1222 FOR_EACH_PTR(expr_list
, expr
) {
1223 expr_set_parent_expr(expr
, parent
);
1225 __process_post_op_stack();
1226 } END_FOR_EACH_PTR(expr
);
1229 static void split_sym(struct symbol
*sym
)
1233 if (!(sym
->namespace & NS_SYMBOL
))
1236 __split_stmt(sym
->stmt
);
1237 __split_expr(sym
->array_size
);
1238 split_symlist(sym
->arguments
);
1239 split_symlist(sym
->symbol_list
);
1240 __split_stmt(sym
->inline_stmt
);
1241 split_symlist(sym
->inline_symbol_list
);
1244 static void split_symlist(struct symbol_list
*sym_list
)
1248 FOR_EACH_PTR(sym_list
, sym
) {
1250 } END_FOR_EACH_PTR(sym
);
1253 typedef void (fake_cb
)(struct expression
*expr
);
1255 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1257 struct symbol
*type
, *tmp
;
1263 name
= member
->name
;
1265 type
= get_type(expr
);
1266 if (!type
|| type
->type
!= SYM_STRUCT
)
1270 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1274 if (strcmp(name
, tmp
->ident
->name
) == 0)
1276 } END_FOR_EACH_PTR(tmp
);
1280 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1282 struct symbol
*type
, *member
;
1285 type
= get_type(expr
);
1286 if (!type
|| type
->type
!= SYM_STRUCT
)
1289 FOR_EACH_PTR(type
->symbol_list
, member
) {
1291 return member
->ident
;
1293 } END_FOR_EACH_PTR(member
);
1297 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1299 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1301 struct expression
*edge_member
, *assign
;
1302 struct symbol
*base
= get_real_base_type(member
);
1306 expr
= member_expression(expr
, '.', member
->ident
);
1308 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1309 struct symbol
*type
;
1311 type
= get_real_base_type(tmp
);
1315 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1316 if (get_extra_state(edge_member
))
1319 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1320 set_inner_struct_members(expr
, tmp
);
1327 assign
= assign_expression(edge_member
, '=', zero_expr());
1328 __split_expr(assign
);
1329 } END_FOR_EACH_PTR(tmp
);
1334 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1337 struct expression
*member
= NULL
;
1338 struct expression
*assign
;
1341 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1342 expr
= strip_expr(expr
->unop
);
1346 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1347 type
= get_real_base_type(tmp
);
1352 member
= member_expression(expr
, op
, tmp
->ident
);
1353 if (get_extra_state(member
))
1357 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1358 set_inner_struct_members(expr
, tmp
);
1361 if (type
->type
== SYM_ARRAY
)
1366 assign
= assign_expression(member
, '=', zero_expr());
1367 __split_expr(assign
);
1368 } END_FOR_EACH_PTR(tmp
);
1371 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1373 struct expression
*deref
, *assign
, *tmp
, *right
;
1374 struct symbol
*struct_type
, *type
;
1375 struct ident
*member
;
1378 struct_type
= get_type(symbol
);
1380 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1384 * We're parsing an initializer that could look something like this:
1385 * struct foo foo = {
1387 * .whatever.xxx = 11,
1391 * So what we have here is a list with 42, .whatever, and .zzz. We need
1392 * to break it up into left and right sides of the assignments.
1396 FOR_EACH_PTR(members
, tmp
) {
1398 if (tmp
->type
== EXPR_IDENTIFIER
) {
1399 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1400 while (tmp
->type
== EXPR_IDENTIFIER
) {
1401 member
= tmp
->expr_ident
;
1402 tmp
= tmp
->ident_expression
;
1404 deref
= member_expression(deref
, '.', member
);
1406 deref
= member_expression(symbol
, '.', member
);
1409 member
= number_to_member(symbol
, member_idx
);
1410 deref
= member_expression(symbol
, '.', member
);
1414 if (right
->type
== EXPR_INITIALIZER
) {
1415 type
= get_type(deref
);
1416 if (type
&& type
->type
== SYM_ARRAY
)
1417 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1419 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1421 assign
= assign_expression(deref
, '=', right
);
1424 } END_FOR_EACH_PTR(tmp
);
1426 set_unset_to_zero(struct_type
, symbol
);
1429 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1431 fake_member_assigns_helper(symbol_expression(sym
),
1432 sym
->initializer
->expr_list
, fake_cb
);
1435 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1437 struct expression
*offset
, *binop
, *assign
, *tmp
;
1438 struct symbol
*type
;
1441 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1445 FOR_EACH_PTR(expr_list
, tmp
) {
1446 if (tmp
->type
== EXPR_INDEX
) {
1447 if (tmp
->idx_from
!= tmp
->idx_to
)
1449 idx
= tmp
->idx_from
;
1450 if (!tmp
->idx_expression
)
1452 tmp
= tmp
->idx_expression
;
1454 offset
= value_expr(idx
);
1455 binop
= array_element_expression(array
, offset
);
1456 if (tmp
->type
== EXPR_INITIALIZER
) {
1457 type
= get_type(binop
);
1458 if (type
&& type
->type
== SYM_ARRAY
)
1459 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1461 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1463 assign
= assign_expression(binop
, '=', tmp
);
1468 } END_FOR_EACH_PTR(tmp
);
1471 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1473 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1476 static void fake_assign_expr(struct symbol
*sym
)
1478 struct expression
*assign
, *symbol
;
1480 symbol
= symbol_expression(sym
);
1481 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1482 __split_expr(assign
);
1485 static void do_initializer_stuff(struct symbol
*sym
)
1487 if (!sym
->initializer
)
1490 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1491 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1492 fake_element_assigns(sym
, __split_expr
);
1494 fake_member_assigns(sym
, __split_expr
);
1496 fake_assign_expr(sym
);
1500 static void split_declaration(struct symbol_list
*sym_list
)
1504 FOR_EACH_PTR(sym_list
, sym
) {
1505 __pass_to_client(sym
, DECLARATION_HOOK
);
1506 do_initializer_stuff(sym
);
1508 } END_FOR_EACH_PTR(sym
);
1511 static void call_global_assign_hooks(struct expression
*assign
)
1513 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1516 static void fake_global_assign(struct symbol
*sym
)
1518 struct expression
*assign
, *symbol
;
1520 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1521 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1522 fake_element_assigns(sym
, call_global_assign_hooks
);
1523 } else if (sym
->initializer
) {
1524 symbol
= symbol_expression(sym
);
1525 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1526 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1528 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1530 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1531 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1532 fake_member_assigns(sym
, call_global_assign_hooks
);
1533 } else if (sym
->initializer
) {
1534 symbol
= symbol_expression(sym
);
1535 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1536 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1538 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1541 symbol
= symbol_expression(sym
);
1542 if (sym
->initializer
) {
1543 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1544 __split_expr(assign
);
1546 assign
= assign_expression(symbol
, '=', zero_expr());
1548 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1552 static void start_function_definition(struct symbol
*sym
)
1554 __in_function_def
= 1;
1555 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1556 __in_function_def
= 0;
1557 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1561 static void split_function(struct symbol
*sym
)
1563 struct symbol
*base_type
= get_base_type(sym
);
1564 struct timeval stop
;
1566 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1569 gettimeofday(&outer_fn_start_time
, NULL
);
1570 gettimeofday(&fn_start_time
, NULL
);
1573 cur_func
= sym
->ident
->name
;
1574 set_position(sym
->pos
);
1576 last_goto_statement_handled
= 0;
1577 sm_debug("new function: %s\n", cur_func
);
1579 if (option_two_passes
) {
1583 start_function_definition(sym
);
1584 __split_stmt(base_type
->stmt
);
1585 __split_stmt(base_type
->inline_stmt
);
1591 start_function_definition(sym
);
1592 __split_stmt(base_type
->stmt
);
1593 __split_stmt(base_type
->inline_stmt
);
1594 __pass_to_client(sym
, END_FUNC_HOOK
);
1595 if (need_delayed_scope_hooks())
1596 __call_scope_hooks();
1597 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1601 gettimeofday(&stop
, NULL
);
1602 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1604 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1607 cur_func_sym
= NULL
;
1609 free_data_info_allocs();
1610 free_expression_stack(&switch_expr_stack
);
1611 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1612 __bail_on_rest_of_function
= 0;
1615 static void save_flow_state(void)
1617 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1618 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1619 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1621 __add_ptr_list(&backup
, big_statement_stack
, 0);
1622 __add_ptr_list(&backup
, big_expression_stack
, 0);
1623 __add_ptr_list(&backup
, big_condition_stack
, 0);
1624 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1626 __add_ptr_list(&backup
, cur_func_sym
, 0);
1628 __add_ptr_list(&backup
, __prev_stmt
, 0);
1629 __add_ptr_list(&backup
, __cur_stmt
, 0);
1630 __add_ptr_list(&backup
, __next_stmt
, 0);
1634 static void *pop_backup(void)
1638 ret
= last_ptr_list(backup
);
1639 delete_ptr_list_last(&backup
);
1643 static void restore_flow_state(void)
1645 __next_stmt
= pop_backup();
1646 __cur_stmt
= pop_backup();
1647 __prev_stmt
= pop_backup();
1649 cur_func_sym
= pop_backup();
1650 switch_expr_stack
= pop_backup();
1651 big_condition_stack
= pop_backup();
1652 big_expression_stack
= pop_backup();
1653 big_statement_stack
= pop_backup();
1654 final_pass
= PTR_INT(pop_backup()) >> 2;
1655 loop_count
= PTR_INT(pop_backup()) >> 2;
1656 loop_num
= PTR_INT(pop_backup()) >> 2;
1659 static void parse_inline(struct expression
*call
)
1661 struct symbol
*base_type
;
1662 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1663 struct timeval time_backup
= fn_start_time
;
1664 struct expression
*orig_inline
= __inline_fn
;
1667 if (out_of_memory() || taking_too_long())
1672 __pass_to_client(call
, INLINE_FN_START
);
1673 final_pass
= 0; /* don't print anything */
1675 orig_budget
= inline_budget
;
1676 inline_budget
= inline_budget
- 5;
1678 base_type
= get_base_type(call
->fn
->symbol
);
1679 cur_func_sym
= call
->fn
->symbol
;
1680 if (call
->fn
->symbol
->ident
)
1681 cur_func
= call
->fn
->symbol
->ident
->name
;
1684 set_position(call
->fn
->symbol
->pos
);
1687 big_statement_stack
= NULL
;
1688 big_expression_stack
= NULL
;
1689 big_condition_stack
= NULL
;
1690 switch_expr_stack
= NULL
;
1692 sm_debug("inline function: %s\n", cur_func
);
1696 start_function_definition(call
->fn
->symbol
);
1697 __split_stmt(base_type
->stmt
);
1698 __split_stmt(base_type
->inline_stmt
);
1699 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1700 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1702 free_expression_stack(&switch_expr_stack
);
1703 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1707 restore_flow_state();
1708 fn_start_time
= time_backup
;
1709 cur_func
= cur_func_bak
;
1711 restore_all_states();
1712 set_position(call
->pos
);
1713 __inline_fn
= orig_inline
;
1714 inline_budget
= orig_budget
;
1715 __pass_to_client(call
, INLINE_FN_END
);
1718 static struct symbol_list
*inlines_called
;
1719 static void add_inline_function(struct symbol
*sym
)
1721 static struct symbol_list
*already_added
;
1724 FOR_EACH_PTR(already_added
, tmp
) {
1727 } END_FOR_EACH_PTR(tmp
);
1729 add_ptr_list(&already_added
, sym
);
1730 add_ptr_list(&inlines_called
, sym
);
1733 static void process_inlines(void)
1737 FOR_EACH_PTR(inlines_called
, tmp
) {
1738 split_function(tmp
);
1739 } END_FOR_EACH_PTR(tmp
);
1740 free_ptr_list(&inlines_called
);
1743 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1747 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1750 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1752 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1754 } END_FOR_EACH_PTR_REVERSE(sym
);
1759 static bool interesting_function(struct symbol
*sym
)
1761 static int prev_stream
= -1;
1762 static bool prev_answer
;
1763 const char *filename
;
1766 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1769 if (sym
->pos
.stream
== prev_stream
)
1772 prev_stream
= sym
->pos
.stream
;
1773 prev_answer
= false;
1775 filename
= stream_name(sym
->pos
.stream
);
1776 len
= strlen(filename
);
1777 if (len
> 0 && filename
[len
- 1] == 'c')
1782 static void split_inlines_in_scope(struct symbol
*sym
)
1784 struct symbol
*base
;
1785 struct symbol_list
*scope_list
;
1788 scope_list
= sym
->scope
->symbols
;
1789 stream
= sym
->pos
.stream
;
1791 /* find the last static symbol in the file */
1792 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1793 if (sym
->pos
.stream
!= stream
)
1795 if (sym
->type
!= SYM_NODE
)
1797 base
= get_base_type(sym
);
1800 if (base
->type
!= SYM_FN
)
1802 if (!base
->inline_stmt
)
1804 if (!interesting_function(sym
))
1806 add_inline_function(sym
);
1807 } END_FOR_EACH_PTR_REVERSE(sym
);
1812 static void split_inlines(struct symbol_list
*sym_list
)
1816 sym
= get_last_scoped_symbol(sym_list
, 0);
1818 split_inlines_in_scope(sym
);
1819 sym
= get_last_scoped_symbol(sym_list
, 1);
1821 split_inlines_in_scope(sym
);
1824 static struct stree
*clone_estates_perm(struct stree
*orig
)
1826 struct stree
*ret
= NULL
;
1827 struct sm_state
*tmp
;
1829 FOR_EACH_SM(orig
, tmp
) {
1830 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1831 } END_FOR_EACH_SM(tmp
);
1836 struct position last_pos
;
1837 static void split_c_file_functions(struct symbol_list
*sym_list
)
1842 FOR_EACH_PTR(sym_list
, sym
) {
1843 set_position(sym
->pos
);
1844 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1845 __pass_to_client(sym
, BASE_HOOK
);
1846 fake_global_assign(sym
);
1848 } END_FOR_EACH_PTR(sym
);
1849 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1852 FOR_EACH_PTR(sym_list
, sym
) {
1853 set_position(sym
->pos
);
1854 last_pos
= sym
->pos
;
1855 if (!interesting_function(sym
))
1857 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1858 split_function(sym
);
1861 last_pos
= sym
->pos
;
1862 } END_FOR_EACH_PTR(sym
);
1863 split_inlines(sym_list
);
1864 __pass_to_client(sym_list
, END_FILE_HOOK
);
1867 static int final_before_fake
;
1868 void init_fake_env(void)
1871 final_before_fake
= final_pass
;
1873 __push_fake_cur_stree();
1877 void end_fake_env(void)
1879 __pop_fake_cur_stree();
1882 final_pass
= final_before_fake
;
1885 static void open_output_files(char *base_file
)
1889 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1890 sm_outfd
= fopen(buf
, "w");
1892 printf("Error: Cannot open %s\n", buf
);
1899 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1900 sql_outfd
= fopen(buf
, "w");
1902 printf("Error: Cannot open %s\n", buf
);
1906 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1907 caller_info_fd
= fopen(buf
, "w");
1908 if (!caller_info_fd
) {
1909 printf("Error: Cannot open %s\n", buf
);
1914 void smatch(int argc
, char **argv
)
1916 struct string_list
*filelist
= NULL
;
1917 struct symbol_list
*sym_list
;
1918 struct timeval stop
, start
;
1922 gettimeofday(&start
, NULL
);
1925 printf("Usage: smatch [--debug] <filename.c>\n");
1928 sparse_initialize(argc
, argv
, &filelist
);
1929 set_valid_ptr_max();
1930 alloc_valid_ptr_rl();
1931 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1932 path
= getcwd(NULL
, 0);
1933 free(full_base_file
);
1935 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
1936 full_base_file
= malloc(len
);
1937 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
1939 full_base_file
= alloc_string(base_file
);
1941 if (option_file_output
)
1942 open_output_files(base_file
);
1943 sym_list
= sparse_keep_tokens(base_file
);
1944 split_c_file_functions(sym_list
);
1945 } END_FOR_EACH_PTR_NOTAG(base_file
);
1947 gettimeofday(&stop
, NULL
);
1949 set_position(last_pos
);
1951 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1953 sm_msg("mem: %luKb", get_max_memory());