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 static int last_goto_statement_handled
;
42 int __expr_stmt_count
;
43 int __in_function_def
;
44 static struct expression_list
*switch_expr_stack
= NULL
;
45 static struct expression_list
*post_op_stack
= NULL
;
47 struct expression_list
*big_expression_stack
;
48 struct statement_list
*big_statement_stack
;
49 struct statement
*__prev_stmt
;
50 struct statement
*__cur_stmt
;
51 struct statement
*__next_stmt
;
52 int __in_pre_condition
= 0;
53 int __bail_on_rest_of_function
= 0;
54 static struct timeval fn_start_time
;
55 char *get_function(void) { return cur_func
; }
56 int get_lineno(void) { return __smatch_lineno
; }
57 int inside_loop(void) { return !!loop_count
; }
58 int definitely_inside_loop(void) { return !!(loop_count
& ~0x80000000); }
59 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
60 int in_expression_statement(void) { return !!__expr_stmt_count
; }
62 static void split_symlist(struct symbol_list
*sym_list
);
63 static void split_declaration(struct symbol_list
*sym_list
);
64 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
65 static void add_inline_function(struct symbol
*sym
);
66 static void parse_inline(struct expression
*expr
);
68 int option_assume_loops
= 0;
69 int option_two_passes
= 0;
70 struct symbol
*cur_func_sym
= NULL
;
71 struct stree
*global_states
;
73 long long valid_ptr_min
= 4096;
74 long long valid_ptr_max
= 2117777777;
75 sval_t valid_ptr_min_sval
= {
79 sval_t valid_ptr_max_sval
= {
81 {.value
= LONG_MAX
- 100000},
84 static void set_valid_ptr_max(void)
86 if (type_bits(&ptr_ctype
) == 32)
87 valid_ptr_max
= 2117777777;
88 else if (type_bits(&ptr_ctype
) == 64)
89 valid_ptr_max
= 2117777777777777777LL;
91 valid_ptr_max_sval
.value
= valid_ptr_max
;
94 int outside_of_function(void)
96 return cur_func_sym
== NULL
;
99 const char *get_filename(void)
103 if (option_full_path
)
104 return full_filename
;
108 const char *get_base_file(void)
113 static void set_position(struct position pos
)
116 static int prev_stream
= -1;
118 if (pos
.stream
== 0 && pos
.line
== 0)
121 __smatch_lineno
= pos
.line
;
123 if (pos
.stream
== prev_stream
)
126 filename
= stream_name(pos
.stream
);
129 pathname
= getcwd(NULL
, 0);
131 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
132 full_filename
= malloc(len
);
133 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
135 full_filename
= alloc_string(filename
);
140 static void set_parent(struct expression
*expr
, struct expression
*parent
)
144 expr
->parent
= parent
;
147 static void set_parent_stmt(struct statement
*stmt
, struct statement
*parent
)
151 stmt
->parent
= parent
;
154 int is_assigned_call(struct expression
*expr
)
156 struct expression
*tmp
;
158 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
159 if (tmp
->type
== EXPR_ASSIGNMENT
&& tmp
->op
== '=' &&
160 strip_expr(tmp
->right
) == expr
)
162 if (tmp
->pos
.line
< expr
->pos
.line
)
164 } END_FOR_EACH_PTR_REVERSE(tmp
);
168 static int is_inline_func(struct expression
*expr
)
170 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
172 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
177 static int is_noreturn_func(struct expression
*expr
)
179 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
181 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
186 int inlinable(struct expression
*expr
)
189 struct statement
*last_stmt
= NULL
;
191 if (__inline_fn
) /* don't nest */
194 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
196 if (is_no_inline_function(expr
->symbol
->ident
->name
))
198 sym
= get_base_type(expr
->symbol
);
199 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
200 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
202 if (sym
->stmt
->type
!= STMT_COMPOUND
)
204 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
206 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
207 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
209 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
211 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
217 /* the magic numbers in this function are pulled out of my bum. */
218 if (last_stmt
->pos
.line
> sym
->pos
.line
+ 20)
224 void __process_post_op_stack(void)
226 struct expression
*expr
;
228 FOR_EACH_PTR(post_op_stack
, expr
) {
229 __pass_to_client(expr
, OP_HOOK
);
230 } END_FOR_EACH_PTR(expr
);
232 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
235 static int handle_comma_assigns(struct expression
*expr
)
237 struct expression
*right
;
238 struct expression
*assign
;
240 right
= strip_expr(expr
->right
);
241 if (right
->type
!= EXPR_COMMA
)
244 __split_expr(right
->left
);
245 __process_post_op_stack();
247 assign
= assign_expression(expr
->left
, right
->right
);
248 __split_expr(assign
);
253 static int prev_expression_is_getting_address(struct expression
*expr
)
255 struct expression
*parent
;
258 parent
= expr
->parent
;
262 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
264 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
266 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
275 void __split_expr(struct expression
*expr
)
280 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
282 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
284 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
287 push_expression(&big_expression_stack
, expr
);
288 set_position(expr
->pos
);
289 __pass_to_client(expr
, EXPR_HOOK
);
291 switch (expr
->type
) {
293 set_parent(expr
->unop
, expr
);
295 if (expr
->op
== '*' &&
296 !prev_expression_is_getting_address(expr
))
297 __pass_to_client(expr
, DEREF_HOOK
);
298 __split_expr(expr
->unop
);
299 __pass_to_client(expr
, OP_HOOK
);
302 set_parent(expr
->unop
, expr
);
304 __split_expr(expr
->unop
);
305 push_expression(&post_op_stack
, expr
);
309 __split_stmt(expr
->statement
);
314 set_parent(expr
->left
, expr
);
315 set_parent(expr
->right
, expr
);
317 __pass_to_client(expr
, LOGIC_HOOK
);
318 __handle_logic(expr
);
321 set_parent(expr
->left
, expr
);
322 set_parent(expr
->right
, expr
);
324 __pass_to_client(expr
, BINOP_HOOK
);
326 set_parent(expr
->left
, expr
);
327 set_parent(expr
->right
, expr
);
329 __split_expr(expr
->left
);
330 __process_post_op_stack();
331 __split_expr(expr
->right
);
333 case EXPR_ASSIGNMENT
: {
334 struct expression
*tmp
;
336 set_parent(expr
->left
, expr
);
337 set_parent(expr
->right
, expr
);
342 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
345 if (__handle_condition_assigns(expr
))
347 /* foo = (x < 5 ? foo : 5); */
348 if (__handle_select_assigns(expr
))
350 /* foo = ({frob(); frob(); frob(); 1;}) */
351 if (__handle_expr_statement_assigns(expr
))
354 if (handle_comma_assigns(expr
))
357 __split_expr(expr
->right
);
358 if (outside_of_function())
359 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
361 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
363 __fake_struct_member_assignments(expr
);
365 tmp
= strip_expr(expr
->right
);
366 if (expr
->op
== '=' && tmp
->type
== EXPR_CALL
) {
367 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
368 if (!is_fake_call(tmp
))
369 __pass_to_client(tmp
, FUNCTION_CALL_HOOK_AFTER
);
371 if (get_macro_name(tmp
->pos
) &&
372 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
373 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
374 __split_expr(expr
->left
);
378 set_parent(expr
->deref
, expr
);
380 __pass_to_client(expr
, DEREF_HOOK
);
381 __split_expr(expr
->deref
);
384 set_parent(expr
->base
, expr
);
386 __split_expr(expr
->base
);
389 case EXPR_FORCE_CAST
:
390 set_parent(expr
->cast_expression
, expr
);
392 __pass_to_client(expr
, CAST_HOOK
);
393 __split_expr(expr
->cast_expression
);
396 if (expr
->cast_expression
)
397 __pass_to_client(strip_parens(expr
->cast_expression
),
402 evaluate_expression(expr
);
404 case EXPR_CONDITIONAL
:
406 set_parent(expr
->conditional
, expr
);
407 set_parent(expr
->cond_true
, expr
);
408 set_parent(expr
->cond_false
, expr
);
410 if (known_condition_true(expr
->conditional
)) {
411 __split_expr(expr
->cond_true
);
414 if (known_condition_false(expr
->conditional
)) {
415 __split_expr(expr
->cond_false
);
418 __pass_to_client(expr
, SELECT_HOOK
);
419 __split_whole_condition(expr
->conditional
);
420 __split_expr(expr
->cond_true
);
421 __push_true_states();
422 __use_false_states();
423 __split_expr(expr
->cond_false
);
424 __merge_true_states();
427 set_parent(expr
->fn
, expr
);
429 if (sym_name_is("__builtin_constant_p", expr
->fn
))
431 split_expr_list(expr
->args
, expr
);
432 __split_expr(expr
->fn
);
433 if (is_inline_func(expr
->fn
))
434 add_inline_function(expr
->fn
->symbol
);
435 if (inlinable(expr
->fn
))
437 __process_post_op_stack();
438 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
440 if (inlinable(expr
->fn
)) {
443 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
444 if (!is_assigned_call(expr
))
445 __pass_to_client(expr
, FUNCTION_CALL_HOOK_AFTER
);
446 if (is_noreturn_func(expr
->fn
))
449 case EXPR_INITIALIZER
:
450 split_expr_list(expr
->expr_list
, expr
);
452 case EXPR_IDENTIFIER
:
453 set_parent(expr
->ident_expression
, expr
);
454 __split_expr(expr
->ident_expression
);
457 set_parent(expr
->idx_expression
, expr
);
458 __split_expr(expr
->idx_expression
);
461 set_parent(expr
->init_expr
, expr
);
462 __split_expr(expr
->init_expr
);
465 __pass_to_client(expr
, SYM_HOOK
);
468 __pass_to_client(expr
, STRING_HOOK
);
473 pop_expression(&big_expression_stack
);
476 static int is_forever_loop(struct statement
*stmt
)
478 struct expression
*expr
;
480 expr
= strip_expr(stmt
->iterator_pre_condition
);
482 expr
= stmt
->iterator_post_condition
;
484 /* this is a for(;;) loop... */
488 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
495 static char *get_loop_name(int num
)
499 snprintf(buf
, 255, "-loop%d", num
);
501 return alloc_sname(buf
);
505 * Pre Loops are while and for loops.
507 static void handle_pre_loop(struct statement
*stmt
)
509 int once_through
; /* we go through the loop at least once */
510 struct sm_state
*extra_sm
= NULL
;
513 struct stree
*stree
= NULL
;
514 struct sm_state
*sm
= NULL
;
516 loop_name
= get_loop_name(loop_num
);
519 __split_stmt(stmt
->iterator_pre_statement
);
520 __prev_stmt
= stmt
->iterator_pre_statement
;
522 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
528 __merge_gotos(loop_name
);
530 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
531 __in_pre_condition
++;
532 __pass_to_client(stmt
, PRELOOP_HOOK
);
533 __split_whole_condition(stmt
->iterator_pre_condition
);
534 __in_pre_condition
--;
535 FOR_EACH_SM(stree
, sm
) {
536 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
537 } END_FOR_EACH_SM(sm
);
540 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
542 if (option_assume_loops
)
545 __split_stmt(stmt
->iterator_statement
);
546 if (is_forever_loop(stmt
)) {
548 __save_gotos(loop_name
);
550 __push_fake_cur_stree();
551 __split_stmt(stmt
->iterator_post_statement
);
552 stree
= __pop_fake_cur_stree();
554 __discard_false_states();
557 if (!__path_is_null())
558 __merge_stree_into_cur(stree
);
562 unchanged
= __iterator_unchanged(extra_sm
);
563 __split_stmt(stmt
->iterator_post_statement
);
564 __prev_stmt
= stmt
->iterator_post_statement
;
567 __save_gotos(loop_name
);
568 __in_pre_condition
++;
569 __split_whole_condition(stmt
->iterator_pre_condition
);
570 __in_pre_condition
--;
572 __merge_false_states();
574 __discard_false_states();
576 __merge_false_states();
578 if (extra_sm
&& unchanged
)
579 __extra_pre_loop_hook_after(extra_sm
,
580 stmt
->iterator_post_statement
,
581 stmt
->iterator_pre_condition
);
588 * Post loops are do {} while();
590 static void handle_post_loop(struct statement
*stmt
)
594 loop_name
= get_loop_name(loop_num
);
600 __merge_gotos(loop_name
);
601 __split_stmt(stmt
->iterator_statement
);
603 if (!is_zero(stmt
->iterator_post_condition
))
604 __save_gotos(loop_name
);
606 if (is_forever_loop(stmt
)) {
609 __split_whole_condition(stmt
->iterator_post_condition
);
610 __use_false_states();
616 static int empty_statement(struct statement
*stmt
)
620 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
625 static int last_stmt_on_same_line(void)
627 struct statement
*stmt
;
630 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
633 if (stmt
->pos
.line
== get_lineno())
636 } END_FOR_EACH_PTR_REVERSE(stmt
);
640 static void split_asm_constraints(struct expression_list
*expr_list
)
642 struct expression
*expr
;
645 FOR_EACH_PTR(expr_list
, expr
) {
647 case 0: /* identifier */
648 case 1: /* constraint */
651 case 2: /* expression */
656 } END_FOR_EACH_PTR(expr
);
659 static int is_case_val(struct statement
*stmt
, sval_t sval
)
663 if (stmt
->type
!= STMT_CASE
)
665 if (!stmt
->case_expression
) {
669 if (!get_value(stmt
->case_expression
, &case_sval
))
671 if (case_sval
.value
== sval
.value
)
676 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
678 struct statement
*tmp
;
680 __split_expr(stmt
->switch_expression
);
682 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
683 __save_switch_states(top_expression(switch_expr_stack
));
688 stmt
= stmt
->switch_statement
;
690 __push_scope_hooks();
691 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
692 __smatch_lineno
= tmp
->pos
.line
;
693 if (is_case_val(tmp
, sval
)) {
694 __merge_switches(top_expression(switch_expr_stack
),
695 stmt
->case_expression
, stmt
->case_to
);
696 __pass_case_to_client(top_expression(switch_expr_stack
),
697 stmt
->case_expression
);
699 if (__path_is_null())
702 if (__path_is_null()) {
706 } END_FOR_EACH_PTR(tmp
);
708 __call_scope_hooks();
709 if (!__pop_default())
710 __merge_switches(top_expression(switch_expr_stack
),
712 __discard_switches();
714 pop_expression(&switch_expr_stack
);
717 static int taking_too_long(void)
721 ms
= ms_since(&fn_start_time
);
722 if (ms
> 1000 * 60 * 5) /* five minutes */
727 static int is_last_stmt(struct statement
*cur_stmt
)
729 struct symbol
*fn
= get_base_type(cur_func_sym
);
730 struct statement
*stmt
;
736 stmt
= fn
->inline_stmt
;
737 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
739 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
740 if (stmt
&& stmt
->type
== STMT_LABEL
)
741 stmt
= stmt
->label_statement
;
742 if (stmt
== cur_stmt
)
747 static void handle_backward_goto(struct statement
*goto_stmt
)
749 const char *goto_name
, *label_name
;
750 struct statement
*func_stmt
;
751 struct symbol
*base_type
= get_base_type(cur_func_sym
);
752 struct statement
*tmp
;
757 if (last_goto_statement_handled
)
759 last_goto_statement_handled
= 1;
761 if (!goto_stmt
->goto_label
||
762 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
763 !goto_stmt
->goto_label
->ident
)
765 goto_name
= goto_stmt
->goto_label
->ident
->name
;
767 func_stmt
= base_type
->stmt
;
769 func_stmt
= base_type
->inline_stmt
;
772 if (func_stmt
->type
!= STMT_COMPOUND
)
775 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
777 if (tmp
->type
!= STMT_LABEL
)
779 if (!tmp
->label_identifier
||
780 tmp
->label_identifier
->type
!= SYM_LABEL
||
781 !tmp
->label_identifier
->ident
)
783 label_name
= tmp
->label_identifier
->ident
->name
;
784 if (strcmp(goto_name
, label_name
) != 0)
789 } END_FOR_EACH_PTR(tmp
);
792 static void fake_a_return(void)
794 struct symbol
*return_type
;
799 return_type
= get_real_base_type(cur_func_sym
);
800 return_type
= get_real_base_type(return_type
);
801 if (return_type
!= &void_ctype
) {
802 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
807 static void split_compound(struct statement
*stmt
)
809 struct statement
*prev
= NULL
;
810 struct statement
*cur
= NULL
;
811 struct statement
*next
;
813 __push_scope_hooks();
815 FOR_EACH_PTR(stmt
->stmts
, next
) {
816 /* just set them all ahead of time */
817 set_parent_stmt(next
, stmt
);
827 } END_FOR_EACH_PTR(next
);
835 __call_scope_hooks();
838 void __split_stmt(struct statement
*stmt
)
845 if (__bail_on_rest_of_function
)
848 if (out_of_memory() || taking_too_long()) {
850 __bail_on_rest_of_function
= 1;
851 sm_msg("Function too hairy. Giving up.");
853 final_pass
= 0; /* turn off sm_msg() from here */
857 add_ptr_list(&big_statement_stack
, stmt
);
858 free_expression_stack(&big_expression_stack
);
859 set_position(stmt
->pos
);
860 __pass_to_client(stmt
, STMT_HOOK
);
862 switch (stmt
->type
) {
863 case STMT_DECLARATION
:
864 split_declaration(stmt
->declaration
);
867 __split_expr(stmt
->ret_value
);
868 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
869 __process_post_op_stack();
872 case STMT_EXPRESSION
:
873 __split_expr(stmt
->expression
);
876 split_compound(stmt
);
879 set_parent_stmt(stmt
->if_true
, stmt
);
880 set_parent_stmt(stmt
->if_false
, stmt
);
882 if (known_condition_true(stmt
->if_conditional
)) {
883 __split_stmt(stmt
->if_true
);
886 if (known_condition_false(stmt
->if_conditional
)) {
887 __split_stmt(stmt
->if_false
);
890 __split_whole_condition(stmt
->if_conditional
);
891 __split_stmt(stmt
->if_true
);
892 if (empty_statement(stmt
->if_true
) &&
893 last_stmt_on_same_line() &&
894 !get_macro_name(stmt
->if_true
->pos
))
895 sm_msg("warn: if();");
896 __push_true_states();
897 __use_false_states();
898 __split_stmt(stmt
->if_false
);
899 __merge_true_states();
902 set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
903 set_parent_stmt(stmt
->iterator_statement
, stmt
);
904 set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
906 if (stmt
->iterator_pre_condition
)
907 handle_pre_loop(stmt
);
908 else if (stmt
->iterator_post_condition
)
909 handle_post_loop(stmt
);
911 // these are for(;;) type loops.
912 handle_pre_loop(stmt
);
916 set_parent_stmt(stmt
->switch_statement
, stmt
);
918 if (get_value(stmt
->switch_expression
, &sval
)) {
919 split_known_switch(stmt
, sval
);
922 __split_expr(stmt
->switch_expression
);
923 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
924 __save_switch_states(top_expression(switch_expr_stack
));
928 __split_stmt(stmt
->switch_statement
);
929 if (!__pop_default())
930 __merge_switches(top_expression(switch_expr_stack
),
932 __discard_switches();
934 pop_expression(&switch_expr_stack
);
937 __merge_switches(top_expression(switch_expr_stack
),
938 stmt
->case_expression
, stmt
->case_to
);
939 __pass_case_to_client(top_expression(switch_expr_stack
),
940 stmt
->case_expression
);
941 if (!stmt
->case_expression
)
943 __split_expr(stmt
->case_expression
);
944 __split_expr(stmt
->case_to
);
945 __split_stmt(stmt
->case_statement
);
948 if (stmt
->label_identifier
&&
949 stmt
->label_identifier
->type
== SYM_LABEL
&&
950 stmt
->label_identifier
->ident
) {
951 loop_count
|= 0x80000000;
952 __merge_gotos(stmt
->label_identifier
->ident
->name
);
954 __split_stmt(stmt
->label_statement
);
957 __split_expr(stmt
->goto_expression
);
958 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
959 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
961 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
963 __process_continues();
965 } else if (stmt
->goto_label
&&
966 stmt
->goto_label
->type
== SYM_LABEL
&&
967 stmt
->goto_label
->ident
) {
968 __save_gotos(stmt
->goto_label
->ident
->name
);
971 if (is_last_stmt(stmt
))
972 handle_backward_goto(stmt
);
977 __pass_to_client(stmt
, ASM_HOOK
);
978 __split_expr(stmt
->asm_string
);
979 split_asm_constraints(stmt
->asm_outputs
);
980 split_asm_constraints(stmt
->asm_inputs
);
981 split_asm_constraints(stmt
->asm_clobbers
);
986 __split_expr(stmt
->range_expression
);
987 __split_expr(stmt
->range_low
);
988 __split_expr(stmt
->range_high
);
991 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
993 __process_post_op_stack();
996 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
998 struct expression
*expr
;
1000 FOR_EACH_PTR(expr_list
, expr
) {
1001 set_parent(expr
, parent
);
1003 __process_post_op_stack();
1004 } END_FOR_EACH_PTR(expr
);
1007 static void split_sym(struct symbol
*sym
)
1011 if (!(sym
->namespace & NS_SYMBOL
))
1014 __split_stmt(sym
->stmt
);
1015 __split_expr(sym
->array_size
);
1016 split_symlist(sym
->arguments
);
1017 split_symlist(sym
->symbol_list
);
1018 __split_stmt(sym
->inline_stmt
);
1019 split_symlist(sym
->inline_symbol_list
);
1022 static void split_symlist(struct symbol_list
*sym_list
)
1026 FOR_EACH_PTR(sym_list
, sym
) {
1028 } END_FOR_EACH_PTR(sym
);
1031 typedef void (fake_cb
)(struct expression
*expr
);
1033 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1035 struct symbol
*type
, *tmp
;
1041 name
= member
->name
;
1043 type
= get_type(expr
);
1044 if (!type
|| type
->type
!= SYM_STRUCT
)
1048 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1052 if (strcmp(name
, tmp
->ident
->name
) == 0)
1054 } END_FOR_EACH_PTR(tmp
);
1058 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1060 struct symbol
*type
, *member
;
1063 type
= get_type(expr
);
1064 if (!type
|| type
->type
!= SYM_STRUCT
)
1067 FOR_EACH_PTR(type
->symbol_list
, member
) {
1069 return member
->ident
;
1071 } END_FOR_EACH_PTR(member
);
1075 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1078 struct ident
*ident
;
1082 static struct member_set
*alloc_member_set(struct symbol
*type
)
1084 struct member_set
*member_set
;
1085 struct symbol
*member
;
1089 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1090 member_set
= malloc(member_count
* sizeof(*member_set
));
1092 FOR_EACH_PTR(type
->symbol_list
, member
) {
1093 member_set
[member_idx
].ident
= member
->ident
;
1094 member_set
[member_idx
].set
= 0;
1096 } END_FOR_EACH_PTR(member
);
1101 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
1103 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1106 for (i
= 0; i
< member_count
; i
++) {
1107 if (member_set
[i
].ident
== ident
) {
1108 member_set
[i
].set
= 1;
1112 // crap. this is buggy.
1113 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1116 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1118 struct expression
*edge_member
, *assign
;
1119 struct symbol
*base
= get_real_base_type(member
);
1123 expr
= member_expression(expr
, '.', member
->ident
);
1125 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1126 struct symbol
*type
;
1128 type
= get_real_base_type(tmp
);
1133 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1134 if (get_state_expr(SMATCH_EXTRA
, edge_member
))
1138 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1139 set_inner_struct_members(expr
, tmp
);
1146 assign
= assign_expression(edge_member
, zero_expr());
1147 __split_expr(assign
);
1148 } END_FOR_EACH_PTR(tmp
);
1153 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1156 struct expression
*member
, *assign
;
1159 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1160 expr
= strip_expr(expr
->unop
);
1164 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1165 type
= get_real_base_type(tmp
);
1170 member
= member_expression(expr
, op
, tmp
->ident
);
1171 if (get_state_expr(SMATCH_EXTRA
, member
))
1175 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1176 set_inner_struct_members(expr
, tmp
);
1179 if (type
->type
== SYM_ARRAY
)
1184 assign
= assign_expression(member
, zero_expr());
1185 __split_expr(assign
);
1186 } END_FOR_EACH_PTR(tmp
);
1189 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1191 struct expression
*deref
, *assign
, *tmp
;
1192 struct symbol
*struct_type
, *type
;
1193 struct ident
*member
;
1195 struct member_set
*member_set
;
1197 struct_type
= get_type(symbol
);
1199 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1202 member_set
= alloc_member_set(struct_type
);
1205 FOR_EACH_PTR(members
, tmp
) {
1206 member
= number_to_member(symbol
, member_idx
);
1207 while (tmp
->type
== EXPR_IDENTIFIER
) {
1208 member
= tmp
->expr_ident
;
1209 member_idx
= member_to_number(symbol
, member
);
1210 tmp
= tmp
->ident_expression
;
1212 mark_member_as_set(struct_type
, member_set
, member
);
1214 deref
= member_expression(symbol
, '.', member
);
1215 if (tmp
->type
== EXPR_INITIALIZER
) {
1216 type
= get_type(deref
);
1217 if (type
&& type
->type
== SYM_ARRAY
)
1218 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1220 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1222 assign
= assign_expression(deref
, tmp
);
1225 } END_FOR_EACH_PTR(tmp
);
1227 set_unset_to_zero(struct_type
, symbol
);
1230 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1232 fake_member_assigns_helper(symbol_expression(sym
),
1233 sym
->initializer
->expr_list
, fake_cb
);
1236 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1238 struct expression
*offset
, *binop
, *assign
, *tmp
;
1239 struct symbol
*type
;
1242 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1246 FOR_EACH_PTR(expr_list
, tmp
) {
1247 if (tmp
->type
== EXPR_INDEX
) {
1248 if (tmp
->idx_from
!= tmp
->idx_to
)
1250 idx
= tmp
->idx_from
;
1251 if (!tmp
->idx_expression
)
1253 tmp
= tmp
->idx_expression
;
1255 offset
= value_expr(idx
);
1256 binop
= array_element_expression(array
, offset
);
1257 if (tmp
->type
== EXPR_INITIALIZER
) {
1258 type
= get_type(binop
);
1259 if (type
&& type
->type
== SYM_ARRAY
)
1260 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1262 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1264 assign
= assign_expression(binop
, tmp
);
1269 } END_FOR_EACH_PTR(tmp
);
1272 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1274 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1277 static void fake_assign_expr(struct symbol
*sym
)
1279 struct expression
*assign
, *symbol
;
1281 symbol
= symbol_expression(sym
);
1282 assign
= assign_expression(symbol
, sym
->initializer
);
1283 __split_expr(assign
);
1286 static void call_split_expr(struct expression
*expr
)
1291 static void do_initializer_stuff(struct symbol
*sym
)
1293 if (!sym
->initializer
)
1296 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1297 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1298 fake_element_assigns(sym
, call_split_expr
);
1300 fake_member_assigns(sym
, call_split_expr
);
1302 fake_assign_expr(sym
);
1306 static void split_declaration(struct symbol_list
*sym_list
)
1310 FOR_EACH_PTR(sym_list
, sym
) {
1311 __pass_to_client(sym
, DECLARATION_HOOK
);
1312 do_initializer_stuff(sym
);
1314 } END_FOR_EACH_PTR(sym
);
1317 static void call_global_assign_hooks(struct expression
*assign
)
1319 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1322 static void fake_global_assign(struct symbol
*sym
)
1324 struct expression
*assign
, *symbol
;
1326 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1327 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1328 fake_element_assigns(sym
, call_global_assign_hooks
);
1329 } else if (sym
->initializer
) {
1330 symbol
= symbol_expression(sym
);
1331 assign
= assign_expression(symbol
, sym
->initializer
);
1332 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1334 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1336 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1337 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1338 fake_member_assigns(sym
, call_global_assign_hooks
);
1339 } else if (sym
->initializer
) {
1340 symbol
= symbol_expression(sym
);
1341 assign
= assign_expression(symbol
, sym
->initializer
);
1342 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1344 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1347 symbol
= symbol_expression(sym
);
1348 if (sym
->initializer
)
1349 assign
= assign_expression(symbol
, sym
->initializer
);
1351 assign
= assign_expression(symbol
, zero_expr());
1352 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1356 static void start_function_definition(struct symbol
*sym
)
1358 __in_function_def
= 1;
1359 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1360 __in_function_def
= 0;
1361 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1365 static void split_function(struct symbol
*sym
)
1367 struct symbol
*base_type
= get_base_type(sym
);
1369 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1372 gettimeofday(&fn_start_time
, NULL
);
1375 cur_func
= sym
->ident
->name
;
1376 __smatch_lineno
= sym
->pos
.line
;
1378 last_goto_statement_handled
= 0;
1379 sm_debug("new function: %s\n", cur_func
);
1381 if (option_two_passes
) {
1385 start_function_definition(sym
);
1386 __split_stmt(base_type
->stmt
);
1387 __split_stmt(base_type
->inline_stmt
);
1393 start_function_definition(sym
);
1394 __split_stmt(base_type
->stmt
);
1395 __split_stmt(base_type
->inline_stmt
);
1396 __pass_to_client(sym
, END_FUNC_HOOK
);
1397 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1400 cur_func_sym
= NULL
;
1402 free_data_info_allocs();
1403 free_expression_stack(&switch_expr_stack
);
1404 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1405 __bail_on_rest_of_function
= 0;
1408 static void parse_inline(struct expression
*call
)
1410 struct symbol
*base_type
;
1411 int loop_num_bak
= loop_num
;
1412 int final_pass_bak
= final_pass
;
1413 char *cur_func_bak
= cur_func
;
1414 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1415 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1416 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1417 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1419 __pass_to_client(call
, INLINE_FN_START
);
1420 final_pass
= 0; /* don't print anything */
1423 base_type
= get_base_type(call
->fn
->symbol
);
1424 cur_func_sym
= call
->fn
->symbol
;
1425 if (call
->fn
->symbol
->ident
)
1426 cur_func
= call
->fn
->symbol
->ident
->name
;
1429 set_position(call
->fn
->symbol
->pos
);
1432 big_statement_stack
= NULL
;
1433 big_expression_stack
= NULL
;
1434 switch_expr_stack
= NULL
;
1436 sm_debug("inline function: %s\n", cur_func
);
1439 start_function_definition(call
->fn
->symbol
);
1440 __split_stmt(base_type
->stmt
);
1441 __split_stmt(base_type
->inline_stmt
);
1442 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1443 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1445 free_expression_stack(&switch_expr_stack
);
1446 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1450 loop_num
= loop_num_bak
;
1451 final_pass
= final_pass_bak
;
1452 cur_func_sym
= cur_func_sym_bak
;
1453 cur_func
= cur_func_bak
;
1454 big_statement_stack
= big_statement_stack_bak
;
1455 big_expression_stack
= big_expression_stack_bak
;
1456 switch_expr_stack
= switch_expr_stack_bak
;
1458 restore_all_states();
1459 set_position(call
->pos
);
1461 __pass_to_client(call
, INLINE_FN_END
);
1464 static struct symbol_list
*inlines_called
;
1465 static void add_inline_function(struct symbol
*sym
)
1467 static struct symbol_list
*already_added
;
1470 FOR_EACH_PTR(already_added
, tmp
) {
1473 } END_FOR_EACH_PTR(tmp
);
1475 add_ptr_list(&already_added
, sym
);
1476 add_ptr_list(&inlines_called
, sym
);
1479 static void process_inlines(void)
1483 FOR_EACH_PTR(inlines_called
, tmp
) {
1484 split_function(tmp
);
1485 } END_FOR_EACH_PTR(tmp
);
1486 free_ptr_list(&inlines_called
);
1489 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1493 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1496 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1498 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1500 } END_FOR_EACH_PTR_REVERSE(sym
);
1505 static void split_inlines_in_scope(struct symbol
*sym
)
1507 struct symbol
*base
;
1508 struct symbol_list
*scope_list
;
1511 scope_list
= sym
->scope
->symbols
;
1512 stream
= sym
->pos
.stream
;
1514 /* find the last static symbol in the file */
1515 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1516 if (sym
->pos
.stream
!= stream
)
1518 if (sym
->type
!= SYM_NODE
)
1520 base
= get_base_type(sym
);
1523 if (base
->type
!= SYM_FN
)
1525 if (!base
->inline_stmt
)
1527 add_inline_function(sym
);
1528 } END_FOR_EACH_PTR_REVERSE(sym
);
1533 static void split_inlines(struct symbol_list
*sym_list
)
1537 sym
= get_last_scoped_symbol(sym_list
, 0);
1539 split_inlines_in_scope(sym
);
1540 sym
= get_last_scoped_symbol(sym_list
, 1);
1542 split_inlines_in_scope(sym
);
1545 static struct stree
*clone_estates_perm(struct stree
*orig
)
1547 struct stree
*ret
= NULL
;
1548 struct sm_state
*tmp
;
1550 FOR_EACH_SM(orig
, tmp
) {
1551 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1552 } END_FOR_EACH_SM(tmp
);
1557 static void split_functions(struct symbol_list
*sym_list
)
1562 FOR_EACH_PTR(sym_list
, sym
) {
1563 set_position(sym
->pos
);
1564 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1565 __pass_to_client(sym
, BASE_HOOK
);
1566 fake_global_assign(sym
);
1568 } END_FOR_EACH_PTR(sym
);
1569 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1572 FOR_EACH_PTR(sym_list
, sym
) {
1573 set_position(sym
->pos
);
1574 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1575 split_function(sym
);
1578 } END_FOR_EACH_PTR(sym
);
1579 split_inlines(sym_list
);
1580 __pass_to_client(sym_list
, END_FILE_HOOK
);
1583 void smatch(int argc
, char **argv
)
1586 struct string_list
*filelist
= NULL
;
1587 struct symbol_list
*sym_list
;
1590 printf("Usage: smatch [--debug] <filename.c>\n");
1593 sparse_initialize(argc
, argv
, &filelist
);
1594 set_valid_ptr_max();
1595 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1596 if (option_file_output
) {
1599 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1600 sm_outfd
= fopen(buf
, "w");
1602 printf("Error: Cannot open %s\n", base_file
);
1606 sym_list
= sparse_keep_tokens(base_file
);
1607 split_functions(sym_list
);
1608 } END_FOR_EACH_PTR_NOTAG(base_file
);