Add STRING_HOOK for examining strings
[smatch.git] / smatch_flow.c
blob6685915b2a6f8d808fccfccbe4b025fe142c57f7
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 "smatch.h"
15 #include "smatch_expression_stacks.h"
16 #include "smatch_extra.h"
17 #include "smatch_slist.h"
19 int final_pass;
21 static int __smatch_lineno = 0;
23 static const char *filename;
24 static char *pathname;
25 static char *full_filename;
26 static char *cur_func;
27 static int line_func_start;
28 static struct expression_list *switch_expr_stack = NULL;
29 struct expression_list *big_expression_stack;
30 struct statement_list *big_statement_stack;
31 int __in_pre_condition = 0;
32 int __bail_on_rest_of_function = 0;
33 char *get_function(void) { return cur_func; }
34 int get_lineno(void) { return __smatch_lineno; }
35 int get_func_pos(void) { return __smatch_lineno - line_func_start; }
37 static void split_symlist(struct symbol_list *sym_list);
38 static void split_declaration(struct symbol_list *sym_list);
39 static void split_expr_list(struct expression_list *expr_list);
41 int option_assume_loops = 0;
42 int option_known_conditions = 0;
43 int option_two_passes = 0;
44 struct symbol *cur_func_sym = NULL;
46 const char *get_filename(void)
48 if (option_full_path)
49 return full_filename;
50 return filename;
53 static void set_position(struct expression *expr)
55 int len;
56 static int prev_stream = -1;
58 __smatch_lineno = expr->pos.line;
60 if (expr->pos.stream == prev_stream)
61 return;
63 filename = stream_name(expr->pos.stream);
65 free(full_filename);
66 pathname = getcwd(NULL, 0);
67 if (pathname) {
68 len = strlen(pathname) + 1 + strlen(filename) + 1;
69 full_filename = malloc(len);
70 snprintf(full_filename, len, "%s/%s", pathname, filename);
71 } else {
72 full_filename = alloc_string(filename);
74 free(pathname);
77 void __split_expr(struct expression *expr)
79 if (!expr)
80 return;
82 // printf("%d Debug expr_type %d %s\n", get_lineno(), expr->type, show_special(expr->op));
84 push_expression(&big_expression_stack, expr);
85 set_position(expr);
86 __pass_to_client(expr, EXPR_HOOK);
88 switch (expr->type) {
89 case EXPR_PREOP:
90 if (expr->op == '*')
91 __pass_to_client(expr, DEREF_HOOK);
92 case EXPR_POSTOP:
93 __pass_to_client(expr, OP_HOOK);
94 __split_expr(expr->unop);
95 break;
96 case EXPR_STATEMENT:
97 __split_statements(expr->statement);
98 break;
99 case EXPR_BINOP:
100 __pass_to_client(expr, BINOP_HOOK);
101 case EXPR_LOGICAL:
102 case EXPR_COMMA:
103 case EXPR_COMPARE:
104 __split_expr(expr->left);
105 __split_expr(expr->right);
106 break;
107 case EXPR_ASSIGNMENT: {
108 struct expression *tmp;
110 /* foo = !bar() */
111 if (__handle_condition_assigns(expr))
112 break;
114 /* foo = (x < 5 ? foo : 5); */
115 if (__handle_select_assigns(expr))
116 break;
118 __split_expr(expr->right);
119 __pass_to_client(expr, ASSIGNMENT_HOOK);
120 tmp = strip_expr(expr->right);
121 if (tmp->type == EXPR_CALL)
122 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
123 __split_expr(expr->left);
124 break;
126 case EXPR_DEREF:
127 __pass_to_client(expr, DEREF_HOOK);
128 __split_expr(expr->deref);
129 break;
130 case EXPR_SLICE:
131 __split_expr(expr->base);
132 break;
133 case EXPR_CAST:
134 case EXPR_FORCE_CAST:
135 __split_expr(expr->cast_expression);
136 break;
137 case EXPR_SIZEOF:
138 /* there isn't anything to pass a client from inside a sizeof() */
139 break;
140 case EXPR_CONDITIONAL:
141 case EXPR_SELECT:
142 __split_whole_condition(expr->conditional);
143 __split_expr(expr->cond_true);
144 __push_true_states();
145 __use_false_states();
146 __split_expr(expr->cond_false);
147 __merge_true_states();
148 break;
149 case EXPR_CALL:
150 split_expr_list(expr->args);
151 __split_expr(expr->fn);
152 __pass_to_client(expr, FUNCTION_CALL_HOOK);
153 break;
154 case EXPR_INITIALIZER:
155 split_expr_list(expr->expr_list);
156 break;
157 case EXPR_IDENTIFIER:
158 __split_expr(expr->ident_expression);
159 break;
160 case EXPR_INDEX:
161 __split_expr(expr->idx_expression);
162 break;
163 case EXPR_POS:
164 __split_expr(expr->init_expr);
165 break;
166 case EXPR_SYMBOL:
167 __pass_to_client(expr, SYM_HOOK);
168 break;
169 case EXPR_STRING:
170 __pass_to_client(expr, STRING_HOOK);
171 break;
172 default:
173 break;
175 pop_expression(&big_expression_stack);
178 static int is_forever_loop(struct statement *stmt)
181 struct expression *expr;
183 expr = strip_expr(stmt->iterator_pre_condition);
184 if (!expr)
185 expr = stmt->iterator_post_condition;
186 if (!expr) {
187 /* this is a for(;;) loop... */
188 return 1;
191 if (expr->type == EXPR_VALUE && expr->value == 1) {
192 return 1;
195 return 0;
198 static int loop_num;
199 static char *get_loop_name(int num)
201 char buf[256];
203 snprintf(buf, 255, "-loop%d", num);
204 buf[255] = '\0';
205 return alloc_sname(buf);;
209 * Pre Loops are while and for loops.
212 static void handle_pre_loop(struct statement *stmt)
214 int once_through; /* we go through the loop at least once */
215 struct sm_state *extra_sm = NULL;
216 int unchanged = 0;
217 char *loop_name;
218 struct state_list *slist = NULL;
219 struct sm_state *sm = NULL;
221 loop_name = get_loop_name(loop_num);
222 loop_num++;
224 __split_statements(stmt->iterator_pre_statement);
226 once_through = implied_condition_true(stmt->iterator_pre_condition);
228 __push_continues();
229 __push_breaks();
231 __merge_gotos(loop_name);
233 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
234 __in_pre_condition++;
235 __split_whole_condition(stmt->iterator_pre_condition);
236 __in_pre_condition--;
237 FOR_EACH_PTR(slist, sm) {
238 set_state(sm->owner, sm->name, sm->sym, sm->state);
239 } END_FOR_EACH_PTR(sm);
240 free_slist(&slist);
241 if (extra_sm)
242 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
244 if (option_assume_loops)
245 once_through = 1;
247 __split_statements(stmt->iterator_statement);
248 __warn_on_silly_pre_loops();
249 if (is_forever_loop(stmt)) {
250 __save_gotos(loop_name);
251 /* forever loops don't have an iterator_post_statement */
252 __discard_continues();
253 __discard_false_states();
254 __use_breaks();
255 } else {
256 __merge_continues();
257 unchanged = __iterator_unchanged(extra_sm);
258 __split_statements(stmt->iterator_post_statement);
259 __save_gotos(loop_name);
260 __split_whole_condition(stmt->iterator_pre_condition);
261 nullify_path();
262 __merge_false_states();
263 if (once_through) {
264 __discard_false_states();
265 } else {
266 __merge_false_states();
269 if (extra_sm && unchanged)
270 __extra_pre_loop_hook_after(extra_sm,
271 stmt->iterator_post_statement,
272 stmt->iterator_pre_condition);
273 __merge_breaks();
278 * Post loops are do {} while();
280 static void handle_post_loop(struct statement *stmt)
282 char *loop_name;
284 loop_name = get_loop_name(loop_num);
285 loop_num++;
287 __push_continues();
288 __push_breaks();
289 __merge_gotos(loop_name);
290 __split_statements(stmt->iterator_statement);
291 __merge_continues();
292 if (!is_zero(stmt->iterator_post_condition))
293 __save_gotos(loop_name);
295 if (is_forever_loop(stmt)) {
296 __use_breaks();
297 } else {
298 __split_whole_condition(stmt->iterator_post_condition);
299 __use_false_states();
300 __merge_breaks();
304 static int empty_statement(struct statement *stmt)
306 if (!stmt)
307 return 0;
308 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
309 return 1;
310 return 0;
313 static struct statement *last_stmt;
314 static int is_last_stmt(struct statement *stmt)
316 if (stmt == last_stmt)
317 return 1;
318 return 0;
321 static void print_unreached_initializers(struct symbol_list *sym_list)
323 struct symbol *sym;
325 FOR_EACH_PTR(sym_list, sym) {
326 if(sym->initializer)
327 sm_msg("info: '%s' is not actually initialized (unreached code).",
328 (sym->ident ? sym->ident->name : "this variable"));
329 } END_FOR_EACH_PTR(sym);
332 static void print_unreached(struct statement *stmt)
335 static int print = 1;
337 if (!__path_is_null()) {
338 print = 1;
339 return;
341 if (!print)
342 return;
344 switch (stmt->type) {
345 case STMT_COMPOUND: /* after a switch before a case stmt */
346 case STMT_RANGE:
347 case STMT_CASE:
348 case STMT_LABEL:
349 return;
350 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
351 print_unreached_initializers(stmt->declaration);
352 return;
353 case STMT_RETURN: /* gcc complains if you don't have a return statement */
354 if (is_last_stmt(stmt))
355 return;
356 break;
357 case STMT_GOTO:
358 if (!option_spammy)
359 return;
360 break;
361 default:
362 break;
364 if (!option_spammy && empty_statement(stmt))
365 return;
366 sm_msg("info: ignoring unreachable code.");
367 print = 0;
370 void __split_statements(struct statement *stmt)
372 if (!stmt)
373 return;
375 if (out_of_memory() || __bail_on_rest_of_function) {
376 static char *printed = NULL;
378 if (printed != cur_func)
379 sm_msg("Function too hairy. Giving up.");
380 printed = cur_func;
381 return;
384 add_ptr_list(&big_statement_stack, stmt);
385 free_expression_stack(&big_expression_stack);
386 __smatch_lineno = stmt->pos.line;
387 print_unreached(stmt);
388 __pass_to_client(stmt, STMT_HOOK);
390 switch (stmt->type) {
391 case STMT_DECLARATION:
392 split_declaration(stmt->declaration);
393 return;
394 case STMT_RETURN:
395 __split_expr(stmt->ret_value);
396 __pass_to_client(stmt->ret_value, RETURN_HOOK);
397 nullify_path();
398 return;
399 case STMT_EXPRESSION:
400 __split_expr(stmt->expression);
401 return;
402 case STMT_COMPOUND: {
403 struct statement *s;
405 if (!last_stmt)
406 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
407 __push_scope_hooks();
408 FOR_EACH_PTR(stmt->stmts, s) {
409 __split_statements(s);
410 } END_FOR_EACH_PTR(s);
411 __call_scope_hooks();
412 return;
414 case STMT_IF:
415 if (known_condition_true(stmt->if_conditional)) {
416 __split_statements(stmt->if_true);
417 return;
419 if (known_condition_false(stmt->if_conditional)) {
420 __split_statements(stmt->if_false);
421 return;
423 if (option_known_conditions &&
424 implied_condition_true(stmt->if_conditional)) {
425 sm_info("this condition is true.");
426 __split_statements(stmt->if_true);
427 return;
429 if (option_known_conditions &&
430 implied_condition_false(stmt->if_conditional)) {
431 sm_info("this condition is false.");
432 __split_statements(stmt->if_false);
433 return;
435 __split_whole_condition(stmt->if_conditional);
436 __split_statements(stmt->if_true);
437 if (empty_statement(stmt->if_true))
438 sm_msg("warn: if();");
439 __push_true_states();
440 __use_false_states();
441 __split_statements(stmt->if_false);
442 __merge_true_states();
443 return;
444 case STMT_ITERATOR:
445 if (stmt->iterator_pre_condition)
446 handle_pre_loop(stmt);
447 else if (stmt->iterator_post_condition)
448 handle_post_loop(stmt);
449 else {
450 // these are for(;;) type loops.
451 handle_pre_loop(stmt);
453 return;
454 case STMT_SWITCH:
455 __split_expr(stmt->switch_expression);
456 push_expression(&switch_expr_stack, stmt->switch_expression);
457 __save_switch_states(top_expression(switch_expr_stack));
458 nullify_path();
459 __push_default();
460 __push_breaks();
461 __split_statements(stmt->switch_statement);
462 if (!__pop_default())
463 __merge_switches(top_expression(switch_expr_stack),
464 NULL);
465 __discard_switches();
466 __merge_breaks();
467 pop_expression(&switch_expr_stack);
468 return;
469 case STMT_CASE:
470 __merge_switches(top_expression(switch_expr_stack),
471 stmt->case_expression);
472 __pass_case_to_client(top_expression(switch_expr_stack),
473 stmt->case_expression);
474 if (!stmt->case_expression)
475 __set_default();
476 __split_expr(stmt->case_expression);
477 __split_expr(stmt->case_to);
478 __split_statements(stmt->case_statement);
479 return;
480 case STMT_LABEL:
481 if (stmt->label &&
482 stmt->label->type == SYM_LABEL &&
483 stmt->label->ident) {
484 __merge_gotos(stmt->label->ident->name);
486 __split_statements(stmt->label_statement);
487 return;
488 case STMT_GOTO:
489 __split_expr(stmt->goto_expression);
490 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
491 if (!strcmp(stmt->goto_label->ident->name, "break")) {
492 __process_breaks();
493 } else if (!strcmp(stmt->goto_label->ident->name,
494 "continue")) {
495 __process_continues();
497 } else if (stmt->goto_label &&
498 stmt->goto_label->type == SYM_LABEL &&
499 stmt->goto_label->ident) {
500 __save_gotos(stmt->goto_label->ident->name);
502 nullify_path();
503 return;
504 case STMT_NONE:
505 return;
506 case STMT_ASM:
507 __split_expr(stmt->asm_string);
508 split_expr_list(stmt->asm_outputs);
509 split_expr_list(stmt->asm_inputs);
510 split_expr_list(stmt->asm_clobbers);
511 return;
512 case STMT_CONTEXT:
513 return;
514 case STMT_RANGE:
515 __split_expr(stmt->range_expression);
516 __split_expr(stmt->range_low);
517 __split_expr(stmt->range_high);
518 return;
522 static void split_expr_list(struct expression_list *expr_list)
524 struct expression *expr;
525 FOR_EACH_PTR(expr_list, expr) {
526 __split_expr(expr);
527 } END_FOR_EACH_PTR(expr);
531 static void split_sym(struct symbol *sym)
533 if (!sym)
534 return;
535 if (!(sym->namespace & NS_SYMBOL))
536 return;
538 __split_statements(sym->stmt);
539 __split_expr(sym->array_size);
540 split_symlist(sym->arguments);
541 split_symlist(sym->symbol_list);
542 __split_statements(sym->inline_stmt);
543 split_symlist(sym->inline_symbol_list);
546 static void split_symlist(struct symbol_list *sym_list)
548 struct symbol *sym;
550 FOR_EACH_PTR(sym_list, sym) {
551 split_sym(sym);
552 } END_FOR_EACH_PTR(sym);
555 static struct expression *fake_assign_expr(struct symbol *sym)
557 struct expression *e_assign, *e_symbol;
559 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
560 e_symbol = alloc_expression(sym->initializer->pos, EXPR_SYMBOL);
561 e_assign->op = (int)'=';
562 e_symbol->symbol = sym;
563 e_symbol->symbol_name = sym->ident;
564 e_assign->left = e_symbol;
565 e_assign->right = sym->initializer;
566 return e_assign;
569 static void do_initializer_stuff(struct symbol *sym)
571 struct expression *assign;
573 if(!sym->initializer)
574 return;
575 assign = fake_assign_expr(sym);
576 __split_expr(assign);
579 static void split_declaration(struct symbol_list *sym_list)
581 struct symbol *sym;
583 FOR_EACH_PTR(sym_list, sym) {
584 __pass_to_client(sym, DECLARATION_HOOK);
585 do_initializer_stuff(sym);
586 split_sym(sym);
587 } END_FOR_EACH_PTR(sym);
590 static void split_functions(struct symbol_list *sym_list)
592 struct symbol *sym;
594 FOR_EACH_PTR(sym_list, sym) {
595 struct symbol *base_type;
596 base_type = get_base_type(sym);
597 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
598 cur_func_sym = sym;
599 if (base_type->stmt)
600 line_func_start = base_type->stmt->pos.line;
601 if (sym->ident)
602 cur_func = sym->ident->name;
603 __smatch_lineno = sym->pos.line;
604 last_stmt = NULL;
605 sm_debug("new function: %s\n", cur_func);
606 if (option_two_passes) {
607 __unnullify_path();
608 loop_num = 0;
609 final_pass = 0;
610 __pass_to_client(sym, FUNC_DEF_HOOK);
611 __split_statements(base_type->stmt);
612 nullify_path();
614 __unnullify_path();
615 loop_num = 0;
616 final_pass = 1;
617 __pass_to_client(sym, FUNC_DEF_HOOK);
618 __split_statements(base_type->stmt);
619 __pass_to_client(sym, END_FUNC_HOOK);
620 cur_func = NULL;
621 line_func_start = 0;
622 clear_all_states();
623 free_data_info_allocs();
624 free_expression_stack(&switch_expr_stack);
625 __free_ptr_list((struct ptr_list **)&big_statement_stack);
626 __bail_on_rest_of_function = 0;
627 } else {
628 __pass_to_client(sym, BASE_HOOK);
630 } END_FOR_EACH_PTR(sym);
631 __pass_to_client_no_data(END_FILE_HOOK);
634 void smatch (int argc, char **argv)
637 struct string_list *filelist = NULL;
638 struct symbol_list *sym_list;
639 char *file;
641 if (argc < 2) {
642 printf("Usage: smatch [--debug] <filename.c>\n");
643 exit(1);
645 sparse_initialize(argc, argv, &filelist);
646 FOR_EACH_PTR_NOTAG(filelist, file) {
647 sym_list = __sparse(file);
648 split_functions(sym_list);
649 } END_FOR_EACH_PTR_NOTAG(file);