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 void set_parent_expr(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 /* This is to handle *p++ = foo; assignments */
254 static int handle_postop_assigns(struct expression
*expr
)
256 struct expression
*left
, *fake_left
;
257 struct expression
*assign
;
259 left
= strip_expr(expr
->left
);
260 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
262 left
= strip_expr(left
->unop
);
263 if (left
->type
!= EXPR_POSTOP
)
266 fake_left
= deref_expression(strip_expr(left
->unop
));
267 assign
= assign_expression(fake_left
, expr
->right
);
269 __split_expr(assign
);
270 __split_expr(expr
->left
);
275 static int prev_expression_is_getting_address(struct expression
*expr
)
277 struct expression
*parent
;
280 parent
= expr
->parent
;
284 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
286 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
288 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
297 void __split_expr(struct expression
*expr
)
302 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
304 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
306 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
309 push_expression(&big_expression_stack
, expr
);
310 set_position(expr
->pos
);
311 __pass_to_client(expr
, EXPR_HOOK
);
313 switch (expr
->type
) {
315 set_parent_expr(expr
->unop
, expr
);
317 if (expr
->op
== '*' &&
318 !prev_expression_is_getting_address(expr
))
319 __pass_to_client(expr
, DEREF_HOOK
);
320 __split_expr(expr
->unop
);
321 __pass_to_client(expr
, OP_HOOK
);
324 set_parent_expr(expr
->unop
, expr
);
326 __split_expr(expr
->unop
);
327 push_expression(&post_op_stack
, expr
);
331 __split_stmt(expr
->statement
);
336 set_parent_expr(expr
->left
, expr
);
337 set_parent_expr(expr
->right
, expr
);
339 __pass_to_client(expr
, LOGIC_HOOK
);
340 __handle_logic(expr
);
343 set_parent_expr(expr
->left
, expr
);
344 set_parent_expr(expr
->right
, expr
);
346 __pass_to_client(expr
, BINOP_HOOK
);
348 set_parent_expr(expr
->left
, expr
);
349 set_parent_expr(expr
->right
, expr
);
351 __split_expr(expr
->left
);
352 __process_post_op_stack();
353 __split_expr(expr
->right
);
355 case EXPR_ASSIGNMENT
: {
356 struct expression
*tmp
;
358 set_parent_expr(expr
->left
, expr
);
359 set_parent_expr(expr
->right
, expr
);
364 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
367 if (__handle_condition_assigns(expr
))
369 /* foo = (x < 5 ? foo : 5); */
370 if (__handle_select_assigns(expr
))
372 /* foo = ({frob(); frob(); frob(); 1;}) */
373 if (__handle_expr_statement_assigns(expr
))
376 if (handle_comma_assigns(expr
))
378 if (handle_postop_assigns(expr
))
381 __split_expr(expr
->right
);
382 if (outside_of_function())
383 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
385 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
387 __fake_struct_member_assignments(expr
);
389 tmp
= strip_expr(expr
->right
);
390 if (expr
->op
== '=' && tmp
->type
== EXPR_CALL
) {
391 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
393 if (get_macro_name(tmp
->pos
) &&
394 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
395 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
396 __split_expr(expr
->left
);
400 set_parent_expr(expr
->deref
, expr
);
402 __pass_to_client(expr
, DEREF_HOOK
);
403 __split_expr(expr
->deref
);
406 set_parent_expr(expr
->base
, expr
);
408 __split_expr(expr
->base
);
411 case EXPR_FORCE_CAST
:
412 set_parent_expr(expr
->cast_expression
, expr
);
414 __pass_to_client(expr
, CAST_HOOK
);
415 __split_expr(expr
->cast_expression
);
418 if (expr
->cast_expression
)
419 __pass_to_client(strip_parens(expr
->cast_expression
),
424 evaluate_expression(expr
);
426 case EXPR_CONDITIONAL
:
428 set_parent_expr(expr
->conditional
, expr
);
429 set_parent_expr(expr
->cond_true
, expr
);
430 set_parent_expr(expr
->cond_false
, expr
);
432 if (known_condition_true(expr
->conditional
)) {
433 __split_expr(expr
->cond_true
);
436 if (known_condition_false(expr
->conditional
)) {
437 __split_expr(expr
->cond_false
);
440 __pass_to_client(expr
, SELECT_HOOK
);
441 __split_whole_condition(expr
->conditional
);
442 __split_expr(expr
->cond_true
);
443 __push_true_states();
444 __use_false_states();
445 __split_expr(expr
->cond_false
);
446 __merge_true_states();
449 set_parent_expr(expr
->fn
, expr
);
451 if (sym_name_is("__builtin_constant_p", expr
->fn
))
453 split_expr_list(expr
->args
, expr
);
454 __split_expr(expr
->fn
);
455 if (is_inline_func(expr
->fn
))
456 add_inline_function(expr
->fn
->symbol
);
457 if (inlinable(expr
->fn
))
459 __process_post_op_stack();
460 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
462 if (inlinable(expr
->fn
)) {
465 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
466 if (is_noreturn_func(expr
->fn
))
469 case EXPR_INITIALIZER
:
470 split_expr_list(expr
->expr_list
, expr
);
472 case EXPR_IDENTIFIER
:
473 set_parent_expr(expr
->ident_expression
, expr
);
474 __split_expr(expr
->ident_expression
);
477 set_parent_expr(expr
->idx_expression
, expr
);
478 __split_expr(expr
->idx_expression
);
481 set_parent_expr(expr
->init_expr
, expr
);
482 __split_expr(expr
->init_expr
);
485 __pass_to_client(expr
, SYM_HOOK
);
488 __pass_to_client(expr
, STRING_HOOK
);
493 pop_expression(&big_expression_stack
);
496 static int is_forever_loop(struct statement
*stmt
)
498 struct expression
*expr
;
500 expr
= strip_expr(stmt
->iterator_pre_condition
);
502 expr
= stmt
->iterator_post_condition
;
504 /* this is a for(;;) loop... */
508 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
515 static char *get_loop_name(int num
)
519 snprintf(buf
, 255, "-loop%d", num
);
521 return alloc_sname(buf
);
525 * Pre Loops are while and for loops.
527 static void handle_pre_loop(struct statement
*stmt
)
529 int once_through
; /* we go through the loop at least once */
530 struct sm_state
*extra_sm
= NULL
;
533 struct stree
*stree
= NULL
;
534 struct sm_state
*sm
= NULL
;
536 loop_name
= get_loop_name(loop_num
);
539 __split_stmt(stmt
->iterator_pre_statement
);
540 __prev_stmt
= stmt
->iterator_pre_statement
;
542 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
548 __merge_gotos(loop_name
);
550 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
551 __in_pre_condition
++;
552 __pass_to_client(stmt
, PRELOOP_HOOK
);
553 __split_whole_condition(stmt
->iterator_pre_condition
);
554 __in_pre_condition
--;
555 FOR_EACH_SM(stree
, sm
) {
556 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
557 } END_FOR_EACH_SM(sm
);
560 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
562 if (option_assume_loops
)
565 __split_stmt(stmt
->iterator_statement
);
566 if (is_forever_loop(stmt
)) {
568 __save_gotos(loop_name
);
570 __push_fake_cur_stree();
571 __split_stmt(stmt
->iterator_post_statement
);
572 stree
= __pop_fake_cur_stree();
574 __discard_false_states();
577 if (!__path_is_null())
578 __merge_stree_into_cur(stree
);
582 unchanged
= __iterator_unchanged(extra_sm
);
583 __split_stmt(stmt
->iterator_post_statement
);
584 __prev_stmt
= stmt
->iterator_post_statement
;
587 __save_gotos(loop_name
);
588 __in_pre_condition
++;
589 __split_whole_condition(stmt
->iterator_pre_condition
);
590 __in_pre_condition
--;
592 __merge_false_states();
594 __discard_false_states();
596 __merge_false_states();
598 if (extra_sm
&& unchanged
)
599 __extra_pre_loop_hook_after(extra_sm
,
600 stmt
->iterator_post_statement
,
601 stmt
->iterator_pre_condition
);
608 * Post loops are do {} while();
610 static void handle_post_loop(struct statement
*stmt
)
614 loop_name
= get_loop_name(loop_num
);
620 __merge_gotos(loop_name
);
621 __split_stmt(stmt
->iterator_statement
);
623 if (!is_zero(stmt
->iterator_post_condition
))
624 __save_gotos(loop_name
);
626 if (is_forever_loop(stmt
)) {
629 __split_whole_condition(stmt
->iterator_post_condition
);
630 __use_false_states();
636 static int empty_statement(struct statement
*stmt
)
640 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
645 static int last_stmt_on_same_line(void)
647 struct statement
*stmt
;
650 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
653 if (stmt
->pos
.line
== get_lineno())
656 } END_FOR_EACH_PTR_REVERSE(stmt
);
660 static void split_asm_constraints(struct expression_list
*expr_list
)
662 struct expression
*expr
;
665 FOR_EACH_PTR(expr_list
, expr
) {
667 case 0: /* identifier */
668 case 1: /* constraint */
671 case 2: /* expression */
676 } END_FOR_EACH_PTR(expr
);
679 static int is_case_val(struct statement
*stmt
, sval_t sval
)
683 if (stmt
->type
!= STMT_CASE
)
685 if (!stmt
->case_expression
) {
689 if (!get_value(stmt
->case_expression
, &case_sval
))
691 if (case_sval
.value
== sval
.value
)
696 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
698 struct statement
*tmp
;
700 __split_expr(stmt
->switch_expression
);
702 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
703 __save_switch_states(top_expression(switch_expr_stack
));
708 stmt
= stmt
->switch_statement
;
710 __push_scope_hooks();
711 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
712 __smatch_lineno
= tmp
->pos
.line
;
713 if (is_case_val(tmp
, sval
)) {
714 __merge_switches(top_expression(switch_expr_stack
),
715 stmt
->case_expression
, stmt
->case_to
);
716 __pass_case_to_client(top_expression(switch_expr_stack
),
717 stmt
->case_expression
);
719 if (__path_is_null())
722 if (__path_is_null()) {
726 } END_FOR_EACH_PTR(tmp
);
728 __call_scope_hooks();
729 if (!__pop_default())
730 __merge_switches(top_expression(switch_expr_stack
),
732 __discard_switches();
734 pop_expression(&switch_expr_stack
);
737 static int taking_too_long(void)
741 ms
= ms_since(&fn_start_time
);
742 if (ms
> 1000 * 60 * 5) /* five minutes */
747 static int is_last_stmt(struct statement
*cur_stmt
)
749 struct symbol
*fn
= get_base_type(cur_func_sym
);
750 struct statement
*stmt
;
756 stmt
= fn
->inline_stmt
;
757 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
759 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
760 if (stmt
&& stmt
->type
== STMT_LABEL
)
761 stmt
= stmt
->label_statement
;
762 if (stmt
== cur_stmt
)
767 static void handle_backward_goto(struct statement
*goto_stmt
)
769 const char *goto_name
, *label_name
;
770 struct statement
*func_stmt
;
771 struct symbol
*base_type
= get_base_type(cur_func_sym
);
772 struct statement
*tmp
;
777 if (last_goto_statement_handled
)
779 last_goto_statement_handled
= 1;
781 if (!goto_stmt
->goto_label
||
782 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
783 !goto_stmt
->goto_label
->ident
)
785 goto_name
= goto_stmt
->goto_label
->ident
->name
;
787 func_stmt
= base_type
->stmt
;
789 func_stmt
= base_type
->inline_stmt
;
792 if (func_stmt
->type
!= STMT_COMPOUND
)
795 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
797 if (tmp
->type
!= STMT_LABEL
)
799 if (!tmp
->label_identifier
||
800 tmp
->label_identifier
->type
!= SYM_LABEL
||
801 !tmp
->label_identifier
->ident
)
803 label_name
= tmp
->label_identifier
->ident
->name
;
804 if (strcmp(goto_name
, label_name
) != 0)
809 } END_FOR_EACH_PTR(tmp
);
812 static void fake_a_return(void)
814 struct symbol
*return_type
;
819 return_type
= get_real_base_type(cur_func_sym
);
820 return_type
= get_real_base_type(return_type
);
821 if (return_type
!= &void_ctype
) {
822 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
827 static void split_compound(struct statement
*stmt
)
829 struct statement
*prev
= NULL
;
830 struct statement
*cur
= NULL
;
831 struct statement
*next
;
833 __push_scope_hooks();
835 FOR_EACH_PTR(stmt
->stmts
, next
) {
836 /* just set them all ahead of time */
837 set_parent_stmt(next
, stmt
);
847 } END_FOR_EACH_PTR(next
);
855 __call_scope_hooks();
858 void __split_label_stmt(struct statement
*stmt
)
860 if (stmt
->label_identifier
&&
861 stmt
->label_identifier
->type
== SYM_LABEL
&&
862 stmt
->label_identifier
->ident
) {
863 loop_count
|= 0x80000000;
864 __merge_gotos(stmt
->label_identifier
->ident
->name
);
868 void __split_stmt(struct statement
*stmt
)
875 if (__bail_on_rest_of_function
)
878 if (out_of_memory() || taking_too_long()) {
880 __bail_on_rest_of_function
= 1;
881 sm_msg("Function too hairy. Giving up.");
883 final_pass
= 0; /* turn off sm_msg() from here */
887 add_ptr_list(&big_statement_stack
, stmt
);
888 free_expression_stack(&big_expression_stack
);
889 set_position(stmt
->pos
);
890 __pass_to_client(stmt
, STMT_HOOK
);
892 switch (stmt
->type
) {
893 case STMT_DECLARATION
:
894 split_declaration(stmt
->declaration
);
897 __split_expr(stmt
->ret_value
);
898 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
899 __process_post_op_stack();
902 case STMT_EXPRESSION
:
903 __split_expr(stmt
->expression
);
906 split_compound(stmt
);
909 set_parent_stmt(stmt
->if_true
, stmt
);
910 set_parent_stmt(stmt
->if_false
, stmt
);
912 if (known_condition_true(stmt
->if_conditional
)) {
913 __split_stmt(stmt
->if_true
);
916 if (known_condition_false(stmt
->if_conditional
)) {
917 __split_stmt(stmt
->if_false
);
920 __split_whole_condition(stmt
->if_conditional
);
921 __split_stmt(stmt
->if_true
);
922 if (empty_statement(stmt
->if_true
) &&
923 last_stmt_on_same_line() &&
924 !get_macro_name(stmt
->if_true
->pos
))
925 sm_msg("warn: if();");
926 __push_true_states();
927 __use_false_states();
928 __split_stmt(stmt
->if_false
);
929 __merge_true_states();
932 set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
933 set_parent_stmt(stmt
->iterator_statement
, stmt
);
934 set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
936 if (stmt
->iterator_pre_condition
)
937 handle_pre_loop(stmt
);
938 else if (stmt
->iterator_post_condition
)
939 handle_post_loop(stmt
);
941 // these are for(;;) type loops.
942 handle_pre_loop(stmt
);
946 set_parent_stmt(stmt
->switch_statement
, stmt
);
948 if (get_value(stmt
->switch_expression
, &sval
)) {
949 split_known_switch(stmt
, sval
);
952 __split_expr(stmt
->switch_expression
);
953 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
954 __save_switch_states(top_expression(switch_expr_stack
));
958 __split_stmt(stmt
->switch_statement
);
959 if (!__pop_default())
960 __merge_switches(top_expression(switch_expr_stack
),
962 __discard_switches();
964 pop_expression(&switch_expr_stack
);
967 __merge_switches(top_expression(switch_expr_stack
),
968 stmt
->case_expression
, stmt
->case_to
);
969 __pass_case_to_client(top_expression(switch_expr_stack
),
970 stmt
->case_expression
);
971 if (!stmt
->case_expression
)
973 __split_expr(stmt
->case_expression
);
974 __split_expr(stmt
->case_to
);
975 __split_stmt(stmt
->case_statement
);
978 __split_label_stmt(stmt
);
979 __split_stmt(stmt
->label_statement
);
982 __split_expr(stmt
->goto_expression
);
983 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
984 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
986 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
988 __process_continues();
990 } else if (stmt
->goto_label
&&
991 stmt
->goto_label
->type
== SYM_LABEL
&&
992 stmt
->goto_label
->ident
) {
993 __save_gotos(stmt
->goto_label
->ident
->name
);
996 if (is_last_stmt(stmt
))
997 handle_backward_goto(stmt
);
1002 __pass_to_client(stmt
, ASM_HOOK
);
1003 __split_expr(stmt
->asm_string
);
1004 split_asm_constraints(stmt
->asm_outputs
);
1005 split_asm_constraints(stmt
->asm_inputs
);
1006 split_asm_constraints(stmt
->asm_clobbers
);
1011 __split_expr(stmt
->range_expression
);
1012 __split_expr(stmt
->range_low
);
1013 __split_expr(stmt
->range_high
);
1016 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1018 __process_post_op_stack();
1021 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1023 struct expression
*expr
;
1025 FOR_EACH_PTR(expr_list
, expr
) {
1026 set_parent_expr(expr
, parent
);
1028 __process_post_op_stack();
1029 } END_FOR_EACH_PTR(expr
);
1032 static void split_sym(struct symbol
*sym
)
1036 if (!(sym
->namespace & NS_SYMBOL
))
1039 __split_stmt(sym
->stmt
);
1040 __split_expr(sym
->array_size
);
1041 split_symlist(sym
->arguments
);
1042 split_symlist(sym
->symbol_list
);
1043 __split_stmt(sym
->inline_stmt
);
1044 split_symlist(sym
->inline_symbol_list
);
1047 static void split_symlist(struct symbol_list
*sym_list
)
1051 FOR_EACH_PTR(sym_list
, sym
) {
1053 } END_FOR_EACH_PTR(sym
);
1056 typedef void (fake_cb
)(struct expression
*expr
);
1058 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1060 struct symbol
*type
, *tmp
;
1066 name
= member
->name
;
1068 type
= get_type(expr
);
1069 if (!type
|| type
->type
!= SYM_STRUCT
)
1073 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1077 if (strcmp(name
, tmp
->ident
->name
) == 0)
1079 } END_FOR_EACH_PTR(tmp
);
1083 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1085 struct symbol
*type
, *member
;
1088 type
= get_type(expr
);
1089 if (!type
|| type
->type
!= SYM_STRUCT
)
1092 FOR_EACH_PTR(type
->symbol_list
, member
) {
1094 return member
->ident
;
1096 } END_FOR_EACH_PTR(member
);
1100 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1103 struct ident
*ident
;
1107 static struct member_set
*alloc_member_set(struct symbol
*type
)
1109 struct member_set
*member_set
;
1110 struct symbol
*member
;
1114 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1115 member_set
= malloc(member_count
* sizeof(*member_set
));
1117 FOR_EACH_PTR(type
->symbol_list
, member
) {
1118 member_set
[member_idx
].ident
= member
->ident
;
1119 member_set
[member_idx
].set
= 0;
1121 } END_FOR_EACH_PTR(member
);
1126 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
1128 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1131 for (i
= 0; i
< member_count
; i
++) {
1132 if (member_set
[i
].ident
== ident
) {
1133 member_set
[i
].set
= 1;
1137 // crap. this is buggy.
1138 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1141 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1143 struct expression
*edge_member
, *assign
;
1144 struct symbol
*base
= get_real_base_type(member
);
1148 expr
= member_expression(expr
, '.', member
->ident
);
1150 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1151 struct symbol
*type
;
1153 type
= get_real_base_type(tmp
);
1158 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1159 if (get_state_expr(SMATCH_EXTRA
, edge_member
))
1163 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1164 set_inner_struct_members(expr
, tmp
);
1171 assign
= assign_expression(edge_member
, zero_expr());
1172 __split_expr(assign
);
1173 } END_FOR_EACH_PTR(tmp
);
1178 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1181 struct expression
*member
= NULL
;
1182 struct expression
*assign
;
1185 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1186 expr
= strip_expr(expr
->unop
);
1190 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1191 type
= get_real_base_type(tmp
);
1196 member
= member_expression(expr
, op
, tmp
->ident
);
1197 if (get_state_expr(SMATCH_EXTRA
, member
))
1201 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1202 set_inner_struct_members(expr
, tmp
);
1205 if (type
->type
== SYM_ARRAY
)
1210 assign
= assign_expression(member
, zero_expr());
1211 __split_expr(assign
);
1212 } END_FOR_EACH_PTR(tmp
);
1215 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1217 struct expression
*deref
, *assign
, *tmp
;
1218 struct symbol
*struct_type
, *type
;
1219 struct ident
*member
;
1221 struct member_set
*member_set
;
1223 struct_type
= get_type(symbol
);
1225 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1228 member_set
= alloc_member_set(struct_type
);
1231 FOR_EACH_PTR(members
, tmp
) {
1232 member
= number_to_member(symbol
, member_idx
);
1233 while (tmp
->type
== EXPR_IDENTIFIER
) {
1234 member
= tmp
->expr_ident
;
1235 member_idx
= member_to_number(symbol
, member
);
1236 tmp
= tmp
->ident_expression
;
1238 mark_member_as_set(struct_type
, member_set
, member
);
1240 deref
= member_expression(symbol
, '.', member
);
1241 if (tmp
->type
== EXPR_INITIALIZER
) {
1242 type
= get_type(deref
);
1243 if (type
&& type
->type
== SYM_ARRAY
)
1244 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1246 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1248 assign
= assign_expression(deref
, tmp
);
1251 } END_FOR_EACH_PTR(tmp
);
1253 set_unset_to_zero(struct_type
, symbol
);
1256 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1258 fake_member_assigns_helper(symbol_expression(sym
),
1259 sym
->initializer
->expr_list
, fake_cb
);
1262 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1264 struct expression
*offset
, *binop
, *assign
, *tmp
;
1265 struct symbol
*type
;
1268 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1272 FOR_EACH_PTR(expr_list
, tmp
) {
1273 if (tmp
->type
== EXPR_INDEX
) {
1274 if (tmp
->idx_from
!= tmp
->idx_to
)
1276 idx
= tmp
->idx_from
;
1277 if (!tmp
->idx_expression
)
1279 tmp
= tmp
->idx_expression
;
1281 offset
= value_expr(idx
);
1282 binop
= array_element_expression(array
, offset
);
1283 if (tmp
->type
== EXPR_INITIALIZER
) {
1284 type
= get_type(binop
);
1285 if (type
&& type
->type
== SYM_ARRAY
)
1286 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1288 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1290 assign
= assign_expression(binop
, tmp
);
1295 } END_FOR_EACH_PTR(tmp
);
1298 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1300 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1303 static void fake_assign_expr(struct symbol
*sym
)
1305 struct expression
*assign
, *symbol
;
1307 symbol
= symbol_expression(sym
);
1308 assign
= assign_expression(symbol
, sym
->initializer
);
1309 __split_expr(assign
);
1312 static void call_split_expr(struct expression
*expr
)
1317 static void do_initializer_stuff(struct symbol
*sym
)
1319 if (!sym
->initializer
)
1322 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1323 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1324 fake_element_assigns(sym
, call_split_expr
);
1326 fake_member_assigns(sym
, call_split_expr
);
1328 fake_assign_expr(sym
);
1332 static void split_declaration(struct symbol_list
*sym_list
)
1336 FOR_EACH_PTR(sym_list
, sym
) {
1337 __pass_to_client(sym
, DECLARATION_HOOK
);
1338 do_initializer_stuff(sym
);
1340 } END_FOR_EACH_PTR(sym
);
1343 static void call_global_assign_hooks(struct expression
*assign
)
1345 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1348 static void fake_global_assign(struct symbol
*sym
)
1350 struct expression
*assign
, *symbol
;
1352 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1353 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1354 fake_element_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_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1362 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1363 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1364 fake_member_assigns(sym
, call_global_assign_hooks
);
1365 } else if (sym
->initializer
) {
1366 symbol
= symbol_expression(sym
);
1367 assign
= assign_expression(symbol
, sym
->initializer
);
1368 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1370 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1373 symbol
= symbol_expression(sym
);
1374 if (sym
->initializer
)
1375 assign
= assign_expression(symbol
, sym
->initializer
);
1377 assign
= assign_expression(symbol
, zero_expr());
1378 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1382 static void start_function_definition(struct symbol
*sym
)
1384 __in_function_def
= 1;
1385 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1386 __in_function_def
= 0;
1387 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1391 static void split_function(struct symbol
*sym
)
1393 struct symbol
*base_type
= get_base_type(sym
);
1395 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1398 gettimeofday(&fn_start_time
, NULL
);
1401 cur_func
= sym
->ident
->name
;
1402 __smatch_lineno
= sym
->pos
.line
;
1404 last_goto_statement_handled
= 0;
1405 sm_debug("new function: %s\n", cur_func
);
1407 if (option_two_passes
) {
1411 start_function_definition(sym
);
1412 __split_stmt(base_type
->stmt
);
1413 __split_stmt(base_type
->inline_stmt
);
1419 start_function_definition(sym
);
1420 __split_stmt(base_type
->stmt
);
1421 __split_stmt(base_type
->inline_stmt
);
1422 __pass_to_client(sym
, END_FUNC_HOOK
);
1423 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1426 cur_func_sym
= NULL
;
1428 free_data_info_allocs();
1429 free_expression_stack(&switch_expr_stack
);
1430 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1431 __bail_on_rest_of_function
= 0;
1434 static void parse_inline(struct expression
*call
)
1436 struct symbol
*base_type
;
1437 int loop_num_bak
= loop_num
;
1438 int loop_count_bak
= loop_count
;
1439 int final_pass_bak
= final_pass
;
1440 char *cur_func_bak
= cur_func
;
1441 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1442 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1443 struct expression_list
*big_condition_stack_bak
= big_condition_stack
;
1444 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1445 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1447 __pass_to_client(call
, INLINE_FN_START
);
1448 final_pass
= 0; /* don't print anything */
1451 base_type
= get_base_type(call
->fn
->symbol
);
1452 cur_func_sym
= call
->fn
->symbol
;
1453 if (call
->fn
->symbol
->ident
)
1454 cur_func
= call
->fn
->symbol
->ident
->name
;
1457 set_position(call
->fn
->symbol
->pos
);
1460 big_statement_stack
= NULL
;
1461 big_expression_stack
= NULL
;
1462 big_condition_stack
= NULL
;
1463 switch_expr_stack
= NULL
;
1465 sm_debug("inline function: %s\n", cur_func
);
1468 start_function_definition(call
->fn
->symbol
);
1469 __split_stmt(base_type
->stmt
);
1470 __split_stmt(base_type
->inline_stmt
);
1471 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1472 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1474 free_expression_stack(&switch_expr_stack
);
1475 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1479 loop_num
= loop_num_bak
;
1480 loop_count
= loop_count_bak
;
1481 final_pass
= final_pass_bak
;
1482 cur_func_sym
= cur_func_sym_bak
;
1483 cur_func
= cur_func_bak
;
1484 big_statement_stack
= big_statement_stack_bak
;
1485 big_expression_stack
= big_expression_stack_bak
;
1486 big_condition_stack
= big_condition_stack_bak
;
1487 switch_expr_stack
= switch_expr_stack_bak
;
1489 restore_all_states();
1490 set_position(call
->pos
);
1492 __pass_to_client(call
, INLINE_FN_END
);
1495 static struct symbol_list
*inlines_called
;
1496 static void add_inline_function(struct symbol
*sym
)
1498 static struct symbol_list
*already_added
;
1501 FOR_EACH_PTR(already_added
, tmp
) {
1504 } END_FOR_EACH_PTR(tmp
);
1506 add_ptr_list(&already_added
, sym
);
1507 add_ptr_list(&inlines_called
, sym
);
1510 static void process_inlines(void)
1514 FOR_EACH_PTR(inlines_called
, tmp
) {
1515 split_function(tmp
);
1516 } END_FOR_EACH_PTR(tmp
);
1517 free_ptr_list(&inlines_called
);
1520 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1524 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1527 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1529 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1531 } END_FOR_EACH_PTR_REVERSE(sym
);
1536 static void split_inlines_in_scope(struct symbol
*sym
)
1538 struct symbol
*base
;
1539 struct symbol_list
*scope_list
;
1542 scope_list
= sym
->scope
->symbols
;
1543 stream
= sym
->pos
.stream
;
1545 /* find the last static symbol in the file */
1546 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1547 if (sym
->pos
.stream
!= stream
)
1549 if (sym
->type
!= SYM_NODE
)
1551 base
= get_base_type(sym
);
1554 if (base
->type
!= SYM_FN
)
1556 if (!base
->inline_stmt
)
1558 add_inline_function(sym
);
1559 } END_FOR_EACH_PTR_REVERSE(sym
);
1564 static void split_inlines(struct symbol_list
*sym_list
)
1568 sym
= get_last_scoped_symbol(sym_list
, 0);
1570 split_inlines_in_scope(sym
);
1571 sym
= get_last_scoped_symbol(sym_list
, 1);
1573 split_inlines_in_scope(sym
);
1576 static struct stree
*clone_estates_perm(struct stree
*orig
)
1578 struct stree
*ret
= NULL
;
1579 struct sm_state
*tmp
;
1581 FOR_EACH_SM(orig
, tmp
) {
1582 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1583 } END_FOR_EACH_SM(tmp
);
1588 struct position last_pos
;
1589 static void split_functions(struct symbol_list
*sym_list
)
1594 FOR_EACH_PTR(sym_list
, sym
) {
1595 set_position(sym
->pos
);
1596 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1597 __pass_to_client(sym
, BASE_HOOK
);
1598 fake_global_assign(sym
);
1600 } END_FOR_EACH_PTR(sym
);
1601 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1604 FOR_EACH_PTR(sym_list
, sym
) {
1605 set_position(sym
->pos
);
1606 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1607 split_function(sym
);
1610 last_pos
= sym
->pos
;
1611 } END_FOR_EACH_PTR(sym
);
1612 split_inlines(sym_list
);
1613 __pass_to_client(sym_list
, END_FILE_HOOK
);
1616 void smatch(int argc
, char **argv
)
1618 struct string_list
*filelist
= NULL
;
1619 struct symbol_list
*sym_list
;
1620 struct timeval stop
, start
;
1622 gettimeofday(&start
, NULL
);
1625 printf("Usage: smatch [--debug] <filename.c>\n");
1628 sparse_initialize(argc
, argv
, &filelist
);
1629 set_valid_ptr_max();
1630 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1631 if (option_file_output
) {
1634 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1635 sm_outfd
= fopen(buf
, "w");
1637 printf("Error: Cannot open %s\n", base_file
);
1641 sym_list
= sparse_keep_tokens(base_file
);
1642 split_functions(sym_list
);
1643 } END_FOR_EACH_PTR_NOTAG(base_file
);
1645 gettimeofday(&stop
, NULL
);
1647 set_position(last_pos
);
1649 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);