2 * Copyright (C) 2006,2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
29 int __in_fake_struct_assign
;
33 struct expression
*__inline_fn
;
35 static int __smatch_lineno
= 0;
37 static char *base_file
;
38 static const char *filename
;
39 static char *pathname
;
40 static char *full_filename
;
41 static char *full_base_file
;
42 static char *cur_func
;
43 static unsigned int loop_count
;
44 static int last_goto_statement_handled
;
45 int __expr_stmt_count
;
46 int __in_function_def
;
47 static struct expression_list
*switch_expr_stack
= NULL
;
48 static struct expression_list
*post_op_stack
= NULL
;
50 static struct ptr_list
*backup
;
52 struct expression_list
*big_expression_stack
;
53 struct statement_list
*big_statement_stack
;
54 struct statement
*__prev_stmt
;
55 struct statement
*__cur_stmt
;
56 struct statement
*__next_stmt
;
57 int __in_pre_condition
= 0;
58 int __bail_on_rest_of_function
= 0;
59 static struct timeval fn_start_time
;
60 char *get_function(void) { return cur_func
; }
61 int get_lineno(void) { return __smatch_lineno
; }
62 int inside_loop(void) { return !!loop_count
; }
63 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
64 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
65 int in_expression_statement(void) { return !!__expr_stmt_count
; }
67 static void split_symlist(struct symbol_list
*sym_list
);
68 static void split_declaration(struct symbol_list
*sym_list
);
69 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
70 static void add_inline_function(struct symbol
*sym
);
71 static void parse_inline(struct expression
*expr
);
73 int option_assume_loops
= 0;
74 int option_two_passes
= 0;
75 struct symbol
*cur_func_sym
= NULL
;
76 struct stree
*global_states
;
78 long long valid_ptr_min
= 4096;
79 long long valid_ptr_max
= 2117777777;
80 sval_t valid_ptr_min_sval
= {
84 sval_t valid_ptr_max_sval
= {
86 {.value
= LONG_MAX
- 100000},
88 struct range_list
*valid_ptr_rl
;
90 static void set_valid_ptr_max(void)
92 if (type_bits(&ptr_ctype
) == 32)
93 valid_ptr_max
= 2117777777;
94 else if (type_bits(&ptr_ctype
) == 64)
95 valid_ptr_max
= 2117777777777777777LL;
97 valid_ptr_max_sval
.value
= valid_ptr_max
;
100 static void alloc_valid_ptr_rl(void)
102 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
103 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
104 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
107 int outside_of_function(void)
109 return cur_func_sym
== NULL
;
112 const char *get_filename(void)
114 if (option_info
&& option_full_path
)
115 return full_base_file
;
118 if (option_full_path
)
119 return full_filename
;
123 const char *get_base_file(void)
125 if (option_full_path
)
126 return full_base_file
;
130 static void set_position(struct position pos
)
133 static int prev_stream
= -1;
138 if (pos
.stream
== 0 && pos
.line
== 0)
141 __smatch_lineno
= pos
.line
;
143 if (pos
.stream
== prev_stream
)
146 filename
= stream_name(pos
.stream
);
149 pathname
= getcwd(NULL
, 0);
151 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
152 full_filename
= malloc(len
);
153 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
155 full_filename
= alloc_string(filename
);
160 int is_assigned_call(struct expression
*expr
)
162 struct expression
*parent
= expr_get_parent_expr(expr
);
165 parent
->type
== EXPR_ASSIGNMENT
&&
167 strip_expr(parent
->right
) == expr
)
173 static int is_inline_func(struct expression
*expr
)
175 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
177 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
182 static int is_noreturn_func(struct expression
*expr
)
184 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
186 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
191 static int inline_budget
= 20;
193 int inlinable(struct expression
*expr
)
196 struct statement
*last_stmt
= NULL
;
198 if (__inline_fn
) /* don't nest */
201 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
203 if (is_no_inline_function(expr
->symbol
->ident
->name
))
205 sym
= get_base_type(expr
->symbol
);
206 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
207 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
209 if (sym
->stmt
->type
!= STMT_COMPOUND
)
211 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
213 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
214 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
216 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
218 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
224 /* the magic numbers in this function are pulled out of my bum. */
225 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
231 void __process_post_op_stack(void)
233 struct expression
*expr
;
235 FOR_EACH_PTR(post_op_stack
, expr
) {
236 __pass_to_client(expr
, OP_HOOK
);
237 } END_FOR_EACH_PTR(expr
);
239 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
242 static int handle_comma_assigns(struct expression
*expr
)
244 struct expression
*right
;
245 struct expression
*assign
;
247 right
= strip_expr(expr
->right
);
248 if (right
->type
!= EXPR_COMMA
)
251 __split_expr(right
->left
);
252 __process_post_op_stack();
254 assign
= assign_expression(expr
->left
, '=', right
->right
);
255 __split_expr(assign
);
260 /* This is to handle *p++ = foo; assignments */
261 static int handle_postop_assigns(struct expression
*expr
)
263 struct expression
*left
, *fake_left
;
264 struct expression
*assign
;
266 left
= strip_expr(expr
->left
);
267 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
269 left
= strip_expr(left
->unop
);
270 if (left
->type
!= EXPR_POSTOP
)
273 fake_left
= deref_expression(strip_expr(left
->unop
));
274 assign
= assign_expression(fake_left
, '=', expr
->right
);
276 __split_expr(assign
);
277 __split_expr(expr
->left
);
282 static int prev_expression_is_getting_address(struct expression
*expr
)
284 struct expression
*parent
;
287 parent
= expr_get_parent_expr(expr
);
291 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
293 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
295 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
304 static int handle__builtin_choose_expr(struct expression
*expr
)
306 struct expression
*const_expr
, *expr1
, *expr2
;
309 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
312 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
313 expr1
= get_argument_from_call_expr(expr
->args
, 1);
314 expr2
= get_argument_from_call_expr(expr
->args
, 2);
316 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
325 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
327 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
330 right
= strip_expr(expr
->right
);
331 if (right
->type
!= EXPR_CALL
)
333 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
336 const_expr
= get_argument_from_call_expr(right
->args
, 0);
337 expr1
= get_argument_from_call_expr(right
->args
, 1);
338 expr2
= get_argument_from_call_expr(right
->args
, 2);
340 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
343 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
348 void __split_expr(struct expression
*expr
)
353 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
355 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
357 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
360 push_expression(&big_expression_stack
, expr
);
361 set_position(expr
->pos
);
362 __pass_to_client(expr
, EXPR_HOOK
);
364 switch (expr
->type
) {
366 expr_set_parent_expr(expr
->unop
, expr
);
368 if (expr
->op
== '*' &&
369 !prev_expression_is_getting_address(expr
))
370 __pass_to_client(expr
, DEREF_HOOK
);
371 __split_expr(expr
->unop
);
372 __pass_to_client(expr
, OP_HOOK
);
375 expr_set_parent_expr(expr
->unop
, expr
);
377 __split_expr(expr
->unop
);
378 push_expression(&post_op_stack
, expr
);
382 if (expr
->statement
&& !expr
->statement
) {
383 stmt_set_parent_stmt(expr
->statement
,
384 last_ptr_list((struct ptr_list
*)big_statement_stack
));
386 __split_stmt(expr
->statement
);
391 expr_set_parent_expr(expr
->left
, expr
);
392 expr_set_parent_expr(expr
->right
, expr
);
394 __pass_to_client(expr
, LOGIC_HOOK
);
395 __handle_logic(expr
);
398 expr_set_parent_expr(expr
->left
, expr
);
399 expr_set_parent_expr(expr
->right
, expr
);
401 __pass_to_client(expr
, BINOP_HOOK
);
403 expr_set_parent_expr(expr
->left
, expr
);
404 expr_set_parent_expr(expr
->right
, expr
);
406 __split_expr(expr
->left
);
407 __process_post_op_stack();
408 __split_expr(expr
->right
);
410 case EXPR_ASSIGNMENT
: {
411 struct expression
*right
;
413 expr_set_parent_expr(expr
->left
, expr
);
414 expr_set_parent_expr(expr
->right
, expr
);
416 right
= strip_expr(expr
->right
);
420 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
423 if (__handle_condition_assigns(expr
))
425 /* foo = (x < 5 ? foo : 5); */
426 if (__handle_select_assigns(expr
))
428 /* foo = ({frob(); frob(); frob(); 1;}) */
429 if (__handle_expr_statement_assigns(expr
))
432 if (handle_comma_assigns(expr
))
434 if (handle_postop_assigns(expr
))
436 if (handle__builtin_choose_expr_assigns(expr
))
439 __split_expr(expr
->right
);
440 if (outside_of_function())
441 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
443 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
445 __fake_struct_member_assignments(expr
);
447 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
448 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
450 if (get_macro_name(right
->pos
) &&
451 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
452 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
454 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
456 __split_expr(expr
->left
);
460 expr_set_parent_expr(expr
->deref
, expr
);
462 __pass_to_client(expr
, DEREF_HOOK
);
463 __split_expr(expr
->deref
);
466 expr_set_parent_expr(expr
->base
, expr
);
468 __split_expr(expr
->base
);
471 case EXPR_FORCE_CAST
:
472 expr_set_parent_expr(expr
->cast_expression
, expr
);
474 __pass_to_client(expr
, CAST_HOOK
);
475 __split_expr(expr
->cast_expression
);
478 if (expr
->cast_expression
)
479 __pass_to_client(strip_parens(expr
->cast_expression
),
484 evaluate_expression(expr
);
486 case EXPR_CONDITIONAL
:
488 expr_set_parent_expr(expr
->conditional
, expr
);
489 expr_set_parent_expr(expr
->cond_true
, expr
);
490 expr_set_parent_expr(expr
->cond_false
, expr
);
492 if (known_condition_true(expr
->conditional
)) {
493 __split_expr(expr
->cond_true
);
496 if (known_condition_false(expr
->conditional
)) {
497 __split_expr(expr
->cond_false
);
500 __pass_to_client(expr
, SELECT_HOOK
);
501 __split_whole_condition(expr
->conditional
);
502 __split_expr(expr
->cond_true
);
503 __push_true_states();
504 __use_false_states();
505 __split_expr(expr
->cond_false
);
506 __merge_true_states();
509 expr_set_parent_expr(expr
->fn
, expr
);
511 if (sym_name_is("__builtin_constant_p", expr
->fn
))
513 if (handle__builtin_choose_expr(expr
))
515 split_expr_list(expr
->args
, expr
);
516 __split_expr(expr
->fn
);
517 if (is_inline_func(expr
->fn
))
518 add_inline_function(expr
->fn
->symbol
);
519 if (inlinable(expr
->fn
))
521 __process_post_op_stack();
522 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
523 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
525 if (inlinable(expr
->fn
)) {
528 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
529 if (is_noreturn_func(expr
->fn
))
532 case EXPR_INITIALIZER
:
533 split_expr_list(expr
->expr_list
, expr
);
535 case EXPR_IDENTIFIER
:
536 expr_set_parent_expr(expr
->ident_expression
, expr
);
537 __split_expr(expr
->ident_expression
);
540 expr_set_parent_expr(expr
->idx_expression
, expr
);
541 __split_expr(expr
->idx_expression
);
544 expr_set_parent_expr(expr
->init_expr
, expr
);
545 __split_expr(expr
->init_expr
);
548 __pass_to_client(expr
, SYM_HOOK
);
551 __pass_to_client(expr
, STRING_HOOK
);
556 pop_expression(&big_expression_stack
);
559 static int is_forever_loop(struct statement
*stmt
)
561 struct expression
*expr
;
564 expr
= strip_expr(stmt
->iterator_pre_condition
);
566 expr
= stmt
->iterator_post_condition
;
568 /* this is a for(;;) loop... */
572 if (get_value(expr
, &sval
) && sval
.value
!= 0)
579 static char *get_loop_name(int num
)
583 snprintf(buf
, 255, "-loop%d", num
);
585 return alloc_sname(buf
);
589 * Pre Loops are while and for loops.
591 static void handle_pre_loop(struct statement
*stmt
)
593 int once_through
; /* we go through the loop at least once */
594 struct sm_state
*extra_sm
= NULL
;
597 struct stree
*stree
= NULL
;
598 struct sm_state
*sm
= NULL
;
600 loop_name
= get_loop_name(loop_num
);
603 __split_stmt(stmt
->iterator_pre_statement
);
604 __prev_stmt
= stmt
->iterator_pre_statement
;
606 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
612 __merge_gotos(loop_name
, NULL
);
614 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
615 __in_pre_condition
++;
616 __pass_to_client(stmt
, PRELOOP_HOOK
);
617 __split_whole_condition(stmt
->iterator_pre_condition
);
618 __in_pre_condition
--;
619 FOR_EACH_SM(stree
, sm
) {
620 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
621 } END_FOR_EACH_SM(sm
);
624 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
626 if (option_assume_loops
)
629 __split_stmt(stmt
->iterator_statement
);
630 if (is_forever_loop(stmt
)) {
632 __save_gotos(loop_name
, NULL
);
634 __push_fake_cur_stree();
635 __split_stmt(stmt
->iterator_post_statement
);
636 stree
= __pop_fake_cur_stree();
638 __discard_false_states();
641 if (!__path_is_null())
642 __merge_stree_into_cur(stree
);
646 unchanged
= __iterator_unchanged(extra_sm
);
647 __split_stmt(stmt
->iterator_post_statement
);
648 __prev_stmt
= stmt
->iterator_post_statement
;
651 __save_gotos(loop_name
, NULL
);
652 __in_pre_condition
++;
653 __split_whole_condition(stmt
->iterator_pre_condition
);
654 __in_pre_condition
--;
656 __merge_false_states();
658 __discard_false_states();
660 __merge_false_states();
662 if (extra_sm
&& unchanged
)
663 __extra_pre_loop_hook_after(extra_sm
,
664 stmt
->iterator_post_statement
,
665 stmt
->iterator_pre_condition
);
672 * Post loops are do {} while();
674 static void handle_post_loop(struct statement
*stmt
)
678 loop_name
= get_loop_name(loop_num
);
684 __merge_gotos(loop_name
, NULL
);
685 __split_stmt(stmt
->iterator_statement
);
687 if (!is_zero(stmt
->iterator_post_condition
))
688 __save_gotos(loop_name
, NULL
);
690 if (is_forever_loop(stmt
)) {
693 __split_whole_condition(stmt
->iterator_post_condition
);
694 __use_false_states();
700 static int empty_statement(struct statement
*stmt
)
704 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
709 static int last_stmt_on_same_line(void)
711 struct statement
*stmt
;
714 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
717 if (stmt
->pos
.line
== get_lineno())
720 } END_FOR_EACH_PTR_REVERSE(stmt
);
724 static void split_asm_constraints(struct expression_list
*expr_list
)
726 struct expression
*expr
;
729 FOR_EACH_PTR(expr_list
, expr
) {
731 case 0: /* identifier */
732 case 1: /* constraint */
735 case 2: /* expression */
740 } END_FOR_EACH_PTR(expr
);
743 static int is_case_val(struct statement
*stmt
, sval_t sval
)
747 if (stmt
->type
!= STMT_CASE
)
749 if (!stmt
->case_expression
) {
753 if (!get_value(stmt
->case_expression
, &case_sval
))
755 if (case_sval
.value
== sval
.value
)
760 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
761 struct expression
*case_expr
,
762 struct expression
*case_to
)
765 struct range_list
*rl
= NULL
;
766 struct symbol
*switch_type
;
768 switch_type
= get_type(switch_expr
);
769 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
770 start
= sval_cast(switch_type
, start
);
771 end
= sval_cast(switch_type
, end
);
772 add_range(&rl
, start
, end
);
773 } else if (get_value(case_expr
, &start
)) {
774 start
= sval_cast(switch_type
, start
);
775 add_range(&rl
, start
, start
);
781 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
783 struct statement
*tmp
;
784 struct range_list
*rl
;
786 __split_expr(stmt
->switch_expression
);
787 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
789 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
790 __save_switch_states(top_expression(switch_expr_stack
));
795 stmt
= stmt
->switch_statement
;
797 __push_scope_hooks();
798 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
799 __smatch_lineno
= tmp
->pos
.line
;
800 if (is_case_val(tmp
, sval
)) {
801 rl
= alloc_rl(sval
, sval
);
802 __merge_switches(top_expression(switch_expr_stack
), rl
);
803 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
805 if (__path_is_null())
808 if (__path_is_null()) {
812 } END_FOR_EACH_PTR(tmp
);
814 __call_scope_hooks();
815 if (!__pop_default())
816 __merge_switches(top_expression(switch_expr_stack
), NULL
);
817 __discard_switches();
819 pop_expression(&switch_expr_stack
);
822 static void split_case(struct statement
*stmt
)
824 struct range_list
*rl
= NULL
;
826 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
827 expr_set_parent_stmt(stmt
->case_to
, stmt
);
829 rl
= get_case_rl(top_expression(switch_expr_stack
),
830 stmt
->case_expression
, stmt
->case_to
);
831 while (stmt
->case_statement
->type
== STMT_CASE
) {
832 struct range_list
*tmp
;
834 tmp
= get_case_rl(top_expression(switch_expr_stack
),
835 stmt
->case_statement
->case_expression
,
836 stmt
->case_statement
->case_to
);
839 rl
= rl_union(rl
, tmp
);
840 if (!stmt
->case_expression
)
842 stmt
= stmt
->case_statement
;
845 __merge_switches(top_expression(switch_expr_stack
), rl
);
847 if (!stmt
->case_expression
)
849 __split_stmt(stmt
->case_statement
);
852 int time_parsing_function(void)
854 return ms_since(&fn_start_time
) / 1000;
857 static int taking_too_long(void)
859 if (time_parsing_function() > 60 * 5) /* five minutes */
864 static int is_last_stmt(struct statement
*cur_stmt
)
866 struct symbol
*fn
= get_base_type(cur_func_sym
);
867 struct statement
*stmt
;
873 stmt
= fn
->inline_stmt
;
874 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
876 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
877 if (stmt
&& stmt
->type
== STMT_LABEL
)
878 stmt
= stmt
->label_statement
;
879 if (stmt
== cur_stmt
)
884 static void handle_backward_goto(struct statement
*goto_stmt
)
886 const char *goto_name
, *label_name
;
887 struct statement
*func_stmt
;
888 struct symbol
*base_type
= get_base_type(cur_func_sym
);
889 struct statement
*tmp
;
894 if (last_goto_statement_handled
)
896 last_goto_statement_handled
= 1;
898 if (!goto_stmt
->goto_label
||
899 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
900 !goto_stmt
->goto_label
->ident
)
902 goto_name
= goto_stmt
->goto_label
->ident
->name
;
904 func_stmt
= base_type
->stmt
;
906 func_stmt
= base_type
->inline_stmt
;
909 if (func_stmt
->type
!= STMT_COMPOUND
)
912 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
914 if (tmp
->type
!= STMT_LABEL
)
916 if (!tmp
->label_identifier
||
917 tmp
->label_identifier
->type
!= SYM_LABEL
||
918 !tmp
->label_identifier
->ident
)
920 label_name
= tmp
->label_identifier
->ident
->name
;
921 if (strcmp(goto_name
, label_name
) != 0)
926 } END_FOR_EACH_PTR(tmp
);
929 static void fake_a_return(void)
931 struct symbol
*return_type
;
936 return_type
= get_real_base_type(cur_func_sym
);
937 return_type
= get_real_base_type(return_type
);
938 if (return_type
!= &void_ctype
) {
939 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
944 static void fake_an_empty_default(struct position pos
)
946 static struct statement none
= {};
949 none
.type
= STMT_NONE
;
950 __merge_switches(top_expression(switch_expr_stack
), NULL
);
954 static void split_compound(struct statement
*stmt
)
956 struct statement
*prev
= NULL
;
957 struct statement
*cur
= NULL
;
958 struct statement
*next
;
960 __push_scope_hooks();
962 FOR_EACH_PTR(stmt
->stmts
, next
) {
963 /* just set them all ahead of time */
964 stmt_set_parent_stmt(next
, stmt
);
974 } END_FOR_EACH_PTR(next
);
983 * For function scope, then delay calling the scope hooks until the
984 * end of function hooks can run. I'm not positive this is the right
987 if (!is_last_stmt(cur
))
988 __call_scope_hooks();
992 * This is a hack, work around for detecting empty functions.
994 static int need_delayed_scope_hooks(void)
996 struct symbol
*fn
= get_base_type(cur_func_sym
);
997 struct statement
*stmt
;
1003 stmt
= fn
->inline_stmt
;
1004 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1009 void __split_label_stmt(struct statement
*stmt
)
1011 if (stmt
->label_identifier
&&
1012 stmt
->label_identifier
->type
== SYM_LABEL
&&
1013 stmt
->label_identifier
->ident
) {
1014 loop_count
|= 0x0800000;
1015 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1019 static void find_asm_gotos(struct statement
*stmt
)
1023 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1024 __save_gotos(sym
->ident
->name
, sym
);
1025 } END_FOR_EACH_PTR(sym
);
1028 void __split_stmt(struct statement
*stmt
)
1035 if (!__in_fake_assign
)
1036 __silence_warnings_for_stmt
= false;
1038 if (__bail_on_rest_of_function
)
1041 if (out_of_memory() || taking_too_long()) {
1042 struct timeval stop
;
1044 gettimeofday(&stop
, NULL
);
1046 __bail_on_rest_of_function
= 1;
1048 sm_msg("Function too hairy. Giving up. %lu seconds",
1049 stop
.tv_sec
- fn_start_time
.tv_sec
);
1051 final_pass
= 0; /* turn off sm_msg() from here */
1055 add_ptr_list(&big_statement_stack
, stmt
);
1056 free_expression_stack(&big_expression_stack
);
1057 set_position(stmt
->pos
);
1058 __pass_to_client(stmt
, STMT_HOOK
);
1060 switch (stmt
->type
) {
1061 case STMT_DECLARATION
:
1062 split_declaration(stmt
->declaration
);
1065 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1067 __split_expr(stmt
->ret_value
);
1068 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1069 __process_post_op_stack();
1072 case STMT_EXPRESSION
:
1073 expr_set_parent_stmt(stmt
->expression
, stmt
);
1074 expr_set_parent_stmt(stmt
->context
, stmt
);
1076 __split_expr(stmt
->expression
);
1079 split_compound(stmt
);
1082 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1083 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1084 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1086 if (known_condition_true(stmt
->if_conditional
)) {
1087 __split_stmt(stmt
->if_true
);
1090 if (known_condition_false(stmt
->if_conditional
)) {
1091 __split_stmt(stmt
->if_false
);
1094 __split_whole_condition(stmt
->if_conditional
);
1095 __split_stmt(stmt
->if_true
);
1096 if (empty_statement(stmt
->if_true
) &&
1097 last_stmt_on_same_line() &&
1098 !get_macro_name(stmt
->if_true
->pos
))
1099 sm_msg("warn: if();");
1100 __push_true_states();
1101 __use_false_states();
1102 __split_stmt(stmt
->if_false
);
1103 __merge_true_states();
1106 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1107 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1108 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1109 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1110 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1112 if (stmt
->iterator_pre_condition
)
1113 handle_pre_loop(stmt
);
1114 else if (stmt
->iterator_post_condition
)
1115 handle_post_loop(stmt
);
1117 // these are for(;;) type loops.
1118 handle_pre_loop(stmt
);
1122 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1123 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1125 if (get_value(stmt
->switch_expression
, &sval
)) {
1126 split_known_switch(stmt
, sval
);
1129 __split_expr(stmt
->switch_expression
);
1130 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1131 __save_switch_states(top_expression(switch_expr_stack
));
1135 __split_stmt(stmt
->switch_statement
);
1136 if (!__pop_default() && have_remaining_cases())
1137 fake_an_empty_default(stmt
->pos
);
1138 __discard_switches();
1140 pop_expression(&switch_expr_stack
);
1146 __split_label_stmt(stmt
);
1147 __split_stmt(stmt
->label_statement
);
1150 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1152 __split_expr(stmt
->goto_expression
);
1153 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1154 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1156 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1158 __process_continues();
1160 } else if (stmt
->goto_label
&&
1161 stmt
->goto_label
->type
== SYM_LABEL
&&
1162 stmt
->goto_label
->ident
) {
1163 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1166 if (is_last_stmt(stmt
))
1167 handle_backward_goto(stmt
);
1172 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1174 find_asm_gotos(stmt
);
1175 __pass_to_client(stmt
, ASM_HOOK
);
1176 __split_expr(stmt
->asm_string
);
1177 split_asm_constraints(stmt
->asm_outputs
);
1178 split_asm_constraints(stmt
->asm_inputs
);
1179 split_asm_constraints(stmt
->asm_clobbers
);
1184 __split_expr(stmt
->range_expression
);
1185 __split_expr(stmt
->range_low
);
1186 __split_expr(stmt
->range_high
);
1189 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1191 __process_post_op_stack();
1194 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1196 struct expression
*expr
;
1198 FOR_EACH_PTR(expr_list
, expr
) {
1199 expr_set_parent_expr(expr
, parent
);
1201 __process_post_op_stack();
1202 } END_FOR_EACH_PTR(expr
);
1205 static void split_sym(struct symbol
*sym
)
1209 if (!(sym
->namespace & NS_SYMBOL
))
1212 __split_stmt(sym
->stmt
);
1213 __split_expr(sym
->array_size
);
1214 split_symlist(sym
->arguments
);
1215 split_symlist(sym
->symbol_list
);
1216 __split_stmt(sym
->inline_stmt
);
1217 split_symlist(sym
->inline_symbol_list
);
1220 static void split_symlist(struct symbol_list
*sym_list
)
1224 FOR_EACH_PTR(sym_list
, sym
) {
1226 } END_FOR_EACH_PTR(sym
);
1229 typedef void (fake_cb
)(struct expression
*expr
);
1231 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1233 struct symbol
*type
, *tmp
;
1239 name
= member
->name
;
1241 type
= get_type(expr
);
1242 if (!type
|| type
->type
!= SYM_STRUCT
)
1246 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1250 if (strcmp(name
, tmp
->ident
->name
) == 0)
1252 } END_FOR_EACH_PTR(tmp
);
1256 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1258 struct symbol
*type
, *member
;
1261 type
= get_type(expr
);
1262 if (!type
|| type
->type
!= SYM_STRUCT
)
1265 FOR_EACH_PTR(type
->symbol_list
, member
) {
1267 return member
->ident
;
1269 } END_FOR_EACH_PTR(member
);
1273 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1275 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1277 struct expression
*edge_member
, *assign
;
1278 struct symbol
*base
= get_real_base_type(member
);
1282 expr
= member_expression(expr
, '.', member
->ident
);
1284 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1285 struct symbol
*type
;
1287 type
= get_real_base_type(tmp
);
1291 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1292 if (get_extra_state(edge_member
))
1295 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1296 set_inner_struct_members(expr
, tmp
);
1303 assign
= assign_expression(edge_member
, '=', zero_expr());
1304 __split_expr(assign
);
1305 } END_FOR_EACH_PTR(tmp
);
1310 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1313 struct expression
*member
= NULL
;
1314 struct expression
*assign
;
1317 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1318 expr
= strip_expr(expr
->unop
);
1322 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1323 type
= get_real_base_type(tmp
);
1328 member
= member_expression(expr
, op
, tmp
->ident
);
1329 if (get_extra_state(member
))
1333 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1334 set_inner_struct_members(expr
, tmp
);
1337 if (type
->type
== SYM_ARRAY
)
1342 assign
= assign_expression(member
, '=', zero_expr());
1343 __split_expr(assign
);
1344 } END_FOR_EACH_PTR(tmp
);
1347 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1349 struct expression
*deref
, *assign
, *tmp
, *right
;
1350 struct symbol
*struct_type
, *type
;
1351 struct ident
*member
;
1354 struct_type
= get_type(symbol
);
1356 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1360 * We're parsing an initializer that could look something like this:
1361 * struct foo foo = {
1363 * .whatever.xxx = 11,
1367 * So what we have here is a list with 42, .whatever, and .zzz. We need
1368 * to break it up into left and right sides of the assignments.
1372 FOR_EACH_PTR(members
, tmp
) {
1374 if (tmp
->type
== EXPR_IDENTIFIER
) {
1375 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1376 while (tmp
->type
== EXPR_IDENTIFIER
) {
1377 member
= tmp
->expr_ident
;
1378 tmp
= tmp
->ident_expression
;
1380 deref
= member_expression(deref
, '.', member
);
1382 deref
= member_expression(symbol
, '.', member
);
1385 member
= number_to_member(symbol
, member_idx
);
1386 deref
= member_expression(symbol
, '.', member
);
1390 if (right
->type
== EXPR_INITIALIZER
) {
1391 type
= get_type(deref
);
1392 if (type
&& type
->type
== SYM_ARRAY
)
1393 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1395 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1397 assign
= assign_expression(deref
, '=', right
);
1400 } END_FOR_EACH_PTR(tmp
);
1402 set_unset_to_zero(struct_type
, symbol
);
1405 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1407 fake_member_assigns_helper(symbol_expression(sym
),
1408 sym
->initializer
->expr_list
, fake_cb
);
1411 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1413 struct expression
*offset
, *binop
, *assign
, *tmp
;
1414 struct symbol
*type
;
1417 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1421 FOR_EACH_PTR(expr_list
, tmp
) {
1422 if (tmp
->type
== EXPR_INDEX
) {
1423 if (tmp
->idx_from
!= tmp
->idx_to
)
1425 idx
= tmp
->idx_from
;
1426 if (!tmp
->idx_expression
)
1428 tmp
= tmp
->idx_expression
;
1430 offset
= value_expr(idx
);
1431 binop
= array_element_expression(array
, offset
);
1432 if (tmp
->type
== EXPR_INITIALIZER
) {
1433 type
= get_type(binop
);
1434 if (type
&& type
->type
== SYM_ARRAY
)
1435 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1437 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1439 assign
= assign_expression(binop
, '=', tmp
);
1444 } END_FOR_EACH_PTR(tmp
);
1447 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1449 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1452 static void fake_assign_expr(struct symbol
*sym
)
1454 struct expression
*assign
, *symbol
;
1456 symbol
= symbol_expression(sym
);
1457 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1458 __split_expr(assign
);
1461 static void do_initializer_stuff(struct symbol
*sym
)
1463 if (!sym
->initializer
)
1466 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1467 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1468 fake_element_assigns(sym
, __split_expr
);
1470 fake_member_assigns(sym
, __split_expr
);
1472 fake_assign_expr(sym
);
1476 static void split_declaration(struct symbol_list
*sym_list
)
1480 FOR_EACH_PTR(sym_list
, sym
) {
1481 __pass_to_client(sym
, DECLARATION_HOOK
);
1482 do_initializer_stuff(sym
);
1484 } END_FOR_EACH_PTR(sym
);
1487 static void call_global_assign_hooks(struct expression
*assign
)
1489 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1492 static void fake_global_assign(struct symbol
*sym
)
1494 struct expression
*assign
, *symbol
;
1496 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1497 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1498 fake_element_assigns(sym
, call_global_assign_hooks
);
1499 } else if (sym
->initializer
) {
1500 symbol
= symbol_expression(sym
);
1501 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1502 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1504 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1506 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1507 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1508 fake_member_assigns(sym
, call_global_assign_hooks
);
1509 } else if (sym
->initializer
) {
1510 symbol
= symbol_expression(sym
);
1511 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1512 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1514 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1517 symbol
= symbol_expression(sym
);
1518 if (sym
->initializer
)
1519 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1521 assign
= assign_expression(symbol
, '=', zero_expr());
1522 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1526 static void start_function_definition(struct symbol
*sym
)
1528 __in_function_def
= 1;
1529 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1530 __in_function_def
= 0;
1531 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1535 static void split_function(struct symbol
*sym
)
1537 struct symbol
*base_type
= get_base_type(sym
);
1538 struct timeval stop
;
1540 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1543 gettimeofday(&fn_start_time
, NULL
);
1546 cur_func
= sym
->ident
->name
;
1547 set_position(sym
->pos
);
1549 last_goto_statement_handled
= 0;
1550 sm_debug("new function: %s\n", cur_func
);
1552 if (option_two_passes
) {
1556 start_function_definition(sym
);
1557 __split_stmt(base_type
->stmt
);
1558 __split_stmt(base_type
->inline_stmt
);
1564 start_function_definition(sym
);
1565 __split_stmt(base_type
->stmt
);
1566 __split_stmt(base_type
->inline_stmt
);
1567 __pass_to_client(sym
, END_FUNC_HOOK
);
1568 if (need_delayed_scope_hooks())
1569 __call_scope_hooks();
1570 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1574 gettimeofday(&stop
, NULL
);
1575 if (option_time
&& stop
.tv_sec
- fn_start_time
.tv_sec
> 2) {
1577 sm_msg("func_time: %lu", stop
.tv_sec
- fn_start_time
.tv_sec
);
1580 cur_func_sym
= NULL
;
1582 free_data_info_allocs();
1583 free_expression_stack(&switch_expr_stack
);
1584 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1585 __bail_on_rest_of_function
= 0;
1588 static void save_flow_state(void)
1590 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1591 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1592 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1594 __add_ptr_list(&backup
, big_statement_stack
, 0);
1595 __add_ptr_list(&backup
, big_expression_stack
, 0);
1596 __add_ptr_list(&backup
, big_condition_stack
, 0);
1597 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1599 __add_ptr_list(&backup
, cur_func_sym
, 0);
1601 __add_ptr_list(&backup
, __prev_stmt
, 0);
1602 __add_ptr_list(&backup
, __cur_stmt
, 0);
1603 __add_ptr_list(&backup
, __next_stmt
, 0);
1607 static void *pop_backup(void)
1611 ret
= last_ptr_list(backup
);
1612 delete_ptr_list_last(&backup
);
1616 static void restore_flow_state(void)
1618 __next_stmt
= pop_backup();
1619 __cur_stmt
= pop_backup();
1620 __prev_stmt
= pop_backup();
1622 cur_func_sym
= pop_backup();
1623 switch_expr_stack
= pop_backup();
1624 big_condition_stack
= pop_backup();
1625 big_expression_stack
= pop_backup();
1626 big_statement_stack
= pop_backup();
1627 final_pass
= PTR_INT(pop_backup()) >> 2;
1628 loop_count
= PTR_INT(pop_backup()) >> 2;
1629 loop_num
= PTR_INT(pop_backup()) >> 2;
1632 static void parse_inline(struct expression
*call
)
1634 struct symbol
*base_type
;
1635 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1636 struct timeval time_backup
= fn_start_time
;
1637 struct expression
*orig_inline
= __inline_fn
;
1642 __pass_to_client(call
, INLINE_FN_START
);
1643 final_pass
= 0; /* don't print anything */
1645 orig_budget
= inline_budget
;
1646 inline_budget
= inline_budget
- 5;
1648 base_type
= get_base_type(call
->fn
->symbol
);
1649 cur_func_sym
= call
->fn
->symbol
;
1650 if (call
->fn
->symbol
->ident
)
1651 cur_func
= call
->fn
->symbol
->ident
->name
;
1654 set_position(call
->fn
->symbol
->pos
);
1657 big_statement_stack
= NULL
;
1658 big_expression_stack
= NULL
;
1659 big_condition_stack
= NULL
;
1660 switch_expr_stack
= NULL
;
1662 sm_debug("inline function: %s\n", cur_func
);
1666 start_function_definition(call
->fn
->symbol
);
1667 __split_stmt(base_type
->stmt
);
1668 __split_stmt(base_type
->inline_stmt
);
1669 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1670 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1672 free_expression_stack(&switch_expr_stack
);
1673 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1677 restore_flow_state();
1678 fn_start_time
= time_backup
;
1679 cur_func
= cur_func_bak
;
1681 restore_all_states();
1682 set_position(call
->pos
);
1683 __inline_fn
= orig_inline
;
1684 inline_budget
= orig_budget
;
1685 __pass_to_client(call
, INLINE_FN_END
);
1688 static struct symbol_list
*inlines_called
;
1689 static void add_inline_function(struct symbol
*sym
)
1691 static struct symbol_list
*already_added
;
1694 FOR_EACH_PTR(already_added
, tmp
) {
1697 } END_FOR_EACH_PTR(tmp
);
1699 add_ptr_list(&already_added
, sym
);
1700 add_ptr_list(&inlines_called
, sym
);
1703 static void process_inlines(void)
1707 FOR_EACH_PTR(inlines_called
, tmp
) {
1708 split_function(tmp
);
1709 } END_FOR_EACH_PTR(tmp
);
1710 free_ptr_list(&inlines_called
);
1713 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1717 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1720 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1722 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1724 } END_FOR_EACH_PTR_REVERSE(sym
);
1729 static bool interesting_function(struct symbol
*sym
)
1731 static int prev_stream
= -1;
1732 static bool prev_answer
;
1733 const char *filename
;
1736 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1739 if (sym
->pos
.stream
== prev_stream
)
1742 prev_stream
= sym
->pos
.stream
;
1743 prev_answer
= false;
1745 filename
= stream_name(sym
->pos
.stream
);
1746 len
= strlen(filename
);
1747 if (len
> 0 && filename
[len
- 1] == 'c')
1752 static void split_inlines_in_scope(struct symbol
*sym
)
1754 struct symbol
*base
;
1755 struct symbol_list
*scope_list
;
1758 scope_list
= sym
->scope
->symbols
;
1759 stream
= sym
->pos
.stream
;
1761 /* find the last static symbol in the file */
1762 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1763 if (sym
->pos
.stream
!= stream
)
1765 if (sym
->type
!= SYM_NODE
)
1767 base
= get_base_type(sym
);
1770 if (base
->type
!= SYM_FN
)
1772 if (!base
->inline_stmt
)
1774 if (!interesting_function(sym
))
1776 add_inline_function(sym
);
1777 } END_FOR_EACH_PTR_REVERSE(sym
);
1782 static void split_inlines(struct symbol_list
*sym_list
)
1786 sym
= get_last_scoped_symbol(sym_list
, 0);
1788 split_inlines_in_scope(sym
);
1789 sym
= get_last_scoped_symbol(sym_list
, 1);
1791 split_inlines_in_scope(sym
);
1794 static struct stree
*clone_estates_perm(struct stree
*orig
)
1796 struct stree
*ret
= NULL
;
1797 struct sm_state
*tmp
;
1799 FOR_EACH_SM(orig
, tmp
) {
1800 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1801 } END_FOR_EACH_SM(tmp
);
1806 struct position last_pos
;
1807 static void split_c_file_functions(struct symbol_list
*sym_list
)
1812 FOR_EACH_PTR(sym_list
, sym
) {
1813 set_position(sym
->pos
);
1814 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1815 __pass_to_client(sym
, BASE_HOOK
);
1816 fake_global_assign(sym
);
1818 } END_FOR_EACH_PTR(sym
);
1819 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1822 FOR_EACH_PTR(sym_list
, sym
) {
1823 set_position(sym
->pos
);
1824 last_pos
= sym
->pos
;
1825 if (!interesting_function(sym
))
1827 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1828 split_function(sym
);
1831 last_pos
= sym
->pos
;
1832 } END_FOR_EACH_PTR(sym
);
1833 split_inlines(sym_list
);
1834 __pass_to_client(sym_list
, END_FILE_HOOK
);
1837 static int final_before_fake
;
1838 void init_fake_env(void)
1841 final_before_fake
= final_pass
;
1843 __push_fake_cur_stree();
1847 void end_fake_env(void)
1849 __pop_fake_cur_stree();
1852 final_pass
= final_before_fake
;
1855 static void open_output_files(char *base_file
)
1859 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1860 sm_outfd
= fopen(buf
, "w");
1862 printf("Error: Cannot open %s\n", buf
);
1869 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1870 sql_outfd
= fopen(buf
, "w");
1872 printf("Error: Cannot open %s\n", buf
);
1876 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1877 caller_info_fd
= fopen(buf
, "w");
1878 if (!caller_info_fd
) {
1879 printf("Error: Cannot open %s\n", buf
);
1884 void smatch(int argc
, char **argv
)
1886 struct string_list
*filelist
= NULL
;
1887 struct symbol_list
*sym_list
;
1888 struct timeval stop
, start
;
1892 gettimeofday(&start
, NULL
);
1895 printf("Usage: smatch [--debug] <filename.c>\n");
1898 sparse_initialize(argc
, argv
, &filelist
);
1899 set_valid_ptr_max();
1900 alloc_valid_ptr_rl();
1901 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1902 path
= getcwd(NULL
, 0);
1903 free(full_base_file
);
1905 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
1906 full_base_file
= malloc(len
);
1907 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
1909 full_base_file
= alloc_string(base_file
);
1911 if (option_file_output
)
1912 open_output_files(base_file
);
1913 sym_list
= sparse_keep_tokens(base_file
);
1914 split_c_file_functions(sym_list
);
1915 } END_FOR_EACH_PTR_NOTAG(base_file
);
1917 gettimeofday(&stop
, NULL
);
1919 set_position(last_pos
);
1921 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1923 sm_msg("mem: %luKb", get_max_memory());