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 static int in_fake_env
;
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 *cur_func
;
42 static unsigned int loop_count
;
43 static int last_goto_statement_handled
;
44 int __expr_stmt_count
;
45 int __in_function_def
;
46 static struct expression_list
*switch_expr_stack
= NULL
;
47 static struct expression_list
*post_op_stack
= NULL
;
49 static struct ptr_list
*backup
;
51 struct expression_list
*big_expression_stack
;
52 struct statement_list
*big_statement_stack
;
53 struct statement
*__prev_stmt
;
54 struct statement
*__cur_stmt
;
55 struct statement
*__next_stmt
;
56 int __in_pre_condition
= 0;
57 int __bail_on_rest_of_function
= 0;
58 static struct timeval fn_start_time
;
59 char *get_function(void) { return cur_func
; }
60 int get_lineno(void) { return __smatch_lineno
; }
61 int inside_loop(void) { return !!loop_count
; }
62 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
63 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
64 int in_expression_statement(void) { return !!__expr_stmt_count
; }
66 static void split_symlist(struct symbol_list
*sym_list
);
67 static void split_declaration(struct symbol_list
*sym_list
);
68 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
69 static void add_inline_function(struct symbol
*sym
);
70 static void parse_inline(struct expression
*expr
);
72 int option_assume_loops
= 0;
73 int option_two_passes
= 0;
74 struct symbol
*cur_func_sym
= NULL
;
75 struct stree
*global_states
;
77 long long valid_ptr_min
= 4096;
78 long long valid_ptr_max
= 2117777777;
79 sval_t valid_ptr_min_sval
= {
83 sval_t valid_ptr_max_sval
= {
85 {.value
= LONG_MAX
- 100000},
88 static void set_valid_ptr_max(void)
90 if (type_bits(&ptr_ctype
) == 32)
91 valid_ptr_max
= 2117777777;
92 else if (type_bits(&ptr_ctype
) == 64)
93 valid_ptr_max
= 2117777777777777777LL;
95 valid_ptr_max_sval
.value
= valid_ptr_max
;
98 int outside_of_function(void)
100 return cur_func_sym
== NULL
;
103 const char *get_filename(void)
107 if (option_full_path
)
108 return full_filename
;
112 const char *get_base_file(void)
117 static void set_position(struct position pos
)
120 static int prev_stream
= -1;
125 if (pos
.stream
== 0 && pos
.line
== 0)
128 __smatch_lineno
= pos
.line
;
130 if (pos
.stream
== prev_stream
)
133 filename
= stream_name(pos
.stream
);
136 pathname
= getcwd(NULL
, 0);
138 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
139 full_filename
= malloc(len
);
140 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
142 full_filename
= alloc_string(filename
);
147 int is_assigned_call(struct expression
*expr
)
149 struct expression
*parent
= expr_get_parent_expr(expr
);
152 parent
->type
== EXPR_ASSIGNMENT
&&
154 strip_expr(parent
->right
) == expr
)
160 static int is_inline_func(struct expression
*expr
)
162 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
164 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
169 static int is_noreturn_func(struct expression
*expr
)
171 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
173 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
178 int inlinable(struct expression
*expr
)
181 struct statement
*last_stmt
= NULL
;
183 if (__inline_fn
) /* don't nest */
186 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
188 if (is_no_inline_function(expr
->symbol
->ident
->name
))
190 sym
= get_base_type(expr
->symbol
);
191 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
192 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
194 if (sym
->stmt
->type
!= STMT_COMPOUND
)
196 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
198 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
199 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
201 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
203 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
209 /* the magic numbers in this function are pulled out of my bum. */
210 if (last_stmt
->pos
.line
> sym
->pos
.line
+ 20)
216 void __process_post_op_stack(void)
218 struct expression
*expr
;
220 FOR_EACH_PTR(post_op_stack
, expr
) {
221 __pass_to_client(expr
, OP_HOOK
);
222 } END_FOR_EACH_PTR(expr
);
224 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
227 static int handle_comma_assigns(struct expression
*expr
)
229 struct expression
*right
;
230 struct expression
*assign
;
232 right
= strip_expr(expr
->right
);
233 if (right
->type
!= EXPR_COMMA
)
236 __split_expr(right
->left
);
237 __process_post_op_stack();
239 assign
= assign_expression(expr
->left
, '=', right
->right
);
240 __split_expr(assign
);
245 /* This is to handle *p++ = foo; assignments */
246 static int handle_postop_assigns(struct expression
*expr
)
248 struct expression
*left
, *fake_left
;
249 struct expression
*assign
;
251 left
= strip_expr(expr
->left
);
252 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
254 left
= strip_expr(left
->unop
);
255 if (left
->type
!= EXPR_POSTOP
)
258 fake_left
= deref_expression(strip_expr(left
->unop
));
259 assign
= assign_expression(fake_left
, '=', expr
->right
);
261 __split_expr(assign
);
262 __split_expr(expr
->left
);
267 static int prev_expression_is_getting_address(struct expression
*expr
)
269 struct expression
*parent
;
272 parent
= expr_get_parent_expr(expr
);
276 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
278 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
280 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
289 static int handle__builtin_choose_expr(struct expression
*expr
)
291 struct expression
*const_expr
, *expr1
, *expr2
;
294 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
297 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
298 expr1
= get_argument_from_call_expr(expr
->args
, 1);
299 expr2
= get_argument_from_call_expr(expr
->args
, 2);
301 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
310 void __split_expr(struct expression
*expr
)
315 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
317 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
319 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
322 push_expression(&big_expression_stack
, expr
);
323 set_position(expr
->pos
);
324 __pass_to_client(expr
, EXPR_HOOK
);
326 switch (expr
->type
) {
328 expr_set_parent_expr(expr
->unop
, expr
);
330 if (expr
->op
== '*' &&
331 !prev_expression_is_getting_address(expr
))
332 __pass_to_client(expr
, DEREF_HOOK
);
333 __split_expr(expr
->unop
);
334 __pass_to_client(expr
, OP_HOOK
);
337 expr_set_parent_expr(expr
->unop
, expr
);
339 __split_expr(expr
->unop
);
340 push_expression(&post_op_stack
, expr
);
344 if (expr
->statement
&& !expr
->statement
) {
345 stmt_set_parent_stmt(expr
->statement
,
346 last_ptr_list((struct ptr_list
*)big_statement_stack
));
348 __split_stmt(expr
->statement
);
353 expr_set_parent_expr(expr
->left
, expr
);
354 expr_set_parent_expr(expr
->right
, expr
);
356 __pass_to_client(expr
, LOGIC_HOOK
);
357 __handle_logic(expr
);
360 expr_set_parent_expr(expr
->left
, expr
);
361 expr_set_parent_expr(expr
->right
, expr
);
363 __pass_to_client(expr
, BINOP_HOOK
);
365 expr_set_parent_expr(expr
->left
, expr
);
366 expr_set_parent_expr(expr
->right
, expr
);
368 __split_expr(expr
->left
);
369 __process_post_op_stack();
370 __split_expr(expr
->right
);
372 case EXPR_ASSIGNMENT
: {
373 struct expression
*right
;
375 expr_set_parent_expr(expr
->left
, expr
);
376 expr_set_parent_expr(expr
->right
, expr
);
378 right
= strip_expr(expr
->right
);
382 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
385 if (__handle_condition_assigns(expr
))
387 /* foo = (x < 5 ? foo : 5); */
388 if (__handle_select_assigns(expr
))
390 /* foo = ({frob(); frob(); frob(); 1;}) */
391 if (__handle_expr_statement_assigns(expr
))
394 if (handle_comma_assigns(expr
))
396 if (handle_postop_assigns(expr
))
399 __split_expr(expr
->right
);
400 if (outside_of_function())
401 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
403 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
405 __fake_struct_member_assignments(expr
);
407 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
408 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
410 if (get_macro_name(right
->pos
) &&
411 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
412 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
414 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
416 __split_expr(expr
->left
);
420 expr_set_parent_expr(expr
->deref
, expr
);
422 __pass_to_client(expr
, DEREF_HOOK
);
423 __split_expr(expr
->deref
);
426 expr_set_parent_expr(expr
->base
, expr
);
428 __split_expr(expr
->base
);
431 case EXPR_FORCE_CAST
:
432 expr_set_parent_expr(expr
->cast_expression
, expr
);
434 __pass_to_client(expr
, CAST_HOOK
);
435 __split_expr(expr
->cast_expression
);
438 if (expr
->cast_expression
)
439 __pass_to_client(strip_parens(expr
->cast_expression
),
444 evaluate_expression(expr
);
446 case EXPR_CONDITIONAL
:
448 expr_set_parent_expr(expr
->conditional
, expr
);
449 expr_set_parent_expr(expr
->cond_true
, expr
);
450 expr_set_parent_expr(expr
->cond_false
, expr
);
452 if (known_condition_true(expr
->conditional
)) {
453 __split_expr(expr
->cond_true
);
456 if (known_condition_false(expr
->conditional
)) {
457 __split_expr(expr
->cond_false
);
460 __pass_to_client(expr
, SELECT_HOOK
);
461 __split_whole_condition(expr
->conditional
);
462 __split_expr(expr
->cond_true
);
463 __push_true_states();
464 __use_false_states();
465 __split_expr(expr
->cond_false
);
466 __merge_true_states();
469 expr_set_parent_expr(expr
->fn
, expr
);
471 if (sym_name_is("__builtin_constant_p", expr
->fn
))
473 if (handle__builtin_choose_expr(expr
))
475 split_expr_list(expr
->args
, expr
);
476 __split_expr(expr
->fn
);
477 if (is_inline_func(expr
->fn
))
478 add_inline_function(expr
->fn
->symbol
);
479 if (inlinable(expr
->fn
))
481 __process_post_op_stack();
482 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
483 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
485 if (inlinable(expr
->fn
)) {
488 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
489 if (is_noreturn_func(expr
->fn
))
492 case EXPR_INITIALIZER
:
493 split_expr_list(expr
->expr_list
, expr
);
495 case EXPR_IDENTIFIER
:
496 expr_set_parent_expr(expr
->ident_expression
, expr
);
497 __split_expr(expr
->ident_expression
);
500 expr_set_parent_expr(expr
->idx_expression
, expr
);
501 __split_expr(expr
->idx_expression
);
504 expr_set_parent_expr(expr
->init_expr
, expr
);
505 __split_expr(expr
->init_expr
);
508 __pass_to_client(expr
, SYM_HOOK
);
511 __pass_to_client(expr
, STRING_HOOK
);
516 pop_expression(&big_expression_stack
);
519 static int is_forever_loop(struct statement
*stmt
)
521 struct expression
*expr
;
524 expr
= strip_expr(stmt
->iterator_pre_condition
);
526 expr
= stmt
->iterator_post_condition
;
528 /* this is a for(;;) loop... */
532 if (get_value(expr
, &sval
) && sval
.value
!= 0)
539 static char *get_loop_name(int num
)
543 snprintf(buf
, 255, "-loop%d", num
);
545 return alloc_sname(buf
);
549 * Pre Loops are while and for loops.
551 static void handle_pre_loop(struct statement
*stmt
)
553 int once_through
; /* we go through the loop at least once */
554 struct sm_state
*extra_sm
= NULL
;
557 struct stree
*stree
= NULL
;
558 struct sm_state
*sm
= NULL
;
560 loop_name
= get_loop_name(loop_num
);
563 __split_stmt(stmt
->iterator_pre_statement
);
564 __prev_stmt
= stmt
->iterator_pre_statement
;
566 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
572 __merge_gotos(loop_name
, NULL
);
574 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
575 __in_pre_condition
++;
576 __pass_to_client(stmt
, PRELOOP_HOOK
);
577 __split_whole_condition(stmt
->iterator_pre_condition
);
578 __in_pre_condition
--;
579 FOR_EACH_SM(stree
, sm
) {
580 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
581 } END_FOR_EACH_SM(sm
);
584 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
586 if (option_assume_loops
)
589 __split_stmt(stmt
->iterator_statement
);
590 if (is_forever_loop(stmt
)) {
592 __save_gotos(loop_name
, NULL
);
594 __push_fake_cur_stree();
595 __split_stmt(stmt
->iterator_post_statement
);
596 stree
= __pop_fake_cur_stree();
598 __discard_false_states();
601 if (!__path_is_null())
602 __merge_stree_into_cur(stree
);
606 unchanged
= __iterator_unchanged(extra_sm
);
607 __split_stmt(stmt
->iterator_post_statement
);
608 __prev_stmt
= stmt
->iterator_post_statement
;
611 __save_gotos(loop_name
, NULL
);
612 __in_pre_condition
++;
613 __split_whole_condition(stmt
->iterator_pre_condition
);
614 __in_pre_condition
--;
616 __merge_false_states();
618 __discard_false_states();
620 __merge_false_states();
622 if (extra_sm
&& unchanged
)
623 __extra_pre_loop_hook_after(extra_sm
,
624 stmt
->iterator_post_statement
,
625 stmt
->iterator_pre_condition
);
632 * Post loops are do {} while();
634 static void handle_post_loop(struct statement
*stmt
)
638 loop_name
= get_loop_name(loop_num
);
644 __merge_gotos(loop_name
, NULL
);
645 __split_stmt(stmt
->iterator_statement
);
647 if (!is_zero(stmt
->iterator_post_condition
))
648 __save_gotos(loop_name
, NULL
);
650 if (is_forever_loop(stmt
)) {
653 __split_whole_condition(stmt
->iterator_post_condition
);
654 __use_false_states();
660 static int empty_statement(struct statement
*stmt
)
664 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
669 static int last_stmt_on_same_line(void)
671 struct statement
*stmt
;
674 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
677 if (stmt
->pos
.line
== get_lineno())
680 } END_FOR_EACH_PTR_REVERSE(stmt
);
684 static void split_asm_constraints(struct expression_list
*expr_list
)
686 struct expression
*expr
;
689 FOR_EACH_PTR(expr_list
, expr
) {
691 case 0: /* identifier */
692 case 1: /* constraint */
695 case 2: /* expression */
700 } END_FOR_EACH_PTR(expr
);
703 static int is_case_val(struct statement
*stmt
, sval_t sval
)
707 if (stmt
->type
!= STMT_CASE
)
709 if (!stmt
->case_expression
) {
713 if (!get_value(stmt
->case_expression
, &case_sval
))
715 if (case_sval
.value
== sval
.value
)
720 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
721 struct expression
*case_expr
,
722 struct expression
*case_to
)
725 struct range_list
*rl
= NULL
;
726 struct symbol
*switch_type
;
728 switch_type
= get_type(switch_expr
);
729 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
730 start
= sval_cast(switch_type
, start
);
731 end
= sval_cast(switch_type
, end
);
732 add_range(&rl
, start
, end
);
733 } else if (get_value(case_expr
, &start
)) {
734 start
= sval_cast(switch_type
, start
);
735 add_range(&rl
, start
, start
);
741 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
743 struct statement
*tmp
;
744 struct range_list
*rl
;
746 __split_expr(stmt
->switch_expression
);
747 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
749 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
750 __save_switch_states(top_expression(switch_expr_stack
));
755 stmt
= stmt
->switch_statement
;
757 __push_scope_hooks();
758 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
759 __smatch_lineno
= tmp
->pos
.line
;
760 if (is_case_val(tmp
, sval
)) {
761 rl
= alloc_rl(sval
, sval
);
762 __merge_switches(top_expression(switch_expr_stack
), rl
);
763 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
765 if (__path_is_null())
768 if (__path_is_null()) {
772 } END_FOR_EACH_PTR(tmp
);
774 __call_scope_hooks();
775 if (!__pop_default())
776 __merge_switches(top_expression(switch_expr_stack
), NULL
);
777 __discard_switches();
779 pop_expression(&switch_expr_stack
);
782 static void split_case(struct statement
*stmt
)
784 struct range_list
*rl
= NULL
;
786 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
787 expr_set_parent_stmt(stmt
->case_to
, stmt
);
789 rl
= get_case_rl(top_expression(switch_expr_stack
),
790 stmt
->case_expression
, stmt
->case_to
);
791 while (stmt
->case_statement
->type
== STMT_CASE
) {
792 struct range_list
*tmp
;
794 tmp
= get_case_rl(top_expression(switch_expr_stack
),
795 stmt
->case_statement
->case_expression
,
796 stmt
->case_statement
->case_to
);
799 rl
= rl_union(rl
, tmp
);
800 if (!stmt
->case_expression
)
802 stmt
= stmt
->case_statement
;
805 __merge_switches(top_expression(switch_expr_stack
), rl
);
807 if (!stmt
->case_expression
)
809 __split_stmt(stmt
->case_statement
);
812 int time_parsing_function(void)
814 return ms_since(&fn_start_time
) / 1000;
817 static int taking_too_long(void)
819 if (time_parsing_function() > 60 * 5) /* five minutes */
824 static int is_last_stmt(struct statement
*cur_stmt
)
826 struct symbol
*fn
= get_base_type(cur_func_sym
);
827 struct statement
*stmt
;
833 stmt
= fn
->inline_stmt
;
834 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
836 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
837 if (stmt
&& stmt
->type
== STMT_LABEL
)
838 stmt
= stmt
->label_statement
;
839 if (stmt
== cur_stmt
)
844 static void handle_backward_goto(struct statement
*goto_stmt
)
846 const char *goto_name
, *label_name
;
847 struct statement
*func_stmt
;
848 struct symbol
*base_type
= get_base_type(cur_func_sym
);
849 struct statement
*tmp
;
854 if (last_goto_statement_handled
)
856 last_goto_statement_handled
= 1;
858 if (!goto_stmt
->goto_label
||
859 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
860 !goto_stmt
->goto_label
->ident
)
862 goto_name
= goto_stmt
->goto_label
->ident
->name
;
864 func_stmt
= base_type
->stmt
;
866 func_stmt
= base_type
->inline_stmt
;
869 if (func_stmt
->type
!= STMT_COMPOUND
)
872 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
874 if (tmp
->type
!= STMT_LABEL
)
876 if (!tmp
->label_identifier
||
877 tmp
->label_identifier
->type
!= SYM_LABEL
||
878 !tmp
->label_identifier
->ident
)
880 label_name
= tmp
->label_identifier
->ident
->name
;
881 if (strcmp(goto_name
, label_name
) != 0)
886 } END_FOR_EACH_PTR(tmp
);
889 static void fake_a_return(void)
891 struct symbol
*return_type
;
896 return_type
= get_real_base_type(cur_func_sym
);
897 return_type
= get_real_base_type(return_type
);
898 if (return_type
!= &void_ctype
) {
899 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
904 static void fake_an_empty_default(struct position pos
)
906 static struct statement none
= {};
909 none
.type
= STMT_NONE
;
910 __merge_switches(top_expression(switch_expr_stack
), NULL
);
914 static void split_compound(struct statement
*stmt
)
916 struct statement
*prev
= NULL
;
917 struct statement
*cur
= NULL
;
918 struct statement
*next
;
920 __push_scope_hooks();
922 FOR_EACH_PTR(stmt
->stmts
, next
) {
923 /* just set them all ahead of time */
924 stmt_set_parent_stmt(next
, stmt
);
934 } END_FOR_EACH_PTR(next
);
943 * For function scope, then delay calling the scope hooks until the
944 * end of function hooks can run. I'm not positive this is the right
947 if (!is_last_stmt(cur
))
948 __call_scope_hooks();
952 * This is a hack, work around for detecting empty functions.
954 static int need_delayed_scope_hooks(void)
956 struct symbol
*fn
= get_base_type(cur_func_sym
);
957 struct statement
*stmt
;
963 stmt
= fn
->inline_stmt
;
964 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
969 void __split_label_stmt(struct statement
*stmt
)
971 if (stmt
->label_identifier
&&
972 stmt
->label_identifier
->type
== SYM_LABEL
&&
973 stmt
->label_identifier
->ident
) {
974 loop_count
|= 0x0800000;
975 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
979 static void find_asm_gotos(struct statement
*stmt
)
983 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
984 __save_gotos(sym
->ident
->name
, sym
);
985 } END_FOR_EACH_PTR(sym
);
988 void __split_stmt(struct statement
*stmt
)
995 if (!__in_fake_assign
)
996 __silence_warnings_for_stmt
= false;
998 if (__bail_on_rest_of_function
)
1001 if (out_of_memory() || taking_too_long()) {
1002 struct timeval stop
;
1004 gettimeofday(&stop
, NULL
);
1006 __bail_on_rest_of_function
= 1;
1008 sm_msg("Function too hairy. Giving up. %lu seconds",
1009 stop
.tv_sec
- fn_start_time
.tv_sec
);
1011 final_pass
= 0; /* turn off sm_msg() from here */
1015 add_ptr_list(&big_statement_stack
, stmt
);
1016 free_expression_stack(&big_expression_stack
);
1017 set_position(stmt
->pos
);
1018 __pass_to_client(stmt
, STMT_HOOK
);
1020 switch (stmt
->type
) {
1021 case STMT_DECLARATION
:
1022 split_declaration(stmt
->declaration
);
1025 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1027 __split_expr(stmt
->ret_value
);
1028 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1029 __process_post_op_stack();
1032 case STMT_EXPRESSION
:
1033 expr_set_parent_stmt(stmt
->expression
, stmt
);
1034 expr_set_parent_stmt(stmt
->context
, stmt
);
1036 __split_expr(stmt
->expression
);
1039 split_compound(stmt
);
1042 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1043 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1044 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1046 if (known_condition_true(stmt
->if_conditional
)) {
1047 __split_stmt(stmt
->if_true
);
1050 if (known_condition_false(stmt
->if_conditional
)) {
1051 __split_stmt(stmt
->if_false
);
1054 __split_whole_condition(stmt
->if_conditional
);
1055 __split_stmt(stmt
->if_true
);
1056 if (empty_statement(stmt
->if_true
) &&
1057 last_stmt_on_same_line() &&
1058 !get_macro_name(stmt
->if_true
->pos
))
1059 sm_msg("warn: if();");
1060 __push_true_states();
1061 __use_false_states();
1062 __split_stmt(stmt
->if_false
);
1063 __merge_true_states();
1066 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1067 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1068 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1069 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1070 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1072 if (stmt
->iterator_pre_condition
)
1073 handle_pre_loop(stmt
);
1074 else if (stmt
->iterator_post_condition
)
1075 handle_post_loop(stmt
);
1077 // these are for(;;) type loops.
1078 handle_pre_loop(stmt
);
1082 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1083 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1085 if (get_value(stmt
->switch_expression
, &sval
)) {
1086 split_known_switch(stmt
, sval
);
1089 __split_expr(stmt
->switch_expression
);
1090 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1091 __save_switch_states(top_expression(switch_expr_stack
));
1095 __split_stmt(stmt
->switch_statement
);
1096 if (!__pop_default() && have_remaining_cases())
1097 fake_an_empty_default(stmt
->pos
);
1098 __discard_switches();
1100 pop_expression(&switch_expr_stack
);
1106 __split_label_stmt(stmt
);
1107 __split_stmt(stmt
->label_statement
);
1110 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1112 __split_expr(stmt
->goto_expression
);
1113 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1114 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1116 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1118 __process_continues();
1120 } else if (stmt
->goto_label
&&
1121 stmt
->goto_label
->type
== SYM_LABEL
&&
1122 stmt
->goto_label
->ident
) {
1123 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1126 if (is_last_stmt(stmt
))
1127 handle_backward_goto(stmt
);
1132 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1134 find_asm_gotos(stmt
);
1135 __pass_to_client(stmt
, ASM_HOOK
);
1136 __split_expr(stmt
->asm_string
);
1137 split_asm_constraints(stmt
->asm_outputs
);
1138 split_asm_constraints(stmt
->asm_inputs
);
1139 split_asm_constraints(stmt
->asm_clobbers
);
1144 __split_expr(stmt
->range_expression
);
1145 __split_expr(stmt
->range_low
);
1146 __split_expr(stmt
->range_high
);
1149 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1151 __process_post_op_stack();
1154 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1156 struct expression
*expr
;
1158 FOR_EACH_PTR(expr_list
, expr
) {
1159 expr_set_parent_expr(expr
, parent
);
1161 __process_post_op_stack();
1162 } END_FOR_EACH_PTR(expr
);
1165 static void split_sym(struct symbol
*sym
)
1169 if (!(sym
->namespace & NS_SYMBOL
))
1172 __split_stmt(sym
->stmt
);
1173 __split_expr(sym
->array_size
);
1174 split_symlist(sym
->arguments
);
1175 split_symlist(sym
->symbol_list
);
1176 __split_stmt(sym
->inline_stmt
);
1177 split_symlist(sym
->inline_symbol_list
);
1180 static void split_symlist(struct symbol_list
*sym_list
)
1184 FOR_EACH_PTR(sym_list
, sym
) {
1186 } END_FOR_EACH_PTR(sym
);
1189 typedef void (fake_cb
)(struct expression
*expr
);
1191 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1193 struct symbol
*type
, *tmp
;
1199 name
= member
->name
;
1201 type
= get_type(expr
);
1202 if (!type
|| type
->type
!= SYM_STRUCT
)
1206 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1210 if (strcmp(name
, tmp
->ident
->name
) == 0)
1212 } END_FOR_EACH_PTR(tmp
);
1216 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1218 struct symbol
*type
, *member
;
1221 type
= get_type(expr
);
1222 if (!type
|| type
->type
!= SYM_STRUCT
)
1225 FOR_EACH_PTR(type
->symbol_list
, member
) {
1227 return member
->ident
;
1229 } END_FOR_EACH_PTR(member
);
1233 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1235 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1237 struct expression
*edge_member
, *assign
;
1238 struct symbol
*base
= get_real_base_type(member
);
1242 expr
= member_expression(expr
, '.', member
->ident
);
1244 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1245 struct symbol
*type
;
1247 type
= get_real_base_type(tmp
);
1251 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1252 if (get_extra_state(edge_member
))
1255 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1256 set_inner_struct_members(expr
, tmp
);
1263 assign
= assign_expression(edge_member
, '=', zero_expr());
1264 __split_expr(assign
);
1265 } END_FOR_EACH_PTR(tmp
);
1270 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1273 struct expression
*member
= NULL
;
1274 struct expression
*assign
;
1277 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1278 expr
= strip_expr(expr
->unop
);
1282 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1283 type
= get_real_base_type(tmp
);
1288 member
= member_expression(expr
, op
, tmp
->ident
);
1289 if (get_extra_state(member
))
1293 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1294 set_inner_struct_members(expr
, tmp
);
1297 if (type
->type
== SYM_ARRAY
)
1302 assign
= assign_expression(member
, '=', zero_expr());
1303 __split_expr(assign
);
1304 } END_FOR_EACH_PTR(tmp
);
1307 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1309 struct expression
*deref
, *assign
, *tmp
, *right
;
1310 struct symbol
*struct_type
, *type
;
1311 struct ident
*member
;
1314 struct_type
= get_type(symbol
);
1316 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1320 * We're parsing an initializer that could look something like this:
1321 * struct foo foo = {
1323 * .whatever.xxx = 11,
1327 * So what we have here is a list with 42, .whatever, and .zzz. We need
1328 * to break it up into left and right sides of the assignments.
1332 FOR_EACH_PTR(members
, tmp
) {
1334 if (tmp
->type
== EXPR_IDENTIFIER
) {
1335 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1336 while (tmp
->type
== EXPR_IDENTIFIER
) {
1337 member
= tmp
->expr_ident
;
1338 tmp
= tmp
->ident_expression
;
1340 deref
= member_expression(deref
, '.', member
);
1342 deref
= member_expression(symbol
, '.', member
);
1345 member
= number_to_member(symbol
, member_idx
);
1346 deref
= member_expression(symbol
, '.', member
);
1350 if (right
->type
== EXPR_INITIALIZER
) {
1351 type
= get_type(deref
);
1352 if (type
&& type
->type
== SYM_ARRAY
)
1353 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1355 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1357 assign
= assign_expression(deref
, '=', right
);
1360 } END_FOR_EACH_PTR(tmp
);
1362 set_unset_to_zero(struct_type
, symbol
);
1365 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1367 fake_member_assigns_helper(symbol_expression(sym
),
1368 sym
->initializer
->expr_list
, fake_cb
);
1371 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1373 struct expression
*offset
, *binop
, *assign
, *tmp
;
1374 struct symbol
*type
;
1377 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1381 FOR_EACH_PTR(expr_list
, tmp
) {
1382 if (tmp
->type
== EXPR_INDEX
) {
1383 if (tmp
->idx_from
!= tmp
->idx_to
)
1385 idx
= tmp
->idx_from
;
1386 if (!tmp
->idx_expression
)
1388 tmp
= tmp
->idx_expression
;
1390 offset
= value_expr(idx
);
1391 binop
= array_element_expression(array
, offset
);
1392 if (tmp
->type
== EXPR_INITIALIZER
) {
1393 type
= get_type(binop
);
1394 if (type
&& type
->type
== SYM_ARRAY
)
1395 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1397 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1399 assign
= assign_expression(binop
, '=', tmp
);
1404 } END_FOR_EACH_PTR(tmp
);
1407 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1409 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1412 static void fake_assign_expr(struct symbol
*sym
)
1414 struct expression
*assign
, *symbol
;
1416 symbol
= symbol_expression(sym
);
1417 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1418 __split_expr(assign
);
1421 static void call_split_expr(struct expression
*expr
)
1426 static void do_initializer_stuff(struct symbol
*sym
)
1428 if (!sym
->initializer
)
1431 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1432 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1433 fake_element_assigns(sym
, call_split_expr
);
1435 fake_member_assigns(sym
, call_split_expr
);
1437 fake_assign_expr(sym
);
1441 static void split_declaration(struct symbol_list
*sym_list
)
1445 FOR_EACH_PTR(sym_list
, sym
) {
1446 __pass_to_client(sym
, DECLARATION_HOOK
);
1447 do_initializer_stuff(sym
);
1449 } END_FOR_EACH_PTR(sym
);
1452 static void call_global_assign_hooks(struct expression
*assign
)
1454 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1457 static void fake_global_assign(struct symbol
*sym
)
1459 struct expression
*assign
, *symbol
;
1461 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1462 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1463 fake_element_assigns(sym
, call_global_assign_hooks
);
1464 } else if (sym
->initializer
) {
1465 symbol
= symbol_expression(sym
);
1466 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1467 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1469 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1471 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1472 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1473 fake_member_assigns(sym
, call_global_assign_hooks
);
1474 } else if (sym
->initializer
) {
1475 symbol
= symbol_expression(sym
);
1476 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1477 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1479 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1482 symbol
= symbol_expression(sym
);
1483 if (sym
->initializer
)
1484 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1486 assign
= assign_expression(symbol
, '=', zero_expr());
1487 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1491 static void start_function_definition(struct symbol
*sym
)
1493 __in_function_def
= 1;
1494 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1495 __in_function_def
= 0;
1496 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1500 static void split_function(struct symbol
*sym
)
1502 struct symbol
*base_type
= get_base_type(sym
);
1504 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1507 gettimeofday(&fn_start_time
, NULL
);
1510 cur_func
= sym
->ident
->name
;
1511 set_position(sym
->pos
);
1513 last_goto_statement_handled
= 0;
1514 sm_debug("new function: %s\n", cur_func
);
1516 if (option_two_passes
) {
1520 start_function_definition(sym
);
1521 __split_stmt(base_type
->stmt
);
1522 __split_stmt(base_type
->inline_stmt
);
1528 start_function_definition(sym
);
1529 __split_stmt(base_type
->stmt
);
1530 __split_stmt(base_type
->inline_stmt
);
1531 __pass_to_client(sym
, END_FUNC_HOOK
);
1532 if (need_delayed_scope_hooks())
1533 __call_scope_hooks();
1534 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1537 cur_func_sym
= NULL
;
1539 free_data_info_allocs();
1540 free_expression_stack(&switch_expr_stack
);
1541 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1542 __bail_on_rest_of_function
= 0;
1545 static void save_flow_state(void)
1547 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1548 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1549 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1551 __add_ptr_list(&backup
, big_statement_stack
, 0);
1552 __add_ptr_list(&backup
, big_expression_stack
, 0);
1553 __add_ptr_list(&backup
, big_condition_stack
, 0);
1554 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1556 __add_ptr_list(&backup
, cur_func_sym
, 0);
1558 __add_ptr_list(&backup
, __prev_stmt
, 0);
1559 __add_ptr_list(&backup
, __cur_stmt
, 0);
1560 __add_ptr_list(&backup
, __next_stmt
, 0);
1564 static void *pop_backup(void)
1568 ret
= last_ptr_list(backup
);
1569 delete_ptr_list_last(&backup
);
1573 static void restore_flow_state(void)
1575 __next_stmt
= pop_backup();
1576 __cur_stmt
= pop_backup();
1577 __prev_stmt
= pop_backup();
1579 cur_func_sym
= pop_backup();
1580 switch_expr_stack
= pop_backup();
1581 big_condition_stack
= pop_backup();
1582 big_expression_stack
= pop_backup();
1583 big_statement_stack
= pop_backup();
1584 final_pass
= PTR_INT(pop_backup()) >> 2;
1585 loop_count
= PTR_INT(pop_backup()) >> 2;
1586 loop_num
= PTR_INT(pop_backup()) >> 2;
1589 static void parse_inline(struct expression
*call
)
1591 struct symbol
*base_type
;
1592 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1593 struct timeval time_backup
= fn_start_time
;
1597 __pass_to_client(call
, INLINE_FN_START
);
1598 final_pass
= 0; /* don't print anything */
1601 base_type
= get_base_type(call
->fn
->symbol
);
1602 cur_func_sym
= call
->fn
->symbol
;
1603 if (call
->fn
->symbol
->ident
)
1604 cur_func
= call
->fn
->symbol
->ident
->name
;
1607 set_position(call
->fn
->symbol
->pos
);
1610 big_statement_stack
= NULL
;
1611 big_expression_stack
= NULL
;
1612 big_condition_stack
= NULL
;
1613 switch_expr_stack
= NULL
;
1615 sm_debug("inline function: %s\n", cur_func
);
1619 start_function_definition(call
->fn
->symbol
);
1620 __split_stmt(base_type
->stmt
);
1621 __split_stmt(base_type
->inline_stmt
);
1622 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1623 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1625 free_expression_stack(&switch_expr_stack
);
1626 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1630 restore_flow_state();
1631 fn_start_time
= time_backup
;
1632 cur_func
= cur_func_bak
;
1634 restore_all_states();
1635 set_position(call
->pos
);
1637 __pass_to_client(call
, INLINE_FN_END
);
1640 static struct symbol_list
*inlines_called
;
1641 static void add_inline_function(struct symbol
*sym
)
1643 static struct symbol_list
*already_added
;
1646 FOR_EACH_PTR(already_added
, tmp
) {
1649 } END_FOR_EACH_PTR(tmp
);
1651 add_ptr_list(&already_added
, sym
);
1652 add_ptr_list(&inlines_called
, sym
);
1655 static void process_inlines(void)
1659 FOR_EACH_PTR(inlines_called
, tmp
) {
1660 split_function(tmp
);
1661 } END_FOR_EACH_PTR(tmp
);
1662 free_ptr_list(&inlines_called
);
1665 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1669 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1672 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1674 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1676 } END_FOR_EACH_PTR_REVERSE(sym
);
1681 static bool interesting_function(struct symbol
*sym
)
1683 static int prev_stream
= -1;
1684 static bool prev_answer
;
1685 const char *filename
;
1688 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1691 if (sym
->pos
.stream
== prev_stream
)
1694 prev_stream
= sym
->pos
.stream
;
1695 prev_answer
= false;
1697 filename
= stream_name(sym
->pos
.stream
);
1698 len
= strlen(filename
);
1699 if (len
> 0 && filename
[len
- 1] == 'c')
1704 static void split_inlines_in_scope(struct symbol
*sym
)
1706 struct symbol
*base
;
1707 struct symbol_list
*scope_list
;
1710 scope_list
= sym
->scope
->symbols
;
1711 stream
= sym
->pos
.stream
;
1713 /* find the last static symbol in the file */
1714 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1715 if (sym
->pos
.stream
!= stream
)
1717 if (sym
->type
!= SYM_NODE
)
1719 base
= get_base_type(sym
);
1722 if (base
->type
!= SYM_FN
)
1724 if (!base
->inline_stmt
)
1726 if (!interesting_function(sym
))
1728 add_inline_function(sym
);
1729 } END_FOR_EACH_PTR_REVERSE(sym
);
1734 static void split_inlines(struct symbol_list
*sym_list
)
1738 sym
= get_last_scoped_symbol(sym_list
, 0);
1740 split_inlines_in_scope(sym
);
1741 sym
= get_last_scoped_symbol(sym_list
, 1);
1743 split_inlines_in_scope(sym
);
1746 static struct stree
*clone_estates_perm(struct stree
*orig
)
1748 struct stree
*ret
= NULL
;
1749 struct sm_state
*tmp
;
1751 FOR_EACH_SM(orig
, tmp
) {
1752 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1753 } END_FOR_EACH_SM(tmp
);
1758 struct position last_pos
;
1759 static void split_c_file_functions(struct symbol_list
*sym_list
)
1764 FOR_EACH_PTR(sym_list
, sym
) {
1765 set_position(sym
->pos
);
1766 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1767 __pass_to_client(sym
, BASE_HOOK
);
1768 fake_global_assign(sym
);
1770 } END_FOR_EACH_PTR(sym
);
1771 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1774 FOR_EACH_PTR(sym_list
, sym
) {
1775 set_position(sym
->pos
);
1776 last_pos
= sym
->pos
;
1777 if (!interesting_function(sym
))
1779 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1780 split_function(sym
);
1783 last_pos
= sym
->pos
;
1784 } END_FOR_EACH_PTR(sym
);
1785 split_inlines(sym_list
);
1786 __pass_to_client(sym_list
, END_FILE_HOOK
);
1789 static int final_before_fake
;
1790 void init_fake_env(void)
1793 final_before_fake
= final_pass
;
1795 __push_fake_cur_stree();
1799 void end_fake_env(void)
1801 __pop_fake_cur_stree();
1804 final_pass
= final_before_fake
;
1807 static void open_output_files(char *base_file
)
1811 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1812 sm_outfd
= fopen(buf
, "w");
1814 printf("Error: Cannot open %s\n", buf
);
1821 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1822 sql_outfd
= fopen(buf
, "w");
1824 printf("Error: Cannot open %s\n", buf
);
1828 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1829 caller_info_fd
= fopen(buf
, "w");
1830 if (!caller_info_fd
) {
1831 printf("Error: Cannot open %s\n", buf
);
1836 void smatch(int argc
, char **argv
)
1838 struct string_list
*filelist
= NULL
;
1839 struct symbol_list
*sym_list
;
1840 struct timeval stop
, start
;
1842 gettimeofday(&start
, NULL
);
1845 printf("Usage: smatch [--debug] <filename.c>\n");
1848 sparse_initialize(argc
, argv
, &filelist
);
1849 set_valid_ptr_max();
1850 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1851 if (option_file_output
)
1852 open_output_files(base_file
);
1853 sym_list
= sparse_keep_tokens(base_file
);
1854 split_c_file_functions(sym_list
);
1855 } END_FOR_EACH_PTR_NOTAG(base_file
);
1857 gettimeofday(&stop
, NULL
);
1859 set_position(last_pos
);
1861 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1863 sm_msg("mem: %luKb", get_max_memory());