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 static struct timeval fn_start_time
;
51 char *get_function(void) { return cur_func
; }
52 int get_lineno(void) { return __smatch_lineno
; }
53 int inside_loop(void) { return !!loop_count
; }
54 int definitely_inside_loop(void) { return !!(loop_count
& ~0x80000000); }
55 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
56 int in_expression_statement(void) { return !!__expr_stmt_count
; }
58 static void split_symlist(struct symbol_list
*sym_list
);
59 static void split_declaration(struct symbol_list
*sym_list
);
60 static void split_expr_list(struct expression_list
*expr_list
);
61 static void add_inline_function(struct symbol
*sym
);
62 static void parse_inline(struct expression
*expr
);
64 int option_assume_loops
= 0;
65 int option_known_conditions
= 0;
66 int option_two_passes
= 0;
67 struct symbol
*cur_func_sym
= NULL
;
68 struct stree
*global_states
;
70 int outside_of_function(void)
72 return cur_func_sym
== NULL
;
75 const char *get_filename(void)
84 const char *get_base_file(void)
89 static void set_position(struct position pos
)
92 static int prev_stream
= -1;
94 if (pos
.stream
== 0 && pos
.line
== 0)
97 __smatch_lineno
= pos
.line
;
99 if (pos
.stream
== prev_stream
)
102 filename
= stream_name(pos
.stream
);
105 pathname
= getcwd(NULL
, 0);
107 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
108 full_filename
= malloc(len
);
109 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
111 full_filename
= alloc_string(filename
);
116 static int is_inline_func(struct expression
*expr
)
118 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
120 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
125 static int is_noreturn_func(struct expression
*expr
)
127 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
129 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
134 int inlinable(struct expression
*expr
)
138 if (__inline_fn
) /* don't nest */
141 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
143 if (is_no_inline_function(expr
->symbol
->ident
->name
))
145 sym
= get_base_type(expr
->symbol
);
146 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
147 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
151 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
152 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
159 void __process_post_op_stack(void)
161 struct expression
*expr
;
163 FOR_EACH_PTR(post_op_stack
, expr
) {
164 __pass_to_client(expr
, OP_HOOK
);
165 } END_FOR_EACH_PTR(expr
);
167 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
170 void __split_expr(struct expression
*expr
)
175 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
177 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
179 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
182 push_expression(&big_expression_stack
, expr
);
183 set_position(expr
->pos
);
184 __pass_to_client(expr
, EXPR_HOOK
);
186 switch (expr
->type
) {
189 __pass_to_client(expr
, DEREF_HOOK
);
190 __split_expr(expr
->unop
);
191 __pass_to_client(expr
, OP_HOOK
);
194 __split_expr(expr
->unop
);
195 push_expression(&post_op_stack
, expr
);
199 __split_stmt(expr
->statement
);
204 __pass_to_client(expr
, LOGIC_HOOK
);
205 __handle_logic(expr
);
208 __pass_to_client(expr
, BINOP_HOOK
);
210 __split_expr(expr
->left
);
211 __process_post_op_stack();
212 __split_expr(expr
->right
);
214 case EXPR_ASSIGNMENT
: {
215 struct expression
*tmp
;
220 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
223 if (__handle_condition_assigns(expr
))
225 /* foo = (x < 5 ? foo : 5); */
226 if (__handle_select_assigns(expr
))
228 /* foo = ({frob(); frob(); frob(); 1;}) */
229 if (__handle_expr_statement_assigns(expr
))
232 __split_expr(expr
->right
);
233 if (outside_of_function())
234 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
236 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
238 __fake_struct_member_assignments(expr
);
240 tmp
= strip_expr(expr
->right
);
241 if (tmp
->type
== EXPR_CALL
)
242 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
243 if (get_macro_name(tmp
->pos
) &&
244 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
245 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
246 __split_expr(expr
->left
);
250 __pass_to_client(expr
, DEREF_HOOK
);
251 __split_expr(expr
->deref
);
254 __split_expr(expr
->base
);
257 case EXPR_FORCE_CAST
:
258 __pass_to_client(expr
, CAST_HOOK
);
259 __split_expr(expr
->cast_expression
);
262 if (expr
->cast_expression
)
263 __pass_to_client(strip_parens(expr
->cast_expression
),
268 evaluate_expression(expr
);
270 case EXPR_CONDITIONAL
:
272 if (known_condition_true(expr
->conditional
)) {
273 __split_expr(expr
->cond_true
);
276 if (known_condition_false(expr
->conditional
)) {
277 __split_expr(expr
->cond_false
);
280 __pass_to_client(expr
, SELECT_HOOK
);
281 __split_whole_condition(expr
->conditional
);
282 __split_expr(expr
->cond_true
);
283 __push_true_states();
284 __use_false_states();
285 __split_expr(expr
->cond_false
);
286 __merge_true_states();
289 if (sym_name_is("__builtin_constant_p", expr
->fn
))
291 split_expr_list(expr
->args
);
292 __split_expr(expr
->fn
);
293 if (is_inline_func(expr
->fn
))
294 add_inline_function(expr
->fn
->symbol
);
295 if (inlinable(expr
->fn
))
297 __process_post_op_stack();
298 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
300 if (inlinable(expr
->fn
)) {
303 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
304 if (is_noreturn_func(expr
->fn
))
307 case EXPR_INITIALIZER
:
308 split_expr_list(expr
->expr_list
);
310 case EXPR_IDENTIFIER
:
311 __split_expr(expr
->ident_expression
);
314 __split_expr(expr
->idx_expression
);
317 __split_expr(expr
->init_expr
);
320 __pass_to_client(expr
, SYM_HOOK
);
323 __pass_to_client(expr
, STRING_HOOK
);
328 pop_expression(&big_expression_stack
);
331 static int is_forever_loop(struct statement
*stmt
)
333 struct expression
*expr
;
335 expr
= strip_expr(stmt
->iterator_pre_condition
);
337 expr
= stmt
->iterator_post_condition
;
339 /* this is a for(;;) loop... */
343 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
350 static char *get_loop_name(int num
)
354 snprintf(buf
, 255, "-loop%d", num
);
356 return alloc_sname(buf
);
360 * Pre Loops are while and for loops.
362 static void handle_pre_loop(struct statement
*stmt
)
364 int once_through
; /* we go through the loop at least once */
365 struct sm_state
*extra_sm
= NULL
;
368 struct stree
*stree
= NULL
;
369 struct sm_state
*sm
= NULL
;
371 loop_name
= get_loop_name(loop_num
);
374 __split_stmt(stmt
->iterator_pre_statement
);
376 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
382 __merge_gotos(loop_name
);
384 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
385 __in_pre_condition
++;
386 __pass_to_client(stmt
, PRELOOP_HOOK
);
387 __split_whole_condition(stmt
->iterator_pre_condition
);
388 __in_pre_condition
--;
389 FOR_EACH_SM(stree
, sm
) {
390 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
391 } END_FOR_EACH_SM(sm
);
394 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
396 if (option_assume_loops
)
399 __split_stmt(stmt
->iterator_statement
);
400 __warn_on_silly_pre_loops();
401 if (is_forever_loop(stmt
)) {
403 __save_gotos(loop_name
);
405 __push_fake_cur_stree();
406 __split_stmt(stmt
->iterator_post_statement
);
407 stree
= __pop_fake_cur_stree();
409 __discard_false_states();
412 if (!__path_is_null())
413 __merge_stree_into_cur(stree
);
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(void)
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 void split_asm_constraints(struct expression_list
*expr_list
)
492 struct expression
*expr
;
495 FOR_EACH_PTR(expr_list
, expr
) {
497 case 0: /* identifier */
498 case 1: /* constraint */
501 case 2: /* expression */
506 } END_FOR_EACH_PTR(expr
);
509 static int is_case_val(struct statement
*stmt
, sval_t sval
)
513 if (stmt
->type
!= STMT_CASE
)
515 if (!stmt
->case_expression
) {
519 if (!get_value(stmt
->case_expression
, &case_sval
))
521 if (case_sval
.value
== sval
.value
)
526 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
528 struct statement
*tmp
;
530 __split_expr(stmt
->switch_expression
);
532 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
533 __save_switch_states(top_expression(switch_expr_stack
));
538 stmt
= stmt
->switch_statement
;
540 __push_scope_hooks();
541 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
542 __smatch_lineno
= tmp
->pos
.line
;
543 if (is_case_val(tmp
, sval
)) {
544 __merge_switches(top_expression(switch_expr_stack
),
545 stmt
->case_expression
);
546 __pass_case_to_client(top_expression(switch_expr_stack
),
547 stmt
->case_expression
);
549 if (__path_is_null())
552 if (__path_is_null()) {
556 } END_FOR_EACH_PTR(tmp
);
558 __call_scope_hooks();
559 if (!__pop_default())
560 __merge_switches(top_expression(switch_expr_stack
),
562 __discard_switches();
564 pop_expression(&switch_expr_stack
);
567 static int taking_too_long(void)
571 ms
= ms_since(&fn_start_time
);
572 if (ms
> 1000 * 60 * 5) /* five minutes */
577 void __split_stmt(struct statement
*stmt
)
584 if (__bail_on_rest_of_function
|| out_of_memory() || taking_too_long()) {
585 static char *printed
= NULL
;
587 __bail_on_rest_of_function
= 1;
588 if (printed
!= cur_func
)
589 sm_msg("Function too hairy. Giving up.");
590 final_pass
= 0; /* turn off sm_msg() from here */
595 add_ptr_list(&big_statement_stack
, stmt
);
596 free_expression_stack(&big_expression_stack
);
597 set_position(stmt
->pos
);
598 __pass_to_client(stmt
, STMT_HOOK
);
600 switch (stmt
->type
) {
601 case STMT_DECLARATION
:
602 split_declaration(stmt
->declaration
);
605 __split_expr(stmt
->ret_value
);
606 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
607 __process_post_op_stack();
610 case STMT_EXPRESSION
:
611 __split_expr(stmt
->expression
);
613 case STMT_COMPOUND
: {
614 struct statement
*tmp
;
616 __push_scope_hooks();
617 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
619 } END_FOR_EACH_PTR(tmp
);
620 __call_scope_hooks();
624 if (known_condition_true(stmt
->if_conditional
)) {
625 __split_stmt(stmt
->if_true
);
628 if (known_condition_false(stmt
->if_conditional
)) {
629 __split_stmt(stmt
->if_false
);
632 if (option_known_conditions
&&
633 implied_condition_true(stmt
->if_conditional
)) {
634 sm_info("this condition is true.");
635 __split_stmt(stmt
->if_true
);
638 if (option_known_conditions
&&
639 implied_condition_false(stmt
->if_conditional
)) {
640 sm_info("this condition is false.");
641 __split_stmt(stmt
->if_false
);
644 __split_whole_condition(stmt
->if_conditional
);
645 __split_stmt(stmt
->if_true
);
646 if (empty_statement(stmt
->if_true
) &&
647 last_stmt_on_same_line() &&
648 !get_macro_name(stmt
->if_true
->pos
))
649 sm_msg("warn: if();");
650 __push_true_states();
651 __use_false_states();
652 __split_stmt(stmt
->if_false
);
653 __merge_true_states();
656 if (stmt
->iterator_pre_condition
)
657 handle_pre_loop(stmt
);
658 else if (stmt
->iterator_post_condition
)
659 handle_post_loop(stmt
);
661 // these are for(;;) type loops.
662 handle_pre_loop(stmt
);
666 if (get_value(stmt
->switch_expression
, &sval
)) {
667 split_known_switch(stmt
, sval
);
670 __split_expr(stmt
->switch_expression
);
671 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
672 __save_switch_states(top_expression(switch_expr_stack
));
676 __split_stmt(stmt
->switch_statement
);
677 if (!__pop_default())
678 __merge_switches(top_expression(switch_expr_stack
),
680 __discard_switches();
682 pop_expression(&switch_expr_stack
);
685 __merge_switches(top_expression(switch_expr_stack
),
686 stmt
->case_expression
);
687 __pass_case_to_client(top_expression(switch_expr_stack
),
688 stmt
->case_expression
);
689 if (!stmt
->case_expression
)
691 __split_expr(stmt
->case_expression
);
692 __split_expr(stmt
->case_to
);
693 __split_stmt(stmt
->case_statement
);
696 if (stmt
->label_identifier
&&
697 stmt
->label_identifier
->type
== SYM_LABEL
&&
698 stmt
->label_identifier
->ident
) {
699 loop_count
|= 0x80000000;
700 __merge_gotos(stmt
->label_identifier
->ident
->name
);
702 __split_stmt(stmt
->label_statement
);
705 __split_expr(stmt
->goto_expression
);
706 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
707 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
709 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
711 __process_continues();
713 } else if (stmt
->goto_label
&&
714 stmt
->goto_label
->type
== SYM_LABEL
&&
715 stmt
->goto_label
->ident
) {
716 __save_gotos(stmt
->goto_label
->ident
->name
);
723 __pass_to_client(stmt
, ASM_HOOK
);
724 __split_expr(stmt
->asm_string
);
725 split_asm_constraints(stmt
->asm_outputs
);
726 split_asm_constraints(stmt
->asm_inputs
);
727 split_asm_constraints(stmt
->asm_clobbers
);
732 __split_expr(stmt
->range_expression
);
733 __split_expr(stmt
->range_low
);
734 __split_expr(stmt
->range_high
);
737 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
739 __process_post_op_stack();
742 static void split_expr_list(struct expression_list
*expr_list
)
744 struct expression
*expr
;
746 FOR_EACH_PTR(expr_list
, expr
) {
748 __process_post_op_stack();
749 } END_FOR_EACH_PTR(expr
);
752 static void split_sym(struct symbol
*sym
)
756 if (!(sym
->namespace & NS_SYMBOL
))
759 __split_stmt(sym
->stmt
);
760 __split_expr(sym
->array_size
);
761 split_symlist(sym
->arguments
);
762 split_symlist(sym
->symbol_list
);
763 __split_stmt(sym
->inline_stmt
);
764 split_symlist(sym
->inline_symbol_list
);
767 static void split_symlist(struct symbol_list
*sym_list
)
771 FOR_EACH_PTR(sym_list
, sym
) {
773 } END_FOR_EACH_PTR(sym
);
776 typedef void (fake_cb
)(struct expression
*expr
);
778 static int member_to_number(struct expression
*expr
, struct ident
*member
)
780 struct symbol
*type
, *tmp
;
788 type
= get_type(expr
);
789 if (!type
|| type
->type
!= SYM_STRUCT
)
793 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
797 if (strcmp(name
, tmp
->ident
->name
) == 0)
799 } END_FOR_EACH_PTR(tmp
);
803 static struct ident
*number_to_member(struct expression
*expr
, int num
)
805 struct symbol
*type
, *member
;
808 type
= get_type(expr
);
809 if (!type
|| type
->type
!= SYM_STRUCT
)
812 FOR_EACH_PTR(type
->symbol_list
, member
) {
814 return member
->ident
;
816 } END_FOR_EACH_PTR(member
);
820 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
827 static struct member_set
*alloc_member_set(struct symbol
*type
)
829 struct member_set
*member_set
;
830 struct symbol
*member
;
834 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
835 member_set
= malloc(member_count
* sizeof(*member_set
));
837 FOR_EACH_PTR(type
->symbol_list
, member
) {
838 member_set
[member_idx
].ident
= member
->ident
;
839 member_set
[member_idx
].set
= 0;
841 } END_FOR_EACH_PTR(member
);
846 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
848 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
851 for (i
= 0; i
< member_count
; i
++) {
852 if (member_set
[i
].ident
== ident
) {
853 member_set
[i
].set
= 1;
857 // crap. this is buggy.
858 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
861 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
863 struct expression
*deref
, *assign
;
864 struct symbol
*member
, *member_type
;
868 FOR_EACH_PTR(type
->symbol_list
, member
) {
869 if (!member
->ident
|| member_set
[member_idx
].set
) {
873 member_type
= get_real_base_type(member
);
874 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
878 /* TODO: this should be handled recursively and not ignored */
879 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
883 deref
= member_expression(symbol
, '.', member
->ident
);
884 assign
= assign_expression(deref
, zero_expr());
885 __split_expr(assign
);
887 } END_FOR_EACH_PTR(member
);
891 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
893 struct expression
*deref
, *assign
, *tmp
;
894 struct symbol
*struct_type
, *type
;
895 struct ident
*member
;
897 struct member_set
*member_set
;
899 struct_type
= get_type(symbol
);
901 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
904 member_set
= alloc_member_set(struct_type
);
907 FOR_EACH_PTR(members
, tmp
) {
908 member
= number_to_member(symbol
, member_idx
);
909 while (tmp
->type
== EXPR_IDENTIFIER
) {
910 member
= tmp
->expr_ident
;
911 member_idx
= member_to_number(symbol
, member
);
912 tmp
= tmp
->ident_expression
;
914 mark_member_as_set(struct_type
, member_set
, member
);
916 deref
= member_expression(symbol
, '.', member
);
917 if (tmp
->type
== EXPR_INITIALIZER
) {
918 type
= get_type(deref
);
919 if (type
&& type
->type
== SYM_ARRAY
)
920 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
922 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
924 assign
= assign_expression(deref
, tmp
);
927 } END_FOR_EACH_PTR(tmp
);
929 set_unset_to_zero(symbol
, struct_type
, member_set
);
932 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
934 fake_member_assigns_helper(symbol_expression(sym
),
935 sym
->initializer
->expr_list
, fake_cb
);
938 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
940 struct expression
*offset
, *binop
, *assign
, *tmp
;
945 FOR_EACH_PTR(expr_list
, tmp
) {
946 if (tmp
->type
== EXPR_INDEX
) {
947 if (tmp
->idx_from
!= tmp
->idx_to
)
950 if (!tmp
->idx_expression
)
952 tmp
= tmp
->idx_expression
;
954 offset
= value_expr(idx
);
955 binop
= array_element_expression(array
, offset
);
956 if (tmp
->type
== EXPR_INITIALIZER
) {
957 type
= get_type(binop
);
958 if (type
&& type
->type
== SYM_ARRAY
)
959 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
961 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
963 assign
= assign_expression(binop
, tmp
);
968 } END_FOR_EACH_PTR(tmp
);
971 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
973 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
976 static void fake_assign_expr(struct symbol
*sym
)
978 struct expression
*assign
, *symbol
;
980 symbol
= symbol_expression(sym
);
981 assign
= assign_expression(symbol
, sym
->initializer
);
982 __split_expr(assign
);
985 static void call_split_expr(struct expression
*expr
)
990 static void do_initializer_stuff(struct symbol
*sym
)
992 if (!sym
->initializer
)
995 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
996 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
997 fake_element_assigns(sym
, call_split_expr
);
999 fake_member_assigns(sym
, call_split_expr
);
1001 fake_assign_expr(sym
);
1005 static void split_declaration(struct symbol_list
*sym_list
)
1009 FOR_EACH_PTR(sym_list
, sym
) {
1010 __pass_to_client(sym
, DECLARATION_HOOK
);
1011 do_initializer_stuff(sym
);
1013 } END_FOR_EACH_PTR(sym
);
1016 static void call_global_assign_hooks(struct expression
*assign
)
1018 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1021 static void fake_global_assign(struct symbol
*sym
)
1023 struct expression
*assign
, *symbol
;
1025 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1026 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1027 fake_element_assigns(sym
, call_global_assign_hooks
);
1028 } else if (sym
->initializer
) {
1029 symbol
= symbol_expression(sym
);
1030 assign
= assign_expression(symbol
, sym
->initializer
);
1031 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1033 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1035 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1036 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1037 fake_member_assigns(sym
, call_global_assign_hooks
);
1038 } else if (sym
->initializer
) {
1039 symbol
= symbol_expression(sym
);
1040 assign
= assign_expression(symbol
, sym
->initializer
);
1041 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1043 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1046 symbol
= symbol_expression(sym
);
1047 if (sym
->initializer
)
1048 assign
= assign_expression(symbol
, sym
->initializer
);
1050 assign
= assign_expression(symbol
, zero_expr());
1051 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1055 static void start_function_definition(struct symbol
*sym
)
1057 __in_function_def
= 1;
1058 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1059 __in_function_def
= 0;
1060 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1064 static void split_function(struct symbol
*sym
)
1066 struct symbol
*base_type
= get_base_type(sym
);
1068 gettimeofday(&fn_start_time
, NULL
);
1071 cur_func
= sym
->ident
->name
;
1072 __smatch_lineno
= sym
->pos
.line
;
1074 sm_debug("new function: %s\n", cur_func
);
1076 if (option_two_passes
) {
1080 start_function_definition(sym
);
1081 __split_stmt(base_type
->stmt
);
1082 __split_stmt(base_type
->inline_stmt
);
1088 start_function_definition(sym
);
1089 __split_stmt(base_type
->stmt
);
1090 __split_stmt(base_type
->inline_stmt
);
1091 __pass_to_client(sym
, END_FUNC_HOOK
);
1092 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1094 cur_func_sym
= NULL
;
1097 free_data_info_allocs();
1098 free_expression_stack(&switch_expr_stack
);
1099 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1100 __bail_on_rest_of_function
= 0;
1103 static void parse_inline(struct expression
*call
)
1105 struct symbol
*base_type
;
1106 int loop_num_bak
= loop_num
;
1107 int final_pass_bak
= final_pass
;
1108 char *cur_func_bak
= cur_func
;
1109 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1110 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1111 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1112 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1114 __pass_to_client(call
, INLINE_FN_START
);
1115 final_pass
= 0; /* don't print anything */
1118 base_type
= get_base_type(call
->fn
->symbol
);
1119 cur_func_sym
= call
->fn
->symbol
;
1120 if (call
->fn
->symbol
->ident
)
1121 cur_func
= call
->fn
->symbol
->ident
->name
;
1124 set_position(call
->fn
->symbol
->pos
);
1127 big_statement_stack
= NULL
;
1128 big_expression_stack
= NULL
;
1129 switch_expr_stack
= NULL
;
1131 sm_debug("inline function: %s\n", cur_func
);
1134 start_function_definition(call
->fn
->symbol
);
1135 __split_stmt(base_type
->stmt
);
1136 __split_stmt(base_type
->inline_stmt
);
1137 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1138 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1140 free_expression_stack(&switch_expr_stack
);
1141 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1145 loop_num
= loop_num_bak
;
1146 final_pass
= final_pass_bak
;
1147 cur_func_sym
= cur_func_sym_bak
;
1148 cur_func
= cur_func_bak
;
1149 big_statement_stack
= big_statement_stack_bak
;
1150 big_expression_stack
= big_expression_stack_bak
;
1151 switch_expr_stack
= switch_expr_stack_bak
;
1153 restore_all_states();
1154 set_position(call
->pos
);
1156 __pass_to_client(call
, INLINE_FN_END
);
1159 static struct symbol_list
*inlines_called
;
1160 static void add_inline_function(struct symbol
*sym
)
1162 static struct symbol_list
*already_added
;
1165 FOR_EACH_PTR(already_added
, tmp
) {
1168 } END_FOR_EACH_PTR(tmp
);
1170 add_ptr_list(&already_added
, sym
);
1171 add_ptr_list(&inlines_called
, sym
);
1174 static void process_inlines(void)
1178 FOR_EACH_PTR(inlines_called
, tmp
) {
1179 split_function(tmp
);
1180 } END_FOR_EACH_PTR(tmp
);
1181 free_ptr_list(&inlines_called
);
1184 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1188 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1191 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1193 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1195 } END_FOR_EACH_PTR_REVERSE(sym
);
1200 static void split_inlines_in_scope(struct symbol
*sym
)
1202 struct symbol
*base
;
1203 struct symbol_list
*scope_list
;
1206 scope_list
= sym
->scope
->symbols
;
1207 stream
= sym
->pos
.stream
;
1209 /* find the last static symbol in the file */
1210 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1211 if (sym
->pos
.stream
!= stream
)
1213 if (sym
->type
!= SYM_NODE
)
1215 base
= get_base_type(sym
);
1218 if (base
->type
!= SYM_FN
)
1220 if (!base
->inline_stmt
)
1222 add_inline_function(sym
);
1223 } END_FOR_EACH_PTR_REVERSE(sym
);
1228 static void split_inlines(struct symbol_list
*sym_list
)
1232 sym
= get_last_scoped_symbol(sym_list
, 0);
1234 split_inlines_in_scope(sym
);
1235 sym
= get_last_scoped_symbol(sym_list
, 1);
1237 split_inlines_in_scope(sym
);
1240 static struct stree
*clone_estates_perm(struct stree
*orig
)
1242 struct stree
*ret
= NULL
;
1243 struct sm_state
*tmp
;
1245 FOR_EACH_SM(orig
, tmp
) {
1246 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1247 } END_FOR_EACH_SM(tmp
);
1252 static void split_functions(struct symbol_list
*sym_list
)
1257 FOR_EACH_PTR(sym_list
, sym
) {
1258 set_position(sym
->pos
);
1259 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1260 __pass_to_client(sym
, BASE_HOOK
);
1261 fake_global_assign(sym
);
1263 } END_FOR_EACH_PTR(sym
);
1264 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1267 FOR_EACH_PTR(sym_list
, sym
) {
1268 set_position(sym
->pos
);
1269 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1270 split_function(sym
);
1273 } END_FOR_EACH_PTR(sym
);
1274 split_inlines(sym_list
);
1275 __pass_to_client(sym_list
, END_FILE_HOOK
);
1278 void smatch(int argc
, char **argv
)
1281 struct string_list
*filelist
= NULL
;
1282 struct symbol_list
*sym_list
;
1285 printf("Usage: smatch [--debug] <filename.c>\n");
1288 sparse_initialize(argc
, argv
, &filelist
);
1289 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1290 if (option_file_output
) {
1293 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1294 sm_outfd
= fopen(buf
, "w");
1296 printf("Error: Cannot open %s\n", base_file
);
1300 sym_list
= sparse_keep_tokens(base_file
);
1301 split_functions(sym_list
);
1302 } END_FOR_EACH_PTR_NOTAG(base_file
);