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"
31 struct expression
*__inline_fn
;
33 static int __smatch_lineno
= 0;
35 static char *base_file
;
36 static const char *filename
;
37 static char *pathname
;
38 static char *full_filename
;
39 static char *cur_func
;
40 static unsigned int loop_count
;
41 static int last_goto_statement_handled
;
42 int __expr_stmt_count
;
43 int __in_function_def
;
44 static struct expression_list
*switch_expr_stack
= NULL
;
45 static struct expression_list
*post_op_stack
= NULL
;
47 struct expression_list
*big_expression_stack
;
48 struct statement_list
*big_statement_stack
;
49 struct statement
*__prev_stmt
;
50 struct statement
*__cur_stmt
;
51 struct statement
*__next_stmt
;
52 int __in_pre_condition
= 0;
53 int __bail_on_rest_of_function
= 0;
54 static struct timeval fn_start_time
;
55 char *get_function(void) { return cur_func
; }
56 int get_lineno(void) { return __smatch_lineno
; }
57 int inside_loop(void) { return !!loop_count
; }
58 int definitely_inside_loop(void) { return !!(loop_count
& ~0x80000000); }
59 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
60 int in_expression_statement(void) { return !!__expr_stmt_count
; }
62 static void split_symlist(struct symbol_list
*sym_list
);
63 static void split_declaration(struct symbol_list
*sym_list
);
64 static void split_expr_list(struct expression_list
*expr_list
);
65 static void add_inline_function(struct symbol
*sym
);
66 static void parse_inline(struct expression
*expr
);
68 int option_assume_loops
= 0;
69 int option_known_conditions
= 0;
70 int option_two_passes
= 0;
71 struct symbol
*cur_func_sym
= NULL
;
72 struct stree
*global_states
;
74 long long valid_ptr_min
= 4096;
75 long long valid_ptr_max
= 2117777777;
76 sval_t valid_ptr_min_sval
= {
80 sval_t valid_ptr_max_sval
= {
82 {.value
= LONG_MAX
- 100000},
85 static void set_valid_ptr_max(void)
87 if (type_bits(&ptr_ctype
) == 32)
88 valid_ptr_max
= 2117777777;
89 else if (type_bits(&ptr_ctype
) == 64)
90 valid_ptr_max
= 2117777777777777777LL;
92 valid_ptr_max_sval
.value
= valid_ptr_max
;
95 int outside_of_function(void)
97 return cur_func_sym
== NULL
;
100 const char *get_filename(void)
104 if (option_full_path
)
105 return full_filename
;
109 const char *get_base_file(void)
114 static void set_position(struct position pos
)
117 static int prev_stream
= -1;
119 if (pos
.stream
== 0 && pos
.line
== 0)
122 __smatch_lineno
= pos
.line
;
124 if (pos
.stream
== prev_stream
)
127 filename
= stream_name(pos
.stream
);
130 pathname
= getcwd(NULL
, 0);
132 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
133 full_filename
= malloc(len
);
134 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
136 full_filename
= alloc_string(filename
);
141 int is_assigned_call(struct expression
*expr
)
143 struct expression
*tmp
;
145 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
146 if (tmp
->type
== EXPR_ASSIGNMENT
&& tmp
->op
== '=' &&
147 strip_expr(tmp
->right
) == expr
)
149 if (tmp
->pos
.line
< expr
->pos
.line
)
151 } END_FOR_EACH_PTR_REVERSE(tmp
);
155 static int is_inline_func(struct expression
*expr
)
157 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
159 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
164 static int is_noreturn_func(struct expression
*expr
)
166 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
168 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
173 int inlinable(struct expression
*expr
)
176 struct statement
*last_stmt
= NULL
;
178 if (__inline_fn
) /* don't nest */
181 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
183 if (is_no_inline_function(expr
->symbol
->ident
->name
))
185 sym
= get_base_type(expr
->symbol
);
186 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
187 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
189 if (sym
->stmt
->type
!= STMT_COMPOUND
)
191 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
193 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
194 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
196 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
198 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
204 /* the magic numbers in this function are pulled out of my bum. */
205 if (last_stmt
->pos
.line
> sym
->pos
.line
+ 20)
211 void __process_post_op_stack(void)
213 struct expression
*expr
;
215 FOR_EACH_PTR(post_op_stack
, expr
) {
216 __pass_to_client(expr
, OP_HOOK
);
217 } END_FOR_EACH_PTR(expr
);
219 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
222 static int handle_comma_assigns(struct expression
*expr
)
224 struct expression
*right
;
225 struct expression
*assign
;
227 right
= strip_expr(expr
->right
);
228 if (right
->type
!= EXPR_COMMA
)
231 __split_expr(right
->left
);
232 __process_post_op_stack();
234 assign
= assign_expression(expr
->left
, right
->right
);
235 __split_expr(assign
);
240 void __split_expr(struct expression
*expr
)
245 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
247 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
249 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
252 push_expression(&big_expression_stack
, expr
);
253 set_position(expr
->pos
);
254 __pass_to_client(expr
, EXPR_HOOK
);
256 switch (expr
->type
) {
259 __pass_to_client(expr
, DEREF_HOOK
);
260 __split_expr(expr
->unop
);
261 __pass_to_client(expr
, OP_HOOK
);
264 __split_expr(expr
->unop
);
265 push_expression(&post_op_stack
, expr
);
269 __split_stmt(expr
->statement
);
274 __pass_to_client(expr
, LOGIC_HOOK
);
275 __handle_logic(expr
);
278 __pass_to_client(expr
, BINOP_HOOK
);
280 __split_expr(expr
->left
);
281 __process_post_op_stack();
282 __split_expr(expr
->right
);
284 case EXPR_ASSIGNMENT
: {
285 struct expression
*tmp
;
290 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
293 if (__handle_condition_assigns(expr
))
295 /* foo = (x < 5 ? foo : 5); */
296 if (__handle_select_assigns(expr
))
298 /* foo = ({frob(); frob(); frob(); 1;}) */
299 if (__handle_expr_statement_assigns(expr
))
302 if (handle_comma_assigns(expr
))
305 __split_expr(expr
->right
);
306 if (outside_of_function())
307 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
309 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
311 __fake_struct_member_assignments(expr
);
313 tmp
= strip_expr(expr
->right
);
314 if (expr
->op
== '=' && tmp
->type
== EXPR_CALL
) {
315 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
316 if (!is_fake_call(tmp
))
317 __pass_to_client(tmp
, FUNCTION_CALL_HOOK_AFTER
);
319 if (get_macro_name(tmp
->pos
) &&
320 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
321 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
322 __split_expr(expr
->left
);
326 __pass_to_client(expr
, DEREF_HOOK
);
327 __split_expr(expr
->deref
);
330 __split_expr(expr
->base
);
333 case EXPR_FORCE_CAST
:
334 __pass_to_client(expr
, CAST_HOOK
);
335 __split_expr(expr
->cast_expression
);
338 if (expr
->cast_expression
)
339 __pass_to_client(strip_parens(expr
->cast_expression
),
344 evaluate_expression(expr
);
346 case EXPR_CONDITIONAL
:
348 if (known_condition_true(expr
->conditional
)) {
349 __split_expr(expr
->cond_true
);
352 if (known_condition_false(expr
->conditional
)) {
353 __split_expr(expr
->cond_false
);
356 __pass_to_client(expr
, SELECT_HOOK
);
357 __split_whole_condition(expr
->conditional
);
358 __split_expr(expr
->cond_true
);
359 __push_true_states();
360 __use_false_states();
361 __split_expr(expr
->cond_false
);
362 __merge_true_states();
365 if (sym_name_is("__builtin_constant_p", expr
->fn
))
367 split_expr_list(expr
->args
);
368 __split_expr(expr
->fn
);
369 if (is_inline_func(expr
->fn
))
370 add_inline_function(expr
->fn
->symbol
);
371 if (inlinable(expr
->fn
))
373 __process_post_op_stack();
374 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
376 if (inlinable(expr
->fn
)) {
379 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
380 if (!is_assigned_call(expr
))
381 __pass_to_client(expr
, FUNCTION_CALL_HOOK_AFTER
);
382 if (is_noreturn_func(expr
->fn
))
385 case EXPR_INITIALIZER
:
386 split_expr_list(expr
->expr_list
);
388 case EXPR_IDENTIFIER
:
389 __split_expr(expr
->ident_expression
);
392 __split_expr(expr
->idx_expression
);
395 __split_expr(expr
->init_expr
);
398 __pass_to_client(expr
, SYM_HOOK
);
401 __pass_to_client(expr
, STRING_HOOK
);
406 pop_expression(&big_expression_stack
);
409 static int is_forever_loop(struct statement
*stmt
)
411 struct expression
*expr
;
413 expr
= strip_expr(stmt
->iterator_pre_condition
);
415 expr
= stmt
->iterator_post_condition
;
417 /* this is a for(;;) loop... */
421 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
428 static char *get_loop_name(int num
)
432 snprintf(buf
, 255, "-loop%d", num
);
434 return alloc_sname(buf
);
438 * Pre Loops are while and for loops.
440 static void handle_pre_loop(struct statement
*stmt
)
442 int once_through
; /* we go through the loop at least once */
443 struct sm_state
*extra_sm
= NULL
;
446 struct stree
*stree
= NULL
;
447 struct sm_state
*sm
= NULL
;
449 loop_name
= get_loop_name(loop_num
);
452 __split_stmt(stmt
->iterator_pre_statement
);
454 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
460 __merge_gotos(loop_name
);
462 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
463 __in_pre_condition
++;
464 __pass_to_client(stmt
, PRELOOP_HOOK
);
465 __split_whole_condition(stmt
->iterator_pre_condition
);
466 __in_pre_condition
--;
467 FOR_EACH_SM(stree
, sm
) {
468 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
469 } END_FOR_EACH_SM(sm
);
472 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
474 if (option_assume_loops
)
477 __split_stmt(stmt
->iterator_statement
);
478 __warn_on_silly_pre_loops();
479 if (is_forever_loop(stmt
)) {
481 __save_gotos(loop_name
);
483 __push_fake_cur_stree();
484 __split_stmt(stmt
->iterator_post_statement
);
485 stree
= __pop_fake_cur_stree();
487 __discard_false_states();
490 if (!__path_is_null())
491 __merge_stree_into_cur(stree
);
495 unchanged
= __iterator_unchanged(extra_sm
);
496 __split_stmt(stmt
->iterator_post_statement
);
497 __save_gotos(loop_name
);
498 __in_pre_condition
++;
499 __split_whole_condition(stmt
->iterator_pre_condition
);
500 __in_pre_condition
--;
502 __merge_false_states();
504 __discard_false_states();
506 __merge_false_states();
508 if (extra_sm
&& unchanged
)
509 __extra_pre_loop_hook_after(extra_sm
,
510 stmt
->iterator_post_statement
,
511 stmt
->iterator_pre_condition
);
518 * Post loops are do {} while();
520 static void handle_post_loop(struct statement
*stmt
)
524 loop_name
= get_loop_name(loop_num
);
530 __merge_gotos(loop_name
);
531 __split_stmt(stmt
->iterator_statement
);
533 if (!is_zero(stmt
->iterator_post_condition
))
534 __save_gotos(loop_name
);
536 if (is_forever_loop(stmt
)) {
539 __split_whole_condition(stmt
->iterator_post_condition
);
540 __use_false_states();
546 static int empty_statement(struct statement
*stmt
)
550 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
555 static int last_stmt_on_same_line(void)
557 struct statement
*stmt
;
560 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
563 if (stmt
->pos
.line
== get_lineno())
566 } END_FOR_EACH_PTR_REVERSE(stmt
);
570 static void split_asm_constraints(struct expression_list
*expr_list
)
572 struct expression
*expr
;
575 FOR_EACH_PTR(expr_list
, expr
) {
577 case 0: /* identifier */
578 case 1: /* constraint */
581 case 2: /* expression */
586 } END_FOR_EACH_PTR(expr
);
589 static int is_case_val(struct statement
*stmt
, sval_t sval
)
593 if (stmt
->type
!= STMT_CASE
)
595 if (!stmt
->case_expression
) {
599 if (!get_value(stmt
->case_expression
, &case_sval
))
601 if (case_sval
.value
== sval
.value
)
606 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
608 struct statement
*tmp
;
610 __split_expr(stmt
->switch_expression
);
612 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
613 __save_switch_states(top_expression(switch_expr_stack
));
618 stmt
= stmt
->switch_statement
;
620 __push_scope_hooks();
621 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
622 __smatch_lineno
= tmp
->pos
.line
;
623 if (is_case_val(tmp
, sval
)) {
624 __merge_switches(top_expression(switch_expr_stack
),
625 stmt
->case_expression
);
626 __pass_case_to_client(top_expression(switch_expr_stack
),
627 stmt
->case_expression
);
629 if (__path_is_null())
632 if (__path_is_null()) {
636 } END_FOR_EACH_PTR(tmp
);
638 __call_scope_hooks();
639 if (!__pop_default())
640 __merge_switches(top_expression(switch_expr_stack
),
642 __discard_switches();
644 pop_expression(&switch_expr_stack
);
647 static int taking_too_long(void)
651 ms
= ms_since(&fn_start_time
);
652 if (ms
> 1000 * 60 * 5) /* five minutes */
657 static int is_last_stmt(struct statement
*cur_stmt
)
659 struct symbol
*fn
= get_base_type(cur_func_sym
);
660 struct statement
*stmt
;
666 stmt
= fn
->inline_stmt
;
667 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
669 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
670 if (stmt
&& stmt
->type
== STMT_LABEL
)
671 stmt
= stmt
->label_statement
;
672 if (stmt
== cur_stmt
)
677 static void handle_backward_goto(struct statement
*goto_stmt
)
679 const char *goto_name
, *label_name
;
680 struct statement
*func_stmt
;
681 struct symbol
*base_type
= get_base_type(cur_func_sym
);
682 struct statement
*tmp
;
687 if (last_goto_statement_handled
)
689 last_goto_statement_handled
= 1;
691 if (!goto_stmt
->goto_label
||
692 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
693 !goto_stmt
->goto_label
->ident
)
695 goto_name
= goto_stmt
->goto_label
->ident
->name
;
697 func_stmt
= base_type
->stmt
;
699 func_stmt
= base_type
->inline_stmt
;
702 if (func_stmt
->type
!= STMT_COMPOUND
)
705 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
707 if (tmp
->type
!= STMT_LABEL
)
709 if (!tmp
->label_identifier
||
710 tmp
->label_identifier
->type
!= SYM_LABEL
||
711 !tmp
->label_identifier
->ident
)
713 label_name
= tmp
->label_identifier
->ident
->name
;
714 if (strcmp(goto_name
, label_name
) != 0)
719 } END_FOR_EACH_PTR(tmp
);
722 static void fake_a_return(void)
724 struct symbol
*return_type
;
729 return_type
= get_real_base_type(cur_func_sym
);
730 return_type
= get_real_base_type(return_type
);
731 if (return_type
!= &void_ctype
) {
732 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
736 __pass_to_client(cur_func_sym
, END_FUNC_HOOK
);
737 __pass_to_client(cur_func_sym
, AFTER_FUNC_HOOK
);
740 static void split_compound(struct statement
*stmt
)
742 struct statement
*prev
= NULL
;
743 struct statement
*cur
= NULL
;
744 struct statement
*next
;
746 __push_scope_hooks();
748 FOR_EACH_PTR(stmt
->stmts
, next
) {
757 } END_FOR_EACH_PTR(next
);
765 __call_scope_hooks();
768 void __split_stmt(struct statement
*stmt
)
775 if (__bail_on_rest_of_function
|| out_of_memory() || taking_too_long()) {
776 static char *printed
= NULL
;
778 __bail_on_rest_of_function
= 1;
779 if (printed
!= cur_func
)
780 sm_msg("Function too hairy. Giving up.");
782 final_pass
= 0; /* turn off sm_msg() from here */
787 add_ptr_list(&big_statement_stack
, stmt
);
788 free_expression_stack(&big_expression_stack
);
789 set_position(stmt
->pos
);
790 __pass_to_client(stmt
, STMT_HOOK
);
792 switch (stmt
->type
) {
793 case STMT_DECLARATION
:
794 split_declaration(stmt
->declaration
);
797 __split_expr(stmt
->ret_value
);
798 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
799 __process_post_op_stack();
802 case STMT_EXPRESSION
:
803 __split_expr(stmt
->expression
);
806 split_compound(stmt
);
809 if (known_condition_true(stmt
->if_conditional
)) {
810 __split_stmt(stmt
->if_true
);
813 if (known_condition_false(stmt
->if_conditional
)) {
814 __split_stmt(stmt
->if_false
);
817 if (option_known_conditions
&&
818 implied_condition_true(stmt
->if_conditional
)) {
819 sm_info("this condition is true.");
820 __split_stmt(stmt
->if_true
);
823 if (option_known_conditions
&&
824 implied_condition_false(stmt
->if_conditional
)) {
825 sm_info("this condition is false.");
826 __split_stmt(stmt
->if_false
);
829 __split_whole_condition(stmt
->if_conditional
);
830 __split_stmt(stmt
->if_true
);
831 if (empty_statement(stmt
->if_true
) &&
832 last_stmt_on_same_line() &&
833 !get_macro_name(stmt
->if_true
->pos
))
834 sm_msg("warn: if();");
835 __push_true_states();
836 __use_false_states();
837 __split_stmt(stmt
->if_false
);
838 __merge_true_states();
841 if (stmt
->iterator_pre_condition
)
842 handle_pre_loop(stmt
);
843 else if (stmt
->iterator_post_condition
)
844 handle_post_loop(stmt
);
846 // these are for(;;) type loops.
847 handle_pre_loop(stmt
);
851 if (get_value(stmt
->switch_expression
, &sval
)) {
852 split_known_switch(stmt
, sval
);
855 __split_expr(stmt
->switch_expression
);
856 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
857 __save_switch_states(top_expression(switch_expr_stack
));
861 __split_stmt(stmt
->switch_statement
);
862 if (!__pop_default())
863 __merge_switches(top_expression(switch_expr_stack
),
865 __discard_switches();
867 pop_expression(&switch_expr_stack
);
870 __merge_switches(top_expression(switch_expr_stack
),
871 stmt
->case_expression
);
872 __pass_case_to_client(top_expression(switch_expr_stack
),
873 stmt
->case_expression
);
874 if (!stmt
->case_expression
)
876 __split_expr(stmt
->case_expression
);
877 __split_expr(stmt
->case_to
);
878 __split_stmt(stmt
->case_statement
);
881 if (stmt
->label_identifier
&&
882 stmt
->label_identifier
->type
== SYM_LABEL
&&
883 stmt
->label_identifier
->ident
) {
884 loop_count
|= 0x80000000;
885 __merge_gotos(stmt
->label_identifier
->ident
->name
);
887 __split_stmt(stmt
->label_statement
);
890 __split_expr(stmt
->goto_expression
);
891 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
892 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
894 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
896 __process_continues();
898 } else if (stmt
->goto_label
&&
899 stmt
->goto_label
->type
== SYM_LABEL
&&
900 stmt
->goto_label
->ident
) {
901 __save_gotos(stmt
->goto_label
->ident
->name
);
904 if (is_last_stmt(stmt
))
905 handle_backward_goto(stmt
);
910 __pass_to_client(stmt
, ASM_HOOK
);
911 __split_expr(stmt
->asm_string
);
912 split_asm_constraints(stmt
->asm_outputs
);
913 split_asm_constraints(stmt
->asm_inputs
);
914 split_asm_constraints(stmt
->asm_clobbers
);
919 __split_expr(stmt
->range_expression
);
920 __split_expr(stmt
->range_low
);
921 __split_expr(stmt
->range_high
);
924 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
926 __process_post_op_stack();
929 static void split_expr_list(struct expression_list
*expr_list
)
931 struct expression
*expr
;
933 FOR_EACH_PTR(expr_list
, expr
) {
935 __process_post_op_stack();
936 } END_FOR_EACH_PTR(expr
);
939 static void split_sym(struct symbol
*sym
)
943 if (!(sym
->namespace & NS_SYMBOL
))
946 __split_stmt(sym
->stmt
);
947 __split_expr(sym
->array_size
);
948 split_symlist(sym
->arguments
);
949 split_symlist(sym
->symbol_list
);
950 __split_stmt(sym
->inline_stmt
);
951 split_symlist(sym
->inline_symbol_list
);
954 static void split_symlist(struct symbol_list
*sym_list
)
958 FOR_EACH_PTR(sym_list
, sym
) {
960 } END_FOR_EACH_PTR(sym
);
963 typedef void (fake_cb
)(struct expression
*expr
);
965 static int member_to_number(struct expression
*expr
, struct ident
*member
)
967 struct symbol
*type
, *tmp
;
975 type
= get_type(expr
);
976 if (!type
|| type
->type
!= SYM_STRUCT
)
980 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
984 if (strcmp(name
, tmp
->ident
->name
) == 0)
986 } END_FOR_EACH_PTR(tmp
);
990 static struct ident
*number_to_member(struct expression
*expr
, int num
)
992 struct symbol
*type
, *member
;
995 type
= get_type(expr
);
996 if (!type
|| type
->type
!= SYM_STRUCT
)
999 FOR_EACH_PTR(type
->symbol_list
, member
) {
1001 return member
->ident
;
1003 } END_FOR_EACH_PTR(member
);
1007 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1010 struct ident
*ident
;
1014 static struct member_set
*alloc_member_set(struct symbol
*type
)
1016 struct member_set
*member_set
;
1017 struct symbol
*member
;
1021 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1022 member_set
= malloc(member_count
* sizeof(*member_set
));
1024 FOR_EACH_PTR(type
->symbol_list
, member
) {
1025 member_set
[member_idx
].ident
= member
->ident
;
1026 member_set
[member_idx
].set
= 0;
1028 } END_FOR_EACH_PTR(member
);
1033 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
1035 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1038 for (i
= 0; i
< member_count
; i
++) {
1039 if (member_set
[i
].ident
== ident
) {
1040 member_set
[i
].set
= 1;
1044 // crap. this is buggy.
1045 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1048 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1050 struct expression
*edge_member
, *assign
;
1051 struct symbol
*base
= get_real_base_type(member
);
1055 expr
= member_expression(expr
, '.', member
->ident
);
1057 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1058 struct symbol
*type
;
1060 type
= get_real_base_type(tmp
);
1065 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1066 if (get_state_expr(SMATCH_EXTRA
, edge_member
))
1070 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1071 set_inner_struct_members(expr
, tmp
);
1078 assign
= assign_expression(edge_member
, zero_expr());
1079 __split_expr(assign
);
1080 } END_FOR_EACH_PTR(tmp
);
1085 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1088 struct expression
*member
, *assign
;
1091 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1092 expr
= strip_expr(expr
->unop
);
1096 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1097 type
= get_real_base_type(tmp
);
1102 member
= member_expression(expr
, op
, tmp
->ident
);
1103 if (get_state_expr(SMATCH_EXTRA
, member
))
1107 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1108 set_inner_struct_members(expr
, tmp
);
1111 if (type
->type
== SYM_ARRAY
)
1116 assign
= assign_expression(member
, zero_expr());
1117 __split_expr(assign
);
1118 } END_FOR_EACH_PTR(tmp
);
1121 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1123 struct expression
*deref
, *assign
, *tmp
;
1124 struct symbol
*struct_type
, *type
;
1125 struct ident
*member
;
1127 struct member_set
*member_set
;
1129 struct_type
= get_type(symbol
);
1131 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1134 member_set
= alloc_member_set(struct_type
);
1137 FOR_EACH_PTR(members
, tmp
) {
1138 member
= number_to_member(symbol
, member_idx
);
1139 while (tmp
->type
== EXPR_IDENTIFIER
) {
1140 member
= tmp
->expr_ident
;
1141 member_idx
= member_to_number(symbol
, member
);
1142 tmp
= tmp
->ident_expression
;
1144 mark_member_as_set(struct_type
, member_set
, member
);
1146 deref
= member_expression(symbol
, '.', member
);
1147 if (tmp
->type
== EXPR_INITIALIZER
) {
1148 type
= get_type(deref
);
1149 if (type
&& type
->type
== SYM_ARRAY
)
1150 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1152 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1154 assign
= assign_expression(deref
, tmp
);
1157 } END_FOR_EACH_PTR(tmp
);
1159 set_unset_to_zero(struct_type
, symbol
);
1162 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1164 fake_member_assigns_helper(symbol_expression(sym
),
1165 sym
->initializer
->expr_list
, fake_cb
);
1168 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1170 struct expression
*offset
, *binop
, *assign
, *tmp
;
1171 struct symbol
*type
;
1175 FOR_EACH_PTR(expr_list
, tmp
) {
1176 if (tmp
->type
== EXPR_INDEX
) {
1177 if (tmp
->idx_from
!= tmp
->idx_to
)
1179 idx
= tmp
->idx_from
;
1180 if (!tmp
->idx_expression
)
1182 tmp
= tmp
->idx_expression
;
1184 offset
= value_expr(idx
);
1185 binop
= array_element_expression(array
, offset
);
1186 if (tmp
->type
== EXPR_INITIALIZER
) {
1187 type
= get_type(binop
);
1188 if (type
&& type
->type
== SYM_ARRAY
)
1189 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1191 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1193 assign
= assign_expression(binop
, tmp
);
1198 } END_FOR_EACH_PTR(tmp
);
1201 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1203 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1206 static void fake_assign_expr(struct symbol
*sym
)
1208 struct expression
*assign
, *symbol
;
1210 symbol
= symbol_expression(sym
);
1211 assign
= assign_expression(symbol
, sym
->initializer
);
1212 __split_expr(assign
);
1215 static void call_split_expr(struct expression
*expr
)
1220 static void do_initializer_stuff(struct symbol
*sym
)
1222 if (!sym
->initializer
)
1225 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1226 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1227 fake_element_assigns(sym
, call_split_expr
);
1229 fake_member_assigns(sym
, call_split_expr
);
1231 fake_assign_expr(sym
);
1235 static void split_declaration(struct symbol_list
*sym_list
)
1239 FOR_EACH_PTR(sym_list
, sym
) {
1240 __pass_to_client(sym
, DECLARATION_HOOK
);
1241 do_initializer_stuff(sym
);
1243 } END_FOR_EACH_PTR(sym
);
1246 static void call_global_assign_hooks(struct expression
*assign
)
1248 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1251 static void fake_global_assign(struct symbol
*sym
)
1253 struct expression
*assign
, *symbol
;
1255 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1256 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1257 fake_element_assigns(sym
, call_global_assign_hooks
);
1258 } else if (sym
->initializer
) {
1259 symbol
= symbol_expression(sym
);
1260 assign
= assign_expression(symbol
, sym
->initializer
);
1261 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1263 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1265 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1266 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1267 fake_member_assigns(sym
, call_global_assign_hooks
);
1268 } else if (sym
->initializer
) {
1269 symbol
= symbol_expression(sym
);
1270 assign
= assign_expression(symbol
, sym
->initializer
);
1271 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1273 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1276 symbol
= symbol_expression(sym
);
1277 if (sym
->initializer
)
1278 assign
= assign_expression(symbol
, sym
->initializer
);
1280 assign
= assign_expression(symbol
, zero_expr());
1281 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1285 static void start_function_definition(struct symbol
*sym
)
1287 __in_function_def
= 1;
1288 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1289 __in_function_def
= 0;
1290 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1294 static void split_function(struct symbol
*sym
)
1296 struct symbol
*base_type
= get_base_type(sym
);
1298 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1301 gettimeofday(&fn_start_time
, NULL
);
1304 cur_func
= sym
->ident
->name
;
1305 __smatch_lineno
= sym
->pos
.line
;
1307 last_goto_statement_handled
= 0;
1308 sm_debug("new function: %s\n", cur_func
);
1310 if (option_two_passes
) {
1314 start_function_definition(sym
);
1315 __split_stmt(base_type
->stmt
);
1316 __split_stmt(base_type
->inline_stmt
);
1322 start_function_definition(sym
);
1323 __split_stmt(base_type
->stmt
);
1324 __split_stmt(base_type
->inline_stmt
);
1325 __pass_to_client(sym
, END_FUNC_HOOK
);
1326 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1329 cur_func_sym
= NULL
;
1331 free_data_info_allocs();
1332 free_expression_stack(&switch_expr_stack
);
1333 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1334 __bail_on_rest_of_function
= 0;
1337 static void parse_inline(struct expression
*call
)
1339 struct symbol
*base_type
;
1340 int loop_num_bak
= loop_num
;
1341 int final_pass_bak
= final_pass
;
1342 char *cur_func_bak
= cur_func
;
1343 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1344 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1345 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1346 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1348 __pass_to_client(call
, INLINE_FN_START
);
1349 final_pass
= 0; /* don't print anything */
1352 base_type
= get_base_type(call
->fn
->symbol
);
1353 cur_func_sym
= call
->fn
->symbol
;
1354 if (call
->fn
->symbol
->ident
)
1355 cur_func
= call
->fn
->symbol
->ident
->name
;
1358 set_position(call
->fn
->symbol
->pos
);
1361 big_statement_stack
= NULL
;
1362 big_expression_stack
= NULL
;
1363 switch_expr_stack
= NULL
;
1365 sm_debug("inline function: %s\n", cur_func
);
1368 start_function_definition(call
->fn
->symbol
);
1369 __split_stmt(base_type
->stmt
);
1370 __split_stmt(base_type
->inline_stmt
);
1371 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1372 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1374 free_expression_stack(&switch_expr_stack
);
1375 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1379 loop_num
= loop_num_bak
;
1380 final_pass
= final_pass_bak
;
1381 cur_func_sym
= cur_func_sym_bak
;
1382 cur_func
= cur_func_bak
;
1383 big_statement_stack
= big_statement_stack_bak
;
1384 big_expression_stack
= big_expression_stack_bak
;
1385 switch_expr_stack
= switch_expr_stack_bak
;
1387 restore_all_states();
1388 set_position(call
->pos
);
1390 __pass_to_client(call
, INLINE_FN_END
);
1393 static struct symbol_list
*inlines_called
;
1394 static void add_inline_function(struct symbol
*sym
)
1396 static struct symbol_list
*already_added
;
1399 FOR_EACH_PTR(already_added
, tmp
) {
1402 } END_FOR_EACH_PTR(tmp
);
1404 add_ptr_list(&already_added
, sym
);
1405 add_ptr_list(&inlines_called
, sym
);
1408 static void process_inlines(void)
1412 FOR_EACH_PTR(inlines_called
, tmp
) {
1413 split_function(tmp
);
1414 } END_FOR_EACH_PTR(tmp
);
1415 free_ptr_list(&inlines_called
);
1418 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1422 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1425 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1427 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1429 } END_FOR_EACH_PTR_REVERSE(sym
);
1434 static void split_inlines_in_scope(struct symbol
*sym
)
1436 struct symbol
*base
;
1437 struct symbol_list
*scope_list
;
1440 scope_list
= sym
->scope
->symbols
;
1441 stream
= sym
->pos
.stream
;
1443 /* find the last static symbol in the file */
1444 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1445 if (sym
->pos
.stream
!= stream
)
1447 if (sym
->type
!= SYM_NODE
)
1449 base
= get_base_type(sym
);
1452 if (base
->type
!= SYM_FN
)
1454 if (!base
->inline_stmt
)
1456 add_inline_function(sym
);
1457 } END_FOR_EACH_PTR_REVERSE(sym
);
1462 static void split_inlines(struct symbol_list
*sym_list
)
1466 sym
= get_last_scoped_symbol(sym_list
, 0);
1468 split_inlines_in_scope(sym
);
1469 sym
= get_last_scoped_symbol(sym_list
, 1);
1471 split_inlines_in_scope(sym
);
1474 static struct stree
*clone_estates_perm(struct stree
*orig
)
1476 struct stree
*ret
= NULL
;
1477 struct sm_state
*tmp
;
1479 FOR_EACH_SM(orig
, tmp
) {
1480 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1481 } END_FOR_EACH_SM(tmp
);
1486 static void split_functions(struct symbol_list
*sym_list
)
1491 FOR_EACH_PTR(sym_list
, sym
) {
1492 set_position(sym
->pos
);
1493 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1494 __pass_to_client(sym
, BASE_HOOK
);
1495 fake_global_assign(sym
);
1497 } END_FOR_EACH_PTR(sym
);
1498 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1501 FOR_EACH_PTR(sym_list
, sym
) {
1502 set_position(sym
->pos
);
1503 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1504 split_function(sym
);
1507 } END_FOR_EACH_PTR(sym
);
1508 split_inlines(sym_list
);
1509 __pass_to_client(sym_list
, END_FILE_HOOK
);
1512 void smatch(int argc
, char **argv
)
1515 struct string_list
*filelist
= NULL
;
1516 struct symbol_list
*sym_list
;
1519 printf("Usage: smatch [--debug] <filename.c>\n");
1522 sparse_initialize(argc
, argv
, &filelist
);
1523 set_valid_ptr_max();
1524 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1525 if (option_file_output
) {
1528 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1529 sm_outfd
= fopen(buf
, "w");
1531 printf("Error: Cannot open %s\n", base_file
);
1535 sym_list
= sparse_keep_tokens(base_file
);
1536 split_functions(sym_list
);
1537 } END_FOR_EACH_PTR_NOTAG(base_file
);