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
*call
, struct expression
*expr
, int nr
);
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 save_func_time(void *_rl
, int argc
, char **argv
, char **azColName
)
209 unsigned long *rl
= _rl
;
211 *rl
= strtoul(argv
[0], NULL
, 10);
215 static int get_func_time(struct symbol
*sym
)
217 unsigned long time
= 0;
219 run_sql(&save_func_time
, &time
,
220 "select key from return_implies where %s and type = %d;",
221 get_static_filter(sym
), FUNC_TIME
);
226 static int inline_budget
= 20;
228 int inlinable(struct expression
*expr
)
231 struct statement
*last_stmt
= NULL
;
233 if (__inline_fn
) /* don't nest */
236 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
238 if (is_no_inline_function(expr
->symbol
->ident
->name
))
240 sym
= get_base_type(expr
->symbol
);
241 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
242 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
244 if (sym
->stmt
->type
!= STMT_COMPOUND
)
246 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
248 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
249 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
251 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
253 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
259 /* the magic numbers in this function are pulled out of my bum. */
260 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
263 if (get_func_time(expr
->symbol
) >= 2)
269 void __process_post_op_stack(void)
271 struct expression
*expr
;
273 FOR_EACH_PTR(post_op_stack
, expr
) {
274 __pass_to_client(expr
, OP_HOOK
);
275 } END_FOR_EACH_PTR(expr
);
277 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
280 static int handle_comma_assigns(struct expression
*expr
)
282 struct expression
*right
;
283 struct expression
*assign
;
285 right
= strip_expr(expr
->right
);
286 if (right
->type
!= EXPR_COMMA
)
289 __split_expr(right
->left
);
290 __process_post_op_stack();
292 assign
= assign_expression(expr
->left
, '=', right
->right
);
293 __split_expr(assign
);
298 /* This is to handle *p++ = foo; assignments */
299 static int handle_postop_assigns(struct expression
*expr
)
301 struct expression
*left
, *fake_left
;
302 struct expression
*assign
;
304 left
= strip_expr(expr
->left
);
305 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
307 left
= strip_expr(left
->unop
);
308 if (left
->type
!= EXPR_POSTOP
)
311 fake_left
= deref_expression(strip_expr(left
->unop
));
312 assign
= assign_expression(fake_left
, '=', expr
->right
);
314 __split_expr(assign
);
315 __split_expr(expr
->left
);
320 static int prev_expression_is_getting_address(struct expression
*expr
)
322 struct expression
*parent
;
325 parent
= expr_get_parent_expr(expr
);
329 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
331 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
333 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
342 static void handle_builtin_overflow_func(struct expression
*expr
)
344 struct expression
*a
, *b
, *res
, *assign
;
347 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
349 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
351 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
356 a
= get_argument_from_call_expr(expr
->args
, 0);
357 b
= get_argument_from_call_expr(expr
->args
, 1);
358 res
= get_argument_from_call_expr(expr
->args
, 2);
360 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
361 __split_expr(assign
);
364 static int handle__builtin_choose_expr(struct expression
*expr
)
366 struct expression
*const_expr
, *expr1
, *expr2
;
369 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
372 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
373 expr1
= get_argument_from_call_expr(expr
->args
, 1);
374 expr2
= get_argument_from_call_expr(expr
->args
, 2);
376 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
385 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
387 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
390 right
= strip_expr(expr
->right
);
391 if (right
->type
!= EXPR_CALL
)
393 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
396 const_expr
= get_argument_from_call_expr(right
->args
, 0);
397 expr1
= get_argument_from_call_expr(right
->args
, 1);
398 expr2
= get_argument_from_call_expr(right
->args
, 2);
400 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
403 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
408 void __split_expr(struct expression
*expr
)
413 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
415 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
417 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
420 push_expression(&big_expression_stack
, expr
);
421 set_position(expr
->pos
);
422 __pass_to_client(expr
, EXPR_HOOK
);
424 switch (expr
->type
) {
426 expr_set_parent_expr(expr
->unop
, expr
);
428 if (expr
->op
== '*' &&
429 !prev_expression_is_getting_address(expr
))
430 __pass_to_client(expr
, DEREF_HOOK
);
431 __split_expr(expr
->unop
);
432 __pass_to_client(expr
, OP_HOOK
);
435 expr_set_parent_expr(expr
->unop
, expr
);
437 __split_expr(expr
->unop
);
438 push_expression(&post_op_stack
, expr
);
442 if (expr
->statement
&& !expr
->statement
) {
443 stmt_set_parent_stmt(expr
->statement
,
444 last_ptr_list((struct ptr_list
*)big_statement_stack
));
446 __split_stmt(expr
->statement
);
451 expr_set_parent_expr(expr
->left
, expr
);
452 expr_set_parent_expr(expr
->right
, expr
);
454 __pass_to_client(expr
, LOGIC_HOOK
);
455 __handle_logic(expr
);
458 expr_set_parent_expr(expr
->left
, expr
);
459 expr_set_parent_expr(expr
->right
, expr
);
461 __pass_to_client(expr
, BINOP_HOOK
);
463 expr_set_parent_expr(expr
->left
, expr
);
464 expr_set_parent_expr(expr
->right
, expr
);
466 __split_expr(expr
->left
);
467 __process_post_op_stack();
468 __split_expr(expr
->right
);
470 case EXPR_ASSIGNMENT
: {
471 struct expression
*right
;
473 expr_set_parent_expr(expr
->left
, expr
);
474 expr_set_parent_expr(expr
->right
, expr
);
476 right
= strip_expr(expr
->right
);
480 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
483 if (__handle_condition_assigns(expr
))
485 /* foo = (x < 5 ? foo : 5); */
486 if (__handle_select_assigns(expr
))
488 /* foo = ({frob(); frob(); frob(); 1;}) */
489 if (__handle_expr_statement_assigns(expr
))
490 break; // FIXME: got after
492 if (handle_comma_assigns(expr
))
494 if (handle__builtin_choose_expr_assigns(expr
))
496 if (handle_postop_assigns(expr
))
497 break; /* no need to goto after_assign */
499 __split_expr(expr
->right
);
500 if (outside_of_function())
501 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
503 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
505 __fake_struct_member_assignments(expr
);
507 /* Re-examine ->right for inlines. See the commit message */
508 right
= strip_expr(expr
->right
);
509 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
510 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
512 if (get_macro_name(right
->pos
) &&
513 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
514 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
517 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
518 __split_expr(expr
->left
);
522 expr_set_parent_expr(expr
->deref
, expr
);
524 __pass_to_client(expr
, DEREF_HOOK
);
525 __split_expr(expr
->deref
);
528 expr_set_parent_expr(expr
->base
, expr
);
530 __split_expr(expr
->base
);
533 case EXPR_FORCE_CAST
:
534 expr_set_parent_expr(expr
->cast_expression
, expr
);
536 __pass_to_client(expr
, CAST_HOOK
);
537 __split_expr(expr
->cast_expression
);
540 if (expr
->cast_expression
)
541 __pass_to_client(strip_parens(expr
->cast_expression
),
547 case EXPR_CONDITIONAL
:
549 expr_set_parent_expr(expr
->conditional
, expr
);
550 expr_set_parent_expr(expr
->cond_true
, expr
);
551 expr_set_parent_expr(expr
->cond_false
, expr
);
553 if (known_condition_true(expr
->conditional
)) {
554 __split_expr(expr
->cond_true
);
557 if (known_condition_false(expr
->conditional
)) {
558 __split_expr(expr
->cond_false
);
561 __pass_to_client(expr
, SELECT_HOOK
);
562 __split_whole_condition(expr
->conditional
);
563 __split_expr(expr
->cond_true
);
564 __push_true_states();
565 __use_false_states();
566 __split_expr(expr
->cond_false
);
567 __merge_true_states();
570 expr_set_parent_expr(expr
->fn
, expr
);
572 if (sym_name_is("__builtin_constant_p", expr
->fn
))
574 if (handle__builtin_choose_expr(expr
))
576 __split_expr(expr
->fn
);
578 if (is_inline_func(expr
->fn
))
579 add_inline_function(expr
->fn
->symbol
->definition
);
580 if (inlinable(expr
->fn
))
582 __process_post_op_stack();
583 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
584 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
586 if (inlinable(expr
->fn
))
588 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
589 if (is_noreturn_func(expr
->fn
))
591 if (!expr_get_parent_expr(expr
))
592 __discard_fake_states(expr
);
593 handle_builtin_overflow_func(expr
);
595 case EXPR_INITIALIZER
:
596 split_expr_list(expr
->expr_list
, expr
);
598 case EXPR_IDENTIFIER
:
599 expr_set_parent_expr(expr
->ident_expression
, expr
);
600 __split_expr(expr
->ident_expression
);
603 expr_set_parent_expr(expr
->idx_expression
, expr
);
604 __split_expr(expr
->idx_expression
);
607 expr_set_parent_expr(expr
->init_expr
, expr
);
608 __split_expr(expr
->init_expr
);
611 __pass_to_client(expr
, SYM_HOOK
);
614 __pass_to_client(expr
, STRING_HOOK
);
619 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
620 pop_expression(&big_expression_stack
);
623 static int is_forever_loop(struct statement
*stmt
)
625 struct expression
*expr
;
628 expr
= strip_expr(stmt
->iterator_pre_condition
);
630 expr
= stmt
->iterator_post_condition
;
632 /* this is a for(;;) loop... */
636 if (get_value(expr
, &sval
) && sval
.value
!= 0)
643 static char *get_loop_name(int num
)
647 snprintf(buf
, 255, "-loop%d", num
);
649 return alloc_sname(buf
);
653 * Pre Loops are while and for loops.
655 static void handle_pre_loop(struct statement
*stmt
)
657 int once_through
; /* we go through the loop at least once */
658 struct sm_state
*extra_sm
= NULL
;
661 struct stree
*stree
= NULL
;
662 struct sm_state
*sm
= NULL
;
664 loop_name
= get_loop_name(loop_num
);
667 __split_stmt(stmt
->iterator_pre_statement
);
668 __prev_stmt
= stmt
->iterator_pre_statement
;
670 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
676 __merge_gotos(loop_name
, NULL
);
678 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
679 __in_pre_condition
++;
680 __pass_to_client(stmt
, PRELOOP_HOOK
);
681 __split_whole_condition(stmt
->iterator_pre_condition
);
682 __in_pre_condition
--;
683 FOR_EACH_SM(stree
, sm
) {
684 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
685 } END_FOR_EACH_SM(sm
);
688 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
690 if (option_assume_loops
)
693 __split_stmt(stmt
->iterator_statement
);
694 if (is_forever_loop(stmt
)) {
696 __save_gotos(loop_name
, NULL
);
698 __push_fake_cur_stree();
699 __split_stmt(stmt
->iterator_post_statement
);
700 stree
= __pop_fake_cur_stree();
702 __discard_false_states();
705 if (!__path_is_null())
706 __merge_stree_into_cur(stree
);
710 unchanged
= __iterator_unchanged(extra_sm
);
711 __split_stmt(stmt
->iterator_post_statement
);
712 __prev_stmt
= stmt
->iterator_post_statement
;
715 __save_gotos(loop_name
, NULL
);
716 __in_pre_condition
++;
717 __split_whole_condition(stmt
->iterator_pre_condition
);
718 __in_pre_condition
--;
720 __merge_false_states();
722 __discard_false_states();
724 __merge_false_states();
726 if (extra_sm
&& unchanged
)
727 __extra_pre_loop_hook_after(extra_sm
,
728 stmt
->iterator_post_statement
,
729 stmt
->iterator_pre_condition
);
736 * Post loops are do {} while();
738 static void handle_post_loop(struct statement
*stmt
)
742 loop_name
= get_loop_name(loop_num
);
748 __merge_gotos(loop_name
, NULL
);
749 __split_stmt(stmt
->iterator_statement
);
751 if (!expr_is_zero(stmt
->iterator_post_condition
))
752 __save_gotos(loop_name
, NULL
);
754 if (is_forever_loop(stmt
)) {
757 __split_whole_condition(stmt
->iterator_post_condition
);
758 __use_false_states();
764 static int empty_statement(struct statement
*stmt
)
768 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
773 static int last_stmt_on_same_line(void)
775 struct statement
*stmt
;
778 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
781 if (stmt
->pos
.line
== get_lineno())
784 } END_FOR_EACH_PTR_REVERSE(stmt
);
788 static void split_asm_ops(struct asm_operand_list
*ops
)
790 struct asm_operand
*op
;
792 FOR_EACH_PTR(ops
, op
) {
793 __split_expr(op
->expr
);
794 } END_FOR_EACH_PTR(op
);
797 static int is_case_val(struct statement
*stmt
, sval_t sval
)
801 if (stmt
->type
!= STMT_CASE
)
803 if (!stmt
->case_expression
) {
807 if (!get_value(stmt
->case_expression
, &case_sval
))
809 if (case_sval
.value
== sval
.value
)
814 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
815 struct expression
*case_expr
,
816 struct expression
*case_to
)
819 struct range_list
*rl
= NULL
;
820 struct symbol
*switch_type
;
822 switch_type
= get_type(switch_expr
);
823 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
824 start
= sval_cast(switch_type
, start
);
825 end
= sval_cast(switch_type
, end
);
826 add_range(&rl
, start
, end
);
827 } else if (get_value(case_expr
, &start
)) {
828 start
= sval_cast(switch_type
, start
);
829 add_range(&rl
, start
, start
);
835 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
837 struct statement
*tmp
;
838 struct range_list
*rl
;
840 __split_expr(stmt
->switch_expression
);
841 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
843 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
844 __save_switch_states(top_expression(switch_expr_stack
));
849 stmt
= stmt
->switch_statement
;
851 __push_scope_hooks();
852 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
853 __smatch_lineno
= tmp
->pos
.line
;
854 if (is_case_val(tmp
, sval
)) {
855 rl
= alloc_rl(sval
, sval
);
856 __merge_switches(top_expression(switch_expr_stack
), rl
);
857 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
859 if (__path_is_null())
862 if (__path_is_null()) {
866 } END_FOR_EACH_PTR(tmp
);
868 __call_scope_hooks();
869 if (!__pop_default())
870 __merge_switches(top_expression(switch_expr_stack
), NULL
);
871 __discard_switches();
873 pop_expression(&switch_expr_stack
);
876 static void split_case(struct statement
*stmt
)
878 struct range_list
*rl
= NULL
;
880 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
881 expr_set_parent_stmt(stmt
->case_to
, stmt
);
883 rl
= get_case_rl(top_expression(switch_expr_stack
),
884 stmt
->case_expression
, stmt
->case_to
);
885 while (stmt
->case_statement
->type
== STMT_CASE
) {
886 struct range_list
*tmp
;
888 tmp
= get_case_rl(top_expression(switch_expr_stack
),
889 stmt
->case_statement
->case_expression
,
890 stmt
->case_statement
->case_to
);
893 rl
= rl_union(rl
, tmp
);
894 if (!stmt
->case_expression
)
896 stmt
= stmt
->case_statement
;
899 __merge_switches(top_expression(switch_expr_stack
), rl
);
901 if (!stmt
->case_expression
)
903 __split_stmt(stmt
->case_statement
);
906 int time_parsing_function(void)
908 return ms_since(&fn_start_time
) / 1000;
911 bool taking_too_long(void)
913 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
918 int is_last_stmt(struct statement
*cur_stmt
)
921 struct statement
*stmt
;
925 fn
= get_base_type(cur_func_sym
);
930 stmt
= fn
->inline_stmt
;
931 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
933 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
934 if (stmt
&& stmt
->type
== STMT_LABEL
)
935 stmt
= stmt
->label_statement
;
936 if (stmt
== cur_stmt
)
941 static void handle_backward_goto(struct statement
*goto_stmt
)
943 const char *goto_name
, *label_name
;
944 struct statement
*func_stmt
;
945 struct symbol
*base_type
= get_base_type(cur_func_sym
);
946 struct statement
*tmp
;
951 if (last_goto_statement_handled
)
953 last_goto_statement_handled
= 1;
955 if (!goto_stmt
->goto_label
||
956 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
957 !goto_stmt
->goto_label
->ident
)
959 goto_name
= goto_stmt
->goto_label
->ident
->name
;
961 func_stmt
= base_type
->stmt
;
963 func_stmt
= base_type
->inline_stmt
;
966 if (func_stmt
->type
!= STMT_COMPOUND
)
969 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
971 if (tmp
->type
!= STMT_LABEL
)
973 if (!tmp
->label_identifier
||
974 tmp
->label_identifier
->type
!= SYM_LABEL
||
975 !tmp
->label_identifier
->ident
)
977 label_name
= tmp
->label_identifier
->ident
->name
;
978 if (strcmp(goto_name
, label_name
) != 0)
983 } END_FOR_EACH_PTR(tmp
);
986 static void fake_a_return(void)
988 struct symbol
*return_type
;
993 return_type
= get_real_base_type(cur_func_sym
);
994 return_type
= get_real_base_type(return_type
);
995 if (return_type
!= &void_ctype
) {
996 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
1001 static void split_ret_value(struct expression
*expr
)
1003 struct symbol
*type
;
1008 type
= get_real_base_type(cur_func_sym
);
1009 type
= get_real_base_type(type
);
1010 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1012 __in_fake_var_assign
++;
1014 __in_fake_var_assign
--;
1017 static void fake_an_empty_default(struct position pos
)
1019 static struct statement none
= {};
1022 none
.type
= STMT_NONE
;
1023 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1024 __split_stmt(&none
);
1027 static void split_compound(struct statement
*stmt
)
1029 struct statement
*prev
= NULL
;
1030 struct statement
*cur
= NULL
;
1031 struct statement
*next
;
1033 __push_scope_hooks();
1035 FOR_EACH_PTR(stmt
->stmts
, next
) {
1036 /* just set them all ahead of time */
1037 stmt_set_parent_stmt(next
, stmt
);
1047 } END_FOR_EACH_PTR(next
);
1056 * For function scope, then delay calling the scope hooks until the
1057 * end of function hooks can run. I'm not positive this is the right
1060 if (!is_last_stmt(cur
))
1061 __call_scope_hooks();
1065 * This is a hack, work around for detecting empty functions.
1067 static int need_delayed_scope_hooks(void)
1069 struct symbol
*fn
= get_base_type(cur_func_sym
);
1070 struct statement
*stmt
;
1076 stmt
= fn
->inline_stmt
;
1077 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1082 void __split_label_stmt(struct statement
*stmt
)
1084 if (stmt
->label_identifier
&&
1085 stmt
->label_identifier
->type
== SYM_LABEL
&&
1086 stmt
->label_identifier
->ident
) {
1087 loop_count
|= 0x0800000;
1088 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1092 static void find_asm_gotos(struct statement
*stmt
)
1096 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1097 __save_gotos(sym
->ident
->name
, sym
);
1098 } END_FOR_EACH_PTR(sym
);
1101 void __split_stmt(struct statement
*stmt
)
1103 static int indent_cnt
;
1105 struct timeval start
, stop
;
1107 gettimeofday(&start
, NULL
);
1112 if (!__in_fake_assign
)
1113 __silence_warnings_for_stmt
= false;
1115 if (__bail_on_rest_of_function
|| is_skipped_function())
1118 if (out_of_memory() || taking_too_long()) {
1119 gettimeofday(&start
, NULL
);
1121 __bail_on_rest_of_function
= 1;
1123 sm_perror("Function too hairy. Giving up. %lu seconds",
1124 start
.tv_sec
- fn_start_time
.tv_sec
);
1126 final_pass
= 0; /* turn off sm_msg() from here */
1132 add_ptr_list(&big_statement_stack
, stmt
);
1133 free_expression_stack(&big_expression_stack
);
1134 set_position(stmt
->pos
);
1135 __pass_to_client(stmt
, STMT_HOOK
);
1137 switch (stmt
->type
) {
1138 case STMT_DECLARATION
:
1139 split_declaration(stmt
->declaration
);
1142 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1144 split_ret_value(stmt
->ret_value
);
1145 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1146 __process_post_op_stack();
1149 case STMT_EXPRESSION
:
1150 expr_set_parent_stmt(stmt
->expression
, stmt
);
1151 expr_set_parent_stmt(stmt
->context
, stmt
);
1153 __split_expr(stmt
->expression
);
1156 split_compound(stmt
);
1159 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1160 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1161 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1163 if (known_condition_true(stmt
->if_conditional
)) {
1164 __split_stmt(stmt
->if_true
);
1167 if (known_condition_false(stmt
->if_conditional
)) {
1168 __split_stmt(stmt
->if_false
);
1171 __split_whole_condition(stmt
->if_conditional
);
1172 __split_stmt(stmt
->if_true
);
1173 if (empty_statement(stmt
->if_true
) &&
1174 last_stmt_on_same_line() &&
1175 !get_macro_name(stmt
->if_true
->pos
))
1176 sm_warning("if();");
1177 __push_true_states();
1178 __use_false_states();
1179 __split_stmt(stmt
->if_false
);
1180 __merge_true_states();
1183 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1184 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1185 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1186 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1187 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1189 if (stmt
->iterator_pre_condition
)
1190 handle_pre_loop(stmt
);
1191 else if (stmt
->iterator_post_condition
)
1192 handle_post_loop(stmt
);
1194 // these are for(;;) type loops.
1195 handle_pre_loop(stmt
);
1199 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1200 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1202 if (get_value(stmt
->switch_expression
, &sval
)) {
1203 split_known_switch(stmt
, sval
);
1206 __split_expr(stmt
->switch_expression
);
1207 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1208 __save_switch_states(top_expression(switch_expr_stack
));
1212 __split_stmt(stmt
->switch_statement
);
1213 if (!__pop_default() && have_remaining_cases())
1214 fake_an_empty_default(stmt
->pos
);
1215 __discard_switches();
1217 pop_expression(&switch_expr_stack
);
1223 __split_label_stmt(stmt
);
1224 __split_stmt(stmt
->label_statement
);
1227 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1229 __split_expr(stmt
->goto_expression
);
1230 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1231 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1233 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1235 __process_continues();
1237 } else if (stmt
->goto_label
&&
1238 stmt
->goto_label
->type
== SYM_LABEL
&&
1239 stmt
->goto_label
->ident
) {
1240 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1243 if (is_last_stmt(stmt
))
1244 handle_backward_goto(stmt
);
1249 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1251 find_asm_gotos(stmt
);
1252 __pass_to_client(stmt
, ASM_HOOK
);
1253 __split_expr(stmt
->asm_string
);
1254 split_asm_ops(stmt
->asm_outputs
);
1255 split_asm_ops(stmt
->asm_inputs
);
1256 split_expr_list(stmt
->asm_clobbers
, NULL
);
1261 __split_expr(stmt
->range_expression
);
1262 __split_expr(stmt
->range_low
);
1263 __split_expr(stmt
->range_high
);
1266 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1267 if (--indent_cnt
== 1)
1268 __discard_fake_states(NULL
);
1271 __process_post_op_stack();
1273 gettimeofday(&stop
, NULL
);
1274 if (option_time_stmt
&& stmt
)
1275 sm_msg("stmt_time%s: %ld",
1276 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1277 stop
.tv_sec
- start
.tv_sec
);
1280 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1282 struct expression
*expr
;
1284 FOR_EACH_PTR(expr_list
, expr
) {
1285 expr_set_parent_expr(expr
, parent
);
1287 __process_post_op_stack();
1288 } END_FOR_EACH_PTR(expr
);
1291 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1293 struct symbol
*orig
;
1298 arg
= strip_parens(arg
);
1299 if (arg
!= strip_expr(arg
))
1302 orig
= get_type(arg
);
1308 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1312 * I would have expected that we could just do use (orig == type) but I
1313 * guess for pointers we need to get the basetype to do that comparison.
1317 if (orig
->type
!= SYM_PTR
||
1318 type
->type
!= SYM_PTR
) {
1319 if (type_fits(type
, orig
))
1323 orig
= get_real_base_type(orig
);
1324 type
= get_real_base_type(type
);
1331 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1333 struct expression
*var
, *assign
, *parent
;
1340 if (expr
->type
== EXPR_ASSIGNMENT
)
1343 cast
= cast_arg(type
, expr
);
1345 * Using expr_to_sym() here is a hack. We want to say that we don't
1346 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1347 * It turns out faking these assignments is way more expensive than I
1348 * would have imagined. I'm not sure why exactly.
1353 * if the code is "return *p;" where "p" is a user pointer then
1354 * we want to create a fake assignment so that it sets the state
1355 * in check_kernel_user_data.c.
1358 if (expr
->type
!= EXPR_PREOP
&&
1359 expr
->op
!= '*' && expr
->op
!= '&' &&
1365 snprintf(buf
, sizeof(buf
), "__sm_fake_%p", expr
);
1367 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1368 var
= fake_variable(type
, buf
);
1369 assign
= assign_expression(var
, '=', expr
);
1370 assign
->smatch_flags
|= Fake
;
1372 parent
= expr_get_parent_expr(expr
);
1373 expr_set_parent_expr(assign
, parent
);
1374 expr_set_parent_expr(expr
, assign
);
1381 static void split_args(struct expression
*expr
)
1383 struct expression
*arg
, *tmp
;
1384 struct symbol
*type
;
1388 FOR_EACH_PTR(expr
->args
, arg
) {
1390 expr_set_parent_expr(arg
, expr
);
1391 type
= get_arg_type(expr
->fn
, i
);
1392 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1394 __in_fake_var_assign
++;
1397 __in_fake_var_assign
--;
1398 __process_post_op_stack();
1399 } END_FOR_EACH_PTR(arg
);
1402 static void split_sym(struct symbol
*sym
)
1406 if (!(sym
->namespace & NS_SYMBOL
))
1409 __split_stmt(sym
->stmt
);
1410 __split_expr(sym
->array_size
);
1411 split_symlist(sym
->arguments
);
1412 split_symlist(sym
->symbol_list
);
1413 __split_stmt(sym
->inline_stmt
);
1414 split_symlist(sym
->inline_symbol_list
);
1417 static void split_symlist(struct symbol_list
*sym_list
)
1421 FOR_EACH_PTR(sym_list
, sym
) {
1423 } END_FOR_EACH_PTR(sym
);
1426 typedef void (fake_cb
)(struct expression
*expr
);
1428 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1430 struct symbol
*type
, *tmp
;
1436 name
= member
->name
;
1438 type
= get_type(expr
);
1439 if (!type
|| type
->type
!= SYM_STRUCT
)
1443 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1447 if (strcmp(name
, tmp
->ident
->name
) == 0)
1449 } END_FOR_EACH_PTR(tmp
);
1453 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1455 struct symbol
*type
, *member
;
1458 type
= get_type(expr
);
1459 if (!type
|| type
->type
!= SYM_STRUCT
)
1462 FOR_EACH_PTR(type
->symbol_list
, member
) {
1464 return member
->ident
;
1466 } END_FOR_EACH_PTR(member
);
1470 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1472 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1474 struct expression
*edge_member
, *assign
;
1475 struct symbol
*base
= get_real_base_type(member
);
1479 expr
= member_expression(expr
, '.', member
->ident
);
1481 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1482 struct symbol
*type
;
1484 type
= get_real_base_type(tmp
);
1488 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1489 if (get_extra_state(edge_member
))
1492 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1493 set_inner_struct_members(expr
, tmp
);
1500 assign
= assign_expression(edge_member
, '=', zero_expr());
1501 __split_expr(assign
);
1502 } END_FOR_EACH_PTR(tmp
);
1507 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1510 struct expression
*member
= NULL
;
1511 struct expression
*assign
;
1514 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1515 expr
= strip_expr(expr
->unop
);
1519 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1520 type
= get_real_base_type(tmp
);
1525 member
= member_expression(expr
, op
, tmp
->ident
);
1526 if (get_extra_state(member
))
1530 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1531 set_inner_struct_members(expr
, tmp
);
1534 if (type
->type
== SYM_ARRAY
)
1539 assign
= assign_expression(member
, '=', zero_expr());
1540 __split_expr(assign
);
1541 } END_FOR_EACH_PTR(tmp
);
1544 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1546 struct expression
*deref
, *assign
, *tmp
, *right
;
1547 struct symbol
*struct_type
, *type
;
1548 struct ident
*member
;
1551 struct_type
= get_type(symbol
);
1553 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1557 * We're parsing an initializer that could look something like this:
1558 * struct foo foo = {
1560 * .whatever.xxx = 11,
1564 * So what we have here is a list with 42, .whatever, and .zzz. We need
1565 * to break it up into left and right sides of the assignments.
1569 FOR_EACH_PTR(members
, tmp
) {
1571 if (tmp
->type
== EXPR_IDENTIFIER
) {
1572 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1573 while (tmp
->type
== EXPR_IDENTIFIER
) {
1574 member
= tmp
->expr_ident
;
1575 tmp
= tmp
->ident_expression
;
1577 deref
= member_expression(deref
, '.', member
);
1579 deref
= member_expression(symbol
, '.', member
);
1582 member
= number_to_member(symbol
, member_idx
);
1583 deref
= member_expression(symbol
, '.', member
);
1587 if (right
->type
== EXPR_INITIALIZER
) {
1588 type
= get_type(deref
);
1589 if (type
&& type
->type
== SYM_ARRAY
)
1590 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1592 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1594 assign
= assign_expression(deref
, '=', right
);
1597 } END_FOR_EACH_PTR(tmp
);
1599 set_unset_to_zero(struct_type
, symbol
);
1602 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1604 fake_member_assigns_helper(symbol_expression(sym
),
1605 sym
->initializer
->expr_list
, fake_cb
);
1608 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1610 struct expression
*offset
, *binop
, *assign
, *tmp
;
1611 struct symbol
*type
;
1614 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1618 FOR_EACH_PTR(expr_list
, tmp
) {
1619 if (tmp
->type
== EXPR_INDEX
) {
1620 if (tmp
->idx_from
!= tmp
->idx_to
)
1622 idx
= tmp
->idx_from
;
1623 if (!tmp
->idx_expression
)
1625 tmp
= tmp
->idx_expression
;
1627 offset
= value_expr(idx
);
1628 binop
= array_element_expression(array
, offset
);
1629 if (tmp
->type
== EXPR_INITIALIZER
) {
1630 type
= get_type(binop
);
1631 if (type
&& type
->type
== SYM_ARRAY
)
1632 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1634 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1636 assign
= assign_expression(binop
, '=', tmp
);
1641 } END_FOR_EACH_PTR(tmp
);
1644 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1646 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1649 static void fake_assign_expr(struct symbol
*sym
)
1651 struct expression
*assign
, *symbol
;
1653 symbol
= symbol_expression(sym
);
1654 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1655 __split_expr(assign
);
1658 static void do_initializer_stuff(struct symbol
*sym
)
1660 if (!sym
->initializer
)
1663 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1664 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1665 fake_element_assigns(sym
, __split_expr
);
1667 fake_member_assigns(sym
, __split_expr
);
1669 fake_assign_expr(sym
);
1673 static void split_declaration(struct symbol_list
*sym_list
)
1677 FOR_EACH_PTR(sym_list
, sym
) {
1678 __pass_to_client(sym
, DECLARATION_HOOK
);
1679 do_initializer_stuff(sym
);
1681 } END_FOR_EACH_PTR(sym
);
1684 static void call_global_assign_hooks(struct expression
*assign
)
1686 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1689 static void fake_global_assign(struct symbol
*sym
)
1691 struct expression
*assign
, *symbol
;
1693 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1694 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1695 fake_element_assigns(sym
, call_global_assign_hooks
);
1696 } else if (sym
->initializer
) {
1697 symbol
= symbol_expression(sym
);
1698 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1699 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1701 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1703 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1704 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1705 fake_member_assigns(sym
, call_global_assign_hooks
);
1706 } else if (sym
->initializer
) {
1707 symbol
= symbol_expression(sym
);
1708 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1709 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1711 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1714 symbol
= symbol_expression(sym
);
1715 if (sym
->initializer
) {
1716 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1717 __split_expr(assign
);
1719 assign
= assign_expression(symbol
, '=', zero_expr());
1721 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1725 static void start_function_definition(struct symbol
*sym
)
1727 __in_function_def
= 1;
1728 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1729 __in_function_def
= 0;
1730 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1734 void add_function_data(unsigned long *fn_data
)
1736 __add_ptr_list(&fn_data_list
, fn_data
);
1739 static void clear_function_data(void)
1743 FOR_EACH_PTR(fn_data_list
, tmp
) {
1745 } END_FOR_EACH_PTR(tmp
);
1748 static void record_func_time(void)
1750 struct timeval stop
;
1754 gettimeofday(&stop
, NULL
);
1755 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
1756 snprintf(buf
, sizeof(buf
), "%d", func_time
);
1757 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
1758 if (option_time
&& func_time
> 2) {
1760 sm_msg("func_time: %d", func_time
);
1765 static void split_function(struct symbol
*sym
)
1767 struct symbol
*base_type
= get_base_type(sym
);
1769 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1772 gettimeofday(&outer_fn_start_time
, NULL
);
1773 gettimeofday(&fn_start_time
, NULL
);
1776 cur_func
= sym
->ident
->name
;
1777 set_position(sym
->pos
);
1778 clear_function_data();
1780 last_goto_statement_handled
= 0;
1781 sm_debug("new function: %s\n", cur_func
);
1783 if (option_two_passes
) {
1787 start_function_definition(sym
);
1788 __split_stmt(base_type
->stmt
);
1789 __split_stmt(base_type
->inline_stmt
);
1795 start_function_definition(sym
);
1796 __split_stmt(base_type
->stmt
);
1797 __split_stmt(base_type
->inline_stmt
);
1798 __pass_to_client(sym
, END_FUNC_HOOK
);
1799 if (need_delayed_scope_hooks())
1800 __call_scope_hooks();
1801 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1808 cur_func_sym
= NULL
;
1810 free_data_info_allocs();
1811 free_expression_stack(&switch_expr_stack
);
1812 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1813 __bail_on_rest_of_function
= 0;
1816 static void save_flow_state(void)
1820 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
1821 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
1822 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
1824 __add_ptr_list(&backup
, big_statement_stack
);
1825 __add_ptr_list(&backup
, big_expression_stack
);
1826 __add_ptr_list(&backup
, big_condition_stack
);
1827 __add_ptr_list(&backup
, switch_expr_stack
);
1829 __add_ptr_list(&backup
, cur_func_sym
);
1831 __add_ptr_list(&backup
, __prev_stmt
);
1832 __add_ptr_list(&backup
, __cur_stmt
);
1833 __add_ptr_list(&backup
, __next_stmt
);
1835 FOR_EACH_PTR(fn_data_list
, tmp
) {
1836 __add_ptr_list(&backup
, (void *)*tmp
);
1837 } END_FOR_EACH_PTR(tmp
);
1840 static void *pop_backup(void)
1844 ret
= last_ptr_list(backup
);
1845 delete_ptr_list_last(&backup
);
1849 static void restore_flow_state(void)
1853 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
1854 *tmp
= (unsigned long)pop_backup();
1855 } END_FOR_EACH_PTR_REVERSE(tmp
);
1857 __next_stmt
= pop_backup();
1858 __cur_stmt
= pop_backup();
1859 __prev_stmt
= pop_backup();
1861 cur_func_sym
= pop_backup();
1862 switch_expr_stack
= pop_backup();
1863 big_condition_stack
= pop_backup();
1864 big_expression_stack
= pop_backup();
1865 big_statement_stack
= pop_backup();
1866 final_pass
= PTR_INT(pop_backup()) >> 2;
1867 loop_count
= PTR_INT(pop_backup()) >> 2;
1868 loop_num
= PTR_INT(pop_backup()) >> 2;
1871 static void parse_inline(struct expression
*call
)
1873 struct symbol
*base_type
;
1874 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1875 struct timeval time_backup
= fn_start_time
;
1876 struct expression
*orig_inline
= __inline_fn
;
1879 if (out_of_memory() || taking_too_long())
1884 __pass_to_client(call
, INLINE_FN_START
);
1885 final_pass
= 0; /* don't print anything */
1887 orig_budget
= inline_budget
;
1888 inline_budget
= inline_budget
- 5;
1890 base_type
= get_base_type(call
->fn
->symbol
);
1891 cur_func_sym
= call
->fn
->symbol
;
1892 if (call
->fn
->symbol
->ident
)
1893 cur_func
= call
->fn
->symbol
->ident
->name
;
1896 set_position(call
->fn
->symbol
->pos
);
1899 big_statement_stack
= NULL
;
1900 big_expression_stack
= NULL
;
1901 big_condition_stack
= NULL
;
1902 switch_expr_stack
= NULL
;
1904 sm_debug("inline function: %s\n", cur_func
);
1906 clear_function_data();
1909 start_function_definition(call
->fn
->symbol
);
1910 __split_stmt(base_type
->stmt
);
1911 __split_stmt(base_type
->inline_stmt
);
1912 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1913 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1914 call
->fn
->symbol
->parsed
= true;
1916 free_expression_stack(&switch_expr_stack
);
1917 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1921 restore_flow_state();
1922 fn_start_time
= time_backup
;
1923 cur_func
= cur_func_bak
;
1925 restore_all_states();
1926 set_position(call
->pos
);
1927 __inline_fn
= orig_inline
;
1928 inline_budget
= orig_budget
;
1929 __pass_to_client(call
, INLINE_FN_END
);
1932 static struct symbol_list
*inlines_called
;
1933 static void add_inline_function(struct symbol
*sym
)
1935 static struct symbol_list
*already_added
;
1938 FOR_EACH_PTR(already_added
, tmp
) {
1941 } END_FOR_EACH_PTR(tmp
);
1943 add_ptr_list(&already_added
, sym
);
1944 add_ptr_list(&inlines_called
, sym
);
1947 static void process_inlines(void)
1951 FOR_EACH_PTR(inlines_called
, tmp
) {
1952 split_function(tmp
);
1953 } END_FOR_EACH_PTR(tmp
);
1954 free_ptr_list(&inlines_called
);
1957 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1961 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1964 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1966 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1968 } END_FOR_EACH_PTR_REVERSE(sym
);
1973 static bool interesting_function(struct symbol
*sym
)
1975 static int prev_stream
= -1;
1976 static bool prev_answer
;
1977 const char *filename
;
1980 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1983 if (sym
->pos
.stream
== prev_stream
)
1986 prev_stream
= sym
->pos
.stream
;
1987 prev_answer
= false;
1989 filename
= stream_name(sym
->pos
.stream
);
1990 len
= strlen(filename
);
1991 if (len
> 0 && filename
[len
- 1] == 'c')
1996 static void split_inlines_in_scope(struct symbol
*sym
)
1998 struct symbol
*base
;
1999 struct symbol_list
*scope_list
;
2002 scope_list
= sym
->scope
->symbols
;
2003 stream
= sym
->pos
.stream
;
2005 /* find the last static symbol in the file */
2006 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2007 if (sym
->pos
.stream
!= stream
)
2009 if (sym
->type
!= SYM_NODE
)
2011 base
= get_base_type(sym
);
2014 if (base
->type
!= SYM_FN
)
2016 if (!base
->inline_stmt
)
2018 if (!interesting_function(sym
))
2020 add_inline_function(sym
);
2021 } END_FOR_EACH_PTR_REVERSE(sym
);
2026 static void split_inlines(struct symbol_list
*sym_list
)
2030 sym
= get_last_scoped_symbol(sym_list
, 0);
2032 split_inlines_in_scope(sym
);
2033 sym
= get_last_scoped_symbol(sym_list
, 1);
2035 split_inlines_in_scope(sym
);
2038 static struct stree
*clone_estates_perm(struct stree
*orig
)
2040 struct stree
*ret
= NULL
;
2041 struct sm_state
*tmp
;
2043 FOR_EACH_SM(orig
, tmp
) {
2044 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2045 } END_FOR_EACH_SM(tmp
);
2050 struct position last_pos
;
2051 static void split_c_file_functions(struct symbol_list
*sym_list
)
2056 FOR_EACH_PTR(sym_list
, sym
) {
2057 set_position(sym
->pos
);
2058 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2059 __pass_to_client(sym
, BASE_HOOK
);
2060 fake_global_assign(sym
);
2062 } END_FOR_EACH_PTR(sym
);
2063 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2066 FOR_EACH_PTR(sym_list
, sym
) {
2067 set_position(sym
->pos
);
2068 last_pos
= sym
->pos
;
2069 if (!interesting_function(sym
))
2071 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2072 split_function(sym
);
2075 last_pos
= sym
->pos
;
2076 } END_FOR_EACH_PTR(sym
);
2077 split_inlines(sym_list
);
2078 __pass_to_client(sym_list
, END_FILE_HOOK
);
2081 static int final_before_fake
;
2082 void init_fake_env(void)
2085 final_before_fake
= final_pass
;
2087 __push_fake_cur_stree();
2091 void end_fake_env(void)
2093 __pop_fake_cur_stree();
2096 final_pass
= final_before_fake
;
2099 static void open_output_files(char *base_file
)
2103 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2104 sm_outfd
= fopen(buf
, "w");
2106 sm_fatal("Cannot open %s", buf
);
2111 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2112 sql_outfd
= fopen(buf
, "w");
2114 sm_fatal("Error: Cannot open %s", buf
);
2116 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2117 caller_info_fd
= fopen(buf
, "w");
2118 if (!caller_info_fd
)
2119 sm_fatal("Error: Cannot open %s", buf
);
2122 void smatch(struct string_list
*filelist
)
2124 struct symbol_list
*sym_list
;
2125 struct timeval stop
, start
;
2129 gettimeofday(&start
, NULL
);
2131 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2132 path
= getcwd(NULL
, 0);
2133 free(full_base_file
);
2135 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2136 full_base_file
= malloc(len
);
2137 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2139 full_base_file
= alloc_string(base_file
);
2141 if (option_file_output
)
2142 open_output_files(base_file
);
2143 sym_list
= sparse_keep_tokens(base_file
);
2144 split_c_file_functions(sym_list
);
2145 } END_FOR_EACH_PTR_NOTAG(base_file
);
2147 gettimeofday(&stop
, NULL
);
2149 set_position(last_pos
);
2152 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2154 sm_msg("mem: %luKb", get_max_memory());