ranges: problems parsing "s32min-(-1)[<=p2]"
[smatch.git] / smatch_flow.c
blob24a057481a9fccb1b73de73e226a9cc89eec89e7
1 /*
2 * sparse/smatch_flow.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #define _GNU_SOURCE 1
11 #include <unistd.h>
12 #include <stdio.h>
13 #include "token.h"
14 #include "scope.h"
15 #include "smatch.h"
16 #include "smatch_expression_stacks.h"
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
20 int final_pass;
21 int __inline_call;
22 struct expression *__inline_fn;
24 static int __smatch_lineno = 0;
26 static char *base_file;
27 static const char *filename;
28 static char *pathname;
29 static char *full_filename;
30 static char *cur_func;
31 static int loop_count;
32 int __expr_stmt_count;
33 static struct expression_list *switch_expr_stack = NULL;
34 static struct expression_list *post_op_stack = NULL;
36 struct expression_list *big_expression_stack;
37 struct statement_list *big_statement_stack;
38 int __in_pre_condition = 0;
39 int __bail_on_rest_of_function = 0;
40 char *get_function(void) { return cur_func; }
41 int get_lineno(void) { return __smatch_lineno; }
42 int inside_loop(void) { return !!loop_count; }
43 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
44 int in_expression_statement(void) { return !!__expr_stmt_count; }
46 static void split_symlist(struct symbol_list *sym_list);
47 static void split_declaration(struct symbol_list *sym_list);
48 static void split_expr_list(struct expression_list *expr_list);
49 static void add_inline_function(struct symbol *sym);
50 static void parse_inline(struct expression *expr);
52 int option_assume_loops = 0;
53 int option_known_conditions = 0;
54 int option_two_passes = 0;
55 struct symbol *cur_func_sym = NULL;
57 const char *get_filename(void)
59 if (option_info)
60 return base_file;
61 if (option_full_path)
62 return full_filename;
63 return filename;
66 const char *get_base_file(void)
68 return base_file;
71 static void set_position(struct position pos)
73 int len;
74 static int prev_stream = -1;
76 if (pos.stream == 0 && pos.line == 0)
77 return;
79 __smatch_lineno = pos.line;
81 if (pos.stream == prev_stream)
82 return;
84 filename = stream_name(pos.stream);
86 free(full_filename);
87 pathname = getcwd(NULL, 0);
88 if (pathname) {
89 len = strlen(pathname) + 1 + strlen(filename) + 1;
90 full_filename = malloc(len);
91 snprintf(full_filename, len, "%s/%s", pathname, filename);
92 } else {
93 full_filename = alloc_string(filename);
95 free(pathname);
98 static int is_inline_func(struct expression *expr)
100 if (expr->type != EXPR_SYMBOL || !expr->symbol)
101 return 0;
102 if (expr->symbol->ctype.modifiers & MOD_INLINE)
103 return 1;
104 return 0;
107 static int is_noreturn_func(struct expression *expr)
109 if (expr->type != EXPR_SYMBOL || !expr->symbol)
110 return 0;
111 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
112 return 1;
113 return 0;
116 int inlinable(struct expression *expr)
118 struct symbol *sym;
120 if (__inline_fn) /* don't nest */
121 return 0;
123 if (expr->type != EXPR_SYMBOL || !expr->symbol)
124 return 0;
125 sym = get_base_type(expr->symbol);
126 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
127 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
128 return 1;
129 return 0;
131 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
132 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
133 return 1;
134 return 0;
136 return 0;
139 void __process_post_op_stack(void)
141 struct expression *expr;
143 FOR_EACH_PTR(post_op_stack, expr) {
144 __pass_to_client(expr, OP_HOOK);
145 } END_FOR_EACH_PTR(expr);
147 __free_ptr_list((struct ptr_list **)&post_op_stack);
150 void __split_expr(struct expression *expr)
152 if (!expr)
153 return;
155 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
157 push_expression(&big_expression_stack, expr);
158 set_position(expr->pos);
159 __pass_to_client(expr, EXPR_HOOK);
161 switch (expr->type) {
162 case EXPR_PREOP:
163 if (expr->op == '*')
164 __pass_to_client(expr, DEREF_HOOK);
165 __split_expr(expr->unop);
166 __pass_to_client(expr, OP_HOOK);
167 break;
168 case EXPR_POSTOP:
169 __split_expr(expr->unop);
170 push_expression(&post_op_stack, expr);
171 break;
172 case EXPR_STATEMENT:
173 __expr_stmt_count++;
174 __split_stmt(expr->statement);
175 __expr_stmt_count--;
176 break;
177 case EXPR_LOGICAL:
178 case EXPR_COMPARE:
179 __pass_to_client(expr, LOGIC_HOOK);
180 __handle_logic(expr);
181 break;
182 case EXPR_BINOP:
183 __pass_to_client(expr, BINOP_HOOK);
184 case EXPR_COMMA:
185 __split_expr(expr->left);
186 __process_post_op_stack();
187 __split_expr(expr->right);
188 break;
189 case EXPR_ASSIGNMENT: {
190 struct expression *tmp;
192 if (!expr->right)
193 break;
195 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
197 /* foo = !bar() */
198 if (__handle_condition_assigns(expr))
199 break;
200 /* foo = (x < 5 ? foo : 5); */
201 if (__handle_select_assigns(expr))
202 break;
203 /* foo = ({frob(); frob(); frob(); 1;}) */
204 if (__handle_expr_statement_assigns(expr))
205 break;
207 __split_expr(expr->right);
208 __pass_to_client(expr, ASSIGNMENT_HOOK);
209 tmp = strip_expr(expr->right);
210 if (tmp->type == EXPR_CALL)
211 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
212 if (get_macro_name(tmp->pos) &&
213 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
214 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
215 __split_expr(expr->left);
216 break;
218 case EXPR_DEREF:
219 __pass_to_client(expr, DEREF_HOOK);
220 __split_expr(expr->deref);
221 break;
222 case EXPR_SLICE:
223 __split_expr(expr->base);
224 break;
225 case EXPR_CAST:
226 case EXPR_FORCE_CAST:
227 __pass_to_client(expr, CAST_HOOK);
228 __split_expr(expr->cast_expression);
229 break;
230 case EXPR_SIZEOF:
231 if (expr->cast_expression)
232 __pass_to_client(strip_parens(expr->cast_expression),
233 SIZEOF_HOOK);
234 break;
235 case EXPR_OFFSETOF:
236 case EXPR_ALIGNOF:
237 evaluate_expression(expr);
238 break;
239 case EXPR_CONDITIONAL:
240 case EXPR_SELECT:
241 if (known_condition_true(expr->conditional)) {
242 __split_expr(expr->cond_true);
243 break;
245 if (known_condition_false(expr->conditional)) {
246 __split_expr(expr->cond_false);
247 break;
249 __pass_to_client(expr, SELECT_HOOK);
250 __split_whole_condition(expr->conditional);
251 __split_expr(expr->cond_true);
252 __push_true_states();
253 __use_false_states();
254 __split_expr(expr->cond_false);
255 __merge_true_states();
256 break;
257 case EXPR_CALL:
258 if (sym_name_is("__builtin_constant_p", expr->fn))
259 break;
260 split_expr_list(expr->args);
261 __split_expr(expr->fn);
262 if (is_inline_func(expr->fn))
263 add_inline_function(expr->fn->symbol);
264 if (inlinable(expr->fn))
265 __inline_call = 1;
266 __process_post_op_stack();
267 __pass_to_client(expr, FUNCTION_CALL_HOOK);
268 __inline_call = 0;
269 if (inlinable(expr->fn)) {
270 parse_inline(expr);
272 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
273 if (is_noreturn_func(expr->fn))
274 nullify_path();
275 break;
276 case EXPR_INITIALIZER:
277 split_expr_list(expr->expr_list);
278 break;
279 case EXPR_IDENTIFIER:
280 __split_expr(expr->ident_expression);
281 break;
282 case EXPR_INDEX:
283 __split_expr(expr->idx_expression);
284 break;
285 case EXPR_POS:
286 __split_expr(expr->init_expr);
287 break;
288 case EXPR_SYMBOL:
289 __pass_to_client(expr, SYM_HOOK);
290 break;
291 case EXPR_STRING:
292 __pass_to_client(expr, STRING_HOOK);
293 break;
294 default:
295 break;
297 pop_expression(&big_expression_stack);
300 static int is_forever_loop(struct statement *stmt)
302 struct expression *expr;
304 expr = strip_expr(stmt->iterator_pre_condition);
305 if (!expr)
306 expr = stmt->iterator_post_condition;
307 if (!expr) {
308 /* this is a for(;;) loop... */
309 return 1;
312 if (expr->type == EXPR_VALUE && expr->value == 1)
313 return 1;
315 return 0;
318 static int loop_num;
319 static char *get_loop_name(int num)
321 char buf[256];
323 snprintf(buf, 255, "-loop%d", num);
324 buf[255] = '\0';
325 return alloc_sname(buf);
329 * Pre Loops are while and for loops.
331 static void handle_pre_loop(struct statement *stmt)
333 int once_through; /* we go through the loop at least once */
334 struct sm_state *extra_sm = NULL;
335 int unchanged = 0;
336 char *loop_name;
337 struct state_list *slist = NULL;
338 struct sm_state *sm = NULL;
340 loop_name = get_loop_name(loop_num);
341 loop_num++;
343 __split_stmt(stmt->iterator_pre_statement);
345 once_through = implied_condition_true(stmt->iterator_pre_condition);
347 loop_count++;
348 __push_continues();
349 __push_breaks();
351 __merge_gotos(loop_name);
353 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
354 __in_pre_condition++;
355 __pass_to_client(stmt, PRELOOP_HOOK);
356 __split_whole_condition(stmt->iterator_pre_condition);
357 __in_pre_condition--;
358 FOR_EACH_PTR(slist, sm) {
359 set_state(sm->owner, sm->name, sm->sym, sm->state);
360 } END_FOR_EACH_PTR(sm);
361 free_slist(&slist);
362 if (extra_sm)
363 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
365 if (option_assume_loops)
366 once_through = 1;
368 __split_stmt(stmt->iterator_statement);
369 __warn_on_silly_pre_loops();
370 if (is_forever_loop(stmt)) {
371 struct state_list *slist;
373 __save_gotos(loop_name);
375 __push_fake_cur_slist();
376 __split_stmt(stmt->iterator_post_statement);
377 slist = __pop_fake_cur_slist();
379 __discard_continues();
380 __discard_false_states();
381 __use_breaks();
383 if (!__path_is_null())
384 __merge_slist_into_cur(slist);
385 free_slist(&slist);
386 } else {
387 __merge_continues();
388 unchanged = __iterator_unchanged(extra_sm);
389 __split_stmt(stmt->iterator_post_statement);
390 __save_gotos(loop_name);
391 __split_whole_condition(stmt->iterator_pre_condition);
392 nullify_path();
393 __merge_false_states();
394 if (once_through)
395 __discard_false_states();
396 else
397 __merge_false_states();
399 if (extra_sm && unchanged)
400 __extra_pre_loop_hook_after(extra_sm,
401 stmt->iterator_post_statement,
402 stmt->iterator_pre_condition);
403 __merge_breaks();
405 loop_count--;
409 * Post loops are do {} while();
411 static void handle_post_loop(struct statement *stmt)
413 char *loop_name;
415 loop_name = get_loop_name(loop_num);
416 loop_num++;
417 loop_count++;
419 __push_continues();
420 __push_breaks();
421 __merge_gotos(loop_name);
422 __split_stmt(stmt->iterator_statement);
423 __merge_continues();
424 if (!is_zero(stmt->iterator_post_condition))
425 __save_gotos(loop_name);
427 if (is_forever_loop(stmt)) {
428 __use_breaks();
429 } else {
430 __split_whole_condition(stmt->iterator_post_condition);
431 __use_false_states();
432 __merge_breaks();
434 loop_count--;
437 static int empty_statement(struct statement *stmt)
439 if (!stmt)
440 return 0;
441 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
442 return 1;
443 return 0;
446 static int last_stmt_on_same_line()
448 struct statement *stmt;
449 int i = 0;
451 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
452 if (!i++)
453 continue;
454 if (stmt->pos.line == get_lineno())
455 return 1;
456 return 0;
457 } END_FOR_EACH_PTR_REVERSE(stmt);
458 return 0;
461 static struct statement *last_stmt;
462 static int is_last_stmt(struct statement *stmt)
464 if (stmt == last_stmt)
465 return 1;
466 return 0;
469 static void print_unreached_initializers(struct symbol_list *sym_list)
471 struct symbol *sym;
473 FOR_EACH_PTR(sym_list, sym) {
474 if (sym->initializer)
475 sm_msg("info: '%s' is not actually initialized (unreached code).",
476 (sym->ident ? sym->ident->name : "this variable"));
477 } END_FOR_EACH_PTR(sym);
480 static void print_unreached(struct statement *stmt)
482 static int print = 1;
484 if (__inline_fn)
485 return;
487 if (!__path_is_null()) {
488 print = 1;
489 return;
491 if (!print)
492 return;
494 switch (stmt->type) {
495 case STMT_COMPOUND: /* after a switch before a case stmt */
496 case STMT_RANGE:
497 case STMT_CASE:
498 case STMT_LABEL:
499 return;
500 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
501 print_unreached_initializers(stmt->declaration);
502 return;
503 case STMT_RETURN: /* gcc complains if you don't have a return statement */
504 if (is_last_stmt(stmt))
505 return;
506 break;
507 case STMT_GOTO:
508 if (!option_spammy)
509 return;
510 break;
511 default:
512 break;
514 if (!option_spammy && empty_statement(stmt))
515 return;
516 sm_msg("info: ignoring unreachable code.");
517 print = 0;
520 static void split_asm_constraints(struct expression_list *expr_list)
522 struct expression *expr;
523 int state = 0;
525 FOR_EACH_PTR(expr_list, expr) {
526 switch (state) {
527 case 0: /* identifier */
528 case 1: /* constraint */
529 state++;
530 continue;
531 case 2: /* expression */
532 state = 0;
533 __split_expr(expr);
534 continue;
536 } END_FOR_EACH_PTR(expr);
539 static int is_case_val(struct statement *stmt, sval_t sval)
541 sval_t case_sval;
543 if (stmt->type != STMT_CASE)
544 return 0;
545 if (!stmt->case_expression) {
546 __set_default();
547 return 1;
549 if (!get_value(stmt->case_expression, &case_sval))
550 return 0;
551 if (case_sval.value == sval.value)
552 return 1;
553 return 0;
556 static void split_known_switch(struct statement *stmt, sval_t sval)
558 struct statement *tmp;
560 __split_expr(stmt->switch_expression);
562 push_expression(&switch_expr_stack, stmt->switch_expression);
563 __save_switch_states(top_expression(switch_expr_stack));
564 nullify_path();
565 __push_default();
566 __push_breaks();
568 stmt = stmt->switch_statement;
570 if (!last_stmt)
571 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
573 __push_scope_hooks();
574 FOR_EACH_PTR(stmt->stmts, tmp) {
575 __smatch_lineno = tmp->pos.line;
576 if (is_case_val(tmp, sval)) {
577 __merge_switches(top_expression(switch_expr_stack),
578 stmt->case_expression);
579 __pass_case_to_client(top_expression(switch_expr_stack),
580 stmt->case_expression);
582 if (__path_is_null())
583 continue;
584 __split_stmt(tmp);
585 if (__path_is_null()) {
586 __set_default();
587 goto out;
589 } END_FOR_EACH_PTR(tmp);
590 out:
591 __call_scope_hooks();
592 if (!__pop_default())
593 __merge_switches(top_expression(switch_expr_stack),
594 NULL);
595 __discard_switches();
596 __merge_breaks();
597 pop_expression(&switch_expr_stack);
600 void __split_stmt(struct statement *stmt)
602 sval_t sval;
604 if (!stmt)
605 goto out;
607 if (out_of_memory() || __bail_on_rest_of_function) {
608 static char *printed = NULL;
610 if (printed != cur_func)
611 sm_msg("Function too hairy. Giving up.");
612 final_pass = 0; /* turn off sm_msg() from here */
613 printed = cur_func;
614 return;
617 add_ptr_list(&big_statement_stack, stmt);
618 free_expression_stack(&big_expression_stack);
619 set_position(stmt->pos);
620 print_unreached(stmt);
621 __pass_to_client(stmt, STMT_HOOK);
623 switch (stmt->type) {
624 case STMT_DECLARATION:
625 split_declaration(stmt->declaration);
626 break;
627 case STMT_RETURN:
628 __split_expr(stmt->ret_value);
629 __pass_to_client(stmt->ret_value, RETURN_HOOK);
630 nullify_path();
631 break;
632 case STMT_EXPRESSION:
633 __split_expr(stmt->expression);
634 break;
635 case STMT_COMPOUND: {
636 struct statement *tmp;
638 if (!last_stmt)
639 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
640 __push_scope_hooks();
641 FOR_EACH_PTR(stmt->stmts, tmp) {
642 __split_stmt(tmp);
643 } END_FOR_EACH_PTR(tmp);
644 __call_scope_hooks();
645 break;
647 case STMT_IF:
648 if (known_condition_true(stmt->if_conditional)) {
649 __split_stmt(stmt->if_true);
650 break;
652 if (known_condition_false(stmt->if_conditional)) {
653 __split_stmt(stmt->if_false);
654 break;
656 if (option_known_conditions &&
657 implied_condition_true(stmt->if_conditional)) {
658 sm_info("this condition is true.");
659 __split_stmt(stmt->if_true);
660 break;
662 if (option_known_conditions &&
663 implied_condition_false(stmt->if_conditional)) {
664 sm_info("this condition is false.");
665 __split_stmt(stmt->if_false);
666 break;
668 __split_whole_condition(stmt->if_conditional);
669 __split_stmt(stmt->if_true);
670 if (empty_statement(stmt->if_true) &&
671 last_stmt_on_same_line() &&
672 !get_macro_name(stmt->if_true->pos))
673 sm_msg("warn: if();");
674 __push_true_states();
675 __use_false_states();
676 __split_stmt(stmt->if_false);
677 __merge_true_states();
678 break;
679 case STMT_ITERATOR:
680 if (stmt->iterator_pre_condition)
681 handle_pre_loop(stmt);
682 else if (stmt->iterator_post_condition)
683 handle_post_loop(stmt);
684 else {
685 // these are for(;;) type loops.
686 handle_pre_loop(stmt);
688 break;
689 case STMT_SWITCH:
690 if (get_value(stmt->switch_expression, &sval)) {
691 split_known_switch(stmt, sval);
692 break;
694 __split_expr(stmt->switch_expression);
695 push_expression(&switch_expr_stack, stmt->switch_expression);
696 __save_switch_states(top_expression(switch_expr_stack));
697 nullify_path();
698 __push_default();
699 __push_breaks();
700 __split_stmt(stmt->switch_statement);
701 if (!__pop_default())
702 __merge_switches(top_expression(switch_expr_stack),
703 NULL);
704 __discard_switches();
705 __merge_breaks();
706 pop_expression(&switch_expr_stack);
707 break;
708 case STMT_CASE:
709 __merge_switches(top_expression(switch_expr_stack),
710 stmt->case_expression);
711 __pass_case_to_client(top_expression(switch_expr_stack),
712 stmt->case_expression);
713 if (!stmt->case_expression)
714 __set_default();
715 __split_expr(stmt->case_expression);
716 __split_expr(stmt->case_to);
717 __split_stmt(stmt->case_statement);
718 break;
719 case STMT_LABEL:
720 if (stmt->label_identifier &&
721 stmt->label_identifier->type == SYM_LABEL &&
722 stmt->label_identifier->ident) {
723 loop_count = 1000000;
724 __merge_gotos(stmt->label_identifier->ident->name);
726 __split_stmt(stmt->label_statement);
727 break;
728 case STMT_GOTO:
729 __split_expr(stmt->goto_expression);
730 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
731 if (!strcmp(stmt->goto_label->ident->name, "break")) {
732 __process_breaks();
733 } else if (!strcmp(stmt->goto_label->ident->name,
734 "continue")) {
735 __process_continues();
737 } else if (stmt->goto_label &&
738 stmt->goto_label->type == SYM_LABEL &&
739 stmt->goto_label->ident) {
740 __save_gotos(stmt->goto_label->ident->name);
742 nullify_path();
743 break;
744 case STMT_NONE:
745 break;
746 case STMT_ASM:
747 __pass_to_client(stmt, ASM_HOOK);
748 __split_expr(stmt->asm_string);
749 split_asm_constraints(stmt->asm_outputs);
750 split_asm_constraints(stmt->asm_inputs);
751 split_asm_constraints(stmt->asm_clobbers);
752 break;
753 case STMT_CONTEXT:
754 break;
755 case STMT_RANGE:
756 __split_expr(stmt->range_expression);
757 __split_expr(stmt->range_low);
758 __split_expr(stmt->range_high);
759 break;
761 out:
762 __process_post_op_stack();
765 static void split_expr_list(struct expression_list *expr_list)
767 struct expression *expr;
769 FOR_EACH_PTR(expr_list, expr) {
770 __split_expr(expr);
771 __process_post_op_stack();
772 } END_FOR_EACH_PTR(expr);
775 static void split_sym(struct symbol *sym)
777 if (!sym)
778 return;
779 if (!(sym->namespace & NS_SYMBOL))
780 return;
782 __split_stmt(sym->stmt);
783 __split_expr(sym->array_size);
784 split_symlist(sym->arguments);
785 split_symlist(sym->symbol_list);
786 __split_stmt(sym->inline_stmt);
787 split_symlist(sym->inline_symbol_list);
790 static void split_symlist(struct symbol_list *sym_list)
792 struct symbol *sym;
794 FOR_EACH_PTR(sym_list, sym) {
795 split_sym(sym);
796 } END_FOR_EACH_PTR(sym);
799 typedef void (fake_cb)(struct expression *expr);
801 static int member_to_number(struct expression *expr, struct ident *member)
803 struct symbol *type, *tmp;
804 char *name;
805 int i;
807 if (!member)
808 return -1;
809 name = member->name;
811 type = get_type(expr);
812 if (!type || type->type != SYM_STRUCT)
813 return -1;
815 i = -1;
816 FOR_EACH_PTR(type->symbol_list, tmp) {
817 i++;
818 if (!tmp->ident)
819 continue;
820 if (strcmp(name, tmp->ident->name) == 0)
821 return i;
822 } END_FOR_EACH_PTR(tmp);
823 return -1;
826 static struct ident *number_to_member(struct expression *expr, int num)
828 struct symbol *type, *member;
829 int i = 0;
831 type = get_type(expr);
832 if (!type || type->type != SYM_STRUCT)
833 return NULL;
835 FOR_EACH_PTR(type->symbol_list, member) {
836 if (i == num)
837 return member->ident;
838 i++;
839 } END_FOR_EACH_PTR(member);
840 return NULL;
843 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
845 struct expression *deref, *assign, *tmp;
846 struct symbol *type;
847 struct ident *member;
848 int member_idx = 0;
850 type = get_type(symbol);
851 if (!type || type->type != SYM_STRUCT)
852 return;
854 FOR_EACH_PTR(members, tmp) {
855 if (tmp->type == EXPR_IDENTIFIER) {
856 member = tmp->expr_ident;
857 member_idx = member_to_number(symbol, member);
858 tmp = tmp->ident_expression;
859 } else {
860 member = number_to_member(symbol, member_idx);
862 member_idx++;
863 deref = member_expression(symbol, '.', member);
864 if (tmp->type == EXPR_INITIALIZER) {
865 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
866 } else {
867 assign = assign_expression(deref, tmp);
868 fake_cb(assign);
870 } END_FOR_EACH_PTR(tmp);
873 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
875 fake_member_assigns_helper(symbol_expression(sym),
876 sym->initializer->expr_list, fake_cb);
879 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
881 struct expression *array, *offset, *binop, *assign, *tmp;
882 int idx;
884 array = symbol_expression(sym);
885 idx = 0;
886 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
887 if (tmp->type == EXPR_INDEX) {
888 if (tmp->idx_from != tmp->idx_to)
889 return;
890 idx = tmp->idx_from;
891 if (!tmp->idx_expression)
892 goto next;
893 tmp = tmp->idx_expression;
895 offset = value_expr(idx);
896 binop = array_element_expression(array, offset);
897 if (tmp->type == EXPR_INITIALIZER) {
898 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
899 } else {
900 assign = assign_expression(binop, tmp);
901 fake_cb(assign);
903 next:
904 idx++;
905 } END_FOR_EACH_PTR(tmp);
908 static void fake_assign_expr(struct symbol *sym)
910 struct expression *assign, *symbol;
912 symbol = symbol_expression(sym);
913 assign = assign_expression(symbol, sym->initializer);
914 __split_expr(assign);
917 static void call_split_expr(struct expression *expr)
919 __split_expr(expr);
922 static void do_initializer_stuff(struct symbol *sym)
924 if (!sym->initializer)
925 return;
927 if (sym->initializer->type == EXPR_INITIALIZER) {
928 if (get_real_base_type(sym)->type == SYM_ARRAY)
929 fake_element_assigns(sym, call_split_expr);
930 else
931 fake_member_assigns(sym, call_split_expr);
932 } else {
933 fake_assign_expr(sym);
937 static void split_declaration(struct symbol_list *sym_list)
939 struct symbol *sym;
941 FOR_EACH_PTR(sym_list, sym) {
942 __pass_to_client(sym, DECLARATION_HOOK);
943 do_initializer_stuff(sym);
944 split_sym(sym);
945 } END_FOR_EACH_PTR(sym);
948 static void call_global_assign_hooks(struct expression *assign)
950 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
953 static void fake_global_assign(struct symbol *sym)
955 struct expression *assign, *symbol;
957 if (!sym->initializer)
958 return;
959 if (sym->initializer->type == EXPR_INITIALIZER) {
960 if (get_real_base_type(sym)->type == SYM_ARRAY)
961 fake_element_assigns(sym, call_global_assign_hooks);
962 else
963 fake_member_assigns(sym, call_global_assign_hooks);
964 } else {
965 symbol = symbol_expression(sym);
966 assign = assign_expression(symbol, sym->initializer);
967 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
971 static void split_function(struct symbol *sym)
973 struct symbol *base_type = get_base_type(sym);
975 cur_func_sym = sym;
976 if (sym->ident)
977 cur_func = sym->ident->name;
978 __smatch_lineno = sym->pos.line;
979 last_stmt = NULL;
980 loop_count = 0;
981 sm_debug("new function: %s\n", cur_func);
982 __slist_id = 0;
983 if (option_two_passes) {
984 __unnullify_path();
985 loop_num = 0;
986 final_pass = 0;
987 __pass_to_client(sym, FUNC_DEF_HOOK);
988 __pass_to_client(sym, AFTER_DEF_HOOK);
989 __split_stmt(base_type->stmt);
990 __split_stmt(base_type->inline_stmt);
991 nullify_path();
993 __unnullify_path();
994 loop_num = 0;
995 final_pass = 1;
996 __pass_to_client(sym, FUNC_DEF_HOOK);
997 __pass_to_client(sym, AFTER_DEF_HOOK);
998 __split_stmt(base_type->stmt);
999 __split_stmt(base_type->inline_stmt);
1000 __pass_to_client(sym, END_FUNC_HOOK);
1001 __pass_to_client(sym, AFTER_FUNC_HOOK);
1002 cur_func = NULL;
1003 clear_all_states();
1004 free_data_info_allocs();
1005 free_expression_stack(&switch_expr_stack);
1006 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1007 __bail_on_rest_of_function = 0;
1010 static void parse_inline(struct expression *call)
1012 struct symbol *base_type;
1013 int loop_num_bak = loop_num;
1014 int final_pass_bak = final_pass;
1015 char *cur_func_bak = cur_func;
1016 struct statement_list *big_statement_stack_bak = big_statement_stack;
1017 struct expression_list *big_expression_stack_bak = big_expression_stack;
1018 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1019 struct symbol *cur_func_sym_bak = cur_func_sym;
1021 __pass_to_client(call, INLINE_FN_START);
1022 final_pass = 0; /* don't print anything */
1023 __inline_fn = call;
1025 base_type = get_base_type(call->fn->symbol);
1026 cur_func_sym = call->fn->symbol;
1027 if (call->fn->symbol->ident)
1028 cur_func = call->fn->symbol->ident->name;
1029 else
1030 cur_func = NULL;
1031 set_position(call->fn->symbol->pos);
1033 save_all_states();
1034 nullify_all_states();
1035 big_statement_stack = NULL;
1036 big_expression_stack = NULL;
1037 switch_expr_stack = NULL;
1039 sm_debug("inline function: %s\n", cur_func);
1040 __unnullify_path();
1041 loop_num = 0;
1042 __pass_to_client(call->fn->symbol, FUNC_DEF_HOOK);
1043 __pass_to_client(call->fn->symbol, AFTER_DEF_HOOK);
1044 __split_stmt(base_type->stmt);
1045 __split_stmt(base_type->inline_stmt);
1046 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1047 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1049 free_expression_stack(&switch_expr_stack);
1050 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1051 nullify_path();
1053 loop_num = loop_num_bak;
1054 final_pass = final_pass_bak;
1055 cur_func_sym = cur_func_sym_bak;
1056 cur_func = cur_func_bak;
1057 big_statement_stack = big_statement_stack_bak;
1058 big_expression_stack = big_expression_stack_bak;
1059 switch_expr_stack = switch_expr_stack_bak;
1061 restore_all_states();
1062 set_position(call->pos);
1063 __inline_fn = NULL;
1064 __pass_to_client(call, INLINE_FN_END);
1067 static struct symbol_list *inlines_called;
1068 static void add_inline_function(struct symbol *sym)
1070 static struct symbol_list *already_added;
1071 struct symbol *tmp;
1073 FOR_EACH_PTR(already_added, tmp) {
1074 if (tmp == sym)
1075 return;
1076 } END_FOR_EACH_PTR(tmp);
1078 add_ptr_list(&already_added, sym);
1079 add_ptr_list(&inlines_called, sym);
1082 static void process_inlines()
1084 struct symbol *tmp;
1086 FOR_EACH_PTR(inlines_called, tmp) {
1087 split_function(tmp);
1088 } END_FOR_EACH_PTR(tmp);
1089 free_ptr_list(&inlines_called);
1092 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1094 struct symbol *sym;
1096 FOR_EACH_PTR_REVERSE(big_list, sym) {
1097 if (!sym->scope)
1098 continue;
1099 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1100 return sym;
1101 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1102 return sym;
1103 } END_FOR_EACH_PTR_REVERSE(sym);
1105 return NULL;
1108 static void split_inlines_in_scope(struct symbol *sym)
1110 struct symbol *base;
1111 struct symbol_list *scope_list;
1112 int stream;
1114 scope_list = sym->scope->symbols;
1115 stream = sym->pos.stream;
1117 /* find the last static symbol in the file */
1118 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1119 if (sym->pos.stream != stream)
1120 continue;
1121 if (sym->type != SYM_NODE)
1122 continue;
1123 base = get_base_type(sym);
1124 if (!base)
1125 continue;
1126 if (base->type != SYM_FN)
1127 continue;
1128 if (!base->inline_stmt)
1129 continue;
1130 add_inline_function(sym);
1131 } END_FOR_EACH_PTR_REVERSE(sym);
1133 process_inlines();
1136 static void split_inlines(struct symbol_list *sym_list)
1138 struct symbol *sym;
1140 sym = get_last_scoped_symbol(sym_list, 0);
1141 if (sym)
1142 split_inlines_in_scope(sym);
1143 sym = get_last_scoped_symbol(sym_list, 1);
1144 if (sym)
1145 split_inlines_in_scope(sym);
1148 static void split_functions(struct symbol_list *sym_list)
1150 struct symbol *sym;
1152 FOR_EACH_PTR(sym_list, sym) {
1153 set_position(sym->pos);
1154 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1155 split_function(sym);
1156 process_inlines();
1157 } else {
1158 __pass_to_client(sym, BASE_HOOK);
1159 fake_global_assign(sym);
1161 } END_FOR_EACH_PTR(sym);
1162 split_inlines(sym_list);
1163 __pass_to_client(sym_list, END_FILE_HOOK);
1166 void smatch(int argc, char **argv)
1169 struct string_list *filelist = NULL;
1170 struct symbol_list *sym_list;
1172 if (argc < 2) {
1173 printf("Usage: smatch [--debug] <filename.c>\n");
1174 exit(1);
1176 sparse_initialize(argc, argv, &filelist);
1177 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1178 if (option_file_output) {
1179 char buf[256];
1181 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1182 sm_outfd = fopen(buf, "w");
1183 if (!sm_outfd) {
1184 printf("Error: Cannot open %s\n", base_file);
1185 exit(1);
1188 sym_list = sparse_keep_tokens(base_file);
1189 split_functions(sym_list);
1190 } END_FOR_EACH_PTR_NOTAG(base_file);