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
);
461 if (inlinable(expr
->fn
)) {
464 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
465 if (is_noreturn_func(expr
->fn
))
468 case EXPR_INITIALIZER
:
469 split_expr_list(expr
->expr_list
, expr
);
471 case EXPR_IDENTIFIER
:
472 expr_set_parent_expr(expr
->ident_expression
, expr
);
473 __split_expr(expr
->ident_expression
);
476 expr_set_parent_expr(expr
->idx_expression
, expr
);
477 __split_expr(expr
->idx_expression
);
480 expr_set_parent_expr(expr
->init_expr
, expr
);
481 __split_expr(expr
->init_expr
);
484 __pass_to_client(expr
, SYM_HOOK
);
487 __pass_to_client(expr
, STRING_HOOK
);
492 pop_expression(&big_expression_stack
);
495 static int is_forever_loop(struct statement
*stmt
)
497 struct expression
*expr
;
500 expr
= strip_expr(stmt
->iterator_pre_condition
);
502 expr
= stmt
->iterator_post_condition
;
504 /* this is a for(;;) loop... */
508 if (get_value(expr
, &sval
) && sval
.value
!= 0)
515 static char *get_loop_name(int num
)
519 snprintf(buf
, 255, "-loop%d", num
);
521 return alloc_sname(buf
);
525 * Pre Loops are while and for loops.
527 static void handle_pre_loop(struct statement
*stmt
)
529 int once_through
; /* we go through the loop at least once */
530 struct sm_state
*extra_sm
= NULL
;
533 struct stree
*stree
= NULL
;
534 struct sm_state
*sm
= NULL
;
536 loop_name
= get_loop_name(loop_num
);
539 __split_stmt(stmt
->iterator_pre_statement
);
540 __prev_stmt
= stmt
->iterator_pre_statement
;
542 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
548 __merge_gotos(loop_name
, NULL
);
550 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
551 __in_pre_condition
++;
552 __pass_to_client(stmt
, PRELOOP_HOOK
);
553 __split_whole_condition(stmt
->iterator_pre_condition
);
554 __in_pre_condition
--;
555 FOR_EACH_SM(stree
, sm
) {
556 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
557 } END_FOR_EACH_SM(sm
);
560 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
562 if (option_assume_loops
)
565 __split_stmt(stmt
->iterator_statement
);
566 if (is_forever_loop(stmt
)) {
568 __save_gotos(loop_name
, NULL
);
570 __push_fake_cur_stree();
571 __split_stmt(stmt
->iterator_post_statement
);
572 stree
= __pop_fake_cur_stree();
574 __discard_false_states();
577 if (!__path_is_null())
578 __merge_stree_into_cur(stree
);
582 unchanged
= __iterator_unchanged(extra_sm
);
583 __split_stmt(stmt
->iterator_post_statement
);
584 __prev_stmt
= stmt
->iterator_post_statement
;
587 __save_gotos(loop_name
, NULL
);
588 __in_pre_condition
++;
589 __split_whole_condition(stmt
->iterator_pre_condition
);
590 __in_pre_condition
--;
592 __merge_false_states();
594 __discard_false_states();
596 __merge_false_states();
598 if (extra_sm
&& unchanged
)
599 __extra_pre_loop_hook_after(extra_sm
,
600 stmt
->iterator_post_statement
,
601 stmt
->iterator_pre_condition
);
608 * Post loops are do {} while();
610 static void handle_post_loop(struct statement
*stmt
)
614 loop_name
= get_loop_name(loop_num
);
620 __merge_gotos(loop_name
, NULL
);
621 __split_stmt(stmt
->iterator_statement
);
623 if (!is_zero(stmt
->iterator_post_condition
))
624 __save_gotos(loop_name
, NULL
);
626 if (is_forever_loop(stmt
)) {
629 __split_whole_condition(stmt
->iterator_post_condition
);
630 __use_false_states();
636 static int empty_statement(struct statement
*stmt
)
640 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
645 static int last_stmt_on_same_line(void)
647 struct statement
*stmt
;
650 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
653 if (stmt
->pos
.line
== get_lineno())
656 } END_FOR_EACH_PTR_REVERSE(stmt
);
660 static void split_asm_constraints(struct expression_list
*expr_list
)
662 struct expression
*expr
;
665 FOR_EACH_PTR(expr_list
, expr
) {
667 case 0: /* identifier */
668 case 1: /* constraint */
671 case 2: /* expression */
676 } END_FOR_EACH_PTR(expr
);
679 static int is_case_val(struct statement
*stmt
, sval_t sval
)
683 if (stmt
->type
!= STMT_CASE
)
685 if (!stmt
->case_expression
) {
689 if (!get_value(stmt
->case_expression
, &case_sval
))
691 if (case_sval
.value
== sval
.value
)
696 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
697 struct expression
*case_expr
,
698 struct expression
*case_to
)
701 struct range_list
*rl
= NULL
;
702 struct symbol
*switch_type
;
704 switch_type
= get_type(switch_expr
);
705 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
706 start
= sval_cast(switch_type
, start
);
707 end
= sval_cast(switch_type
, end
);
708 add_range(&rl
, start
, end
);
709 } else if (get_value(case_expr
, &start
)) {
710 start
= sval_cast(switch_type
, start
);
711 add_range(&rl
, start
, start
);
717 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
719 struct statement
*tmp
;
720 struct range_list
*rl
;
722 __split_expr(stmt
->switch_expression
);
723 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
725 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
726 __save_switch_states(top_expression(switch_expr_stack
));
731 stmt
= stmt
->switch_statement
;
733 __push_scope_hooks();
734 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
735 __smatch_lineno
= tmp
->pos
.line
;
736 if (is_case_val(tmp
, sval
)) {
737 rl
= alloc_rl(sval
, sval
);
738 __merge_switches(top_expression(switch_expr_stack
), rl
);
739 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
741 if (__path_is_null())
744 if (__path_is_null()) {
748 } END_FOR_EACH_PTR(tmp
);
750 __call_scope_hooks();
751 if (!__pop_default())
752 __merge_switches(top_expression(switch_expr_stack
), NULL
);
753 __discard_switches();
755 pop_expression(&switch_expr_stack
);
758 static void split_case(struct statement
*stmt
)
760 struct range_list
*rl
= NULL
;
762 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
763 expr_set_parent_stmt(stmt
->case_to
, stmt
);
765 rl
= get_case_rl(top_expression(switch_expr_stack
),
766 stmt
->case_expression
, stmt
->case_to
);
767 while (stmt
->case_statement
->type
== STMT_CASE
) {
768 struct range_list
*tmp
;
770 tmp
= get_case_rl(top_expression(switch_expr_stack
),
771 stmt
->case_statement
->case_expression
,
772 stmt
->case_statement
->case_to
);
775 rl
= rl_union(rl
, tmp
);
776 if (!stmt
->case_expression
)
778 stmt
= stmt
->case_statement
;
781 __merge_switches(top_expression(switch_expr_stack
), rl
);
783 if (!stmt
->case_expression
)
785 __split_stmt(stmt
->case_statement
);
788 int time_parsing_function(void)
790 return ms_since(&fn_start_time
) / 1000;
793 static int taking_too_long(void)
795 if (time_parsing_function() > 60 * 5) /* five minutes */
800 static int is_last_stmt(struct statement
*cur_stmt
)
802 struct symbol
*fn
= get_base_type(cur_func_sym
);
803 struct statement
*stmt
;
809 stmt
= fn
->inline_stmt
;
810 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
812 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
813 if (stmt
&& stmt
->type
== STMT_LABEL
)
814 stmt
= stmt
->label_statement
;
815 if (stmt
== cur_stmt
)
820 static void handle_backward_goto(struct statement
*goto_stmt
)
822 const char *goto_name
, *label_name
;
823 struct statement
*func_stmt
;
824 struct symbol
*base_type
= get_base_type(cur_func_sym
);
825 struct statement
*tmp
;
830 if (last_goto_statement_handled
)
832 last_goto_statement_handled
= 1;
834 if (!goto_stmt
->goto_label
||
835 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
836 !goto_stmt
->goto_label
->ident
)
838 goto_name
= goto_stmt
->goto_label
->ident
->name
;
840 func_stmt
= base_type
->stmt
;
842 func_stmt
= base_type
->inline_stmt
;
845 if (func_stmt
->type
!= STMT_COMPOUND
)
848 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
850 if (tmp
->type
!= STMT_LABEL
)
852 if (!tmp
->label_identifier
||
853 tmp
->label_identifier
->type
!= SYM_LABEL
||
854 !tmp
->label_identifier
->ident
)
856 label_name
= tmp
->label_identifier
->ident
->name
;
857 if (strcmp(goto_name
, label_name
) != 0)
862 } END_FOR_EACH_PTR(tmp
);
865 static void fake_a_return(void)
867 struct symbol
*return_type
;
872 return_type
= get_real_base_type(cur_func_sym
);
873 return_type
= get_real_base_type(return_type
);
874 if (return_type
!= &void_ctype
) {
875 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
880 static void fake_an_empty_default(struct position pos
)
882 static struct statement none
= {};
885 none
.type
= STMT_NONE
;
886 __merge_switches(top_expression(switch_expr_stack
), NULL
);
890 static void split_compound(struct statement
*stmt
)
892 struct statement
*prev
= NULL
;
893 struct statement
*cur
= NULL
;
894 struct statement
*next
;
896 __push_scope_hooks();
898 FOR_EACH_PTR(stmt
->stmts
, next
) {
899 /* just set them all ahead of time */
900 stmt_set_parent_stmt(next
, stmt
);
910 } END_FOR_EACH_PTR(next
);
919 * For function scope, then delay calling the scope hooks until the
920 * end of function hooks can run. I'm not positive this is the right
923 if (!is_last_stmt(cur
))
924 __call_scope_hooks();
928 * This is a hack, work around for detecting empty functions.
930 static int need_delayed_scope_hooks(void)
932 struct symbol
*fn
= get_base_type(cur_func_sym
);
933 struct statement
*stmt
;
939 stmt
= fn
->inline_stmt
;
940 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
945 void __split_label_stmt(struct statement
*stmt
)
947 if (stmt
->label_identifier
&&
948 stmt
->label_identifier
->type
== SYM_LABEL
&&
949 stmt
->label_identifier
->ident
) {
950 loop_count
|= 0x0800000;
951 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
955 static void find_asm_gotos(struct statement
*stmt
)
959 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
960 __save_gotos(sym
->ident
->name
, sym
);
961 } END_FOR_EACH_PTR(sym
);
964 void __split_stmt(struct statement
*stmt
)
971 if (!__in_fake_assign
)
972 __silence_warnings_for_stmt
= false;
974 if (__bail_on_rest_of_function
)
977 if (out_of_memory() || taking_too_long()) {
980 gettimeofday(&stop
, NULL
);
982 __bail_on_rest_of_function
= 1;
984 sm_msg("Function too hairy. Giving up. %lu seconds",
985 stop
.tv_sec
- fn_start_time
.tv_sec
);
987 final_pass
= 0; /* turn off sm_msg() from here */
991 add_ptr_list(&big_statement_stack
, stmt
);
992 free_expression_stack(&big_expression_stack
);
993 set_position(stmt
->pos
);
994 __pass_to_client(stmt
, STMT_HOOK
);
996 switch (stmt
->type
) {
997 case STMT_DECLARATION
:
998 split_declaration(stmt
->declaration
);
1001 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1003 __split_expr(stmt
->ret_value
);
1004 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1005 __process_post_op_stack();
1008 case STMT_EXPRESSION
:
1009 expr_set_parent_stmt(stmt
->expression
, stmt
);
1010 expr_set_parent_stmt(stmt
->context
, stmt
);
1012 __split_expr(stmt
->expression
);
1015 split_compound(stmt
);
1018 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1019 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1020 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1022 if (known_condition_true(stmt
->if_conditional
)) {
1023 __split_stmt(stmt
->if_true
);
1026 if (known_condition_false(stmt
->if_conditional
)) {
1027 __split_stmt(stmt
->if_false
);
1030 __split_whole_condition(stmt
->if_conditional
);
1031 __split_stmt(stmt
->if_true
);
1032 if (empty_statement(stmt
->if_true
) &&
1033 last_stmt_on_same_line() &&
1034 !get_macro_name(stmt
->if_true
->pos
))
1035 sm_msg("warn: if();");
1036 __push_true_states();
1037 __use_false_states();
1038 __split_stmt(stmt
->if_false
);
1039 __merge_true_states();
1042 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1043 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1044 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1045 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1046 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1048 if (stmt
->iterator_pre_condition
)
1049 handle_pre_loop(stmt
);
1050 else if (stmt
->iterator_post_condition
)
1051 handle_post_loop(stmt
);
1053 // these are for(;;) type loops.
1054 handle_pre_loop(stmt
);
1058 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1059 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1061 if (get_value(stmt
->switch_expression
, &sval
)) {
1062 split_known_switch(stmt
, sval
);
1065 __split_expr(stmt
->switch_expression
);
1066 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1067 __save_switch_states(top_expression(switch_expr_stack
));
1071 __split_stmt(stmt
->switch_statement
);
1072 if (!__pop_default() && have_remaining_cases())
1073 fake_an_empty_default(stmt
->pos
);
1074 __discard_switches();
1076 pop_expression(&switch_expr_stack
);
1082 __split_label_stmt(stmt
);
1083 __split_stmt(stmt
->label_statement
);
1086 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1088 __split_expr(stmt
->goto_expression
);
1089 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1090 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1092 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1094 __process_continues();
1096 } else if (stmt
->goto_label
&&
1097 stmt
->goto_label
->type
== SYM_LABEL
&&
1098 stmt
->goto_label
->ident
) {
1099 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1102 if (is_last_stmt(stmt
))
1103 handle_backward_goto(stmt
);
1108 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1110 find_asm_gotos(stmt
);
1111 __pass_to_client(stmt
, ASM_HOOK
);
1112 __split_expr(stmt
->asm_string
);
1113 split_asm_constraints(stmt
->asm_outputs
);
1114 split_asm_constraints(stmt
->asm_inputs
);
1115 split_asm_constraints(stmt
->asm_clobbers
);
1120 __split_expr(stmt
->range_expression
);
1121 __split_expr(stmt
->range_low
);
1122 __split_expr(stmt
->range_high
);
1125 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1127 __process_post_op_stack();
1130 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1132 struct expression
*expr
;
1134 FOR_EACH_PTR(expr_list
, expr
) {
1135 expr_set_parent_expr(expr
, parent
);
1137 __process_post_op_stack();
1138 } END_FOR_EACH_PTR(expr
);
1141 static void split_sym(struct symbol
*sym
)
1145 if (!(sym
->namespace & NS_SYMBOL
))
1148 __split_stmt(sym
->stmt
);
1149 __split_expr(sym
->array_size
);
1150 split_symlist(sym
->arguments
);
1151 split_symlist(sym
->symbol_list
);
1152 __split_stmt(sym
->inline_stmt
);
1153 split_symlist(sym
->inline_symbol_list
);
1156 static void split_symlist(struct symbol_list
*sym_list
)
1160 FOR_EACH_PTR(sym_list
, sym
) {
1162 } END_FOR_EACH_PTR(sym
);
1165 typedef void (fake_cb
)(struct expression
*expr
);
1167 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1169 struct symbol
*type
, *tmp
;
1175 name
= member
->name
;
1177 type
= get_type(expr
);
1178 if (!type
|| type
->type
!= SYM_STRUCT
)
1182 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1186 if (strcmp(name
, tmp
->ident
->name
) == 0)
1188 } END_FOR_EACH_PTR(tmp
);
1192 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1194 struct symbol
*type
, *member
;
1197 type
= get_type(expr
);
1198 if (!type
|| type
->type
!= SYM_STRUCT
)
1201 FOR_EACH_PTR(type
->symbol_list
, member
) {
1203 return member
->ident
;
1205 } END_FOR_EACH_PTR(member
);
1209 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1211 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1213 struct expression
*edge_member
, *assign
;
1214 struct symbol
*base
= get_real_base_type(member
);
1218 expr
= member_expression(expr
, '.', member
->ident
);
1220 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1221 struct symbol
*type
;
1223 type
= get_real_base_type(tmp
);
1227 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1228 if (get_extra_state(edge_member
))
1231 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1232 set_inner_struct_members(expr
, tmp
);
1239 assign
= assign_expression(edge_member
, zero_expr());
1240 __split_expr(assign
);
1241 } END_FOR_EACH_PTR(tmp
);
1246 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1249 struct expression
*member
= NULL
;
1250 struct expression
*assign
;
1253 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1254 expr
= strip_expr(expr
->unop
);
1258 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1259 type
= get_real_base_type(tmp
);
1264 member
= member_expression(expr
, op
, tmp
->ident
);
1265 if (get_extra_state(member
))
1269 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1270 set_inner_struct_members(expr
, tmp
);
1273 if (type
->type
== SYM_ARRAY
)
1278 assign
= assign_expression(member
, zero_expr());
1279 __split_expr(assign
);
1280 } END_FOR_EACH_PTR(tmp
);
1283 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1285 struct expression
*deref
, *assign
, *tmp
, *right
;
1286 struct symbol
*struct_type
, *type
;
1287 struct ident
*member
;
1290 struct_type
= get_type(symbol
);
1292 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1296 * We're parsing an initializer that could look something like this:
1297 * struct foo foo = {
1299 * .whatever.xxx = 11,
1303 * So what we have here is a list with 42, .whatever, and .zzz. We need
1304 * to break it up into left and right sides of the assignments.
1308 FOR_EACH_PTR(members
, tmp
) {
1310 if (tmp
->type
== EXPR_IDENTIFIER
) {
1311 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1312 while (tmp
->type
== EXPR_IDENTIFIER
) {
1313 member
= tmp
->expr_ident
;
1314 tmp
= tmp
->ident_expression
;
1316 deref
= member_expression(deref
, '.', member
);
1318 deref
= member_expression(symbol
, '.', member
);
1321 member
= number_to_member(symbol
, member_idx
);
1322 deref
= member_expression(symbol
, '.', member
);
1326 if (right
->type
== EXPR_INITIALIZER
) {
1327 type
= get_type(deref
);
1328 if (type
&& type
->type
== SYM_ARRAY
)
1329 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1331 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1333 assign
= assign_expression(deref
, right
);
1336 } END_FOR_EACH_PTR(tmp
);
1338 set_unset_to_zero(struct_type
, symbol
);
1341 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1343 fake_member_assigns_helper(symbol_expression(sym
),
1344 sym
->initializer
->expr_list
, fake_cb
);
1347 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1349 struct expression
*offset
, *binop
, *assign
, *tmp
;
1350 struct symbol
*type
;
1353 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1357 FOR_EACH_PTR(expr_list
, tmp
) {
1358 if (tmp
->type
== EXPR_INDEX
) {
1359 if (tmp
->idx_from
!= tmp
->idx_to
)
1361 idx
= tmp
->idx_from
;
1362 if (!tmp
->idx_expression
)
1364 tmp
= tmp
->idx_expression
;
1366 offset
= value_expr(idx
);
1367 binop
= array_element_expression(array
, offset
);
1368 if (tmp
->type
== EXPR_INITIALIZER
) {
1369 type
= get_type(binop
);
1370 if (type
&& type
->type
== SYM_ARRAY
)
1371 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1373 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1375 assign
= assign_expression(binop
, tmp
);
1380 } END_FOR_EACH_PTR(tmp
);
1383 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1385 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1388 static void fake_assign_expr(struct symbol
*sym
)
1390 struct expression
*assign
, *symbol
;
1392 symbol
= symbol_expression(sym
);
1393 assign
= assign_expression(symbol
, sym
->initializer
);
1394 __split_expr(assign
);
1397 static void call_split_expr(struct expression
*expr
)
1402 static void do_initializer_stuff(struct symbol
*sym
)
1404 if (!sym
->initializer
)
1407 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1408 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1409 fake_element_assigns(sym
, call_split_expr
);
1411 fake_member_assigns(sym
, call_split_expr
);
1413 fake_assign_expr(sym
);
1417 static void split_declaration(struct symbol_list
*sym_list
)
1421 FOR_EACH_PTR(sym_list
, sym
) {
1422 __pass_to_client(sym
, DECLARATION_HOOK
);
1423 do_initializer_stuff(sym
);
1425 } END_FOR_EACH_PTR(sym
);
1428 static void call_global_assign_hooks(struct expression
*assign
)
1430 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1433 static void fake_global_assign(struct symbol
*sym
)
1435 struct expression
*assign
, *symbol
;
1437 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1438 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1439 fake_element_assigns(sym
, call_global_assign_hooks
);
1440 } else if (sym
->initializer
) {
1441 symbol
= symbol_expression(sym
);
1442 assign
= assign_expression(symbol
, sym
->initializer
);
1443 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1445 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1447 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1448 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1449 fake_member_assigns(sym
, call_global_assign_hooks
);
1450 } else if (sym
->initializer
) {
1451 symbol
= symbol_expression(sym
);
1452 assign
= assign_expression(symbol
, sym
->initializer
);
1453 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1455 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1458 symbol
= symbol_expression(sym
);
1459 if (sym
->initializer
)
1460 assign
= assign_expression(symbol
, sym
->initializer
);
1462 assign
= assign_expression(symbol
, zero_expr());
1463 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1467 static void start_function_definition(struct symbol
*sym
)
1469 __in_function_def
= 1;
1470 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1471 __in_function_def
= 0;
1472 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1476 static void split_function(struct symbol
*sym
)
1478 struct symbol
*base_type
= get_base_type(sym
);
1480 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1483 gettimeofday(&fn_start_time
, NULL
);
1486 cur_func
= sym
->ident
->name
;
1487 set_position(sym
->pos
);
1489 last_goto_statement_handled
= 0;
1490 sm_debug("new function: %s\n", cur_func
);
1492 if (option_two_passes
) {
1496 start_function_definition(sym
);
1497 __split_stmt(base_type
->stmt
);
1498 __split_stmt(base_type
->inline_stmt
);
1504 start_function_definition(sym
);
1505 __split_stmt(base_type
->stmt
);
1506 __split_stmt(base_type
->inline_stmt
);
1507 __pass_to_client(sym
, END_FUNC_HOOK
);
1508 if (need_delayed_scope_hooks())
1509 __call_scope_hooks();
1510 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1513 cur_func_sym
= NULL
;
1515 free_data_info_allocs();
1516 free_expression_stack(&switch_expr_stack
);
1517 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1518 __bail_on_rest_of_function
= 0;
1521 static void save_flow_state(void)
1523 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1524 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1525 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1527 __add_ptr_list(&backup
, big_statement_stack
, 0);
1528 __add_ptr_list(&backup
, big_expression_stack
, 0);
1529 __add_ptr_list(&backup
, big_condition_stack
, 0);
1530 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1532 __add_ptr_list(&backup
, cur_func_sym
, 0);
1534 __add_ptr_list(&backup
, __prev_stmt
, 0);
1535 __add_ptr_list(&backup
, __cur_stmt
, 0);
1536 __add_ptr_list(&backup
, __next_stmt
, 0);
1540 static void *pop_backup(void)
1544 ret
= last_ptr_list(backup
);
1545 delete_ptr_list_last(&backup
);
1549 static void restore_flow_state(void)
1551 __next_stmt
= pop_backup();
1552 __cur_stmt
= pop_backup();
1553 __prev_stmt
= pop_backup();
1555 cur_func_sym
= pop_backup();
1556 switch_expr_stack
= pop_backup();
1557 big_condition_stack
= pop_backup();
1558 big_expression_stack
= pop_backup();
1559 big_statement_stack
= pop_backup();
1560 final_pass
= PTR_INT(pop_backup()) >> 2;
1561 loop_count
= PTR_INT(pop_backup()) >> 2;
1562 loop_num
= PTR_INT(pop_backup()) >> 2;
1565 static void parse_inline(struct expression
*call
)
1567 struct symbol
*base_type
;
1568 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1569 struct timeval time_backup
= fn_start_time
;
1573 __pass_to_client(call
, INLINE_FN_START
);
1574 final_pass
= 0; /* don't print anything */
1577 base_type
= get_base_type(call
->fn
->symbol
);
1578 cur_func_sym
= call
->fn
->symbol
;
1579 if (call
->fn
->symbol
->ident
)
1580 cur_func
= call
->fn
->symbol
->ident
->name
;
1583 set_position(call
->fn
->symbol
->pos
);
1586 big_statement_stack
= NULL
;
1587 big_expression_stack
= NULL
;
1588 big_condition_stack
= NULL
;
1589 switch_expr_stack
= NULL
;
1591 sm_debug("inline function: %s\n", cur_func
);
1595 start_function_definition(call
->fn
->symbol
);
1596 __split_stmt(base_type
->stmt
);
1597 __split_stmt(base_type
->inline_stmt
);
1598 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1599 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1601 free_expression_stack(&switch_expr_stack
);
1602 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1606 restore_flow_state();
1607 fn_start_time
= time_backup
;
1608 cur_func
= cur_func_bak
;
1610 restore_all_states();
1611 set_position(call
->pos
);
1613 __pass_to_client(call
, INLINE_FN_END
);
1616 static struct symbol_list
*inlines_called
;
1617 static void add_inline_function(struct symbol
*sym
)
1619 static struct symbol_list
*already_added
;
1622 FOR_EACH_PTR(already_added
, tmp
) {
1625 } END_FOR_EACH_PTR(tmp
);
1627 add_ptr_list(&already_added
, sym
);
1628 add_ptr_list(&inlines_called
, sym
);
1631 static void process_inlines(void)
1635 FOR_EACH_PTR(inlines_called
, tmp
) {
1636 split_function(tmp
);
1637 } END_FOR_EACH_PTR(tmp
);
1638 free_ptr_list(&inlines_called
);
1641 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1645 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1648 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1650 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1652 } END_FOR_EACH_PTR_REVERSE(sym
);
1657 static void split_inlines_in_scope(struct symbol
*sym
)
1659 struct symbol
*base
;
1660 struct symbol_list
*scope_list
;
1663 scope_list
= sym
->scope
->symbols
;
1664 stream
= sym
->pos
.stream
;
1666 /* find the last static symbol in the file */
1667 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1668 if (sym
->pos
.stream
!= stream
)
1670 if (sym
->type
!= SYM_NODE
)
1672 base
= get_base_type(sym
);
1675 if (base
->type
!= SYM_FN
)
1677 if (!base
->inline_stmt
)
1679 add_inline_function(sym
);
1680 } END_FOR_EACH_PTR_REVERSE(sym
);
1685 static void split_inlines(struct symbol_list
*sym_list
)
1689 sym
= get_last_scoped_symbol(sym_list
, 0);
1691 split_inlines_in_scope(sym
);
1692 sym
= get_last_scoped_symbol(sym_list
, 1);
1694 split_inlines_in_scope(sym
);
1697 static struct stree
*clone_estates_perm(struct stree
*orig
)
1699 struct stree
*ret
= NULL
;
1700 struct sm_state
*tmp
;
1702 FOR_EACH_SM(orig
, tmp
) {
1703 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1704 } END_FOR_EACH_SM(tmp
);
1709 static bool interesting_function(struct symbol
*sym
)
1711 static int prev_stream
= -1;
1712 static bool prev_answer
;
1713 const char *filename
;
1717 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1720 if (sym
->pos
.stream
== prev_stream
)
1723 prev_stream
= sym
->pos
.stream
;
1724 prev_answer
= false;
1726 filename
= stream_name(sym
->pos
.stream
);
1727 len
= strlen(filename
);
1728 if (len
> 0 && filename
[len
- 1] == 'c')
1733 struct position last_pos
;
1734 static void split_c_file_functions(struct symbol_list
*sym_list
)
1739 FOR_EACH_PTR(sym_list
, sym
) {
1740 set_position(sym
->pos
);
1741 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1742 __pass_to_client(sym
, BASE_HOOK
);
1743 fake_global_assign(sym
);
1745 } END_FOR_EACH_PTR(sym
);
1746 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1749 FOR_EACH_PTR(sym_list
, sym
) {
1750 set_position(sym
->pos
);
1751 last_pos
= sym
->pos
;
1752 if (!interesting_function(sym
))
1754 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1755 split_function(sym
);
1758 last_pos
= sym
->pos
;
1759 } END_FOR_EACH_PTR(sym
);
1760 split_inlines(sym_list
);
1761 __pass_to_client(sym_list
, END_FILE_HOOK
);
1764 static int final_before_fake
;
1765 void init_fake_env(void)
1768 final_before_fake
= final_pass
;
1770 __push_fake_cur_stree();
1774 void end_fake_env(void)
1776 __pop_fake_cur_stree();
1779 final_pass
= final_before_fake
;
1782 static void open_output_files(char *base_file
)
1786 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1787 sm_outfd
= fopen(buf
, "w");
1789 printf("Error: Cannot open %s\n", buf
);
1796 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1797 sql_outfd
= fopen(buf
, "w");
1799 printf("Error: Cannot open %s\n", buf
);
1803 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1804 caller_info_fd
= fopen(buf
, "w");
1805 if (!caller_info_fd
) {
1806 printf("Error: Cannot open %s\n", buf
);
1811 void smatch(int argc
, char **argv
)
1813 struct string_list
*filelist
= NULL
;
1814 struct symbol_list
*sym_list
;
1815 struct timeval stop
, start
;
1817 gettimeofday(&start
, NULL
);
1820 printf("Usage: smatch [--debug] <filename.c>\n");
1823 sparse_initialize(argc
, argv
, &filelist
);
1824 set_valid_ptr_max();
1825 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1826 if (option_file_output
)
1827 open_output_files(base_file
);
1828 sym_list
= sparse_keep_tokens(base_file
);
1829 split_c_file_functions(sym_list
);
1830 } END_FOR_EACH_PTR_NOTAG(base_file
);
1832 gettimeofday(&stop
, NULL
);
1834 set_position(last_pos
);
1836 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);