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
;
69 int outside_of_function(void)
71 return cur_func_sym
== NULL
;
74 const char *get_filename(void)
83 const char *get_base_file(void)
88 static void set_position(struct position pos
)
91 static int prev_stream
= -1;
93 if (pos
.stream
== 0 && pos
.line
== 0)
96 __smatch_lineno
= pos
.line
;
98 if (pos
.stream
== prev_stream
)
101 filename
= stream_name(pos
.stream
);
104 pathname
= getcwd(NULL
, 0);
106 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
107 full_filename
= malloc(len
);
108 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
110 full_filename
= alloc_string(filename
);
115 static int is_inline_func(struct expression
*expr
)
117 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
119 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
124 static int is_noreturn_func(struct expression
*expr
)
126 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
128 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
133 int inlinable(struct expression
*expr
)
137 if (__inline_fn
) /* don't nest */
140 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
142 if (is_no_inline_function(expr
->symbol
->ident
->name
))
144 sym
= get_base_type(expr
->symbol
);
145 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
146 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
150 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
151 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
158 void __process_post_op_stack(void)
160 struct expression
*expr
;
162 FOR_EACH_PTR(post_op_stack
, expr
) {
163 __pass_to_client(expr
, OP_HOOK
);
164 } END_FOR_EACH_PTR(expr
);
166 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
169 void __split_expr(struct expression
*expr
)
174 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
176 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
178 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
181 push_expression(&big_expression_stack
, expr
);
182 set_position(expr
->pos
);
183 __pass_to_client(expr
, EXPR_HOOK
);
185 switch (expr
->type
) {
188 __pass_to_client(expr
, DEREF_HOOK
);
189 __split_expr(expr
->unop
);
190 __pass_to_client(expr
, OP_HOOK
);
193 __split_expr(expr
->unop
);
194 push_expression(&post_op_stack
, expr
);
198 __split_stmt(expr
->statement
);
203 __pass_to_client(expr
, LOGIC_HOOK
);
204 __handle_logic(expr
);
207 __pass_to_client(expr
, BINOP_HOOK
);
209 __split_expr(expr
->left
);
210 __process_post_op_stack();
211 __split_expr(expr
->right
);
213 case EXPR_ASSIGNMENT
: {
214 struct expression
*tmp
;
219 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
222 if (__handle_condition_assigns(expr
))
224 /* foo = (x < 5 ? foo : 5); */
225 if (__handle_select_assigns(expr
))
227 /* foo = ({frob(); frob(); frob(); 1;}) */
228 if (__handle_expr_statement_assigns(expr
))
231 __split_expr(expr
->right
);
232 if (outside_of_function())
233 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
235 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
237 __fake_struct_member_assignments(expr
);
239 tmp
= strip_expr(expr
->right
);
240 if (tmp
->type
== EXPR_CALL
)
241 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
242 if (get_macro_name(tmp
->pos
) &&
243 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
244 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
245 __split_expr(expr
->left
);
249 __pass_to_client(expr
, DEREF_HOOK
);
250 __split_expr(expr
->deref
);
253 __split_expr(expr
->base
);
256 case EXPR_FORCE_CAST
:
257 __pass_to_client(expr
, CAST_HOOK
);
258 __split_expr(expr
->cast_expression
);
261 if (expr
->cast_expression
)
262 __pass_to_client(strip_parens(expr
->cast_expression
),
267 evaluate_expression(expr
);
269 case EXPR_CONDITIONAL
:
271 if (known_condition_true(expr
->conditional
)) {
272 __split_expr(expr
->cond_true
);
275 if (known_condition_false(expr
->conditional
)) {
276 __split_expr(expr
->cond_false
);
279 __pass_to_client(expr
, SELECT_HOOK
);
280 __split_whole_condition(expr
->conditional
);
281 __split_expr(expr
->cond_true
);
282 __push_true_states();
283 __use_false_states();
284 __split_expr(expr
->cond_false
);
285 __merge_true_states();
288 if (sym_name_is("__builtin_constant_p", expr
->fn
))
290 split_expr_list(expr
->args
);
291 __split_expr(expr
->fn
);
292 if (is_inline_func(expr
->fn
))
293 add_inline_function(expr
->fn
->symbol
);
294 if (inlinable(expr
->fn
))
296 __process_post_op_stack();
297 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
299 if (inlinable(expr
->fn
)) {
302 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
303 if (is_noreturn_func(expr
->fn
))
306 case EXPR_INITIALIZER
:
307 split_expr_list(expr
->expr_list
);
309 case EXPR_IDENTIFIER
:
310 __split_expr(expr
->ident_expression
);
313 __split_expr(expr
->idx_expression
);
316 __split_expr(expr
->init_expr
);
319 __pass_to_client(expr
, SYM_HOOK
);
322 __pass_to_client(expr
, STRING_HOOK
);
327 pop_expression(&big_expression_stack
);
330 static int is_forever_loop(struct statement
*stmt
)
332 struct expression
*expr
;
334 expr
= strip_expr(stmt
->iterator_pre_condition
);
336 expr
= stmt
->iterator_post_condition
;
338 /* this is a for(;;) loop... */
342 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
349 static char *get_loop_name(int num
)
353 snprintf(buf
, 255, "-loop%d", num
);
355 return alloc_sname(buf
);
359 * Pre Loops are while and for loops.
361 static void handle_pre_loop(struct statement
*stmt
)
363 int once_through
; /* we go through the loop at least once */
364 struct sm_state
*extra_sm
= NULL
;
367 struct stree
*stree
= NULL
;
368 struct sm_state
*sm
= NULL
;
370 loop_name
= get_loop_name(loop_num
);
373 __split_stmt(stmt
->iterator_pre_statement
);
375 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
381 __merge_gotos(loop_name
);
383 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
384 __in_pre_condition
++;
385 __pass_to_client(stmt
, PRELOOP_HOOK
);
386 __split_whole_condition(stmt
->iterator_pre_condition
);
387 __in_pre_condition
--;
388 FOR_EACH_SM(stree
, sm
) {
389 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
390 } END_FOR_EACH_SM(sm
);
393 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
395 if (option_assume_loops
)
398 __split_stmt(stmt
->iterator_statement
);
399 __warn_on_silly_pre_loops();
400 if (is_forever_loop(stmt
)) {
402 __save_gotos(loop_name
);
404 __push_fake_cur_stree();
405 __split_stmt(stmt
->iterator_post_statement
);
406 stree
= __pop_fake_cur_stree();
408 __discard_false_states();
411 if (!__path_is_null())
412 __merge_stree_into_cur(stree
);
416 unchanged
= __iterator_unchanged(extra_sm
);
417 __split_stmt(stmt
->iterator_post_statement
);
418 __save_gotos(loop_name
);
419 __split_whole_condition(stmt
->iterator_pre_condition
);
421 __merge_false_states();
423 __discard_false_states();
425 __merge_false_states();
427 if (extra_sm
&& unchanged
)
428 __extra_pre_loop_hook_after(extra_sm
,
429 stmt
->iterator_post_statement
,
430 stmt
->iterator_pre_condition
);
437 * Post loops are do {} while();
439 static void handle_post_loop(struct statement
*stmt
)
443 loop_name
= get_loop_name(loop_num
);
449 __merge_gotos(loop_name
);
450 __split_stmt(stmt
->iterator_statement
);
452 if (!is_zero(stmt
->iterator_post_condition
))
453 __save_gotos(loop_name
);
455 if (is_forever_loop(stmt
)) {
458 __split_whole_condition(stmt
->iterator_post_condition
);
459 __use_false_states();
465 static int empty_statement(struct statement
*stmt
)
469 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
474 static int last_stmt_on_same_line()
476 struct statement
*stmt
;
479 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
482 if (stmt
->pos
.line
== get_lineno())
485 } END_FOR_EACH_PTR_REVERSE(stmt
);
489 static void split_asm_constraints(struct expression_list
*expr_list
)
491 struct expression
*expr
;
494 FOR_EACH_PTR(expr_list
, expr
) {
496 case 0: /* identifier */
497 case 1: /* constraint */
500 case 2: /* expression */
505 } END_FOR_EACH_PTR(expr
);
508 static int is_case_val(struct statement
*stmt
, sval_t sval
)
512 if (stmt
->type
!= STMT_CASE
)
514 if (!stmt
->case_expression
) {
518 if (!get_value(stmt
->case_expression
, &case_sval
))
520 if (case_sval
.value
== sval
.value
)
525 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
527 struct statement
*tmp
;
529 __split_expr(stmt
->switch_expression
);
531 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
532 __save_switch_states(top_expression(switch_expr_stack
));
537 stmt
= stmt
->switch_statement
;
539 __push_scope_hooks();
540 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
541 __smatch_lineno
= tmp
->pos
.line
;
542 if (is_case_val(tmp
, sval
)) {
543 __merge_switches(top_expression(switch_expr_stack
),
544 stmt
->case_expression
);
545 __pass_case_to_client(top_expression(switch_expr_stack
),
546 stmt
->case_expression
);
548 if (__path_is_null())
551 if (__path_is_null()) {
555 } END_FOR_EACH_PTR(tmp
);
557 __call_scope_hooks();
558 if (!__pop_default())
559 __merge_switches(top_expression(switch_expr_stack
),
561 __discard_switches();
563 pop_expression(&switch_expr_stack
);
566 static int taking_too_long(void)
570 ms
= ms_since(&fn_start_time
);
571 if (ms
> 1000 * 60 * 5) /* five minutes */
576 void __split_stmt(struct statement
*stmt
)
583 if (__bail_on_rest_of_function
|| out_of_memory() || taking_too_long()) {
584 static char *printed
= NULL
;
586 __bail_on_rest_of_function
= 1;
587 if (printed
!= cur_func
)
588 sm_msg("Function too hairy. Giving up.");
589 final_pass
= 0; /* turn off sm_msg() from here */
594 add_ptr_list(&big_statement_stack
, stmt
);
595 free_expression_stack(&big_expression_stack
);
596 set_position(stmt
->pos
);
597 __pass_to_client(stmt
, STMT_HOOK
);
599 switch (stmt
->type
) {
600 case STMT_DECLARATION
:
601 split_declaration(stmt
->declaration
);
604 __split_expr(stmt
->ret_value
);
605 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
606 __process_post_op_stack();
609 case STMT_EXPRESSION
:
610 __split_expr(stmt
->expression
);
612 case STMT_COMPOUND
: {
613 struct statement
*tmp
;
615 __push_scope_hooks();
616 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
618 } END_FOR_EACH_PTR(tmp
);
619 __call_scope_hooks();
623 if (known_condition_true(stmt
->if_conditional
)) {
624 __split_stmt(stmt
->if_true
);
627 if (known_condition_false(stmt
->if_conditional
)) {
628 __split_stmt(stmt
->if_false
);
631 if (option_known_conditions
&&
632 implied_condition_true(stmt
->if_conditional
)) {
633 sm_info("this condition is true.");
634 __split_stmt(stmt
->if_true
);
637 if (option_known_conditions
&&
638 implied_condition_false(stmt
->if_conditional
)) {
639 sm_info("this condition is false.");
640 __split_stmt(stmt
->if_false
);
643 __split_whole_condition(stmt
->if_conditional
);
644 __split_stmt(stmt
->if_true
);
645 if (empty_statement(stmt
->if_true
) &&
646 last_stmt_on_same_line() &&
647 !get_macro_name(stmt
->if_true
->pos
))
648 sm_msg("warn: if();");
649 __push_true_states();
650 __use_false_states();
651 __split_stmt(stmt
->if_false
);
652 __merge_true_states();
655 if (stmt
->iterator_pre_condition
)
656 handle_pre_loop(stmt
);
657 else if (stmt
->iterator_post_condition
)
658 handle_post_loop(stmt
);
660 // these are for(;;) type loops.
661 handle_pre_loop(stmt
);
665 if (get_value(stmt
->switch_expression
, &sval
)) {
666 split_known_switch(stmt
, sval
);
669 __split_expr(stmt
->switch_expression
);
670 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
671 __save_switch_states(top_expression(switch_expr_stack
));
675 __split_stmt(stmt
->switch_statement
);
676 if (!__pop_default())
677 __merge_switches(top_expression(switch_expr_stack
),
679 __discard_switches();
681 pop_expression(&switch_expr_stack
);
684 __merge_switches(top_expression(switch_expr_stack
),
685 stmt
->case_expression
);
686 __pass_case_to_client(top_expression(switch_expr_stack
),
687 stmt
->case_expression
);
688 if (!stmt
->case_expression
)
690 __split_expr(stmt
->case_expression
);
691 __split_expr(stmt
->case_to
);
692 __split_stmt(stmt
->case_statement
);
695 if (stmt
->label_identifier
&&
696 stmt
->label_identifier
->type
== SYM_LABEL
&&
697 stmt
->label_identifier
->ident
) {
698 loop_count
|= 0x80000000;
699 __merge_gotos(stmt
->label_identifier
->ident
->name
);
701 __split_stmt(stmt
->label_statement
);
704 __split_expr(stmt
->goto_expression
);
705 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
706 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
708 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
710 __process_continues();
712 } else if (stmt
->goto_label
&&
713 stmt
->goto_label
->type
== SYM_LABEL
&&
714 stmt
->goto_label
->ident
) {
715 __save_gotos(stmt
->goto_label
->ident
->name
);
722 __pass_to_client(stmt
, ASM_HOOK
);
723 __split_expr(stmt
->asm_string
);
724 split_asm_constraints(stmt
->asm_outputs
);
725 split_asm_constraints(stmt
->asm_inputs
);
726 split_asm_constraints(stmt
->asm_clobbers
);
731 __split_expr(stmt
->range_expression
);
732 __split_expr(stmt
->range_low
);
733 __split_expr(stmt
->range_high
);
736 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
738 __process_post_op_stack();
741 static void split_expr_list(struct expression_list
*expr_list
)
743 struct expression
*expr
;
745 FOR_EACH_PTR(expr_list
, expr
) {
747 __process_post_op_stack();
748 } END_FOR_EACH_PTR(expr
);
751 static void split_sym(struct symbol
*sym
)
755 if (!(sym
->namespace & NS_SYMBOL
))
758 __split_stmt(sym
->stmt
);
759 __split_expr(sym
->array_size
);
760 split_symlist(sym
->arguments
);
761 split_symlist(sym
->symbol_list
);
762 __split_stmt(sym
->inline_stmt
);
763 split_symlist(sym
->inline_symbol_list
);
766 static void split_symlist(struct symbol_list
*sym_list
)
770 FOR_EACH_PTR(sym_list
, sym
) {
772 } END_FOR_EACH_PTR(sym
);
775 typedef void (fake_cb
)(struct expression
*expr
);
777 static int member_to_number(struct expression
*expr
, struct ident
*member
)
779 struct symbol
*type
, *tmp
;
787 type
= get_type(expr
);
788 if (!type
|| type
->type
!= SYM_STRUCT
)
792 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
796 if (strcmp(name
, tmp
->ident
->name
) == 0)
798 } END_FOR_EACH_PTR(tmp
);
802 static struct ident
*number_to_member(struct expression
*expr
, int num
)
804 struct symbol
*type
, *member
;
807 type
= get_type(expr
);
808 if (!type
|| type
->type
!= SYM_STRUCT
)
811 FOR_EACH_PTR(type
->symbol_list
, member
) {
813 return member
->ident
;
815 } END_FOR_EACH_PTR(member
);
819 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
826 static struct member_set
*alloc_member_set(struct symbol
*type
)
828 struct member_set
*member_set
;
829 struct symbol
*member
;
833 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
834 member_set
= malloc(member_count
* sizeof(*member_set
));
836 FOR_EACH_PTR(type
->symbol_list
, member
) {
837 member_set
[member_idx
].ident
= member
->ident
;
838 member_set
[member_idx
].set
= 0;
840 } END_FOR_EACH_PTR(member
);
845 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
847 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
850 for (i
= 0; i
< member_count
; i
++) {
851 if (member_set
[i
].ident
== ident
) {
852 member_set
[i
].set
= 1;
856 // crap. this is buggy.
857 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
860 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
862 struct expression
*deref
, *assign
;
863 struct symbol
*member
, *member_type
;
867 FOR_EACH_PTR(type
->symbol_list
, member
) {
868 if (!member
->ident
|| member_set
[member_idx
].set
) {
872 member_type
= get_real_base_type(member
);
873 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
877 /* TODO: this should be handled recursively and not ignored */
878 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
882 deref
= member_expression(symbol
, '.', member
->ident
);
883 assign
= assign_expression(deref
, zero_expr());
884 __split_expr(assign
);
886 } END_FOR_EACH_PTR(member
);
890 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
892 struct expression
*deref
, *assign
, *tmp
;
893 struct symbol
*struct_type
, *type
;
894 struct ident
*member
;
896 struct member_set
*member_set
;
898 struct_type
= get_type(symbol
);
900 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
903 member_set
= alloc_member_set(struct_type
);
906 FOR_EACH_PTR(members
, tmp
) {
907 member
= number_to_member(symbol
, member_idx
);
908 while (tmp
->type
== EXPR_IDENTIFIER
) {
909 member
= tmp
->expr_ident
;
910 member_idx
= member_to_number(symbol
, member
);
911 tmp
= tmp
->ident_expression
;
913 mark_member_as_set(struct_type
, member_set
, member
);
915 deref
= member_expression(symbol
, '.', member
);
916 if (tmp
->type
== EXPR_INITIALIZER
) {
917 type
= get_type(deref
);
918 if (type
&& type
->type
== SYM_ARRAY
)
919 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
921 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
923 assign
= assign_expression(deref
, tmp
);
926 } END_FOR_EACH_PTR(tmp
);
928 set_unset_to_zero(symbol
, struct_type
, member_set
);
931 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
933 fake_member_assigns_helper(symbol_expression(sym
),
934 sym
->initializer
->expr_list
, fake_cb
);
937 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
939 struct expression
*offset
, *binop
, *assign
, *tmp
;
944 FOR_EACH_PTR(expr_list
, tmp
) {
945 if (tmp
->type
== EXPR_INDEX
) {
946 if (tmp
->idx_from
!= tmp
->idx_to
)
949 if (!tmp
->idx_expression
)
951 tmp
= tmp
->idx_expression
;
953 offset
= value_expr(idx
);
954 binop
= array_element_expression(array
, offset
);
955 if (tmp
->type
== EXPR_INITIALIZER
) {
956 type
= get_type(binop
);
957 if (type
&& type
->type
== SYM_ARRAY
)
958 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
960 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
962 assign
= assign_expression(binop
, tmp
);
967 } END_FOR_EACH_PTR(tmp
);
970 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
972 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
975 static void fake_assign_expr(struct symbol
*sym
)
977 struct expression
*assign
, *symbol
;
979 symbol
= symbol_expression(sym
);
980 assign
= assign_expression(symbol
, sym
->initializer
);
981 __split_expr(assign
);
984 static void call_split_expr(struct expression
*expr
)
989 static void do_initializer_stuff(struct symbol
*sym
)
991 if (!sym
->initializer
)
994 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
995 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
996 fake_element_assigns(sym
, call_split_expr
);
998 fake_member_assigns(sym
, call_split_expr
);
1000 fake_assign_expr(sym
);
1004 static void split_declaration(struct symbol_list
*sym_list
)
1008 FOR_EACH_PTR(sym_list
, sym
) {
1009 __pass_to_client(sym
, DECLARATION_HOOK
);
1010 do_initializer_stuff(sym
);
1012 } END_FOR_EACH_PTR(sym
);
1015 static void call_global_assign_hooks(struct expression
*assign
)
1017 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1020 static void fake_global_assign(struct symbol
*sym
)
1022 struct expression
*assign
, *symbol
;
1024 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1025 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1026 fake_element_assigns(sym
, call_global_assign_hooks
);
1027 } else if (sym
->initializer
) {
1028 symbol
= symbol_expression(sym
);
1029 assign
= assign_expression(symbol
, sym
->initializer
);
1030 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1032 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1034 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1035 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1036 fake_member_assigns(sym
, call_global_assign_hooks
);
1037 } else if (sym
->initializer
) {
1038 symbol
= symbol_expression(sym
);
1039 assign
= assign_expression(symbol
, sym
->initializer
);
1040 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1042 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1045 symbol
= symbol_expression(sym
);
1046 if (sym
->initializer
)
1047 assign
= assign_expression(symbol
, sym
->initializer
);
1049 assign
= assign_expression(symbol
, zero_expr());
1050 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1054 static void start_function_definition(struct symbol
*sym
)
1056 __in_function_def
= 1;
1057 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1058 __in_function_def
= 0;
1059 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1063 static void split_function(struct symbol
*sym
)
1065 struct symbol
*base_type
= get_base_type(sym
);
1067 gettimeofday(&fn_start_time
, NULL
);
1070 cur_func
= sym
->ident
->name
;
1071 __smatch_lineno
= sym
->pos
.line
;
1073 sm_debug("new function: %s\n", cur_func
);
1075 if (option_two_passes
) {
1079 start_function_definition(sym
);
1080 __split_stmt(base_type
->stmt
);
1081 __split_stmt(base_type
->inline_stmt
);
1087 start_function_definition(sym
);
1088 __split_stmt(base_type
->stmt
);
1089 __split_stmt(base_type
->inline_stmt
);
1090 __pass_to_client(sym
, END_FUNC_HOOK
);
1091 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1093 cur_func_sym
= NULL
;
1096 free_data_info_allocs();
1097 free_expression_stack(&switch_expr_stack
);
1098 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1099 __bail_on_rest_of_function
= 0;
1102 static void parse_inline(struct expression
*call
)
1104 struct symbol
*base_type
;
1105 int loop_num_bak
= loop_num
;
1106 int final_pass_bak
= final_pass
;
1107 char *cur_func_bak
= cur_func
;
1108 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1109 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1110 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1111 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1113 __pass_to_client(call
, INLINE_FN_START
);
1114 final_pass
= 0; /* don't print anything */
1117 base_type
= get_base_type(call
->fn
->symbol
);
1118 cur_func_sym
= call
->fn
->symbol
;
1119 if (call
->fn
->symbol
->ident
)
1120 cur_func
= call
->fn
->symbol
->ident
->name
;
1123 set_position(call
->fn
->symbol
->pos
);
1126 big_statement_stack
= NULL
;
1127 big_expression_stack
= NULL
;
1128 switch_expr_stack
= NULL
;
1130 sm_debug("inline function: %s\n", cur_func
);
1133 start_function_definition(call
->fn
->symbol
);
1134 __split_stmt(base_type
->stmt
);
1135 __split_stmt(base_type
->inline_stmt
);
1136 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1137 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1139 free_expression_stack(&switch_expr_stack
);
1140 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1144 loop_num
= loop_num_bak
;
1145 final_pass
= final_pass_bak
;
1146 cur_func_sym
= cur_func_sym_bak
;
1147 cur_func
= cur_func_bak
;
1148 big_statement_stack
= big_statement_stack_bak
;
1149 big_expression_stack
= big_expression_stack_bak
;
1150 switch_expr_stack
= switch_expr_stack_bak
;
1152 restore_all_states();
1153 set_position(call
->pos
);
1155 __pass_to_client(call
, INLINE_FN_END
);
1158 static struct symbol_list
*inlines_called
;
1159 static void add_inline_function(struct symbol
*sym
)
1161 static struct symbol_list
*already_added
;
1164 FOR_EACH_PTR(already_added
, tmp
) {
1167 } END_FOR_EACH_PTR(tmp
);
1169 add_ptr_list(&already_added
, sym
);
1170 add_ptr_list(&inlines_called
, sym
);
1173 static void process_inlines()
1177 FOR_EACH_PTR(inlines_called
, tmp
) {
1178 split_function(tmp
);
1179 } END_FOR_EACH_PTR(tmp
);
1180 free_ptr_list(&inlines_called
);
1183 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1187 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1190 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1192 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1194 } END_FOR_EACH_PTR_REVERSE(sym
);
1199 static void split_inlines_in_scope(struct symbol
*sym
)
1201 struct symbol
*base
;
1202 struct symbol_list
*scope_list
;
1205 scope_list
= sym
->scope
->symbols
;
1206 stream
= sym
->pos
.stream
;
1208 /* find the last static symbol in the file */
1209 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1210 if (sym
->pos
.stream
!= stream
)
1212 if (sym
->type
!= SYM_NODE
)
1214 base
= get_base_type(sym
);
1217 if (base
->type
!= SYM_FN
)
1219 if (!base
->inline_stmt
)
1221 add_inline_function(sym
);
1222 } END_FOR_EACH_PTR_REVERSE(sym
);
1227 static void split_inlines(struct symbol_list
*sym_list
)
1231 sym
= get_last_scoped_symbol(sym_list
, 0);
1233 split_inlines_in_scope(sym
);
1234 sym
= get_last_scoped_symbol(sym_list
, 1);
1236 split_inlines_in_scope(sym
);
1239 static void split_functions(struct symbol_list
*sym_list
)
1243 FOR_EACH_PTR(sym_list
, sym
) {
1244 set_position(sym
->pos
);
1245 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1246 split_function(sym
);
1249 __pass_to_client(sym
, BASE_HOOK
);
1250 fake_global_assign(sym
);
1252 } END_FOR_EACH_PTR(sym
);
1253 split_inlines(sym_list
);
1254 __pass_to_client(sym_list
, END_FILE_HOOK
);
1257 void smatch(int argc
, char **argv
)
1260 struct string_list
*filelist
= NULL
;
1261 struct symbol_list
*sym_list
;
1264 printf("Usage: smatch [--debug] <filename.c>\n");
1267 sparse_initialize(argc
, argv
, &filelist
);
1268 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1269 if (option_file_output
) {
1272 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1273 sm_outfd
= fopen(buf
, "w");
1275 printf("Error: Cannot open %s\n", base_file
);
1279 sym_list
= sparse_keep_tokens(base_file
);
1280 split_functions(sym_list
);
1281 } END_FOR_EACH_PTR_NOTAG(base_file
);