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 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 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
46 int in_expression_statement(void) { return !!__expr_stmt_count
; }
48 static void split_symlist(struct symbol_list
*sym_list
);
49 static void split_declaration(struct symbol_list
*sym_list
);
50 static void split_expr_list(struct expression_list
*expr_list
);
51 static void add_inline_function(struct symbol
*sym
);
52 static void parse_inline(struct expression
*expr
);
54 int option_assume_loops
= 0;
55 int option_known_conditions
= 0;
56 int option_two_passes
= 0;
57 struct symbol
*cur_func_sym
= NULL
;
59 int outside_of_function(void)
61 return cur_func_sym
== NULL
;
64 const char *get_filename(void)
73 const char *get_base_file(void)
78 static void set_position(struct position pos
)
81 static int prev_stream
= -1;
83 if (pos
.stream
== 0 && pos
.line
== 0)
86 __smatch_lineno
= pos
.line
;
88 if (pos
.stream
== prev_stream
)
91 filename
= stream_name(pos
.stream
);
94 pathname
= getcwd(NULL
, 0);
96 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
97 full_filename
= malloc(len
);
98 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
100 full_filename
= alloc_string(filename
);
105 static int is_inline_func(struct expression
*expr
)
107 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
109 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
114 static int is_noreturn_func(struct expression
*expr
)
116 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
118 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
123 int inlinable(struct expression
*expr
)
127 if (__inline_fn
) /* don't nest */
130 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
132 if (is_no_inline_function(expr
->symbol
->ident
->name
))
134 sym
= get_base_type(expr
->symbol
);
135 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
136 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
140 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
141 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
148 void __process_post_op_stack(void)
150 struct expression
*expr
;
152 FOR_EACH_PTR(post_op_stack
, expr
) {
153 __pass_to_client(expr
, OP_HOOK
);
154 } END_FOR_EACH_PTR(expr
);
156 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
159 void __split_expr(struct expression
*expr
)
164 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
166 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
168 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
171 push_expression(&big_expression_stack
, expr
);
172 set_position(expr
->pos
);
173 __pass_to_client(expr
, EXPR_HOOK
);
175 switch (expr
->type
) {
178 __pass_to_client(expr
, DEREF_HOOK
);
179 __split_expr(expr
->unop
);
180 __pass_to_client(expr
, OP_HOOK
);
183 __split_expr(expr
->unop
);
184 push_expression(&post_op_stack
, expr
);
188 __split_stmt(expr
->statement
);
193 __pass_to_client(expr
, LOGIC_HOOK
);
194 __handle_logic(expr
);
197 __pass_to_client(expr
, BINOP_HOOK
);
199 __split_expr(expr
->left
);
200 __process_post_op_stack();
201 __split_expr(expr
->right
);
203 case EXPR_ASSIGNMENT
: {
204 struct expression
*tmp
;
209 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
212 if (__handle_condition_assigns(expr
))
214 /* foo = (x < 5 ? foo : 5); */
215 if (__handle_select_assigns(expr
))
217 /* foo = ({frob(); frob(); frob(); 1;}) */
218 if (__handle_expr_statement_assigns(expr
))
221 __split_expr(expr
->right
);
222 if (outside_of_function())
223 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
225 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
227 __fake_struct_member_assignments(expr
);
229 tmp
= strip_expr(expr
->right
);
230 if (tmp
->type
== EXPR_CALL
)
231 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
232 if (get_macro_name(tmp
->pos
) &&
233 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
234 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
235 __split_expr(expr
->left
);
239 __pass_to_client(expr
, DEREF_HOOK
);
240 __split_expr(expr
->deref
);
243 __split_expr(expr
->base
);
246 case EXPR_FORCE_CAST
:
247 __pass_to_client(expr
, CAST_HOOK
);
248 __split_expr(expr
->cast_expression
);
251 if (expr
->cast_expression
)
252 __pass_to_client(strip_parens(expr
->cast_expression
),
257 evaluate_expression(expr
);
259 case EXPR_CONDITIONAL
:
261 if (known_condition_true(expr
->conditional
)) {
262 __split_expr(expr
->cond_true
);
265 if (known_condition_false(expr
->conditional
)) {
266 __split_expr(expr
->cond_false
);
269 __pass_to_client(expr
, SELECT_HOOK
);
270 __split_whole_condition(expr
->conditional
);
271 __split_expr(expr
->cond_true
);
272 __push_true_states();
273 __use_false_states();
274 __split_expr(expr
->cond_false
);
275 __merge_true_states();
278 if (sym_name_is("__builtin_constant_p", expr
->fn
))
280 split_expr_list(expr
->args
);
281 __split_expr(expr
->fn
);
282 if (is_inline_func(expr
->fn
))
283 add_inline_function(expr
->fn
->symbol
);
284 if (inlinable(expr
->fn
))
286 __process_post_op_stack();
287 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
289 if (inlinable(expr
->fn
)) {
292 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
293 if (is_noreturn_func(expr
->fn
))
296 case EXPR_INITIALIZER
:
297 split_expr_list(expr
->expr_list
);
299 case EXPR_IDENTIFIER
:
300 __split_expr(expr
->ident_expression
);
303 __split_expr(expr
->idx_expression
);
306 __split_expr(expr
->init_expr
);
309 __pass_to_client(expr
, SYM_HOOK
);
312 __pass_to_client(expr
, STRING_HOOK
);
317 pop_expression(&big_expression_stack
);
320 static int is_forever_loop(struct statement
*stmt
)
322 struct expression
*expr
;
324 expr
= strip_expr(stmt
->iterator_pre_condition
);
326 expr
= stmt
->iterator_post_condition
;
328 /* this is a for(;;) loop... */
332 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
339 static char *get_loop_name(int num
)
343 snprintf(buf
, 255, "-loop%d", num
);
345 return alloc_sname(buf
);
349 * Pre Loops are while and for loops.
351 static void handle_pre_loop(struct statement
*stmt
)
353 int once_through
; /* we go through the loop at least once */
354 struct sm_state
*extra_sm
= NULL
;
357 struct state_list
*slist
= NULL
;
358 struct sm_state
*sm
= NULL
;
360 loop_name
= get_loop_name(loop_num
);
363 __split_stmt(stmt
->iterator_pre_statement
);
365 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
371 __merge_gotos(loop_name
);
373 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
374 __in_pre_condition
++;
375 __pass_to_client(stmt
, PRELOOP_HOOK
);
376 __split_whole_condition(stmt
->iterator_pre_condition
);
377 __in_pre_condition
--;
378 FOR_EACH_PTR(slist
, sm
) {
379 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
380 } END_FOR_EACH_PTR(sm
);
383 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
385 if (option_assume_loops
)
388 __split_stmt(stmt
->iterator_statement
);
389 __warn_on_silly_pre_loops();
390 if (is_forever_loop(stmt
)) {
391 struct state_list
*slist
;
393 __save_gotos(loop_name
);
395 __push_fake_cur_slist();
396 __split_stmt(stmt
->iterator_post_statement
);
397 slist
= __pop_fake_cur_slist();
399 __discard_continues();
400 __discard_false_states();
403 if (!__path_is_null())
404 __merge_slist_into_cur(slist
);
408 unchanged
= __iterator_unchanged(extra_sm
);
409 __split_stmt(stmt
->iterator_post_statement
);
410 __save_gotos(loop_name
);
411 __split_whole_condition(stmt
->iterator_pre_condition
);
413 __merge_false_states();
415 __discard_false_states();
417 __merge_false_states();
419 if (extra_sm
&& unchanged
)
420 __extra_pre_loop_hook_after(extra_sm
,
421 stmt
->iterator_post_statement
,
422 stmt
->iterator_pre_condition
);
429 * Post loops are do {} while();
431 static void handle_post_loop(struct statement
*stmt
)
435 loop_name
= get_loop_name(loop_num
);
441 __merge_gotos(loop_name
);
442 __split_stmt(stmt
->iterator_statement
);
444 if (!is_zero(stmt
->iterator_post_condition
))
445 __save_gotos(loop_name
);
447 if (is_forever_loop(stmt
)) {
450 __split_whole_condition(stmt
->iterator_post_condition
);
451 __use_false_states();
457 static int empty_statement(struct statement
*stmt
)
461 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
466 static int last_stmt_on_same_line()
468 struct statement
*stmt
;
471 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
474 if (stmt
->pos
.line
== get_lineno())
477 } END_FOR_EACH_PTR_REVERSE(stmt
);
481 static struct statement
*last_stmt
;
482 static int is_last_stmt(struct statement
*stmt
)
484 if (stmt
== last_stmt
)
489 static void print_unreached_initializers(struct symbol_list
*sym_list
)
493 FOR_EACH_PTR(sym_list
, sym
) {
494 if (sym
->initializer
)
495 sm_msg("info: '%s' is not actually initialized (unreached code).",
496 (sym
->ident
? sym
->ident
->name
: "this variable"));
497 } END_FOR_EACH_PTR(sym
);
500 static void print_unreached(struct statement
*stmt
)
502 static int print
= 1;
507 if (!__path_is_null()) {
514 switch (stmt
->type
) {
515 case STMT_COMPOUND
: /* after a switch before a case stmt */
520 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
521 print_unreached_initializers(stmt
->declaration
);
523 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
524 if (is_last_stmt(stmt
))
528 /* people put extra breaks inside switch statements */
529 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
&&
530 strcmp(stmt
->goto_label
->ident
->name
, "break") == 0)
536 if (empty_statement(stmt
))
540 sm_msg("info: ignoring unreachable code.");
544 static void split_asm_constraints(struct expression_list
*expr_list
)
546 struct expression
*expr
;
549 FOR_EACH_PTR(expr_list
, expr
) {
551 case 0: /* identifier */
552 case 1: /* constraint */
555 case 2: /* expression */
560 } END_FOR_EACH_PTR(expr
);
563 static int is_case_val(struct statement
*stmt
, sval_t sval
)
567 if (stmt
->type
!= STMT_CASE
)
569 if (!stmt
->case_expression
) {
573 if (!get_value(stmt
->case_expression
, &case_sval
))
575 if (case_sval
.value
== sval
.value
)
580 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
582 struct statement
*tmp
;
584 __split_expr(stmt
->switch_expression
);
586 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
587 __save_switch_states(top_expression(switch_expr_stack
));
592 stmt
= stmt
->switch_statement
;
595 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
597 __push_scope_hooks();
598 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
599 __smatch_lineno
= tmp
->pos
.line
;
600 if (is_case_val(tmp
, sval
)) {
601 __merge_switches(top_expression(switch_expr_stack
),
602 stmt
->case_expression
);
603 __pass_case_to_client(top_expression(switch_expr_stack
),
604 stmt
->case_expression
);
606 if (__path_is_null())
609 if (__path_is_null()) {
613 } END_FOR_EACH_PTR(tmp
);
615 __call_scope_hooks();
616 if (!__pop_default())
617 __merge_switches(top_expression(switch_expr_stack
),
619 __discard_switches();
621 pop_expression(&switch_expr_stack
);
624 void __split_stmt(struct statement
*stmt
)
631 if (out_of_memory() || __bail_on_rest_of_function
) {
632 static char *printed
= NULL
;
634 if (printed
!= cur_func
)
635 sm_msg("Function too hairy. Giving up.");
636 final_pass
= 0; /* turn off sm_msg() from here */
641 add_ptr_list(&big_statement_stack
, stmt
);
642 free_expression_stack(&big_expression_stack
);
643 set_position(stmt
->pos
);
644 print_unreached(stmt
);
645 __pass_to_client(stmt
, STMT_HOOK
);
647 switch (stmt
->type
) {
648 case STMT_DECLARATION
:
649 split_declaration(stmt
->declaration
);
652 __split_expr(stmt
->ret_value
);
653 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
656 case STMT_EXPRESSION
:
657 __split_expr(stmt
->expression
);
659 case STMT_COMPOUND
: {
660 struct statement
*tmp
;
663 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
664 __push_scope_hooks();
665 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
667 } END_FOR_EACH_PTR(tmp
);
668 __call_scope_hooks();
672 if (known_condition_true(stmt
->if_conditional
)) {
673 __split_stmt(stmt
->if_true
);
676 if (known_condition_false(stmt
->if_conditional
)) {
677 __split_stmt(stmt
->if_false
);
680 if (option_known_conditions
&&
681 implied_condition_true(stmt
->if_conditional
)) {
682 sm_info("this condition is true.");
683 __split_stmt(stmt
->if_true
);
686 if (option_known_conditions
&&
687 implied_condition_false(stmt
->if_conditional
)) {
688 sm_info("this condition is false.");
689 __split_stmt(stmt
->if_false
);
692 __split_whole_condition(stmt
->if_conditional
);
693 __split_stmt(stmt
->if_true
);
694 if (empty_statement(stmt
->if_true
) &&
695 last_stmt_on_same_line() &&
696 !get_macro_name(stmt
->if_true
->pos
))
697 sm_msg("warn: if();");
698 __push_true_states();
699 __use_false_states();
700 __split_stmt(stmt
->if_false
);
701 __merge_true_states();
704 if (stmt
->iterator_pre_condition
)
705 handle_pre_loop(stmt
);
706 else if (stmt
->iterator_post_condition
)
707 handle_post_loop(stmt
);
709 // these are for(;;) type loops.
710 handle_pre_loop(stmt
);
714 if (get_value(stmt
->switch_expression
, &sval
)) {
715 split_known_switch(stmt
, sval
);
718 __split_expr(stmt
->switch_expression
);
719 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
720 __save_switch_states(top_expression(switch_expr_stack
));
724 __split_stmt(stmt
->switch_statement
);
725 if (!__pop_default())
726 __merge_switches(top_expression(switch_expr_stack
),
728 __discard_switches();
730 pop_expression(&switch_expr_stack
);
733 __merge_switches(top_expression(switch_expr_stack
),
734 stmt
->case_expression
);
735 __pass_case_to_client(top_expression(switch_expr_stack
),
736 stmt
->case_expression
);
737 if (!stmt
->case_expression
)
739 __split_expr(stmt
->case_expression
);
740 __split_expr(stmt
->case_to
);
741 __split_stmt(stmt
->case_statement
);
744 if (stmt
->label_identifier
&&
745 stmt
->label_identifier
->type
== SYM_LABEL
&&
746 stmt
->label_identifier
->ident
) {
747 loop_count
= 1000000;
748 __merge_gotos(stmt
->label_identifier
->ident
->name
);
750 __split_stmt(stmt
->label_statement
);
753 __split_expr(stmt
->goto_expression
);
754 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
755 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
757 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
759 __process_continues();
761 } else if (stmt
->goto_label
&&
762 stmt
->goto_label
->type
== SYM_LABEL
&&
763 stmt
->goto_label
->ident
) {
764 __save_gotos(stmt
->goto_label
->ident
->name
);
771 __pass_to_client(stmt
, ASM_HOOK
);
772 __split_expr(stmt
->asm_string
);
773 split_asm_constraints(stmt
->asm_outputs
);
774 split_asm_constraints(stmt
->asm_inputs
);
775 split_asm_constraints(stmt
->asm_clobbers
);
780 __split_expr(stmt
->range_expression
);
781 __split_expr(stmt
->range_low
);
782 __split_expr(stmt
->range_high
);
785 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
787 __process_post_op_stack();
790 static void split_expr_list(struct expression_list
*expr_list
)
792 struct expression
*expr
;
794 FOR_EACH_PTR(expr_list
, expr
) {
796 __process_post_op_stack();
797 } END_FOR_EACH_PTR(expr
);
800 static void split_sym(struct symbol
*sym
)
804 if (!(sym
->namespace & NS_SYMBOL
))
807 __split_stmt(sym
->stmt
);
808 __split_expr(sym
->array_size
);
809 split_symlist(sym
->arguments
);
810 split_symlist(sym
->symbol_list
);
811 __split_stmt(sym
->inline_stmt
);
812 split_symlist(sym
->inline_symbol_list
);
815 static void split_symlist(struct symbol_list
*sym_list
)
819 FOR_EACH_PTR(sym_list
, sym
) {
821 } END_FOR_EACH_PTR(sym
);
824 typedef void (fake_cb
)(struct expression
*expr
);
826 static int member_to_number(struct expression
*expr
, struct ident
*member
)
828 struct symbol
*type
, *tmp
;
836 type
= get_type(expr
);
837 if (!type
|| type
->type
!= SYM_STRUCT
)
841 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
845 if (strcmp(name
, tmp
->ident
->name
) == 0)
847 } END_FOR_EACH_PTR(tmp
);
851 static struct ident
*number_to_member(struct expression
*expr
, int num
)
853 struct symbol
*type
, *member
;
856 type
= get_type(expr
);
857 if (!type
|| type
->type
!= SYM_STRUCT
)
860 FOR_EACH_PTR(type
->symbol_list
, member
) {
862 return member
->ident
;
864 } END_FOR_EACH_PTR(member
);
868 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
875 static struct member_set
*alloc_member_set(struct symbol
*type
)
877 struct member_set
*member_set
;
878 struct symbol
*member
;
882 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
883 member_set
= malloc(member_count
* sizeof(*member_set
));
885 FOR_EACH_PTR(type
->symbol_list
, member
) {
886 member_set
[member_idx
].ident
= member
->ident
;
887 member_set
[member_idx
].set
= 0;
889 } END_FOR_EACH_PTR(member
);
894 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
896 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
899 for (i
= 0; i
< member_count
; i
++) {
900 if (member_set
[i
].ident
== ident
) {
901 member_set
[i
].set
= 1;
905 // crap. this is buggy.
906 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
909 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
911 struct expression
*deref
, *assign
;
912 struct symbol
*member
, *member_type
;
916 FOR_EACH_PTR(type
->symbol_list
, member
) {
917 if (!member
->ident
|| member_set
[member_idx
].set
) {
921 member_type
= get_real_base_type(member
);
922 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
926 /* TODO: this should be handled recursively and not ignored */
927 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
931 deref
= member_expression(symbol
, '.', member
->ident
);
932 assign
= assign_expression(deref
, zero_expr());
933 __split_expr(assign
);
935 } END_FOR_EACH_PTR(member
);
939 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
941 struct expression
*deref
, *assign
, *tmp
;
942 struct symbol
*struct_type
, *type
;
943 struct ident
*member
;
945 struct member_set
*member_set
;
947 struct_type
= get_type(symbol
);
949 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
952 member_set
= alloc_member_set(struct_type
);
955 FOR_EACH_PTR(members
, tmp
) {
956 member
= number_to_member(symbol
, member_idx
);
957 while (tmp
->type
== EXPR_IDENTIFIER
) {
958 member
= tmp
->expr_ident
;
959 member_idx
= member_to_number(symbol
, member
);
960 tmp
= tmp
->ident_expression
;
962 mark_member_as_set(struct_type
, member_set
, member
);
964 deref
= member_expression(symbol
, '.', member
);
965 if (tmp
->type
== EXPR_INITIALIZER
) {
966 type
= get_type(deref
);
967 if (type
&& type
->type
== SYM_ARRAY
)
968 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
970 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
972 assign
= assign_expression(deref
, tmp
);
975 } END_FOR_EACH_PTR(tmp
);
977 set_unset_to_zero(symbol
, struct_type
, member_set
);
980 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
982 fake_member_assigns_helper(symbol_expression(sym
),
983 sym
->initializer
->expr_list
, fake_cb
);
986 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
988 struct expression
*offset
, *binop
, *assign
, *tmp
;
993 FOR_EACH_PTR(expr_list
, tmp
) {
994 if (tmp
->type
== EXPR_INDEX
) {
995 if (tmp
->idx_from
!= tmp
->idx_to
)
998 if (!tmp
->idx_expression
)
1000 tmp
= tmp
->idx_expression
;
1002 offset
= value_expr(idx
);
1003 binop
= array_element_expression(array
, offset
);
1004 if (tmp
->type
== EXPR_INITIALIZER
) {
1005 type
= get_type(binop
);
1006 if (type
&& type
->type
== SYM_ARRAY
)
1007 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1009 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1011 assign
= assign_expression(binop
, tmp
);
1016 } END_FOR_EACH_PTR(tmp
);
1019 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1021 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1024 static void fake_assign_expr(struct symbol
*sym
)
1026 struct expression
*assign
, *symbol
;
1028 symbol
= symbol_expression(sym
);
1029 assign
= assign_expression(symbol
, sym
->initializer
);
1030 __split_expr(assign
);
1033 static void call_split_expr(struct expression
*expr
)
1038 static void do_initializer_stuff(struct symbol
*sym
)
1040 if (!sym
->initializer
)
1043 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1044 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1045 fake_element_assigns(sym
, call_split_expr
);
1047 fake_member_assigns(sym
, call_split_expr
);
1049 fake_assign_expr(sym
);
1053 static void split_declaration(struct symbol_list
*sym_list
)
1057 FOR_EACH_PTR(sym_list
, sym
) {
1058 __pass_to_client(sym
, DECLARATION_HOOK
);
1059 do_initializer_stuff(sym
);
1061 } END_FOR_EACH_PTR(sym
);
1064 static void call_global_assign_hooks(struct expression
*assign
)
1066 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1069 static void fake_global_assign(struct symbol
*sym
)
1071 struct expression
*assign
, *symbol
;
1073 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1074 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1075 fake_element_assigns(sym
, call_global_assign_hooks
);
1076 } else if (sym
->initializer
) {
1077 symbol
= symbol_expression(sym
);
1078 assign
= assign_expression(symbol
, sym
->initializer
);
1079 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1081 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1083 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1084 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1085 fake_member_assigns(sym
, call_global_assign_hooks
);
1086 } else if (sym
->initializer
) {
1087 symbol
= symbol_expression(sym
);
1088 assign
= assign_expression(symbol
, sym
->initializer
);
1089 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1091 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1094 symbol
= symbol_expression(sym
);
1095 if (sym
->initializer
)
1096 assign
= assign_expression(symbol
, sym
->initializer
);
1098 assign
= assign_expression(symbol
, zero_expr());
1099 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1103 static void start_function_definition(struct symbol
*sym
)
1105 __in_function_def
= 1;
1106 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1107 __in_function_def
= 0;
1108 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1112 static void split_function(struct symbol
*sym
)
1114 struct symbol
*base_type
= get_base_type(sym
);
1118 cur_func
= sym
->ident
->name
;
1119 __smatch_lineno
= sym
->pos
.line
;
1122 sm_debug("new function: %s\n", cur_func
);
1124 if (option_two_passes
) {
1128 start_function_definition(sym
);
1129 __split_stmt(base_type
->stmt
);
1130 __split_stmt(base_type
->inline_stmt
);
1135 start_function_definition(sym
);
1136 __split_stmt(base_type
->stmt
);
1137 __split_stmt(base_type
->inline_stmt
);
1138 __pass_to_client(sym
, END_FUNC_HOOK
);
1139 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1140 cur_func_sym
= NULL
;
1143 free_data_info_allocs();
1144 free_expression_stack(&switch_expr_stack
);
1145 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1146 __bail_on_rest_of_function
= 0;
1149 static void parse_inline(struct expression
*call
)
1151 struct symbol
*base_type
;
1152 int loop_num_bak
= loop_num
;
1153 int final_pass_bak
= final_pass
;
1154 char *cur_func_bak
= cur_func
;
1155 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1156 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1157 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1158 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1160 __pass_to_client(call
, INLINE_FN_START
);
1161 final_pass
= 0; /* don't print anything */
1164 base_type
= get_base_type(call
->fn
->symbol
);
1165 cur_func_sym
= call
->fn
->symbol
;
1166 if (call
->fn
->symbol
->ident
)
1167 cur_func
= call
->fn
->symbol
->ident
->name
;
1170 set_position(call
->fn
->symbol
->pos
);
1173 nullify_all_states();
1174 big_statement_stack
= NULL
;
1175 big_expression_stack
= NULL
;
1176 switch_expr_stack
= NULL
;
1178 sm_debug("inline function: %s\n", cur_func
);
1181 start_function_definition(call
->fn
->symbol
);
1182 __split_stmt(base_type
->stmt
);
1183 __split_stmt(base_type
->inline_stmt
);
1184 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1185 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1187 free_expression_stack(&switch_expr_stack
);
1188 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1191 loop_num
= loop_num_bak
;
1192 final_pass
= final_pass_bak
;
1193 cur_func_sym
= cur_func_sym_bak
;
1194 cur_func
= cur_func_bak
;
1195 big_statement_stack
= big_statement_stack_bak
;
1196 big_expression_stack
= big_expression_stack_bak
;
1197 switch_expr_stack
= switch_expr_stack_bak
;
1199 restore_all_states();
1200 set_position(call
->pos
);
1202 __pass_to_client(call
, INLINE_FN_END
);
1205 static struct symbol_list
*inlines_called
;
1206 static void add_inline_function(struct symbol
*sym
)
1208 static struct symbol_list
*already_added
;
1211 FOR_EACH_PTR(already_added
, tmp
) {
1214 } END_FOR_EACH_PTR(tmp
);
1216 add_ptr_list(&already_added
, sym
);
1217 add_ptr_list(&inlines_called
, sym
);
1220 static void process_inlines()
1224 FOR_EACH_PTR(inlines_called
, tmp
) {
1225 split_function(tmp
);
1226 } END_FOR_EACH_PTR(tmp
);
1227 free_ptr_list(&inlines_called
);
1230 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1234 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1237 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1239 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1241 } END_FOR_EACH_PTR_REVERSE(sym
);
1246 static void split_inlines_in_scope(struct symbol
*sym
)
1248 struct symbol
*base
;
1249 struct symbol_list
*scope_list
;
1252 scope_list
= sym
->scope
->symbols
;
1253 stream
= sym
->pos
.stream
;
1255 /* find the last static symbol in the file */
1256 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1257 if (sym
->pos
.stream
!= stream
)
1259 if (sym
->type
!= SYM_NODE
)
1261 base
= get_base_type(sym
);
1264 if (base
->type
!= SYM_FN
)
1266 if (!base
->inline_stmt
)
1268 add_inline_function(sym
);
1269 } END_FOR_EACH_PTR_REVERSE(sym
);
1274 static void split_inlines(struct symbol_list
*sym_list
)
1278 sym
= get_last_scoped_symbol(sym_list
, 0);
1280 split_inlines_in_scope(sym
);
1281 sym
= get_last_scoped_symbol(sym_list
, 1);
1283 split_inlines_in_scope(sym
);
1286 static void split_functions(struct symbol_list
*sym_list
)
1290 FOR_EACH_PTR(sym_list
, sym
) {
1291 set_position(sym
->pos
);
1292 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1293 split_function(sym
);
1296 __pass_to_client(sym
, BASE_HOOK
);
1297 fake_global_assign(sym
);
1299 } END_FOR_EACH_PTR(sym
);
1300 split_inlines(sym_list
);
1301 __pass_to_client(sym_list
, END_FILE_HOOK
);
1304 void smatch(int argc
, char **argv
)
1307 struct string_list
*filelist
= NULL
;
1308 struct symbol_list
*sym_list
;
1311 printf("Usage: smatch [--debug] <filename.c>\n");
1314 sparse_initialize(argc
, argv
, &filelist
);
1315 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1316 if (option_file_output
) {
1319 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1320 sm_outfd
= fopen(buf
, "w");
1322 printf("Error: Cannot open %s\n", base_file
);
1326 sym_list
= sparse_keep_tokens(base_file
);
1327 split_functions(sym_list
);
1328 } END_FOR_EACH_PTR_NOTAG(base_file
);