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 int __expr_stmt_count
;
42 int __in_function_def
;
43 static struct expression_list
*switch_expr_stack
= NULL
;
44 static struct expression_list
*post_op_stack
= NULL
;
46 struct expression_list
*big_expression_stack
;
47 struct statement_list
*big_statement_stack
;
48 int __in_pre_condition
= 0;
49 int __bail_on_rest_of_function
= 0;
50 char *get_function(void) { return cur_func
; }
51 int get_lineno(void) { return __smatch_lineno
; }
52 int inside_loop(void) { return !!loop_count
; }
53 int definitely_inside_loop(void) { return !!(loop_count
& ~0x80000000); }
54 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
55 int in_expression_statement(void) { return !!__expr_stmt_count
; }
57 static void split_symlist(struct symbol_list
*sym_list
);
58 static void split_declaration(struct symbol_list
*sym_list
);
59 static void split_expr_list(struct expression_list
*expr_list
);
60 static void add_inline_function(struct symbol
*sym
);
61 static void parse_inline(struct expression
*expr
);
63 int option_assume_loops
= 0;
64 int option_known_conditions
= 0;
65 int option_two_passes
= 0;
66 struct symbol
*cur_func_sym
= NULL
;
68 int outside_of_function(void)
70 return cur_func_sym
== NULL
;
73 const char *get_filename(void)
82 const char *get_base_file(void)
87 static void set_position(struct position pos
)
90 static int prev_stream
= -1;
92 if (pos
.stream
== 0 && pos
.line
== 0)
95 __smatch_lineno
= pos
.line
;
97 if (pos
.stream
== prev_stream
)
100 filename
= stream_name(pos
.stream
);
103 pathname
= getcwd(NULL
, 0);
105 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
106 full_filename
= malloc(len
);
107 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
109 full_filename
= alloc_string(filename
);
114 static int is_inline_func(struct expression
*expr
)
116 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
118 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
123 static int is_noreturn_func(struct expression
*expr
)
125 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
127 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
132 int inlinable(struct expression
*expr
)
136 if (__inline_fn
) /* don't nest */
139 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
141 if (is_no_inline_function(expr
->symbol
->ident
->name
))
143 sym
= get_base_type(expr
->symbol
);
144 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
145 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
149 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
150 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
157 void __process_post_op_stack(void)
159 struct expression
*expr
;
161 FOR_EACH_PTR(post_op_stack
, expr
) {
162 __pass_to_client(expr
, OP_HOOK
);
163 } END_FOR_EACH_PTR(expr
);
165 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
168 void __split_expr(struct expression
*expr
)
173 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
175 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
177 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
180 push_expression(&big_expression_stack
, expr
);
181 set_position(expr
->pos
);
182 __pass_to_client(expr
, EXPR_HOOK
);
184 switch (expr
->type
) {
187 __pass_to_client(expr
, DEREF_HOOK
);
188 __split_expr(expr
->unop
);
189 __pass_to_client(expr
, OP_HOOK
);
192 __split_expr(expr
->unop
);
193 push_expression(&post_op_stack
, expr
);
197 __split_stmt(expr
->statement
);
202 __pass_to_client(expr
, LOGIC_HOOK
);
203 __handle_logic(expr
);
206 __pass_to_client(expr
, BINOP_HOOK
);
208 __split_expr(expr
->left
);
209 __process_post_op_stack();
210 __split_expr(expr
->right
);
212 case EXPR_ASSIGNMENT
: {
213 struct expression
*tmp
;
218 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
221 if (__handle_condition_assigns(expr
))
223 /* foo = (x < 5 ? foo : 5); */
224 if (__handle_select_assigns(expr
))
226 /* foo = ({frob(); frob(); frob(); 1;}) */
227 if (__handle_expr_statement_assigns(expr
))
230 __split_expr(expr
->right
);
231 if (outside_of_function())
232 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
234 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
236 __fake_struct_member_assignments(expr
);
238 tmp
= strip_expr(expr
->right
);
239 if (tmp
->type
== EXPR_CALL
)
240 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
241 if (get_macro_name(tmp
->pos
) &&
242 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
243 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
244 __split_expr(expr
->left
);
248 __pass_to_client(expr
, DEREF_HOOK
);
249 __split_expr(expr
->deref
);
252 __split_expr(expr
->base
);
255 case EXPR_FORCE_CAST
:
256 __pass_to_client(expr
, CAST_HOOK
);
257 __split_expr(expr
->cast_expression
);
260 if (expr
->cast_expression
)
261 __pass_to_client(strip_parens(expr
->cast_expression
),
266 evaluate_expression(expr
);
268 case EXPR_CONDITIONAL
:
270 if (known_condition_true(expr
->conditional
)) {
271 __split_expr(expr
->cond_true
);
274 if (known_condition_false(expr
->conditional
)) {
275 __split_expr(expr
->cond_false
);
278 __pass_to_client(expr
, SELECT_HOOK
);
279 __split_whole_condition(expr
->conditional
);
280 __split_expr(expr
->cond_true
);
281 __push_true_states();
282 __use_false_states();
283 __split_expr(expr
->cond_false
);
284 __merge_true_states();
287 if (sym_name_is("__builtin_constant_p", expr
->fn
))
289 split_expr_list(expr
->args
);
290 __split_expr(expr
->fn
);
291 if (is_inline_func(expr
->fn
))
292 add_inline_function(expr
->fn
->symbol
);
293 if (inlinable(expr
->fn
))
295 __process_post_op_stack();
296 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
298 if (inlinable(expr
->fn
)) {
301 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
302 if (is_noreturn_func(expr
->fn
))
305 case EXPR_INITIALIZER
:
306 split_expr_list(expr
->expr_list
);
308 case EXPR_IDENTIFIER
:
309 __split_expr(expr
->ident_expression
);
312 __split_expr(expr
->idx_expression
);
315 __split_expr(expr
->init_expr
);
318 __pass_to_client(expr
, SYM_HOOK
);
321 __pass_to_client(expr
, STRING_HOOK
);
326 pop_expression(&big_expression_stack
);
329 static int is_forever_loop(struct statement
*stmt
)
331 struct expression
*expr
;
333 expr
= strip_expr(stmt
->iterator_pre_condition
);
335 expr
= stmt
->iterator_post_condition
;
337 /* this is a for(;;) loop... */
341 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
348 static char *get_loop_name(int num
)
352 snprintf(buf
, 255, "-loop%d", num
);
354 return alloc_sname(buf
);
358 * Pre Loops are while and for loops.
360 static void handle_pre_loop(struct statement
*stmt
)
362 int once_through
; /* we go through the loop at least once */
363 struct sm_state
*extra_sm
= NULL
;
366 struct state_list
*slist
= NULL
;
367 struct sm_state
*sm
= NULL
;
369 loop_name
= get_loop_name(loop_num
);
372 __split_stmt(stmt
->iterator_pre_statement
);
374 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
380 __merge_gotos(loop_name
);
382 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
383 __in_pre_condition
++;
384 __pass_to_client(stmt
, PRELOOP_HOOK
);
385 __split_whole_condition(stmt
->iterator_pre_condition
);
386 __in_pre_condition
--;
387 FOR_EACH_PTR(slist
, sm
) {
388 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
389 } END_FOR_EACH_PTR(sm
);
392 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
394 if (option_assume_loops
)
397 __split_stmt(stmt
->iterator_statement
);
398 __warn_on_silly_pre_loops();
399 if (is_forever_loop(stmt
)) {
400 struct state_list
*slist
;
402 __save_gotos(loop_name
);
404 __push_fake_cur_slist();
405 __split_stmt(stmt
->iterator_post_statement
);
406 slist
= __pop_fake_cur_slist();
408 __discard_continues();
409 __discard_false_states();
412 if (!__path_is_null())
413 __merge_slist_into_cur(slist
);
417 unchanged
= __iterator_unchanged(extra_sm
);
418 __split_stmt(stmt
->iterator_post_statement
);
419 __save_gotos(loop_name
);
420 __split_whole_condition(stmt
->iterator_pre_condition
);
422 __merge_false_states();
424 __discard_false_states();
426 __merge_false_states();
428 if (extra_sm
&& unchanged
)
429 __extra_pre_loop_hook_after(extra_sm
,
430 stmt
->iterator_post_statement
,
431 stmt
->iterator_pre_condition
);
438 * Post loops are do {} while();
440 static void handle_post_loop(struct statement
*stmt
)
444 loop_name
= get_loop_name(loop_num
);
450 __merge_gotos(loop_name
);
451 __split_stmt(stmt
->iterator_statement
);
453 if (!is_zero(stmt
->iterator_post_condition
))
454 __save_gotos(loop_name
);
456 if (is_forever_loop(stmt
)) {
459 __split_whole_condition(stmt
->iterator_post_condition
);
460 __use_false_states();
466 static int empty_statement(struct statement
*stmt
)
470 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
475 static int last_stmt_on_same_line()
477 struct statement
*stmt
;
480 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
483 if (stmt
->pos
.line
== get_lineno())
486 } END_FOR_EACH_PTR_REVERSE(stmt
);
490 static struct statement
*last_stmt
;
491 static int is_last_stmt(struct statement
*stmt
)
493 if (stmt
== last_stmt
)
498 static void print_unreached_initializers(struct symbol_list
*sym_list
)
502 FOR_EACH_PTR(sym_list
, sym
) {
503 if (sym
->initializer
)
504 sm_msg("info: '%s' is not actually initialized (unreached code).",
505 (sym
->ident
? sym
->ident
->name
: "this variable"));
506 } END_FOR_EACH_PTR(sym
);
509 static void print_unreached(struct statement
*stmt
)
511 static int print
= 1;
516 if (!__path_is_null()) {
523 switch (stmt
->type
) {
524 case STMT_COMPOUND
: /* after a switch before a case stmt */
529 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
530 print_unreached_initializers(stmt
->declaration
);
532 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
533 if (is_last_stmt(stmt
))
537 /* people put extra breaks inside switch statements */
538 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
&&
539 strcmp(stmt
->goto_label
->ident
->name
, "break") == 0)
545 if (empty_statement(stmt
))
549 sm_msg("info: ignoring unreachable code.");
553 static void split_asm_constraints(struct expression_list
*expr_list
)
555 struct expression
*expr
;
558 FOR_EACH_PTR(expr_list
, expr
) {
560 case 0: /* identifier */
561 case 1: /* constraint */
564 case 2: /* expression */
569 } END_FOR_EACH_PTR(expr
);
572 static int is_case_val(struct statement
*stmt
, sval_t sval
)
576 if (stmt
->type
!= STMT_CASE
)
578 if (!stmt
->case_expression
) {
582 if (!get_value(stmt
->case_expression
, &case_sval
))
584 if (case_sval
.value
== sval
.value
)
589 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
591 struct statement
*tmp
;
593 __split_expr(stmt
->switch_expression
);
595 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
596 __save_switch_states(top_expression(switch_expr_stack
));
601 stmt
= stmt
->switch_statement
;
604 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
606 __push_scope_hooks();
607 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
608 __smatch_lineno
= tmp
->pos
.line
;
609 if (is_case_val(tmp
, sval
)) {
610 __merge_switches(top_expression(switch_expr_stack
),
611 stmt
->case_expression
);
612 __pass_case_to_client(top_expression(switch_expr_stack
),
613 stmt
->case_expression
);
615 if (__path_is_null())
618 if (__path_is_null()) {
622 } END_FOR_EACH_PTR(tmp
);
624 __call_scope_hooks();
625 if (!__pop_default())
626 __merge_switches(top_expression(switch_expr_stack
),
628 __discard_switches();
630 pop_expression(&switch_expr_stack
);
633 void __split_stmt(struct statement
*stmt
)
640 if (out_of_memory() || __bail_on_rest_of_function
) {
641 static char *printed
= NULL
;
643 if (printed
!= cur_func
)
644 sm_msg("Function too hairy. Giving up.");
645 final_pass
= 0; /* turn off sm_msg() from here */
650 add_ptr_list(&big_statement_stack
, stmt
);
651 free_expression_stack(&big_expression_stack
);
652 set_position(stmt
->pos
);
653 print_unreached(stmt
);
654 __pass_to_client(stmt
, STMT_HOOK
);
656 switch (stmt
->type
) {
657 case STMT_DECLARATION
:
658 split_declaration(stmt
->declaration
);
661 __split_expr(stmt
->ret_value
);
662 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
665 case STMT_EXPRESSION
:
666 __split_expr(stmt
->expression
);
668 case STMT_COMPOUND
: {
669 struct statement
*tmp
;
672 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
673 __push_scope_hooks();
674 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
676 } END_FOR_EACH_PTR(tmp
);
677 __call_scope_hooks();
681 if (known_condition_true(stmt
->if_conditional
)) {
682 __split_stmt(stmt
->if_true
);
685 if (known_condition_false(stmt
->if_conditional
)) {
686 __split_stmt(stmt
->if_false
);
689 if (option_known_conditions
&&
690 implied_condition_true(stmt
->if_conditional
)) {
691 sm_info("this condition is true.");
692 __split_stmt(stmt
->if_true
);
695 if (option_known_conditions
&&
696 implied_condition_false(stmt
->if_conditional
)) {
697 sm_info("this condition is false.");
698 __split_stmt(stmt
->if_false
);
701 __split_whole_condition(stmt
->if_conditional
);
702 __split_stmt(stmt
->if_true
);
703 if (empty_statement(stmt
->if_true
) &&
704 last_stmt_on_same_line() &&
705 !get_macro_name(stmt
->if_true
->pos
))
706 sm_msg("warn: if();");
707 __push_true_states();
708 __use_false_states();
709 __split_stmt(stmt
->if_false
);
710 __merge_true_states();
713 if (stmt
->iterator_pre_condition
)
714 handle_pre_loop(stmt
);
715 else if (stmt
->iterator_post_condition
)
716 handle_post_loop(stmt
);
718 // these are for(;;) type loops.
719 handle_pre_loop(stmt
);
723 if (get_value(stmt
->switch_expression
, &sval
)) {
724 split_known_switch(stmt
, sval
);
727 __split_expr(stmt
->switch_expression
);
728 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
729 __save_switch_states(top_expression(switch_expr_stack
));
733 __split_stmt(stmt
->switch_statement
);
734 if (!__pop_default())
735 __merge_switches(top_expression(switch_expr_stack
),
737 __discard_switches();
739 pop_expression(&switch_expr_stack
);
742 __merge_switches(top_expression(switch_expr_stack
),
743 stmt
->case_expression
);
744 __pass_case_to_client(top_expression(switch_expr_stack
),
745 stmt
->case_expression
);
746 if (!stmt
->case_expression
)
748 __split_expr(stmt
->case_expression
);
749 __split_expr(stmt
->case_to
);
750 __split_stmt(stmt
->case_statement
);
753 if (stmt
->label_identifier
&&
754 stmt
->label_identifier
->type
== SYM_LABEL
&&
755 stmt
->label_identifier
->ident
) {
756 loop_count
|= 0x80000000;
757 __merge_gotos(stmt
->label_identifier
->ident
->name
);
759 __split_stmt(stmt
->label_statement
);
762 __split_expr(stmt
->goto_expression
);
763 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
764 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
766 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
768 __process_continues();
770 } else if (stmt
->goto_label
&&
771 stmt
->goto_label
->type
== SYM_LABEL
&&
772 stmt
->goto_label
->ident
) {
773 __save_gotos(stmt
->goto_label
->ident
->name
);
780 __pass_to_client(stmt
, ASM_HOOK
);
781 __split_expr(stmt
->asm_string
);
782 split_asm_constraints(stmt
->asm_outputs
);
783 split_asm_constraints(stmt
->asm_inputs
);
784 split_asm_constraints(stmt
->asm_clobbers
);
789 __split_expr(stmt
->range_expression
);
790 __split_expr(stmt
->range_low
);
791 __split_expr(stmt
->range_high
);
794 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
796 __process_post_op_stack();
799 static void split_expr_list(struct expression_list
*expr_list
)
801 struct expression
*expr
;
803 FOR_EACH_PTR(expr_list
, expr
) {
805 __process_post_op_stack();
806 } END_FOR_EACH_PTR(expr
);
809 static void split_sym(struct symbol
*sym
)
813 if (!(sym
->namespace & NS_SYMBOL
))
816 __split_stmt(sym
->stmt
);
817 __split_expr(sym
->array_size
);
818 split_symlist(sym
->arguments
);
819 split_symlist(sym
->symbol_list
);
820 __split_stmt(sym
->inline_stmt
);
821 split_symlist(sym
->inline_symbol_list
);
824 static void split_symlist(struct symbol_list
*sym_list
)
828 FOR_EACH_PTR(sym_list
, sym
) {
830 } END_FOR_EACH_PTR(sym
);
833 typedef void (fake_cb
)(struct expression
*expr
);
835 static int member_to_number(struct expression
*expr
, struct ident
*member
)
837 struct symbol
*type
, *tmp
;
845 type
= get_type(expr
);
846 if (!type
|| type
->type
!= SYM_STRUCT
)
850 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
854 if (strcmp(name
, tmp
->ident
->name
) == 0)
856 } END_FOR_EACH_PTR(tmp
);
860 static struct ident
*number_to_member(struct expression
*expr
, int num
)
862 struct symbol
*type
, *member
;
865 type
= get_type(expr
);
866 if (!type
|| type
->type
!= SYM_STRUCT
)
869 FOR_EACH_PTR(type
->symbol_list
, member
) {
871 return member
->ident
;
873 } END_FOR_EACH_PTR(member
);
877 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
884 static struct member_set
*alloc_member_set(struct symbol
*type
)
886 struct member_set
*member_set
;
887 struct symbol
*member
;
891 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
892 member_set
= malloc(member_count
* sizeof(*member_set
));
894 FOR_EACH_PTR(type
->symbol_list
, member
) {
895 member_set
[member_idx
].ident
= member
->ident
;
896 member_set
[member_idx
].set
= 0;
898 } END_FOR_EACH_PTR(member
);
903 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
905 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
908 for (i
= 0; i
< member_count
; i
++) {
909 if (member_set
[i
].ident
== ident
) {
910 member_set
[i
].set
= 1;
914 // crap. this is buggy.
915 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
918 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
920 struct expression
*deref
, *assign
;
921 struct symbol
*member
, *member_type
;
925 FOR_EACH_PTR(type
->symbol_list
, member
) {
926 if (!member
->ident
|| member_set
[member_idx
].set
) {
930 member_type
= get_real_base_type(member
);
931 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
935 /* TODO: this should be handled recursively and not ignored */
936 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
940 deref
= member_expression(symbol
, '.', member
->ident
);
941 assign
= assign_expression(deref
, zero_expr());
942 __split_expr(assign
);
944 } END_FOR_EACH_PTR(member
);
948 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
950 struct expression
*deref
, *assign
, *tmp
;
951 struct symbol
*struct_type
, *type
;
952 struct ident
*member
;
954 struct member_set
*member_set
;
956 struct_type
= get_type(symbol
);
958 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
961 member_set
= alloc_member_set(struct_type
);
964 FOR_EACH_PTR(members
, tmp
) {
965 member
= number_to_member(symbol
, member_idx
);
966 while (tmp
->type
== EXPR_IDENTIFIER
) {
967 member
= tmp
->expr_ident
;
968 member_idx
= member_to_number(symbol
, member
);
969 tmp
= tmp
->ident_expression
;
971 mark_member_as_set(struct_type
, member_set
, member
);
973 deref
= member_expression(symbol
, '.', member
);
974 if (tmp
->type
== EXPR_INITIALIZER
) {
975 type
= get_type(deref
);
976 if (type
&& type
->type
== SYM_ARRAY
)
977 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
979 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
981 assign
= assign_expression(deref
, tmp
);
984 } END_FOR_EACH_PTR(tmp
);
986 set_unset_to_zero(symbol
, struct_type
, member_set
);
989 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
991 fake_member_assigns_helper(symbol_expression(sym
),
992 sym
->initializer
->expr_list
, fake_cb
);
995 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
997 struct expression
*offset
, *binop
, *assign
, *tmp
;
1002 FOR_EACH_PTR(expr_list
, tmp
) {
1003 if (tmp
->type
== EXPR_INDEX
) {
1004 if (tmp
->idx_from
!= tmp
->idx_to
)
1006 idx
= tmp
->idx_from
;
1007 if (!tmp
->idx_expression
)
1009 tmp
= tmp
->idx_expression
;
1011 offset
= value_expr(idx
);
1012 binop
= array_element_expression(array
, offset
);
1013 if (tmp
->type
== EXPR_INITIALIZER
) {
1014 type
= get_type(binop
);
1015 if (type
&& type
->type
== SYM_ARRAY
)
1016 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1018 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1020 assign
= assign_expression(binop
, tmp
);
1025 } END_FOR_EACH_PTR(tmp
);
1028 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1030 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1033 static void fake_assign_expr(struct symbol
*sym
)
1035 struct expression
*assign
, *symbol
;
1037 symbol
= symbol_expression(sym
);
1038 assign
= assign_expression(symbol
, sym
->initializer
);
1039 __split_expr(assign
);
1042 static void call_split_expr(struct expression
*expr
)
1047 static void do_initializer_stuff(struct symbol
*sym
)
1049 if (!sym
->initializer
)
1052 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1053 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1054 fake_element_assigns(sym
, call_split_expr
);
1056 fake_member_assigns(sym
, call_split_expr
);
1058 fake_assign_expr(sym
);
1062 static void split_declaration(struct symbol_list
*sym_list
)
1066 FOR_EACH_PTR(sym_list
, sym
) {
1067 __pass_to_client(sym
, DECLARATION_HOOK
);
1068 do_initializer_stuff(sym
);
1070 } END_FOR_EACH_PTR(sym
);
1073 static void call_global_assign_hooks(struct expression
*assign
)
1075 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1078 static void fake_global_assign(struct symbol
*sym
)
1080 struct expression
*assign
, *symbol
;
1082 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1083 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1084 fake_element_assigns(sym
, call_global_assign_hooks
);
1085 } else if (sym
->initializer
) {
1086 symbol
= symbol_expression(sym
);
1087 assign
= assign_expression(symbol
, sym
->initializer
);
1088 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1090 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1092 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1093 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1094 fake_member_assigns(sym
, call_global_assign_hooks
);
1095 } else if (sym
->initializer
) {
1096 symbol
= symbol_expression(sym
);
1097 assign
= assign_expression(symbol
, sym
->initializer
);
1098 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1100 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1103 symbol
= symbol_expression(sym
);
1104 if (sym
->initializer
)
1105 assign
= assign_expression(symbol
, sym
->initializer
);
1107 assign
= assign_expression(symbol
, zero_expr());
1108 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1112 static void start_function_definition(struct symbol
*sym
)
1114 __in_function_def
= 1;
1115 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1116 __in_function_def
= 0;
1117 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1121 static void split_function(struct symbol
*sym
)
1123 struct symbol
*base_type
= get_base_type(sym
);
1127 cur_func
= sym
->ident
->name
;
1128 __smatch_lineno
= sym
->pos
.line
;
1131 sm_debug("new function: %s\n", cur_func
);
1133 if (option_two_passes
) {
1137 start_function_definition(sym
);
1138 __split_stmt(base_type
->stmt
);
1139 __split_stmt(base_type
->inline_stmt
);
1144 start_function_definition(sym
);
1145 __split_stmt(base_type
->stmt
);
1146 __split_stmt(base_type
->inline_stmt
);
1147 __pass_to_client(sym
, END_FUNC_HOOK
);
1148 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1149 cur_func_sym
= NULL
;
1152 free_data_info_allocs();
1153 free_expression_stack(&switch_expr_stack
);
1154 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1155 __bail_on_rest_of_function
= 0;
1158 static void parse_inline(struct expression
*call
)
1160 struct symbol
*base_type
;
1161 int loop_num_bak
= loop_num
;
1162 int final_pass_bak
= final_pass
;
1163 char *cur_func_bak
= cur_func
;
1164 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1165 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1166 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1167 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1169 __pass_to_client(call
, INLINE_FN_START
);
1170 final_pass
= 0; /* don't print anything */
1173 base_type
= get_base_type(call
->fn
->symbol
);
1174 cur_func_sym
= call
->fn
->symbol
;
1175 if (call
->fn
->symbol
->ident
)
1176 cur_func
= call
->fn
->symbol
->ident
->name
;
1179 set_position(call
->fn
->symbol
->pos
);
1182 nullify_all_states();
1183 big_statement_stack
= NULL
;
1184 big_expression_stack
= NULL
;
1185 switch_expr_stack
= NULL
;
1187 sm_debug("inline function: %s\n", cur_func
);
1190 start_function_definition(call
->fn
->symbol
);
1191 __split_stmt(base_type
->stmt
);
1192 __split_stmt(base_type
->inline_stmt
);
1193 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1194 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1196 free_expression_stack(&switch_expr_stack
);
1197 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1200 loop_num
= loop_num_bak
;
1201 final_pass
= final_pass_bak
;
1202 cur_func_sym
= cur_func_sym_bak
;
1203 cur_func
= cur_func_bak
;
1204 big_statement_stack
= big_statement_stack_bak
;
1205 big_expression_stack
= big_expression_stack_bak
;
1206 switch_expr_stack
= switch_expr_stack_bak
;
1208 restore_all_states();
1209 set_position(call
->pos
);
1211 __pass_to_client(call
, INLINE_FN_END
);
1214 static struct symbol_list
*inlines_called
;
1215 static void add_inline_function(struct symbol
*sym
)
1217 static struct symbol_list
*already_added
;
1220 FOR_EACH_PTR(already_added
, tmp
) {
1223 } END_FOR_EACH_PTR(tmp
);
1225 add_ptr_list(&already_added
, sym
);
1226 add_ptr_list(&inlines_called
, sym
);
1229 static void process_inlines()
1233 FOR_EACH_PTR(inlines_called
, tmp
) {
1234 split_function(tmp
);
1235 } END_FOR_EACH_PTR(tmp
);
1236 free_ptr_list(&inlines_called
);
1239 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1243 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1246 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1248 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1250 } END_FOR_EACH_PTR_REVERSE(sym
);
1255 static void split_inlines_in_scope(struct symbol
*sym
)
1257 struct symbol
*base
;
1258 struct symbol_list
*scope_list
;
1261 scope_list
= sym
->scope
->symbols
;
1262 stream
= sym
->pos
.stream
;
1264 /* find the last static symbol in the file */
1265 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1266 if (sym
->pos
.stream
!= stream
)
1268 if (sym
->type
!= SYM_NODE
)
1270 base
= get_base_type(sym
);
1273 if (base
->type
!= SYM_FN
)
1275 if (!base
->inline_stmt
)
1277 add_inline_function(sym
);
1278 } END_FOR_EACH_PTR_REVERSE(sym
);
1283 static void split_inlines(struct symbol_list
*sym_list
)
1287 sym
= get_last_scoped_symbol(sym_list
, 0);
1289 split_inlines_in_scope(sym
);
1290 sym
= get_last_scoped_symbol(sym_list
, 1);
1292 split_inlines_in_scope(sym
);
1295 static void split_functions(struct symbol_list
*sym_list
)
1299 FOR_EACH_PTR(sym_list
, sym
) {
1300 set_position(sym
->pos
);
1301 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1302 split_function(sym
);
1305 __pass_to_client(sym
, BASE_HOOK
);
1306 fake_global_assign(sym
);
1308 } END_FOR_EACH_PTR(sym
);
1309 split_inlines(sym_list
);
1310 __pass_to_client(sym_list
, END_FILE_HOOK
);
1313 void smatch(int argc
, char **argv
)
1316 struct string_list
*filelist
= NULL
;
1317 struct symbol_list
*sym_list
;
1320 printf("Usage: smatch [--debug] <filename.c>\n");
1323 sparse_initialize(argc
, argv
, &filelist
);
1324 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1325 if (option_file_output
) {
1328 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1329 sm_outfd
= fopen(buf
, "w");
1331 printf("Error: Cannot open %s\n", base_file
);
1335 sym_list
= sparse_keep_tokens(base_file
);
1336 split_functions(sym_list
);
1337 } END_FOR_EACH_PTR_NOTAG(base_file
);