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_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
1050 struct expression
*deref
, *assign
;
1051 struct symbol
*member
, *member_type
;
1055 FOR_EACH_PTR(type
->symbol_list
, member
) {
1056 if (!member
->ident
|| member_set
[member_idx
].set
) {
1060 member_type
= get_real_base_type(member
);
1061 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
1065 /* TODO: this should be handled recursively and not ignored */
1066 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
1070 deref
= member_expression(symbol
, '.', member
->ident
);
1071 assign
= assign_expression(deref
, zero_expr());
1072 __split_expr(assign
);
1074 } END_FOR_EACH_PTR(member
);
1078 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1080 struct expression
*deref
, *assign
, *tmp
;
1081 struct symbol
*struct_type
, *type
;
1082 struct ident
*member
;
1084 struct member_set
*member_set
;
1086 struct_type
= get_type(symbol
);
1088 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1091 member_set
= alloc_member_set(struct_type
);
1094 FOR_EACH_PTR(members
, tmp
) {
1095 member
= number_to_member(symbol
, member_idx
);
1096 while (tmp
->type
== EXPR_IDENTIFIER
) {
1097 member
= tmp
->expr_ident
;
1098 member_idx
= member_to_number(symbol
, member
);
1099 tmp
= tmp
->ident_expression
;
1101 mark_member_as_set(struct_type
, member_set
, member
);
1103 deref
= member_expression(symbol
, '.', member
);
1104 if (tmp
->type
== EXPR_INITIALIZER
) {
1105 type
= get_type(deref
);
1106 if (type
&& type
->type
== SYM_ARRAY
)
1107 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1109 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1111 assign
= assign_expression(deref
, tmp
);
1114 } END_FOR_EACH_PTR(tmp
);
1116 set_unset_to_zero(symbol
, struct_type
, member_set
);
1119 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1121 fake_member_assigns_helper(symbol_expression(sym
),
1122 sym
->initializer
->expr_list
, fake_cb
);
1125 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1127 struct expression
*offset
, *binop
, *assign
, *tmp
;
1128 struct symbol
*type
;
1132 FOR_EACH_PTR(expr_list
, tmp
) {
1133 if (tmp
->type
== EXPR_INDEX
) {
1134 if (tmp
->idx_from
!= tmp
->idx_to
)
1136 idx
= tmp
->idx_from
;
1137 if (!tmp
->idx_expression
)
1139 tmp
= tmp
->idx_expression
;
1141 offset
= value_expr(idx
);
1142 binop
= array_element_expression(array
, offset
);
1143 if (tmp
->type
== EXPR_INITIALIZER
) {
1144 type
= get_type(binop
);
1145 if (type
&& type
->type
== SYM_ARRAY
)
1146 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1148 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1150 assign
= assign_expression(binop
, tmp
);
1155 } END_FOR_EACH_PTR(tmp
);
1158 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1160 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1163 static void fake_assign_expr(struct symbol
*sym
)
1165 struct expression
*assign
, *symbol
;
1167 symbol
= symbol_expression(sym
);
1168 assign
= assign_expression(symbol
, sym
->initializer
);
1169 __split_expr(assign
);
1172 static void call_split_expr(struct expression
*expr
)
1177 static void do_initializer_stuff(struct symbol
*sym
)
1179 if (!sym
->initializer
)
1182 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1183 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1184 fake_element_assigns(sym
, call_split_expr
);
1186 fake_member_assigns(sym
, call_split_expr
);
1188 fake_assign_expr(sym
);
1192 static void split_declaration(struct symbol_list
*sym_list
)
1196 FOR_EACH_PTR(sym_list
, sym
) {
1197 __pass_to_client(sym
, DECLARATION_HOOK
);
1198 do_initializer_stuff(sym
);
1200 } END_FOR_EACH_PTR(sym
);
1203 static void call_global_assign_hooks(struct expression
*assign
)
1205 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1208 static void fake_global_assign(struct symbol
*sym
)
1210 struct expression
*assign
, *symbol
;
1212 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1213 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1214 fake_element_assigns(sym
, call_global_assign_hooks
);
1215 } else if (sym
->initializer
) {
1216 symbol
= symbol_expression(sym
);
1217 assign
= assign_expression(symbol
, sym
->initializer
);
1218 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1220 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1222 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1223 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1224 fake_member_assigns(sym
, call_global_assign_hooks
);
1225 } else if (sym
->initializer
) {
1226 symbol
= symbol_expression(sym
);
1227 assign
= assign_expression(symbol
, sym
->initializer
);
1228 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1230 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1233 symbol
= symbol_expression(sym
);
1234 if (sym
->initializer
)
1235 assign
= assign_expression(symbol
, sym
->initializer
);
1237 assign
= assign_expression(symbol
, zero_expr());
1238 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1242 static void start_function_definition(struct symbol
*sym
)
1244 __in_function_def
= 1;
1245 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1246 __in_function_def
= 0;
1247 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1251 static void split_function(struct symbol
*sym
)
1253 struct symbol
*base_type
= get_base_type(sym
);
1255 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1258 gettimeofday(&fn_start_time
, NULL
);
1261 cur_func
= sym
->ident
->name
;
1262 __smatch_lineno
= sym
->pos
.line
;
1264 last_goto_statement_handled
= 0;
1265 sm_debug("new function: %s\n", cur_func
);
1267 if (option_two_passes
) {
1271 start_function_definition(sym
);
1272 __split_stmt(base_type
->stmt
);
1273 __split_stmt(base_type
->inline_stmt
);
1279 start_function_definition(sym
);
1280 __split_stmt(base_type
->stmt
);
1281 __split_stmt(base_type
->inline_stmt
);
1282 __pass_to_client(sym
, END_FUNC_HOOK
);
1283 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1286 cur_func_sym
= NULL
;
1288 free_data_info_allocs();
1289 free_expression_stack(&switch_expr_stack
);
1290 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1291 __bail_on_rest_of_function
= 0;
1294 static void parse_inline(struct expression
*call
)
1296 struct symbol
*base_type
;
1297 int loop_num_bak
= loop_num
;
1298 int final_pass_bak
= final_pass
;
1299 char *cur_func_bak
= cur_func
;
1300 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1301 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1302 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1303 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1305 __pass_to_client(call
, INLINE_FN_START
);
1306 final_pass
= 0; /* don't print anything */
1309 base_type
= get_base_type(call
->fn
->symbol
);
1310 cur_func_sym
= call
->fn
->symbol
;
1311 if (call
->fn
->symbol
->ident
)
1312 cur_func
= call
->fn
->symbol
->ident
->name
;
1315 set_position(call
->fn
->symbol
->pos
);
1318 big_statement_stack
= NULL
;
1319 big_expression_stack
= NULL
;
1320 switch_expr_stack
= NULL
;
1322 sm_debug("inline function: %s\n", cur_func
);
1325 start_function_definition(call
->fn
->symbol
);
1326 __split_stmt(base_type
->stmt
);
1327 __split_stmt(base_type
->inline_stmt
);
1328 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1329 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1331 free_expression_stack(&switch_expr_stack
);
1332 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1336 loop_num
= loop_num_bak
;
1337 final_pass
= final_pass_bak
;
1338 cur_func_sym
= cur_func_sym_bak
;
1339 cur_func
= cur_func_bak
;
1340 big_statement_stack
= big_statement_stack_bak
;
1341 big_expression_stack
= big_expression_stack_bak
;
1342 switch_expr_stack
= switch_expr_stack_bak
;
1344 restore_all_states();
1345 set_position(call
->pos
);
1347 __pass_to_client(call
, INLINE_FN_END
);
1350 static struct symbol_list
*inlines_called
;
1351 static void add_inline_function(struct symbol
*sym
)
1353 static struct symbol_list
*already_added
;
1356 FOR_EACH_PTR(already_added
, tmp
) {
1359 } END_FOR_EACH_PTR(tmp
);
1361 add_ptr_list(&already_added
, sym
);
1362 add_ptr_list(&inlines_called
, sym
);
1365 static void process_inlines(void)
1369 FOR_EACH_PTR(inlines_called
, tmp
) {
1370 split_function(tmp
);
1371 } END_FOR_EACH_PTR(tmp
);
1372 free_ptr_list(&inlines_called
);
1375 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1379 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1382 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1384 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1386 } END_FOR_EACH_PTR_REVERSE(sym
);
1391 static void split_inlines_in_scope(struct symbol
*sym
)
1393 struct symbol
*base
;
1394 struct symbol_list
*scope_list
;
1397 scope_list
= sym
->scope
->symbols
;
1398 stream
= sym
->pos
.stream
;
1400 /* find the last static symbol in the file */
1401 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1402 if (sym
->pos
.stream
!= stream
)
1404 if (sym
->type
!= SYM_NODE
)
1406 base
= get_base_type(sym
);
1409 if (base
->type
!= SYM_FN
)
1411 if (!base
->inline_stmt
)
1413 add_inline_function(sym
);
1414 } END_FOR_EACH_PTR_REVERSE(sym
);
1419 static void split_inlines(struct symbol_list
*sym_list
)
1423 sym
= get_last_scoped_symbol(sym_list
, 0);
1425 split_inlines_in_scope(sym
);
1426 sym
= get_last_scoped_symbol(sym_list
, 1);
1428 split_inlines_in_scope(sym
);
1431 static struct stree
*clone_estates_perm(struct stree
*orig
)
1433 struct stree
*ret
= NULL
;
1434 struct sm_state
*tmp
;
1436 FOR_EACH_SM(orig
, tmp
) {
1437 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1438 } END_FOR_EACH_SM(tmp
);
1443 static void split_functions(struct symbol_list
*sym_list
)
1448 FOR_EACH_PTR(sym_list
, sym
) {
1449 set_position(sym
->pos
);
1450 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1451 __pass_to_client(sym
, BASE_HOOK
);
1452 fake_global_assign(sym
);
1454 } END_FOR_EACH_PTR(sym
);
1455 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1458 FOR_EACH_PTR(sym_list
, sym
) {
1459 set_position(sym
->pos
);
1460 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1461 split_function(sym
);
1464 } END_FOR_EACH_PTR(sym
);
1465 split_inlines(sym_list
);
1466 __pass_to_client(sym_list
, END_FILE_HOOK
);
1469 void smatch(int argc
, char **argv
)
1472 struct string_list
*filelist
= NULL
;
1473 struct symbol_list
*sym_list
;
1476 printf("Usage: smatch [--debug] <filename.c>\n");
1479 sparse_initialize(argc
, argv
, &filelist
);
1480 set_valid_ptr_max();
1481 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1482 if (option_file_output
) {
1485 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1486 sm_outfd
= fopen(buf
, "w");
1488 printf("Error: Cannot open %s\n", base_file
);
1492 sym_list
= sparse_keep_tokens(base_file
);
1493 split_functions(sym_list
);
1494 } END_FOR_EACH_PTR_NOTAG(base_file
);