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
;
30 int __in_fake_var_assign
;
35 struct expression
*__inline_fn
;
37 static int __smatch_lineno
= 0;
39 static char *base_file
;
40 static const char *filename
;
41 static char *pathname
;
42 static char *full_filename
;
43 static char *full_base_file
;
44 static char *cur_func
;
45 static unsigned int loop_count
;
46 static int last_goto_statement_handled
;
47 int __expr_stmt_count
;
48 int __in_function_def
;
49 int __in_unmatched_hook
;
50 static struct expression_list
*switch_expr_stack
= NULL
;
51 static struct expression_list
*post_op_stack
= NULL
;
53 static struct ptr_list
*fn_data_list
;
54 static struct ptr_list
*backup
;
56 struct expression_list
*big_expression_stack
;
57 struct statement_list
*big_statement_stack
;
58 struct statement
*__prev_stmt
;
59 struct statement
*__cur_stmt
;
60 struct statement
*__next_stmt
;
61 int __in_pre_condition
= 0;
62 int __bail_on_rest_of_function
= 0;
63 static struct timeval fn_start_time
;
64 static struct timeval outer_fn_start_time
;
65 char *get_function(void) { return cur_func
; }
66 int get_lineno(void) { return __smatch_lineno
; }
67 int inside_loop(void) { return !!loop_count
; }
68 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
69 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
70 int in_expression_statement(void) { return !!__expr_stmt_count
; }
72 static void split_symlist(struct symbol_list
*sym_list
);
73 static void split_declaration(struct symbol_list
*sym_list
);
74 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
75 static void split_args(struct expression
*expr
);
76 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*expr
);
77 static void add_inline_function(struct symbol
*sym
);
78 static void parse_inline(struct expression
*expr
);
80 int option_assume_loops
= 0;
81 int option_two_passes
= 0;
82 struct symbol
*cur_func_sym
= NULL
;
83 struct stree
*global_states
;
85 const unsigned long valid_ptr_min
= 4096;
86 unsigned long valid_ptr_max
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
);
87 const sval_t valid_ptr_min_sval
= {
91 sval_t valid_ptr_max_sval
= {
93 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
95 struct range_list
*valid_ptr_rl
;
97 void alloc_valid_ptr_rl(void)
99 valid_ptr_max
= sval_type_max(&ulong_ctype
).value
& ~(MTAG_OFFSET_MASK
);
100 valid_ptr_max_sval
.value
= valid_ptr_max
;
102 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
103 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
104 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
107 int outside_of_function(void)
109 return cur_func_sym
== NULL
;
112 const char *get_filename(void)
114 if (option_info
&& option_full_path
)
115 return full_base_file
;
118 if (option_full_path
)
119 return full_filename
;
123 const char *get_base_file(void)
125 if (option_full_path
)
126 return full_base_file
;
130 static void set_position(struct position pos
)
133 static int prev_stream
= -1;
138 if (pos
.stream
== 0 && pos
.line
== 0)
141 __smatch_lineno
= pos
.line
;
143 if (pos
.stream
== prev_stream
)
146 filename
= stream_name(pos
.stream
);
149 pathname
= getcwd(NULL
, 0);
151 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
152 full_filename
= malloc(len
);
153 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
155 full_filename
= alloc_string(filename
);
160 int is_assigned_call(struct expression
*expr
)
162 struct expression
*parent
= expr_get_parent_expr(expr
);
165 parent
->type
== EXPR_ASSIGNMENT
&&
167 strip_expr(parent
->right
) == expr
)
173 int is_fake_assigned_call(struct expression
*expr
)
175 struct expression
*parent
= expr_get_fake_parent_expr(expr
);
178 parent
->type
== EXPR_ASSIGNMENT
&&
180 strip_expr(parent
->right
) == expr
)
186 static bool is_inline_func(struct expression
*expr
)
188 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
190 if (!expr
->symbol
->definition
)
192 if (expr
->symbol
->definition
->ctype
.modifiers
& MOD_INLINE
)
198 static int is_noreturn_func(struct expression
*expr
)
200 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
202 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
207 static int inline_budget
= 20;
209 int inlinable(struct expression
*expr
)
212 struct statement
*last_stmt
= NULL
;
214 if (__inline_fn
) /* don't nest */
217 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
219 if (is_no_inline_function(expr
->symbol
->ident
->name
))
221 sym
= get_base_type(expr
->symbol
);
222 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
223 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
225 if (sym
->stmt
->type
!= STMT_COMPOUND
)
227 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
229 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
230 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
232 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
234 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
240 /* the magic numbers in this function are pulled out of my bum. */
241 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
247 void __process_post_op_stack(void)
249 struct expression
*expr
;
251 FOR_EACH_PTR(post_op_stack
, expr
) {
252 __pass_to_client(expr
, OP_HOOK
);
253 } END_FOR_EACH_PTR(expr
);
255 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
258 static int handle_comma_assigns(struct expression
*expr
)
260 struct expression
*right
;
261 struct expression
*assign
;
263 right
= strip_expr(expr
->right
);
264 if (right
->type
!= EXPR_COMMA
)
267 __split_expr(right
->left
);
268 __process_post_op_stack();
270 assign
= assign_expression(expr
->left
, '=', right
->right
);
271 __split_expr(assign
);
276 /* This is to handle *p++ = foo; assignments */
277 static int handle_postop_assigns(struct expression
*expr
)
279 struct expression
*left
, *fake_left
;
280 struct expression
*assign
;
282 left
= strip_expr(expr
->left
);
283 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
285 left
= strip_expr(left
->unop
);
286 if (left
->type
!= EXPR_POSTOP
)
289 fake_left
= deref_expression(strip_expr(left
->unop
));
290 assign
= assign_expression(fake_left
, '=', expr
->right
);
292 __split_expr(assign
);
293 __split_expr(expr
->left
);
298 static int prev_expression_is_getting_address(struct expression
*expr
)
300 struct expression
*parent
;
303 parent
= expr_get_parent_expr(expr
);
307 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
309 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
311 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
320 static void handle_builtin_overflow_func(struct expression
*expr
)
322 struct expression
*a
, *b
, *res
, *assign
;
325 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
327 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
329 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
334 a
= get_argument_from_call_expr(expr
->args
, 0);
335 b
= get_argument_from_call_expr(expr
->args
, 1);
336 res
= get_argument_from_call_expr(expr
->args
, 2);
338 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
339 __split_expr(assign
);
342 static int handle__builtin_choose_expr(struct expression
*expr
)
344 struct expression
*const_expr
, *expr1
, *expr2
;
347 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
350 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
351 expr1
= get_argument_from_call_expr(expr
->args
, 1);
352 expr2
= get_argument_from_call_expr(expr
->args
, 2);
354 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
363 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
365 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
368 right
= strip_expr(expr
->right
);
369 if (right
->type
!= EXPR_CALL
)
371 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
374 const_expr
= get_argument_from_call_expr(right
->args
, 0);
375 expr1
= get_argument_from_call_expr(right
->args
, 1);
376 expr2
= get_argument_from_call_expr(right
->args
, 2);
378 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
381 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
386 void __split_expr(struct expression
*expr
)
391 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
393 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
395 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
398 push_expression(&big_expression_stack
, expr
);
399 set_position(expr
->pos
);
400 __pass_to_client(expr
, EXPR_HOOK
);
402 switch (expr
->type
) {
404 expr_set_parent_expr(expr
->unop
, expr
);
406 if (expr
->op
== '*' &&
407 !prev_expression_is_getting_address(expr
))
408 __pass_to_client(expr
, DEREF_HOOK
);
409 __split_expr(expr
->unop
);
410 __pass_to_client(expr
, OP_HOOK
);
413 expr_set_parent_expr(expr
->unop
, expr
);
415 __split_expr(expr
->unop
);
416 push_expression(&post_op_stack
, expr
);
420 if (expr
->statement
&& !expr
->statement
) {
421 stmt_set_parent_stmt(expr
->statement
,
422 last_ptr_list((struct ptr_list
*)big_statement_stack
));
424 __split_stmt(expr
->statement
);
429 expr_set_parent_expr(expr
->left
, expr
);
430 expr_set_parent_expr(expr
->right
, expr
);
432 __pass_to_client(expr
, LOGIC_HOOK
);
433 __handle_logic(expr
);
436 expr_set_parent_expr(expr
->left
, expr
);
437 expr_set_parent_expr(expr
->right
, expr
);
439 __pass_to_client(expr
, BINOP_HOOK
);
441 expr_set_parent_expr(expr
->left
, expr
);
442 expr_set_parent_expr(expr
->right
, expr
);
444 __split_expr(expr
->left
);
445 __process_post_op_stack();
446 __split_expr(expr
->right
);
448 case EXPR_ASSIGNMENT
: {
449 struct expression
*right
;
451 expr_set_parent_expr(expr
->left
, expr
);
452 expr_set_parent_expr(expr
->right
, expr
);
454 right
= strip_expr(expr
->right
);
458 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
461 if (__handle_condition_assigns(expr
))
463 /* foo = (x < 5 ? foo : 5); */
464 if (__handle_select_assigns(expr
))
466 /* foo = ({frob(); frob(); frob(); 1;}) */
467 if (__handle_expr_statement_assigns(expr
))
468 break; // FIXME: got after
470 if (handle_comma_assigns(expr
))
472 if (handle__builtin_choose_expr_assigns(expr
))
474 if (handle_postop_assigns(expr
))
475 break; /* no need to goto after_assign */
477 __split_expr(expr
->right
);
478 if (outside_of_function())
479 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
481 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
483 __fake_struct_member_assignments(expr
);
485 /* Re-examine ->right for inlines. See the commit message */
486 right
= strip_expr(expr
->right
);
487 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
488 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
490 if (get_macro_name(right
->pos
) &&
491 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
492 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
495 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
496 __split_expr(expr
->left
);
500 expr_set_parent_expr(expr
->deref
, expr
);
502 __pass_to_client(expr
, DEREF_HOOK
);
503 __split_expr(expr
->deref
);
506 expr_set_parent_expr(expr
->base
, expr
);
508 __split_expr(expr
->base
);
511 case EXPR_FORCE_CAST
:
512 expr_set_parent_expr(expr
->cast_expression
, expr
);
514 __pass_to_client(expr
, CAST_HOOK
);
515 __split_expr(expr
->cast_expression
);
518 if (expr
->cast_expression
)
519 __pass_to_client(strip_parens(expr
->cast_expression
),
525 case EXPR_CONDITIONAL
:
527 expr_set_parent_expr(expr
->conditional
, expr
);
528 expr_set_parent_expr(expr
->cond_true
, expr
);
529 expr_set_parent_expr(expr
->cond_false
, expr
);
531 if (known_condition_true(expr
->conditional
)) {
532 __split_expr(expr
->cond_true
);
535 if (known_condition_false(expr
->conditional
)) {
536 __split_expr(expr
->cond_false
);
539 __pass_to_client(expr
, SELECT_HOOK
);
540 __split_whole_condition(expr
->conditional
);
541 __split_expr(expr
->cond_true
);
542 __push_true_states();
543 __use_false_states();
544 __split_expr(expr
->cond_false
);
545 __merge_true_states();
548 expr_set_parent_expr(expr
->fn
, expr
);
550 if (sym_name_is("__builtin_constant_p", expr
->fn
))
552 if (handle__builtin_choose_expr(expr
))
554 __split_expr(expr
->fn
);
556 if (is_inline_func(expr
->fn
))
557 add_inline_function(expr
->fn
->symbol
->definition
);
558 if (inlinable(expr
->fn
))
560 __process_post_op_stack();
561 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
562 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
564 if (inlinable(expr
->fn
))
566 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
567 if (is_noreturn_func(expr
->fn
))
569 handle_builtin_overflow_func(expr
);
571 case EXPR_INITIALIZER
:
572 split_expr_list(expr
->expr_list
, expr
);
574 case EXPR_IDENTIFIER
:
575 expr_set_parent_expr(expr
->ident_expression
, expr
);
576 __split_expr(expr
->ident_expression
);
579 expr_set_parent_expr(expr
->idx_expression
, expr
);
580 __split_expr(expr
->idx_expression
);
583 expr_set_parent_expr(expr
->init_expr
, expr
);
584 __split_expr(expr
->init_expr
);
587 __pass_to_client(expr
, SYM_HOOK
);
590 __pass_to_client(expr
, STRING_HOOK
);
595 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
596 pop_expression(&big_expression_stack
);
599 static int is_forever_loop(struct statement
*stmt
)
601 struct expression
*expr
;
604 expr
= strip_expr(stmt
->iterator_pre_condition
);
606 expr
= stmt
->iterator_post_condition
;
608 /* this is a for(;;) loop... */
612 if (get_value(expr
, &sval
) && sval
.value
!= 0)
619 static char *get_loop_name(int num
)
623 snprintf(buf
, 255, "-loop%d", num
);
625 return alloc_sname(buf
);
629 * Pre Loops are while and for loops.
631 static void handle_pre_loop(struct statement
*stmt
)
633 int once_through
; /* we go through the loop at least once */
634 struct sm_state
*extra_sm
= NULL
;
637 struct stree
*stree
= NULL
;
638 struct sm_state
*sm
= NULL
;
640 loop_name
= get_loop_name(loop_num
);
643 __split_stmt(stmt
->iterator_pre_statement
);
644 __prev_stmt
= stmt
->iterator_pre_statement
;
646 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
652 __merge_gotos(loop_name
, NULL
);
654 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
655 __in_pre_condition
++;
656 __pass_to_client(stmt
, PRELOOP_HOOK
);
657 __split_whole_condition(stmt
->iterator_pre_condition
);
658 __in_pre_condition
--;
659 FOR_EACH_SM(stree
, sm
) {
660 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
661 } END_FOR_EACH_SM(sm
);
664 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
666 if (option_assume_loops
)
669 __split_stmt(stmt
->iterator_statement
);
670 if (is_forever_loop(stmt
)) {
672 __save_gotos(loop_name
, NULL
);
674 __push_fake_cur_stree();
675 __split_stmt(stmt
->iterator_post_statement
);
676 stree
= __pop_fake_cur_stree();
678 __discard_false_states();
681 if (!__path_is_null())
682 __merge_stree_into_cur(stree
);
686 unchanged
= __iterator_unchanged(extra_sm
);
687 __split_stmt(stmt
->iterator_post_statement
);
688 __prev_stmt
= stmt
->iterator_post_statement
;
691 __save_gotos(loop_name
, NULL
);
692 __in_pre_condition
++;
693 __split_whole_condition(stmt
->iterator_pre_condition
);
694 __in_pre_condition
--;
696 __merge_false_states();
698 __discard_false_states();
700 __merge_false_states();
702 if (extra_sm
&& unchanged
)
703 __extra_pre_loop_hook_after(extra_sm
,
704 stmt
->iterator_post_statement
,
705 stmt
->iterator_pre_condition
);
712 * Post loops are do {} while();
714 static void handle_post_loop(struct statement
*stmt
)
718 loop_name
= get_loop_name(loop_num
);
724 __merge_gotos(loop_name
, NULL
);
725 __split_stmt(stmt
->iterator_statement
);
727 if (!expr_is_zero(stmt
->iterator_post_condition
))
728 __save_gotos(loop_name
, NULL
);
730 if (is_forever_loop(stmt
)) {
733 __split_whole_condition(stmt
->iterator_post_condition
);
734 __use_false_states();
740 static int empty_statement(struct statement
*stmt
)
744 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
749 static int last_stmt_on_same_line(void)
751 struct statement
*stmt
;
754 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
757 if (stmt
->pos
.line
== get_lineno())
760 } END_FOR_EACH_PTR_REVERSE(stmt
);
764 static void split_asm_ops(struct asm_operand_list
*ops
)
766 struct asm_operand
*op
;
768 FOR_EACH_PTR(ops
, op
) {
769 __split_expr(op
->expr
);
770 } END_FOR_EACH_PTR(op
);
773 static int is_case_val(struct statement
*stmt
, sval_t sval
)
777 if (stmt
->type
!= STMT_CASE
)
779 if (!stmt
->case_expression
) {
783 if (!get_value(stmt
->case_expression
, &case_sval
))
785 if (case_sval
.value
== sval
.value
)
790 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
791 struct expression
*case_expr
,
792 struct expression
*case_to
)
795 struct range_list
*rl
= NULL
;
796 struct symbol
*switch_type
;
798 switch_type
= get_type(switch_expr
);
799 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
800 start
= sval_cast(switch_type
, start
);
801 end
= sval_cast(switch_type
, end
);
802 add_range(&rl
, start
, end
);
803 } else if (get_value(case_expr
, &start
)) {
804 start
= sval_cast(switch_type
, start
);
805 add_range(&rl
, start
, start
);
811 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
813 struct statement
*tmp
;
814 struct range_list
*rl
;
816 __split_expr(stmt
->switch_expression
);
817 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
819 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
820 __save_switch_states(top_expression(switch_expr_stack
));
825 stmt
= stmt
->switch_statement
;
827 __push_scope_hooks();
828 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
829 __smatch_lineno
= tmp
->pos
.line
;
830 if (is_case_val(tmp
, sval
)) {
831 rl
= alloc_rl(sval
, sval
);
832 __merge_switches(top_expression(switch_expr_stack
), rl
);
833 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
835 if (__path_is_null())
838 if (__path_is_null()) {
842 } END_FOR_EACH_PTR(tmp
);
844 __call_scope_hooks();
845 if (!__pop_default())
846 __merge_switches(top_expression(switch_expr_stack
), NULL
);
847 __discard_switches();
849 pop_expression(&switch_expr_stack
);
852 static void split_case(struct statement
*stmt
)
854 struct range_list
*rl
= NULL
;
856 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
857 expr_set_parent_stmt(stmt
->case_to
, stmt
);
859 rl
= get_case_rl(top_expression(switch_expr_stack
),
860 stmt
->case_expression
, stmt
->case_to
);
861 while (stmt
->case_statement
->type
== STMT_CASE
) {
862 struct range_list
*tmp
;
864 tmp
= get_case_rl(top_expression(switch_expr_stack
),
865 stmt
->case_statement
->case_expression
,
866 stmt
->case_statement
->case_to
);
869 rl
= rl_union(rl
, tmp
);
870 if (!stmt
->case_expression
)
872 stmt
= stmt
->case_statement
;
875 __merge_switches(top_expression(switch_expr_stack
), rl
);
877 if (!stmt
->case_expression
)
879 __split_stmt(stmt
->case_statement
);
882 int time_parsing_function(void)
884 return ms_since(&fn_start_time
) / 1000;
887 bool taking_too_long(void)
889 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
894 static int is_last_stmt(struct statement
*cur_stmt
)
897 struct statement
*stmt
;
901 fn
= get_base_type(cur_func_sym
);
906 stmt
= fn
->inline_stmt
;
907 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
909 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
910 if (stmt
&& stmt
->type
== STMT_LABEL
)
911 stmt
= stmt
->label_statement
;
912 if (stmt
== cur_stmt
)
917 static void handle_backward_goto(struct statement
*goto_stmt
)
919 const char *goto_name
, *label_name
;
920 struct statement
*func_stmt
;
921 struct symbol
*base_type
= get_base_type(cur_func_sym
);
922 struct statement
*tmp
;
927 if (last_goto_statement_handled
)
929 last_goto_statement_handled
= 1;
931 if (!goto_stmt
->goto_label
||
932 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
933 !goto_stmt
->goto_label
->ident
)
935 goto_name
= goto_stmt
->goto_label
->ident
->name
;
937 func_stmt
= base_type
->stmt
;
939 func_stmt
= base_type
->inline_stmt
;
942 if (func_stmt
->type
!= STMT_COMPOUND
)
945 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
947 if (tmp
->type
!= STMT_LABEL
)
949 if (!tmp
->label_identifier
||
950 tmp
->label_identifier
->type
!= SYM_LABEL
||
951 !tmp
->label_identifier
->ident
)
953 label_name
= tmp
->label_identifier
->ident
->name
;
954 if (strcmp(goto_name
, label_name
) != 0)
959 } END_FOR_EACH_PTR(tmp
);
962 static void fake_a_return(void)
964 struct symbol
*return_type
;
969 return_type
= get_real_base_type(cur_func_sym
);
970 return_type
= get_real_base_type(return_type
);
971 if (return_type
!= &void_ctype
) {
972 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
977 static void split_ret_value(struct expression
*expr
)
984 type
= get_real_base_type(cur_func_sym
);
985 type
= get_real_base_type(type
);
986 expr
= fake_a_variable_assign(type
, expr
);
988 __in_fake_var_assign
++;
990 __in_fake_var_assign
--;
993 static void fake_an_empty_default(struct position pos
)
995 static struct statement none
= {};
998 none
.type
= STMT_NONE
;
999 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1000 __split_stmt(&none
);
1003 static void split_compound(struct statement
*stmt
)
1005 struct statement
*prev
= NULL
;
1006 struct statement
*cur
= NULL
;
1007 struct statement
*next
;
1009 __push_scope_hooks();
1011 FOR_EACH_PTR(stmt
->stmts
, next
) {
1012 /* just set them all ahead of time */
1013 stmt_set_parent_stmt(next
, stmt
);
1023 } END_FOR_EACH_PTR(next
);
1032 * For function scope, then delay calling the scope hooks until the
1033 * end of function hooks can run. I'm not positive this is the right
1036 if (!is_last_stmt(cur
))
1037 __call_scope_hooks();
1041 * This is a hack, work around for detecting empty functions.
1043 static int need_delayed_scope_hooks(void)
1045 struct symbol
*fn
= get_base_type(cur_func_sym
);
1046 struct statement
*stmt
;
1052 stmt
= fn
->inline_stmt
;
1053 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1058 void __split_label_stmt(struct statement
*stmt
)
1060 if (stmt
->label_identifier
&&
1061 stmt
->label_identifier
->type
== SYM_LABEL
&&
1062 stmt
->label_identifier
->ident
) {
1063 loop_count
|= 0x0800000;
1064 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1068 static void find_asm_gotos(struct statement
*stmt
)
1072 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1073 __save_gotos(sym
->ident
->name
, sym
);
1074 } END_FOR_EACH_PTR(sym
);
1077 void __split_stmt(struct statement
*stmt
)
1079 static int indent_cnt
;
1085 if (!__in_fake_assign
)
1086 __silence_warnings_for_stmt
= false;
1088 if (__bail_on_rest_of_function
|| is_skipped_function())
1091 if (out_of_memory() || taking_too_long()) {
1092 struct timeval stop
;
1094 gettimeofday(&stop
, NULL
);
1096 __bail_on_rest_of_function
= 1;
1098 sm_perror("Function too hairy. Giving up. %lu seconds",
1099 stop
.tv_sec
- fn_start_time
.tv_sec
);
1101 final_pass
= 0; /* turn off sm_msg() from here */
1107 add_ptr_list(&big_statement_stack
, stmt
);
1108 free_expression_stack(&big_expression_stack
);
1109 set_position(stmt
->pos
);
1110 __pass_to_client(stmt
, STMT_HOOK
);
1112 switch (stmt
->type
) {
1113 case STMT_DECLARATION
:
1114 split_declaration(stmt
->declaration
);
1117 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1119 split_ret_value(stmt
->ret_value
);
1120 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1121 __process_post_op_stack();
1124 case STMT_EXPRESSION
:
1125 expr_set_parent_stmt(stmt
->expression
, stmt
);
1126 expr_set_parent_stmt(stmt
->context
, stmt
);
1128 __split_expr(stmt
->expression
);
1131 split_compound(stmt
);
1134 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1135 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1136 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1138 if (known_condition_true(stmt
->if_conditional
)) {
1139 __split_stmt(stmt
->if_true
);
1142 if (known_condition_false(stmt
->if_conditional
)) {
1143 __split_stmt(stmt
->if_false
);
1146 __split_whole_condition(stmt
->if_conditional
);
1147 __split_stmt(stmt
->if_true
);
1148 if (empty_statement(stmt
->if_true
) &&
1149 last_stmt_on_same_line() &&
1150 !get_macro_name(stmt
->if_true
->pos
))
1151 sm_warning("if();");
1152 __push_true_states();
1153 __use_false_states();
1154 __split_stmt(stmt
->if_false
);
1155 __merge_true_states();
1158 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1159 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1160 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1161 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1162 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1164 if (stmt
->iterator_pre_condition
)
1165 handle_pre_loop(stmt
);
1166 else if (stmt
->iterator_post_condition
)
1167 handle_post_loop(stmt
);
1169 // these are for(;;) type loops.
1170 handle_pre_loop(stmt
);
1174 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1175 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1177 if (get_value(stmt
->switch_expression
, &sval
)) {
1178 split_known_switch(stmt
, sval
);
1181 __split_expr(stmt
->switch_expression
);
1182 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1183 __save_switch_states(top_expression(switch_expr_stack
));
1187 __split_stmt(stmt
->switch_statement
);
1188 if (!__pop_default() && have_remaining_cases())
1189 fake_an_empty_default(stmt
->pos
);
1190 __discard_switches();
1192 pop_expression(&switch_expr_stack
);
1198 __split_label_stmt(stmt
);
1199 __split_stmt(stmt
->label_statement
);
1202 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1204 __split_expr(stmt
->goto_expression
);
1205 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1206 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1208 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1210 __process_continues();
1212 } else if (stmt
->goto_label
&&
1213 stmt
->goto_label
->type
== SYM_LABEL
&&
1214 stmt
->goto_label
->ident
) {
1215 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1218 if (is_last_stmt(stmt
))
1219 handle_backward_goto(stmt
);
1224 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1226 find_asm_gotos(stmt
);
1227 __pass_to_client(stmt
, ASM_HOOK
);
1228 __split_expr(stmt
->asm_string
);
1229 split_asm_ops(stmt
->asm_outputs
);
1230 split_asm_ops(stmt
->asm_inputs
);
1231 split_expr_list(stmt
->asm_clobbers
, NULL
);
1236 __split_expr(stmt
->range_expression
);
1237 __split_expr(stmt
->range_low
);
1238 __split_expr(stmt
->range_high
);
1241 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1242 if (--indent_cnt
== 0)
1243 __discard_fake_states();
1245 __process_post_op_stack();
1248 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1250 struct expression
*expr
;
1252 FOR_EACH_PTR(expr_list
, expr
) {
1253 expr_set_parent_expr(expr
, parent
);
1255 __process_post_op_stack();
1256 } END_FOR_EACH_PTR(expr
);
1259 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1261 struct symbol
*orig
;
1266 arg
= strip_parens(arg
);
1267 if (arg
!= strip_expr(arg
))
1270 orig
= get_type(arg
);
1277 * I would have expected that we could just do use (orig == type) but I
1278 * guess for pointers we need to get the basetype to do that comparison.
1282 if (orig
->type
!= SYM_PTR
||
1283 type
->type
!= SYM_PTR
) {
1284 if (type_fits(type
, orig
))
1288 orig
= get_real_base_type(orig
);
1289 type
= get_real_base_type(type
);
1296 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*expr
)
1298 struct expression
*var
, *assign
, *parent
;
1305 if (expr
->type
== EXPR_ASSIGNMENT
)
1308 cast
= cast_arg(type
, expr
);
1310 * Using expr_to_sym() here is a hack. We want to say that we don't
1311 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1312 * It turns out faking these assignments is way more expensive than I
1313 * would have imagined. I'm not sure why exactly.
1318 * if the code is "return *p;" where "p" is a user pointer then
1319 * we want to create a fake assignment so that it sets the state
1320 * in check_kernel_user_data.c.
1323 if (expr
->type
!= EXPR_PREOP
&&
1324 expr
->op
!= '*' && expr
->op
!= '&' &&
1329 snprintf(buf
, sizeof(buf
), "__sm_fake_%p", expr
);
1330 var
= fake_variable(type
, buf
);
1331 assign
= assign_expression(var
, '=', expr
);
1332 assign
->smatch_flags
|= Fake
;
1334 parent
= expr_get_parent_expr(expr
);
1335 expr_set_parent_expr(assign
, parent
);
1336 expr_set_parent_expr(expr
, assign
);
1343 static void split_args(struct expression
*expr
)
1345 struct expression
*arg
, *tmp
;
1346 struct symbol
*type
;
1350 FOR_EACH_PTR(expr
->args
, arg
) {
1352 expr_set_parent_expr(arg
, expr
);
1353 type
= get_arg_type(expr
->fn
, i
);
1354 tmp
= fake_a_variable_assign(type
, arg
);
1356 __in_fake_var_assign
++;
1359 __in_fake_var_assign
--;
1360 __process_post_op_stack();
1361 } END_FOR_EACH_PTR(arg
);
1364 static void split_sym(struct symbol
*sym
)
1368 if (!(sym
->namespace & NS_SYMBOL
))
1371 __split_stmt(sym
->stmt
);
1372 __split_expr(sym
->array_size
);
1373 split_symlist(sym
->arguments
);
1374 split_symlist(sym
->symbol_list
);
1375 __split_stmt(sym
->inline_stmt
);
1376 split_symlist(sym
->inline_symbol_list
);
1379 static void split_symlist(struct symbol_list
*sym_list
)
1383 FOR_EACH_PTR(sym_list
, sym
) {
1385 } END_FOR_EACH_PTR(sym
);
1388 typedef void (fake_cb
)(struct expression
*expr
);
1390 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1392 struct symbol
*type
, *tmp
;
1398 name
= member
->name
;
1400 type
= get_type(expr
);
1401 if (!type
|| type
->type
!= SYM_STRUCT
)
1405 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1409 if (strcmp(name
, tmp
->ident
->name
) == 0)
1411 } END_FOR_EACH_PTR(tmp
);
1415 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1417 struct symbol
*type
, *member
;
1420 type
= get_type(expr
);
1421 if (!type
|| type
->type
!= SYM_STRUCT
)
1424 FOR_EACH_PTR(type
->symbol_list
, member
) {
1426 return member
->ident
;
1428 } END_FOR_EACH_PTR(member
);
1432 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1434 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1436 struct expression
*edge_member
, *assign
;
1437 struct symbol
*base
= get_real_base_type(member
);
1441 expr
= member_expression(expr
, '.', member
->ident
);
1443 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1444 struct symbol
*type
;
1446 type
= get_real_base_type(tmp
);
1450 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1451 if (get_extra_state(edge_member
))
1454 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1455 set_inner_struct_members(expr
, tmp
);
1462 assign
= assign_expression(edge_member
, '=', zero_expr());
1463 __split_expr(assign
);
1464 } END_FOR_EACH_PTR(tmp
);
1469 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1472 struct expression
*member
= NULL
;
1473 struct expression
*assign
;
1476 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1477 expr
= strip_expr(expr
->unop
);
1481 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1482 type
= get_real_base_type(tmp
);
1487 member
= member_expression(expr
, op
, tmp
->ident
);
1488 if (get_extra_state(member
))
1492 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1493 set_inner_struct_members(expr
, tmp
);
1496 if (type
->type
== SYM_ARRAY
)
1501 assign
= assign_expression(member
, '=', zero_expr());
1502 __split_expr(assign
);
1503 } END_FOR_EACH_PTR(tmp
);
1506 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1508 struct expression
*deref
, *assign
, *tmp
, *right
;
1509 struct symbol
*struct_type
, *type
;
1510 struct ident
*member
;
1513 struct_type
= get_type(symbol
);
1515 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1519 * We're parsing an initializer that could look something like this:
1520 * struct foo foo = {
1522 * .whatever.xxx = 11,
1526 * So what we have here is a list with 42, .whatever, and .zzz. We need
1527 * to break it up into left and right sides of the assignments.
1531 FOR_EACH_PTR(members
, tmp
) {
1533 if (tmp
->type
== EXPR_IDENTIFIER
) {
1534 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1535 while (tmp
->type
== EXPR_IDENTIFIER
) {
1536 member
= tmp
->expr_ident
;
1537 tmp
= tmp
->ident_expression
;
1539 deref
= member_expression(deref
, '.', member
);
1541 deref
= member_expression(symbol
, '.', member
);
1544 member
= number_to_member(symbol
, member_idx
);
1545 deref
= member_expression(symbol
, '.', member
);
1549 if (right
->type
== EXPR_INITIALIZER
) {
1550 type
= get_type(deref
);
1551 if (type
&& type
->type
== SYM_ARRAY
)
1552 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1554 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1556 assign
= assign_expression(deref
, '=', right
);
1559 } END_FOR_EACH_PTR(tmp
);
1561 set_unset_to_zero(struct_type
, symbol
);
1564 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1566 fake_member_assigns_helper(symbol_expression(sym
),
1567 sym
->initializer
->expr_list
, fake_cb
);
1570 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1572 struct expression
*offset
, *binop
, *assign
, *tmp
;
1573 struct symbol
*type
;
1576 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1580 FOR_EACH_PTR(expr_list
, tmp
) {
1581 if (tmp
->type
== EXPR_INDEX
) {
1582 if (tmp
->idx_from
!= tmp
->idx_to
)
1584 idx
= tmp
->idx_from
;
1585 if (!tmp
->idx_expression
)
1587 tmp
= tmp
->idx_expression
;
1589 offset
= value_expr(idx
);
1590 binop
= array_element_expression(array
, offset
);
1591 if (tmp
->type
== EXPR_INITIALIZER
) {
1592 type
= get_type(binop
);
1593 if (type
&& type
->type
== SYM_ARRAY
)
1594 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1596 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1598 assign
= assign_expression(binop
, '=', tmp
);
1603 } END_FOR_EACH_PTR(tmp
);
1606 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1608 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1611 static void fake_assign_expr(struct symbol
*sym
)
1613 struct expression
*assign
, *symbol
;
1615 symbol
= symbol_expression(sym
);
1616 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1617 __split_expr(assign
);
1620 static void do_initializer_stuff(struct symbol
*sym
)
1622 if (!sym
->initializer
)
1625 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1626 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1627 fake_element_assigns(sym
, __split_expr
);
1629 fake_member_assigns(sym
, __split_expr
);
1631 fake_assign_expr(sym
);
1635 static void split_declaration(struct symbol_list
*sym_list
)
1639 FOR_EACH_PTR(sym_list
, sym
) {
1640 __pass_to_client(sym
, DECLARATION_HOOK
);
1641 do_initializer_stuff(sym
);
1643 } END_FOR_EACH_PTR(sym
);
1646 static void call_global_assign_hooks(struct expression
*assign
)
1648 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1651 static void fake_global_assign(struct symbol
*sym
)
1653 struct expression
*assign
, *symbol
;
1655 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1656 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1657 fake_element_assigns(sym
, call_global_assign_hooks
);
1658 } else if (sym
->initializer
) {
1659 symbol
= symbol_expression(sym
);
1660 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1661 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1663 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1665 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1666 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1667 fake_member_assigns(sym
, call_global_assign_hooks
);
1668 } else if (sym
->initializer
) {
1669 symbol
= symbol_expression(sym
);
1670 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1671 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1673 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1676 symbol
= symbol_expression(sym
);
1677 if (sym
->initializer
) {
1678 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1679 __split_expr(assign
);
1681 assign
= assign_expression(symbol
, '=', zero_expr());
1683 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1687 static void start_function_definition(struct symbol
*sym
)
1689 __in_function_def
= 1;
1690 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1691 __in_function_def
= 0;
1692 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1696 void add_function_data(unsigned long *fn_data
)
1698 __add_ptr_list(&fn_data_list
, fn_data
);
1701 static void clear_function_data(void)
1705 FOR_EACH_PTR(fn_data_list
, tmp
) {
1707 } END_FOR_EACH_PTR(tmp
);
1710 static void split_function(struct symbol
*sym
)
1712 struct symbol
*base_type
= get_base_type(sym
);
1713 struct timeval stop
;
1715 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1718 gettimeofday(&outer_fn_start_time
, NULL
);
1719 gettimeofday(&fn_start_time
, NULL
);
1722 cur_func
= sym
->ident
->name
;
1723 set_position(sym
->pos
);
1724 clear_function_data();
1726 last_goto_statement_handled
= 0;
1727 sm_debug("new function: %s\n", cur_func
);
1729 if (option_two_passes
) {
1733 start_function_definition(sym
);
1734 __split_stmt(base_type
->stmt
);
1735 __split_stmt(base_type
->inline_stmt
);
1741 start_function_definition(sym
);
1742 __split_stmt(base_type
->stmt
);
1743 __split_stmt(base_type
->inline_stmt
);
1744 __pass_to_client(sym
, END_FUNC_HOOK
);
1745 if (need_delayed_scope_hooks())
1746 __call_scope_hooks();
1747 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1751 gettimeofday(&stop
, NULL
);
1752 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1754 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1757 cur_func_sym
= NULL
;
1759 free_data_info_allocs();
1760 free_expression_stack(&switch_expr_stack
);
1761 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1762 __bail_on_rest_of_function
= 0;
1765 static void save_flow_state(void)
1769 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
1770 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
1771 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
1773 __add_ptr_list(&backup
, big_statement_stack
);
1774 __add_ptr_list(&backup
, big_expression_stack
);
1775 __add_ptr_list(&backup
, big_condition_stack
);
1776 __add_ptr_list(&backup
, switch_expr_stack
);
1778 __add_ptr_list(&backup
, cur_func_sym
);
1780 __add_ptr_list(&backup
, __prev_stmt
);
1781 __add_ptr_list(&backup
, __cur_stmt
);
1782 __add_ptr_list(&backup
, __next_stmt
);
1784 FOR_EACH_PTR(fn_data_list
, tmp
) {
1785 __add_ptr_list(&backup
, (void *)*tmp
);
1786 } END_FOR_EACH_PTR(tmp
);
1789 static void *pop_backup(void)
1793 ret
= last_ptr_list(backup
);
1794 delete_ptr_list_last(&backup
);
1798 static void restore_flow_state(void)
1802 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
1803 *tmp
= (unsigned long)pop_backup();
1804 } END_FOR_EACH_PTR_REVERSE(tmp
);
1806 __next_stmt
= pop_backup();
1807 __cur_stmt
= pop_backup();
1808 __prev_stmt
= pop_backup();
1810 cur_func_sym
= pop_backup();
1811 switch_expr_stack
= pop_backup();
1812 big_condition_stack
= pop_backup();
1813 big_expression_stack
= pop_backup();
1814 big_statement_stack
= pop_backup();
1815 final_pass
= PTR_INT(pop_backup()) >> 2;
1816 loop_count
= PTR_INT(pop_backup()) >> 2;
1817 loop_num
= PTR_INT(pop_backup()) >> 2;
1820 static void parse_inline(struct expression
*call
)
1822 struct symbol
*base_type
;
1823 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1824 struct timeval time_backup
= fn_start_time
;
1825 struct expression
*orig_inline
= __inline_fn
;
1828 if (out_of_memory() || taking_too_long())
1833 __pass_to_client(call
, INLINE_FN_START
);
1834 final_pass
= 0; /* don't print anything */
1836 orig_budget
= inline_budget
;
1837 inline_budget
= inline_budget
- 5;
1839 base_type
= get_base_type(call
->fn
->symbol
);
1840 cur_func_sym
= call
->fn
->symbol
;
1841 if (call
->fn
->symbol
->ident
)
1842 cur_func
= call
->fn
->symbol
->ident
->name
;
1845 set_position(call
->fn
->symbol
->pos
);
1848 big_statement_stack
= NULL
;
1849 big_expression_stack
= NULL
;
1850 big_condition_stack
= NULL
;
1851 switch_expr_stack
= NULL
;
1853 sm_debug("inline function: %s\n", cur_func
);
1855 clear_function_data();
1858 start_function_definition(call
->fn
->symbol
);
1859 __split_stmt(base_type
->stmt
);
1860 __split_stmt(base_type
->inline_stmt
);
1861 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1862 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1864 free_expression_stack(&switch_expr_stack
);
1865 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1869 restore_flow_state();
1870 fn_start_time
= time_backup
;
1871 cur_func
= cur_func_bak
;
1873 restore_all_states();
1874 set_position(call
->pos
);
1875 __inline_fn
= orig_inline
;
1876 inline_budget
= orig_budget
;
1877 __pass_to_client(call
, INLINE_FN_END
);
1880 static struct symbol_list
*inlines_called
;
1881 static void add_inline_function(struct symbol
*sym
)
1883 static struct symbol_list
*already_added
;
1886 FOR_EACH_PTR(already_added
, tmp
) {
1889 } END_FOR_EACH_PTR(tmp
);
1891 add_ptr_list(&already_added
, sym
);
1892 add_ptr_list(&inlines_called
, sym
);
1895 static void process_inlines(void)
1899 FOR_EACH_PTR(inlines_called
, tmp
) {
1900 split_function(tmp
);
1901 } END_FOR_EACH_PTR(tmp
);
1902 free_ptr_list(&inlines_called
);
1905 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1909 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1912 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1914 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1916 } END_FOR_EACH_PTR_REVERSE(sym
);
1921 static bool interesting_function(struct symbol
*sym
)
1923 static int prev_stream
= -1;
1924 static bool prev_answer
;
1925 const char *filename
;
1928 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1931 if (sym
->pos
.stream
== prev_stream
)
1934 prev_stream
= sym
->pos
.stream
;
1935 prev_answer
= false;
1937 filename
= stream_name(sym
->pos
.stream
);
1938 len
= strlen(filename
);
1939 if (len
> 0 && filename
[len
- 1] == 'c')
1944 static void split_inlines_in_scope(struct symbol
*sym
)
1946 struct symbol
*base
;
1947 struct symbol_list
*scope_list
;
1950 scope_list
= sym
->scope
->symbols
;
1951 stream
= sym
->pos
.stream
;
1953 /* find the last static symbol in the file */
1954 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1955 if (sym
->pos
.stream
!= stream
)
1957 if (sym
->type
!= SYM_NODE
)
1959 base
= get_base_type(sym
);
1962 if (base
->type
!= SYM_FN
)
1964 if (!base
->inline_stmt
)
1966 if (!interesting_function(sym
))
1968 add_inline_function(sym
);
1969 } END_FOR_EACH_PTR_REVERSE(sym
);
1974 static void split_inlines(struct symbol_list
*sym_list
)
1978 sym
= get_last_scoped_symbol(sym_list
, 0);
1980 split_inlines_in_scope(sym
);
1981 sym
= get_last_scoped_symbol(sym_list
, 1);
1983 split_inlines_in_scope(sym
);
1986 static struct stree
*clone_estates_perm(struct stree
*orig
)
1988 struct stree
*ret
= NULL
;
1989 struct sm_state
*tmp
;
1991 FOR_EACH_SM(orig
, tmp
) {
1992 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1993 } END_FOR_EACH_SM(tmp
);
1998 struct position last_pos
;
1999 static void split_c_file_functions(struct symbol_list
*sym_list
)
2004 FOR_EACH_PTR(sym_list
, sym
) {
2005 set_position(sym
->pos
);
2006 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2007 __pass_to_client(sym
, BASE_HOOK
);
2008 fake_global_assign(sym
);
2010 } END_FOR_EACH_PTR(sym
);
2011 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2014 FOR_EACH_PTR(sym_list
, sym
) {
2015 set_position(sym
->pos
);
2016 last_pos
= sym
->pos
;
2017 if (!interesting_function(sym
))
2019 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2020 split_function(sym
);
2023 last_pos
= sym
->pos
;
2024 } END_FOR_EACH_PTR(sym
);
2025 split_inlines(sym_list
);
2026 __pass_to_client(sym_list
, END_FILE_HOOK
);
2029 static int final_before_fake
;
2030 void init_fake_env(void)
2033 final_before_fake
= final_pass
;
2035 __push_fake_cur_stree();
2039 void end_fake_env(void)
2041 __pop_fake_cur_stree();
2044 final_pass
= final_before_fake
;
2047 static void open_output_files(char *base_file
)
2051 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2052 sm_outfd
= fopen(buf
, "w");
2054 sm_fatal("Cannot open %s", buf
);
2059 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2060 sql_outfd
= fopen(buf
, "w");
2062 sm_fatal("Error: Cannot open %s", buf
);
2064 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2065 caller_info_fd
= fopen(buf
, "w");
2066 if (!caller_info_fd
)
2067 sm_fatal("Error: Cannot open %s", buf
);
2070 void smatch(struct string_list
*filelist
)
2072 struct symbol_list
*sym_list
;
2073 struct timeval stop
, start
;
2077 gettimeofday(&start
, NULL
);
2079 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2080 path
= getcwd(NULL
, 0);
2081 free(full_base_file
);
2083 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2084 full_base_file
= malloc(len
);
2085 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2087 full_base_file
= alloc_string(base_file
);
2089 if (option_file_output
)
2090 open_output_files(base_file
);
2091 sym_list
= sparse_keep_tokens(base_file
);
2092 split_c_file_functions(sym_list
);
2093 } END_FOR_EACH_PTR_NOTAG(base_file
);
2095 gettimeofday(&stop
, NULL
);
2097 set_position(last_pos
);
2100 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2102 sm_msg("mem: %luKb", get_max_memory());