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 void __split_expr(struct expression
*expr
)
294 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
296 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
298 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
301 push_expression(&big_expression_stack
, expr
);
302 set_position(expr
->pos
);
303 __pass_to_client(expr
, EXPR_HOOK
);
305 switch (expr
->type
) {
307 expr_set_parent_expr(expr
->unop
, expr
);
309 if (expr
->op
== '*' &&
310 !prev_expression_is_getting_address(expr
))
311 __pass_to_client(expr
, DEREF_HOOK
);
312 __split_expr(expr
->unop
);
313 __pass_to_client(expr
, OP_HOOK
);
316 expr_set_parent_expr(expr
->unop
, expr
);
318 __split_expr(expr
->unop
);
319 push_expression(&post_op_stack
, expr
);
323 if (expr
->statement
&& !expr
->statement
) {
324 stmt_set_parent_stmt(expr
->statement
,
325 last_ptr_list((struct ptr_list
*)big_statement_stack
));
327 __split_stmt(expr
->statement
);
332 expr_set_parent_expr(expr
->left
, expr
);
333 expr_set_parent_expr(expr
->right
, expr
);
335 __pass_to_client(expr
, LOGIC_HOOK
);
336 __handle_logic(expr
);
339 expr_set_parent_expr(expr
->left
, expr
);
340 expr_set_parent_expr(expr
->right
, expr
);
342 __pass_to_client(expr
, BINOP_HOOK
);
344 expr_set_parent_expr(expr
->left
, expr
);
345 expr_set_parent_expr(expr
->right
, expr
);
347 __split_expr(expr
->left
);
348 __process_post_op_stack();
349 __split_expr(expr
->right
);
351 case EXPR_ASSIGNMENT
: {
352 struct expression
*right
;
354 expr_set_parent_expr(expr
->left
, expr
);
355 expr_set_parent_expr(expr
->right
, expr
);
357 right
= strip_expr(expr
->right
);
361 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
364 if (__handle_condition_assigns(expr
))
366 /* foo = (x < 5 ? foo : 5); */
367 if (__handle_select_assigns(expr
))
369 /* foo = ({frob(); frob(); frob(); 1;}) */
370 if (__handle_expr_statement_assigns(expr
))
373 if (handle_comma_assigns(expr
))
375 if (handle_postop_assigns(expr
))
378 __split_expr(expr
->right
);
379 if (outside_of_function())
380 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
382 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
384 __fake_struct_member_assignments(expr
);
386 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
387 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
389 if (get_macro_name(right
->pos
) &&
390 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
391 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
393 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
395 __split_expr(expr
->left
);
399 expr_set_parent_expr(expr
->deref
, expr
);
401 __pass_to_client(expr
, DEREF_HOOK
);
402 __split_expr(expr
->deref
);
405 expr_set_parent_expr(expr
->base
, expr
);
407 __split_expr(expr
->base
);
410 case EXPR_FORCE_CAST
:
411 expr_set_parent_expr(expr
->cast_expression
, expr
);
413 __pass_to_client(expr
, CAST_HOOK
);
414 __split_expr(expr
->cast_expression
);
417 if (expr
->cast_expression
)
418 __pass_to_client(strip_parens(expr
->cast_expression
),
423 evaluate_expression(expr
);
425 case EXPR_CONDITIONAL
:
427 expr_set_parent_expr(expr
->conditional
, expr
);
428 expr_set_parent_expr(expr
->cond_true
, expr
);
429 expr_set_parent_expr(expr
->cond_false
, expr
);
431 if (known_condition_true(expr
->conditional
)) {
432 __split_expr(expr
->cond_true
);
435 if (known_condition_false(expr
->conditional
)) {
436 __split_expr(expr
->cond_false
);
439 __pass_to_client(expr
, SELECT_HOOK
);
440 __split_whole_condition(expr
->conditional
);
441 __split_expr(expr
->cond_true
);
442 __push_true_states();
443 __use_false_states();
444 __split_expr(expr
->cond_false
);
445 __merge_true_states();
448 expr_set_parent_expr(expr
->fn
, expr
);
450 if (sym_name_is("__builtin_constant_p", expr
->fn
))
452 split_expr_list(expr
->args
, expr
);
453 __split_expr(expr
->fn
);
454 if (is_inline_func(expr
->fn
))
455 add_inline_function(expr
->fn
->symbol
);
456 if (inlinable(expr
->fn
))
458 __process_post_op_stack();
459 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
460 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
462 if (inlinable(expr
->fn
)) {
465 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
466 if (is_noreturn_func(expr
->fn
))
469 case EXPR_INITIALIZER
:
470 split_expr_list(expr
->expr_list
, expr
);
472 case EXPR_IDENTIFIER
:
473 expr_set_parent_expr(expr
->ident_expression
, expr
);
474 __split_expr(expr
->ident_expression
);
477 expr_set_parent_expr(expr
->idx_expression
, expr
);
478 __split_expr(expr
->idx_expression
);
481 expr_set_parent_expr(expr
->init_expr
, expr
);
482 __split_expr(expr
->init_expr
);
485 __pass_to_client(expr
, SYM_HOOK
);
488 __pass_to_client(expr
, STRING_HOOK
);
493 pop_expression(&big_expression_stack
);
496 static int is_forever_loop(struct statement
*stmt
)
498 struct expression
*expr
;
501 expr
= strip_expr(stmt
->iterator_pre_condition
);
503 expr
= stmt
->iterator_post_condition
;
505 /* this is a for(;;) loop... */
509 if (get_value(expr
, &sval
) && sval
.value
!= 0)
516 static char *get_loop_name(int num
)
520 snprintf(buf
, 255, "-loop%d", num
);
522 return alloc_sname(buf
);
526 * Pre Loops are while and for loops.
528 static void handle_pre_loop(struct statement
*stmt
)
530 int once_through
; /* we go through the loop at least once */
531 struct sm_state
*extra_sm
= NULL
;
534 struct stree
*stree
= NULL
;
535 struct sm_state
*sm
= NULL
;
537 loop_name
= get_loop_name(loop_num
);
540 __split_stmt(stmt
->iterator_pre_statement
);
541 __prev_stmt
= stmt
->iterator_pre_statement
;
543 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
549 __merge_gotos(loop_name
, NULL
);
551 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
552 __in_pre_condition
++;
553 __pass_to_client(stmt
, PRELOOP_HOOK
);
554 __split_whole_condition(stmt
->iterator_pre_condition
);
555 __in_pre_condition
--;
556 FOR_EACH_SM(stree
, sm
) {
557 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
558 } END_FOR_EACH_SM(sm
);
561 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
563 if (option_assume_loops
)
566 __split_stmt(stmt
->iterator_statement
);
567 if (is_forever_loop(stmt
)) {
569 __save_gotos(loop_name
, NULL
);
571 __push_fake_cur_stree();
572 __split_stmt(stmt
->iterator_post_statement
);
573 stree
= __pop_fake_cur_stree();
575 __discard_false_states();
578 if (!__path_is_null())
579 __merge_stree_into_cur(stree
);
583 unchanged
= __iterator_unchanged(extra_sm
);
584 __split_stmt(stmt
->iterator_post_statement
);
585 __prev_stmt
= stmt
->iterator_post_statement
;
588 __save_gotos(loop_name
, NULL
);
589 __in_pre_condition
++;
590 __split_whole_condition(stmt
->iterator_pre_condition
);
591 __in_pre_condition
--;
593 __merge_false_states();
595 __discard_false_states();
597 __merge_false_states();
599 if (extra_sm
&& unchanged
)
600 __extra_pre_loop_hook_after(extra_sm
,
601 stmt
->iterator_post_statement
,
602 stmt
->iterator_pre_condition
);
609 * Post loops are do {} while();
611 static void handle_post_loop(struct statement
*stmt
)
615 loop_name
= get_loop_name(loop_num
);
621 __merge_gotos(loop_name
, NULL
);
622 __split_stmt(stmt
->iterator_statement
);
624 if (!is_zero(stmt
->iterator_post_condition
))
625 __save_gotos(loop_name
, NULL
);
627 if (is_forever_loop(stmt
)) {
630 __split_whole_condition(stmt
->iterator_post_condition
);
631 __use_false_states();
637 static int empty_statement(struct statement
*stmt
)
641 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
646 static int last_stmt_on_same_line(void)
648 struct statement
*stmt
;
651 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
654 if (stmt
->pos
.line
== get_lineno())
657 } END_FOR_EACH_PTR_REVERSE(stmt
);
661 static void split_asm_constraints(struct expression_list
*expr_list
)
663 struct expression
*expr
;
666 FOR_EACH_PTR(expr_list
, expr
) {
668 case 0: /* identifier */
669 case 1: /* constraint */
672 case 2: /* expression */
677 } END_FOR_EACH_PTR(expr
);
680 static int is_case_val(struct statement
*stmt
, sval_t sval
)
684 if (stmt
->type
!= STMT_CASE
)
686 if (!stmt
->case_expression
) {
690 if (!get_value(stmt
->case_expression
, &case_sval
))
692 if (case_sval
.value
== sval
.value
)
697 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
698 struct expression
*case_expr
,
699 struct expression
*case_to
)
702 struct range_list
*rl
= NULL
;
703 struct symbol
*switch_type
;
705 switch_type
= get_type(switch_expr
);
706 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
707 start
= sval_cast(switch_type
, start
);
708 end
= sval_cast(switch_type
, end
);
709 add_range(&rl
, start
, end
);
710 } else if (get_value(case_expr
, &start
)) {
711 start
= sval_cast(switch_type
, start
);
712 add_range(&rl
, start
, start
);
718 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
720 struct statement
*tmp
;
721 struct range_list
*rl
;
723 __split_expr(stmt
->switch_expression
);
724 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
726 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
727 __save_switch_states(top_expression(switch_expr_stack
));
732 stmt
= stmt
->switch_statement
;
734 __push_scope_hooks();
735 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
736 __smatch_lineno
= tmp
->pos
.line
;
737 if (is_case_val(tmp
, sval
)) {
738 rl
= alloc_rl(sval
, sval
);
739 __merge_switches(top_expression(switch_expr_stack
), rl
);
740 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
742 if (__path_is_null())
745 if (__path_is_null()) {
749 } END_FOR_EACH_PTR(tmp
);
751 __call_scope_hooks();
752 if (!__pop_default())
753 __merge_switches(top_expression(switch_expr_stack
), NULL
);
754 __discard_switches();
756 pop_expression(&switch_expr_stack
);
759 static void split_case(struct statement
*stmt
)
761 struct range_list
*rl
= NULL
;
763 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
764 expr_set_parent_stmt(stmt
->case_to
, stmt
);
766 rl
= get_case_rl(top_expression(switch_expr_stack
),
767 stmt
->case_expression
, stmt
->case_to
);
768 while (stmt
->case_statement
->type
== STMT_CASE
) {
769 struct range_list
*tmp
;
771 tmp
= get_case_rl(top_expression(switch_expr_stack
),
772 stmt
->case_statement
->case_expression
,
773 stmt
->case_statement
->case_to
);
776 rl
= rl_union(rl
, tmp
);
777 if (!stmt
->case_expression
)
779 stmt
= stmt
->case_statement
;
782 __merge_switches(top_expression(switch_expr_stack
), rl
);
784 if (!stmt
->case_expression
)
786 __split_stmt(stmt
->case_statement
);
789 int time_parsing_function(void)
791 return ms_since(&fn_start_time
) / 1000;
794 static int taking_too_long(void)
796 if (time_parsing_function() > 60 * 5) /* five minutes */
801 static int is_last_stmt(struct statement
*cur_stmt
)
803 struct symbol
*fn
= get_base_type(cur_func_sym
);
804 struct statement
*stmt
;
810 stmt
= fn
->inline_stmt
;
811 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
813 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
814 if (stmt
&& stmt
->type
== STMT_LABEL
)
815 stmt
= stmt
->label_statement
;
816 if (stmt
== cur_stmt
)
821 static void handle_backward_goto(struct statement
*goto_stmt
)
823 const char *goto_name
, *label_name
;
824 struct statement
*func_stmt
;
825 struct symbol
*base_type
= get_base_type(cur_func_sym
);
826 struct statement
*tmp
;
831 if (last_goto_statement_handled
)
833 last_goto_statement_handled
= 1;
835 if (!goto_stmt
->goto_label
||
836 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
837 !goto_stmt
->goto_label
->ident
)
839 goto_name
= goto_stmt
->goto_label
->ident
->name
;
841 func_stmt
= base_type
->stmt
;
843 func_stmt
= base_type
->inline_stmt
;
846 if (func_stmt
->type
!= STMT_COMPOUND
)
849 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
851 if (tmp
->type
!= STMT_LABEL
)
853 if (!tmp
->label_identifier
||
854 tmp
->label_identifier
->type
!= SYM_LABEL
||
855 !tmp
->label_identifier
->ident
)
857 label_name
= tmp
->label_identifier
->ident
->name
;
858 if (strcmp(goto_name
, label_name
) != 0)
863 } END_FOR_EACH_PTR(tmp
);
866 static void fake_a_return(void)
868 struct symbol
*return_type
;
873 return_type
= get_real_base_type(cur_func_sym
);
874 return_type
= get_real_base_type(return_type
);
875 if (return_type
!= &void_ctype
) {
876 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
881 static void fake_an_empty_default(struct position pos
)
883 static struct statement none
= {};
886 none
.type
= STMT_NONE
;
887 __merge_switches(top_expression(switch_expr_stack
), NULL
);
891 static void split_compound(struct statement
*stmt
)
893 struct statement
*prev
= NULL
;
894 struct statement
*cur
= NULL
;
895 struct statement
*next
;
897 __push_scope_hooks();
899 FOR_EACH_PTR(stmt
->stmts
, next
) {
900 /* just set them all ahead of time */
901 stmt_set_parent_stmt(next
, stmt
);
911 } END_FOR_EACH_PTR(next
);
920 * For function scope, then delay calling the scope hooks until the
921 * end of function hooks can run. I'm not positive this is the right
924 if (!is_last_stmt(cur
))
925 __call_scope_hooks();
929 * This is a hack, work around for detecting empty functions.
931 static int need_delayed_scope_hooks(void)
933 struct symbol
*fn
= get_base_type(cur_func_sym
);
934 struct statement
*stmt
;
940 stmt
= fn
->inline_stmt
;
941 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
946 void __split_label_stmt(struct statement
*stmt
)
948 if (stmt
->label_identifier
&&
949 stmt
->label_identifier
->type
== SYM_LABEL
&&
950 stmt
->label_identifier
->ident
) {
951 loop_count
|= 0x0800000;
952 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
956 static void find_asm_gotos(struct statement
*stmt
)
960 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
961 __save_gotos(sym
->ident
->name
, sym
);
962 } END_FOR_EACH_PTR(sym
);
965 void __split_stmt(struct statement
*stmt
)
972 if (!__in_fake_assign
)
973 __silence_warnings_for_stmt
= false;
975 if (__bail_on_rest_of_function
)
978 if (out_of_memory() || taking_too_long()) {
981 gettimeofday(&stop
, NULL
);
983 __bail_on_rest_of_function
= 1;
985 sm_msg("Function too hairy. Giving up. %lu seconds",
986 stop
.tv_sec
- fn_start_time
.tv_sec
);
988 final_pass
= 0; /* turn off sm_msg() from here */
992 add_ptr_list(&big_statement_stack
, stmt
);
993 free_expression_stack(&big_expression_stack
);
994 set_position(stmt
->pos
);
995 __pass_to_client(stmt
, STMT_HOOK
);
997 switch (stmt
->type
) {
998 case STMT_DECLARATION
:
999 split_declaration(stmt
->declaration
);
1002 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1004 __split_expr(stmt
->ret_value
);
1005 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1006 __process_post_op_stack();
1009 case STMT_EXPRESSION
:
1010 expr_set_parent_stmt(stmt
->expression
, stmt
);
1011 expr_set_parent_stmt(stmt
->context
, stmt
);
1013 __split_expr(stmt
->expression
);
1016 split_compound(stmt
);
1019 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1020 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1021 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1023 if (known_condition_true(stmt
->if_conditional
)) {
1024 __split_stmt(stmt
->if_true
);
1027 if (known_condition_false(stmt
->if_conditional
)) {
1028 __split_stmt(stmt
->if_false
);
1031 __split_whole_condition(stmt
->if_conditional
);
1032 __split_stmt(stmt
->if_true
);
1033 if (empty_statement(stmt
->if_true
) &&
1034 last_stmt_on_same_line() &&
1035 !get_macro_name(stmt
->if_true
->pos
))
1036 sm_msg("warn: if();");
1037 __push_true_states();
1038 __use_false_states();
1039 __split_stmt(stmt
->if_false
);
1040 __merge_true_states();
1043 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1044 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1045 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1046 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1047 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1049 if (stmt
->iterator_pre_condition
)
1050 handle_pre_loop(stmt
);
1051 else if (stmt
->iterator_post_condition
)
1052 handle_post_loop(stmt
);
1054 // these are for(;;) type loops.
1055 handle_pre_loop(stmt
);
1059 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1060 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1062 if (get_value(stmt
->switch_expression
, &sval
)) {
1063 split_known_switch(stmt
, sval
);
1066 __split_expr(stmt
->switch_expression
);
1067 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1068 __save_switch_states(top_expression(switch_expr_stack
));
1072 __split_stmt(stmt
->switch_statement
);
1073 if (!__pop_default() && have_remaining_cases())
1074 fake_an_empty_default(stmt
->pos
);
1075 __discard_switches();
1077 pop_expression(&switch_expr_stack
);
1083 __split_label_stmt(stmt
);
1084 __split_stmt(stmt
->label_statement
);
1087 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1089 __split_expr(stmt
->goto_expression
);
1090 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1091 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1093 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1095 __process_continues();
1097 } else if (stmt
->goto_label
&&
1098 stmt
->goto_label
->type
== SYM_LABEL
&&
1099 stmt
->goto_label
->ident
) {
1100 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1103 if (is_last_stmt(stmt
))
1104 handle_backward_goto(stmt
);
1109 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1111 find_asm_gotos(stmt
);
1112 __pass_to_client(stmt
, ASM_HOOK
);
1113 __split_expr(stmt
->asm_string
);
1114 split_asm_constraints(stmt
->asm_outputs
);
1115 split_asm_constraints(stmt
->asm_inputs
);
1116 split_asm_constraints(stmt
->asm_clobbers
);
1121 __split_expr(stmt
->range_expression
);
1122 __split_expr(stmt
->range_low
);
1123 __split_expr(stmt
->range_high
);
1126 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1128 __process_post_op_stack();
1131 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1133 struct expression
*expr
;
1135 FOR_EACH_PTR(expr_list
, expr
) {
1136 expr_set_parent_expr(expr
, parent
);
1138 __process_post_op_stack();
1139 } END_FOR_EACH_PTR(expr
);
1142 static void split_sym(struct symbol
*sym
)
1146 if (!(sym
->namespace & NS_SYMBOL
))
1149 __split_stmt(sym
->stmt
);
1150 __split_expr(sym
->array_size
);
1151 split_symlist(sym
->arguments
);
1152 split_symlist(sym
->symbol_list
);
1153 __split_stmt(sym
->inline_stmt
);
1154 split_symlist(sym
->inline_symbol_list
);
1157 static void split_symlist(struct symbol_list
*sym_list
)
1161 FOR_EACH_PTR(sym_list
, sym
) {
1163 } END_FOR_EACH_PTR(sym
);
1166 typedef void (fake_cb
)(struct expression
*expr
);
1168 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1170 struct symbol
*type
, *tmp
;
1176 name
= member
->name
;
1178 type
= get_type(expr
);
1179 if (!type
|| type
->type
!= SYM_STRUCT
)
1183 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1187 if (strcmp(name
, tmp
->ident
->name
) == 0)
1189 } END_FOR_EACH_PTR(tmp
);
1193 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1195 struct symbol
*type
, *member
;
1198 type
= get_type(expr
);
1199 if (!type
|| type
->type
!= SYM_STRUCT
)
1202 FOR_EACH_PTR(type
->symbol_list
, member
) {
1204 return member
->ident
;
1206 } END_FOR_EACH_PTR(member
);
1210 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1212 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1214 struct expression
*edge_member
, *assign
;
1215 struct symbol
*base
= get_real_base_type(member
);
1219 expr
= member_expression(expr
, '.', member
->ident
);
1221 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1222 struct symbol
*type
;
1224 type
= get_real_base_type(tmp
);
1228 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1229 if (get_extra_state(edge_member
))
1232 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1233 set_inner_struct_members(expr
, tmp
);
1240 assign
= assign_expression(edge_member
, zero_expr());
1241 __split_expr(assign
);
1242 } END_FOR_EACH_PTR(tmp
);
1247 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1250 struct expression
*member
= NULL
;
1251 struct expression
*assign
;
1254 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1255 expr
= strip_expr(expr
->unop
);
1259 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1260 type
= get_real_base_type(tmp
);
1265 member
= member_expression(expr
, op
, tmp
->ident
);
1266 if (get_extra_state(member
))
1270 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1271 set_inner_struct_members(expr
, tmp
);
1274 if (type
->type
== SYM_ARRAY
)
1279 assign
= assign_expression(member
, zero_expr());
1280 __split_expr(assign
);
1281 } END_FOR_EACH_PTR(tmp
);
1284 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1286 struct expression
*deref
, *assign
, *tmp
, *right
;
1287 struct symbol
*struct_type
, *type
;
1288 struct ident
*member
;
1291 struct_type
= get_type(symbol
);
1293 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1297 * We're parsing an initializer that could look something like this:
1298 * struct foo foo = {
1300 * .whatever.xxx = 11,
1304 * So what we have here is a list with 42, .whatever, and .zzz. We need
1305 * to break it up into left and right sides of the assignments.
1309 FOR_EACH_PTR(members
, tmp
) {
1311 if (tmp
->type
== EXPR_IDENTIFIER
) {
1312 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1313 while (tmp
->type
== EXPR_IDENTIFIER
) {
1314 member
= tmp
->expr_ident
;
1315 tmp
= tmp
->ident_expression
;
1317 deref
= member_expression(deref
, '.', member
);
1319 deref
= member_expression(symbol
, '.', member
);
1322 member
= number_to_member(symbol
, member_idx
);
1323 deref
= member_expression(symbol
, '.', member
);
1327 if (right
->type
== EXPR_INITIALIZER
) {
1328 type
= get_type(deref
);
1329 if (type
&& type
->type
== SYM_ARRAY
)
1330 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1332 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1334 assign
= assign_expression(deref
, right
);
1337 } END_FOR_EACH_PTR(tmp
);
1339 set_unset_to_zero(struct_type
, symbol
);
1342 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1344 fake_member_assigns_helper(symbol_expression(sym
),
1345 sym
->initializer
->expr_list
, fake_cb
);
1348 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1350 struct expression
*offset
, *binop
, *assign
, *tmp
;
1351 struct symbol
*type
;
1354 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1358 FOR_EACH_PTR(expr_list
, tmp
) {
1359 if (tmp
->type
== EXPR_INDEX
) {
1360 if (tmp
->idx_from
!= tmp
->idx_to
)
1362 idx
= tmp
->idx_from
;
1363 if (!tmp
->idx_expression
)
1365 tmp
= tmp
->idx_expression
;
1367 offset
= value_expr(idx
);
1368 binop
= array_element_expression(array
, offset
);
1369 if (tmp
->type
== EXPR_INITIALIZER
) {
1370 type
= get_type(binop
);
1371 if (type
&& type
->type
== SYM_ARRAY
)
1372 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1374 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1376 assign
= assign_expression(binop
, tmp
);
1381 } END_FOR_EACH_PTR(tmp
);
1384 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1386 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1389 static void fake_assign_expr(struct symbol
*sym
)
1391 struct expression
*assign
, *symbol
;
1393 symbol
= symbol_expression(sym
);
1394 assign
= assign_expression(symbol
, sym
->initializer
);
1395 __split_expr(assign
);
1398 static void call_split_expr(struct expression
*expr
)
1403 static void do_initializer_stuff(struct symbol
*sym
)
1405 if (!sym
->initializer
)
1408 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1409 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1410 fake_element_assigns(sym
, call_split_expr
);
1412 fake_member_assigns(sym
, call_split_expr
);
1414 fake_assign_expr(sym
);
1418 static void split_declaration(struct symbol_list
*sym_list
)
1422 FOR_EACH_PTR(sym_list
, sym
) {
1423 __pass_to_client(sym
, DECLARATION_HOOK
);
1424 do_initializer_stuff(sym
);
1426 } END_FOR_EACH_PTR(sym
);
1429 static void call_global_assign_hooks(struct expression
*assign
)
1431 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1434 static void fake_global_assign(struct symbol
*sym
)
1436 struct expression
*assign
, *symbol
;
1438 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1439 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1440 fake_element_assigns(sym
, call_global_assign_hooks
);
1441 } else if (sym
->initializer
) {
1442 symbol
= symbol_expression(sym
);
1443 assign
= assign_expression(symbol
, sym
->initializer
);
1444 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1446 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1448 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1449 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1450 fake_member_assigns(sym
, call_global_assign_hooks
);
1451 } else if (sym
->initializer
) {
1452 symbol
= symbol_expression(sym
);
1453 assign
= assign_expression(symbol
, sym
->initializer
);
1454 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1456 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1459 symbol
= symbol_expression(sym
);
1460 if (sym
->initializer
)
1461 assign
= assign_expression(symbol
, sym
->initializer
);
1463 assign
= assign_expression(symbol
, zero_expr());
1464 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1468 static void start_function_definition(struct symbol
*sym
)
1470 __in_function_def
= 1;
1471 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1472 __in_function_def
= 0;
1473 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1477 static void split_function(struct symbol
*sym
)
1479 struct symbol
*base_type
= get_base_type(sym
);
1481 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1484 gettimeofday(&fn_start_time
, NULL
);
1487 cur_func
= sym
->ident
->name
;
1488 set_position(sym
->pos
);
1490 last_goto_statement_handled
= 0;
1491 sm_debug("new function: %s\n", cur_func
);
1493 if (option_two_passes
) {
1497 start_function_definition(sym
);
1498 __split_stmt(base_type
->stmt
);
1499 __split_stmt(base_type
->inline_stmt
);
1505 start_function_definition(sym
);
1506 __split_stmt(base_type
->stmt
);
1507 __split_stmt(base_type
->inline_stmt
);
1508 __pass_to_client(sym
, END_FUNC_HOOK
);
1509 if (need_delayed_scope_hooks())
1510 __call_scope_hooks();
1511 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1514 cur_func_sym
= NULL
;
1516 free_data_info_allocs();
1517 free_expression_stack(&switch_expr_stack
);
1518 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1519 __bail_on_rest_of_function
= 0;
1522 static void save_flow_state(void)
1524 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1525 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1526 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1528 __add_ptr_list(&backup
, big_statement_stack
, 0);
1529 __add_ptr_list(&backup
, big_expression_stack
, 0);
1530 __add_ptr_list(&backup
, big_condition_stack
, 0);
1531 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1533 __add_ptr_list(&backup
, cur_func_sym
, 0);
1535 __add_ptr_list(&backup
, __prev_stmt
, 0);
1536 __add_ptr_list(&backup
, __cur_stmt
, 0);
1537 __add_ptr_list(&backup
, __next_stmt
, 0);
1541 static void *pop_backup(void)
1545 ret
= last_ptr_list(backup
);
1546 delete_ptr_list_last(&backup
);
1550 static void restore_flow_state(void)
1552 __next_stmt
= pop_backup();
1553 __cur_stmt
= pop_backup();
1554 __prev_stmt
= pop_backup();
1556 cur_func_sym
= pop_backup();
1557 switch_expr_stack
= pop_backup();
1558 big_condition_stack
= pop_backup();
1559 big_expression_stack
= pop_backup();
1560 big_statement_stack
= pop_backup();
1561 final_pass
= PTR_INT(pop_backup()) >> 2;
1562 loop_count
= PTR_INT(pop_backup()) >> 2;
1563 loop_num
= PTR_INT(pop_backup()) >> 2;
1566 static void parse_inline(struct expression
*call
)
1568 struct symbol
*base_type
;
1569 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1570 struct timeval time_backup
= fn_start_time
;
1574 __pass_to_client(call
, INLINE_FN_START
);
1575 final_pass
= 0; /* don't print anything */
1578 base_type
= get_base_type(call
->fn
->symbol
);
1579 cur_func_sym
= call
->fn
->symbol
;
1580 if (call
->fn
->symbol
->ident
)
1581 cur_func
= call
->fn
->symbol
->ident
->name
;
1584 set_position(call
->fn
->symbol
->pos
);
1587 big_statement_stack
= NULL
;
1588 big_expression_stack
= NULL
;
1589 big_condition_stack
= NULL
;
1590 switch_expr_stack
= NULL
;
1592 sm_debug("inline function: %s\n", cur_func
);
1596 start_function_definition(call
->fn
->symbol
);
1597 __split_stmt(base_type
->stmt
);
1598 __split_stmt(base_type
->inline_stmt
);
1599 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1600 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1602 free_expression_stack(&switch_expr_stack
);
1603 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1607 restore_flow_state();
1608 fn_start_time
= time_backup
;
1609 cur_func
= cur_func_bak
;
1611 restore_all_states();
1612 set_position(call
->pos
);
1614 __pass_to_client(call
, INLINE_FN_END
);
1617 static struct symbol_list
*inlines_called
;
1618 static void add_inline_function(struct symbol
*sym
)
1620 static struct symbol_list
*already_added
;
1623 FOR_EACH_PTR(already_added
, tmp
) {
1626 } END_FOR_EACH_PTR(tmp
);
1628 add_ptr_list(&already_added
, sym
);
1629 add_ptr_list(&inlines_called
, sym
);
1632 static void process_inlines(void)
1636 FOR_EACH_PTR(inlines_called
, tmp
) {
1637 split_function(tmp
);
1638 } END_FOR_EACH_PTR(tmp
);
1639 free_ptr_list(&inlines_called
);
1642 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1646 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1649 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1651 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1653 } END_FOR_EACH_PTR_REVERSE(sym
);
1658 static void split_inlines_in_scope(struct symbol
*sym
)
1660 struct symbol
*base
;
1661 struct symbol_list
*scope_list
;
1664 scope_list
= sym
->scope
->symbols
;
1665 stream
= sym
->pos
.stream
;
1667 /* find the last static symbol in the file */
1668 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1669 if (sym
->pos
.stream
!= stream
)
1671 if (sym
->type
!= SYM_NODE
)
1673 base
= get_base_type(sym
);
1676 if (base
->type
!= SYM_FN
)
1678 if (!base
->inline_stmt
)
1680 add_inline_function(sym
);
1681 } END_FOR_EACH_PTR_REVERSE(sym
);
1686 static void split_inlines(struct symbol_list
*sym_list
)
1690 sym
= get_last_scoped_symbol(sym_list
, 0);
1692 split_inlines_in_scope(sym
);
1693 sym
= get_last_scoped_symbol(sym_list
, 1);
1695 split_inlines_in_scope(sym
);
1698 static struct stree
*clone_estates_perm(struct stree
*orig
)
1700 struct stree
*ret
= NULL
;
1701 struct sm_state
*tmp
;
1703 FOR_EACH_SM(orig
, tmp
) {
1704 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1705 } END_FOR_EACH_SM(tmp
);
1710 static bool interesting_function(struct symbol
*sym
)
1712 static int prev_stream
= -1;
1713 static bool prev_answer
;
1714 const char *filename
;
1718 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1721 if (sym
->pos
.stream
== prev_stream
)
1724 prev_stream
= sym
->pos
.stream
;
1725 prev_answer
= false;
1727 filename
= stream_name(sym
->pos
.stream
);
1728 len
= strlen(filename
);
1729 if (len
> 0 && filename
[len
- 1] == 'c')
1734 struct position last_pos
;
1735 static void split_c_file_functions(struct symbol_list
*sym_list
)
1740 FOR_EACH_PTR(sym_list
, sym
) {
1741 set_position(sym
->pos
);
1742 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1743 __pass_to_client(sym
, BASE_HOOK
);
1744 fake_global_assign(sym
);
1746 } END_FOR_EACH_PTR(sym
);
1747 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1750 FOR_EACH_PTR(sym_list
, sym
) {
1751 set_position(sym
->pos
);
1752 last_pos
= sym
->pos
;
1753 if (!interesting_function(sym
))
1755 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1756 split_function(sym
);
1759 last_pos
= sym
->pos
;
1760 } END_FOR_EACH_PTR(sym
);
1761 split_inlines(sym_list
);
1762 __pass_to_client(sym_list
, END_FILE_HOOK
);
1765 static int final_before_fake
;
1766 void init_fake_env(void)
1769 final_before_fake
= final_pass
;
1771 __push_fake_cur_stree();
1775 void end_fake_env(void)
1777 __pop_fake_cur_stree();
1780 final_pass
= final_before_fake
;
1783 static void open_output_files(char *base_file
)
1787 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1788 sm_outfd
= fopen(buf
, "w");
1790 printf("Error: Cannot open %s\n", buf
);
1797 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1798 sql_outfd
= fopen(buf
, "w");
1800 printf("Error: Cannot open %s\n", buf
);
1804 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1805 caller_info_fd
= fopen(buf
, "w");
1806 if (!caller_info_fd
) {
1807 printf("Error: Cannot open %s\n", buf
);
1812 void smatch(int argc
, char **argv
)
1814 struct string_list
*filelist
= NULL
;
1815 struct symbol_list
*sym_list
;
1816 struct timeval stop
, start
;
1818 gettimeofday(&start
, NULL
);
1821 printf("Usage: smatch [--debug] <filename.c>\n");
1824 sparse_initialize(argc
, argv
, &filelist
);
1825 set_valid_ptr_max();
1826 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1827 if (option_file_output
)
1828 open_output_files(base_file
);
1829 sym_list
= sparse_keep_tokens(base_file
);
1830 split_c_file_functions(sym_list
);
1831 } END_FOR_EACH_PTR_NOTAG(base_file
);
1833 gettimeofday(&stop
, NULL
);
1835 set_position(last_pos
);
1837 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
1839 sm_msg("mem: %luKb", get_max_memory());