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_known_conditions
= 0;
70 int option_two_passes
= 0;
71 struct symbol
*cur_func_sym
= NULL
;
72 struct stree
*global_states
;
74 long long valid_ptr_min
= 4096;
75 long long valid_ptr_max
= 2117777777;
76 sval_t valid_ptr_min_sval
= {
80 sval_t valid_ptr_max_sval
= {
82 {.value
= LONG_MAX
- 100000},
85 static void set_valid_ptr_max(void)
87 if (type_bits(&ptr_ctype
) == 32)
88 valid_ptr_max
= 2117777777;
89 else if (type_bits(&ptr_ctype
) == 64)
90 valid_ptr_max
= 2117777777777777777LL;
92 valid_ptr_max_sval
.value
= valid_ptr_max
;
95 int outside_of_function(void)
97 return cur_func_sym
== NULL
;
100 const char *get_filename(void)
104 if (option_full_path
)
105 return full_filename
;
109 const char *get_base_file(void)
114 static void set_position(struct position pos
)
117 static int prev_stream
= -1;
119 if (pos
.stream
== 0 && pos
.line
== 0)
122 __smatch_lineno
= pos
.line
;
124 if (pos
.stream
== prev_stream
)
127 filename
= stream_name(pos
.stream
);
130 pathname
= getcwd(NULL
, 0);
132 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
133 full_filename
= malloc(len
);
134 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
136 full_filename
= alloc_string(filename
);
141 static void set_parent(struct expression
*expr
, struct expression
*parent
)
145 expr
->parent
= parent
;
148 static void set_parent_stmt(struct statement
*stmt
, struct statement
*parent
)
152 stmt
->parent
= parent
;
155 int is_assigned_call(struct expression
*expr
)
157 struct expression
*tmp
;
159 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
160 if (tmp
->type
== EXPR_ASSIGNMENT
&& tmp
->op
== '=' &&
161 strip_expr(tmp
->right
) == expr
)
163 if (tmp
->pos
.line
< expr
->pos
.line
)
165 } END_FOR_EACH_PTR_REVERSE(tmp
);
169 static int is_inline_func(struct expression
*expr
)
171 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
173 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
178 static int is_noreturn_func(struct expression
*expr
)
180 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
182 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
187 int inlinable(struct expression
*expr
)
190 struct statement
*last_stmt
= NULL
;
192 if (__inline_fn
) /* don't nest */
195 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
197 if (is_no_inline_function(expr
->symbol
->ident
->name
))
199 sym
= get_base_type(expr
->symbol
);
200 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
201 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
203 if (sym
->stmt
->type
!= STMT_COMPOUND
)
205 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
207 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
208 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
210 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
212 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
218 /* the magic numbers in this function are pulled out of my bum. */
219 if (last_stmt
->pos
.line
> sym
->pos
.line
+ 20)
225 void __process_post_op_stack(void)
227 struct expression
*expr
;
229 FOR_EACH_PTR(post_op_stack
, expr
) {
230 __pass_to_client(expr
, OP_HOOK
);
231 } END_FOR_EACH_PTR(expr
);
233 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
236 static int handle_comma_assigns(struct expression
*expr
)
238 struct expression
*right
;
239 struct expression
*assign
;
241 right
= strip_expr(expr
->right
);
242 if (right
->type
!= EXPR_COMMA
)
245 __split_expr(right
->left
);
246 __process_post_op_stack();
248 assign
= assign_expression(expr
->left
, right
->right
);
249 __split_expr(assign
);
254 static int prev_expression_is_getting_address(struct expression
*expr
)
256 struct expression
*parent
;
259 parent
= expr
->parent
;
263 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
265 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
267 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
276 void __split_expr(struct expression
*expr
)
281 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
283 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
285 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
288 push_expression(&big_expression_stack
, expr
);
289 set_position(expr
->pos
);
290 __pass_to_client(expr
, EXPR_HOOK
);
292 switch (expr
->type
) {
294 set_parent(expr
->unop
, expr
);
296 if (expr
->op
== '*' &&
297 !prev_expression_is_getting_address(expr
))
298 __pass_to_client(expr
, DEREF_HOOK
);
299 __split_expr(expr
->unop
);
300 __pass_to_client(expr
, OP_HOOK
);
303 set_parent(expr
->unop
, expr
);
305 __split_expr(expr
->unop
);
306 push_expression(&post_op_stack
, expr
);
310 __split_stmt(expr
->statement
);
315 set_parent(expr
->left
, expr
);
316 set_parent(expr
->right
, expr
);
318 __pass_to_client(expr
, LOGIC_HOOK
);
319 __handle_logic(expr
);
322 set_parent(expr
->left
, expr
);
323 set_parent(expr
->right
, expr
);
325 __pass_to_client(expr
, BINOP_HOOK
);
327 set_parent(expr
->left
, expr
);
328 set_parent(expr
->right
, expr
);
330 __split_expr(expr
->left
);
331 __process_post_op_stack();
332 __split_expr(expr
->right
);
334 case EXPR_ASSIGNMENT
: {
335 struct expression
*tmp
;
337 set_parent(expr
->left
, expr
);
338 set_parent(expr
->right
, expr
);
343 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
346 if (__handle_condition_assigns(expr
))
348 /* foo = (x < 5 ? foo : 5); */
349 if (__handle_select_assigns(expr
))
351 /* foo = ({frob(); frob(); frob(); 1;}) */
352 if (__handle_expr_statement_assigns(expr
))
355 if (handle_comma_assigns(expr
))
358 __split_expr(expr
->right
);
359 if (outside_of_function())
360 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
362 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
364 __fake_struct_member_assignments(expr
);
366 tmp
= strip_expr(expr
->right
);
367 if (expr
->op
== '=' && tmp
->type
== EXPR_CALL
) {
368 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
369 if (!is_fake_call(tmp
))
370 __pass_to_client(tmp
, FUNCTION_CALL_HOOK_AFTER
);
372 if (get_macro_name(tmp
->pos
) &&
373 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
374 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
375 __split_expr(expr
->left
);
379 set_parent(expr
->deref
, expr
);
381 __pass_to_client(expr
, DEREF_HOOK
);
382 __split_expr(expr
->deref
);
385 set_parent(expr
->base
, expr
);
387 __split_expr(expr
->base
);
390 case EXPR_FORCE_CAST
:
391 set_parent(expr
->cast_expression
, expr
);
393 __pass_to_client(expr
, CAST_HOOK
);
394 __split_expr(expr
->cast_expression
);
397 if (expr
->cast_expression
)
398 __pass_to_client(strip_parens(expr
->cast_expression
),
403 evaluate_expression(expr
);
405 case EXPR_CONDITIONAL
:
407 set_parent(expr
->conditional
, expr
);
408 set_parent(expr
->cond_true
, expr
);
409 set_parent(expr
->cond_false
, expr
);
411 if (known_condition_true(expr
->conditional
)) {
412 __split_expr(expr
->cond_true
);
415 if (known_condition_false(expr
->conditional
)) {
416 __split_expr(expr
->cond_false
);
419 __pass_to_client(expr
, SELECT_HOOK
);
420 __split_whole_condition(expr
->conditional
);
421 __split_expr(expr
->cond_true
);
422 __push_true_states();
423 __use_false_states();
424 __split_expr(expr
->cond_false
);
425 __merge_true_states();
428 set_parent(expr
->fn
, expr
);
430 if (sym_name_is("__builtin_constant_p", expr
->fn
))
432 split_expr_list(expr
->args
, expr
);
433 __split_expr(expr
->fn
);
434 if (is_inline_func(expr
->fn
))
435 add_inline_function(expr
->fn
->symbol
);
436 if (inlinable(expr
->fn
))
438 __process_post_op_stack();
439 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
441 if (inlinable(expr
->fn
)) {
444 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
445 if (!is_assigned_call(expr
))
446 __pass_to_client(expr
, FUNCTION_CALL_HOOK_AFTER
);
447 if (is_noreturn_func(expr
->fn
))
450 case EXPR_INITIALIZER
:
451 split_expr_list(expr
->expr_list
, expr
);
453 case EXPR_IDENTIFIER
:
454 set_parent(expr
->ident_expression
, expr
);
455 __split_expr(expr
->ident_expression
);
458 set_parent(expr
->idx_expression
, expr
);
459 __split_expr(expr
->idx_expression
);
462 set_parent(expr
->init_expr
, expr
);
463 __split_expr(expr
->init_expr
);
466 __pass_to_client(expr
, SYM_HOOK
);
469 __pass_to_client(expr
, STRING_HOOK
);
474 pop_expression(&big_expression_stack
);
477 static int is_forever_loop(struct statement
*stmt
)
479 struct expression
*expr
;
481 expr
= strip_expr(stmt
->iterator_pre_condition
);
483 expr
= stmt
->iterator_post_condition
;
485 /* this is a for(;;) loop... */
489 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
496 static char *get_loop_name(int num
)
500 snprintf(buf
, 255, "-loop%d", num
);
502 return alloc_sname(buf
);
506 * Pre Loops are while and for loops.
508 static void handle_pre_loop(struct statement
*stmt
)
510 int once_through
; /* we go through the loop at least once */
511 struct sm_state
*extra_sm
= NULL
;
514 struct stree
*stree
= NULL
;
515 struct sm_state
*sm
= NULL
;
517 loop_name
= get_loop_name(loop_num
);
520 __split_stmt(stmt
->iterator_pre_statement
);
521 __prev_stmt
= stmt
->iterator_pre_statement
;
523 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
529 __merge_gotos(loop_name
);
531 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
532 __in_pre_condition
++;
533 __pass_to_client(stmt
, PRELOOP_HOOK
);
534 __split_whole_condition(stmt
->iterator_pre_condition
);
535 __in_pre_condition
--;
536 FOR_EACH_SM(stree
, sm
) {
537 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
538 } END_FOR_EACH_SM(sm
);
541 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
543 if (option_assume_loops
)
546 __split_stmt(stmt
->iterator_statement
);
547 if (is_forever_loop(stmt
)) {
549 __save_gotos(loop_name
);
551 __push_fake_cur_stree();
552 __split_stmt(stmt
->iterator_post_statement
);
553 stree
= __pop_fake_cur_stree();
555 __discard_false_states();
558 if (!__path_is_null())
559 __merge_stree_into_cur(stree
);
563 unchanged
= __iterator_unchanged(extra_sm
);
564 __split_stmt(stmt
->iterator_post_statement
);
565 __prev_stmt
= stmt
->iterator_post_statement
;
568 __save_gotos(loop_name
);
569 __in_pre_condition
++;
570 __split_whole_condition(stmt
->iterator_pre_condition
);
571 __in_pre_condition
--;
573 __merge_false_states();
575 __discard_false_states();
577 __merge_false_states();
579 if (extra_sm
&& unchanged
)
580 __extra_pre_loop_hook_after(extra_sm
,
581 stmt
->iterator_post_statement
,
582 stmt
->iterator_pre_condition
);
589 * Post loops are do {} while();
591 static void handle_post_loop(struct statement
*stmt
)
595 loop_name
= get_loop_name(loop_num
);
601 __merge_gotos(loop_name
);
602 __split_stmt(stmt
->iterator_statement
);
604 if (!is_zero(stmt
->iterator_post_condition
))
605 __save_gotos(loop_name
);
607 if (is_forever_loop(stmt
)) {
610 __split_whole_condition(stmt
->iterator_post_condition
);
611 __use_false_states();
617 static int empty_statement(struct statement
*stmt
)
621 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
626 static int last_stmt_on_same_line(void)
628 struct statement
*stmt
;
631 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
634 if (stmt
->pos
.line
== get_lineno())
637 } END_FOR_EACH_PTR_REVERSE(stmt
);
641 static void split_asm_constraints(struct expression_list
*expr_list
)
643 struct expression
*expr
;
646 FOR_EACH_PTR(expr_list
, expr
) {
648 case 0: /* identifier */
649 case 1: /* constraint */
652 case 2: /* expression */
657 } END_FOR_EACH_PTR(expr
);
660 static int is_case_val(struct statement
*stmt
, sval_t sval
)
664 if (stmt
->type
!= STMT_CASE
)
666 if (!stmt
->case_expression
) {
670 if (!get_value(stmt
->case_expression
, &case_sval
))
672 if (case_sval
.value
== sval
.value
)
677 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
679 struct statement
*tmp
;
681 __split_expr(stmt
->switch_expression
);
683 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
684 __save_switch_states(top_expression(switch_expr_stack
));
689 stmt
= stmt
->switch_statement
;
691 __push_scope_hooks();
692 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
693 __smatch_lineno
= tmp
->pos
.line
;
694 if (is_case_val(tmp
, sval
)) {
695 __merge_switches(top_expression(switch_expr_stack
),
696 stmt
->case_expression
);
697 __pass_case_to_client(top_expression(switch_expr_stack
),
698 stmt
->case_expression
);
700 if (__path_is_null())
703 if (__path_is_null()) {
707 } END_FOR_EACH_PTR(tmp
);
709 __call_scope_hooks();
710 if (!__pop_default())
711 __merge_switches(top_expression(switch_expr_stack
),
713 __discard_switches();
715 pop_expression(&switch_expr_stack
);
718 static int taking_too_long(void)
722 ms
= ms_since(&fn_start_time
);
723 if (ms
> 1000 * 60 * 5) /* five minutes */
728 static int is_last_stmt(struct statement
*cur_stmt
)
730 struct symbol
*fn
= get_base_type(cur_func_sym
);
731 struct statement
*stmt
;
737 stmt
= fn
->inline_stmt
;
738 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
740 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
741 if (stmt
&& stmt
->type
== STMT_LABEL
)
742 stmt
= stmt
->label_statement
;
743 if (stmt
== cur_stmt
)
748 static void handle_backward_goto(struct statement
*goto_stmt
)
750 const char *goto_name
, *label_name
;
751 struct statement
*func_stmt
;
752 struct symbol
*base_type
= get_base_type(cur_func_sym
);
753 struct statement
*tmp
;
758 if (last_goto_statement_handled
)
760 last_goto_statement_handled
= 1;
762 if (!goto_stmt
->goto_label
||
763 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
764 !goto_stmt
->goto_label
->ident
)
766 goto_name
= goto_stmt
->goto_label
->ident
->name
;
768 func_stmt
= base_type
->stmt
;
770 func_stmt
= base_type
->inline_stmt
;
773 if (func_stmt
->type
!= STMT_COMPOUND
)
776 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
778 if (tmp
->type
!= STMT_LABEL
)
780 if (!tmp
->label_identifier
||
781 tmp
->label_identifier
->type
!= SYM_LABEL
||
782 !tmp
->label_identifier
->ident
)
784 label_name
= tmp
->label_identifier
->ident
->name
;
785 if (strcmp(goto_name
, label_name
) != 0)
790 } END_FOR_EACH_PTR(tmp
);
793 static void fake_a_return(void)
795 struct symbol
*return_type
;
800 return_type
= get_real_base_type(cur_func_sym
);
801 return_type
= get_real_base_type(return_type
);
802 if (return_type
!= &void_ctype
) {
803 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
807 __pass_to_client(cur_func_sym
, END_FUNC_HOOK
);
808 __pass_to_client(cur_func_sym
, AFTER_FUNC_HOOK
);
811 static void split_compound(struct statement
*stmt
)
813 struct statement
*prev
= NULL
;
814 struct statement
*cur
= NULL
;
815 struct statement
*next
;
817 __push_scope_hooks();
819 FOR_EACH_PTR(stmt
->stmts
, next
) {
820 /* just set them all ahead of time */
821 set_parent_stmt(next
, stmt
);
831 } END_FOR_EACH_PTR(next
);
839 __call_scope_hooks();
842 void __split_stmt(struct statement
*stmt
)
849 if (__bail_on_rest_of_function
|| out_of_memory() || taking_too_long()) {
850 static char *printed
= NULL
;
852 __bail_on_rest_of_function
= 1;
853 if (printed
!= cur_func
)
854 sm_msg("Function too hairy. Giving up.");
856 final_pass
= 0; /* turn off sm_msg() from here */
861 add_ptr_list(&big_statement_stack
, stmt
);
862 free_expression_stack(&big_expression_stack
);
863 set_position(stmt
->pos
);
864 __pass_to_client(stmt
, STMT_HOOK
);
866 switch (stmt
->type
) {
867 case STMT_DECLARATION
:
868 split_declaration(stmt
->declaration
);
871 __split_expr(stmt
->ret_value
);
872 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
873 __process_post_op_stack();
876 case STMT_EXPRESSION
:
877 __split_expr(stmt
->expression
);
880 split_compound(stmt
);
883 set_parent_stmt(stmt
->if_true
, stmt
);
884 set_parent_stmt(stmt
->if_false
, stmt
);
886 if (known_condition_true(stmt
->if_conditional
)) {
887 __split_stmt(stmt
->if_true
);
890 if (known_condition_false(stmt
->if_conditional
)) {
891 __split_stmt(stmt
->if_false
);
894 if (option_known_conditions
&&
895 implied_condition_true(stmt
->if_conditional
)) {
896 sm_info("this condition is true.");
897 __split_stmt(stmt
->if_true
);
900 if (option_known_conditions
&&
901 implied_condition_false(stmt
->if_conditional
)) {
902 sm_info("this condition is false.");
903 __split_stmt(stmt
->if_false
);
906 __split_whole_condition(stmt
->if_conditional
);
907 __split_stmt(stmt
->if_true
);
908 if (empty_statement(stmt
->if_true
) &&
909 last_stmt_on_same_line() &&
910 !get_macro_name(stmt
->if_true
->pos
))
911 sm_msg("warn: if();");
912 __push_true_states();
913 __use_false_states();
914 __split_stmt(stmt
->if_false
);
915 __merge_true_states();
918 set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
919 set_parent_stmt(stmt
->iterator_statement
, stmt
);
920 set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
922 if (stmt
->iterator_pre_condition
)
923 handle_pre_loop(stmt
);
924 else if (stmt
->iterator_post_condition
)
925 handle_post_loop(stmt
);
927 // these are for(;;) type loops.
928 handle_pre_loop(stmt
);
932 set_parent_stmt(stmt
->switch_statement
, stmt
);
934 if (get_value(stmt
->switch_expression
, &sval
)) {
935 split_known_switch(stmt
, sval
);
938 __split_expr(stmt
->switch_expression
);
939 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
940 __save_switch_states(top_expression(switch_expr_stack
));
944 __split_stmt(stmt
->switch_statement
);
945 if (!__pop_default())
946 __merge_switches(top_expression(switch_expr_stack
),
948 __discard_switches();
950 pop_expression(&switch_expr_stack
);
953 __merge_switches(top_expression(switch_expr_stack
),
954 stmt
->case_expression
);
955 __pass_case_to_client(top_expression(switch_expr_stack
),
956 stmt
->case_expression
);
957 if (!stmt
->case_expression
)
959 __split_expr(stmt
->case_expression
);
960 __split_expr(stmt
->case_to
);
961 __split_stmt(stmt
->case_statement
);
964 if (stmt
->label_identifier
&&
965 stmt
->label_identifier
->type
== SYM_LABEL
&&
966 stmt
->label_identifier
->ident
) {
967 loop_count
|= 0x80000000;
968 __merge_gotos(stmt
->label_identifier
->ident
->name
);
970 __split_stmt(stmt
->label_statement
);
973 __split_expr(stmt
->goto_expression
);
974 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
975 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
977 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
979 __process_continues();
981 } else if (stmt
->goto_label
&&
982 stmt
->goto_label
->type
== SYM_LABEL
&&
983 stmt
->goto_label
->ident
) {
984 __save_gotos(stmt
->goto_label
->ident
->name
);
987 if (is_last_stmt(stmt
))
988 handle_backward_goto(stmt
);
993 __pass_to_client(stmt
, ASM_HOOK
);
994 __split_expr(stmt
->asm_string
);
995 split_asm_constraints(stmt
->asm_outputs
);
996 split_asm_constraints(stmt
->asm_inputs
);
997 split_asm_constraints(stmt
->asm_clobbers
);
1002 __split_expr(stmt
->range_expression
);
1003 __split_expr(stmt
->range_low
);
1004 __split_expr(stmt
->range_high
);
1007 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1009 __process_post_op_stack();
1012 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1014 struct expression
*expr
;
1016 FOR_EACH_PTR(expr_list
, expr
) {
1017 set_parent(expr
, parent
);
1019 __process_post_op_stack();
1020 } END_FOR_EACH_PTR(expr
);
1023 static void split_sym(struct symbol
*sym
)
1027 if (!(sym
->namespace & NS_SYMBOL
))
1030 __split_stmt(sym
->stmt
);
1031 __split_expr(sym
->array_size
);
1032 split_symlist(sym
->arguments
);
1033 split_symlist(sym
->symbol_list
);
1034 __split_stmt(sym
->inline_stmt
);
1035 split_symlist(sym
->inline_symbol_list
);
1038 static void split_symlist(struct symbol_list
*sym_list
)
1042 FOR_EACH_PTR(sym_list
, sym
) {
1044 } END_FOR_EACH_PTR(sym
);
1047 typedef void (fake_cb
)(struct expression
*expr
);
1049 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1051 struct symbol
*type
, *tmp
;
1057 name
= member
->name
;
1059 type
= get_type(expr
);
1060 if (!type
|| type
->type
!= SYM_STRUCT
)
1064 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1068 if (strcmp(name
, tmp
->ident
->name
) == 0)
1070 } END_FOR_EACH_PTR(tmp
);
1074 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1076 struct symbol
*type
, *member
;
1079 type
= get_type(expr
);
1080 if (!type
|| type
->type
!= SYM_STRUCT
)
1083 FOR_EACH_PTR(type
->symbol_list
, member
) {
1085 return member
->ident
;
1087 } END_FOR_EACH_PTR(member
);
1091 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1094 struct ident
*ident
;
1098 static struct member_set
*alloc_member_set(struct symbol
*type
)
1100 struct member_set
*member_set
;
1101 struct symbol
*member
;
1105 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1106 member_set
= malloc(member_count
* sizeof(*member_set
));
1108 FOR_EACH_PTR(type
->symbol_list
, member
) {
1109 member_set
[member_idx
].ident
= member
->ident
;
1110 member_set
[member_idx
].set
= 0;
1112 } END_FOR_EACH_PTR(member
);
1117 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
1119 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1122 for (i
= 0; i
< member_count
; i
++) {
1123 if (member_set
[i
].ident
== ident
) {
1124 member_set
[i
].set
= 1;
1128 // crap. this is buggy.
1129 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1132 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1134 struct expression
*edge_member
, *assign
;
1135 struct symbol
*base
= get_real_base_type(member
);
1139 expr
= member_expression(expr
, '.', member
->ident
);
1141 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1142 struct symbol
*type
;
1144 type
= get_real_base_type(tmp
);
1149 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1150 if (get_state_expr(SMATCH_EXTRA
, edge_member
))
1154 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1155 set_inner_struct_members(expr
, tmp
);
1162 assign
= assign_expression(edge_member
, zero_expr());
1163 __split_expr(assign
);
1164 } END_FOR_EACH_PTR(tmp
);
1169 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1172 struct expression
*member
, *assign
;
1175 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1176 expr
= strip_expr(expr
->unop
);
1180 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1181 type
= get_real_base_type(tmp
);
1186 member
= member_expression(expr
, op
, tmp
->ident
);
1187 if (get_state_expr(SMATCH_EXTRA
, member
))
1191 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1192 set_inner_struct_members(expr
, tmp
);
1195 if (type
->type
== SYM_ARRAY
)
1200 assign
= assign_expression(member
, zero_expr());
1201 __split_expr(assign
);
1202 } END_FOR_EACH_PTR(tmp
);
1205 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1207 struct expression
*deref
, *assign
, *tmp
;
1208 struct symbol
*struct_type
, *type
;
1209 struct ident
*member
;
1211 struct member_set
*member_set
;
1213 struct_type
= get_type(symbol
);
1215 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1218 member_set
= alloc_member_set(struct_type
);
1221 FOR_EACH_PTR(members
, tmp
) {
1222 member
= number_to_member(symbol
, member_idx
);
1223 while (tmp
->type
== EXPR_IDENTIFIER
) {
1224 member
= tmp
->expr_ident
;
1225 member_idx
= member_to_number(symbol
, member
);
1226 tmp
= tmp
->ident_expression
;
1228 mark_member_as_set(struct_type
, member_set
, member
);
1230 deref
= member_expression(symbol
, '.', member
);
1231 if (tmp
->type
== EXPR_INITIALIZER
) {
1232 type
= get_type(deref
);
1233 if (type
&& type
->type
== SYM_ARRAY
)
1234 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1236 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1238 assign
= assign_expression(deref
, tmp
);
1241 } END_FOR_EACH_PTR(tmp
);
1243 set_unset_to_zero(struct_type
, symbol
);
1246 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1248 fake_member_assigns_helper(symbol_expression(sym
),
1249 sym
->initializer
->expr_list
, fake_cb
);
1252 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1254 struct expression
*offset
, *binop
, *assign
, *tmp
;
1255 struct symbol
*type
;
1258 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1262 FOR_EACH_PTR(expr_list
, tmp
) {
1263 if (tmp
->type
== EXPR_INDEX
) {
1264 if (tmp
->idx_from
!= tmp
->idx_to
)
1266 idx
= tmp
->idx_from
;
1267 if (!tmp
->idx_expression
)
1269 tmp
= tmp
->idx_expression
;
1271 offset
= value_expr(idx
);
1272 binop
= array_element_expression(array
, offset
);
1273 if (tmp
->type
== EXPR_INITIALIZER
) {
1274 type
= get_type(binop
);
1275 if (type
&& type
->type
== SYM_ARRAY
)
1276 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1278 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1280 assign
= assign_expression(binop
, tmp
);
1285 } END_FOR_EACH_PTR(tmp
);
1288 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1290 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1293 static void fake_assign_expr(struct symbol
*sym
)
1295 struct expression
*assign
, *symbol
;
1297 symbol
= symbol_expression(sym
);
1298 assign
= assign_expression(symbol
, sym
->initializer
);
1299 __split_expr(assign
);
1302 static void call_split_expr(struct expression
*expr
)
1307 static void do_initializer_stuff(struct symbol
*sym
)
1309 if (!sym
->initializer
)
1312 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1313 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1314 fake_element_assigns(sym
, call_split_expr
);
1316 fake_member_assigns(sym
, call_split_expr
);
1318 fake_assign_expr(sym
);
1322 static void split_declaration(struct symbol_list
*sym_list
)
1326 FOR_EACH_PTR(sym_list
, sym
) {
1327 __pass_to_client(sym
, DECLARATION_HOOK
);
1328 do_initializer_stuff(sym
);
1330 } END_FOR_EACH_PTR(sym
);
1333 static void call_global_assign_hooks(struct expression
*assign
)
1335 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1338 static void fake_global_assign(struct symbol
*sym
)
1340 struct expression
*assign
, *symbol
;
1342 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1343 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1344 fake_element_assigns(sym
, call_global_assign_hooks
);
1345 } else if (sym
->initializer
) {
1346 symbol
= symbol_expression(sym
);
1347 assign
= assign_expression(symbol
, sym
->initializer
);
1348 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1350 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1352 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1353 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1354 fake_member_assigns(sym
, call_global_assign_hooks
);
1355 } else if (sym
->initializer
) {
1356 symbol
= symbol_expression(sym
);
1357 assign
= assign_expression(symbol
, sym
->initializer
);
1358 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1360 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1363 symbol
= symbol_expression(sym
);
1364 if (sym
->initializer
)
1365 assign
= assign_expression(symbol
, sym
->initializer
);
1367 assign
= assign_expression(symbol
, zero_expr());
1368 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1372 static void start_function_definition(struct symbol
*sym
)
1374 __in_function_def
= 1;
1375 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1376 __in_function_def
= 0;
1377 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1381 static void split_function(struct symbol
*sym
)
1383 struct symbol
*base_type
= get_base_type(sym
);
1385 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1388 gettimeofday(&fn_start_time
, NULL
);
1391 cur_func
= sym
->ident
->name
;
1392 __smatch_lineno
= sym
->pos
.line
;
1394 last_goto_statement_handled
= 0;
1395 sm_debug("new function: %s\n", cur_func
);
1397 if (option_two_passes
) {
1401 start_function_definition(sym
);
1402 __split_stmt(base_type
->stmt
);
1403 __split_stmt(base_type
->inline_stmt
);
1409 start_function_definition(sym
);
1410 __split_stmt(base_type
->stmt
);
1411 __split_stmt(base_type
->inline_stmt
);
1412 __pass_to_client(sym
, END_FUNC_HOOK
);
1413 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1416 cur_func_sym
= NULL
;
1418 free_data_info_allocs();
1419 free_expression_stack(&switch_expr_stack
);
1420 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1421 __bail_on_rest_of_function
= 0;
1424 static void parse_inline(struct expression
*call
)
1426 struct symbol
*base_type
;
1427 int loop_num_bak
= loop_num
;
1428 int final_pass_bak
= final_pass
;
1429 char *cur_func_bak
= cur_func
;
1430 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1431 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1432 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1433 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1435 __pass_to_client(call
, INLINE_FN_START
);
1436 final_pass
= 0; /* don't print anything */
1439 base_type
= get_base_type(call
->fn
->symbol
);
1440 cur_func_sym
= call
->fn
->symbol
;
1441 if (call
->fn
->symbol
->ident
)
1442 cur_func
= call
->fn
->symbol
->ident
->name
;
1445 set_position(call
->fn
->symbol
->pos
);
1448 big_statement_stack
= NULL
;
1449 big_expression_stack
= NULL
;
1450 switch_expr_stack
= NULL
;
1452 sm_debug("inline function: %s\n", cur_func
);
1455 start_function_definition(call
->fn
->symbol
);
1456 __split_stmt(base_type
->stmt
);
1457 __split_stmt(base_type
->inline_stmt
);
1458 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1459 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1461 free_expression_stack(&switch_expr_stack
);
1462 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1466 loop_num
= loop_num_bak
;
1467 final_pass
= final_pass_bak
;
1468 cur_func_sym
= cur_func_sym_bak
;
1469 cur_func
= cur_func_bak
;
1470 big_statement_stack
= big_statement_stack_bak
;
1471 big_expression_stack
= big_expression_stack_bak
;
1472 switch_expr_stack
= switch_expr_stack_bak
;
1474 restore_all_states();
1475 set_position(call
->pos
);
1477 __pass_to_client(call
, INLINE_FN_END
);
1480 static struct symbol_list
*inlines_called
;
1481 static void add_inline_function(struct symbol
*sym
)
1483 static struct symbol_list
*already_added
;
1486 FOR_EACH_PTR(already_added
, tmp
) {
1489 } END_FOR_EACH_PTR(tmp
);
1491 add_ptr_list(&already_added
, sym
);
1492 add_ptr_list(&inlines_called
, sym
);
1495 static void process_inlines(void)
1499 FOR_EACH_PTR(inlines_called
, tmp
) {
1500 split_function(tmp
);
1501 } END_FOR_EACH_PTR(tmp
);
1502 free_ptr_list(&inlines_called
);
1505 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1509 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1512 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1514 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1516 } END_FOR_EACH_PTR_REVERSE(sym
);
1521 static void split_inlines_in_scope(struct symbol
*sym
)
1523 struct symbol
*base
;
1524 struct symbol_list
*scope_list
;
1527 scope_list
= sym
->scope
->symbols
;
1528 stream
= sym
->pos
.stream
;
1530 /* find the last static symbol in the file */
1531 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1532 if (sym
->pos
.stream
!= stream
)
1534 if (sym
->type
!= SYM_NODE
)
1536 base
= get_base_type(sym
);
1539 if (base
->type
!= SYM_FN
)
1541 if (!base
->inline_stmt
)
1543 add_inline_function(sym
);
1544 } END_FOR_EACH_PTR_REVERSE(sym
);
1549 static void split_inlines(struct symbol_list
*sym_list
)
1553 sym
= get_last_scoped_symbol(sym_list
, 0);
1555 split_inlines_in_scope(sym
);
1556 sym
= get_last_scoped_symbol(sym_list
, 1);
1558 split_inlines_in_scope(sym
);
1561 static struct stree
*clone_estates_perm(struct stree
*orig
)
1563 struct stree
*ret
= NULL
;
1564 struct sm_state
*tmp
;
1566 FOR_EACH_SM(orig
, tmp
) {
1567 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1568 } END_FOR_EACH_SM(tmp
);
1573 static void split_functions(struct symbol_list
*sym_list
)
1578 FOR_EACH_PTR(sym_list
, sym
) {
1579 set_position(sym
->pos
);
1580 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1581 __pass_to_client(sym
, BASE_HOOK
);
1582 fake_global_assign(sym
);
1584 } END_FOR_EACH_PTR(sym
);
1585 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1588 FOR_EACH_PTR(sym_list
, sym
) {
1589 set_position(sym
->pos
);
1590 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1591 split_function(sym
);
1594 } END_FOR_EACH_PTR(sym
);
1595 split_inlines(sym_list
);
1596 __pass_to_client(sym_list
, END_FILE_HOOK
);
1599 void smatch(int argc
, char **argv
)
1602 struct string_list
*filelist
= NULL
;
1603 struct symbol_list
*sym_list
;
1606 printf("Usage: smatch [--debug] <filename.c>\n");
1609 sparse_initialize(argc
, argv
, &filelist
);
1610 set_valid_ptr_max();
1611 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1612 if (option_file_output
) {
1615 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1616 sm_outfd
= fopen(buf
, "w");
1618 printf("Error: Cannot open %s\n", base_file
);
1622 sym_list
= sparse_keep_tokens(base_file
);
1623 split_functions(sym_list
);
1624 } END_FOR_EACH_PTR_NOTAG(base_file
);