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
);
453 __prev_stmt
= stmt
->iterator_pre_statement
;
455 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
461 __merge_gotos(loop_name
);
463 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
464 __in_pre_condition
++;
465 __pass_to_client(stmt
, PRELOOP_HOOK
);
466 __split_whole_condition(stmt
->iterator_pre_condition
);
467 __in_pre_condition
--;
468 FOR_EACH_SM(stree
, sm
) {
469 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
470 } END_FOR_EACH_SM(sm
);
473 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
475 if (option_assume_loops
)
478 __split_stmt(stmt
->iterator_statement
);
479 __warn_on_silly_pre_loops();
480 if (is_forever_loop(stmt
)) {
482 __save_gotos(loop_name
);
484 __push_fake_cur_stree();
485 __split_stmt(stmt
->iterator_post_statement
);
486 stree
= __pop_fake_cur_stree();
488 __discard_false_states();
491 if (!__path_is_null())
492 __merge_stree_into_cur(stree
);
496 unchanged
= __iterator_unchanged(extra_sm
);
497 __split_stmt(stmt
->iterator_post_statement
);
498 __prev_stmt
= stmt
->iterator_post_statement
;
501 __save_gotos(loop_name
);
502 __in_pre_condition
++;
503 __split_whole_condition(stmt
->iterator_pre_condition
);
504 __in_pre_condition
--;
506 __merge_false_states();
508 __discard_false_states();
510 __merge_false_states();
512 if (extra_sm
&& unchanged
)
513 __extra_pre_loop_hook_after(extra_sm
,
514 stmt
->iterator_post_statement
,
515 stmt
->iterator_pre_condition
);
522 * Post loops are do {} while();
524 static void handle_post_loop(struct statement
*stmt
)
528 loop_name
= get_loop_name(loop_num
);
534 __merge_gotos(loop_name
);
535 __split_stmt(stmt
->iterator_statement
);
537 if (!is_zero(stmt
->iterator_post_condition
))
538 __save_gotos(loop_name
);
540 if (is_forever_loop(stmt
)) {
543 __split_whole_condition(stmt
->iterator_post_condition
);
544 __use_false_states();
550 static int empty_statement(struct statement
*stmt
)
554 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
559 static int last_stmt_on_same_line(void)
561 struct statement
*stmt
;
564 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
567 if (stmt
->pos
.line
== get_lineno())
570 } END_FOR_EACH_PTR_REVERSE(stmt
);
574 static void split_asm_constraints(struct expression_list
*expr_list
)
576 struct expression
*expr
;
579 FOR_EACH_PTR(expr_list
, expr
) {
581 case 0: /* identifier */
582 case 1: /* constraint */
585 case 2: /* expression */
590 } END_FOR_EACH_PTR(expr
);
593 static int is_case_val(struct statement
*stmt
, sval_t sval
)
597 if (stmt
->type
!= STMT_CASE
)
599 if (!stmt
->case_expression
) {
603 if (!get_value(stmt
->case_expression
, &case_sval
))
605 if (case_sval
.value
== sval
.value
)
610 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
612 struct statement
*tmp
;
614 __split_expr(stmt
->switch_expression
);
616 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
617 __save_switch_states(top_expression(switch_expr_stack
));
622 stmt
= stmt
->switch_statement
;
624 __push_scope_hooks();
625 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
626 __smatch_lineno
= tmp
->pos
.line
;
627 if (is_case_val(tmp
, sval
)) {
628 __merge_switches(top_expression(switch_expr_stack
),
629 stmt
->case_expression
);
630 __pass_case_to_client(top_expression(switch_expr_stack
),
631 stmt
->case_expression
);
633 if (__path_is_null())
636 if (__path_is_null()) {
640 } END_FOR_EACH_PTR(tmp
);
642 __call_scope_hooks();
643 if (!__pop_default())
644 __merge_switches(top_expression(switch_expr_stack
),
646 __discard_switches();
648 pop_expression(&switch_expr_stack
);
651 static int taking_too_long(void)
655 ms
= ms_since(&fn_start_time
);
656 if (ms
> 1000 * 60 * 5) /* five minutes */
661 static int is_last_stmt(struct statement
*cur_stmt
)
663 struct symbol
*fn
= get_base_type(cur_func_sym
);
664 struct statement
*stmt
;
670 stmt
= fn
->inline_stmt
;
671 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
673 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
674 if (stmt
&& stmt
->type
== STMT_LABEL
)
675 stmt
= stmt
->label_statement
;
676 if (stmt
== cur_stmt
)
681 static void handle_backward_goto(struct statement
*goto_stmt
)
683 const char *goto_name
, *label_name
;
684 struct statement
*func_stmt
;
685 struct symbol
*base_type
= get_base_type(cur_func_sym
);
686 struct statement
*tmp
;
691 if (last_goto_statement_handled
)
693 last_goto_statement_handled
= 1;
695 if (!goto_stmt
->goto_label
||
696 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
697 !goto_stmt
->goto_label
->ident
)
699 goto_name
= goto_stmt
->goto_label
->ident
->name
;
701 func_stmt
= base_type
->stmt
;
703 func_stmt
= base_type
->inline_stmt
;
706 if (func_stmt
->type
!= STMT_COMPOUND
)
709 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
711 if (tmp
->type
!= STMT_LABEL
)
713 if (!tmp
->label_identifier
||
714 tmp
->label_identifier
->type
!= SYM_LABEL
||
715 !tmp
->label_identifier
->ident
)
717 label_name
= tmp
->label_identifier
->ident
->name
;
718 if (strcmp(goto_name
, label_name
) != 0)
723 } END_FOR_EACH_PTR(tmp
);
726 static void fake_a_return(void)
728 struct symbol
*return_type
;
733 return_type
= get_real_base_type(cur_func_sym
);
734 return_type
= get_real_base_type(return_type
);
735 if (return_type
!= &void_ctype
) {
736 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
740 __pass_to_client(cur_func_sym
, END_FUNC_HOOK
);
741 __pass_to_client(cur_func_sym
, AFTER_FUNC_HOOK
);
744 static void split_compound(struct statement
*stmt
)
746 struct statement
*prev
= NULL
;
747 struct statement
*cur
= NULL
;
748 struct statement
*next
;
750 __push_scope_hooks();
752 FOR_EACH_PTR(stmt
->stmts
, next
) {
761 } END_FOR_EACH_PTR(next
);
769 __call_scope_hooks();
772 void __split_stmt(struct statement
*stmt
)
779 if (__bail_on_rest_of_function
|| out_of_memory() || taking_too_long()) {
780 static char *printed
= NULL
;
782 __bail_on_rest_of_function
= 1;
783 if (printed
!= cur_func
)
784 sm_msg("Function too hairy. Giving up.");
786 final_pass
= 0; /* turn off sm_msg() from here */
791 add_ptr_list(&big_statement_stack
, stmt
);
792 free_expression_stack(&big_expression_stack
);
793 set_position(stmt
->pos
);
794 __pass_to_client(stmt
, STMT_HOOK
);
796 switch (stmt
->type
) {
797 case STMT_DECLARATION
:
798 split_declaration(stmt
->declaration
);
801 __split_expr(stmt
->ret_value
);
802 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
803 __process_post_op_stack();
806 case STMT_EXPRESSION
:
807 __split_expr(stmt
->expression
);
810 split_compound(stmt
);
813 if (known_condition_true(stmt
->if_conditional
)) {
814 __split_stmt(stmt
->if_true
);
817 if (known_condition_false(stmt
->if_conditional
)) {
818 __split_stmt(stmt
->if_false
);
821 if (option_known_conditions
&&
822 implied_condition_true(stmt
->if_conditional
)) {
823 sm_info("this condition is true.");
824 __split_stmt(stmt
->if_true
);
827 if (option_known_conditions
&&
828 implied_condition_false(stmt
->if_conditional
)) {
829 sm_info("this condition is false.");
830 __split_stmt(stmt
->if_false
);
833 __split_whole_condition(stmt
->if_conditional
);
834 __split_stmt(stmt
->if_true
);
835 if (empty_statement(stmt
->if_true
) &&
836 last_stmt_on_same_line() &&
837 !get_macro_name(stmt
->if_true
->pos
))
838 sm_msg("warn: if();");
839 __push_true_states();
840 __use_false_states();
841 __split_stmt(stmt
->if_false
);
842 __merge_true_states();
845 if (stmt
->iterator_pre_condition
)
846 handle_pre_loop(stmt
);
847 else if (stmt
->iterator_post_condition
)
848 handle_post_loop(stmt
);
850 // these are for(;;) type loops.
851 handle_pre_loop(stmt
);
855 if (get_value(stmt
->switch_expression
, &sval
)) {
856 split_known_switch(stmt
, sval
);
859 __split_expr(stmt
->switch_expression
);
860 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
861 __save_switch_states(top_expression(switch_expr_stack
));
865 __split_stmt(stmt
->switch_statement
);
866 if (!__pop_default())
867 __merge_switches(top_expression(switch_expr_stack
),
869 __discard_switches();
871 pop_expression(&switch_expr_stack
);
874 __merge_switches(top_expression(switch_expr_stack
),
875 stmt
->case_expression
);
876 __pass_case_to_client(top_expression(switch_expr_stack
),
877 stmt
->case_expression
);
878 if (!stmt
->case_expression
)
880 __split_expr(stmt
->case_expression
);
881 __split_expr(stmt
->case_to
);
882 __split_stmt(stmt
->case_statement
);
885 if (stmt
->label_identifier
&&
886 stmt
->label_identifier
->type
== SYM_LABEL
&&
887 stmt
->label_identifier
->ident
) {
888 loop_count
|= 0x80000000;
889 __merge_gotos(stmt
->label_identifier
->ident
->name
);
891 __split_stmt(stmt
->label_statement
);
894 __split_expr(stmt
->goto_expression
);
895 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
896 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
898 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
900 __process_continues();
902 } else if (stmt
->goto_label
&&
903 stmt
->goto_label
->type
== SYM_LABEL
&&
904 stmt
->goto_label
->ident
) {
905 __save_gotos(stmt
->goto_label
->ident
->name
);
908 if (is_last_stmt(stmt
))
909 handle_backward_goto(stmt
);
914 __pass_to_client(stmt
, ASM_HOOK
);
915 __split_expr(stmt
->asm_string
);
916 split_asm_constraints(stmt
->asm_outputs
);
917 split_asm_constraints(stmt
->asm_inputs
);
918 split_asm_constraints(stmt
->asm_clobbers
);
923 __split_expr(stmt
->range_expression
);
924 __split_expr(stmt
->range_low
);
925 __split_expr(stmt
->range_high
);
928 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
930 __process_post_op_stack();
933 static void split_expr_list(struct expression_list
*expr_list
)
935 struct expression
*expr
;
937 FOR_EACH_PTR(expr_list
, expr
) {
939 __process_post_op_stack();
940 } END_FOR_EACH_PTR(expr
);
943 static void split_sym(struct symbol
*sym
)
947 if (!(sym
->namespace & NS_SYMBOL
))
950 __split_stmt(sym
->stmt
);
951 __split_expr(sym
->array_size
);
952 split_symlist(sym
->arguments
);
953 split_symlist(sym
->symbol_list
);
954 __split_stmt(sym
->inline_stmt
);
955 split_symlist(sym
->inline_symbol_list
);
958 static void split_symlist(struct symbol_list
*sym_list
)
962 FOR_EACH_PTR(sym_list
, sym
) {
964 } END_FOR_EACH_PTR(sym
);
967 typedef void (fake_cb
)(struct expression
*expr
);
969 static int member_to_number(struct expression
*expr
, struct ident
*member
)
971 struct symbol
*type
, *tmp
;
979 type
= get_type(expr
);
980 if (!type
|| type
->type
!= SYM_STRUCT
)
984 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
988 if (strcmp(name
, tmp
->ident
->name
) == 0)
990 } END_FOR_EACH_PTR(tmp
);
994 static struct ident
*number_to_member(struct expression
*expr
, int num
)
996 struct symbol
*type
, *member
;
999 type
= get_type(expr
);
1000 if (!type
|| type
->type
!= SYM_STRUCT
)
1003 FOR_EACH_PTR(type
->symbol_list
, member
) {
1005 return member
->ident
;
1007 } END_FOR_EACH_PTR(member
);
1011 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1014 struct ident
*ident
;
1018 static struct member_set
*alloc_member_set(struct symbol
*type
)
1020 struct member_set
*member_set
;
1021 struct symbol
*member
;
1025 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1026 member_set
= malloc(member_count
* sizeof(*member_set
));
1028 FOR_EACH_PTR(type
->symbol_list
, member
) {
1029 member_set
[member_idx
].ident
= member
->ident
;
1030 member_set
[member_idx
].set
= 0;
1032 } END_FOR_EACH_PTR(member
);
1037 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
1039 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1042 for (i
= 0; i
< member_count
; i
++) {
1043 if (member_set
[i
].ident
== ident
) {
1044 member_set
[i
].set
= 1;
1048 // crap. this is buggy.
1049 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1052 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1054 struct expression
*edge_member
, *assign
;
1055 struct symbol
*base
= get_real_base_type(member
);
1059 expr
= member_expression(expr
, '.', member
->ident
);
1061 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1062 struct symbol
*type
;
1064 type
= get_real_base_type(tmp
);
1069 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1070 if (get_state_expr(SMATCH_EXTRA
, edge_member
))
1074 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1075 set_inner_struct_members(expr
, tmp
);
1082 assign
= assign_expression(edge_member
, zero_expr());
1083 __split_expr(assign
);
1084 } END_FOR_EACH_PTR(tmp
);
1089 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1092 struct expression
*member
, *assign
;
1095 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1096 expr
= strip_expr(expr
->unop
);
1100 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1101 type
= get_real_base_type(tmp
);
1106 member
= member_expression(expr
, op
, tmp
->ident
);
1107 if (get_state_expr(SMATCH_EXTRA
, member
))
1111 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1112 set_inner_struct_members(expr
, tmp
);
1115 if (type
->type
== SYM_ARRAY
)
1120 assign
= assign_expression(member
, zero_expr());
1121 __split_expr(assign
);
1122 } END_FOR_EACH_PTR(tmp
);
1125 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1127 struct expression
*deref
, *assign
, *tmp
;
1128 struct symbol
*struct_type
, *type
;
1129 struct ident
*member
;
1131 struct member_set
*member_set
;
1133 struct_type
= get_type(symbol
);
1135 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1138 member_set
= alloc_member_set(struct_type
);
1141 FOR_EACH_PTR(members
, tmp
) {
1142 member
= number_to_member(symbol
, member_idx
);
1143 while (tmp
->type
== EXPR_IDENTIFIER
) {
1144 member
= tmp
->expr_ident
;
1145 member_idx
= member_to_number(symbol
, member
);
1146 tmp
= tmp
->ident_expression
;
1148 mark_member_as_set(struct_type
, member_set
, member
);
1150 deref
= member_expression(symbol
, '.', member
);
1151 if (tmp
->type
== EXPR_INITIALIZER
) {
1152 type
= get_type(deref
);
1153 if (type
&& type
->type
== SYM_ARRAY
)
1154 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1156 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1158 assign
= assign_expression(deref
, tmp
);
1161 } END_FOR_EACH_PTR(tmp
);
1163 set_unset_to_zero(struct_type
, symbol
);
1166 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1168 fake_member_assigns_helper(symbol_expression(sym
),
1169 sym
->initializer
->expr_list
, fake_cb
);
1172 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1174 struct expression
*offset
, *binop
, *assign
, *tmp
;
1175 struct symbol
*type
;
1179 FOR_EACH_PTR(expr_list
, tmp
) {
1180 if (tmp
->type
== EXPR_INDEX
) {
1181 if (tmp
->idx_from
!= tmp
->idx_to
)
1183 idx
= tmp
->idx_from
;
1184 if (!tmp
->idx_expression
)
1186 tmp
= tmp
->idx_expression
;
1188 offset
= value_expr(idx
);
1189 binop
= array_element_expression(array
, offset
);
1190 if (tmp
->type
== EXPR_INITIALIZER
) {
1191 type
= get_type(binop
);
1192 if (type
&& type
->type
== SYM_ARRAY
)
1193 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1195 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1197 assign
= assign_expression(binop
, tmp
);
1202 } END_FOR_EACH_PTR(tmp
);
1205 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1207 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1210 static void fake_assign_expr(struct symbol
*sym
)
1212 struct expression
*assign
, *symbol
;
1214 symbol
= symbol_expression(sym
);
1215 assign
= assign_expression(symbol
, sym
->initializer
);
1216 __split_expr(assign
);
1219 static void call_split_expr(struct expression
*expr
)
1224 static void do_initializer_stuff(struct symbol
*sym
)
1226 if (!sym
->initializer
)
1229 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1230 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1231 fake_element_assigns(sym
, call_split_expr
);
1233 fake_member_assigns(sym
, call_split_expr
);
1235 fake_assign_expr(sym
);
1239 static void split_declaration(struct symbol_list
*sym_list
)
1243 FOR_EACH_PTR(sym_list
, sym
) {
1244 __pass_to_client(sym
, DECLARATION_HOOK
);
1245 do_initializer_stuff(sym
);
1247 } END_FOR_EACH_PTR(sym
);
1250 static void call_global_assign_hooks(struct expression
*assign
)
1252 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1255 static void fake_global_assign(struct symbol
*sym
)
1257 struct expression
*assign
, *symbol
;
1259 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1260 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1261 fake_element_assigns(sym
, call_global_assign_hooks
);
1262 } else if (sym
->initializer
) {
1263 symbol
= symbol_expression(sym
);
1264 assign
= assign_expression(symbol
, sym
->initializer
);
1265 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1267 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1269 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1270 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1271 fake_member_assigns(sym
, call_global_assign_hooks
);
1272 } else if (sym
->initializer
) {
1273 symbol
= symbol_expression(sym
);
1274 assign
= assign_expression(symbol
, sym
->initializer
);
1275 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1277 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1280 symbol
= symbol_expression(sym
);
1281 if (sym
->initializer
)
1282 assign
= assign_expression(symbol
, sym
->initializer
);
1284 assign
= assign_expression(symbol
, zero_expr());
1285 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1289 static void start_function_definition(struct symbol
*sym
)
1291 __in_function_def
= 1;
1292 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1293 __in_function_def
= 0;
1294 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1298 static void split_function(struct symbol
*sym
)
1300 struct symbol
*base_type
= get_base_type(sym
);
1302 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1305 gettimeofday(&fn_start_time
, NULL
);
1308 cur_func
= sym
->ident
->name
;
1309 __smatch_lineno
= sym
->pos
.line
;
1311 last_goto_statement_handled
= 0;
1312 sm_debug("new function: %s\n", cur_func
);
1314 if (option_two_passes
) {
1318 start_function_definition(sym
);
1319 __split_stmt(base_type
->stmt
);
1320 __split_stmt(base_type
->inline_stmt
);
1326 start_function_definition(sym
);
1327 __split_stmt(base_type
->stmt
);
1328 __split_stmt(base_type
->inline_stmt
);
1329 __pass_to_client(sym
, END_FUNC_HOOK
);
1330 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1333 cur_func_sym
= NULL
;
1335 free_data_info_allocs();
1336 free_expression_stack(&switch_expr_stack
);
1337 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1338 __bail_on_rest_of_function
= 0;
1341 static void parse_inline(struct expression
*call
)
1343 struct symbol
*base_type
;
1344 int loop_num_bak
= loop_num
;
1345 int final_pass_bak
= final_pass
;
1346 char *cur_func_bak
= cur_func
;
1347 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1348 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1349 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1350 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1352 __pass_to_client(call
, INLINE_FN_START
);
1353 final_pass
= 0; /* don't print anything */
1356 base_type
= get_base_type(call
->fn
->symbol
);
1357 cur_func_sym
= call
->fn
->symbol
;
1358 if (call
->fn
->symbol
->ident
)
1359 cur_func
= call
->fn
->symbol
->ident
->name
;
1362 set_position(call
->fn
->symbol
->pos
);
1365 big_statement_stack
= NULL
;
1366 big_expression_stack
= NULL
;
1367 switch_expr_stack
= NULL
;
1369 sm_debug("inline function: %s\n", cur_func
);
1372 start_function_definition(call
->fn
->symbol
);
1373 __split_stmt(base_type
->stmt
);
1374 __split_stmt(base_type
->inline_stmt
);
1375 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1376 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1378 free_expression_stack(&switch_expr_stack
);
1379 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1383 loop_num
= loop_num_bak
;
1384 final_pass
= final_pass_bak
;
1385 cur_func_sym
= cur_func_sym_bak
;
1386 cur_func
= cur_func_bak
;
1387 big_statement_stack
= big_statement_stack_bak
;
1388 big_expression_stack
= big_expression_stack_bak
;
1389 switch_expr_stack
= switch_expr_stack_bak
;
1391 restore_all_states();
1392 set_position(call
->pos
);
1394 __pass_to_client(call
, INLINE_FN_END
);
1397 static struct symbol_list
*inlines_called
;
1398 static void add_inline_function(struct symbol
*sym
)
1400 static struct symbol_list
*already_added
;
1403 FOR_EACH_PTR(already_added
, tmp
) {
1406 } END_FOR_EACH_PTR(tmp
);
1408 add_ptr_list(&already_added
, sym
);
1409 add_ptr_list(&inlines_called
, sym
);
1412 static void process_inlines(void)
1416 FOR_EACH_PTR(inlines_called
, tmp
) {
1417 split_function(tmp
);
1418 } END_FOR_EACH_PTR(tmp
);
1419 free_ptr_list(&inlines_called
);
1422 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1426 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1429 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1431 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1433 } END_FOR_EACH_PTR_REVERSE(sym
);
1438 static void split_inlines_in_scope(struct symbol
*sym
)
1440 struct symbol
*base
;
1441 struct symbol_list
*scope_list
;
1444 scope_list
= sym
->scope
->symbols
;
1445 stream
= sym
->pos
.stream
;
1447 /* find the last static symbol in the file */
1448 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1449 if (sym
->pos
.stream
!= stream
)
1451 if (sym
->type
!= SYM_NODE
)
1453 base
= get_base_type(sym
);
1456 if (base
->type
!= SYM_FN
)
1458 if (!base
->inline_stmt
)
1460 add_inline_function(sym
);
1461 } END_FOR_EACH_PTR_REVERSE(sym
);
1466 static void split_inlines(struct symbol_list
*sym_list
)
1470 sym
= get_last_scoped_symbol(sym_list
, 0);
1472 split_inlines_in_scope(sym
);
1473 sym
= get_last_scoped_symbol(sym_list
, 1);
1475 split_inlines_in_scope(sym
);
1478 static struct stree
*clone_estates_perm(struct stree
*orig
)
1480 struct stree
*ret
= NULL
;
1481 struct sm_state
*tmp
;
1483 FOR_EACH_SM(orig
, tmp
) {
1484 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1485 } END_FOR_EACH_SM(tmp
);
1490 static void split_functions(struct symbol_list
*sym_list
)
1495 FOR_EACH_PTR(sym_list
, sym
) {
1496 set_position(sym
->pos
);
1497 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1498 __pass_to_client(sym
, BASE_HOOK
);
1499 fake_global_assign(sym
);
1501 } END_FOR_EACH_PTR(sym
);
1502 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1505 FOR_EACH_PTR(sym_list
, sym
) {
1506 set_position(sym
->pos
);
1507 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1508 split_function(sym
);
1511 } END_FOR_EACH_PTR(sym
);
1512 split_inlines(sym_list
);
1513 __pass_to_client(sym_list
, END_FILE_HOOK
);
1516 void smatch(int argc
, char **argv
)
1519 struct string_list
*filelist
= NULL
;
1520 struct symbol_list
*sym_list
;
1523 printf("Usage: smatch [--debug] <filename.c>\n");
1526 sparse_initialize(argc
, argv
, &filelist
);
1527 set_valid_ptr_max();
1528 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1529 if (option_file_output
) {
1532 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1533 sm_outfd
= fopen(buf
, "w");
1535 printf("Error: Cannot open %s\n", base_file
);
1539 sym_list
= sparse_keep_tokens(base_file
);
1540 split_functions(sym_list
);
1541 } END_FOR_EACH_PTR_NOTAG(base_file
);