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 if (expr
->statement
&& !expr
->statement
->parent
) {
332 set_parent_stmt(expr
->statement
,
333 last_ptr_list((struct ptr_list
*)big_statement_stack
));
335 __split_stmt(expr
->statement
);
340 set_parent_expr(expr
->left
, expr
);
341 set_parent_expr(expr
->right
, expr
);
343 __pass_to_client(expr
, LOGIC_HOOK
);
344 __handle_logic(expr
);
347 set_parent_expr(expr
->left
, expr
);
348 set_parent_expr(expr
->right
, expr
);
350 __pass_to_client(expr
, BINOP_HOOK
);
352 set_parent_expr(expr
->left
, expr
);
353 set_parent_expr(expr
->right
, expr
);
355 __split_expr(expr
->left
);
356 __process_post_op_stack();
357 __split_expr(expr
->right
);
359 case EXPR_ASSIGNMENT
: {
360 struct expression
*tmp
;
362 set_parent_expr(expr
->left
, expr
);
363 set_parent_expr(expr
->right
, expr
);
368 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
371 if (__handle_condition_assigns(expr
))
373 /* foo = (x < 5 ? foo : 5); */
374 if (__handle_select_assigns(expr
))
376 /* foo = ({frob(); frob(); frob(); 1;}) */
377 if (__handle_expr_statement_assigns(expr
))
380 if (handle_comma_assigns(expr
))
382 if (handle_postop_assigns(expr
))
385 __split_expr(expr
->right
);
386 if (outside_of_function())
387 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
389 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
391 __fake_struct_member_assignments(expr
);
393 tmp
= strip_expr(expr
->right
);
394 if (expr
->op
== '=' && tmp
->type
== EXPR_CALL
) {
395 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
397 if (get_macro_name(tmp
->pos
) &&
398 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
399 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
400 __split_expr(expr
->left
);
404 set_parent_expr(expr
->deref
, expr
);
406 __pass_to_client(expr
, DEREF_HOOK
);
407 __split_expr(expr
->deref
);
410 set_parent_expr(expr
->base
, expr
);
412 __split_expr(expr
->base
);
415 case EXPR_FORCE_CAST
:
416 set_parent_expr(expr
->cast_expression
, expr
);
418 __pass_to_client(expr
, CAST_HOOK
);
419 __split_expr(expr
->cast_expression
);
422 if (expr
->cast_expression
)
423 __pass_to_client(strip_parens(expr
->cast_expression
),
428 evaluate_expression(expr
);
430 case EXPR_CONDITIONAL
:
432 set_parent_expr(expr
->conditional
, expr
);
433 set_parent_expr(expr
->cond_true
, expr
);
434 set_parent_expr(expr
->cond_false
, expr
);
436 if (known_condition_true(expr
->conditional
)) {
437 __split_expr(expr
->cond_true
);
440 if (known_condition_false(expr
->conditional
)) {
441 __split_expr(expr
->cond_false
);
444 __pass_to_client(expr
, SELECT_HOOK
);
445 __split_whole_condition(expr
->conditional
);
446 __split_expr(expr
->cond_true
);
447 __push_true_states();
448 __use_false_states();
449 __split_expr(expr
->cond_false
);
450 __merge_true_states();
453 set_parent_expr(expr
->fn
, expr
);
455 if (sym_name_is("__builtin_constant_p", expr
->fn
))
457 split_expr_list(expr
->args
, expr
);
458 __split_expr(expr
->fn
);
459 if (is_inline_func(expr
->fn
))
460 add_inline_function(expr
->fn
->symbol
);
461 if (inlinable(expr
->fn
))
463 __process_post_op_stack();
464 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
466 if (inlinable(expr
->fn
)) {
469 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
470 if (is_noreturn_func(expr
->fn
))
473 case EXPR_INITIALIZER
:
474 split_expr_list(expr
->expr_list
, expr
);
476 case EXPR_IDENTIFIER
:
477 set_parent_expr(expr
->ident_expression
, expr
);
478 __split_expr(expr
->ident_expression
);
481 set_parent_expr(expr
->idx_expression
, expr
);
482 __split_expr(expr
->idx_expression
);
485 set_parent_expr(expr
->init_expr
, expr
);
486 __split_expr(expr
->init_expr
);
489 __pass_to_client(expr
, SYM_HOOK
);
492 __pass_to_client(expr
, STRING_HOOK
);
497 pop_expression(&big_expression_stack
);
500 static int is_forever_loop(struct statement
*stmt
)
502 struct expression
*expr
;
504 expr
= strip_expr(stmt
->iterator_pre_condition
);
506 expr
= stmt
->iterator_post_condition
;
508 /* this is a for(;;) loop... */
512 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
519 static char *get_loop_name(int num
)
523 snprintf(buf
, 255, "-loop%d", num
);
525 return alloc_sname(buf
);
529 * Pre Loops are while and for loops.
531 static void handle_pre_loop(struct statement
*stmt
)
533 int once_through
; /* we go through the loop at least once */
534 struct sm_state
*extra_sm
= NULL
;
537 struct stree
*stree
= NULL
;
538 struct sm_state
*sm
= NULL
;
540 loop_name
= get_loop_name(loop_num
);
543 __split_stmt(stmt
->iterator_pre_statement
);
544 __prev_stmt
= stmt
->iterator_pre_statement
;
546 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
552 __merge_gotos(loop_name
);
554 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
555 __in_pre_condition
++;
556 __pass_to_client(stmt
, PRELOOP_HOOK
);
557 __split_whole_condition(stmt
->iterator_pre_condition
);
558 __in_pre_condition
--;
559 FOR_EACH_SM(stree
, sm
) {
560 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
561 } END_FOR_EACH_SM(sm
);
564 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
566 if (option_assume_loops
)
569 __split_stmt(stmt
->iterator_statement
);
570 if (is_forever_loop(stmt
)) {
572 __save_gotos(loop_name
);
574 __push_fake_cur_stree();
575 __split_stmt(stmt
->iterator_post_statement
);
576 stree
= __pop_fake_cur_stree();
578 __discard_false_states();
581 if (!__path_is_null())
582 __merge_stree_into_cur(stree
);
586 unchanged
= __iterator_unchanged(extra_sm
);
587 __split_stmt(stmt
->iterator_post_statement
);
588 __prev_stmt
= stmt
->iterator_post_statement
;
591 __save_gotos(loop_name
);
592 __in_pre_condition
++;
593 __split_whole_condition(stmt
->iterator_pre_condition
);
594 __in_pre_condition
--;
596 __merge_false_states();
598 __discard_false_states();
600 __merge_false_states();
602 if (extra_sm
&& unchanged
)
603 __extra_pre_loop_hook_after(extra_sm
,
604 stmt
->iterator_post_statement
,
605 stmt
->iterator_pre_condition
);
612 * Post loops are do {} while();
614 static void handle_post_loop(struct statement
*stmt
)
618 loop_name
= get_loop_name(loop_num
);
624 __merge_gotos(loop_name
);
625 __split_stmt(stmt
->iterator_statement
);
627 if (!is_zero(stmt
->iterator_post_condition
))
628 __save_gotos(loop_name
);
630 if (is_forever_loop(stmt
)) {
633 __split_whole_condition(stmt
->iterator_post_condition
);
634 __use_false_states();
640 static int empty_statement(struct statement
*stmt
)
644 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
649 static int last_stmt_on_same_line(void)
651 struct statement
*stmt
;
654 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
657 if (stmt
->pos
.line
== get_lineno())
660 } END_FOR_EACH_PTR_REVERSE(stmt
);
664 static void split_asm_constraints(struct expression_list
*expr_list
)
666 struct expression
*expr
;
669 FOR_EACH_PTR(expr_list
, expr
) {
671 case 0: /* identifier */
672 case 1: /* constraint */
675 case 2: /* expression */
680 } END_FOR_EACH_PTR(expr
);
683 static int is_case_val(struct statement
*stmt
, sval_t sval
)
687 if (stmt
->type
!= STMT_CASE
)
689 if (!stmt
->case_expression
) {
693 if (!get_value(stmt
->case_expression
, &case_sval
))
695 if (case_sval
.value
== sval
.value
)
700 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
701 struct expression
*case_expr
,
702 struct expression
*case_to
)
705 struct range_list
*rl
= NULL
;
706 struct symbol
*switch_type
;
708 switch_type
= get_type(switch_expr
);
709 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
710 start
= sval_cast(switch_type
, start
);
711 end
= sval_cast(switch_type
, end
);
712 add_range(&rl
, start
, end
);
713 } else if (get_value(case_expr
, &start
)) {
714 start
= sval_cast(switch_type
, start
);
715 add_range(&rl
, start
, start
);
721 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
723 struct statement
*tmp
;
724 struct range_list
*rl
;
726 __split_expr(stmt
->switch_expression
);
727 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
729 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
730 __save_switch_states(top_expression(switch_expr_stack
));
735 stmt
= stmt
->switch_statement
;
737 __push_scope_hooks();
738 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
739 __smatch_lineno
= tmp
->pos
.line
;
740 if (is_case_val(tmp
, sval
)) {
741 rl
= alloc_rl(sval
, sval
);
742 __merge_switches(top_expression(switch_expr_stack
), rl
);
743 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
745 if (__path_is_null())
748 if (__path_is_null()) {
752 } END_FOR_EACH_PTR(tmp
);
754 __call_scope_hooks();
755 if (!__pop_default())
756 __merge_switches(top_expression(switch_expr_stack
), NULL
);
757 __discard_switches();
759 pop_expression(&switch_expr_stack
);
762 static void split_case(struct statement
*stmt
)
764 struct range_list
*rl
= NULL
;
766 rl
= get_case_rl(top_expression(switch_expr_stack
),
767 stmt
->case_expression
, stmt
->case_to
);
768 while (stmt
->case_statement
->type
== STMT_CASE
) {
769 struct range_list
*tmp
;
771 tmp
= get_case_rl(top_expression(switch_expr_stack
),
772 stmt
->case_statement
->case_expression
,
773 stmt
->case_statement
->case_to
);
776 rl
= rl_union(rl
, tmp
);
777 if (!stmt
->case_expression
)
779 stmt
= stmt
->case_statement
;
782 __merge_switches(top_expression(switch_expr_stack
), rl
);
784 if (!stmt
->case_expression
)
786 __split_stmt(stmt
->case_statement
);
789 static int taking_too_long(void)
793 ms
= ms_since(&fn_start_time
);
794 if (ms
> 1000 * 60 * 5) /* five minutes */
799 static int is_last_stmt(struct statement
*cur_stmt
)
801 struct symbol
*fn
= get_base_type(cur_func_sym
);
802 struct statement
*stmt
;
808 stmt
= fn
->inline_stmt
;
809 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
811 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
812 if (stmt
&& stmt
->type
== STMT_LABEL
)
813 stmt
= stmt
->label_statement
;
814 if (stmt
== cur_stmt
)
819 static void handle_backward_goto(struct statement
*goto_stmt
)
821 const char *goto_name
, *label_name
;
822 struct statement
*func_stmt
;
823 struct symbol
*base_type
= get_base_type(cur_func_sym
);
824 struct statement
*tmp
;
829 if (last_goto_statement_handled
)
831 last_goto_statement_handled
= 1;
833 if (!goto_stmt
->goto_label
||
834 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
835 !goto_stmt
->goto_label
->ident
)
837 goto_name
= goto_stmt
->goto_label
->ident
->name
;
839 func_stmt
= base_type
->stmt
;
841 func_stmt
= base_type
->inline_stmt
;
844 if (func_stmt
->type
!= STMT_COMPOUND
)
847 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
849 if (tmp
->type
!= STMT_LABEL
)
851 if (!tmp
->label_identifier
||
852 tmp
->label_identifier
->type
!= SYM_LABEL
||
853 !tmp
->label_identifier
->ident
)
855 label_name
= tmp
->label_identifier
->ident
->name
;
856 if (strcmp(goto_name
, label_name
) != 0)
861 } END_FOR_EACH_PTR(tmp
);
864 static void fake_a_return(void)
866 struct symbol
*return_type
;
871 return_type
= get_real_base_type(cur_func_sym
);
872 return_type
= get_real_base_type(return_type
);
873 if (return_type
!= &void_ctype
) {
874 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
879 static void fake_an_empty_default(struct position pos
)
881 static struct statement none
= {};
884 none
.type
= STMT_NONE
;
885 __merge_switches(top_expression(switch_expr_stack
), NULL
);
889 static void split_compound(struct statement
*stmt
)
891 struct statement
*prev
= NULL
;
892 struct statement
*cur
= NULL
;
893 struct statement
*next
;
895 __push_scope_hooks();
897 FOR_EACH_PTR(stmt
->stmts
, next
) {
898 /* just set them all ahead of time */
899 set_parent_stmt(next
, stmt
);
909 } END_FOR_EACH_PTR(next
);
917 __call_scope_hooks();
920 void __split_label_stmt(struct statement
*stmt
)
922 if (stmt
->label_identifier
&&
923 stmt
->label_identifier
->type
== SYM_LABEL
&&
924 stmt
->label_identifier
->ident
) {
925 loop_count
|= 0x80000000;
926 __merge_gotos(stmt
->label_identifier
->ident
->name
);
930 static void find_asm_gotos(struct statement
*stmt
)
934 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
936 __save_gotos(sym
->ident
->name
);
937 } END_FOR_EACH_PTR(sym
);
940 void __split_stmt(struct statement
*stmt
)
947 if (__bail_on_rest_of_function
)
950 if (out_of_memory() || taking_too_long()) {
952 __bail_on_rest_of_function
= 1;
954 sm_msg("Function too hairy. Giving up.");
956 final_pass
= 0; /* turn off sm_msg() from here */
960 add_ptr_list(&big_statement_stack
, stmt
);
961 free_expression_stack(&big_expression_stack
);
962 set_position(stmt
->pos
);
963 __pass_to_client(stmt
, STMT_HOOK
);
965 switch (stmt
->type
) {
966 case STMT_DECLARATION
:
967 split_declaration(stmt
->declaration
);
970 __split_expr(stmt
->ret_value
);
971 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
972 __process_post_op_stack();
975 case STMT_EXPRESSION
:
976 __split_expr(stmt
->expression
);
979 split_compound(stmt
);
982 set_parent_stmt(stmt
->if_true
, stmt
);
983 set_parent_stmt(stmt
->if_false
, stmt
);
985 if (known_condition_true(stmt
->if_conditional
)) {
986 __split_stmt(stmt
->if_true
);
989 if (known_condition_false(stmt
->if_conditional
)) {
990 __split_stmt(stmt
->if_false
);
993 __split_whole_condition(stmt
->if_conditional
);
994 __split_stmt(stmt
->if_true
);
995 if (empty_statement(stmt
->if_true
) &&
996 last_stmt_on_same_line() &&
997 !get_macro_name(stmt
->if_true
->pos
))
998 sm_msg("warn: if();");
999 __push_true_states();
1000 __use_false_states();
1001 __split_stmt(stmt
->if_false
);
1002 __merge_true_states();
1005 set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1006 set_parent_stmt(stmt
->iterator_statement
, stmt
);
1007 set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1009 if (stmt
->iterator_pre_condition
)
1010 handle_pre_loop(stmt
);
1011 else if (stmt
->iterator_post_condition
)
1012 handle_post_loop(stmt
);
1014 // these are for(;;) type loops.
1015 handle_pre_loop(stmt
);
1019 set_parent_stmt(stmt
->switch_statement
, stmt
);
1021 if (get_value(stmt
->switch_expression
, &sval
)) {
1022 split_known_switch(stmt
, sval
);
1025 __split_expr(stmt
->switch_expression
);
1026 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1027 __save_switch_states(top_expression(switch_expr_stack
));
1031 __split_stmt(stmt
->switch_statement
);
1032 if (!__pop_default())
1033 fake_an_empty_default(stmt
->pos
);
1034 __discard_switches();
1036 pop_expression(&switch_expr_stack
);
1042 __split_label_stmt(stmt
);
1043 __split_stmt(stmt
->label_statement
);
1046 __split_expr(stmt
->goto_expression
);
1047 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1048 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1050 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1052 __process_continues();
1054 } else if (stmt
->goto_label
&&
1055 stmt
->goto_label
->type
== SYM_LABEL
&&
1056 stmt
->goto_label
->ident
) {
1057 __save_gotos(stmt
->goto_label
->ident
->name
);
1060 if (is_last_stmt(stmt
))
1061 handle_backward_goto(stmt
);
1066 find_asm_gotos(stmt
);
1067 __pass_to_client(stmt
, ASM_HOOK
);
1068 __split_expr(stmt
->asm_string
);
1069 split_asm_constraints(stmt
->asm_outputs
);
1070 split_asm_constraints(stmt
->asm_inputs
);
1071 split_asm_constraints(stmt
->asm_clobbers
);
1076 __split_expr(stmt
->range_expression
);
1077 __split_expr(stmt
->range_low
);
1078 __split_expr(stmt
->range_high
);
1081 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1083 __process_post_op_stack();
1086 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1088 struct expression
*expr
;
1090 FOR_EACH_PTR(expr_list
, expr
) {
1091 set_parent_expr(expr
, parent
);
1093 __process_post_op_stack();
1094 } END_FOR_EACH_PTR(expr
);
1097 static void split_sym(struct symbol
*sym
)
1101 if (!(sym
->namespace & NS_SYMBOL
))
1104 __split_stmt(sym
->stmt
);
1105 __split_expr(sym
->array_size
);
1106 split_symlist(sym
->arguments
);
1107 split_symlist(sym
->symbol_list
);
1108 __split_stmt(sym
->inline_stmt
);
1109 split_symlist(sym
->inline_symbol_list
);
1112 static void split_symlist(struct symbol_list
*sym_list
)
1116 FOR_EACH_PTR(sym_list
, sym
) {
1118 } END_FOR_EACH_PTR(sym
);
1121 typedef void (fake_cb
)(struct expression
*expr
);
1123 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1125 struct symbol
*type
, *tmp
;
1131 name
= member
->name
;
1133 type
= get_type(expr
);
1134 if (!type
|| type
->type
!= SYM_STRUCT
)
1138 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1142 if (strcmp(name
, tmp
->ident
->name
) == 0)
1144 } END_FOR_EACH_PTR(tmp
);
1148 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1150 struct symbol
*type
, *member
;
1153 type
= get_type(expr
);
1154 if (!type
|| type
->type
!= SYM_STRUCT
)
1157 FOR_EACH_PTR(type
->symbol_list
, member
) {
1159 return member
->ident
;
1161 } END_FOR_EACH_PTR(member
);
1165 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1168 struct ident
*ident
;
1172 static struct member_set
*alloc_member_set(struct symbol
*type
)
1174 struct member_set
*member_set
;
1175 struct symbol
*member
;
1179 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1180 member_set
= malloc(member_count
* sizeof(*member_set
));
1182 FOR_EACH_PTR(type
->symbol_list
, member
) {
1183 member_set
[member_idx
].ident
= member
->ident
;
1184 member_set
[member_idx
].set
= 0;
1186 } END_FOR_EACH_PTR(member
);
1191 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
1193 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
1196 for (i
= 0; i
< member_count
; i
++) {
1197 if (member_set
[i
].ident
== ident
) {
1198 member_set
[i
].set
= 1;
1202 // crap. this is buggy.
1203 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1206 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1208 struct expression
*edge_member
, *assign
;
1209 struct symbol
*base
= get_real_base_type(member
);
1213 expr
= member_expression(expr
, '.', member
->ident
);
1215 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1216 struct symbol
*type
;
1218 type
= get_real_base_type(tmp
);
1223 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1224 if (get_state_expr(SMATCH_EXTRA
, edge_member
))
1228 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1229 set_inner_struct_members(expr
, tmp
);
1236 assign
= assign_expression(edge_member
, zero_expr());
1237 __split_expr(assign
);
1238 } END_FOR_EACH_PTR(tmp
);
1243 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1246 struct expression
*member
= NULL
;
1247 struct expression
*assign
;
1250 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1251 expr
= strip_expr(expr
->unop
);
1255 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1256 type
= get_real_base_type(tmp
);
1261 member
= member_expression(expr
, op
, tmp
->ident
);
1262 if (get_state_expr(SMATCH_EXTRA
, member
))
1266 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1267 set_inner_struct_members(expr
, tmp
);
1270 if (type
->type
== SYM_ARRAY
)
1275 assign
= assign_expression(member
, zero_expr());
1276 __split_expr(assign
);
1277 } END_FOR_EACH_PTR(tmp
);
1280 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1282 struct expression
*deref
, *assign
, *tmp
;
1283 struct symbol
*struct_type
, *type
;
1284 struct ident
*member
;
1286 struct member_set
*member_set
;
1288 struct_type
= get_type(symbol
);
1290 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1293 member_set
= alloc_member_set(struct_type
);
1296 FOR_EACH_PTR(members
, tmp
) {
1297 member
= number_to_member(symbol
, member_idx
);
1298 while (tmp
->type
== EXPR_IDENTIFIER
) {
1299 member
= tmp
->expr_ident
;
1300 member_idx
= member_to_number(symbol
, member
);
1301 tmp
= tmp
->ident_expression
;
1303 mark_member_as_set(struct_type
, member_set
, member
);
1305 deref
= member_expression(symbol
, '.', member
);
1306 if (tmp
->type
== EXPR_INITIALIZER
) {
1307 type
= get_type(deref
);
1308 if (type
&& type
->type
== SYM_ARRAY
)
1309 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1311 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1313 assign
= assign_expression(deref
, tmp
);
1316 } END_FOR_EACH_PTR(tmp
);
1318 set_unset_to_zero(struct_type
, symbol
);
1321 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1323 fake_member_assigns_helper(symbol_expression(sym
),
1324 sym
->initializer
->expr_list
, fake_cb
);
1327 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1329 struct expression
*offset
, *binop
, *assign
, *tmp
;
1330 struct symbol
*type
;
1333 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1337 FOR_EACH_PTR(expr_list
, tmp
) {
1338 if (tmp
->type
== EXPR_INDEX
) {
1339 if (tmp
->idx_from
!= tmp
->idx_to
)
1341 idx
= tmp
->idx_from
;
1342 if (!tmp
->idx_expression
)
1344 tmp
= tmp
->idx_expression
;
1346 offset
= value_expr(idx
);
1347 binop
= array_element_expression(array
, offset
);
1348 if (tmp
->type
== EXPR_INITIALIZER
) {
1349 type
= get_type(binop
);
1350 if (type
&& type
->type
== SYM_ARRAY
)
1351 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1353 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1355 assign
= assign_expression(binop
, tmp
);
1360 } END_FOR_EACH_PTR(tmp
);
1363 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1365 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1368 static void fake_assign_expr(struct symbol
*sym
)
1370 struct expression
*assign
, *symbol
;
1372 symbol
= symbol_expression(sym
);
1373 assign
= assign_expression(symbol
, sym
->initializer
);
1374 __split_expr(assign
);
1377 static void call_split_expr(struct expression
*expr
)
1382 static void do_initializer_stuff(struct symbol
*sym
)
1384 if (!sym
->initializer
)
1387 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1388 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1389 fake_element_assigns(sym
, call_split_expr
);
1391 fake_member_assigns(sym
, call_split_expr
);
1393 fake_assign_expr(sym
);
1397 static void split_declaration(struct symbol_list
*sym_list
)
1401 FOR_EACH_PTR(sym_list
, sym
) {
1402 __pass_to_client(sym
, DECLARATION_HOOK
);
1403 do_initializer_stuff(sym
);
1405 } END_FOR_EACH_PTR(sym
);
1408 static void call_global_assign_hooks(struct expression
*assign
)
1410 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1413 static void fake_global_assign(struct symbol
*sym
)
1415 struct expression
*assign
, *symbol
;
1417 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1418 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1419 fake_element_assigns(sym
, call_global_assign_hooks
);
1420 } else if (sym
->initializer
) {
1421 symbol
= symbol_expression(sym
);
1422 assign
= assign_expression(symbol
, sym
->initializer
);
1423 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1425 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1427 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1428 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1429 fake_member_assigns(sym
, call_global_assign_hooks
);
1430 } else if (sym
->initializer
) {
1431 symbol
= symbol_expression(sym
);
1432 assign
= assign_expression(symbol
, sym
->initializer
);
1433 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1435 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1438 symbol
= symbol_expression(sym
);
1439 if (sym
->initializer
)
1440 assign
= assign_expression(symbol
, sym
->initializer
);
1442 assign
= assign_expression(symbol
, zero_expr());
1443 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1447 static void start_function_definition(struct symbol
*sym
)
1449 __in_function_def
= 1;
1450 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1451 __in_function_def
= 0;
1452 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1456 static void split_function(struct symbol
*sym
)
1458 struct symbol
*base_type
= get_base_type(sym
);
1460 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1463 gettimeofday(&fn_start_time
, NULL
);
1466 cur_func
= sym
->ident
->name
;
1467 set_position(sym
->pos
);
1469 last_goto_statement_handled
= 0;
1470 sm_debug("new function: %s\n", cur_func
);
1472 if (option_two_passes
) {
1476 start_function_definition(sym
);
1477 __split_stmt(base_type
->stmt
);
1478 __split_stmt(base_type
->inline_stmt
);
1484 start_function_definition(sym
);
1485 __split_stmt(base_type
->stmt
);
1486 __split_stmt(base_type
->inline_stmt
);
1487 __pass_to_client(sym
, END_FUNC_HOOK
);
1488 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1491 cur_func_sym
= NULL
;
1493 free_data_info_allocs();
1494 free_expression_stack(&switch_expr_stack
);
1495 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1496 __bail_on_rest_of_function
= 0;
1499 static void parse_inline(struct expression
*call
)
1501 struct symbol
*base_type
;
1502 int loop_num_bak
= loop_num
;
1503 int loop_count_bak
= loop_count
;
1504 int final_pass_bak
= final_pass
;
1505 char *cur_func_bak
= cur_func
;
1506 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1507 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1508 struct expression_list
*big_condition_stack_bak
= big_condition_stack
;
1509 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1510 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1512 __pass_to_client(call
, INLINE_FN_START
);
1513 final_pass
= 0; /* don't print anything */
1516 base_type
= get_base_type(call
->fn
->symbol
);
1517 cur_func_sym
= call
->fn
->symbol
;
1518 if (call
->fn
->symbol
->ident
)
1519 cur_func
= call
->fn
->symbol
->ident
->name
;
1522 set_position(call
->fn
->symbol
->pos
);
1525 big_statement_stack
= NULL
;
1526 big_expression_stack
= NULL
;
1527 big_condition_stack
= NULL
;
1528 switch_expr_stack
= NULL
;
1530 sm_debug("inline function: %s\n", cur_func
);
1533 start_function_definition(call
->fn
->symbol
);
1534 __split_stmt(base_type
->stmt
);
1535 __split_stmt(base_type
->inline_stmt
);
1536 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1537 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1539 free_expression_stack(&switch_expr_stack
);
1540 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1544 loop_num
= loop_num_bak
;
1545 loop_count
= loop_count_bak
;
1546 final_pass
= final_pass_bak
;
1547 cur_func_sym
= cur_func_sym_bak
;
1548 cur_func
= cur_func_bak
;
1549 big_statement_stack
= big_statement_stack_bak
;
1550 big_expression_stack
= big_expression_stack_bak
;
1551 big_condition_stack
= big_condition_stack_bak
;
1552 switch_expr_stack
= switch_expr_stack_bak
;
1554 restore_all_states();
1555 set_position(call
->pos
);
1557 __pass_to_client(call
, INLINE_FN_END
);
1560 static struct symbol_list
*inlines_called
;
1561 static void add_inline_function(struct symbol
*sym
)
1563 static struct symbol_list
*already_added
;
1566 FOR_EACH_PTR(already_added
, tmp
) {
1569 } END_FOR_EACH_PTR(tmp
);
1571 add_ptr_list(&already_added
, sym
);
1572 add_ptr_list(&inlines_called
, sym
);
1575 static void process_inlines(void)
1579 FOR_EACH_PTR(inlines_called
, tmp
) {
1580 split_function(tmp
);
1581 } END_FOR_EACH_PTR(tmp
);
1582 free_ptr_list(&inlines_called
);
1585 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1589 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1592 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1594 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1596 } END_FOR_EACH_PTR_REVERSE(sym
);
1601 static void split_inlines_in_scope(struct symbol
*sym
)
1603 struct symbol
*base
;
1604 struct symbol_list
*scope_list
;
1607 scope_list
= sym
->scope
->symbols
;
1608 stream
= sym
->pos
.stream
;
1610 /* find the last static symbol in the file */
1611 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1612 if (sym
->pos
.stream
!= stream
)
1614 if (sym
->type
!= SYM_NODE
)
1616 base
= get_base_type(sym
);
1619 if (base
->type
!= SYM_FN
)
1621 if (!base
->inline_stmt
)
1623 add_inline_function(sym
);
1624 } END_FOR_EACH_PTR_REVERSE(sym
);
1629 static void split_inlines(struct symbol_list
*sym_list
)
1633 sym
= get_last_scoped_symbol(sym_list
, 0);
1635 split_inlines_in_scope(sym
);
1636 sym
= get_last_scoped_symbol(sym_list
, 1);
1638 split_inlines_in_scope(sym
);
1641 static struct stree
*clone_estates_perm(struct stree
*orig
)
1643 struct stree
*ret
= NULL
;
1644 struct sm_state
*tmp
;
1646 FOR_EACH_SM(orig
, tmp
) {
1647 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1648 } END_FOR_EACH_SM(tmp
);
1653 struct position last_pos
;
1654 static void split_functions(struct symbol_list
*sym_list
)
1659 FOR_EACH_PTR(sym_list
, sym
) {
1660 set_position(sym
->pos
);
1661 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1662 __pass_to_client(sym
, BASE_HOOK
);
1663 fake_global_assign(sym
);
1665 } END_FOR_EACH_PTR(sym
);
1666 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1669 FOR_EACH_PTR(sym_list
, sym
) {
1670 set_position(sym
->pos
);
1671 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1672 split_function(sym
);
1675 last_pos
= sym
->pos
;
1676 } END_FOR_EACH_PTR(sym
);
1677 split_inlines(sym_list
);
1678 __pass_to_client(sym_list
, END_FILE_HOOK
);
1681 void smatch(int argc
, char **argv
)
1683 struct string_list
*filelist
= NULL
;
1684 struct symbol_list
*sym_list
;
1685 struct timeval stop
, start
;
1687 gettimeofday(&start
, NULL
);
1690 printf("Usage: smatch [--debug] <filename.c>\n");
1693 sparse_initialize(argc
, argv
, &filelist
);
1694 set_valid_ptr_max();
1695 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1696 if (option_file_output
) {
1699 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1700 sm_outfd
= fopen(buf
, "w");
1702 printf("Error: Cannot open %s\n", base_file
);
1706 sym_list
= sparse_keep_tokens(base_file
);
1707 split_functions(sym_list
);
1708 } END_FOR_EACH_PTR_NOTAG(base_file
);
1710 gettimeofday(&stop
, NULL
);
1712 set_position(last_pos
);
1714 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);