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 static int in_fake_env
;
32 struct expression
*__inline_fn
;
34 static int __smatch_lineno
= 0;
36 static char *base_file
;
37 static const char *filename
;
38 static char *pathname
;
39 static char *full_filename
;
40 static char *cur_func
;
41 static unsigned int loop_count
;
42 static int last_goto_statement_handled
;
43 int __expr_stmt_count
;
44 int __in_function_def
;
45 static struct expression_list
*switch_expr_stack
= NULL
;
46 static struct expression_list
*post_op_stack
= NULL
;
48 static struct ptr_list
*backup
;
50 struct expression_list
*big_expression_stack
;
51 struct statement_list
*big_statement_stack
;
52 struct statement
*__prev_stmt
;
53 struct statement
*__cur_stmt
;
54 struct statement
*__next_stmt
;
55 int __in_pre_condition
= 0;
56 int __bail_on_rest_of_function
= 0;
57 static struct timeval fn_start_time
;
58 char *get_function(void) { return cur_func
; }
59 int get_lineno(void) { return __smatch_lineno
; }
60 int inside_loop(void) { return !!loop_count
; }
61 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
62 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
63 int in_expression_statement(void) { return !!__expr_stmt_count
; }
65 static void split_symlist(struct symbol_list
*sym_list
);
66 static void split_declaration(struct symbol_list
*sym_list
);
67 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
68 static void add_inline_function(struct symbol
*sym
);
69 static void parse_inline(struct expression
*expr
);
71 int option_assume_loops
= 0;
72 int option_two_passes
= 0;
73 struct symbol
*cur_func_sym
= NULL
;
74 struct stree
*global_states
;
76 long long valid_ptr_min
= 4096;
77 long long valid_ptr_max
= 2117777777;
78 sval_t valid_ptr_min_sval
= {
82 sval_t valid_ptr_max_sval
= {
84 {.value
= LONG_MAX
- 100000},
87 static void set_valid_ptr_max(void)
89 if (type_bits(&ptr_ctype
) == 32)
90 valid_ptr_max
= 2117777777;
91 else if (type_bits(&ptr_ctype
) == 64)
92 valid_ptr_max
= 2117777777777777777LL;
94 valid_ptr_max_sval
.value
= valid_ptr_max
;
97 int outside_of_function(void)
99 return cur_func_sym
== NULL
;
102 const char *get_filename(void)
106 if (option_full_path
)
107 return full_filename
;
111 const char *get_base_file(void)
116 static void set_position(struct position pos
)
119 static int prev_stream
= -1;
124 if (pos
.stream
== 0 && pos
.line
== 0)
127 __smatch_lineno
= pos
.line
;
129 if (pos
.stream
== prev_stream
)
132 filename
= stream_name(pos
.stream
);
135 pathname
= getcwd(NULL
, 0);
137 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
138 full_filename
= malloc(len
);
139 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
141 full_filename
= alloc_string(filename
);
146 int is_assigned_call(struct expression
*expr
)
148 struct expression
*parent
= expr_get_parent_expr(expr
);
151 parent
->type
== EXPR_ASSIGNMENT
&&
153 strip_expr(parent
->right
) == expr
)
159 static int is_inline_func(struct expression
*expr
)
161 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
163 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
168 static int is_noreturn_func(struct expression
*expr
)
170 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
172 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
177 int inlinable(struct expression
*expr
)
180 struct statement
*last_stmt
= NULL
;
182 if (__inline_fn
) /* don't nest */
185 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
187 if (is_no_inline_function(expr
->symbol
->ident
->name
))
189 sym
= get_base_type(expr
->symbol
);
190 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
191 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
193 if (sym
->stmt
->type
!= STMT_COMPOUND
)
195 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
197 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
198 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
200 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
202 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
208 /* the magic numbers in this function are pulled out of my bum. */
209 if (last_stmt
->pos
.line
> sym
->pos
.line
+ 20)
215 void __process_post_op_stack(void)
217 struct expression
*expr
;
219 FOR_EACH_PTR(post_op_stack
, expr
) {
220 __pass_to_client(expr
, OP_HOOK
);
221 } END_FOR_EACH_PTR(expr
);
223 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
226 static int handle_comma_assigns(struct expression
*expr
)
228 struct expression
*right
;
229 struct expression
*assign
;
231 right
= strip_expr(expr
->right
);
232 if (right
->type
!= EXPR_COMMA
)
235 __split_expr(right
->left
);
236 __process_post_op_stack();
238 assign
= assign_expression(expr
->left
, right
->right
);
239 __split_expr(assign
);
244 /* This is to handle *p++ = foo; assignments */
245 static int handle_postop_assigns(struct expression
*expr
)
247 struct expression
*left
, *fake_left
;
248 struct expression
*assign
;
250 left
= strip_expr(expr
->left
);
251 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
253 left
= strip_expr(left
->unop
);
254 if (left
->type
!= EXPR_POSTOP
)
257 fake_left
= deref_expression(strip_expr(left
->unop
));
258 assign
= assign_expression(fake_left
, expr
->right
);
260 __split_expr(assign
);
261 __split_expr(expr
->left
);
266 static int prev_expression_is_getting_address(struct expression
*expr
)
268 struct expression
*parent
;
271 parent
= expr_get_parent_expr(expr
);
275 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
277 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
279 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
288 void __split_expr(struct expression
*expr
)
293 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
295 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
297 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
300 push_expression(&big_expression_stack
, expr
);
301 set_position(expr
->pos
);
302 __pass_to_client(expr
, EXPR_HOOK
);
304 switch (expr
->type
) {
306 expr_set_parent_expr(expr
->unop
, expr
);
308 if (expr
->op
== '*' &&
309 !prev_expression_is_getting_address(expr
))
310 __pass_to_client(expr
, DEREF_HOOK
);
311 __split_expr(expr
->unop
);
312 __pass_to_client(expr
, OP_HOOK
);
315 expr_set_parent_expr(expr
->unop
, expr
);
317 __split_expr(expr
->unop
);
318 push_expression(&post_op_stack
, expr
);
322 if (expr
->statement
&& !expr
->statement
) {
323 stmt_set_parent_stmt(expr
->statement
,
324 last_ptr_list((struct ptr_list
*)big_statement_stack
));
326 __split_stmt(expr
->statement
);
331 expr_set_parent_expr(expr
->left
, expr
);
332 expr_set_parent_expr(expr
->right
, expr
);
334 __pass_to_client(expr
, LOGIC_HOOK
);
335 __handle_logic(expr
);
338 expr_set_parent_expr(expr
->left
, expr
);
339 expr_set_parent_expr(expr
->right
, expr
);
341 __pass_to_client(expr
, BINOP_HOOK
);
343 expr_set_parent_expr(expr
->left
, expr
);
344 expr_set_parent_expr(expr
->right
, expr
);
346 __split_expr(expr
->left
);
347 __process_post_op_stack();
348 __split_expr(expr
->right
);
350 case EXPR_ASSIGNMENT
: {
351 struct expression
*right
;
353 expr_set_parent_expr(expr
->left
, expr
);
354 expr_set_parent_expr(expr
->right
, expr
);
356 right
= strip_expr(expr
->right
);
360 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
363 if (__handle_condition_assigns(expr
))
365 /* foo = (x < 5 ? foo : 5); */
366 if (__handle_select_assigns(expr
))
368 /* foo = ({frob(); frob(); frob(); 1;}) */
369 if (__handle_expr_statement_assigns(expr
))
372 if (handle_comma_assigns(expr
))
374 if (handle_postop_assigns(expr
))
377 __split_expr(expr
->right
);
378 if (outside_of_function())
379 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
381 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
383 __fake_struct_member_assignments(expr
);
385 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
386 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
388 if (get_macro_name(right
->pos
) &&
389 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
390 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
392 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
394 __split_expr(expr
->left
);
398 expr_set_parent_expr(expr
->deref
, expr
);
400 __pass_to_client(expr
, DEREF_HOOK
);
401 __split_expr(expr
->deref
);
404 expr_set_parent_expr(expr
->base
, expr
);
406 __split_expr(expr
->base
);
409 case EXPR_FORCE_CAST
:
410 expr_set_parent_expr(expr
->cast_expression
, expr
);
412 __pass_to_client(expr
, CAST_HOOK
);
413 __split_expr(expr
->cast_expression
);
416 if (expr
->cast_expression
)
417 __pass_to_client(strip_parens(expr
->cast_expression
),
422 evaluate_expression(expr
);
424 case EXPR_CONDITIONAL
:
426 expr_set_parent_expr(expr
->conditional
, expr
);
427 expr_set_parent_expr(expr
->cond_true
, expr
);
428 expr_set_parent_expr(expr
->cond_false
, expr
);
430 if (known_condition_true(expr
->conditional
)) {
431 __split_expr(expr
->cond_true
);
434 if (known_condition_false(expr
->conditional
)) {
435 __split_expr(expr
->cond_false
);
438 __pass_to_client(expr
, SELECT_HOOK
);
439 __split_whole_condition(expr
->conditional
);
440 __split_expr(expr
->cond_true
);
441 __push_true_states();
442 __use_false_states();
443 __split_expr(expr
->cond_false
);
444 __merge_true_states();
447 expr_set_parent_expr(expr
->fn
, expr
);
449 if (sym_name_is("__builtin_constant_p", expr
->fn
))
451 split_expr_list(expr
->args
, expr
);
452 __split_expr(expr
->fn
);
453 if (is_inline_func(expr
->fn
))
454 add_inline_function(expr
->fn
->symbol
);
455 if (inlinable(expr
->fn
))
457 __process_post_op_stack();
458 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
460 if (inlinable(expr
->fn
)) {
463 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
464 if (is_noreturn_func(expr
->fn
))
467 case EXPR_INITIALIZER
:
468 split_expr_list(expr
->expr_list
, expr
);
470 case EXPR_IDENTIFIER
:
471 expr_set_parent_expr(expr
->ident_expression
, expr
);
472 __split_expr(expr
->ident_expression
);
475 expr_set_parent_expr(expr
->idx_expression
, expr
);
476 __split_expr(expr
->idx_expression
);
479 expr_set_parent_expr(expr
->init_expr
, expr
);
480 __split_expr(expr
->init_expr
);
483 __pass_to_client(expr
, SYM_HOOK
);
486 __pass_to_client(expr
, STRING_HOOK
);
491 pop_expression(&big_expression_stack
);
494 static int is_forever_loop(struct statement
*stmt
)
496 struct expression
*expr
;
499 expr
= strip_expr(stmt
->iterator_pre_condition
);
501 expr
= stmt
->iterator_post_condition
;
503 /* this is a for(;;) loop... */
507 if (get_value(expr
, &sval
) && sval
.value
!= 0)
514 static char *get_loop_name(int num
)
518 snprintf(buf
, 255, "-loop%d", num
);
520 return alloc_sname(buf
);
524 * Pre Loops are while and for loops.
526 static void handle_pre_loop(struct statement
*stmt
)
528 int once_through
; /* we go through the loop at least once */
529 struct sm_state
*extra_sm
= NULL
;
532 struct stree
*stree
= NULL
;
533 struct sm_state
*sm
= NULL
;
535 loop_name
= get_loop_name(loop_num
);
538 __split_stmt(stmt
->iterator_pre_statement
);
539 __prev_stmt
= stmt
->iterator_pre_statement
;
541 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
547 __merge_gotos(loop_name
, NULL
);
549 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
550 __in_pre_condition
++;
551 __pass_to_client(stmt
, PRELOOP_HOOK
);
552 __split_whole_condition(stmt
->iterator_pre_condition
);
553 __in_pre_condition
--;
554 FOR_EACH_SM(stree
, sm
) {
555 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
556 } END_FOR_EACH_SM(sm
);
559 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
561 if (option_assume_loops
)
564 __split_stmt(stmt
->iterator_statement
);
565 if (is_forever_loop(stmt
)) {
567 __save_gotos(loop_name
, NULL
);
569 __push_fake_cur_stree();
570 __split_stmt(stmt
->iterator_post_statement
);
571 stree
= __pop_fake_cur_stree();
573 __discard_false_states();
576 if (!__path_is_null())
577 __merge_stree_into_cur(stree
);
581 unchanged
= __iterator_unchanged(extra_sm
);
582 __split_stmt(stmt
->iterator_post_statement
);
583 __prev_stmt
= stmt
->iterator_post_statement
;
586 __save_gotos(loop_name
, NULL
);
587 __in_pre_condition
++;
588 __split_whole_condition(stmt
->iterator_pre_condition
);
589 __in_pre_condition
--;
591 __merge_false_states();
593 __discard_false_states();
595 __merge_false_states();
597 if (extra_sm
&& unchanged
)
598 __extra_pre_loop_hook_after(extra_sm
,
599 stmt
->iterator_post_statement
,
600 stmt
->iterator_pre_condition
);
607 * Post loops are do {} while();
609 static void handle_post_loop(struct statement
*stmt
)
613 loop_name
= get_loop_name(loop_num
);
619 __merge_gotos(loop_name
, NULL
);
620 __split_stmt(stmt
->iterator_statement
);
622 if (!is_zero(stmt
->iterator_post_condition
))
623 __save_gotos(loop_name
, NULL
);
625 if (is_forever_loop(stmt
)) {
628 __split_whole_condition(stmt
->iterator_post_condition
);
629 __use_false_states();
635 static int empty_statement(struct statement
*stmt
)
639 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
644 static int last_stmt_on_same_line(void)
646 struct statement
*stmt
;
649 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
652 if (stmt
->pos
.line
== get_lineno())
655 } END_FOR_EACH_PTR_REVERSE(stmt
);
659 static void split_asm_constraints(struct expression_list
*expr_list
)
661 struct expression
*expr
;
664 FOR_EACH_PTR(expr_list
, expr
) {
666 case 0: /* identifier */
667 case 1: /* constraint */
670 case 2: /* expression */
675 } END_FOR_EACH_PTR(expr
);
678 static int is_case_val(struct statement
*stmt
, sval_t sval
)
682 if (stmt
->type
!= STMT_CASE
)
684 if (!stmt
->case_expression
) {
688 if (!get_value(stmt
->case_expression
, &case_sval
))
690 if (case_sval
.value
== sval
.value
)
695 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
696 struct expression
*case_expr
,
697 struct expression
*case_to
)
700 struct range_list
*rl
= NULL
;
701 struct symbol
*switch_type
;
703 switch_type
= get_type(switch_expr
);
704 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
705 start
= sval_cast(switch_type
, start
);
706 end
= sval_cast(switch_type
, end
);
707 add_range(&rl
, start
, end
);
708 } else if (get_value(case_expr
, &start
)) {
709 start
= sval_cast(switch_type
, start
);
710 add_range(&rl
, start
, start
);
716 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
718 struct statement
*tmp
;
719 struct range_list
*rl
;
721 __split_expr(stmt
->switch_expression
);
722 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
724 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
725 __save_switch_states(top_expression(switch_expr_stack
));
730 stmt
= stmt
->switch_statement
;
732 __push_scope_hooks();
733 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
734 __smatch_lineno
= tmp
->pos
.line
;
735 if (is_case_val(tmp
, sval
)) {
736 rl
= alloc_rl(sval
, sval
);
737 __merge_switches(top_expression(switch_expr_stack
), rl
);
738 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
740 if (__path_is_null())
743 if (__path_is_null()) {
747 } END_FOR_EACH_PTR(tmp
);
749 __call_scope_hooks();
750 if (!__pop_default())
751 __merge_switches(top_expression(switch_expr_stack
), NULL
);
752 __discard_switches();
754 pop_expression(&switch_expr_stack
);
757 static void split_case(struct statement
*stmt
)
759 struct range_list
*rl
= NULL
;
761 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
762 expr_set_parent_stmt(stmt
->case_to
, stmt
);
764 rl
= get_case_rl(top_expression(switch_expr_stack
),
765 stmt
->case_expression
, stmt
->case_to
);
766 while (stmt
->case_statement
->type
== STMT_CASE
) {
767 struct range_list
*tmp
;
769 tmp
= get_case_rl(top_expression(switch_expr_stack
),
770 stmt
->case_statement
->case_expression
,
771 stmt
->case_statement
->case_to
);
774 rl
= rl_union(rl
, tmp
);
775 if (!stmt
->case_expression
)
777 stmt
= stmt
->case_statement
;
780 __merge_switches(top_expression(switch_expr_stack
), rl
);
782 if (!stmt
->case_expression
)
784 __split_stmt(stmt
->case_statement
);
787 int time_parsing_function(void)
789 return ms_since(&fn_start_time
) / 1000;
792 static int taking_too_long(void)
794 if (time_parsing_function() > 60 * 5) /* five minutes */
799 static int is_last_stmt(struct statement
*cur_stmt
)
801 struct symbol
*fn
= get_base_type(cur_func_sym
);
802 struct statement
*stmt
;
808 stmt
= fn
->inline_stmt
;
809 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
811 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
812 if (stmt
&& stmt
->type
== STMT_LABEL
)
813 stmt
= stmt
->label_statement
;
814 if (stmt
== cur_stmt
)
819 static void handle_backward_goto(struct statement
*goto_stmt
)
821 const char *goto_name
, *label_name
;
822 struct statement
*func_stmt
;
823 struct symbol
*base_type
= get_base_type(cur_func_sym
);
824 struct statement
*tmp
;
829 if (last_goto_statement_handled
)
831 last_goto_statement_handled
= 1;
833 if (!goto_stmt
->goto_label
||
834 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
835 !goto_stmt
->goto_label
->ident
)
837 goto_name
= goto_stmt
->goto_label
->ident
->name
;
839 func_stmt
= base_type
->stmt
;
841 func_stmt
= base_type
->inline_stmt
;
844 if (func_stmt
->type
!= STMT_COMPOUND
)
847 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
849 if (tmp
->type
!= STMT_LABEL
)
851 if (!tmp
->label_identifier
||
852 tmp
->label_identifier
->type
!= SYM_LABEL
||
853 !tmp
->label_identifier
->ident
)
855 label_name
= tmp
->label_identifier
->ident
->name
;
856 if (strcmp(goto_name
, label_name
) != 0)
861 } END_FOR_EACH_PTR(tmp
);
864 static void fake_a_return(void)
866 struct symbol
*return_type
;
871 return_type
= get_real_base_type(cur_func_sym
);
872 return_type
= get_real_base_type(return_type
);
873 if (return_type
!= &void_ctype
) {
874 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
879 static void fake_an_empty_default(struct position pos
)
881 static struct statement none
= {};
884 none
.type
= STMT_NONE
;
885 __merge_switches(top_expression(switch_expr_stack
), NULL
);
889 static void split_compound(struct statement
*stmt
)
891 struct statement
*prev
= NULL
;
892 struct statement
*cur
= NULL
;
893 struct statement
*next
;
895 __push_scope_hooks();
897 FOR_EACH_PTR(stmt
->stmts
, next
) {
898 /* just set them all ahead of time */
899 stmt_set_parent_stmt(next
, stmt
);
909 } END_FOR_EACH_PTR(next
);
918 * For function scope, then delay calling the scope hooks until the
919 * end of function hooks can run. I'm not positive this is the right
922 if (!is_last_stmt(cur
))
923 __call_scope_hooks();
927 * This is a hack, work around for detecting empty functions.
929 static int need_delayed_scope_hooks(void)
931 struct symbol
*fn
= get_base_type(cur_func_sym
);
932 struct statement
*stmt
;
938 stmt
= fn
->inline_stmt
;
939 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
944 void __split_label_stmt(struct statement
*stmt
)
946 if (stmt
->label_identifier
&&
947 stmt
->label_identifier
->type
== SYM_LABEL
&&
948 stmt
->label_identifier
->ident
) {
949 loop_count
|= 0x0800000;
950 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
954 static void find_asm_gotos(struct statement
*stmt
)
958 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
959 __save_gotos(sym
->ident
->name
, sym
);
960 } END_FOR_EACH_PTR(sym
);
963 void __split_stmt(struct statement
*stmt
)
970 if (__bail_on_rest_of_function
)
973 if (out_of_memory() || taking_too_long()) {
976 gettimeofday(&stop
, NULL
);
978 __bail_on_rest_of_function
= 1;
980 sm_msg("Function too hairy. Giving up. %lu seconds",
981 stop
.tv_sec
- fn_start_time
.tv_sec
);
983 final_pass
= 0; /* turn off sm_msg() from here */
987 add_ptr_list(&big_statement_stack
, stmt
);
988 free_expression_stack(&big_expression_stack
);
989 set_position(stmt
->pos
);
990 __pass_to_client(stmt
, STMT_HOOK
);
992 switch (stmt
->type
) {
993 case STMT_DECLARATION
:
994 split_declaration(stmt
->declaration
);
997 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
999 __split_expr(stmt
->ret_value
);
1000 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1001 __process_post_op_stack();
1004 case STMT_EXPRESSION
:
1005 expr_set_parent_stmt(stmt
->expression
, stmt
);
1006 expr_set_parent_stmt(stmt
->context
, stmt
);
1008 __split_expr(stmt
->expression
);
1011 split_compound(stmt
);
1014 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1015 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1016 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1018 if (known_condition_true(stmt
->if_conditional
)) {
1019 __split_stmt(stmt
->if_true
);
1022 if (known_condition_false(stmt
->if_conditional
)) {
1023 __split_stmt(stmt
->if_false
);
1026 __split_whole_condition(stmt
->if_conditional
);
1027 __split_stmt(stmt
->if_true
);
1028 if (empty_statement(stmt
->if_true
) &&
1029 last_stmt_on_same_line() &&
1030 !get_macro_name(stmt
->if_true
->pos
))
1031 sm_msg("warn: if();");
1032 __push_true_states();
1033 __use_false_states();
1034 __split_stmt(stmt
->if_false
);
1035 __merge_true_states();
1038 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1039 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1040 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1041 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1042 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1044 if (stmt
->iterator_pre_condition
)
1045 handle_pre_loop(stmt
);
1046 else if (stmt
->iterator_post_condition
)
1047 handle_post_loop(stmt
);
1049 // these are for(;;) type loops.
1050 handle_pre_loop(stmt
);
1054 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1055 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1057 if (get_value(stmt
->switch_expression
, &sval
)) {
1058 split_known_switch(stmt
, sval
);
1061 __split_expr(stmt
->switch_expression
);
1062 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1063 __save_switch_states(top_expression(switch_expr_stack
));
1067 __split_stmt(stmt
->switch_statement
);
1068 if (!__pop_default())
1069 fake_an_empty_default(stmt
->pos
);
1070 __discard_switches();
1072 pop_expression(&switch_expr_stack
);
1078 __split_label_stmt(stmt
);
1079 __split_stmt(stmt
->label_statement
);
1082 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1084 __split_expr(stmt
->goto_expression
);
1085 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1086 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1088 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1090 __process_continues();
1092 } else if (stmt
->goto_label
&&
1093 stmt
->goto_label
->type
== SYM_LABEL
&&
1094 stmt
->goto_label
->ident
) {
1095 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1098 if (is_last_stmt(stmt
))
1099 handle_backward_goto(stmt
);
1104 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1106 find_asm_gotos(stmt
);
1107 __pass_to_client(stmt
, ASM_HOOK
);
1108 __split_expr(stmt
->asm_string
);
1109 split_asm_constraints(stmt
->asm_outputs
);
1110 split_asm_constraints(stmt
->asm_inputs
);
1111 split_asm_constraints(stmt
->asm_clobbers
);
1116 __split_expr(stmt
->range_expression
);
1117 __split_expr(stmt
->range_low
);
1118 __split_expr(stmt
->range_high
);
1121 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1123 __process_post_op_stack();
1126 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1128 struct expression
*expr
;
1130 FOR_EACH_PTR(expr_list
, expr
) {
1131 expr_set_parent_expr(expr
, parent
);
1133 __process_post_op_stack();
1134 } END_FOR_EACH_PTR(expr
);
1137 static void split_sym(struct symbol
*sym
)
1141 if (!(sym
->namespace & NS_SYMBOL
))
1144 __split_stmt(sym
->stmt
);
1145 __split_expr(sym
->array_size
);
1146 split_symlist(sym
->arguments
);
1147 split_symlist(sym
->symbol_list
);
1148 __split_stmt(sym
->inline_stmt
);
1149 split_symlist(sym
->inline_symbol_list
);
1152 static void split_symlist(struct symbol_list
*sym_list
)
1156 FOR_EACH_PTR(sym_list
, sym
) {
1158 } END_FOR_EACH_PTR(sym
);
1161 typedef void (fake_cb
)(struct expression
*expr
);
1163 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1165 struct symbol
*type
, *tmp
;
1171 name
= member
->name
;
1173 type
= get_type(expr
);
1174 if (!type
|| type
->type
!= SYM_STRUCT
)
1178 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1182 if (strcmp(name
, tmp
->ident
->name
) == 0)
1184 } END_FOR_EACH_PTR(tmp
);
1188 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1190 struct symbol
*type
, *member
;
1193 type
= get_type(expr
);
1194 if (!type
|| type
->type
!= SYM_STRUCT
)
1197 FOR_EACH_PTR(type
->symbol_list
, member
) {
1199 return member
->ident
;
1201 } END_FOR_EACH_PTR(member
);
1205 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1207 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1209 struct expression
*edge_member
, *assign
;
1210 struct symbol
*base
= get_real_base_type(member
);
1214 expr
= member_expression(expr
, '.', member
->ident
);
1216 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1217 struct symbol
*type
;
1219 type
= get_real_base_type(tmp
);
1223 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1224 if (get_extra_state(edge_member
))
1227 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1228 set_inner_struct_members(expr
, tmp
);
1235 assign
= assign_expression(edge_member
, zero_expr());
1236 __split_expr(assign
);
1237 } END_FOR_EACH_PTR(tmp
);
1242 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1245 struct expression
*member
= NULL
;
1246 struct expression
*assign
;
1249 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1250 expr
= strip_expr(expr
->unop
);
1254 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1255 type
= get_real_base_type(tmp
);
1260 member
= member_expression(expr
, op
, tmp
->ident
);
1261 if (get_extra_state(member
))
1265 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1266 set_inner_struct_members(expr
, tmp
);
1269 if (type
->type
== SYM_ARRAY
)
1274 assign
= assign_expression(member
, zero_expr());
1275 __split_expr(assign
);
1276 } END_FOR_EACH_PTR(tmp
);
1279 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1281 struct expression
*deref
, *assign
, *tmp
, *right
;
1282 struct symbol
*struct_type
, *type
;
1283 struct ident
*member
;
1286 struct_type
= get_type(symbol
);
1288 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1292 * We're parsing an initializer that could look something like this:
1293 * struct foo foo = {
1295 * .whatever.xxx = 11,
1299 * So what we have here is a list with 42, .whatever, and .zzz. We need
1300 * to break it up into left and right sides of the assignments.
1304 FOR_EACH_PTR(members
, tmp
) {
1306 if (tmp
->type
== EXPR_IDENTIFIER
) {
1307 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1308 while (tmp
->type
== EXPR_IDENTIFIER
) {
1309 member
= tmp
->expr_ident
;
1310 tmp
= tmp
->ident_expression
;
1312 deref
= member_expression(deref
, '.', member
);
1314 deref
= member_expression(symbol
, '.', member
);
1317 member
= number_to_member(symbol
, member_idx
);
1318 deref
= member_expression(symbol
, '.', member
);
1322 if (right
->type
== EXPR_INITIALIZER
) {
1323 type
= get_type(deref
);
1324 if (type
&& type
->type
== SYM_ARRAY
)
1325 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1327 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1329 assign
= assign_expression(deref
, right
);
1332 } END_FOR_EACH_PTR(tmp
);
1334 set_unset_to_zero(struct_type
, symbol
);
1337 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1339 fake_member_assigns_helper(symbol_expression(sym
),
1340 sym
->initializer
->expr_list
, fake_cb
);
1343 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1345 struct expression
*offset
, *binop
, *assign
, *tmp
;
1346 struct symbol
*type
;
1349 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1353 FOR_EACH_PTR(expr_list
, tmp
) {
1354 if (tmp
->type
== EXPR_INDEX
) {
1355 if (tmp
->idx_from
!= tmp
->idx_to
)
1357 idx
= tmp
->idx_from
;
1358 if (!tmp
->idx_expression
)
1360 tmp
= tmp
->idx_expression
;
1362 offset
= value_expr(idx
);
1363 binop
= array_element_expression(array
, offset
);
1364 if (tmp
->type
== EXPR_INITIALIZER
) {
1365 type
= get_type(binop
);
1366 if (type
&& type
->type
== SYM_ARRAY
)
1367 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1369 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1371 assign
= assign_expression(binop
, tmp
);
1376 } END_FOR_EACH_PTR(tmp
);
1379 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1381 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1384 static void fake_assign_expr(struct symbol
*sym
)
1386 struct expression
*assign
, *symbol
;
1388 symbol
= symbol_expression(sym
);
1389 assign
= assign_expression(symbol
, sym
->initializer
);
1390 __split_expr(assign
);
1393 static void call_split_expr(struct expression
*expr
)
1398 static void do_initializer_stuff(struct symbol
*sym
)
1400 if (!sym
->initializer
)
1403 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1404 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1405 fake_element_assigns(sym
, call_split_expr
);
1407 fake_member_assigns(sym
, call_split_expr
);
1409 fake_assign_expr(sym
);
1413 static void split_declaration(struct symbol_list
*sym_list
)
1417 FOR_EACH_PTR(sym_list
, sym
) {
1418 __pass_to_client(sym
, DECLARATION_HOOK
);
1419 do_initializer_stuff(sym
);
1421 } END_FOR_EACH_PTR(sym
);
1424 static void call_global_assign_hooks(struct expression
*assign
)
1426 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1429 static void fake_global_assign(struct symbol
*sym
)
1431 struct expression
*assign
, *symbol
;
1433 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1434 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1435 fake_element_assigns(sym
, call_global_assign_hooks
);
1436 } else if (sym
->initializer
) {
1437 symbol
= symbol_expression(sym
);
1438 assign
= assign_expression(symbol
, sym
->initializer
);
1439 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1441 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1443 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1444 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1445 fake_member_assigns(sym
, call_global_assign_hooks
);
1446 } else if (sym
->initializer
) {
1447 symbol
= symbol_expression(sym
);
1448 assign
= assign_expression(symbol
, sym
->initializer
);
1449 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1451 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1454 symbol
= symbol_expression(sym
);
1455 if (sym
->initializer
)
1456 assign
= assign_expression(symbol
, sym
->initializer
);
1458 assign
= assign_expression(symbol
, zero_expr());
1459 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1463 static void start_function_definition(struct symbol
*sym
)
1465 __in_function_def
= 1;
1466 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1467 __in_function_def
= 0;
1468 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1472 static void split_function(struct symbol
*sym
)
1474 struct symbol
*base_type
= get_base_type(sym
);
1476 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1479 gettimeofday(&fn_start_time
, NULL
);
1482 cur_func
= sym
->ident
->name
;
1483 set_position(sym
->pos
);
1485 last_goto_statement_handled
= 0;
1486 sm_debug("new function: %s\n", cur_func
);
1488 if (option_two_passes
) {
1492 start_function_definition(sym
);
1493 __split_stmt(base_type
->stmt
);
1494 __split_stmt(base_type
->inline_stmt
);
1500 start_function_definition(sym
);
1501 __split_stmt(base_type
->stmt
);
1502 __split_stmt(base_type
->inline_stmt
);
1503 __pass_to_client(sym
, END_FUNC_HOOK
);
1504 if (need_delayed_scope_hooks())
1505 __call_scope_hooks();
1506 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1509 cur_func_sym
= NULL
;
1511 free_data_info_allocs();
1512 free_expression_stack(&switch_expr_stack
);
1513 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1514 __bail_on_rest_of_function
= 0;
1517 static void save_flow_state(void)
1519 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2), 0);
1520 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2), 0);
1521 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2), 0);
1523 __add_ptr_list(&backup
, big_statement_stack
, 0);
1524 __add_ptr_list(&backup
, big_expression_stack
, 0);
1525 __add_ptr_list(&backup
, big_condition_stack
, 0);
1526 __add_ptr_list(&backup
, switch_expr_stack
, 0);
1528 __add_ptr_list(&backup
, cur_func_sym
, 0);
1530 __add_ptr_list(&backup
, __prev_stmt
, 0);
1531 __add_ptr_list(&backup
, __cur_stmt
, 0);
1532 __add_ptr_list(&backup
, __next_stmt
, 0);
1536 static void *pop_backup(void)
1540 ret
= last_ptr_list(backup
);
1541 delete_ptr_list_last(&backup
);
1545 static void restore_flow_state(void)
1547 __next_stmt
= pop_backup();
1548 __cur_stmt
= pop_backup();
1549 __prev_stmt
= pop_backup();
1551 cur_func_sym
= pop_backup();
1552 switch_expr_stack
= pop_backup();
1553 big_condition_stack
= pop_backup();
1554 big_expression_stack
= pop_backup();
1555 big_statement_stack
= pop_backup();
1556 final_pass
= PTR_INT(pop_backup()) >> 2;
1557 loop_count
= PTR_INT(pop_backup()) >> 2;
1558 loop_num
= PTR_INT(pop_backup()) >> 2;
1561 static void parse_inline(struct expression
*call
)
1563 struct symbol
*base_type
;
1564 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1565 struct timeval time_backup
= fn_start_time
;
1569 __pass_to_client(call
, INLINE_FN_START
);
1570 final_pass
= 0; /* don't print anything */
1573 base_type
= get_base_type(call
->fn
->symbol
);
1574 cur_func_sym
= call
->fn
->symbol
;
1575 if (call
->fn
->symbol
->ident
)
1576 cur_func
= call
->fn
->symbol
->ident
->name
;
1579 set_position(call
->fn
->symbol
->pos
);
1582 big_statement_stack
= NULL
;
1583 big_expression_stack
= NULL
;
1584 big_condition_stack
= NULL
;
1585 switch_expr_stack
= NULL
;
1587 sm_debug("inline function: %s\n", cur_func
);
1590 start_function_definition(call
->fn
->symbol
);
1591 __split_stmt(base_type
->stmt
);
1592 __split_stmt(base_type
->inline_stmt
);
1593 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1594 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1596 free_expression_stack(&switch_expr_stack
);
1597 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1601 restore_flow_state();
1602 fn_start_time
= time_backup
;
1603 cur_func
= cur_func_bak
;
1605 restore_all_states();
1606 set_position(call
->pos
);
1608 __pass_to_client(call
, INLINE_FN_END
);
1611 static struct symbol_list
*inlines_called
;
1612 static void add_inline_function(struct symbol
*sym
)
1614 static struct symbol_list
*already_added
;
1617 FOR_EACH_PTR(already_added
, tmp
) {
1620 } END_FOR_EACH_PTR(tmp
);
1622 add_ptr_list(&already_added
, sym
);
1623 add_ptr_list(&inlines_called
, sym
);
1626 static void process_inlines(void)
1630 FOR_EACH_PTR(inlines_called
, tmp
) {
1631 split_function(tmp
);
1632 } END_FOR_EACH_PTR(tmp
);
1633 free_ptr_list(&inlines_called
);
1636 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1640 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1643 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1645 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1647 } END_FOR_EACH_PTR_REVERSE(sym
);
1652 static void split_inlines_in_scope(struct symbol
*sym
)
1654 struct symbol
*base
;
1655 struct symbol_list
*scope_list
;
1658 scope_list
= sym
->scope
->symbols
;
1659 stream
= sym
->pos
.stream
;
1661 /* find the last static symbol in the file */
1662 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1663 if (sym
->pos
.stream
!= stream
)
1665 if (sym
->type
!= SYM_NODE
)
1667 base
= get_base_type(sym
);
1670 if (base
->type
!= SYM_FN
)
1672 if (!base
->inline_stmt
)
1674 add_inline_function(sym
);
1675 } END_FOR_EACH_PTR_REVERSE(sym
);
1680 static void split_inlines(struct symbol_list
*sym_list
)
1684 sym
= get_last_scoped_symbol(sym_list
, 0);
1686 split_inlines_in_scope(sym
);
1687 sym
= get_last_scoped_symbol(sym_list
, 1);
1689 split_inlines_in_scope(sym
);
1692 static struct stree
*clone_estates_perm(struct stree
*orig
)
1694 struct stree
*ret
= NULL
;
1695 struct sm_state
*tmp
;
1697 FOR_EACH_SM(orig
, tmp
) {
1698 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1699 } END_FOR_EACH_SM(tmp
);
1704 static bool interesting_function(struct symbol
*sym
)
1706 static int prev_stream
= -1;
1707 static bool prev_answer
;
1708 const char *filename
;
1712 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
1715 if (sym
->pos
.stream
== prev_stream
)
1718 prev_stream
= sym
->pos
.stream
;
1719 prev_answer
= false;
1721 filename
= stream_name(sym
->pos
.stream
);
1722 len
= strlen(filename
);
1723 if (len
> 0 && filename
[len
- 1] == 'c')
1728 struct position last_pos
;
1729 static void split_c_file_functions(struct symbol_list
*sym_list
)
1734 FOR_EACH_PTR(sym_list
, sym
) {
1735 set_position(sym
->pos
);
1736 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1737 __pass_to_client(sym
, BASE_HOOK
);
1738 fake_global_assign(sym
);
1740 } END_FOR_EACH_PTR(sym
);
1741 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1744 FOR_EACH_PTR(sym_list
, sym
) {
1745 set_position(sym
->pos
);
1746 last_pos
= sym
->pos
;
1747 if (!interesting_function(sym
))
1749 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1750 split_function(sym
);
1753 last_pos
= sym
->pos
;
1754 } END_FOR_EACH_PTR(sym
);
1755 split_inlines(sym_list
);
1756 __pass_to_client(sym_list
, END_FILE_HOOK
);
1759 static int final_before_fake
;
1760 void init_fake_env(void)
1763 final_before_fake
= final_pass
;
1765 __push_fake_cur_stree();
1769 void end_fake_env(void)
1771 __pop_fake_cur_stree();
1774 final_pass
= final_before_fake
;
1777 static void open_output_files(char *base_file
)
1781 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1782 sm_outfd
= fopen(buf
, "w");
1784 printf("Error: Cannot open %s\n", buf
);
1791 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
1792 sql_outfd
= fopen(buf
, "w");
1794 printf("Error: Cannot open %s\n", buf
);
1798 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
1799 caller_info_fd
= fopen(buf
, "w");
1800 if (!caller_info_fd
) {
1801 printf("Error: Cannot open %s\n", buf
);
1806 void smatch(int argc
, char **argv
)
1808 struct string_list
*filelist
= NULL
;
1809 struct symbol_list
*sym_list
;
1810 struct timeval stop
, start
;
1812 gettimeofday(&start
, NULL
);
1815 printf("Usage: smatch [--debug] <filename.c>\n");
1818 sparse_initialize(argc
, argv
, &filelist
);
1819 set_valid_ptr_max();
1820 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1821 if (option_file_output
)
1822 open_output_files(base_file
);
1823 sym_list
= sparse_keep_tokens(base_file
);
1824 split_c_file_functions(sym_list
);
1825 } END_FOR_EACH_PTR_NOTAG(base_file
);
1827 gettimeofday(&stop
, NULL
);
1829 set_position(last_pos
);
1831 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);