4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
16 #include "smatch_expression_stacks.h"
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
23 struct expression
*__inline_fn
;
25 static int __smatch_lineno
= 0;
27 static char *base_file
;
28 static const char *filename
;
29 static char *pathname
;
30 static char *full_filename
;
31 static char *cur_func
;
32 static unsigned int loop_count
;
33 int __expr_stmt_count
;
34 int __in_function_def
;
35 static struct expression_list
*switch_expr_stack
= NULL
;
36 static struct expression_list
*post_op_stack
= NULL
;
38 struct expression_list
*big_expression_stack
;
39 struct statement_list
*big_statement_stack
;
40 int __in_pre_condition
= 0;
41 int __bail_on_rest_of_function
= 0;
42 char *get_function(void) { return cur_func
; }
43 int get_lineno(void) { return __smatch_lineno
; }
44 int inside_loop(void) { return !!loop_count
; }
45 int definitely_inside_loop(void) { return !!(loop_count
& ~0x80000000); }
46 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
47 int in_expression_statement(void) { return !!__expr_stmt_count
; }
49 static void split_symlist(struct symbol_list
*sym_list
);
50 static void split_declaration(struct symbol_list
*sym_list
);
51 static void split_expr_list(struct expression_list
*expr_list
);
52 static void add_inline_function(struct symbol
*sym
);
53 static void parse_inline(struct expression
*expr
);
55 int option_assume_loops
= 0;
56 int option_known_conditions
= 0;
57 int option_two_passes
= 0;
58 struct symbol
*cur_func_sym
= NULL
;
60 int outside_of_function(void)
62 return cur_func_sym
== NULL
;
65 const char *get_filename(void)
74 const char *get_base_file(void)
79 static void set_position(struct position pos
)
82 static int prev_stream
= -1;
84 if (pos
.stream
== 0 && pos
.line
== 0)
87 __smatch_lineno
= pos
.line
;
89 if (pos
.stream
== prev_stream
)
92 filename
= stream_name(pos
.stream
);
95 pathname
= getcwd(NULL
, 0);
97 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
98 full_filename
= malloc(len
);
99 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
101 full_filename
= alloc_string(filename
);
106 static int is_inline_func(struct expression
*expr
)
108 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
110 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
115 static int is_noreturn_func(struct expression
*expr
)
117 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
119 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
124 int inlinable(struct expression
*expr
)
128 if (__inline_fn
) /* don't nest */
131 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
133 if (is_no_inline_function(expr
->symbol
->ident
->name
))
135 sym
= get_base_type(expr
->symbol
);
136 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
137 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
141 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
142 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
149 void __process_post_op_stack(void)
151 struct expression
*expr
;
153 FOR_EACH_PTR(post_op_stack
, expr
) {
154 __pass_to_client(expr
, OP_HOOK
);
155 } END_FOR_EACH_PTR(expr
);
157 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
160 void __split_expr(struct expression
*expr
)
165 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
167 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
169 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
172 push_expression(&big_expression_stack
, expr
);
173 set_position(expr
->pos
);
174 __pass_to_client(expr
, EXPR_HOOK
);
176 switch (expr
->type
) {
179 __pass_to_client(expr
, DEREF_HOOK
);
180 __split_expr(expr
->unop
);
181 __pass_to_client(expr
, OP_HOOK
);
184 __split_expr(expr
->unop
);
185 push_expression(&post_op_stack
, expr
);
189 __split_stmt(expr
->statement
);
194 __pass_to_client(expr
, LOGIC_HOOK
);
195 __handle_logic(expr
);
198 __pass_to_client(expr
, BINOP_HOOK
);
200 __split_expr(expr
->left
);
201 __process_post_op_stack();
202 __split_expr(expr
->right
);
204 case EXPR_ASSIGNMENT
: {
205 struct expression
*tmp
;
210 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
213 if (__handle_condition_assigns(expr
))
215 /* foo = (x < 5 ? foo : 5); */
216 if (__handle_select_assigns(expr
))
218 /* foo = ({frob(); frob(); frob(); 1;}) */
219 if (__handle_expr_statement_assigns(expr
))
222 __split_expr(expr
->right
);
223 if (outside_of_function())
224 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
226 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
228 __fake_struct_member_assignments(expr
);
230 tmp
= strip_expr(expr
->right
);
231 if (tmp
->type
== EXPR_CALL
)
232 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
233 if (get_macro_name(tmp
->pos
) &&
234 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
235 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
236 __split_expr(expr
->left
);
240 __pass_to_client(expr
, DEREF_HOOK
);
241 __split_expr(expr
->deref
);
244 __split_expr(expr
->base
);
247 case EXPR_FORCE_CAST
:
248 __pass_to_client(expr
, CAST_HOOK
);
249 __split_expr(expr
->cast_expression
);
252 if (expr
->cast_expression
)
253 __pass_to_client(strip_parens(expr
->cast_expression
),
258 evaluate_expression(expr
);
260 case EXPR_CONDITIONAL
:
262 if (known_condition_true(expr
->conditional
)) {
263 __split_expr(expr
->cond_true
);
266 if (known_condition_false(expr
->conditional
)) {
267 __split_expr(expr
->cond_false
);
270 __pass_to_client(expr
, SELECT_HOOK
);
271 __split_whole_condition(expr
->conditional
);
272 __split_expr(expr
->cond_true
);
273 __push_true_states();
274 __use_false_states();
275 __split_expr(expr
->cond_false
);
276 __merge_true_states();
279 if (sym_name_is("__builtin_constant_p", expr
->fn
))
281 split_expr_list(expr
->args
);
282 __split_expr(expr
->fn
);
283 if (is_inline_func(expr
->fn
))
284 add_inline_function(expr
->fn
->symbol
);
285 if (inlinable(expr
->fn
))
287 __process_post_op_stack();
288 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
290 if (inlinable(expr
->fn
)) {
293 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
294 if (is_noreturn_func(expr
->fn
))
297 case EXPR_INITIALIZER
:
298 split_expr_list(expr
->expr_list
);
300 case EXPR_IDENTIFIER
:
301 __split_expr(expr
->ident_expression
);
304 __split_expr(expr
->idx_expression
);
307 __split_expr(expr
->init_expr
);
310 __pass_to_client(expr
, SYM_HOOK
);
313 __pass_to_client(expr
, STRING_HOOK
);
318 pop_expression(&big_expression_stack
);
321 static int is_forever_loop(struct statement
*stmt
)
323 struct expression
*expr
;
325 expr
= strip_expr(stmt
->iterator_pre_condition
);
327 expr
= stmt
->iterator_post_condition
;
329 /* this is a for(;;) loop... */
333 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
340 static char *get_loop_name(int num
)
344 snprintf(buf
, 255, "-loop%d", num
);
346 return alloc_sname(buf
);
350 * Pre Loops are while and for loops.
352 static void handle_pre_loop(struct statement
*stmt
)
354 int once_through
; /* we go through the loop at least once */
355 struct sm_state
*extra_sm
= NULL
;
358 struct state_list
*slist
= NULL
;
359 struct sm_state
*sm
= NULL
;
361 loop_name
= get_loop_name(loop_num
);
364 __split_stmt(stmt
->iterator_pre_statement
);
366 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
372 __merge_gotos(loop_name
);
374 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
375 __in_pre_condition
++;
376 __pass_to_client(stmt
, PRELOOP_HOOK
);
377 __split_whole_condition(stmt
->iterator_pre_condition
);
378 __in_pre_condition
--;
379 FOR_EACH_PTR(slist
, sm
) {
380 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
381 } END_FOR_EACH_PTR(sm
);
384 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
386 if (option_assume_loops
)
389 __split_stmt(stmt
->iterator_statement
);
390 __warn_on_silly_pre_loops();
391 if (is_forever_loop(stmt
)) {
392 struct state_list
*slist
;
394 __save_gotos(loop_name
);
396 __push_fake_cur_slist();
397 __split_stmt(stmt
->iterator_post_statement
);
398 slist
= __pop_fake_cur_slist();
400 __discard_continues();
401 __discard_false_states();
404 if (!__path_is_null())
405 __merge_slist_into_cur(slist
);
409 unchanged
= __iterator_unchanged(extra_sm
);
410 __split_stmt(stmt
->iterator_post_statement
);
411 __save_gotos(loop_name
);
412 __split_whole_condition(stmt
->iterator_pre_condition
);
414 __merge_false_states();
416 __discard_false_states();
418 __merge_false_states();
420 if (extra_sm
&& unchanged
)
421 __extra_pre_loop_hook_after(extra_sm
,
422 stmt
->iterator_post_statement
,
423 stmt
->iterator_pre_condition
);
430 * Post loops are do {} while();
432 static void handle_post_loop(struct statement
*stmt
)
436 loop_name
= get_loop_name(loop_num
);
442 __merge_gotos(loop_name
);
443 __split_stmt(stmt
->iterator_statement
);
445 if (!is_zero(stmt
->iterator_post_condition
))
446 __save_gotos(loop_name
);
448 if (is_forever_loop(stmt
)) {
451 __split_whole_condition(stmt
->iterator_post_condition
);
452 __use_false_states();
458 static int empty_statement(struct statement
*stmt
)
462 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
467 static int last_stmt_on_same_line()
469 struct statement
*stmt
;
472 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
475 if (stmt
->pos
.line
== get_lineno())
478 } END_FOR_EACH_PTR_REVERSE(stmt
);
482 static struct statement
*last_stmt
;
483 static int is_last_stmt(struct statement
*stmt
)
485 if (stmt
== last_stmt
)
490 static void print_unreached_initializers(struct symbol_list
*sym_list
)
494 FOR_EACH_PTR(sym_list
, sym
) {
495 if (sym
->initializer
)
496 sm_msg("info: '%s' is not actually initialized (unreached code).",
497 (sym
->ident
? sym
->ident
->name
: "this variable"));
498 } END_FOR_EACH_PTR(sym
);
501 static void print_unreached(struct statement
*stmt
)
503 static int print
= 1;
508 if (!__path_is_null()) {
515 switch (stmt
->type
) {
516 case STMT_COMPOUND
: /* after a switch before a case stmt */
521 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
522 print_unreached_initializers(stmt
->declaration
);
524 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
525 if (is_last_stmt(stmt
))
529 /* people put extra breaks inside switch statements */
530 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
&&
531 strcmp(stmt
->goto_label
->ident
->name
, "break") == 0)
537 if (empty_statement(stmt
))
541 sm_msg("info: ignoring unreachable code.");
545 static void split_asm_constraints(struct expression_list
*expr_list
)
547 struct expression
*expr
;
550 FOR_EACH_PTR(expr_list
, expr
) {
552 case 0: /* identifier */
553 case 1: /* constraint */
556 case 2: /* expression */
561 } END_FOR_EACH_PTR(expr
);
564 static int is_case_val(struct statement
*stmt
, sval_t sval
)
568 if (stmt
->type
!= STMT_CASE
)
570 if (!stmt
->case_expression
) {
574 if (!get_value(stmt
->case_expression
, &case_sval
))
576 if (case_sval
.value
== sval
.value
)
581 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
583 struct statement
*tmp
;
585 __split_expr(stmt
->switch_expression
);
587 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
588 __save_switch_states(top_expression(switch_expr_stack
));
593 stmt
= stmt
->switch_statement
;
596 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
598 __push_scope_hooks();
599 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
600 __smatch_lineno
= tmp
->pos
.line
;
601 if (is_case_val(tmp
, sval
)) {
602 __merge_switches(top_expression(switch_expr_stack
),
603 stmt
->case_expression
);
604 __pass_case_to_client(top_expression(switch_expr_stack
),
605 stmt
->case_expression
);
607 if (__path_is_null())
610 if (__path_is_null()) {
614 } END_FOR_EACH_PTR(tmp
);
616 __call_scope_hooks();
617 if (!__pop_default())
618 __merge_switches(top_expression(switch_expr_stack
),
620 __discard_switches();
622 pop_expression(&switch_expr_stack
);
625 void __split_stmt(struct statement
*stmt
)
632 if (out_of_memory() || __bail_on_rest_of_function
) {
633 static char *printed
= NULL
;
635 if (printed
!= cur_func
)
636 sm_msg("Function too hairy. Giving up.");
637 final_pass
= 0; /* turn off sm_msg() from here */
642 add_ptr_list(&big_statement_stack
, stmt
);
643 free_expression_stack(&big_expression_stack
);
644 set_position(stmt
->pos
);
645 print_unreached(stmt
);
646 __pass_to_client(stmt
, STMT_HOOK
);
648 switch (stmt
->type
) {
649 case STMT_DECLARATION
:
650 split_declaration(stmt
->declaration
);
653 __split_expr(stmt
->ret_value
);
654 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
657 case STMT_EXPRESSION
:
658 __split_expr(stmt
->expression
);
660 case STMT_COMPOUND
: {
661 struct statement
*tmp
;
664 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
665 __push_scope_hooks();
666 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
668 } END_FOR_EACH_PTR(tmp
);
669 __call_scope_hooks();
673 if (known_condition_true(stmt
->if_conditional
)) {
674 __split_stmt(stmt
->if_true
);
677 if (known_condition_false(stmt
->if_conditional
)) {
678 __split_stmt(stmt
->if_false
);
681 if (option_known_conditions
&&
682 implied_condition_true(stmt
->if_conditional
)) {
683 sm_info("this condition is true.");
684 __split_stmt(stmt
->if_true
);
687 if (option_known_conditions
&&
688 implied_condition_false(stmt
->if_conditional
)) {
689 sm_info("this condition is false.");
690 __split_stmt(stmt
->if_false
);
693 __split_whole_condition(stmt
->if_conditional
);
694 __split_stmt(stmt
->if_true
);
695 if (empty_statement(stmt
->if_true
) &&
696 last_stmt_on_same_line() &&
697 !get_macro_name(stmt
->if_true
->pos
))
698 sm_msg("warn: if();");
699 __push_true_states();
700 __use_false_states();
701 __split_stmt(stmt
->if_false
);
702 __merge_true_states();
705 if (stmt
->iterator_pre_condition
)
706 handle_pre_loop(stmt
);
707 else if (stmt
->iterator_post_condition
)
708 handle_post_loop(stmt
);
710 // these are for(;;) type loops.
711 handle_pre_loop(stmt
);
715 if (get_value(stmt
->switch_expression
, &sval
)) {
716 split_known_switch(stmt
, sval
);
719 __split_expr(stmt
->switch_expression
);
720 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
721 __save_switch_states(top_expression(switch_expr_stack
));
725 __split_stmt(stmt
->switch_statement
);
726 if (!__pop_default())
727 __merge_switches(top_expression(switch_expr_stack
),
729 __discard_switches();
731 pop_expression(&switch_expr_stack
);
734 __merge_switches(top_expression(switch_expr_stack
),
735 stmt
->case_expression
);
736 __pass_case_to_client(top_expression(switch_expr_stack
),
737 stmt
->case_expression
);
738 if (!stmt
->case_expression
)
740 __split_expr(stmt
->case_expression
);
741 __split_expr(stmt
->case_to
);
742 __split_stmt(stmt
->case_statement
);
745 if (stmt
->label_identifier
&&
746 stmt
->label_identifier
->type
== SYM_LABEL
&&
747 stmt
->label_identifier
->ident
) {
748 loop_count
|= 0x80000000;
749 __merge_gotos(stmt
->label_identifier
->ident
->name
);
751 __split_stmt(stmt
->label_statement
);
754 __split_expr(stmt
->goto_expression
);
755 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
756 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
758 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
760 __process_continues();
762 } else if (stmt
->goto_label
&&
763 stmt
->goto_label
->type
== SYM_LABEL
&&
764 stmt
->goto_label
->ident
) {
765 __save_gotos(stmt
->goto_label
->ident
->name
);
772 __pass_to_client(stmt
, ASM_HOOK
);
773 __split_expr(stmt
->asm_string
);
774 split_asm_constraints(stmt
->asm_outputs
);
775 split_asm_constraints(stmt
->asm_inputs
);
776 split_asm_constraints(stmt
->asm_clobbers
);
781 __split_expr(stmt
->range_expression
);
782 __split_expr(stmt
->range_low
);
783 __split_expr(stmt
->range_high
);
786 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
788 __process_post_op_stack();
791 static void split_expr_list(struct expression_list
*expr_list
)
793 struct expression
*expr
;
795 FOR_EACH_PTR(expr_list
, expr
) {
797 __process_post_op_stack();
798 } END_FOR_EACH_PTR(expr
);
801 static void split_sym(struct symbol
*sym
)
805 if (!(sym
->namespace & NS_SYMBOL
))
808 __split_stmt(sym
->stmt
);
809 __split_expr(sym
->array_size
);
810 split_symlist(sym
->arguments
);
811 split_symlist(sym
->symbol_list
);
812 __split_stmt(sym
->inline_stmt
);
813 split_symlist(sym
->inline_symbol_list
);
816 static void split_symlist(struct symbol_list
*sym_list
)
820 FOR_EACH_PTR(sym_list
, sym
) {
822 } END_FOR_EACH_PTR(sym
);
825 typedef void (fake_cb
)(struct expression
*expr
);
827 static int member_to_number(struct expression
*expr
, struct ident
*member
)
829 struct symbol
*type
, *tmp
;
837 type
= get_type(expr
);
838 if (!type
|| type
->type
!= SYM_STRUCT
)
842 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
846 if (strcmp(name
, tmp
->ident
->name
) == 0)
848 } END_FOR_EACH_PTR(tmp
);
852 static struct ident
*number_to_member(struct expression
*expr
, int num
)
854 struct symbol
*type
, *member
;
857 type
= get_type(expr
);
858 if (!type
|| type
->type
!= SYM_STRUCT
)
861 FOR_EACH_PTR(type
->symbol_list
, member
) {
863 return member
->ident
;
865 } END_FOR_EACH_PTR(member
);
869 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
876 static struct member_set
*alloc_member_set(struct symbol
*type
)
878 struct member_set
*member_set
;
879 struct symbol
*member
;
883 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
884 member_set
= malloc(member_count
* sizeof(*member_set
));
886 FOR_EACH_PTR(type
->symbol_list
, member
) {
887 member_set
[member_idx
].ident
= member
->ident
;
888 member_set
[member_idx
].set
= 0;
890 } END_FOR_EACH_PTR(member
);
895 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
897 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
900 for (i
= 0; i
< member_count
; i
++) {
901 if (member_set
[i
].ident
== ident
) {
902 member_set
[i
].set
= 1;
906 // crap. this is buggy.
907 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
910 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
912 struct expression
*deref
, *assign
;
913 struct symbol
*member
, *member_type
;
917 FOR_EACH_PTR(type
->symbol_list
, member
) {
918 if (!member
->ident
|| member_set
[member_idx
].set
) {
922 member_type
= get_real_base_type(member
);
923 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
927 /* TODO: this should be handled recursively and not ignored */
928 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
932 deref
= member_expression(symbol
, '.', member
->ident
);
933 assign
= assign_expression(deref
, zero_expr());
934 __split_expr(assign
);
936 } END_FOR_EACH_PTR(member
);
940 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
942 struct expression
*deref
, *assign
, *tmp
;
943 struct symbol
*struct_type
, *type
;
944 struct ident
*member
;
946 struct member_set
*member_set
;
948 struct_type
= get_type(symbol
);
950 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
953 member_set
= alloc_member_set(struct_type
);
956 FOR_EACH_PTR(members
, tmp
) {
957 member
= number_to_member(symbol
, member_idx
);
958 while (tmp
->type
== EXPR_IDENTIFIER
) {
959 member
= tmp
->expr_ident
;
960 member_idx
= member_to_number(symbol
, member
);
961 tmp
= tmp
->ident_expression
;
963 mark_member_as_set(struct_type
, member_set
, member
);
965 deref
= member_expression(symbol
, '.', member
);
966 if (tmp
->type
== EXPR_INITIALIZER
) {
967 type
= get_type(deref
);
968 if (type
&& type
->type
== SYM_ARRAY
)
969 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
971 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
973 assign
= assign_expression(deref
, tmp
);
976 } END_FOR_EACH_PTR(tmp
);
978 set_unset_to_zero(symbol
, struct_type
, member_set
);
981 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
983 fake_member_assigns_helper(symbol_expression(sym
),
984 sym
->initializer
->expr_list
, fake_cb
);
987 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
989 struct expression
*offset
, *binop
, *assign
, *tmp
;
994 FOR_EACH_PTR(expr_list
, tmp
) {
995 if (tmp
->type
== EXPR_INDEX
) {
996 if (tmp
->idx_from
!= tmp
->idx_to
)
999 if (!tmp
->idx_expression
)
1001 tmp
= tmp
->idx_expression
;
1003 offset
= value_expr(idx
);
1004 binop
= array_element_expression(array
, offset
);
1005 if (tmp
->type
== EXPR_INITIALIZER
) {
1006 type
= get_type(binop
);
1007 if (type
&& type
->type
== SYM_ARRAY
)
1008 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1010 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1012 assign
= assign_expression(binop
, tmp
);
1017 } END_FOR_EACH_PTR(tmp
);
1020 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1022 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1025 static void fake_assign_expr(struct symbol
*sym
)
1027 struct expression
*assign
, *symbol
;
1029 symbol
= symbol_expression(sym
);
1030 assign
= assign_expression(symbol
, sym
->initializer
);
1031 __split_expr(assign
);
1034 static void call_split_expr(struct expression
*expr
)
1039 static void do_initializer_stuff(struct symbol
*sym
)
1041 if (!sym
->initializer
)
1044 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1045 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1046 fake_element_assigns(sym
, call_split_expr
);
1048 fake_member_assigns(sym
, call_split_expr
);
1050 fake_assign_expr(sym
);
1054 static void split_declaration(struct symbol_list
*sym_list
)
1058 FOR_EACH_PTR(sym_list
, sym
) {
1059 __pass_to_client(sym
, DECLARATION_HOOK
);
1060 do_initializer_stuff(sym
);
1062 } END_FOR_EACH_PTR(sym
);
1065 static void call_global_assign_hooks(struct expression
*assign
)
1067 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1070 static void fake_global_assign(struct symbol
*sym
)
1072 struct expression
*assign
, *symbol
;
1074 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1075 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1076 fake_element_assigns(sym
, call_global_assign_hooks
);
1077 } else if (sym
->initializer
) {
1078 symbol
= symbol_expression(sym
);
1079 assign
= assign_expression(symbol
, sym
->initializer
);
1080 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1082 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1084 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1085 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1086 fake_member_assigns(sym
, call_global_assign_hooks
);
1087 } else if (sym
->initializer
) {
1088 symbol
= symbol_expression(sym
);
1089 assign
= assign_expression(symbol
, sym
->initializer
);
1090 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1092 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1095 symbol
= symbol_expression(sym
);
1096 if (sym
->initializer
)
1097 assign
= assign_expression(symbol
, sym
->initializer
);
1099 assign
= assign_expression(symbol
, zero_expr());
1100 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1104 static void start_function_definition(struct symbol
*sym
)
1106 __in_function_def
= 1;
1107 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1108 __in_function_def
= 0;
1109 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1113 static void split_function(struct symbol
*sym
)
1115 struct symbol
*base_type
= get_base_type(sym
);
1119 cur_func
= sym
->ident
->name
;
1120 __smatch_lineno
= sym
->pos
.line
;
1123 sm_debug("new function: %s\n", cur_func
);
1125 if (option_two_passes
) {
1129 start_function_definition(sym
);
1130 __split_stmt(base_type
->stmt
);
1131 __split_stmt(base_type
->inline_stmt
);
1136 start_function_definition(sym
);
1137 __split_stmt(base_type
->stmt
);
1138 __split_stmt(base_type
->inline_stmt
);
1139 __pass_to_client(sym
, END_FUNC_HOOK
);
1140 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1141 cur_func_sym
= NULL
;
1144 free_data_info_allocs();
1145 free_expression_stack(&switch_expr_stack
);
1146 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1147 __bail_on_rest_of_function
= 0;
1150 static void parse_inline(struct expression
*call
)
1152 struct symbol
*base_type
;
1153 int loop_num_bak
= loop_num
;
1154 int final_pass_bak
= final_pass
;
1155 char *cur_func_bak
= cur_func
;
1156 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1157 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1158 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1159 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1161 __pass_to_client(call
, INLINE_FN_START
);
1162 final_pass
= 0; /* don't print anything */
1165 base_type
= get_base_type(call
->fn
->symbol
);
1166 cur_func_sym
= call
->fn
->symbol
;
1167 if (call
->fn
->symbol
->ident
)
1168 cur_func
= call
->fn
->symbol
->ident
->name
;
1171 set_position(call
->fn
->symbol
->pos
);
1174 nullify_all_states();
1175 big_statement_stack
= NULL
;
1176 big_expression_stack
= NULL
;
1177 switch_expr_stack
= NULL
;
1179 sm_debug("inline function: %s\n", cur_func
);
1182 start_function_definition(call
->fn
->symbol
);
1183 __split_stmt(base_type
->stmt
);
1184 __split_stmt(base_type
->inline_stmt
);
1185 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1186 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1188 free_expression_stack(&switch_expr_stack
);
1189 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1192 loop_num
= loop_num_bak
;
1193 final_pass
= final_pass_bak
;
1194 cur_func_sym
= cur_func_sym_bak
;
1195 cur_func
= cur_func_bak
;
1196 big_statement_stack
= big_statement_stack_bak
;
1197 big_expression_stack
= big_expression_stack_bak
;
1198 switch_expr_stack
= switch_expr_stack_bak
;
1200 restore_all_states();
1201 set_position(call
->pos
);
1203 __pass_to_client(call
, INLINE_FN_END
);
1206 static struct symbol_list
*inlines_called
;
1207 static void add_inline_function(struct symbol
*sym
)
1209 static struct symbol_list
*already_added
;
1212 FOR_EACH_PTR(already_added
, tmp
) {
1215 } END_FOR_EACH_PTR(tmp
);
1217 add_ptr_list(&already_added
, sym
);
1218 add_ptr_list(&inlines_called
, sym
);
1221 static void process_inlines()
1225 FOR_EACH_PTR(inlines_called
, tmp
) {
1226 split_function(tmp
);
1227 } END_FOR_EACH_PTR(tmp
);
1228 free_ptr_list(&inlines_called
);
1231 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1235 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1238 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1240 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1242 } END_FOR_EACH_PTR_REVERSE(sym
);
1247 static void split_inlines_in_scope(struct symbol
*sym
)
1249 struct symbol
*base
;
1250 struct symbol_list
*scope_list
;
1253 scope_list
= sym
->scope
->symbols
;
1254 stream
= sym
->pos
.stream
;
1256 /* find the last static symbol in the file */
1257 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1258 if (sym
->pos
.stream
!= stream
)
1260 if (sym
->type
!= SYM_NODE
)
1262 base
= get_base_type(sym
);
1265 if (base
->type
!= SYM_FN
)
1267 if (!base
->inline_stmt
)
1269 add_inline_function(sym
);
1270 } END_FOR_EACH_PTR_REVERSE(sym
);
1275 static void split_inlines(struct symbol_list
*sym_list
)
1279 sym
= get_last_scoped_symbol(sym_list
, 0);
1281 split_inlines_in_scope(sym
);
1282 sym
= get_last_scoped_symbol(sym_list
, 1);
1284 split_inlines_in_scope(sym
);
1287 static void split_functions(struct symbol_list
*sym_list
)
1291 FOR_EACH_PTR(sym_list
, sym
) {
1292 set_position(sym
->pos
);
1293 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1294 split_function(sym
);
1297 __pass_to_client(sym
, BASE_HOOK
);
1298 fake_global_assign(sym
);
1300 } END_FOR_EACH_PTR(sym
);
1301 split_inlines(sym_list
);
1302 __pass_to_client(sym_list
, END_FILE_HOOK
);
1305 void smatch(int argc
, char **argv
)
1308 struct string_list
*filelist
= NULL
;
1309 struct symbol_list
*sym_list
;
1312 printf("Usage: smatch [--debug] <filename.c>\n");
1315 sparse_initialize(argc
, argv
, &filelist
);
1316 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1317 if (option_file_output
) {
1320 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1321 sm_outfd
= fopen(buf
, "w");
1323 printf("Error: Cannot open %s\n", base_file
);
1327 sym_list
= sparse_keep_tokens(base_file
);
1328 split_functions(sym_list
);
1329 } END_FOR_EACH_PTR_NOTAG(base_file
);