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
*backup
;
55 struct expression_list
*big_expression_stack
;
56 struct statement_list
*big_statement_stack
;
57 struct statement
*__prev_stmt
;
58 struct statement
*__cur_stmt
;
59 struct statement
*__next_stmt
;
60 int __in_pre_condition
= 0;
61 int __bail_on_rest_of_function
= 0;
62 static struct timeval fn_start_time
;
63 static struct timeval outer_fn_start_time
;
64 char *get_function(void) { return cur_func
; }
65 int get_lineno(void) { return __smatch_lineno
; }
66 int inside_loop(void) { return !!loop_count
; }
67 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
68 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
69 int in_expression_statement(void) { return !!__expr_stmt_count
; }
71 static void split_symlist(struct symbol_list
*sym_list
);
72 static void split_declaration(struct symbol_list
*sym_list
);
73 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
74 static void split_args(struct expression
*expr
);
75 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*expr
);
76 static void add_inline_function(struct symbol
*sym
);
77 static void parse_inline(struct expression
*expr
);
79 int option_assume_loops
= 0;
80 int option_two_passes
= 0;
81 struct symbol
*cur_func_sym
= NULL
;
82 struct stree
*global_states
;
84 const unsigned long valid_ptr_min
= 4096;
85 unsigned long valid_ptr_max
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
);
86 const sval_t valid_ptr_min_sval
= {
90 sval_t valid_ptr_max_sval
= {
92 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
94 struct range_list
*valid_ptr_rl
;
96 void alloc_valid_ptr_rl(void)
98 valid_ptr_max
= sval_type_max(&ulong_ctype
).value
& ~(MTAG_OFFSET_MASK
);
99 valid_ptr_max_sval
.value
= valid_ptr_max
;
101 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
102 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
103 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
106 int outside_of_function(void)
108 return cur_func_sym
== NULL
;
111 const char *get_filename(void)
113 if (option_info
&& option_full_path
)
114 return full_base_file
;
117 if (option_full_path
)
118 return full_filename
;
122 const char *get_base_file(void)
124 if (option_full_path
)
125 return full_base_file
;
129 static void set_position(struct position pos
)
132 static int prev_stream
= -1;
137 if (pos
.stream
== 0 && pos
.line
== 0)
140 __smatch_lineno
= pos
.line
;
142 if (pos
.stream
== prev_stream
)
145 filename
= stream_name(pos
.stream
);
148 pathname
= getcwd(NULL
, 0);
150 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
151 full_filename
= malloc(len
);
152 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
154 full_filename
= alloc_string(filename
);
159 int is_assigned_call(struct expression
*expr
)
161 struct expression
*parent
= expr_get_parent_expr(expr
);
164 parent
->type
== EXPR_ASSIGNMENT
&&
166 strip_expr(parent
->right
) == expr
)
172 int is_fake_assigned_call(struct expression
*expr
)
174 struct expression
*parent
= expr_get_fake_parent_expr(expr
);
177 parent
->type
== EXPR_ASSIGNMENT
&&
179 strip_expr(parent
->right
) == expr
)
185 static bool is_inline_func(struct expression
*expr
)
187 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
189 if (!expr
->symbol
->definition
)
191 if (expr
->symbol
->definition
->ctype
.modifiers
& MOD_INLINE
)
197 static int is_noreturn_func(struct expression
*expr
)
199 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
201 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
206 static int inline_budget
= 20;
208 int inlinable(struct expression
*expr
)
211 struct statement
*last_stmt
= NULL
;
213 if (__inline_fn
) /* don't nest */
216 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
218 if (is_no_inline_function(expr
->symbol
->ident
->name
))
220 sym
= get_base_type(expr
->symbol
);
221 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
222 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
224 if (sym
->stmt
->type
!= STMT_COMPOUND
)
226 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
228 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
229 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
231 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
233 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
239 /* the magic numbers in this function are pulled out of my bum. */
240 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
246 void __process_post_op_stack(void)
248 struct expression
*expr
;
250 FOR_EACH_PTR(post_op_stack
, expr
) {
251 __pass_to_client(expr
, OP_HOOK
);
252 } END_FOR_EACH_PTR(expr
);
254 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
257 static int handle_comma_assigns(struct expression
*expr
)
259 struct expression
*right
;
260 struct expression
*assign
;
262 right
= strip_expr(expr
->right
);
263 if (right
->type
!= EXPR_COMMA
)
266 __split_expr(right
->left
);
267 __process_post_op_stack();
269 assign
= assign_expression(expr
->left
, '=', right
->right
);
270 __split_expr(assign
);
275 /* This is to handle *p++ = foo; assignments */
276 static int handle_postop_assigns(struct expression
*expr
)
278 struct expression
*left
, *fake_left
;
279 struct expression
*assign
;
281 left
= strip_expr(expr
->left
);
282 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
284 left
= strip_expr(left
->unop
);
285 if (left
->type
!= EXPR_POSTOP
)
288 fake_left
= deref_expression(strip_expr(left
->unop
));
289 assign
= assign_expression(fake_left
, '=', expr
->right
);
291 __split_expr(assign
);
292 __split_expr(expr
->left
);
297 static int prev_expression_is_getting_address(struct expression
*expr
)
299 struct expression
*parent
;
302 parent
= expr_get_parent_expr(expr
);
306 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
308 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
310 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
319 static void handle_builtin_overflow_func(struct expression
*expr
)
321 struct expression
*a
, *b
, *res
, *assign
;
324 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
326 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
328 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
333 a
= get_argument_from_call_expr(expr
->args
, 0);
334 b
= get_argument_from_call_expr(expr
->args
, 1);
335 res
= get_argument_from_call_expr(expr
->args
, 2);
337 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
338 __split_expr(assign
);
341 static int handle__builtin_choose_expr(struct expression
*expr
)
343 struct expression
*const_expr
, *expr1
, *expr2
;
346 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
349 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
350 expr1
= get_argument_from_call_expr(expr
->args
, 1);
351 expr2
= get_argument_from_call_expr(expr
->args
, 2);
353 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
362 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
364 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
367 right
= strip_expr(expr
->right
);
368 if (right
->type
!= EXPR_CALL
)
370 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
373 const_expr
= get_argument_from_call_expr(right
->args
, 0);
374 expr1
= get_argument_from_call_expr(right
->args
, 1);
375 expr2
= get_argument_from_call_expr(right
->args
, 2);
377 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
380 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
385 void __split_expr(struct expression
*expr
)
390 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
392 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
394 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
397 push_expression(&big_expression_stack
, expr
);
398 set_position(expr
->pos
);
399 __pass_to_client(expr
, EXPR_HOOK
);
401 switch (expr
->type
) {
403 expr_set_parent_expr(expr
->unop
, expr
);
405 if (expr
->op
== '*' &&
406 !prev_expression_is_getting_address(expr
))
407 __pass_to_client(expr
, DEREF_HOOK
);
408 __split_expr(expr
->unop
);
409 __pass_to_client(expr
, OP_HOOK
);
412 expr_set_parent_expr(expr
->unop
, expr
);
414 __split_expr(expr
->unop
);
415 push_expression(&post_op_stack
, expr
);
419 if (expr
->statement
&& !expr
->statement
) {
420 stmt_set_parent_stmt(expr
->statement
,
421 last_ptr_list((struct ptr_list
*)big_statement_stack
));
423 __split_stmt(expr
->statement
);
428 expr_set_parent_expr(expr
->left
, expr
);
429 expr_set_parent_expr(expr
->right
, expr
);
431 __pass_to_client(expr
, LOGIC_HOOK
);
432 __handle_logic(expr
);
435 expr_set_parent_expr(expr
->left
, expr
);
436 expr_set_parent_expr(expr
->right
, expr
);
438 __pass_to_client(expr
, BINOP_HOOK
);
440 expr_set_parent_expr(expr
->left
, expr
);
441 expr_set_parent_expr(expr
->right
, expr
);
443 __split_expr(expr
->left
);
444 __process_post_op_stack();
445 __split_expr(expr
->right
);
447 case EXPR_ASSIGNMENT
: {
448 struct expression
*right
;
450 expr_set_parent_expr(expr
->left
, expr
);
451 expr_set_parent_expr(expr
->right
, expr
);
453 right
= strip_expr(expr
->right
);
457 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
460 if (__handle_condition_assigns(expr
))
462 /* foo = (x < 5 ? foo : 5); */
463 if (__handle_select_assigns(expr
))
465 /* foo = ({frob(); frob(); frob(); 1;}) */
466 if (__handle_expr_statement_assigns(expr
))
467 break; // FIXME: got after
469 if (handle_comma_assigns(expr
))
471 if (handle__builtin_choose_expr_assigns(expr
))
473 if (handle_postop_assigns(expr
))
474 break; /* no need to goto after_assign */
476 __split_expr(expr
->right
);
477 if (outside_of_function())
478 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
480 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
482 __fake_struct_member_assignments(expr
);
484 /* Re-examine ->right for inlines. See the commit message */
485 right
= strip_expr(expr
->right
);
486 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
487 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
489 if (get_macro_name(right
->pos
) &&
490 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
491 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
494 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
495 __split_expr(expr
->left
);
499 expr_set_parent_expr(expr
->deref
, expr
);
501 __pass_to_client(expr
, DEREF_HOOK
);
502 __split_expr(expr
->deref
);
505 expr_set_parent_expr(expr
->base
, expr
);
507 __split_expr(expr
->base
);
510 case EXPR_FORCE_CAST
:
511 expr_set_parent_expr(expr
->cast_expression
, expr
);
513 __pass_to_client(expr
, CAST_HOOK
);
514 __split_expr(expr
->cast_expression
);
517 if (expr
->cast_expression
)
518 __pass_to_client(strip_parens(expr
->cast_expression
),
524 case EXPR_CONDITIONAL
:
526 expr_set_parent_expr(expr
->conditional
, expr
);
527 expr_set_parent_expr(expr
->cond_true
, expr
);
528 expr_set_parent_expr(expr
->cond_false
, expr
);
530 if (known_condition_true(expr
->conditional
)) {
531 __split_expr(expr
->cond_true
);
534 if (known_condition_false(expr
->conditional
)) {
535 __split_expr(expr
->cond_false
);
538 __pass_to_client(expr
, SELECT_HOOK
);
539 __split_whole_condition(expr
->conditional
);
540 __split_expr(expr
->cond_true
);
541 __push_true_states();
542 __use_false_states();
543 __split_expr(expr
->cond_false
);
544 __merge_true_states();
547 expr_set_parent_expr(expr
->fn
, expr
);
549 if (sym_name_is("__builtin_constant_p", expr
->fn
))
551 if (handle__builtin_choose_expr(expr
))
553 __split_expr(expr
->fn
);
555 if (is_inline_func(expr
->fn
))
556 add_inline_function(expr
->fn
->symbol
->definition
);
557 if (inlinable(expr
->fn
))
559 __process_post_op_stack();
560 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
561 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
563 if (inlinable(expr
->fn
))
565 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
566 if (is_noreturn_func(expr
->fn
))
568 handle_builtin_overflow_func(expr
);
570 case EXPR_INITIALIZER
:
571 split_expr_list(expr
->expr_list
, expr
);
573 case EXPR_IDENTIFIER
:
574 expr_set_parent_expr(expr
->ident_expression
, expr
);
575 __split_expr(expr
->ident_expression
);
578 expr_set_parent_expr(expr
->idx_expression
, expr
);
579 __split_expr(expr
->idx_expression
);
582 expr_set_parent_expr(expr
->init_expr
, expr
);
583 __split_expr(expr
->init_expr
);
586 __pass_to_client(expr
, SYM_HOOK
);
589 __pass_to_client(expr
, STRING_HOOK
);
594 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
595 pop_expression(&big_expression_stack
);
598 static int is_forever_loop(struct statement
*stmt
)
600 struct expression
*expr
;
603 expr
= strip_expr(stmt
->iterator_pre_condition
);
605 expr
= stmt
->iterator_post_condition
;
607 /* this is a for(;;) loop... */
611 if (get_value(expr
, &sval
) && sval
.value
!= 0)
618 static char *get_loop_name(int num
)
622 snprintf(buf
, 255, "-loop%d", num
);
624 return alloc_sname(buf
);
628 * Pre Loops are while and for loops.
630 static void handle_pre_loop(struct statement
*stmt
)
632 int once_through
; /* we go through the loop at least once */
633 struct sm_state
*extra_sm
= NULL
;
636 struct stree
*stree
= NULL
;
637 struct sm_state
*sm
= NULL
;
639 loop_name
= get_loop_name(loop_num
);
642 __split_stmt(stmt
->iterator_pre_statement
);
643 __prev_stmt
= stmt
->iterator_pre_statement
;
645 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
651 __merge_gotos(loop_name
, NULL
);
653 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
654 __in_pre_condition
++;
655 __pass_to_client(stmt
, PRELOOP_HOOK
);
656 __split_whole_condition(stmt
->iterator_pre_condition
);
657 __in_pre_condition
--;
658 FOR_EACH_SM(stree
, sm
) {
659 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
660 } END_FOR_EACH_SM(sm
);
663 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
665 if (option_assume_loops
)
668 __split_stmt(stmt
->iterator_statement
);
669 if (is_forever_loop(stmt
)) {
671 __save_gotos(loop_name
, NULL
);
673 __push_fake_cur_stree();
674 __split_stmt(stmt
->iterator_post_statement
);
675 stree
= __pop_fake_cur_stree();
677 __discard_false_states();
680 if (!__path_is_null())
681 __merge_stree_into_cur(stree
);
685 unchanged
= __iterator_unchanged(extra_sm
);
686 __split_stmt(stmt
->iterator_post_statement
);
687 __prev_stmt
= stmt
->iterator_post_statement
;
690 __save_gotos(loop_name
, NULL
);
691 __in_pre_condition
++;
692 __split_whole_condition(stmt
->iterator_pre_condition
);
693 __in_pre_condition
--;
695 __merge_false_states();
697 __discard_false_states();
699 __merge_false_states();
701 if (extra_sm
&& unchanged
)
702 __extra_pre_loop_hook_after(extra_sm
,
703 stmt
->iterator_post_statement
,
704 stmt
->iterator_pre_condition
);
711 * Post loops are do {} while();
713 static void handle_post_loop(struct statement
*stmt
)
717 loop_name
= get_loop_name(loop_num
);
723 __merge_gotos(loop_name
, NULL
);
724 __split_stmt(stmt
->iterator_statement
);
726 if (!expr_is_zero(stmt
->iterator_post_condition
))
727 __save_gotos(loop_name
, NULL
);
729 if (is_forever_loop(stmt
)) {
732 __split_whole_condition(stmt
->iterator_post_condition
);
733 __use_false_states();
739 static int empty_statement(struct statement
*stmt
)
743 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
748 static int last_stmt_on_same_line(void)
750 struct statement
*stmt
;
753 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
756 if (stmt
->pos
.line
== get_lineno())
759 } END_FOR_EACH_PTR_REVERSE(stmt
);
763 static void split_asm_ops(struct asm_operand_list
*ops
)
765 struct asm_operand
*op
;
767 FOR_EACH_PTR(ops
, op
) {
768 __split_expr(op
->expr
);
769 } END_FOR_EACH_PTR(op
);
772 static int is_case_val(struct statement
*stmt
, sval_t sval
)
776 if (stmt
->type
!= STMT_CASE
)
778 if (!stmt
->case_expression
) {
782 if (!get_value(stmt
->case_expression
, &case_sval
))
784 if (case_sval
.value
== sval
.value
)
789 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
790 struct expression
*case_expr
,
791 struct expression
*case_to
)
794 struct range_list
*rl
= NULL
;
795 struct symbol
*switch_type
;
797 switch_type
= get_type(switch_expr
);
798 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
799 start
= sval_cast(switch_type
, start
);
800 end
= sval_cast(switch_type
, end
);
801 add_range(&rl
, start
, end
);
802 } else if (get_value(case_expr
, &start
)) {
803 start
= sval_cast(switch_type
, start
);
804 add_range(&rl
, start
, start
);
810 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
812 struct statement
*tmp
;
813 struct range_list
*rl
;
815 __split_expr(stmt
->switch_expression
);
816 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
818 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
819 __save_switch_states(top_expression(switch_expr_stack
));
824 stmt
= stmt
->switch_statement
;
826 __push_scope_hooks();
827 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
828 __smatch_lineno
= tmp
->pos
.line
;
829 if (is_case_val(tmp
, sval
)) {
830 rl
= alloc_rl(sval
, sval
);
831 __merge_switches(top_expression(switch_expr_stack
), rl
);
832 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
834 if (__path_is_null())
837 if (__path_is_null()) {
841 } END_FOR_EACH_PTR(tmp
);
843 __call_scope_hooks();
844 if (!__pop_default())
845 __merge_switches(top_expression(switch_expr_stack
), NULL
);
846 __discard_switches();
848 pop_expression(&switch_expr_stack
);
851 static void split_case(struct statement
*stmt
)
853 struct range_list
*rl
= NULL
;
855 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
856 expr_set_parent_stmt(stmt
->case_to
, stmt
);
858 rl
= get_case_rl(top_expression(switch_expr_stack
),
859 stmt
->case_expression
, stmt
->case_to
);
860 while (stmt
->case_statement
->type
== STMT_CASE
) {
861 struct range_list
*tmp
;
863 tmp
= get_case_rl(top_expression(switch_expr_stack
),
864 stmt
->case_statement
->case_expression
,
865 stmt
->case_statement
->case_to
);
868 rl
= rl_union(rl
, tmp
);
869 if (!stmt
->case_expression
)
871 stmt
= stmt
->case_statement
;
874 __merge_switches(top_expression(switch_expr_stack
), rl
);
876 if (!stmt
->case_expression
)
878 __split_stmt(stmt
->case_statement
);
881 int time_parsing_function(void)
883 return ms_since(&fn_start_time
) / 1000;
886 bool taking_too_long(void)
888 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
893 static int is_last_stmt(struct statement
*cur_stmt
)
896 struct statement
*stmt
;
900 fn
= get_base_type(cur_func_sym
);
905 stmt
= fn
->inline_stmt
;
906 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
908 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
909 if (stmt
&& stmt
->type
== STMT_LABEL
)
910 stmt
= stmt
->label_statement
;
911 if (stmt
== cur_stmt
)
916 static void handle_backward_goto(struct statement
*goto_stmt
)
918 const char *goto_name
, *label_name
;
919 struct statement
*func_stmt
;
920 struct symbol
*base_type
= get_base_type(cur_func_sym
);
921 struct statement
*tmp
;
926 if (last_goto_statement_handled
)
928 last_goto_statement_handled
= 1;
930 if (!goto_stmt
->goto_label
||
931 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
932 !goto_stmt
->goto_label
->ident
)
934 goto_name
= goto_stmt
->goto_label
->ident
->name
;
936 func_stmt
= base_type
->stmt
;
938 func_stmt
= base_type
->inline_stmt
;
941 if (func_stmt
->type
!= STMT_COMPOUND
)
944 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
946 if (tmp
->type
!= STMT_LABEL
)
948 if (!tmp
->label_identifier
||
949 tmp
->label_identifier
->type
!= SYM_LABEL
||
950 !tmp
->label_identifier
->ident
)
952 label_name
= tmp
->label_identifier
->ident
->name
;
953 if (strcmp(goto_name
, label_name
) != 0)
958 } END_FOR_EACH_PTR(tmp
);
961 static void fake_a_return(void)
963 struct symbol
*return_type
;
968 return_type
= get_real_base_type(cur_func_sym
);
969 return_type
= get_real_base_type(return_type
);
970 if (return_type
!= &void_ctype
) {
971 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
976 static void split_ret_value(struct expression
*expr
)
983 type
= get_real_base_type(cur_func_sym
);
984 type
= get_real_base_type(type
);
985 expr
= fake_a_variable_assign(type
, expr
);
987 __in_fake_var_assign
++;
989 __in_fake_var_assign
--;
992 static void fake_an_empty_default(struct position pos
)
994 static struct statement none
= {};
997 none
.type
= STMT_NONE
;
998 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1002 static void split_compound(struct statement
*stmt
)
1004 struct statement
*prev
= NULL
;
1005 struct statement
*cur
= NULL
;
1006 struct statement
*next
;
1008 __push_scope_hooks();
1010 FOR_EACH_PTR(stmt
->stmts
, next
) {
1011 /* just set them all ahead of time */
1012 stmt_set_parent_stmt(next
, stmt
);
1022 } END_FOR_EACH_PTR(next
);
1031 * For function scope, then delay calling the scope hooks until the
1032 * end of function hooks can run. I'm not positive this is the right
1035 if (!is_last_stmt(cur
))
1036 __call_scope_hooks();
1040 * This is a hack, work around for detecting empty functions.
1042 static int need_delayed_scope_hooks(void)
1044 struct symbol
*fn
= get_base_type(cur_func_sym
);
1045 struct statement
*stmt
;
1051 stmt
= fn
->inline_stmt
;
1052 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1057 void __split_label_stmt(struct statement
*stmt
)
1059 if (stmt
->label_identifier
&&
1060 stmt
->label_identifier
->type
== SYM_LABEL
&&
1061 stmt
->label_identifier
->ident
) {
1062 loop_count
|= 0x0800000;
1063 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1067 static void find_asm_gotos(struct statement
*stmt
)
1071 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1072 __save_gotos(sym
->ident
->name
, sym
);
1073 } END_FOR_EACH_PTR(sym
);
1076 void __split_stmt(struct statement
*stmt
)
1078 static int indent_cnt
;
1084 if (!__in_fake_assign
)
1085 __silence_warnings_for_stmt
= false;
1087 if (__bail_on_rest_of_function
|| is_skipped_function())
1090 if (out_of_memory() || taking_too_long()) {
1091 struct timeval stop
;
1093 gettimeofday(&stop
, NULL
);
1095 __bail_on_rest_of_function
= 1;
1097 sm_perror("Function too hairy. Giving up. %lu seconds",
1098 stop
.tv_sec
- fn_start_time
.tv_sec
);
1100 final_pass
= 0; /* turn off sm_msg() from here */
1106 add_ptr_list(&big_statement_stack
, stmt
);
1107 free_expression_stack(&big_expression_stack
);
1108 set_position(stmt
->pos
);
1109 __pass_to_client(stmt
, STMT_HOOK
);
1111 switch (stmt
->type
) {
1112 case STMT_DECLARATION
:
1113 split_declaration(stmt
->declaration
);
1116 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1118 split_ret_value(stmt
->ret_value
);
1119 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1120 __process_post_op_stack();
1123 case STMT_EXPRESSION
:
1124 expr_set_parent_stmt(stmt
->expression
, stmt
);
1125 expr_set_parent_stmt(stmt
->context
, stmt
);
1127 __split_expr(stmt
->expression
);
1130 split_compound(stmt
);
1133 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1134 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1135 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1137 if (known_condition_true(stmt
->if_conditional
)) {
1138 __split_stmt(stmt
->if_true
);
1141 if (known_condition_false(stmt
->if_conditional
)) {
1142 __split_stmt(stmt
->if_false
);
1145 __split_whole_condition(stmt
->if_conditional
);
1146 __split_stmt(stmt
->if_true
);
1147 if (empty_statement(stmt
->if_true
) &&
1148 last_stmt_on_same_line() &&
1149 !get_macro_name(stmt
->if_true
->pos
))
1150 sm_warning("if();");
1151 __push_true_states();
1152 __use_false_states();
1153 __split_stmt(stmt
->if_false
);
1154 __merge_true_states();
1157 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1158 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1159 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1160 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1161 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1163 if (stmt
->iterator_pre_condition
)
1164 handle_pre_loop(stmt
);
1165 else if (stmt
->iterator_post_condition
)
1166 handle_post_loop(stmt
);
1168 // these are for(;;) type loops.
1169 handle_pre_loop(stmt
);
1173 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1174 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1176 if (get_value(stmt
->switch_expression
, &sval
)) {
1177 split_known_switch(stmt
, sval
);
1180 __split_expr(stmt
->switch_expression
);
1181 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1182 __save_switch_states(top_expression(switch_expr_stack
));
1186 __split_stmt(stmt
->switch_statement
);
1187 if (!__pop_default() && have_remaining_cases())
1188 fake_an_empty_default(stmt
->pos
);
1189 __discard_switches();
1191 pop_expression(&switch_expr_stack
);
1197 __split_label_stmt(stmt
);
1198 __split_stmt(stmt
->label_statement
);
1201 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1203 __split_expr(stmt
->goto_expression
);
1204 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1205 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1207 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1209 __process_continues();
1211 } else if (stmt
->goto_label
&&
1212 stmt
->goto_label
->type
== SYM_LABEL
&&
1213 stmt
->goto_label
->ident
) {
1214 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1217 if (is_last_stmt(stmt
))
1218 handle_backward_goto(stmt
);
1223 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1225 find_asm_gotos(stmt
);
1226 __pass_to_client(stmt
, ASM_HOOK
);
1227 __split_expr(stmt
->asm_string
);
1228 split_asm_ops(stmt
->asm_outputs
);
1229 split_asm_ops(stmt
->asm_inputs
);
1230 split_expr_list(stmt
->asm_clobbers
, NULL
);
1235 __split_expr(stmt
->range_expression
);
1236 __split_expr(stmt
->range_low
);
1237 __split_expr(stmt
->range_high
);
1240 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1241 if (--indent_cnt
== 0)
1242 __discard_fake_states();
1244 __process_post_op_stack();
1247 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1249 struct expression
*expr
;
1251 FOR_EACH_PTR(expr_list
, expr
) {
1252 expr_set_parent_expr(expr
, parent
);
1254 __process_post_op_stack();
1255 } END_FOR_EACH_PTR(expr
);
1258 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1260 struct symbol
*orig
;
1265 arg
= strip_parens(arg
);
1266 if (arg
!= strip_expr(arg
))
1269 orig
= get_type(arg
);
1276 * I would have expected that we could just do use (orig == type) but I
1277 * guess for pointers we need to get the basetype to do that comparison.
1281 if (orig
->type
!= SYM_PTR
||
1282 type
->type
!= SYM_PTR
) {
1283 if (type_fits(type
, orig
))
1287 orig
= get_real_base_type(orig
);
1288 type
= get_real_base_type(type
);
1295 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*expr
)
1297 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
!= '&' &&
1327 if (get_value(expr
, &sval
))
1331 snprintf(buf
, sizeof(buf
), "__sm_fake_%p", expr
);
1332 var
= fake_variable(type
, buf
);
1333 assign
= assign_expression(var
, '=', expr
);
1334 assign
->smatch_flags
|= Fake
;
1336 parent
= expr_get_parent_expr(expr
);
1337 expr_set_parent_expr(assign
, parent
);
1338 expr_set_parent_expr(expr
, assign
);
1345 static void split_args(struct expression
*expr
)
1347 struct expression
*arg
, *tmp
;
1348 struct symbol
*type
;
1352 FOR_EACH_PTR(expr
->args
, arg
) {
1354 expr_set_parent_expr(arg
, expr
);
1355 type
= get_arg_type(expr
->fn
, i
);
1356 tmp
= fake_a_variable_assign(type
, arg
);
1358 __in_fake_var_assign
++;
1361 __in_fake_var_assign
--;
1362 __process_post_op_stack();
1363 } END_FOR_EACH_PTR(arg
);
1366 static void split_sym(struct symbol
*sym
)
1370 if (!(sym
->namespace & NS_SYMBOL
))
1373 __split_stmt(sym
->stmt
);
1374 __split_expr(sym
->array_size
);
1375 split_symlist(sym
->arguments
);
1376 split_symlist(sym
->symbol_list
);
1377 __split_stmt(sym
->inline_stmt
);
1378 split_symlist(sym
->inline_symbol_list
);
1381 static void split_symlist(struct symbol_list
*sym_list
)
1385 FOR_EACH_PTR(sym_list
, sym
) {
1387 } END_FOR_EACH_PTR(sym
);
1390 typedef void (fake_cb
)(struct expression
*expr
);
1392 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1394 struct symbol
*type
, *tmp
;
1400 name
= member
->name
;
1402 type
= get_type(expr
);
1403 if (!type
|| type
->type
!= SYM_STRUCT
)
1407 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1411 if (strcmp(name
, tmp
->ident
->name
) == 0)
1413 } END_FOR_EACH_PTR(tmp
);
1417 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1419 struct symbol
*type
, *member
;
1422 type
= get_type(expr
);
1423 if (!type
|| type
->type
!= SYM_STRUCT
)
1426 FOR_EACH_PTR(type
->symbol_list
, member
) {
1428 return member
->ident
;
1430 } END_FOR_EACH_PTR(member
);
1434 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1436 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1438 struct expression
*edge_member
, *assign
;
1439 struct symbol
*base
= get_real_base_type(member
);
1443 expr
= member_expression(expr
, '.', member
->ident
);
1445 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1446 struct symbol
*type
;
1448 type
= get_real_base_type(tmp
);
1452 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1453 if (get_extra_state(edge_member
))
1456 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1457 set_inner_struct_members(expr
, tmp
);
1464 assign
= assign_expression(edge_member
, '=', zero_expr());
1465 __split_expr(assign
);
1466 } END_FOR_EACH_PTR(tmp
);
1471 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1474 struct expression
*member
= NULL
;
1475 struct expression
*assign
;
1478 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1479 expr
= strip_expr(expr
->unop
);
1483 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1484 type
= get_real_base_type(tmp
);
1489 member
= member_expression(expr
, op
, tmp
->ident
);
1490 if (get_extra_state(member
))
1494 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1495 set_inner_struct_members(expr
, tmp
);
1498 if (type
->type
== SYM_ARRAY
)
1503 assign
= assign_expression(member
, '=', zero_expr());
1504 __split_expr(assign
);
1505 } END_FOR_EACH_PTR(tmp
);
1508 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1510 struct expression
*deref
, *assign
, *tmp
, *right
;
1511 struct symbol
*struct_type
, *type
;
1512 struct ident
*member
;
1515 struct_type
= get_type(symbol
);
1517 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1521 * We're parsing an initializer that could look something like this:
1522 * struct foo foo = {
1524 * .whatever.xxx = 11,
1528 * So what we have here is a list with 42, .whatever, and .zzz. We need
1529 * to break it up into left and right sides of the assignments.
1533 FOR_EACH_PTR(members
, tmp
) {
1535 if (tmp
->type
== EXPR_IDENTIFIER
) {
1536 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1537 while (tmp
->type
== EXPR_IDENTIFIER
) {
1538 member
= tmp
->expr_ident
;
1539 tmp
= tmp
->ident_expression
;
1541 deref
= member_expression(deref
, '.', member
);
1543 deref
= member_expression(symbol
, '.', member
);
1546 member
= number_to_member(symbol
, member_idx
);
1547 deref
= member_expression(symbol
, '.', member
);
1551 if (right
->type
== EXPR_INITIALIZER
) {
1552 type
= get_type(deref
);
1553 if (type
&& type
->type
== SYM_ARRAY
)
1554 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1556 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1558 assign
= assign_expression(deref
, '=', right
);
1561 } END_FOR_EACH_PTR(tmp
);
1563 set_unset_to_zero(struct_type
, symbol
);
1566 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1568 fake_member_assigns_helper(symbol_expression(sym
),
1569 sym
->initializer
->expr_list
, fake_cb
);
1572 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1574 struct expression
*offset
, *binop
, *assign
, *tmp
;
1575 struct symbol
*type
;
1578 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1582 FOR_EACH_PTR(expr_list
, tmp
) {
1583 if (tmp
->type
== EXPR_INDEX
) {
1584 if (tmp
->idx_from
!= tmp
->idx_to
)
1586 idx
= tmp
->idx_from
;
1587 if (!tmp
->idx_expression
)
1589 tmp
= tmp
->idx_expression
;
1591 offset
= value_expr(idx
);
1592 binop
= array_element_expression(array
, offset
);
1593 if (tmp
->type
== EXPR_INITIALIZER
) {
1594 type
= get_type(binop
);
1595 if (type
&& type
->type
== SYM_ARRAY
)
1596 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1598 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1600 assign
= assign_expression(binop
, '=', tmp
);
1605 } END_FOR_EACH_PTR(tmp
);
1608 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1610 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1613 static void fake_assign_expr(struct symbol
*sym
)
1615 struct expression
*assign
, *symbol
;
1617 symbol
= symbol_expression(sym
);
1618 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1619 __split_expr(assign
);
1622 static void do_initializer_stuff(struct symbol
*sym
)
1624 if (!sym
->initializer
)
1627 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1628 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1629 fake_element_assigns(sym
, __split_expr
);
1631 fake_member_assigns(sym
, __split_expr
);
1633 fake_assign_expr(sym
);
1637 static void split_declaration(struct symbol_list
*sym_list
)
1641 FOR_EACH_PTR(sym_list
, sym
) {
1642 __pass_to_client(sym
, DECLARATION_HOOK
);
1643 do_initializer_stuff(sym
);
1645 } END_FOR_EACH_PTR(sym
);
1648 static void call_global_assign_hooks(struct expression
*assign
)
1650 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1653 static void fake_global_assign(struct symbol
*sym
)
1655 struct expression
*assign
, *symbol
;
1657 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1658 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1659 fake_element_assigns(sym
, call_global_assign_hooks
);
1660 } else if (sym
->initializer
) {
1661 symbol
= symbol_expression(sym
);
1662 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1663 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1665 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1667 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1668 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1669 fake_member_assigns(sym
, call_global_assign_hooks
);
1670 } else if (sym
->initializer
) {
1671 symbol
= symbol_expression(sym
);
1672 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1673 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1675 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1678 symbol
= symbol_expression(sym
);
1679 if (sym
->initializer
) {
1680 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1681 __split_expr(assign
);
1683 assign
= assign_expression(symbol
, '=', zero_expr());
1685 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1689 static void start_function_definition(struct symbol
*sym
)
1691 __in_function_def
= 1;
1692 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1693 __in_function_def
= 0;
1694 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1698 static void split_function(struct symbol
*sym
)
1700 struct symbol
*base_type
= get_base_type(sym
);
1701 struct timeval stop
;
1703 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1706 gettimeofday(&outer_fn_start_time
, NULL
);
1707 gettimeofday(&fn_start_time
, NULL
);
1710 cur_func
= sym
->ident
->name
;
1711 set_position(sym
->pos
);
1713 last_goto_statement_handled
= 0;
1714 sm_debug("new function: %s\n", cur_func
);
1716 if (option_two_passes
) {
1720 start_function_definition(sym
);
1721 __split_stmt(base_type
->stmt
);
1722 __split_stmt(base_type
->inline_stmt
);
1728 start_function_definition(sym
);
1729 __split_stmt(base_type
->stmt
);
1730 __split_stmt(base_type
->inline_stmt
);
1731 __pass_to_client(sym
, END_FUNC_HOOK
);
1732 if (need_delayed_scope_hooks())
1733 __call_scope_hooks();
1734 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1738 gettimeofday(&stop
, NULL
);
1739 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1741 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1744 cur_func_sym
= NULL
;
1746 free_data_info_allocs();
1747 free_expression_stack(&switch_expr_stack
);
1748 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1749 __bail_on_rest_of_function
= 0;
1752 static void save_flow_state(void)
1754 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
1755 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
1756 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
1758 __add_ptr_list(&backup
, big_statement_stack
);
1759 __add_ptr_list(&backup
, big_expression_stack
);
1760 __add_ptr_list(&backup
, big_condition_stack
);
1761 __add_ptr_list(&backup
, switch_expr_stack
);
1763 __add_ptr_list(&backup
, cur_func_sym
);
1765 __add_ptr_list(&backup
, __prev_stmt
);
1766 __add_ptr_list(&backup
, __cur_stmt
);
1767 __add_ptr_list(&backup
, __next_stmt
);
1770 static void *pop_backup(void)
1774 ret
= last_ptr_list(backup
);
1775 delete_ptr_list_last(&backup
);
1779 static void restore_flow_state(void)
1781 __next_stmt
= pop_backup();
1782 __cur_stmt
= pop_backup();
1783 __prev_stmt
= pop_backup();
1785 cur_func_sym
= pop_backup();
1786 switch_expr_stack
= pop_backup();
1787 big_condition_stack
= pop_backup();
1788 big_expression_stack
= pop_backup();
1789 big_statement_stack
= pop_backup();
1790 final_pass
= PTR_INT(pop_backup()) >> 2;
1791 loop_count
= PTR_INT(pop_backup()) >> 2;
1792 loop_num
= PTR_INT(pop_backup()) >> 2;
1795 static void parse_inline(struct expression
*call
)
1797 struct symbol
*base_type
;
1798 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1799 struct timeval time_backup
= fn_start_time
;
1800 struct expression
*orig_inline
= __inline_fn
;
1803 if (out_of_memory() || taking_too_long())
1808 __pass_to_client(call
, INLINE_FN_START
);
1809 final_pass
= 0; /* don't print anything */
1811 orig_budget
= inline_budget
;
1812 inline_budget
= inline_budget
- 5;
1814 base_type
= get_base_type(call
->fn
->symbol
);
1815 cur_func_sym
= call
->fn
->symbol
;
1816 if (call
->fn
->symbol
->ident
)
1817 cur_func
= call
->fn
->symbol
->ident
->name
;
1820 set_position(call
->fn
->symbol
->pos
);
1823 big_statement_stack
= NULL
;
1824 big_expression_stack
= NULL
;
1825 big_condition_stack
= NULL
;
1826 switch_expr_stack
= NULL
;
1828 sm_debug("inline function: %s\n", cur_func
);
1832 start_function_definition(call
->fn
->symbol
);
1833 __split_stmt(base_type
->stmt
);
1834 __split_stmt(base_type
->inline_stmt
);
1835 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1836 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1838 free_expression_stack(&switch_expr_stack
);
1839 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1843 restore_flow_state();
1844 fn_start_time
= time_backup
;
1845 cur_func
= cur_func_bak
;
1847 restore_all_states();
1848 set_position(call
->pos
);
1849 __inline_fn
= orig_inline
;
1850 inline_budget
= orig_budget
;
1851 __pass_to_client(call
, INLINE_FN_END
);
1854 static struct symbol_list
*inlines_called
;
1855 static void add_inline_function(struct symbol
*sym
)
1857 static struct symbol_list
*already_added
;
1860 FOR_EACH_PTR(already_added
, tmp
) {
1863 } END_FOR_EACH_PTR(tmp
);
1865 add_ptr_list(&already_added
, sym
);
1866 add_ptr_list(&inlines_called
, sym
);
1869 static void process_inlines(void)
1873 FOR_EACH_PTR(inlines_called
, tmp
) {
1874 split_function(tmp
);
1875 } END_FOR_EACH_PTR(tmp
);
1876 free_ptr_list(&inlines_called
);
1879 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1883 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1886 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1888 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1890 } END_FOR_EACH_PTR_REVERSE(sym
);
1895 static bool interesting_function(struct symbol
*sym
)
1897 static int prev_stream
= -1;
1898 static bool prev_answer
;
1899 const char *filename
;
1902 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1905 if (sym
->pos
.stream
== prev_stream
)
1908 prev_stream
= sym
->pos
.stream
;
1909 prev_answer
= false;
1911 filename
= stream_name(sym
->pos
.stream
);
1912 len
= strlen(filename
);
1913 if (len
> 0 && filename
[len
- 1] == 'c')
1918 static void split_inlines_in_scope(struct symbol
*sym
)
1920 struct symbol
*base
;
1921 struct symbol_list
*scope_list
;
1924 scope_list
= sym
->scope
->symbols
;
1925 stream
= sym
->pos
.stream
;
1927 /* find the last static symbol in the file */
1928 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1929 if (sym
->pos
.stream
!= stream
)
1931 if (sym
->type
!= SYM_NODE
)
1933 base
= get_base_type(sym
);
1936 if (base
->type
!= SYM_FN
)
1938 if (!base
->inline_stmt
)
1940 if (!interesting_function(sym
))
1942 add_inline_function(sym
);
1943 } END_FOR_EACH_PTR_REVERSE(sym
);
1948 static void split_inlines(struct symbol_list
*sym_list
)
1952 sym
= get_last_scoped_symbol(sym_list
, 0);
1954 split_inlines_in_scope(sym
);
1955 sym
= get_last_scoped_symbol(sym_list
, 1);
1957 split_inlines_in_scope(sym
);
1960 static struct stree
*clone_estates_perm(struct stree
*orig
)
1962 struct stree
*ret
= NULL
;
1963 struct sm_state
*tmp
;
1965 FOR_EACH_SM(orig
, tmp
) {
1966 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1967 } END_FOR_EACH_SM(tmp
);
1972 struct position last_pos
;
1973 static void split_c_file_functions(struct symbol_list
*sym_list
)
1978 FOR_EACH_PTR(sym_list
, sym
) {
1979 set_position(sym
->pos
);
1980 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1981 __pass_to_client(sym
, BASE_HOOK
);
1982 fake_global_assign(sym
);
1984 } END_FOR_EACH_PTR(sym
);
1985 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1988 FOR_EACH_PTR(sym_list
, sym
) {
1989 set_position(sym
->pos
);
1990 last_pos
= sym
->pos
;
1991 if (!interesting_function(sym
))
1993 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1994 split_function(sym
);
1997 last_pos
= sym
->pos
;
1998 } END_FOR_EACH_PTR(sym
);
1999 split_inlines(sym_list
);
2000 __pass_to_client(sym_list
, END_FILE_HOOK
);
2003 static int final_before_fake
;
2004 void init_fake_env(void)
2007 final_before_fake
= final_pass
;
2009 __push_fake_cur_stree();
2013 void end_fake_env(void)
2015 __pop_fake_cur_stree();
2018 final_pass
= final_before_fake
;
2021 static void open_output_files(char *base_file
)
2025 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2026 sm_outfd
= fopen(buf
, "w");
2028 sm_fatal("Cannot open %s", buf
);
2033 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2034 sql_outfd
= fopen(buf
, "w");
2036 sm_fatal("Error: Cannot open %s", buf
);
2038 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2039 caller_info_fd
= fopen(buf
, "w");
2040 if (!caller_info_fd
)
2041 sm_fatal("Error: Cannot open %s", buf
);
2044 void smatch(struct string_list
*filelist
)
2046 struct symbol_list
*sym_list
;
2047 struct timeval stop
, start
;
2051 gettimeofday(&start
, NULL
);
2053 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2054 path
= getcwd(NULL
, 0);
2055 free(full_base_file
);
2057 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2058 full_base_file
= malloc(len
);
2059 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2061 full_base_file
= alloc_string(base_file
);
2063 if (option_file_output
)
2064 open_output_files(base_file
);
2065 sym_list
= sparse_keep_tokens(base_file
);
2066 split_c_file_functions(sym_list
);
2067 } END_FOR_EACH_PTR_NOTAG(base_file
);
2069 gettimeofday(&stop
, NULL
);
2071 set_position(last_pos
);
2074 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2076 sm_msg("mem: %luKb", get_max_memory());