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"
22 struct expression
*__inline_fn
;
24 static int __smatch_lineno
= 0;
26 static char *base_file
;
27 static const char *filename
;
28 static char *pathname
;
29 static char *full_filename
;
30 static char *cur_func
;
31 static int loop_count
;
32 int __expr_stmt_count
;
33 int __in_function_def
;
34 static struct expression_list
*switch_expr_stack
= NULL
;
35 static struct expression_list
*post_op_stack
= NULL
;
37 struct expression_list
*big_expression_stack
;
38 struct statement_list
*big_statement_stack
;
39 int __in_pre_condition
= 0;
40 int __bail_on_rest_of_function
= 0;
41 char *get_function(void) { return cur_func
; }
42 int get_lineno(void) { return __smatch_lineno
; }
43 int inside_loop(void) { return !!loop_count
; }
44 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
45 int in_expression_statement(void) { return !!__expr_stmt_count
; }
47 static void split_symlist(struct symbol_list
*sym_list
);
48 static void split_declaration(struct symbol_list
*sym_list
);
49 static void split_expr_list(struct expression_list
*expr_list
);
50 static void add_inline_function(struct symbol
*sym
);
51 static void parse_inline(struct expression
*expr
);
53 int option_assume_loops
= 0;
54 int option_known_conditions
= 0;
55 int option_two_passes
= 0;
56 struct symbol
*cur_func_sym
= NULL
;
58 int outside_of_function(void)
60 return cur_func_sym
== NULL
;
63 const char *get_filename(void)
72 const char *get_base_file(void)
77 static void set_position(struct position pos
)
80 static int prev_stream
= -1;
82 if (pos
.stream
== 0 && pos
.line
== 0)
85 __smatch_lineno
= pos
.line
;
87 if (pos
.stream
== prev_stream
)
90 filename
= stream_name(pos
.stream
);
93 pathname
= getcwd(NULL
, 0);
95 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
96 full_filename
= malloc(len
);
97 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
99 full_filename
= alloc_string(filename
);
104 static int is_inline_func(struct expression
*expr
)
106 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
108 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
113 static int is_noreturn_func(struct expression
*expr
)
115 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
117 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
122 int inlinable(struct expression
*expr
)
126 if (__inline_fn
) /* don't nest */
129 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
131 if (is_no_inline_function(expr
->symbol
->ident
->name
))
133 sym
= get_base_type(expr
->symbol
);
134 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
135 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
139 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
140 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
147 void __process_post_op_stack(void)
149 struct expression
*expr
;
151 FOR_EACH_PTR(post_op_stack
, expr
) {
152 __pass_to_client(expr
, OP_HOOK
);
153 } END_FOR_EACH_PTR(expr
);
155 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
158 void __split_expr(struct expression
*expr
)
163 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
165 push_expression(&big_expression_stack
, expr
);
166 set_position(expr
->pos
);
167 __pass_to_client(expr
, EXPR_HOOK
);
169 switch (expr
->type
) {
172 __pass_to_client(expr
, DEREF_HOOK
);
173 __split_expr(expr
->unop
);
174 __pass_to_client(expr
, OP_HOOK
);
177 __split_expr(expr
->unop
);
178 push_expression(&post_op_stack
, expr
);
182 __split_stmt(expr
->statement
);
187 __pass_to_client(expr
, LOGIC_HOOK
);
188 __handle_logic(expr
);
191 __pass_to_client(expr
, BINOP_HOOK
);
193 __split_expr(expr
->left
);
194 __process_post_op_stack();
195 __split_expr(expr
->right
);
197 case EXPR_ASSIGNMENT
: {
198 struct expression
*tmp
;
203 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
206 if (__handle_condition_assigns(expr
))
208 /* foo = (x < 5 ? foo : 5); */
209 if (__handle_select_assigns(expr
))
211 /* foo = ({frob(); frob(); frob(); 1;}) */
212 if (__handle_expr_statement_assigns(expr
))
215 __split_expr(expr
->right
);
216 if (outside_of_function())
217 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
219 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
220 tmp
= strip_expr(expr
->right
);
221 if (tmp
->type
== EXPR_CALL
)
222 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
223 if (get_macro_name(tmp
->pos
) &&
224 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
225 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
226 __split_expr(expr
->left
);
230 __pass_to_client(expr
, DEREF_HOOK
);
231 __split_expr(expr
->deref
);
234 __split_expr(expr
->base
);
237 case EXPR_FORCE_CAST
:
238 __pass_to_client(expr
, CAST_HOOK
);
239 __split_expr(expr
->cast_expression
);
242 if (expr
->cast_expression
)
243 __pass_to_client(strip_parens(expr
->cast_expression
),
248 evaluate_expression(expr
);
250 case EXPR_CONDITIONAL
:
252 if (known_condition_true(expr
->conditional
)) {
253 __split_expr(expr
->cond_true
);
256 if (known_condition_false(expr
->conditional
)) {
257 __split_expr(expr
->cond_false
);
260 __pass_to_client(expr
, SELECT_HOOK
);
261 __split_whole_condition(expr
->conditional
);
262 __split_expr(expr
->cond_true
);
263 __push_true_states();
264 __use_false_states();
265 __split_expr(expr
->cond_false
);
266 __merge_true_states();
269 if (sym_name_is("__builtin_constant_p", expr
->fn
))
271 split_expr_list(expr
->args
);
272 __split_expr(expr
->fn
);
273 if (is_inline_func(expr
->fn
))
274 add_inline_function(expr
->fn
->symbol
);
275 if (inlinable(expr
->fn
))
277 __process_post_op_stack();
278 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
280 if (inlinable(expr
->fn
)) {
283 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
284 if (is_noreturn_func(expr
->fn
))
287 case EXPR_INITIALIZER
:
288 split_expr_list(expr
->expr_list
);
290 case EXPR_IDENTIFIER
:
291 __split_expr(expr
->ident_expression
);
294 __split_expr(expr
->idx_expression
);
297 __split_expr(expr
->init_expr
);
300 __pass_to_client(expr
, SYM_HOOK
);
303 __pass_to_client(expr
, STRING_HOOK
);
308 pop_expression(&big_expression_stack
);
311 static int is_forever_loop(struct statement
*stmt
)
313 struct expression
*expr
;
315 expr
= strip_expr(stmt
->iterator_pre_condition
);
317 expr
= stmt
->iterator_post_condition
;
319 /* this is a for(;;) loop... */
323 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
330 static char *get_loop_name(int num
)
334 snprintf(buf
, 255, "-loop%d", num
);
336 return alloc_sname(buf
);
340 * Pre Loops are while and for loops.
342 static void handle_pre_loop(struct statement
*stmt
)
344 int once_through
; /* we go through the loop at least once */
345 struct sm_state
*extra_sm
= NULL
;
348 struct state_list
*slist
= NULL
;
349 struct sm_state
*sm
= NULL
;
351 loop_name
= get_loop_name(loop_num
);
354 __split_stmt(stmt
->iterator_pre_statement
);
356 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
362 __merge_gotos(loop_name
);
364 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
365 __in_pre_condition
++;
366 __pass_to_client(stmt
, PRELOOP_HOOK
);
367 __split_whole_condition(stmt
->iterator_pre_condition
);
368 __in_pre_condition
--;
369 FOR_EACH_PTR(slist
, sm
) {
370 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
371 } END_FOR_EACH_PTR(sm
);
374 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
376 if (option_assume_loops
)
379 __split_stmt(stmt
->iterator_statement
);
380 __warn_on_silly_pre_loops();
381 if (is_forever_loop(stmt
)) {
382 struct state_list
*slist
;
384 __save_gotos(loop_name
);
386 __push_fake_cur_slist();
387 __split_stmt(stmt
->iterator_post_statement
);
388 slist
= __pop_fake_cur_slist();
390 __discard_continues();
391 __discard_false_states();
394 if (!__path_is_null())
395 __merge_slist_into_cur(slist
);
399 unchanged
= __iterator_unchanged(extra_sm
);
400 __split_stmt(stmt
->iterator_post_statement
);
401 __save_gotos(loop_name
);
402 __split_whole_condition(stmt
->iterator_pre_condition
);
404 __merge_false_states();
406 __discard_false_states();
408 __merge_false_states();
410 if (extra_sm
&& unchanged
)
411 __extra_pre_loop_hook_after(extra_sm
,
412 stmt
->iterator_post_statement
,
413 stmt
->iterator_pre_condition
);
420 * Post loops are do {} while();
422 static void handle_post_loop(struct statement
*stmt
)
426 loop_name
= get_loop_name(loop_num
);
432 __merge_gotos(loop_name
);
433 __split_stmt(stmt
->iterator_statement
);
435 if (!is_zero(stmt
->iterator_post_condition
))
436 __save_gotos(loop_name
);
438 if (is_forever_loop(stmt
)) {
441 __split_whole_condition(stmt
->iterator_post_condition
);
442 __use_false_states();
448 static int empty_statement(struct statement
*stmt
)
452 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
457 static int last_stmt_on_same_line()
459 struct statement
*stmt
;
462 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
465 if (stmt
->pos
.line
== get_lineno())
468 } END_FOR_EACH_PTR_REVERSE(stmt
);
472 static struct statement
*last_stmt
;
473 static int is_last_stmt(struct statement
*stmt
)
475 if (stmt
== last_stmt
)
480 static void print_unreached_initializers(struct symbol_list
*sym_list
)
484 FOR_EACH_PTR(sym_list
, sym
) {
485 if (sym
->initializer
)
486 sm_msg("info: '%s' is not actually initialized (unreached code).",
487 (sym
->ident
? sym
->ident
->name
: "this variable"));
488 } END_FOR_EACH_PTR(sym
);
491 static void print_unreached(struct statement
*stmt
)
493 static int print
= 1;
498 if (!__path_is_null()) {
505 switch (stmt
->type
) {
506 case STMT_COMPOUND
: /* after a switch before a case stmt */
511 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
512 print_unreached_initializers(stmt
->declaration
);
514 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
515 if (is_last_stmt(stmt
))
525 if (!option_spammy
&& empty_statement(stmt
))
527 sm_msg("info: ignoring unreachable code.");
531 static void split_asm_constraints(struct expression_list
*expr_list
)
533 struct expression
*expr
;
536 FOR_EACH_PTR(expr_list
, expr
) {
538 case 0: /* identifier */
539 case 1: /* constraint */
542 case 2: /* expression */
547 } END_FOR_EACH_PTR(expr
);
550 static int is_case_val(struct statement
*stmt
, sval_t sval
)
554 if (stmt
->type
!= STMT_CASE
)
556 if (!stmt
->case_expression
) {
560 if (!get_value(stmt
->case_expression
, &case_sval
))
562 if (case_sval
.value
== sval
.value
)
567 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
569 struct statement
*tmp
;
571 __split_expr(stmt
->switch_expression
);
573 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
574 __save_switch_states(top_expression(switch_expr_stack
));
579 stmt
= stmt
->switch_statement
;
582 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
584 __push_scope_hooks();
585 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
586 __smatch_lineno
= tmp
->pos
.line
;
587 if (is_case_val(tmp
, sval
)) {
588 __merge_switches(top_expression(switch_expr_stack
),
589 stmt
->case_expression
);
590 __pass_case_to_client(top_expression(switch_expr_stack
),
591 stmt
->case_expression
);
593 if (__path_is_null())
596 if (__path_is_null()) {
600 } END_FOR_EACH_PTR(tmp
);
602 __call_scope_hooks();
603 if (!__pop_default())
604 __merge_switches(top_expression(switch_expr_stack
),
606 __discard_switches();
608 pop_expression(&switch_expr_stack
);
611 void __split_stmt(struct statement
*stmt
)
618 if (out_of_memory() || __bail_on_rest_of_function
) {
619 static char *printed
= NULL
;
621 if (printed
!= cur_func
)
622 sm_msg("Function too hairy. Giving up.");
623 final_pass
= 0; /* turn off sm_msg() from here */
628 add_ptr_list(&big_statement_stack
, stmt
);
629 free_expression_stack(&big_expression_stack
);
630 set_position(stmt
->pos
);
631 print_unreached(stmt
);
632 __pass_to_client(stmt
, STMT_HOOK
);
634 switch (stmt
->type
) {
635 case STMT_DECLARATION
:
636 split_declaration(stmt
->declaration
);
639 __split_expr(stmt
->ret_value
);
640 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
643 case STMT_EXPRESSION
:
644 __split_expr(stmt
->expression
);
646 case STMT_COMPOUND
: {
647 struct statement
*tmp
;
650 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
651 __push_scope_hooks();
652 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
654 } END_FOR_EACH_PTR(tmp
);
655 __call_scope_hooks();
659 if (known_condition_true(stmt
->if_conditional
)) {
660 __split_stmt(stmt
->if_true
);
663 if (known_condition_false(stmt
->if_conditional
)) {
664 __split_stmt(stmt
->if_false
);
667 if (option_known_conditions
&&
668 implied_condition_true(stmt
->if_conditional
)) {
669 sm_info("this condition is true.");
670 __split_stmt(stmt
->if_true
);
673 if (option_known_conditions
&&
674 implied_condition_false(stmt
->if_conditional
)) {
675 sm_info("this condition is false.");
676 __split_stmt(stmt
->if_false
);
679 __split_whole_condition(stmt
->if_conditional
);
680 __split_stmt(stmt
->if_true
);
681 if (empty_statement(stmt
->if_true
) &&
682 last_stmt_on_same_line() &&
683 !get_macro_name(stmt
->if_true
->pos
))
684 sm_msg("warn: if();");
685 __push_true_states();
686 __use_false_states();
687 __split_stmt(stmt
->if_false
);
688 __merge_true_states();
691 if (stmt
->iterator_pre_condition
)
692 handle_pre_loop(stmt
);
693 else if (stmt
->iterator_post_condition
)
694 handle_post_loop(stmt
);
696 // these are for(;;) type loops.
697 handle_pre_loop(stmt
);
701 if (get_value(stmt
->switch_expression
, &sval
)) {
702 split_known_switch(stmt
, sval
);
705 __split_expr(stmt
->switch_expression
);
706 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
707 __save_switch_states(top_expression(switch_expr_stack
));
711 __split_stmt(stmt
->switch_statement
);
712 if (!__pop_default())
713 __merge_switches(top_expression(switch_expr_stack
),
715 __discard_switches();
717 pop_expression(&switch_expr_stack
);
720 __merge_switches(top_expression(switch_expr_stack
),
721 stmt
->case_expression
);
722 __pass_case_to_client(top_expression(switch_expr_stack
),
723 stmt
->case_expression
);
724 if (!stmt
->case_expression
)
726 __split_expr(stmt
->case_expression
);
727 __split_expr(stmt
->case_to
);
728 __split_stmt(stmt
->case_statement
);
731 if (stmt
->label_identifier
&&
732 stmt
->label_identifier
->type
== SYM_LABEL
&&
733 stmt
->label_identifier
->ident
) {
734 loop_count
= 1000000;
735 __merge_gotos(stmt
->label_identifier
->ident
->name
);
737 __split_stmt(stmt
->label_statement
);
740 __split_expr(stmt
->goto_expression
);
741 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
742 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
744 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
746 __process_continues();
748 } else if (stmt
->goto_label
&&
749 stmt
->goto_label
->type
== SYM_LABEL
&&
750 stmt
->goto_label
->ident
) {
751 __save_gotos(stmt
->goto_label
->ident
->name
);
758 __pass_to_client(stmt
, ASM_HOOK
);
759 __split_expr(stmt
->asm_string
);
760 split_asm_constraints(stmt
->asm_outputs
);
761 split_asm_constraints(stmt
->asm_inputs
);
762 split_asm_constraints(stmt
->asm_clobbers
);
767 __split_expr(stmt
->range_expression
);
768 __split_expr(stmt
->range_low
);
769 __split_expr(stmt
->range_high
);
773 __process_post_op_stack();
776 static void split_expr_list(struct expression_list
*expr_list
)
778 struct expression
*expr
;
780 FOR_EACH_PTR(expr_list
, expr
) {
782 __process_post_op_stack();
783 } END_FOR_EACH_PTR(expr
);
786 static void split_sym(struct symbol
*sym
)
790 if (!(sym
->namespace & NS_SYMBOL
))
793 __split_stmt(sym
->stmt
);
794 __split_expr(sym
->array_size
);
795 split_symlist(sym
->arguments
);
796 split_symlist(sym
->symbol_list
);
797 __split_stmt(sym
->inline_stmt
);
798 split_symlist(sym
->inline_symbol_list
);
801 static void split_symlist(struct symbol_list
*sym_list
)
805 FOR_EACH_PTR(sym_list
, sym
) {
807 } END_FOR_EACH_PTR(sym
);
810 typedef void (fake_cb
)(struct expression
*expr
);
812 static int member_to_number(struct expression
*expr
, struct ident
*member
)
814 struct symbol
*type
, *tmp
;
822 type
= get_type(expr
);
823 if (!type
|| type
->type
!= SYM_STRUCT
)
827 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
831 if (strcmp(name
, tmp
->ident
->name
) == 0)
833 } END_FOR_EACH_PTR(tmp
);
837 static struct ident
*number_to_member(struct expression
*expr
, int num
)
839 struct symbol
*type
, *member
;
842 type
= get_type(expr
);
843 if (!type
|| type
->type
!= SYM_STRUCT
)
846 FOR_EACH_PTR(type
->symbol_list
, member
) {
848 return member
->ident
;
850 } END_FOR_EACH_PTR(member
);
854 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
861 static struct member_set
*alloc_member_set(struct symbol
*type
)
863 struct member_set
*member_set
;
864 struct symbol
*member
;
868 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
869 member_set
= malloc(member_count
* sizeof(*member_set
));
871 FOR_EACH_PTR(type
->symbol_list
, member
) {
872 member_set
[member_idx
].ident
= member
->ident
;
873 member_set
[member_idx
].set
= 0;
875 } END_FOR_EACH_PTR(member
);
880 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
882 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
885 for (i
= 0; i
< member_count
; i
++) {
886 if (member_set
[i
].ident
== ident
) {
887 member_set
[i
].set
= 1;
891 // crap. this is buggy.
892 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
895 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
897 struct expression
*deref
, *assign
;
898 struct symbol
*member
, *member_type
;
902 FOR_EACH_PTR(type
->symbol_list
, member
) {
903 if (!member
->ident
|| member_set
[member_idx
].set
) {
907 member_type
= get_real_base_type(member
);
908 if (!member_type
|| member_type
->type
== SYM_ARRAY
)
910 /* TODO: this should be handled recursively and not ignored */
911 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
)
913 deref
= member_expression(symbol
, '.', member
->ident
);
914 assign
= assign_expression(deref
, zero_expr());
915 __split_expr(assign
);
917 } END_FOR_EACH_PTR(member
);
921 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
923 struct expression
*deref
, *assign
, *tmp
;
924 struct symbol
*struct_type
, *type
;
925 struct ident
*member
;
927 struct member_set
*member_set
;
929 struct_type
= get_type(symbol
);
931 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
934 member_set
= alloc_member_set(struct_type
);
937 FOR_EACH_PTR(members
, tmp
) {
938 member
= number_to_member(symbol
, member_idx
);
939 while (tmp
->type
== EXPR_IDENTIFIER
) {
940 member
= tmp
->expr_ident
;
941 member_idx
= member_to_number(symbol
, member
);
942 tmp
= tmp
->ident_expression
;
944 mark_member_as_set(struct_type
, member_set
, member
);
946 deref
= member_expression(symbol
, '.', member
);
947 if (tmp
->type
== EXPR_INITIALIZER
) {
948 type
= get_type(deref
);
949 if (type
&& type
->type
== SYM_ARRAY
)
950 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
952 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
954 assign
= assign_expression(deref
, tmp
);
957 } END_FOR_EACH_PTR(tmp
);
959 set_unset_to_zero(symbol
, struct_type
, member_set
);
962 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
964 fake_member_assigns_helper(symbol_expression(sym
),
965 sym
->initializer
->expr_list
, fake_cb
);
968 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
970 struct expression
*offset
, *binop
, *assign
, *tmp
;
975 FOR_EACH_PTR(expr_list
, tmp
) {
976 if (tmp
->type
== EXPR_INDEX
) {
977 if (tmp
->idx_from
!= tmp
->idx_to
)
980 if (!tmp
->idx_expression
)
982 tmp
= tmp
->idx_expression
;
984 offset
= value_expr(idx
);
985 binop
= array_element_expression(array
, offset
);
986 if (tmp
->type
== EXPR_INITIALIZER
) {
987 type
= get_type(binop
);
988 if (type
&& type
->type
== SYM_ARRAY
)
989 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
991 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
993 assign
= assign_expression(binop
, tmp
);
998 } END_FOR_EACH_PTR(tmp
);
1001 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1003 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1006 static void fake_assign_expr(struct symbol
*sym
)
1008 struct expression
*assign
, *symbol
;
1010 symbol
= symbol_expression(sym
);
1011 assign
= assign_expression(symbol
, sym
->initializer
);
1012 __split_expr(assign
);
1015 static void call_split_expr(struct expression
*expr
)
1020 static void do_initializer_stuff(struct symbol
*sym
)
1022 if (!sym
->initializer
)
1025 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1026 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1027 fake_element_assigns(sym
, call_split_expr
);
1029 fake_member_assigns(sym
, call_split_expr
);
1031 fake_assign_expr(sym
);
1035 static void split_declaration(struct symbol_list
*sym_list
)
1039 FOR_EACH_PTR(sym_list
, sym
) {
1040 __pass_to_client(sym
, DECLARATION_HOOK
);
1041 do_initializer_stuff(sym
);
1043 } END_FOR_EACH_PTR(sym
);
1046 static void call_global_assign_hooks(struct expression
*assign
)
1048 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1051 static void fake_global_assign(struct symbol
*sym
)
1053 struct expression
*assign
, *symbol
;
1055 if (!sym
->initializer
)
1057 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1058 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1059 fake_element_assigns(sym
, call_global_assign_hooks
);
1061 fake_member_assigns(sym
, call_global_assign_hooks
);
1063 symbol
= symbol_expression(sym
);
1064 assign
= assign_expression(symbol
, sym
->initializer
);
1065 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1069 static void start_function_definition(struct symbol
*sym
)
1071 __in_function_def
= 1;
1072 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1073 __in_function_def
= 0;
1074 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1078 static void split_function(struct symbol
*sym
)
1080 struct symbol
*base_type
= get_base_type(sym
);
1084 cur_func
= sym
->ident
->name
;
1085 __smatch_lineno
= sym
->pos
.line
;
1088 sm_debug("new function: %s\n", cur_func
);
1090 if (option_two_passes
) {
1094 start_function_definition(sym
);
1095 __split_stmt(base_type
->stmt
);
1096 __split_stmt(base_type
->inline_stmt
);
1101 start_function_definition(sym
);
1102 __split_stmt(base_type
->stmt
);
1103 __split_stmt(base_type
->inline_stmt
);
1104 __pass_to_client(sym
, END_FUNC_HOOK
);
1105 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1106 cur_func_sym
= NULL
;
1109 free_data_info_allocs();
1110 free_expression_stack(&switch_expr_stack
);
1111 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1112 __bail_on_rest_of_function
= 0;
1115 static void parse_inline(struct expression
*call
)
1117 struct symbol
*base_type
;
1118 int loop_num_bak
= loop_num
;
1119 int final_pass_bak
= final_pass
;
1120 char *cur_func_bak
= cur_func
;
1121 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1122 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1123 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1124 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1126 __pass_to_client(call
, INLINE_FN_START
);
1127 final_pass
= 0; /* don't print anything */
1130 base_type
= get_base_type(call
->fn
->symbol
);
1131 cur_func_sym
= call
->fn
->symbol
;
1132 if (call
->fn
->symbol
->ident
)
1133 cur_func
= call
->fn
->symbol
->ident
->name
;
1136 set_position(call
->fn
->symbol
->pos
);
1139 nullify_all_states();
1140 big_statement_stack
= NULL
;
1141 big_expression_stack
= NULL
;
1142 switch_expr_stack
= NULL
;
1144 sm_debug("inline function: %s\n", cur_func
);
1147 start_function_definition(call
->fn
->symbol
);
1148 __split_stmt(base_type
->stmt
);
1149 __split_stmt(base_type
->inline_stmt
);
1150 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1151 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1153 free_expression_stack(&switch_expr_stack
);
1154 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1157 loop_num
= loop_num_bak
;
1158 final_pass
= final_pass_bak
;
1159 cur_func_sym
= cur_func_sym_bak
;
1160 cur_func
= cur_func_bak
;
1161 big_statement_stack
= big_statement_stack_bak
;
1162 big_expression_stack
= big_expression_stack_bak
;
1163 switch_expr_stack
= switch_expr_stack_bak
;
1165 restore_all_states();
1166 set_position(call
->pos
);
1168 __pass_to_client(call
, INLINE_FN_END
);
1171 static struct symbol_list
*inlines_called
;
1172 static void add_inline_function(struct symbol
*sym
)
1174 static struct symbol_list
*already_added
;
1177 FOR_EACH_PTR(already_added
, tmp
) {
1180 } END_FOR_EACH_PTR(tmp
);
1182 add_ptr_list(&already_added
, sym
);
1183 add_ptr_list(&inlines_called
, sym
);
1186 static void process_inlines()
1190 FOR_EACH_PTR(inlines_called
, tmp
) {
1191 split_function(tmp
);
1192 } END_FOR_EACH_PTR(tmp
);
1193 free_ptr_list(&inlines_called
);
1196 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1200 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1203 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1205 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1207 } END_FOR_EACH_PTR_REVERSE(sym
);
1212 static void split_inlines_in_scope(struct symbol
*sym
)
1214 struct symbol
*base
;
1215 struct symbol_list
*scope_list
;
1218 scope_list
= sym
->scope
->symbols
;
1219 stream
= sym
->pos
.stream
;
1221 /* find the last static symbol in the file */
1222 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1223 if (sym
->pos
.stream
!= stream
)
1225 if (sym
->type
!= SYM_NODE
)
1227 base
= get_base_type(sym
);
1230 if (base
->type
!= SYM_FN
)
1232 if (!base
->inline_stmt
)
1234 add_inline_function(sym
);
1235 } END_FOR_EACH_PTR_REVERSE(sym
);
1240 static void split_inlines(struct symbol_list
*sym_list
)
1244 sym
= get_last_scoped_symbol(sym_list
, 0);
1246 split_inlines_in_scope(sym
);
1247 sym
= get_last_scoped_symbol(sym_list
, 1);
1249 split_inlines_in_scope(sym
);
1252 static void split_functions(struct symbol_list
*sym_list
)
1256 FOR_EACH_PTR(sym_list
, sym
) {
1257 set_position(sym
->pos
);
1258 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1259 split_function(sym
);
1262 __pass_to_client(sym
, BASE_HOOK
);
1263 fake_global_assign(sym
);
1265 } END_FOR_EACH_PTR(sym
);
1266 split_inlines(sym_list
);
1267 __pass_to_client(sym_list
, END_FILE_HOOK
);
1270 void smatch(int argc
, char **argv
)
1273 struct string_list
*filelist
= NULL
;
1274 struct symbol_list
*sym_list
;
1277 printf("Usage: smatch [--debug] <filename.c>\n");
1280 sparse_initialize(argc
, argv
, &filelist
);
1281 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1282 if (option_file_output
) {
1285 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1286 sm_outfd
= fopen(buf
, "w");
1288 printf("Error: Cannot open %s\n", base_file
);
1292 sym_list
= sparse_keep_tokens(base_file
);
1293 split_functions(sym_list
);
1294 } END_FOR_EACH_PTR_NOTAG(base_file
);