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 stree
*stree
= 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
, &stree
);
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_SM(stree
, sm
) {
388 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
389 } END_FOR_EACH_SM(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 __save_gotos(loop_name
);
402 __push_fake_cur_stree();
403 __split_stmt(stmt
->iterator_post_statement
);
404 stree
= __pop_fake_cur_stree();
406 __discard_continues();
407 __discard_false_states();
410 if (!__path_is_null())
411 __merge_stree_into_cur(stree
);
415 unchanged
= __iterator_unchanged(extra_sm
);
416 __split_stmt(stmt
->iterator_post_statement
);
417 __save_gotos(loop_name
);
418 __split_whole_condition(stmt
->iterator_pre_condition
);
420 __merge_false_states();
422 __discard_false_states();
424 __merge_false_states();
426 if (extra_sm
&& unchanged
)
427 __extra_pre_loop_hook_after(extra_sm
,
428 stmt
->iterator_post_statement
,
429 stmt
->iterator_pre_condition
);
436 * Post loops are do {} while();
438 static void handle_post_loop(struct statement
*stmt
)
442 loop_name
= get_loop_name(loop_num
);
448 __merge_gotos(loop_name
);
449 __split_stmt(stmt
->iterator_statement
);
451 if (!is_zero(stmt
->iterator_post_condition
))
452 __save_gotos(loop_name
);
454 if (is_forever_loop(stmt
)) {
457 __split_whole_condition(stmt
->iterator_post_condition
);
458 __use_false_states();
464 static int empty_statement(struct statement
*stmt
)
468 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
473 static int last_stmt_on_same_line()
475 struct statement
*stmt
;
478 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
481 if (stmt
->pos
.line
== get_lineno())
484 } END_FOR_EACH_PTR_REVERSE(stmt
);
488 static struct statement
*last_stmt
;
489 static int is_last_stmt(struct statement
*stmt
)
491 if (stmt
== last_stmt
)
496 static void print_unreached_initializers(struct symbol_list
*sym_list
)
500 FOR_EACH_PTR(sym_list
, sym
) {
501 if (sym
->initializer
)
502 sm_msg("info: '%s' is not actually initialized (unreached code).",
503 (sym
->ident
? sym
->ident
->name
: "this variable"));
504 } END_FOR_EACH_PTR(sym
);
507 static void print_unreached(struct statement
*stmt
)
509 static int print
= 1;
514 if (!__path_is_null()) {
521 switch (stmt
->type
) {
522 case STMT_COMPOUND
: /* after a switch before a case stmt */
527 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
528 print_unreached_initializers(stmt
->declaration
);
530 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
531 if (is_last_stmt(stmt
))
535 /* people put extra breaks inside switch statements */
536 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
&&
537 strcmp(stmt
->goto_label
->ident
->name
, "break") == 0)
543 if (empty_statement(stmt
))
547 sm_msg("info: ignoring unreachable code.");
551 static void split_asm_constraints(struct expression_list
*expr_list
)
553 struct expression
*expr
;
556 FOR_EACH_PTR(expr_list
, expr
) {
558 case 0: /* identifier */
559 case 1: /* constraint */
562 case 2: /* expression */
567 } END_FOR_EACH_PTR(expr
);
570 static int is_case_val(struct statement
*stmt
, sval_t sval
)
574 if (stmt
->type
!= STMT_CASE
)
576 if (!stmt
->case_expression
) {
580 if (!get_value(stmt
->case_expression
, &case_sval
))
582 if (case_sval
.value
== sval
.value
)
587 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
589 struct statement
*tmp
;
591 __split_expr(stmt
->switch_expression
);
593 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
594 __save_switch_states(top_expression(switch_expr_stack
));
599 stmt
= stmt
->switch_statement
;
602 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
604 __push_scope_hooks();
605 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
606 __smatch_lineno
= tmp
->pos
.line
;
607 if (is_case_val(tmp
, sval
)) {
608 __merge_switches(top_expression(switch_expr_stack
),
609 stmt
->case_expression
);
610 __pass_case_to_client(top_expression(switch_expr_stack
),
611 stmt
->case_expression
);
613 if (__path_is_null())
616 if (__path_is_null()) {
620 } END_FOR_EACH_PTR(tmp
);
622 __call_scope_hooks();
623 if (!__pop_default())
624 __merge_switches(top_expression(switch_expr_stack
),
626 __discard_switches();
628 pop_expression(&switch_expr_stack
);
631 void __split_stmt(struct statement
*stmt
)
638 if (out_of_memory() || __bail_on_rest_of_function
) {
639 static char *printed
= NULL
;
641 if (printed
!= cur_func
)
642 sm_msg("Function too hairy. Giving up.");
643 final_pass
= 0; /* turn off sm_msg() from here */
648 add_ptr_list(&big_statement_stack
, stmt
);
649 free_expression_stack(&big_expression_stack
);
650 set_position(stmt
->pos
);
651 print_unreached(stmt
);
652 __pass_to_client(stmt
, STMT_HOOK
);
654 switch (stmt
->type
) {
655 case STMT_DECLARATION
:
656 split_declaration(stmt
->declaration
);
659 __split_expr(stmt
->ret_value
);
660 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
663 case STMT_EXPRESSION
:
664 __split_expr(stmt
->expression
);
666 case STMT_COMPOUND
: {
667 struct statement
*tmp
;
670 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
671 __push_scope_hooks();
672 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
674 } END_FOR_EACH_PTR(tmp
);
675 __call_scope_hooks();
679 if (known_condition_true(stmt
->if_conditional
)) {
680 __split_stmt(stmt
->if_true
);
683 if (known_condition_false(stmt
->if_conditional
)) {
684 __split_stmt(stmt
->if_false
);
687 if (option_known_conditions
&&
688 implied_condition_true(stmt
->if_conditional
)) {
689 sm_info("this condition is true.");
690 __split_stmt(stmt
->if_true
);
693 if (option_known_conditions
&&
694 implied_condition_false(stmt
->if_conditional
)) {
695 sm_info("this condition is false.");
696 __split_stmt(stmt
->if_false
);
699 __split_whole_condition(stmt
->if_conditional
);
700 __split_stmt(stmt
->if_true
);
701 if (empty_statement(stmt
->if_true
) &&
702 last_stmt_on_same_line() &&
703 !get_macro_name(stmt
->if_true
->pos
))
704 sm_msg("warn: if();");
705 __push_true_states();
706 __use_false_states();
707 __split_stmt(stmt
->if_false
);
708 __merge_true_states();
711 if (stmt
->iterator_pre_condition
)
712 handle_pre_loop(stmt
);
713 else if (stmt
->iterator_post_condition
)
714 handle_post_loop(stmt
);
716 // these are for(;;) type loops.
717 handle_pre_loop(stmt
);
721 if (get_value(stmt
->switch_expression
, &sval
)) {
722 split_known_switch(stmt
, sval
);
725 __split_expr(stmt
->switch_expression
);
726 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
727 __save_switch_states(top_expression(switch_expr_stack
));
731 __split_stmt(stmt
->switch_statement
);
732 if (!__pop_default())
733 __merge_switches(top_expression(switch_expr_stack
),
735 __discard_switches();
737 pop_expression(&switch_expr_stack
);
740 __merge_switches(top_expression(switch_expr_stack
),
741 stmt
->case_expression
);
742 __pass_case_to_client(top_expression(switch_expr_stack
),
743 stmt
->case_expression
);
744 if (!stmt
->case_expression
)
746 __split_expr(stmt
->case_expression
);
747 __split_expr(stmt
->case_to
);
748 __split_stmt(stmt
->case_statement
);
751 if (stmt
->label_identifier
&&
752 stmt
->label_identifier
->type
== SYM_LABEL
&&
753 stmt
->label_identifier
->ident
) {
754 loop_count
|= 0x80000000;
755 __merge_gotos(stmt
->label_identifier
->ident
->name
);
757 __split_stmt(stmt
->label_statement
);
760 __split_expr(stmt
->goto_expression
);
761 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
762 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
764 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
766 __process_continues();
768 } else if (stmt
->goto_label
&&
769 stmt
->goto_label
->type
== SYM_LABEL
&&
770 stmt
->goto_label
->ident
) {
771 __save_gotos(stmt
->goto_label
->ident
->name
);
778 __pass_to_client(stmt
, ASM_HOOK
);
779 __split_expr(stmt
->asm_string
);
780 split_asm_constraints(stmt
->asm_outputs
);
781 split_asm_constraints(stmt
->asm_inputs
);
782 split_asm_constraints(stmt
->asm_clobbers
);
787 __split_expr(stmt
->range_expression
);
788 __split_expr(stmt
->range_low
);
789 __split_expr(stmt
->range_high
);
792 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
794 __process_post_op_stack();
797 static void split_expr_list(struct expression_list
*expr_list
)
799 struct expression
*expr
;
801 FOR_EACH_PTR(expr_list
, expr
) {
803 __process_post_op_stack();
804 } END_FOR_EACH_PTR(expr
);
807 static void split_sym(struct symbol
*sym
)
811 if (!(sym
->namespace & NS_SYMBOL
))
814 __split_stmt(sym
->stmt
);
815 __split_expr(sym
->array_size
);
816 split_symlist(sym
->arguments
);
817 split_symlist(sym
->symbol_list
);
818 __split_stmt(sym
->inline_stmt
);
819 split_symlist(sym
->inline_symbol_list
);
822 static void split_symlist(struct symbol_list
*sym_list
)
826 FOR_EACH_PTR(sym_list
, sym
) {
828 } END_FOR_EACH_PTR(sym
);
831 typedef void (fake_cb
)(struct expression
*expr
);
833 static int member_to_number(struct expression
*expr
, struct ident
*member
)
835 struct symbol
*type
, *tmp
;
843 type
= get_type(expr
);
844 if (!type
|| type
->type
!= SYM_STRUCT
)
848 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
852 if (strcmp(name
, tmp
->ident
->name
) == 0)
854 } END_FOR_EACH_PTR(tmp
);
858 static struct ident
*number_to_member(struct expression
*expr
, int num
)
860 struct symbol
*type
, *member
;
863 type
= get_type(expr
);
864 if (!type
|| type
->type
!= SYM_STRUCT
)
867 FOR_EACH_PTR(type
->symbol_list
, member
) {
869 return member
->ident
;
871 } END_FOR_EACH_PTR(member
);
875 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
882 static struct member_set
*alloc_member_set(struct symbol
*type
)
884 struct member_set
*member_set
;
885 struct symbol
*member
;
889 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
890 member_set
= malloc(member_count
* sizeof(*member_set
));
892 FOR_EACH_PTR(type
->symbol_list
, member
) {
893 member_set
[member_idx
].ident
= member
->ident
;
894 member_set
[member_idx
].set
= 0;
896 } END_FOR_EACH_PTR(member
);
901 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
903 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
906 for (i
= 0; i
< member_count
; i
++) {
907 if (member_set
[i
].ident
== ident
) {
908 member_set
[i
].set
= 1;
912 // crap. this is buggy.
913 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
916 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
918 struct expression
*deref
, *assign
;
919 struct symbol
*member
, *member_type
;
923 FOR_EACH_PTR(type
->symbol_list
, member
) {
924 if (!member
->ident
|| member_set
[member_idx
].set
) {
928 member_type
= get_real_base_type(member
);
929 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
933 /* TODO: this should be handled recursively and not ignored */
934 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
938 deref
= member_expression(symbol
, '.', member
->ident
);
939 assign
= assign_expression(deref
, zero_expr());
940 __split_expr(assign
);
942 } END_FOR_EACH_PTR(member
);
946 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
948 struct expression
*deref
, *assign
, *tmp
;
949 struct symbol
*struct_type
, *type
;
950 struct ident
*member
;
952 struct member_set
*member_set
;
954 struct_type
= get_type(symbol
);
956 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
959 member_set
= alloc_member_set(struct_type
);
962 FOR_EACH_PTR(members
, tmp
) {
963 member
= number_to_member(symbol
, member_idx
);
964 while (tmp
->type
== EXPR_IDENTIFIER
) {
965 member
= tmp
->expr_ident
;
966 member_idx
= member_to_number(symbol
, member
);
967 tmp
= tmp
->ident_expression
;
969 mark_member_as_set(struct_type
, member_set
, member
);
971 deref
= member_expression(symbol
, '.', member
);
972 if (tmp
->type
== EXPR_INITIALIZER
) {
973 type
= get_type(deref
);
974 if (type
&& type
->type
== SYM_ARRAY
)
975 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
977 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
979 assign
= assign_expression(deref
, tmp
);
982 } END_FOR_EACH_PTR(tmp
);
984 set_unset_to_zero(symbol
, struct_type
, member_set
);
987 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
989 fake_member_assigns_helper(symbol_expression(sym
),
990 sym
->initializer
->expr_list
, fake_cb
);
993 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
995 struct expression
*offset
, *binop
, *assign
, *tmp
;
1000 FOR_EACH_PTR(expr_list
, tmp
) {
1001 if (tmp
->type
== EXPR_INDEX
) {
1002 if (tmp
->idx_from
!= tmp
->idx_to
)
1004 idx
= tmp
->idx_from
;
1005 if (!tmp
->idx_expression
)
1007 tmp
= tmp
->idx_expression
;
1009 offset
= value_expr(idx
);
1010 binop
= array_element_expression(array
, offset
);
1011 if (tmp
->type
== EXPR_INITIALIZER
) {
1012 type
= get_type(binop
);
1013 if (type
&& type
->type
== SYM_ARRAY
)
1014 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1016 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1018 assign
= assign_expression(binop
, tmp
);
1023 } END_FOR_EACH_PTR(tmp
);
1026 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1028 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1031 static void fake_assign_expr(struct symbol
*sym
)
1033 struct expression
*assign
, *symbol
;
1035 symbol
= symbol_expression(sym
);
1036 assign
= assign_expression(symbol
, sym
->initializer
);
1037 __split_expr(assign
);
1040 static void call_split_expr(struct expression
*expr
)
1045 static void do_initializer_stuff(struct symbol
*sym
)
1047 if (!sym
->initializer
)
1050 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1051 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1052 fake_element_assigns(sym
, call_split_expr
);
1054 fake_member_assigns(sym
, call_split_expr
);
1056 fake_assign_expr(sym
);
1060 static void split_declaration(struct symbol_list
*sym_list
)
1064 FOR_EACH_PTR(sym_list
, sym
) {
1065 __pass_to_client(sym
, DECLARATION_HOOK
);
1066 do_initializer_stuff(sym
);
1068 } END_FOR_EACH_PTR(sym
);
1071 static void call_global_assign_hooks(struct expression
*assign
)
1073 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1076 static void fake_global_assign(struct symbol
*sym
)
1078 struct expression
*assign
, *symbol
;
1080 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1081 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1082 fake_element_assigns(sym
, call_global_assign_hooks
);
1083 } else if (sym
->initializer
) {
1084 symbol
= symbol_expression(sym
);
1085 assign
= assign_expression(symbol
, sym
->initializer
);
1086 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1088 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1090 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1091 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1092 fake_member_assigns(sym
, call_global_assign_hooks
);
1093 } else if (sym
->initializer
) {
1094 symbol
= symbol_expression(sym
);
1095 assign
= assign_expression(symbol
, sym
->initializer
);
1096 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1098 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1101 symbol
= symbol_expression(sym
);
1102 if (sym
->initializer
)
1103 assign
= assign_expression(symbol
, sym
->initializer
);
1105 assign
= assign_expression(symbol
, zero_expr());
1106 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1110 static void start_function_definition(struct symbol
*sym
)
1112 __in_function_def
= 1;
1113 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1114 __in_function_def
= 0;
1115 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1119 static void split_function(struct symbol
*sym
)
1121 struct symbol
*base_type
= get_base_type(sym
);
1125 cur_func
= sym
->ident
->name
;
1126 __smatch_lineno
= sym
->pos
.line
;
1129 sm_debug("new function: %s\n", cur_func
);
1131 if (option_two_passes
) {
1135 start_function_definition(sym
);
1136 __split_stmt(base_type
->stmt
);
1137 __split_stmt(base_type
->inline_stmt
);
1142 start_function_definition(sym
);
1143 __split_stmt(base_type
->stmt
);
1144 __split_stmt(base_type
->inline_stmt
);
1145 __pass_to_client(sym
, END_FUNC_HOOK
);
1146 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1147 cur_func_sym
= NULL
;
1150 free_data_info_allocs();
1151 free_expression_stack(&switch_expr_stack
);
1152 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1153 __bail_on_rest_of_function
= 0;
1156 static void parse_inline(struct expression
*call
)
1158 struct symbol
*base_type
;
1159 int loop_num_bak
= loop_num
;
1160 int final_pass_bak
= final_pass
;
1161 char *cur_func_bak
= cur_func
;
1162 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1163 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1164 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1165 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1167 __pass_to_client(call
, INLINE_FN_START
);
1168 final_pass
= 0; /* don't print anything */
1171 base_type
= get_base_type(call
->fn
->symbol
);
1172 cur_func_sym
= call
->fn
->symbol
;
1173 if (call
->fn
->symbol
->ident
)
1174 cur_func
= call
->fn
->symbol
->ident
->name
;
1177 set_position(call
->fn
->symbol
->pos
);
1180 nullify_all_states();
1181 big_statement_stack
= NULL
;
1182 big_expression_stack
= NULL
;
1183 switch_expr_stack
= NULL
;
1185 sm_debug("inline function: %s\n", cur_func
);
1188 start_function_definition(call
->fn
->symbol
);
1189 __split_stmt(base_type
->stmt
);
1190 __split_stmt(base_type
->inline_stmt
);
1191 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1192 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1194 free_expression_stack(&switch_expr_stack
);
1195 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1198 loop_num
= loop_num_bak
;
1199 final_pass
= final_pass_bak
;
1200 cur_func_sym
= cur_func_sym_bak
;
1201 cur_func
= cur_func_bak
;
1202 big_statement_stack
= big_statement_stack_bak
;
1203 big_expression_stack
= big_expression_stack_bak
;
1204 switch_expr_stack
= switch_expr_stack_bak
;
1206 restore_all_states();
1207 set_position(call
->pos
);
1209 __pass_to_client(call
, INLINE_FN_END
);
1212 static struct symbol_list
*inlines_called
;
1213 static void add_inline_function(struct symbol
*sym
)
1215 static struct symbol_list
*already_added
;
1218 FOR_EACH_PTR(already_added
, tmp
) {
1221 } END_FOR_EACH_PTR(tmp
);
1223 add_ptr_list(&already_added
, sym
);
1224 add_ptr_list(&inlines_called
, sym
);
1227 static void process_inlines()
1231 FOR_EACH_PTR(inlines_called
, tmp
) {
1232 split_function(tmp
);
1233 } END_FOR_EACH_PTR(tmp
);
1234 free_ptr_list(&inlines_called
);
1237 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1241 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1244 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1246 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1248 } END_FOR_EACH_PTR_REVERSE(sym
);
1253 static void split_inlines_in_scope(struct symbol
*sym
)
1255 struct symbol
*base
;
1256 struct symbol_list
*scope_list
;
1259 scope_list
= sym
->scope
->symbols
;
1260 stream
= sym
->pos
.stream
;
1262 /* find the last static symbol in the file */
1263 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1264 if (sym
->pos
.stream
!= stream
)
1266 if (sym
->type
!= SYM_NODE
)
1268 base
= get_base_type(sym
);
1271 if (base
->type
!= SYM_FN
)
1273 if (!base
->inline_stmt
)
1275 add_inline_function(sym
);
1276 } END_FOR_EACH_PTR_REVERSE(sym
);
1281 static void split_inlines(struct symbol_list
*sym_list
)
1285 sym
= get_last_scoped_symbol(sym_list
, 0);
1287 split_inlines_in_scope(sym
);
1288 sym
= get_last_scoped_symbol(sym_list
, 1);
1290 split_inlines_in_scope(sym
);
1293 static void split_functions(struct symbol_list
*sym_list
)
1297 FOR_EACH_PTR(sym_list
, sym
) {
1298 set_position(sym
->pos
);
1299 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1300 split_function(sym
);
1303 __pass_to_client(sym
, BASE_HOOK
);
1304 fake_global_assign(sym
);
1306 } END_FOR_EACH_PTR(sym
);
1307 split_inlines(sym_list
);
1308 __pass_to_client(sym_list
, END_FILE_HOOK
);
1311 void smatch(int argc
, char **argv
)
1314 struct string_list
*filelist
= NULL
;
1315 struct symbol_list
*sym_list
;
1318 printf("Usage: smatch [--debug] <filename.c>\n");
1321 sparse_initialize(argc
, argv
, &filelist
);
1322 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1323 if (option_file_output
) {
1326 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1327 sm_outfd
= fopen(buf
, "w");
1329 printf("Error: Cannot open %s\n", base_file
);
1333 sym_list
= sparse_keep_tokens(base_file
);
1334 split_functions(sym_list
);
1335 } END_FOR_EACH_PTR_NOTAG(base_file
);