user_data2: wrong parameter tagged as user data in database
[smatch.git] / smatch_flow.c
blob5d77cfec8f6e21eea6c14df0fbd9643ce516bb23
1 /*
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
18 #define _GNU_SOURCE 1
19 #include <unistd.h>
20 #include <stdio.h>
21 #include "token.h"
22 #include "scope.h"
23 #include "smatch.h"
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 int __in_fake_assign;
29 int final_pass;
30 int __inline_call;
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 int __in_pre_condition = 0;
50 int __bail_on_rest_of_function = 0;
51 static struct timeval fn_start_time;
52 char *get_function(void) { return cur_func; }
53 int get_lineno(void) { return __smatch_lineno; }
54 int inside_loop(void) { return !!loop_count; }
55 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
56 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
57 int in_expression_statement(void) { return !!__expr_stmt_count; }
59 static void split_symlist(struct symbol_list *sym_list);
60 static void split_declaration(struct symbol_list *sym_list);
61 static void split_expr_list(struct expression_list *expr_list);
62 static void add_inline_function(struct symbol *sym);
63 static void parse_inline(struct expression *expr);
65 int option_assume_loops = 0;
66 int option_known_conditions = 0;
67 int option_two_passes = 0;
68 struct symbol *cur_func_sym = NULL;
69 struct stree *global_states;
71 long long valid_ptr_min = 4096;
72 long long valid_ptr_max = 2117777777;
73 sval_t valid_ptr_min_sval = {
74 .type = &ptr_ctype,
75 {.value = 4096},
77 sval_t valid_ptr_max_sval = {
78 .type = &ptr_ctype,
79 {.value = LONG_MAX - 100000},
82 static void set_valid_ptr_max(void)
84 if (type_bits(&ptr_ctype) == 32)
85 valid_ptr_max = 2117777777;
86 else if (type_bits(&ptr_ctype) == 64)
87 valid_ptr_max = 2117777777777777777LL;
89 valid_ptr_max_sval.value = valid_ptr_max;
92 int outside_of_function(void)
94 return cur_func_sym == NULL;
97 const char *get_filename(void)
99 if (option_info)
100 return base_file;
101 if (option_full_path)
102 return full_filename;
103 return filename;
106 const char *get_base_file(void)
108 return base_file;
111 static void set_position(struct position pos)
113 int len;
114 static int prev_stream = -1;
116 if (pos.stream == 0 && pos.line == 0)
117 return;
119 __smatch_lineno = pos.line;
121 if (pos.stream == prev_stream)
122 return;
124 filename = stream_name(pos.stream);
126 free(full_filename);
127 pathname = getcwd(NULL, 0);
128 if (pathname) {
129 len = strlen(pathname) + 1 + strlen(filename) + 1;
130 full_filename = malloc(len);
131 snprintf(full_filename, len, "%s/%s", pathname, filename);
132 } else {
133 full_filename = alloc_string(filename);
135 free(pathname);
138 int is_assigned_call(struct expression *expr)
140 struct expression *tmp;
142 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
143 if (tmp->type == EXPR_ASSIGNMENT && tmp->op == '=' &&
144 strip_expr(tmp->right) == expr)
145 return 1;
146 if (tmp->pos.line < expr->pos.line)
147 return 0;
148 } END_FOR_EACH_PTR_REVERSE(tmp);
149 return 0;
152 static int is_inline_func(struct expression *expr)
154 if (expr->type != EXPR_SYMBOL || !expr->symbol)
155 return 0;
156 if (expr->symbol->ctype.modifiers & MOD_INLINE)
157 return 1;
158 return 0;
161 static int is_noreturn_func(struct expression *expr)
163 if (expr->type != EXPR_SYMBOL || !expr->symbol)
164 return 0;
165 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
166 return 1;
167 return 0;
170 int inlinable(struct expression *expr)
172 struct symbol *sym;
173 struct statement *last_stmt = NULL;
175 if (__inline_fn) /* don't nest */
176 return 0;
178 if (expr->type != EXPR_SYMBOL || !expr->symbol)
179 return 0;
180 if (is_no_inline_function(expr->symbol->ident->name))
181 return 0;
182 sym = get_base_type(expr->symbol);
183 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
184 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
185 return 0;
186 if (sym->stmt->type != STMT_COMPOUND)
187 return 0;
188 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
190 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
191 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
192 return 0;
193 if (sym->inline_stmt->type != STMT_COMPOUND)
194 return 0;
195 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
198 if (!last_stmt)
199 return 0;
201 /* the magic numbers in this function are pulled out of my bum. */
202 if (last_stmt->pos.line > sym->pos.line + 20)
203 return 0;
205 return 1;
208 void __process_post_op_stack(void)
210 struct expression *expr;
212 FOR_EACH_PTR(post_op_stack, expr) {
213 __pass_to_client(expr, OP_HOOK);
214 } END_FOR_EACH_PTR(expr);
216 __free_ptr_list((struct ptr_list **)&post_op_stack);
219 static int handle_comma_assigns(struct expression *expr)
221 struct expression *right;
222 struct expression *assign;
224 right = strip_expr(expr->right);
225 if (right->type != EXPR_COMMA)
226 return 0;
228 __split_expr(right->left);
229 __process_post_op_stack();
231 assign = assign_expression(expr->left, right->right);
232 __split_expr(assign);
234 return 1;
237 void __split_expr(struct expression *expr)
239 if (!expr)
240 return;
242 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
244 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
245 return;
246 if (__in_fake_assign >= 4) /* don't allow too much nesting */
247 return;
249 push_expression(&big_expression_stack, expr);
250 set_position(expr->pos);
251 __pass_to_client(expr, EXPR_HOOK);
253 switch (expr->type) {
254 case EXPR_PREOP:
255 if (expr->op == '*')
256 __pass_to_client(expr, DEREF_HOOK);
257 __split_expr(expr->unop);
258 __pass_to_client(expr, OP_HOOK);
259 break;
260 case EXPR_POSTOP:
261 __split_expr(expr->unop);
262 push_expression(&post_op_stack, expr);
263 break;
264 case EXPR_STATEMENT:
265 __expr_stmt_count++;
266 __split_stmt(expr->statement);
267 __expr_stmt_count--;
268 break;
269 case EXPR_LOGICAL:
270 case EXPR_COMPARE:
271 __pass_to_client(expr, LOGIC_HOOK);
272 __handle_logic(expr);
273 break;
274 case EXPR_BINOP:
275 __pass_to_client(expr, BINOP_HOOK);
276 case EXPR_COMMA:
277 __split_expr(expr->left);
278 __process_post_op_stack();
279 __split_expr(expr->right);
280 break;
281 case EXPR_ASSIGNMENT: {
282 struct expression *tmp;
284 if (!expr->right)
285 break;
287 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
289 /* foo = !bar() */
290 if (__handle_condition_assigns(expr))
291 break;
292 /* foo = (x < 5 ? foo : 5); */
293 if (__handle_select_assigns(expr))
294 break;
295 /* foo = ({frob(); frob(); frob(); 1;}) */
296 if (__handle_expr_statement_assigns(expr))
297 break;
298 /* foo = (3, 4); */
299 if (handle_comma_assigns(expr))
300 break;
302 __split_expr(expr->right);
303 if (outside_of_function())
304 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
305 else
306 __pass_to_client(expr, ASSIGNMENT_HOOK);
308 __fake_struct_member_assignments(expr);
310 tmp = strip_expr(expr->right);
311 if (expr->op == '=' && tmp->type == EXPR_CALL) {
312 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
313 if (!is_fake_call(tmp))
314 __pass_to_client(tmp, FUNCTION_CALL_HOOK_AFTER);
316 if (get_macro_name(tmp->pos) &&
317 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
318 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
319 __split_expr(expr->left);
320 break;
322 case EXPR_DEREF:
323 __pass_to_client(expr, DEREF_HOOK);
324 __split_expr(expr->deref);
325 break;
326 case EXPR_SLICE:
327 __split_expr(expr->base);
328 break;
329 case EXPR_CAST:
330 case EXPR_FORCE_CAST:
331 __pass_to_client(expr, CAST_HOOK);
332 __split_expr(expr->cast_expression);
333 break;
334 case EXPR_SIZEOF:
335 if (expr->cast_expression)
336 __pass_to_client(strip_parens(expr->cast_expression),
337 SIZEOF_HOOK);
338 break;
339 case EXPR_OFFSETOF:
340 case EXPR_ALIGNOF:
341 evaluate_expression(expr);
342 break;
343 case EXPR_CONDITIONAL:
344 case EXPR_SELECT:
345 if (known_condition_true(expr->conditional)) {
346 __split_expr(expr->cond_true);
347 break;
349 if (known_condition_false(expr->conditional)) {
350 __split_expr(expr->cond_false);
351 break;
353 __pass_to_client(expr, SELECT_HOOK);
354 __split_whole_condition(expr->conditional);
355 __split_expr(expr->cond_true);
356 __push_true_states();
357 __use_false_states();
358 __split_expr(expr->cond_false);
359 __merge_true_states();
360 break;
361 case EXPR_CALL:
362 if (sym_name_is("__builtin_constant_p", expr->fn))
363 break;
364 split_expr_list(expr->args);
365 __split_expr(expr->fn);
366 if (is_inline_func(expr->fn))
367 add_inline_function(expr->fn->symbol);
368 if (inlinable(expr->fn))
369 __inline_call = 1;
370 __process_post_op_stack();
371 __pass_to_client(expr, FUNCTION_CALL_HOOK);
372 __inline_call = 0;
373 if (inlinable(expr->fn)) {
374 parse_inline(expr);
376 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
377 if (!is_assigned_call(expr))
378 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER);
379 if (is_noreturn_func(expr->fn))
380 nullify_path();
381 break;
382 case EXPR_INITIALIZER:
383 split_expr_list(expr->expr_list);
384 break;
385 case EXPR_IDENTIFIER:
386 __split_expr(expr->ident_expression);
387 break;
388 case EXPR_INDEX:
389 __split_expr(expr->idx_expression);
390 break;
391 case EXPR_POS:
392 __split_expr(expr->init_expr);
393 break;
394 case EXPR_SYMBOL:
395 __pass_to_client(expr, SYM_HOOK);
396 break;
397 case EXPR_STRING:
398 __pass_to_client(expr, STRING_HOOK);
399 break;
400 default:
401 break;
403 pop_expression(&big_expression_stack);
406 static int is_forever_loop(struct statement *stmt)
408 struct expression *expr;
410 expr = strip_expr(stmt->iterator_pre_condition);
411 if (!expr)
412 expr = stmt->iterator_post_condition;
413 if (!expr) {
414 /* this is a for(;;) loop... */
415 return 1;
418 if (expr->type == EXPR_VALUE && expr->value == 1)
419 return 1;
421 return 0;
424 static int loop_num;
425 static char *get_loop_name(int num)
427 char buf[256];
429 snprintf(buf, 255, "-loop%d", num);
430 buf[255] = '\0';
431 return alloc_sname(buf);
435 * Pre Loops are while and for loops.
437 static void handle_pre_loop(struct statement *stmt)
439 int once_through; /* we go through the loop at least once */
440 struct sm_state *extra_sm = NULL;
441 int unchanged = 0;
442 char *loop_name;
443 struct stree *stree = NULL;
444 struct sm_state *sm = NULL;
446 loop_name = get_loop_name(loop_num);
447 loop_num++;
449 __split_stmt(stmt->iterator_pre_statement);
451 once_through = implied_condition_true(stmt->iterator_pre_condition);
453 loop_count++;
454 __push_continues();
455 __push_breaks();
457 __merge_gotos(loop_name);
459 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
460 __in_pre_condition++;
461 __pass_to_client(stmt, PRELOOP_HOOK);
462 __split_whole_condition(stmt->iterator_pre_condition);
463 __in_pre_condition--;
464 FOR_EACH_SM(stree, sm) {
465 set_state(sm->owner, sm->name, sm->sym, sm->state);
466 } END_FOR_EACH_SM(sm);
467 free_stree(&stree);
468 if (extra_sm)
469 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
471 if (option_assume_loops)
472 once_through = 1;
474 __split_stmt(stmt->iterator_statement);
475 __warn_on_silly_pre_loops();
476 if (is_forever_loop(stmt)) {
477 __merge_continues();
478 __save_gotos(loop_name);
480 __push_fake_cur_stree();
481 __split_stmt(stmt->iterator_post_statement);
482 stree = __pop_fake_cur_stree();
484 __discard_false_states();
485 __use_breaks();
487 if (!__path_is_null())
488 __merge_stree_into_cur(stree);
489 free_stree(&stree);
490 } else {
491 __merge_continues();
492 unchanged = __iterator_unchanged(extra_sm);
493 __split_stmt(stmt->iterator_post_statement);
494 __save_gotos(loop_name);
495 __in_pre_condition++;
496 __split_whole_condition(stmt->iterator_pre_condition);
497 __in_pre_condition--;
498 nullify_path();
499 __merge_false_states();
500 if (once_through)
501 __discard_false_states();
502 else
503 __merge_false_states();
505 if (extra_sm && unchanged)
506 __extra_pre_loop_hook_after(extra_sm,
507 stmt->iterator_post_statement,
508 stmt->iterator_pre_condition);
509 __merge_breaks();
511 loop_count--;
515 * Post loops are do {} while();
517 static void handle_post_loop(struct statement *stmt)
519 char *loop_name;
521 loop_name = get_loop_name(loop_num);
522 loop_num++;
523 loop_count++;
525 __push_continues();
526 __push_breaks();
527 __merge_gotos(loop_name);
528 __split_stmt(stmt->iterator_statement);
529 __merge_continues();
530 if (!is_zero(stmt->iterator_post_condition))
531 __save_gotos(loop_name);
533 if (is_forever_loop(stmt)) {
534 __use_breaks();
535 } else {
536 __split_whole_condition(stmt->iterator_post_condition);
537 __use_false_states();
538 __merge_breaks();
540 loop_count--;
543 static int empty_statement(struct statement *stmt)
545 if (!stmt)
546 return 0;
547 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
548 return 1;
549 return 0;
552 static int last_stmt_on_same_line(void)
554 struct statement *stmt;
555 int i = 0;
557 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
558 if (!i++)
559 continue;
560 if (stmt->pos.line == get_lineno())
561 return 1;
562 return 0;
563 } END_FOR_EACH_PTR_REVERSE(stmt);
564 return 0;
567 static void split_asm_constraints(struct expression_list *expr_list)
569 struct expression *expr;
570 int state = 0;
572 FOR_EACH_PTR(expr_list, expr) {
573 switch (state) {
574 case 0: /* identifier */
575 case 1: /* constraint */
576 state++;
577 continue;
578 case 2: /* expression */
579 state = 0;
580 __split_expr(expr);
581 continue;
583 } END_FOR_EACH_PTR(expr);
586 static int is_case_val(struct statement *stmt, sval_t sval)
588 sval_t case_sval;
590 if (stmt->type != STMT_CASE)
591 return 0;
592 if (!stmt->case_expression) {
593 __set_default();
594 return 1;
596 if (!get_value(stmt->case_expression, &case_sval))
597 return 0;
598 if (case_sval.value == sval.value)
599 return 1;
600 return 0;
603 static void split_known_switch(struct statement *stmt, sval_t sval)
605 struct statement *tmp;
607 __split_expr(stmt->switch_expression);
609 push_expression(&switch_expr_stack, stmt->switch_expression);
610 __save_switch_states(top_expression(switch_expr_stack));
611 nullify_path();
612 __push_default();
613 __push_breaks();
615 stmt = stmt->switch_statement;
617 __push_scope_hooks();
618 FOR_EACH_PTR(stmt->stmts, tmp) {
619 __smatch_lineno = tmp->pos.line;
620 if (is_case_val(tmp, sval)) {
621 __merge_switches(top_expression(switch_expr_stack),
622 stmt->case_expression);
623 __pass_case_to_client(top_expression(switch_expr_stack),
624 stmt->case_expression);
626 if (__path_is_null())
627 continue;
628 __split_stmt(tmp);
629 if (__path_is_null()) {
630 __set_default();
631 goto out;
633 } END_FOR_EACH_PTR(tmp);
634 out:
635 __call_scope_hooks();
636 if (!__pop_default())
637 __merge_switches(top_expression(switch_expr_stack),
638 NULL);
639 __discard_switches();
640 __merge_breaks();
641 pop_expression(&switch_expr_stack);
644 static int taking_too_long(void)
646 int ms;
648 ms = ms_since(&fn_start_time);
649 if (ms > 1000 * 60 * 5) /* five minutes */
650 return 1;
651 return 0;
654 static int is_last_stmt(struct statement *cur_stmt)
656 struct symbol *fn = get_base_type(cur_func_sym);
657 struct statement *stmt;
659 if (!fn)
660 return 0;
661 stmt = fn->stmt;
662 if (!stmt)
663 stmt = fn->inline_stmt;
664 if (!stmt || stmt->type != STMT_COMPOUND)
665 return 0;
666 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
667 if (stmt && stmt->type == STMT_LABEL)
668 stmt = stmt->label_statement;
669 if (stmt == cur_stmt)
670 return 1;
671 return 0;
674 static void handle_backward_goto(struct statement *goto_stmt)
676 const char *goto_name, *label_name;
677 struct statement *func_stmt;
678 struct symbol *base_type = get_base_type(cur_func_sym);
679 struct statement *tmp;
680 int found = 0;
682 if (!option_info)
683 return;
684 if (last_goto_statement_handled)
685 return;
686 last_goto_statement_handled = 1;
688 if (!goto_stmt->goto_label ||
689 goto_stmt->goto_label->type != SYM_LABEL ||
690 !goto_stmt->goto_label->ident)
691 return;
692 goto_name = goto_stmt->goto_label->ident->name;
694 func_stmt = base_type->stmt;
695 if (!func_stmt)
696 func_stmt = base_type->inline_stmt;
697 if (!func_stmt)
698 return;
699 if (func_stmt->type != STMT_COMPOUND)
700 return;
702 FOR_EACH_PTR(func_stmt->stmts, tmp) {
703 if (!found) {
704 if (tmp->type != STMT_LABEL)
705 continue;
706 if (!tmp->label_identifier ||
707 tmp->label_identifier->type != SYM_LABEL ||
708 !tmp->label_identifier->ident)
709 continue;
710 label_name = tmp->label_identifier->ident->name;
711 if (strcmp(goto_name, label_name) != 0)
712 continue;
713 found = 1;
715 __split_stmt(tmp);
716 } END_FOR_EACH_PTR(tmp);
719 static void fake_a_return(void)
721 struct symbol *return_type;
723 nullify_path();
724 __unnullify_path();
726 return_type = get_real_base_type(cur_func_sym);
727 return_type = get_real_base_type(return_type);
728 if (return_type != &void_ctype) {
729 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
730 nullify_path();
733 __pass_to_client(cur_func_sym, END_FUNC_HOOK);
734 __pass_to_client(cur_func_sym, AFTER_FUNC_HOOK);
737 void __split_stmt(struct statement *stmt)
739 sval_t sval;
741 if (!stmt)
742 goto out;
744 if (__bail_on_rest_of_function || out_of_memory() || taking_too_long()) {
745 static char *printed = NULL;
747 __bail_on_rest_of_function = 1;
748 if (printed != cur_func)
749 sm_msg("Function too hairy. Giving up.");
750 fake_a_return();
751 final_pass = 0; /* turn off sm_msg() from here */
752 printed = cur_func;
753 return;
756 add_ptr_list(&big_statement_stack, stmt);
757 free_expression_stack(&big_expression_stack);
758 set_position(stmt->pos);
759 __pass_to_client(stmt, STMT_HOOK);
761 switch (stmt->type) {
762 case STMT_DECLARATION:
763 split_declaration(stmt->declaration);
764 break;
765 case STMT_RETURN:
766 __split_expr(stmt->ret_value);
767 __pass_to_client(stmt->ret_value, RETURN_HOOK);
768 __process_post_op_stack();
769 nullify_path();
770 break;
771 case STMT_EXPRESSION:
772 __split_expr(stmt->expression);
773 break;
774 case STMT_COMPOUND: {
775 struct statement *tmp;
777 __push_scope_hooks();
778 FOR_EACH_PTR(stmt->stmts, tmp) {
779 __split_stmt(tmp);
780 } END_FOR_EACH_PTR(tmp);
781 __call_scope_hooks();
782 break;
784 case STMT_IF:
785 if (known_condition_true(stmt->if_conditional)) {
786 __split_stmt(stmt->if_true);
787 break;
789 if (known_condition_false(stmt->if_conditional)) {
790 __split_stmt(stmt->if_false);
791 break;
793 if (option_known_conditions &&
794 implied_condition_true(stmt->if_conditional)) {
795 sm_info("this condition is true.");
796 __split_stmt(stmt->if_true);
797 break;
799 if (option_known_conditions &&
800 implied_condition_false(stmt->if_conditional)) {
801 sm_info("this condition is false.");
802 __split_stmt(stmt->if_false);
803 break;
805 __split_whole_condition(stmt->if_conditional);
806 __split_stmt(stmt->if_true);
807 if (empty_statement(stmt->if_true) &&
808 last_stmt_on_same_line() &&
809 !get_macro_name(stmt->if_true->pos))
810 sm_msg("warn: if();");
811 __push_true_states();
812 __use_false_states();
813 __split_stmt(stmt->if_false);
814 __merge_true_states();
815 break;
816 case STMT_ITERATOR:
817 if (stmt->iterator_pre_condition)
818 handle_pre_loop(stmt);
819 else if (stmt->iterator_post_condition)
820 handle_post_loop(stmt);
821 else {
822 // these are for(;;) type loops.
823 handle_pre_loop(stmt);
825 break;
826 case STMT_SWITCH:
827 if (get_value(stmt->switch_expression, &sval)) {
828 split_known_switch(stmt, sval);
829 break;
831 __split_expr(stmt->switch_expression);
832 push_expression(&switch_expr_stack, stmt->switch_expression);
833 __save_switch_states(top_expression(switch_expr_stack));
834 nullify_path();
835 __push_default();
836 __push_breaks();
837 __split_stmt(stmt->switch_statement);
838 if (!__pop_default())
839 __merge_switches(top_expression(switch_expr_stack),
840 NULL);
841 __discard_switches();
842 __merge_breaks();
843 pop_expression(&switch_expr_stack);
844 break;
845 case STMT_CASE:
846 __merge_switches(top_expression(switch_expr_stack),
847 stmt->case_expression);
848 __pass_case_to_client(top_expression(switch_expr_stack),
849 stmt->case_expression);
850 if (!stmt->case_expression)
851 __set_default();
852 __split_expr(stmt->case_expression);
853 __split_expr(stmt->case_to);
854 __split_stmt(stmt->case_statement);
855 break;
856 case STMT_LABEL:
857 if (stmt->label_identifier &&
858 stmt->label_identifier->type == SYM_LABEL &&
859 stmt->label_identifier->ident) {
860 loop_count |= 0x80000000;
861 __merge_gotos(stmt->label_identifier->ident->name);
863 __split_stmt(stmt->label_statement);
864 break;
865 case STMT_GOTO:
866 __split_expr(stmt->goto_expression);
867 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
868 if (!strcmp(stmt->goto_label->ident->name, "break")) {
869 __process_breaks();
870 } else if (!strcmp(stmt->goto_label->ident->name,
871 "continue")) {
872 __process_continues();
874 } else if (stmt->goto_label &&
875 stmt->goto_label->type == SYM_LABEL &&
876 stmt->goto_label->ident) {
877 __save_gotos(stmt->goto_label->ident->name);
879 nullify_path();
880 if (is_last_stmt(stmt))
881 handle_backward_goto(stmt);
882 break;
883 case STMT_NONE:
884 break;
885 case STMT_ASM:
886 __pass_to_client(stmt, ASM_HOOK);
887 __split_expr(stmt->asm_string);
888 split_asm_constraints(stmt->asm_outputs);
889 split_asm_constraints(stmt->asm_inputs);
890 split_asm_constraints(stmt->asm_clobbers);
891 break;
892 case STMT_CONTEXT:
893 break;
894 case STMT_RANGE:
895 __split_expr(stmt->range_expression);
896 __split_expr(stmt->range_low);
897 __split_expr(stmt->range_high);
898 break;
900 __pass_to_client(stmt, STMT_HOOK_AFTER);
901 out:
902 __process_post_op_stack();
905 static void split_expr_list(struct expression_list *expr_list)
907 struct expression *expr;
909 FOR_EACH_PTR(expr_list, expr) {
910 __split_expr(expr);
911 __process_post_op_stack();
912 } END_FOR_EACH_PTR(expr);
915 static void split_sym(struct symbol *sym)
917 if (!sym)
918 return;
919 if (!(sym->namespace & NS_SYMBOL))
920 return;
922 __split_stmt(sym->stmt);
923 __split_expr(sym->array_size);
924 split_symlist(sym->arguments);
925 split_symlist(sym->symbol_list);
926 __split_stmt(sym->inline_stmt);
927 split_symlist(sym->inline_symbol_list);
930 static void split_symlist(struct symbol_list *sym_list)
932 struct symbol *sym;
934 FOR_EACH_PTR(sym_list, sym) {
935 split_sym(sym);
936 } END_FOR_EACH_PTR(sym);
939 typedef void (fake_cb)(struct expression *expr);
941 static int member_to_number(struct expression *expr, struct ident *member)
943 struct symbol *type, *tmp;
944 char *name;
945 int i;
947 if (!member)
948 return -1;
949 name = member->name;
951 type = get_type(expr);
952 if (!type || type->type != SYM_STRUCT)
953 return -1;
955 i = -1;
956 FOR_EACH_PTR(type->symbol_list, tmp) {
957 i++;
958 if (!tmp->ident)
959 continue;
960 if (strcmp(name, tmp->ident->name) == 0)
961 return i;
962 } END_FOR_EACH_PTR(tmp);
963 return -1;
966 static struct ident *number_to_member(struct expression *expr, int num)
968 struct symbol *type, *member;
969 int i = 0;
971 type = get_type(expr);
972 if (!type || type->type != SYM_STRUCT)
973 return NULL;
975 FOR_EACH_PTR(type->symbol_list, member) {
976 if (i == num)
977 return member->ident;
978 i++;
979 } END_FOR_EACH_PTR(member);
980 return NULL;
983 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
985 struct member_set {
986 struct ident *ident;
987 int set;
990 static struct member_set *alloc_member_set(struct symbol *type)
992 struct member_set *member_set;
993 struct symbol *member;
994 int member_count;
995 int member_idx;
997 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
998 member_set = malloc(member_count * sizeof(*member_set));
999 member_idx = 0;
1000 FOR_EACH_PTR(type->symbol_list, member) {
1001 member_set[member_idx].ident = member->ident;
1002 member_set[member_idx].set = 0;
1003 member_idx++;
1004 } END_FOR_EACH_PTR(member);
1006 return member_set;
1009 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
1011 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1012 int i;
1014 for (i = 0; i < member_count; i++) {
1015 if (member_set[i].ident == ident) {
1016 member_set[i].set = 1;
1017 return;
1020 // crap. this is buggy.
1021 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1024 static void set_unset_to_zero(struct expression *symbol, struct symbol *type, struct member_set *member_set)
1026 struct expression *deref, *assign;
1027 struct symbol *member, *member_type;
1028 int member_idx;
1030 member_idx = 0;
1031 FOR_EACH_PTR(type->symbol_list, member) {
1032 if (!member->ident || member_set[member_idx].set) {
1033 member_idx++;
1034 continue;
1036 member_type = get_real_base_type(member);
1037 if (!member_type || member_type->type == SYM_ARRAY) {
1038 member_idx++;
1039 continue;
1041 /* TODO: this should be handled recursively and not ignored */
1042 if (member_type->type == SYM_STRUCT || member_type->type == SYM_UNION) {
1043 member_idx++;
1044 continue;
1046 deref = member_expression(symbol, '.', member->ident);
1047 assign = assign_expression(deref, zero_expr());
1048 __split_expr(assign);
1049 member_idx++;
1050 } END_FOR_EACH_PTR(member);
1054 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1056 struct expression *deref, *assign, *tmp;
1057 struct symbol *struct_type, *type;
1058 struct ident *member;
1059 int member_idx;
1060 struct member_set *member_set;
1062 struct_type = get_type(symbol);
1063 if (!struct_type ||
1064 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1065 return;
1067 member_set = alloc_member_set(struct_type);
1069 member_idx = 0;
1070 FOR_EACH_PTR(members, tmp) {
1071 member = number_to_member(symbol, member_idx);
1072 while (tmp->type == EXPR_IDENTIFIER) {
1073 member = tmp->expr_ident;
1074 member_idx = member_to_number(symbol, member);
1075 tmp = tmp->ident_expression;
1077 mark_member_as_set(struct_type, member_set, member);
1078 member_idx++;
1079 deref = member_expression(symbol, '.', member);
1080 if (tmp->type == EXPR_INITIALIZER) {
1081 type = get_type(deref);
1082 if (type && type->type == SYM_ARRAY)
1083 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1084 else
1085 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1086 } else {
1087 assign = assign_expression(deref, tmp);
1088 fake_cb(assign);
1090 } END_FOR_EACH_PTR(tmp);
1092 set_unset_to_zero(symbol, struct_type, member_set);
1095 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1097 fake_member_assigns_helper(symbol_expression(sym),
1098 sym->initializer->expr_list, fake_cb);
1101 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1103 struct expression *offset, *binop, *assign, *tmp;
1104 struct symbol *type;
1105 int idx;
1107 idx = 0;
1108 FOR_EACH_PTR(expr_list, tmp) {
1109 if (tmp->type == EXPR_INDEX) {
1110 if (tmp->idx_from != tmp->idx_to)
1111 return;
1112 idx = tmp->idx_from;
1113 if (!tmp->idx_expression)
1114 goto next;
1115 tmp = tmp->idx_expression;
1117 offset = value_expr(idx);
1118 binop = array_element_expression(array, offset);
1119 if (tmp->type == EXPR_INITIALIZER) {
1120 type = get_type(binop);
1121 if (type && type->type == SYM_ARRAY)
1122 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1123 else
1124 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1125 } else {
1126 assign = assign_expression(binop, tmp);
1127 fake_cb(assign);
1129 next:
1130 idx++;
1131 } END_FOR_EACH_PTR(tmp);
1134 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1136 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1139 static void fake_assign_expr(struct symbol *sym)
1141 struct expression *assign, *symbol;
1143 symbol = symbol_expression(sym);
1144 assign = assign_expression(symbol, sym->initializer);
1145 __split_expr(assign);
1148 static void call_split_expr(struct expression *expr)
1150 __split_expr(expr);
1153 static void do_initializer_stuff(struct symbol *sym)
1155 if (!sym->initializer)
1156 return;
1158 if (sym->initializer->type == EXPR_INITIALIZER) {
1159 if (get_real_base_type(sym)->type == SYM_ARRAY)
1160 fake_element_assigns(sym, call_split_expr);
1161 else
1162 fake_member_assigns(sym, call_split_expr);
1163 } else {
1164 fake_assign_expr(sym);
1168 static void split_declaration(struct symbol_list *sym_list)
1170 struct symbol *sym;
1172 FOR_EACH_PTR(sym_list, sym) {
1173 __pass_to_client(sym, DECLARATION_HOOK);
1174 do_initializer_stuff(sym);
1175 split_sym(sym);
1176 } END_FOR_EACH_PTR(sym);
1179 static void call_global_assign_hooks(struct expression *assign)
1181 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1184 static void fake_global_assign(struct symbol *sym)
1186 struct expression *assign, *symbol;
1188 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1189 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1190 fake_element_assigns(sym, call_global_assign_hooks);
1191 } else if (sym->initializer) {
1192 symbol = symbol_expression(sym);
1193 assign = assign_expression(symbol, sym->initializer);
1194 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1195 } else {
1196 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1198 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1199 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1200 fake_member_assigns(sym, call_global_assign_hooks);
1201 } else if (sym->initializer) {
1202 symbol = symbol_expression(sym);
1203 assign = assign_expression(symbol, sym->initializer);
1204 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1205 } else {
1206 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1208 } else {
1209 symbol = symbol_expression(sym);
1210 if (sym->initializer)
1211 assign = assign_expression(symbol, sym->initializer);
1212 else
1213 assign = assign_expression(symbol, zero_expr());
1214 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1218 static void start_function_definition(struct symbol *sym)
1220 __in_function_def = 1;
1221 __pass_to_client(sym, FUNC_DEF_HOOK);
1222 __in_function_def = 0;
1223 __pass_to_client(sym, AFTER_DEF_HOOK);
1227 static void split_function(struct symbol *sym)
1229 struct symbol *base_type = get_base_type(sym);
1231 if (!base_type->stmt && !base_type->inline_stmt)
1232 return;
1234 gettimeofday(&fn_start_time, NULL);
1235 cur_func_sym = sym;
1236 if (sym->ident)
1237 cur_func = sym->ident->name;
1238 __smatch_lineno = sym->pos.line;
1239 loop_count = 0;
1240 last_goto_statement_handled = 0;
1241 sm_debug("new function: %s\n", cur_func);
1242 __stree_id = 0;
1243 if (option_two_passes) {
1244 __unnullify_path();
1245 loop_num = 0;
1246 final_pass = 0;
1247 start_function_definition(sym);
1248 __split_stmt(base_type->stmt);
1249 __split_stmt(base_type->inline_stmt);
1250 nullify_path();
1252 __unnullify_path();
1253 loop_num = 0;
1254 final_pass = 1;
1255 start_function_definition(sym);
1256 __split_stmt(base_type->stmt);
1257 __split_stmt(base_type->inline_stmt);
1258 __pass_to_client(sym, END_FUNC_HOOK);
1259 __pass_to_client(sym, AFTER_FUNC_HOOK);
1261 clear_all_states();
1262 cur_func_sym = NULL;
1263 cur_func = NULL;
1264 free_data_info_allocs();
1265 free_expression_stack(&switch_expr_stack);
1266 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1267 __bail_on_rest_of_function = 0;
1270 static void parse_inline(struct expression *call)
1272 struct symbol *base_type;
1273 int loop_num_bak = loop_num;
1274 int final_pass_bak = final_pass;
1275 char *cur_func_bak = cur_func;
1276 struct statement_list *big_statement_stack_bak = big_statement_stack;
1277 struct expression_list *big_expression_stack_bak = big_expression_stack;
1278 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1279 struct symbol *cur_func_sym_bak = cur_func_sym;
1281 __pass_to_client(call, INLINE_FN_START);
1282 final_pass = 0; /* don't print anything */
1283 __inline_fn = call;
1285 base_type = get_base_type(call->fn->symbol);
1286 cur_func_sym = call->fn->symbol;
1287 if (call->fn->symbol->ident)
1288 cur_func = call->fn->symbol->ident->name;
1289 else
1290 cur_func = NULL;
1291 set_position(call->fn->symbol->pos);
1293 save_all_states();
1294 big_statement_stack = NULL;
1295 big_expression_stack = NULL;
1296 switch_expr_stack = NULL;
1298 sm_debug("inline function: %s\n", cur_func);
1299 __unnullify_path();
1300 loop_num = 0;
1301 start_function_definition(call->fn->symbol);
1302 __split_stmt(base_type->stmt);
1303 __split_stmt(base_type->inline_stmt);
1304 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1305 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1307 free_expression_stack(&switch_expr_stack);
1308 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1309 nullify_path();
1310 free_goto_stack();
1312 loop_num = loop_num_bak;
1313 final_pass = final_pass_bak;
1314 cur_func_sym = cur_func_sym_bak;
1315 cur_func = cur_func_bak;
1316 big_statement_stack = big_statement_stack_bak;
1317 big_expression_stack = big_expression_stack_bak;
1318 switch_expr_stack = switch_expr_stack_bak;
1320 restore_all_states();
1321 set_position(call->pos);
1322 __inline_fn = NULL;
1323 __pass_to_client(call, INLINE_FN_END);
1326 static struct symbol_list *inlines_called;
1327 static void add_inline_function(struct symbol *sym)
1329 static struct symbol_list *already_added;
1330 struct symbol *tmp;
1332 FOR_EACH_PTR(already_added, tmp) {
1333 if (tmp == sym)
1334 return;
1335 } END_FOR_EACH_PTR(tmp);
1337 add_ptr_list(&already_added, sym);
1338 add_ptr_list(&inlines_called, sym);
1341 static void process_inlines(void)
1343 struct symbol *tmp;
1345 FOR_EACH_PTR(inlines_called, tmp) {
1346 split_function(tmp);
1347 } END_FOR_EACH_PTR(tmp);
1348 free_ptr_list(&inlines_called);
1351 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1353 struct symbol *sym;
1355 FOR_EACH_PTR_REVERSE(big_list, sym) {
1356 if (!sym->scope)
1357 continue;
1358 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1359 return sym;
1360 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1361 return sym;
1362 } END_FOR_EACH_PTR_REVERSE(sym);
1364 return NULL;
1367 static void split_inlines_in_scope(struct symbol *sym)
1369 struct symbol *base;
1370 struct symbol_list *scope_list;
1371 int stream;
1373 scope_list = sym->scope->symbols;
1374 stream = sym->pos.stream;
1376 /* find the last static symbol in the file */
1377 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1378 if (sym->pos.stream != stream)
1379 continue;
1380 if (sym->type != SYM_NODE)
1381 continue;
1382 base = get_base_type(sym);
1383 if (!base)
1384 continue;
1385 if (base->type != SYM_FN)
1386 continue;
1387 if (!base->inline_stmt)
1388 continue;
1389 add_inline_function(sym);
1390 } END_FOR_EACH_PTR_REVERSE(sym);
1392 process_inlines();
1395 static void split_inlines(struct symbol_list *sym_list)
1397 struct symbol *sym;
1399 sym = get_last_scoped_symbol(sym_list, 0);
1400 if (sym)
1401 split_inlines_in_scope(sym);
1402 sym = get_last_scoped_symbol(sym_list, 1);
1403 if (sym)
1404 split_inlines_in_scope(sym);
1407 static struct stree *clone_estates_perm(struct stree *orig)
1409 struct stree *ret = NULL;
1410 struct sm_state *tmp;
1412 FOR_EACH_SM(orig, tmp) {
1413 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1414 } END_FOR_EACH_SM(tmp);
1416 return ret;
1419 static void split_functions(struct symbol_list *sym_list)
1421 struct symbol *sym;
1423 __unnullify_path();
1424 FOR_EACH_PTR(sym_list, sym) {
1425 set_position(sym->pos);
1426 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1427 __pass_to_client(sym, BASE_HOOK);
1428 fake_global_assign(sym);
1430 } END_FOR_EACH_PTR(sym);
1431 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1432 nullify_path();
1434 FOR_EACH_PTR(sym_list, sym) {
1435 set_position(sym->pos);
1436 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1437 split_function(sym);
1438 process_inlines();
1440 } END_FOR_EACH_PTR(sym);
1441 split_inlines(sym_list);
1442 __pass_to_client(sym_list, END_FILE_HOOK);
1445 void smatch(int argc, char **argv)
1448 struct string_list *filelist = NULL;
1449 struct symbol_list *sym_list;
1451 if (argc < 2) {
1452 printf("Usage: smatch [--debug] <filename.c>\n");
1453 exit(1);
1455 sparse_initialize(argc, argv, &filelist);
1456 set_valid_ptr_max();
1457 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1458 if (option_file_output) {
1459 char buf[256];
1461 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1462 sm_outfd = fopen(buf, "w");
1463 if (!sm_outfd) {
1464 printf("Error: Cannot open %s\n", base_file);
1465 exit(1);
1468 sym_list = sparse_keep_tokens(base_file);
1469 split_functions(sym_list);
1470 } END_FOR_EACH_PTR_NOTAG(base_file);