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
;
46 static unsigned int loop_count
;
47 static int last_goto_statement_handled
;
48 int __expr_stmt_count
;
49 int __in_function_def
;
50 int __in_unmatched_hook
;
51 static struct expression_list
*switch_expr_stack
= NULL
;
52 static struct expression_list
*post_op_stack
= NULL
;
54 static struct ptr_list
*fn_data_list
;
55 static struct ptr_list
*backup
;
57 struct expression_list
*big_expression_stack
;
58 struct statement_list
*big_statement_stack
;
59 struct statement
*__prev_stmt
;
60 struct statement
*__cur_stmt
;
61 struct statement
*__next_stmt
;
62 int __in_pre_condition
= 0;
63 int __bail_on_rest_of_function
= 0;
64 static struct timeval fn_start_time
;
65 static struct timeval outer_fn_start_time
;
66 char *get_function(void) { return cur_func
; }
67 int get_lineno(void) { return __smatch_lineno
; }
68 int inside_loop(void) { return !!loop_count
; }
69 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
70 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
71 int in_expression_statement(void) { return !!__expr_stmt_count
; }
73 static void split_symlist(struct symbol_list
*sym_list
);
74 static void split_declaration(struct symbol_list
*sym_list
);
75 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
76 static void split_args(struct expression
*expr
);
77 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
);
78 static void add_inline_function(struct symbol
*sym
);
79 static void parse_inline(struct expression
*expr
);
81 int option_assume_loops
= 0;
82 int option_two_passes
= 0;
83 struct symbol
*cur_func_sym
= NULL
;
84 struct stree
*global_states
;
86 const unsigned long valid_ptr_min
= 4096;
87 unsigned long valid_ptr_max
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
);
88 const sval_t valid_ptr_min_sval
= {
92 sval_t valid_ptr_max_sval
= {
94 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
96 struct range_list
*valid_ptr_rl
;
98 void alloc_valid_ptr_rl(void)
100 valid_ptr_max
= sval_type_max(&ulong_ctype
).value
& ~(MTAG_OFFSET_MASK
);
101 valid_ptr_max_sval
.value
= valid_ptr_max
;
103 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
104 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
105 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
108 int outside_of_function(void)
110 return cur_func_sym
== NULL
;
113 const char *get_filename(void)
115 if (option_info
&& option_full_path
)
116 return full_base_file
;
119 if (option_full_path
)
120 return full_filename
;
124 const char *get_base_file(void)
126 if (option_full_path
)
127 return full_base_file
;
131 static void set_position(struct position pos
)
134 static int prev_stream
= -1;
139 if (pos
.stream
== 0 && pos
.line
== 0)
142 __smatch_lineno
= pos
.line
;
144 if (pos
.stream
== prev_stream
)
147 filename
= stream_name(pos
.stream
);
150 pathname
= getcwd(NULL
, 0);
152 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
153 full_filename
= malloc(len
);
154 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
156 full_filename
= alloc_string(filename
);
161 int is_assigned_call(struct expression
*expr
)
163 struct expression
*parent
= expr_get_parent_expr(expr
);
166 parent
->type
== EXPR_ASSIGNMENT
&&
168 strip_expr(parent
->right
) == expr
)
174 int is_fake_assigned_call(struct expression
*expr
)
176 struct expression
*parent
= expr_get_fake_parent_expr(expr
);
179 parent
->type
== EXPR_ASSIGNMENT
&&
181 strip_expr(parent
->right
) == expr
)
187 static bool is_inline_func(struct expression
*expr
)
189 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
191 if (!expr
->symbol
->definition
)
193 if (expr
->symbol
->definition
->ctype
.modifiers
& MOD_INLINE
)
199 static int is_noreturn_func(struct expression
*expr
)
201 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
203 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
208 static int save_func_time(void *_rl
, int argc
, char **argv
, char **azColName
)
210 unsigned long *rl
= _rl
;
212 *rl
= strtoul(argv
[0], NULL
, 10);
216 static int get_func_time(struct symbol
*sym
)
218 unsigned long time
= 0;
220 run_sql(&save_func_time
, &time
,
221 "select key from return_implies where %s and type = %d;",
222 get_static_filter(sym
), FUNC_TIME
);
227 static int inline_budget
= 20;
229 int inlinable(struct expression
*expr
)
232 struct statement
*last_stmt
= NULL
;
234 if (__inline_fn
) /* don't nest */
237 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
239 if (is_no_inline_function(expr
->symbol
->ident
->name
))
241 sym
= get_base_type(expr
->symbol
);
242 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
243 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
245 if (sym
->stmt
->type
!= STMT_COMPOUND
)
247 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
249 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
250 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
252 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
254 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
260 /* the magic numbers in this function are pulled out of my bum. */
261 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
264 if (get_func_time(expr
->symbol
) >= 2)
270 void __process_post_op_stack(void)
272 struct expression
*expr
;
274 FOR_EACH_PTR(post_op_stack
, expr
) {
275 __pass_to_client(expr
, OP_HOOK
);
276 } END_FOR_EACH_PTR(expr
);
278 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
281 static int handle_comma_assigns(struct expression
*expr
)
283 struct expression
*right
;
284 struct expression
*assign
;
286 right
= strip_expr(expr
->right
);
287 if (right
->type
!= EXPR_COMMA
)
290 __split_expr(right
->left
);
291 __process_post_op_stack();
293 assign
= assign_expression(expr
->left
, '=', right
->right
);
294 __split_expr(assign
);
299 /* This is to handle *p++ = foo; assignments */
300 static int handle_postop_assigns(struct expression
*expr
)
302 struct expression
*left
, *fake_left
;
303 struct expression
*assign
;
305 left
= strip_expr(expr
->left
);
306 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
308 left
= strip_expr(left
->unop
);
309 if (left
->type
!= EXPR_POSTOP
)
312 fake_left
= deref_expression(strip_expr(left
->unop
));
313 assign
= assign_expression(fake_left
, '=', expr
->right
);
315 __split_expr(assign
);
316 __split_expr(expr
->left
);
321 static int prev_expression_is_getting_address(struct expression
*expr
)
323 struct expression
*parent
;
326 parent
= expr_get_parent_expr(expr
);
330 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
332 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
334 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
343 static void handle_builtin_overflow_func(struct expression
*expr
)
345 struct expression
*a
, *b
, *res
, *assign
;
348 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
350 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
352 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
357 a
= get_argument_from_call_expr(expr
->args
, 0);
358 b
= get_argument_from_call_expr(expr
->args
, 1);
359 res
= get_argument_from_call_expr(expr
->args
, 2);
361 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
362 __split_expr(assign
);
365 static int handle__builtin_choose_expr(struct expression
*expr
)
367 struct expression
*const_expr
, *expr1
, *expr2
;
370 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
373 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
374 expr1
= get_argument_from_call_expr(expr
->args
, 1);
375 expr2
= get_argument_from_call_expr(expr
->args
, 2);
377 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
386 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
388 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
391 right
= strip_expr(expr
->right
);
392 if (right
->type
!= EXPR_CALL
)
394 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
397 const_expr
= get_argument_from_call_expr(right
->args
, 0);
398 expr1
= get_argument_from_call_expr(right
->args
, 1);
399 expr2
= get_argument_from_call_expr(right
->args
, 2);
401 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
404 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
409 void __split_expr(struct expression
*expr
)
414 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
416 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
418 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
421 push_expression(&big_expression_stack
, expr
);
422 set_position(expr
->pos
);
423 __pass_to_client(expr
, EXPR_HOOK
);
425 switch (expr
->type
) {
427 expr_set_parent_expr(expr
->unop
, expr
);
429 if (expr
->op
== '*' &&
430 !prev_expression_is_getting_address(expr
))
431 __pass_to_client(expr
, DEREF_HOOK
);
432 __split_expr(expr
->unop
);
433 __pass_to_client(expr
, OP_HOOK
);
436 expr_set_parent_expr(expr
->unop
, expr
);
438 __split_expr(expr
->unop
);
439 push_expression(&post_op_stack
, expr
);
443 if (expr
->statement
&& !expr
->statement
) {
444 stmt_set_parent_stmt(expr
->statement
,
445 last_ptr_list((struct ptr_list
*)big_statement_stack
));
447 __split_stmt(expr
->statement
);
452 expr_set_parent_expr(expr
->left
, expr
);
453 expr_set_parent_expr(expr
->right
, expr
);
455 __pass_to_client(expr
, LOGIC_HOOK
);
456 __handle_logic(expr
);
459 expr_set_parent_expr(expr
->left
, expr
);
460 expr_set_parent_expr(expr
->right
, expr
);
462 __pass_to_client(expr
, BINOP_HOOK
);
464 expr_set_parent_expr(expr
->left
, expr
);
465 expr_set_parent_expr(expr
->right
, expr
);
467 __split_expr(expr
->left
);
468 __process_post_op_stack();
469 __split_expr(expr
->right
);
471 case EXPR_ASSIGNMENT
: {
472 struct expression
*right
;
474 expr_set_parent_expr(expr
->left
, expr
);
475 expr_set_parent_expr(expr
->right
, expr
);
477 right
= strip_expr(expr
->right
);
481 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
484 if (__handle_condition_assigns(expr
))
486 /* foo = (x < 5 ? foo : 5); */
487 if (__handle_select_assigns(expr
))
489 /* foo = ({frob(); frob(); frob(); 1;}) */
490 if (__handle_expr_statement_assigns(expr
))
491 break; // FIXME: got after
493 if (handle_comma_assigns(expr
))
495 if (handle__builtin_choose_expr_assigns(expr
))
497 if (handle_postop_assigns(expr
))
498 break; /* no need to goto after_assign */
500 __split_expr(expr
->right
);
501 if (outside_of_function())
502 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
504 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
506 __fake_struct_member_assignments(expr
);
508 /* Re-examine ->right for inlines. See the commit message */
509 right
= strip_expr(expr
->right
);
510 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
511 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
514 if (get_macro_name(right
->pos
) &&
515 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
516 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
518 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
519 __split_expr(expr
->left
);
523 expr_set_parent_expr(expr
->deref
, expr
);
525 __pass_to_client(expr
, DEREF_HOOK
);
526 __split_expr(expr
->deref
);
529 expr_set_parent_expr(expr
->base
, expr
);
531 __split_expr(expr
->base
);
534 case EXPR_FORCE_CAST
:
535 expr_set_parent_expr(expr
->cast_expression
, expr
);
537 __pass_to_client(expr
, CAST_HOOK
);
538 __split_expr(expr
->cast_expression
);
541 if (expr
->cast_expression
)
542 __pass_to_client(strip_parens(expr
->cast_expression
),
548 case EXPR_CONDITIONAL
:
550 expr_set_parent_expr(expr
->conditional
, expr
);
551 expr_set_parent_expr(expr
->cond_true
, expr
);
552 expr_set_parent_expr(expr
->cond_false
, expr
);
554 if (known_condition_true(expr
->conditional
)) {
555 __split_expr(expr
->cond_true
);
558 if (known_condition_false(expr
->conditional
)) {
559 __split_expr(expr
->cond_false
);
562 __pass_to_client(expr
, SELECT_HOOK
);
563 __split_whole_condition(expr
->conditional
);
564 __split_expr(expr
->cond_true
);
565 __push_true_states();
566 __use_false_states();
567 __split_expr(expr
->cond_false
);
568 __merge_true_states();
571 expr_set_parent_expr(expr
->fn
, expr
);
573 if (sym_name_is("__builtin_constant_p", expr
->fn
))
575 if (handle__builtin_choose_expr(expr
))
577 __split_expr(expr
->fn
);
579 if (is_inline_func(expr
->fn
))
580 add_inline_function(expr
->fn
->symbol
->definition
);
581 if (inlinable(expr
->fn
))
583 __process_post_op_stack();
584 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
585 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
587 if (inlinable(expr
->fn
))
589 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
590 if (is_noreturn_func(expr
->fn
))
592 if (!expr_get_parent_expr(expr
))
593 __discard_fake_states(expr
);
594 handle_builtin_overflow_func(expr
);
596 case EXPR_INITIALIZER
:
597 split_expr_list(expr
->expr_list
, expr
);
599 case EXPR_IDENTIFIER
:
600 expr_set_parent_expr(expr
->ident_expression
, expr
);
601 __split_expr(expr
->ident_expression
);
604 expr_set_parent_expr(expr
->idx_expression
, expr
);
605 __split_expr(expr
->idx_expression
);
608 expr_set_parent_expr(expr
->init_expr
, expr
);
609 __split_expr(expr
->init_expr
);
612 __pass_to_client(expr
, SYM_HOOK
);
615 __pass_to_client(expr
, STRING_HOOK
);
620 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
621 pop_expression(&big_expression_stack
);
624 static int is_forever_loop(struct statement
*stmt
)
626 struct expression
*expr
;
629 expr
= strip_expr(stmt
->iterator_pre_condition
);
631 expr
= stmt
->iterator_post_condition
;
633 /* this is a for(;;) loop... */
637 if (get_value(expr
, &sval
) && sval
.value
!= 0)
644 static char *get_loop_name(int num
)
648 snprintf(buf
, 255, "-loop%d", num
);
650 return alloc_sname(buf
);
654 * Pre Loops are while and for loops.
656 static void handle_pre_loop(struct statement
*stmt
)
658 int once_through
; /* we go through the loop at least once */
659 struct sm_state
*extra_sm
= NULL
;
662 struct stree
*stree
= NULL
;
663 struct sm_state
*sm
= NULL
;
665 loop_name
= get_loop_name(loop_num
);
668 if (stmt
->iterator_pre_statement
) {
669 __split_stmt(stmt
->iterator_pre_statement
);
670 __prev_stmt
= stmt
->iterator_pre_statement
;
673 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
679 __merge_gotos(loop_name
, NULL
);
681 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
682 __in_pre_condition
++;
683 __pass_to_client(stmt
, PRELOOP_HOOK
);
684 __split_whole_condition(stmt
->iterator_pre_condition
);
685 __in_pre_condition
--;
686 FOR_EACH_SM(stree
, sm
) {
687 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
688 } END_FOR_EACH_SM(sm
);
691 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
693 if (option_assume_loops
)
696 __split_stmt(stmt
->iterator_statement
);
697 if (is_forever_loop(stmt
)) {
699 __save_gotos(loop_name
, NULL
);
701 __push_fake_cur_stree();
702 __split_stmt(stmt
->iterator_post_statement
);
703 stree
= __pop_fake_cur_stree();
705 __discard_false_states();
708 if (!__path_is_null())
709 __merge_stree_into_cur(stree
);
713 unchanged
= __iterator_unchanged(extra_sm
);
714 __split_stmt(stmt
->iterator_post_statement
);
715 __prev_stmt
= stmt
->iterator_post_statement
;
718 __save_gotos(loop_name
, NULL
);
719 __in_pre_condition
++;
720 __split_whole_condition(stmt
->iterator_pre_condition
);
721 __in_pre_condition
--;
723 __merge_false_states();
725 __discard_false_states();
727 __merge_false_states();
729 if (extra_sm
&& unchanged
)
730 __extra_pre_loop_hook_after(extra_sm
,
731 stmt
->iterator_post_statement
,
732 stmt
->iterator_pre_condition
);
739 * Post loops are do {} while();
741 static void handle_post_loop(struct statement
*stmt
)
745 loop_name
= get_loop_name(loop_num
);
751 __merge_gotos(loop_name
, NULL
);
752 __split_stmt(stmt
->iterator_statement
);
754 if (!expr_is_zero(stmt
->iterator_post_condition
))
755 __save_gotos(loop_name
, NULL
);
757 if (is_forever_loop(stmt
)) {
760 __split_whole_condition(stmt
->iterator_post_condition
);
761 __use_false_states();
767 static int empty_statement(struct statement
*stmt
)
771 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
776 static int last_stmt_on_same_line(void)
778 struct statement
*stmt
;
781 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
784 if (stmt
->pos
.line
== get_lineno())
787 } END_FOR_EACH_PTR_REVERSE(stmt
);
791 static void split_asm_ops(struct asm_operand_list
*ops
)
793 struct asm_operand
*op
;
795 FOR_EACH_PTR(ops
, op
) {
796 __split_expr(op
->expr
);
797 } END_FOR_EACH_PTR(op
);
800 static int is_case_val(struct statement
*stmt
, sval_t sval
)
804 if (stmt
->type
!= STMT_CASE
)
806 if (!stmt
->case_expression
) {
810 if (!get_value(stmt
->case_expression
, &case_sval
))
812 if (case_sval
.value
== sval
.value
)
817 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
818 struct expression
*case_expr
,
819 struct expression
*case_to
)
822 struct range_list
*rl
= NULL
;
823 struct symbol
*switch_type
;
825 switch_type
= get_type(switch_expr
);
826 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
827 start
= sval_cast(switch_type
, start
);
828 end
= sval_cast(switch_type
, end
);
829 add_range(&rl
, start
, end
);
830 } else if (get_value(case_expr
, &start
)) {
831 start
= sval_cast(switch_type
, start
);
832 add_range(&rl
, start
, start
);
838 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
840 struct statement
*tmp
;
841 struct range_list
*rl
;
843 __split_expr(stmt
->switch_expression
);
844 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
846 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
847 __save_switch_states(top_expression(switch_expr_stack
));
852 stmt
= stmt
->switch_statement
;
854 __push_scope_hooks();
855 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
856 __smatch_lineno
= tmp
->pos
.line
;
857 if (is_case_val(tmp
, sval
)) {
858 rl
= alloc_rl(sval
, sval
);
859 __merge_switches(top_expression(switch_expr_stack
), rl
);
860 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
862 if (__path_is_null())
865 if (__path_is_null()) {
869 } END_FOR_EACH_PTR(tmp
);
871 __call_scope_hooks();
872 if (!__pop_default())
873 __merge_switches(top_expression(switch_expr_stack
), NULL
);
874 __discard_switches();
876 pop_expression(&switch_expr_stack
);
879 static void split_case(struct statement
*stmt
)
881 struct range_list
*rl
= NULL
;
883 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
884 expr_set_parent_stmt(stmt
->case_to
, stmt
);
886 rl
= get_case_rl(top_expression(switch_expr_stack
),
887 stmt
->case_expression
, stmt
->case_to
);
888 while (stmt
->case_statement
->type
== STMT_CASE
) {
889 struct range_list
*tmp
;
891 tmp
= get_case_rl(top_expression(switch_expr_stack
),
892 stmt
->case_statement
->case_expression
,
893 stmt
->case_statement
->case_to
);
896 rl
= rl_union(rl
, tmp
);
897 if (!stmt
->case_expression
)
899 stmt
= stmt
->case_statement
;
902 __merge_switches(top_expression(switch_expr_stack
), rl
);
904 if (!stmt
->case_expression
)
907 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
908 __split_stmt(stmt
->case_statement
);
911 int time_parsing_function(void)
913 return ms_since(&fn_start_time
) / 1000;
916 bool taking_too_long(void)
918 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
923 struct statement
*get_last_stmt(void)
926 struct statement
*stmt
;
928 fn
= get_base_type(cur_func_sym
);
933 stmt
= fn
->inline_stmt
;
934 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
936 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
937 if (stmt
&& stmt
->type
== STMT_LABEL
)
938 stmt
= stmt
->label_statement
;
942 int is_last_stmt(struct statement
*cur_stmt
)
944 struct statement
*last
;
946 last
= get_last_stmt();
947 if (last
&& last
== cur_stmt
)
952 static void handle_backward_goto(struct statement
*goto_stmt
)
954 const char *goto_name
, *label_name
;
955 struct statement
*func_stmt
;
956 struct symbol
*base_type
= get_base_type(cur_func_sym
);
957 struct statement
*tmp
;
962 if (last_goto_statement_handled
)
964 last_goto_statement_handled
= 1;
966 if (!goto_stmt
->goto_label
||
967 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
968 !goto_stmt
->goto_label
->ident
)
970 goto_name
= goto_stmt
->goto_label
->ident
->name
;
972 func_stmt
= base_type
->stmt
;
974 func_stmt
= base_type
->inline_stmt
;
977 if (func_stmt
->type
!= STMT_COMPOUND
)
980 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
982 if (tmp
->type
!= STMT_LABEL
)
984 if (!tmp
->label_identifier
||
985 tmp
->label_identifier
->type
!= SYM_LABEL
||
986 !tmp
->label_identifier
->ident
)
988 label_name
= tmp
->label_identifier
->ident
->name
;
989 if (strcmp(goto_name
, label_name
) != 0)
994 } END_FOR_EACH_PTR(tmp
);
997 static void fake_a_return(void)
999 struct expression
*ret
= NULL
;
1004 if (cur_func_return_type() != &void_ctype
)
1005 ret
= unknown_value_expression(NULL
);
1007 __pass_to_client(ret
, RETURN_HOOK
);
1011 static void split_ret_value(struct expression
*expr
)
1013 struct symbol
*type
;
1018 type
= get_real_base_type(cur_func_sym
);
1019 type
= get_real_base_type(type
);
1020 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1022 __in_fake_var_assign
++;
1024 __in_fake_var_assign
--;
1027 static void fake_an_empty_default(struct position pos
)
1029 static struct statement none
= {};
1032 none
.type
= STMT_NONE
;
1033 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1034 __split_stmt(&none
);
1037 static void split_compound(struct statement
*stmt
)
1039 struct statement
*prev
= NULL
;
1040 struct statement
*cur
= NULL
;
1041 struct statement
*next
;
1043 __push_scope_hooks();
1045 FOR_EACH_PTR(stmt
->stmts
, next
) {
1046 /* just set them all ahead of time */
1047 stmt_set_parent_stmt(next
, stmt
);
1057 } END_FOR_EACH_PTR(next
);
1066 * For function scope, then delay calling the scope hooks until the
1067 * end of function hooks can run. I'm not positive this is the right
1070 if (!is_last_stmt(cur
))
1071 __call_scope_hooks();
1075 * This is a hack, work around for detecting empty functions.
1077 static int need_delayed_scope_hooks(void)
1079 struct symbol
*fn
= get_base_type(cur_func_sym
);
1080 struct statement
*stmt
;
1086 stmt
= fn
->inline_stmt
;
1087 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1092 void __split_label_stmt(struct statement
*stmt
)
1094 if (stmt
->label_identifier
&&
1095 stmt
->label_identifier
->type
== SYM_LABEL
&&
1096 stmt
->label_identifier
->ident
) {
1097 loop_count
|= 0x0800000;
1098 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1102 static void find_asm_gotos(struct statement
*stmt
)
1106 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1107 __save_gotos(sym
->ident
->name
, sym
);
1108 } END_FOR_EACH_PTR(sym
);
1111 void __split_stmt(struct statement
*stmt
)
1113 static int indent_cnt
;
1115 struct timeval start
, stop
;
1117 gettimeofday(&start
, NULL
);
1122 if (!__in_fake_assign
)
1123 __silence_warnings_for_stmt
= false;
1125 if (__bail_on_rest_of_function
|| is_skipped_function())
1128 if (out_of_memory() || taking_too_long()) {
1129 gettimeofday(&start
, NULL
);
1131 __bail_on_rest_of_function
= 1;
1133 sm_perror("Function too hairy. Giving up. %lu seconds",
1134 start
.tv_sec
- fn_start_time
.tv_sec
);
1136 final_pass
= 0; /* turn off sm_msg() from here */
1142 add_ptr_list(&big_statement_stack
, stmt
);
1143 free_expression_stack(&big_expression_stack
);
1144 set_position(stmt
->pos
);
1145 __pass_to_client(stmt
, STMT_HOOK
);
1147 switch (stmt
->type
) {
1148 case STMT_DECLARATION
:
1149 split_declaration(stmt
->declaration
);
1152 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1154 split_ret_value(stmt
->ret_value
);
1155 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1156 __process_post_op_stack();
1159 case STMT_EXPRESSION
:
1160 expr_set_parent_stmt(stmt
->expression
, stmt
);
1161 expr_set_parent_stmt(stmt
->context
, stmt
);
1163 __split_expr(stmt
->expression
);
1166 split_compound(stmt
);
1169 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1170 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1171 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1173 if (known_condition_true(stmt
->if_conditional
)) {
1174 __split_stmt(stmt
->if_true
);
1177 if (known_condition_false(stmt
->if_conditional
)) {
1178 __split_stmt(stmt
->if_false
);
1181 __split_whole_condition(stmt
->if_conditional
);
1182 __split_stmt(stmt
->if_true
);
1183 if (empty_statement(stmt
->if_true
) &&
1184 last_stmt_on_same_line() &&
1185 !get_macro_name(stmt
->if_true
->pos
))
1186 sm_warning("if();");
1187 __push_true_states();
1188 __use_false_states();
1189 __split_stmt(stmt
->if_false
);
1190 __merge_true_states();
1193 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1194 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1195 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1196 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1197 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1199 if (stmt
->iterator_pre_condition
)
1200 handle_pre_loop(stmt
);
1201 else if (stmt
->iterator_post_condition
)
1202 handle_post_loop(stmt
);
1204 // these are for(;;) type loops.
1205 handle_pre_loop(stmt
);
1209 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1210 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1212 if (get_value(stmt
->switch_expression
, &sval
)) {
1213 split_known_switch(stmt
, sval
);
1216 __split_expr(stmt
->switch_expression
);
1217 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1218 __save_switch_states(top_expression(switch_expr_stack
));
1222 __split_stmt(stmt
->switch_statement
);
1223 if (!__pop_default() && have_remaining_cases())
1224 fake_an_empty_default(stmt
->pos
);
1225 __discard_switches();
1227 pop_expression(&switch_expr_stack
);
1233 __split_label_stmt(stmt
);
1234 __split_stmt(stmt
->label_statement
);
1237 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1239 __split_expr(stmt
->goto_expression
);
1240 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1241 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1243 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1245 __process_continues();
1247 } else if (stmt
->goto_label
&&
1248 stmt
->goto_label
->type
== SYM_LABEL
&&
1249 stmt
->goto_label
->ident
) {
1250 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1253 if (is_last_stmt(stmt
))
1254 handle_backward_goto(stmt
);
1259 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1261 find_asm_gotos(stmt
);
1262 __pass_to_client(stmt
, ASM_HOOK
);
1263 __split_expr(stmt
->asm_string
);
1264 split_asm_ops(stmt
->asm_outputs
);
1265 split_asm_ops(stmt
->asm_inputs
);
1266 split_expr_list(stmt
->asm_clobbers
, NULL
);
1271 __split_expr(stmt
->range_expression
);
1272 __split_expr(stmt
->range_low
);
1273 __split_expr(stmt
->range_high
);
1276 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1277 if (--indent_cnt
== 1)
1278 __discard_fake_states(NULL
);
1281 __process_post_op_stack();
1283 gettimeofday(&stop
, NULL
);
1284 if (option_time_stmt
&& stmt
)
1285 sm_msg("stmt_time%s: %ld",
1286 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1287 stop
.tv_sec
- start
.tv_sec
);
1290 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1292 struct expression
*expr
;
1294 FOR_EACH_PTR(expr_list
, expr
) {
1295 expr_set_parent_expr(expr
, parent
);
1297 __process_post_op_stack();
1298 } END_FOR_EACH_PTR(expr
);
1301 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1303 struct symbol
*orig
;
1308 arg
= strip_parens(arg
);
1309 if (arg
!= strip_expr(arg
))
1312 orig
= get_type(arg
);
1318 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1322 * I would have expected that we could just do use (orig == type) but I
1323 * guess for pointers we need to get the basetype to do that comparison.
1327 if (orig
->type
!= SYM_PTR
||
1328 type
->type
!= SYM_PTR
) {
1329 if (type_fits(type
, orig
))
1333 orig
= get_real_base_type(orig
);
1334 type
= get_real_base_type(type
);
1341 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1343 struct expression
*var
, *assign
, *parent
;
1347 if (!expr
|| !cur_func_sym
)
1350 if (expr
->type
== EXPR_ASSIGNMENT
)
1353 /* for va_args then we don't know the type */
1355 type
= get_type(expr
);
1357 cast
= cast_arg(type
, expr
);
1359 * Using expr_to_sym() here is a hack. We want to say that we don't
1360 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1361 * It turns out faking these assignments is way more expensive than I
1362 * would have imagined. I'm not sure why exactly.
1367 * if the code is "return *p;" where "p" is a user pointer then
1368 * we want to create a fake assignment so that it sets the state
1369 * in check_kernel_user_data.c.
1372 if (expr
->type
!= EXPR_PREOP
&&
1373 expr
->op
!= '*' && expr
->op
!= '&' &&
1379 snprintf(buf
, sizeof(buf
), "__sm_fake_%p", expr
);
1381 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1382 var
= fake_variable(type
, buf
);
1383 assign
= assign_expression(var
, '=', expr
);
1384 assign
->smatch_flags
|= Fake
;
1386 parent
= expr_get_parent_expr(expr
);
1387 expr_set_parent_expr(assign
, parent
);
1388 expr_set_parent_expr(expr
, assign
);
1395 static void split_args(struct expression
*expr
)
1397 struct expression
*arg
, *tmp
;
1398 struct symbol
*type
;
1402 FOR_EACH_PTR(expr
->args
, arg
) {
1404 expr_set_parent_expr(arg
, expr
);
1405 type
= get_arg_type(expr
->fn
, i
);
1406 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1408 __in_fake_var_assign
++;
1411 __in_fake_var_assign
--;
1412 __process_post_op_stack();
1413 } END_FOR_EACH_PTR(arg
);
1416 static void split_sym(struct symbol
*sym
)
1420 if (!(sym
->namespace & NS_SYMBOL
))
1423 __split_stmt(sym
->stmt
);
1424 __split_expr(sym
->array_size
);
1425 split_symlist(sym
->arguments
);
1426 split_symlist(sym
->symbol_list
);
1427 __split_stmt(sym
->inline_stmt
);
1428 split_symlist(sym
->inline_symbol_list
);
1431 static void split_symlist(struct symbol_list
*sym_list
)
1435 FOR_EACH_PTR(sym_list
, sym
) {
1437 } END_FOR_EACH_PTR(sym
);
1440 typedef void (fake_cb
)(struct expression
*expr
);
1442 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1444 struct symbol
*type
, *tmp
;
1450 name
= member
->name
;
1452 type
= get_type(expr
);
1453 if (!type
|| type
->type
!= SYM_STRUCT
)
1457 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1461 if (strcmp(name
, tmp
->ident
->name
) == 0)
1463 } END_FOR_EACH_PTR(tmp
);
1467 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1469 struct symbol
*type
, *member
;
1472 type
= get_type(expr
);
1473 if (!type
|| type
->type
!= SYM_STRUCT
)
1476 FOR_EACH_PTR(type
->symbol_list
, member
) {
1478 return member
->ident
;
1480 } END_FOR_EACH_PTR(member
);
1484 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1486 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1488 struct expression
*edge_member
, *assign
;
1489 struct symbol
*base
= get_real_base_type(member
);
1493 expr
= member_expression(expr
, '.', member
->ident
);
1495 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1496 struct symbol
*type
;
1498 type
= get_real_base_type(tmp
);
1502 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1503 if (get_extra_state(edge_member
))
1506 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1507 set_inner_struct_members(expr
, tmp
);
1514 assign
= assign_expression(edge_member
, '=', zero_expr());
1515 __split_expr(assign
);
1516 } END_FOR_EACH_PTR(tmp
);
1521 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1524 struct expression
*member
= NULL
;
1525 struct expression
*assign
;
1527 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1528 type
= get_real_base_type(tmp
);
1533 member
= member_expression(expr
, '.', tmp
->ident
);
1534 if (get_extra_state(member
))
1538 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1539 set_inner_struct_members(expr
, tmp
);
1542 if (type
->type
== SYM_ARRAY
)
1547 assign
= assign_expression(member
, '=', zero_expr());
1548 __split_expr(assign
);
1549 } END_FOR_EACH_PTR(tmp
);
1552 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1554 struct expression
*deref
, *assign
, *tmp
, *right
;
1555 struct symbol
*struct_type
, *type
;
1556 struct ident
*member
;
1559 struct_type
= get_type(symbol
);
1561 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1565 * We're parsing an initializer that could look something like this:
1566 * struct foo foo = {
1568 * .whatever.xxx = 11,
1572 * So what we have here is a list with 42, .whatever, and .zzz. We need
1573 * to break it up into left and right sides of the assignments.
1577 FOR_EACH_PTR(members
, tmp
) {
1579 if (tmp
->type
== EXPR_IDENTIFIER
) {
1580 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1581 while (tmp
->type
== EXPR_IDENTIFIER
) {
1582 member
= tmp
->expr_ident
;
1583 tmp
= tmp
->ident_expression
;
1585 deref
= member_expression(deref
, '.', member
);
1587 deref
= member_expression(symbol
, '.', member
);
1590 member
= number_to_member(symbol
, member_idx
);
1591 deref
= member_expression(symbol
, '.', member
);
1595 if (right
->type
== EXPR_INITIALIZER
) {
1596 type
= get_type(deref
);
1597 if (type
&& type
->type
== SYM_ARRAY
)
1598 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1600 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1602 assign
= assign_expression(deref
, '=', right
);
1605 } END_FOR_EACH_PTR(tmp
);
1607 set_unset_to_zero(struct_type
, symbol
);
1610 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1612 fake_member_assigns_helper(symbol_expression(sym
),
1613 sym
->initializer
->expr_list
, fake_cb
);
1616 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1618 struct expression
*offset
, *binop
, *assign
, *tmp
;
1619 struct symbol
*type
;
1622 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1627 FOR_EACH_PTR(expr_list
, tmp
) {
1628 if (tmp
->type
== EXPR_INDEX
) {
1629 if (tmp
->idx_from
!= tmp
->idx_to
)
1631 idx
= tmp
->idx_from
;
1634 if (!tmp
->idx_expression
)
1636 tmp
= tmp
->idx_expression
;
1638 offset
= value_expr(idx
);
1639 binop
= array_element_expression(array
, offset
);
1640 if (tmp
->type
== EXPR_INITIALIZER
) {
1641 type
= get_type(binop
);
1642 if (type
&& type
->type
== SYM_ARRAY
)
1643 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1645 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1647 assign
= assign_expression(binop
, '=', tmp
);
1654 } END_FOR_EACH_PTR(tmp
);
1656 __call_array_initialized_hooks(array
, max
);
1659 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1661 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1664 static void fake_assign_expr(struct symbol
*sym
)
1666 struct expression
*assign
, *symbol
;
1668 symbol
= symbol_expression(sym
);
1669 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1670 __split_expr(assign
);
1673 static void do_initializer_stuff(struct symbol
*sym
)
1675 if (!sym
->initializer
)
1678 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1679 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1680 fake_element_assigns(sym
, __split_expr
);
1682 fake_member_assigns(sym
, __split_expr
);
1684 fake_assign_expr(sym
);
1688 static void split_declaration(struct symbol_list
*sym_list
)
1692 FOR_EACH_PTR(sym_list
, sym
) {
1693 __pass_to_client(sym
, DECLARATION_HOOK
);
1694 do_initializer_stuff(sym
);
1695 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
1697 } END_FOR_EACH_PTR(sym
);
1700 static void call_global_assign_hooks(struct expression
*assign
)
1702 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1705 static void fake_global_assign(struct symbol
*sym
)
1707 struct expression
*assign
, *symbol
;
1709 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1710 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1711 fake_element_assigns(sym
, call_global_assign_hooks
);
1712 } else if (sym
->initializer
) {
1713 symbol
= symbol_expression(sym
);
1714 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1715 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1717 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1719 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1720 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1721 fake_member_assigns(sym
, call_global_assign_hooks
);
1722 } else if (sym
->initializer
) {
1723 symbol
= symbol_expression(sym
);
1724 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1725 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1727 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1730 symbol
= symbol_expression(sym
);
1731 if (sym
->initializer
) {
1732 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1733 __split_expr(assign
);
1735 assign
= assign_expression(symbol
, '=', zero_expr());
1737 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1741 static void start_function_definition(struct symbol
*sym
)
1743 __in_function_def
= 1;
1744 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1745 __in_function_def
= 0;
1746 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1750 void add_function_data(unsigned long *fn_data
)
1752 __add_ptr_list(&fn_data_list
, fn_data
);
1755 static void clear_function_data(void)
1759 FOR_EACH_PTR(fn_data_list
, tmp
) {
1761 } END_FOR_EACH_PTR(tmp
);
1764 static void record_func_time(void)
1766 struct timeval stop
;
1770 gettimeofday(&stop
, NULL
);
1771 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
1772 snprintf(buf
, sizeof(buf
), "%d", func_time
);
1773 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
1774 if (option_time
&& func_time
> 2) {
1776 sm_msg("func_time: %d", func_time
);
1781 static void split_function(struct symbol
*sym
)
1783 struct symbol
*base_type
= get_base_type(sym
);
1785 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1788 gettimeofday(&outer_fn_start_time
, NULL
);
1789 gettimeofday(&fn_start_time
, NULL
);
1792 cur_func
= sym
->ident
->name
;
1793 set_position(sym
->pos
);
1794 clear_function_data();
1796 last_goto_statement_handled
= 0;
1797 sm_debug("new function: %s\n", cur_func
);
1799 if (option_two_passes
) {
1803 start_function_definition(sym
);
1804 __split_stmt(base_type
->stmt
);
1805 __split_stmt(base_type
->inline_stmt
);
1811 start_function_definition(sym
);
1812 __split_stmt(base_type
->stmt
);
1813 __split_stmt(base_type
->inline_stmt
);
1814 if (!__path_is_null() &&
1815 cur_func_return_type() == &void_ctype
&&
1816 !__bail_on_rest_of_function
) {
1817 __pass_to_client(NULL
, RETURN_HOOK
);
1820 __pass_to_client(sym
, END_FUNC_HOOK
);
1821 if (need_delayed_scope_hooks())
1822 __call_scope_hooks();
1823 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1830 cur_func_sym
= NULL
;
1832 free_data_info_allocs();
1833 free_expression_stack(&switch_expr_stack
);
1834 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1835 __bail_on_rest_of_function
= 0;
1838 static void save_flow_state(void)
1842 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
1843 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
1844 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
1846 __add_ptr_list(&backup
, big_statement_stack
);
1847 __add_ptr_list(&backup
, big_expression_stack
);
1848 __add_ptr_list(&backup
, big_condition_stack
);
1849 __add_ptr_list(&backup
, switch_expr_stack
);
1851 __add_ptr_list(&backup
, cur_func_sym
);
1853 __add_ptr_list(&backup
, __prev_stmt
);
1854 __add_ptr_list(&backup
, __cur_stmt
);
1855 __add_ptr_list(&backup
, __next_stmt
);
1857 FOR_EACH_PTR(fn_data_list
, tmp
) {
1858 __add_ptr_list(&backup
, (void *)*tmp
);
1859 } END_FOR_EACH_PTR(tmp
);
1862 static void *pop_backup(void)
1866 ret
= last_ptr_list(backup
);
1867 delete_ptr_list_last(&backup
);
1871 static void restore_flow_state(void)
1875 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
1876 *tmp
= (unsigned long)pop_backup();
1877 } END_FOR_EACH_PTR_REVERSE(tmp
);
1879 __next_stmt
= pop_backup();
1880 __cur_stmt
= pop_backup();
1881 __prev_stmt
= pop_backup();
1883 cur_func_sym
= pop_backup();
1884 switch_expr_stack
= pop_backup();
1885 big_condition_stack
= pop_backup();
1886 big_expression_stack
= pop_backup();
1887 big_statement_stack
= pop_backup();
1888 final_pass
= PTR_INT(pop_backup()) >> 2;
1889 loop_count
= PTR_INT(pop_backup()) >> 2;
1890 loop_num
= PTR_INT(pop_backup()) >> 2;
1893 static void parse_inline(struct expression
*call
)
1895 struct symbol
*base_type
;
1896 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1897 struct timeval time_backup
= fn_start_time
;
1898 struct expression
*orig_inline
= __inline_fn
;
1901 if (out_of_memory() || taking_too_long())
1906 __pass_to_client(call
, INLINE_FN_START
);
1907 final_pass
= 0; /* don't print anything */
1909 orig_budget
= inline_budget
;
1910 inline_budget
= inline_budget
- 5;
1912 base_type
= get_base_type(call
->fn
->symbol
);
1913 cur_func_sym
= call
->fn
->symbol
;
1914 if (call
->fn
->symbol
->ident
)
1915 cur_func
= call
->fn
->symbol
->ident
->name
;
1918 set_position(call
->fn
->symbol
->pos
);
1921 big_statement_stack
= NULL
;
1922 big_expression_stack
= NULL
;
1923 big_condition_stack
= NULL
;
1924 switch_expr_stack
= NULL
;
1926 sm_debug("inline function: %s\n", cur_func
);
1928 clear_function_data();
1931 start_function_definition(call
->fn
->symbol
);
1932 __split_stmt(base_type
->stmt
);
1933 __split_stmt(base_type
->inline_stmt
);
1934 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1935 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1936 call
->fn
->symbol
->parsed
= true;
1938 free_expression_stack(&switch_expr_stack
);
1939 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1943 restore_flow_state();
1944 fn_start_time
= time_backup
;
1945 cur_func
= cur_func_bak
;
1947 restore_all_states();
1948 set_position(call
->pos
);
1949 __inline_fn
= orig_inline
;
1950 inline_budget
= orig_budget
;
1951 __pass_to_client(call
, INLINE_FN_END
);
1954 static struct symbol_list
*inlines_called
;
1955 static void add_inline_function(struct symbol
*sym
)
1957 static struct symbol_list
*already_added
;
1960 FOR_EACH_PTR(already_added
, tmp
) {
1963 } END_FOR_EACH_PTR(tmp
);
1965 add_ptr_list(&already_added
, sym
);
1966 add_ptr_list(&inlines_called
, sym
);
1969 static void process_inlines(void)
1973 FOR_EACH_PTR(inlines_called
, tmp
) {
1974 split_function(tmp
);
1975 } END_FOR_EACH_PTR(tmp
);
1976 free_ptr_list(&inlines_called
);
1979 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1983 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1986 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1988 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1990 } END_FOR_EACH_PTR_REVERSE(sym
);
1995 static bool interesting_function(struct symbol
*sym
)
1997 static int prev_stream
= -1;
1998 static bool prev_answer
;
1999 const char *filename
;
2002 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2005 if (sym
->pos
.stream
== prev_stream
)
2008 prev_stream
= sym
->pos
.stream
;
2009 prev_answer
= false;
2011 filename
= stream_name(sym
->pos
.stream
);
2012 len
= strlen(filename
);
2013 if (len
> 0 && filename
[len
- 1] == 'c')
2018 static void split_inlines_in_scope(struct symbol
*sym
)
2020 struct symbol
*base
;
2021 struct symbol_list
*scope_list
;
2024 scope_list
= sym
->scope
->symbols
;
2025 stream
= sym
->pos
.stream
;
2027 /* find the last static symbol in the file */
2028 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2029 if (sym
->pos
.stream
!= stream
)
2031 if (sym
->type
!= SYM_NODE
)
2033 base
= get_base_type(sym
);
2036 if (base
->type
!= SYM_FN
)
2038 if (!base
->inline_stmt
)
2040 if (!interesting_function(sym
))
2042 add_inline_function(sym
);
2043 } END_FOR_EACH_PTR_REVERSE(sym
);
2048 static void split_inlines(struct symbol_list
*sym_list
)
2052 sym
= get_last_scoped_symbol(sym_list
, 0);
2054 split_inlines_in_scope(sym
);
2055 sym
= get_last_scoped_symbol(sym_list
, 1);
2057 split_inlines_in_scope(sym
);
2060 static struct stree
*clone_estates_perm(struct stree
*orig
)
2062 struct stree
*ret
= NULL
;
2063 struct sm_state
*tmp
;
2065 FOR_EACH_SM(orig
, tmp
) {
2066 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2067 } END_FOR_EACH_SM(tmp
);
2072 struct position last_pos
;
2073 static void split_c_file_functions(struct symbol_list
*sym_list
)
2078 FOR_EACH_PTR(sym_list
, sym
) {
2079 set_position(sym
->pos
);
2080 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2081 __pass_to_client(sym
, BASE_HOOK
);
2082 fake_global_assign(sym
);
2083 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2085 } END_FOR_EACH_PTR(sym
);
2086 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2089 FOR_EACH_PTR(sym_list
, sym
) {
2090 set_position(sym
->pos
);
2091 last_pos
= sym
->pos
;
2092 if (!interesting_function(sym
))
2094 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2095 split_function(sym
);
2098 last_pos
= sym
->pos
;
2099 } END_FOR_EACH_PTR(sym
);
2100 split_inlines(sym_list
);
2101 __pass_to_client(sym_list
, END_FILE_HOOK
);
2104 static int final_before_fake
;
2105 void init_fake_env(void)
2108 final_before_fake
= final_pass
;
2110 __push_fake_cur_stree();
2114 void end_fake_env(void)
2116 __pop_fake_cur_stree();
2119 final_pass
= final_before_fake
;
2122 static void open_output_files(char *base_file
)
2126 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2127 sm_outfd
= fopen(buf
, "w");
2129 sm_fatal("Cannot open %s", buf
);
2134 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2135 sql_outfd
= fopen(buf
, "w");
2137 sm_fatal("Error: Cannot open %s", buf
);
2139 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2140 caller_info_fd
= fopen(buf
, "w");
2141 if (!caller_info_fd
)
2142 sm_fatal("Error: Cannot open %s", buf
);
2145 void smatch(struct string_list
*filelist
)
2147 struct symbol_list
*sym_list
;
2148 struct timeval stop
, start
;
2152 gettimeofday(&start
, NULL
);
2154 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2155 path
= getcwd(NULL
, 0);
2156 free(full_base_file
);
2158 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2159 full_base_file
= malloc(len
);
2160 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2162 full_base_file
= alloc_string(base_file
);
2164 if (option_file_output
)
2165 open_output_files(base_file
);
2166 base_file_stream
= input_stream_nr
;
2167 sym_list
= sparse_keep_tokens(base_file
);
2168 split_c_file_functions(sym_list
);
2169 } END_FOR_EACH_PTR_NOTAG(base_file
);
2171 gettimeofday(&stop
, NULL
);
2173 set_position(last_pos
);
2176 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2178 sm_msg("mem: %luKb", get_max_memory());