Add the merge_hook() back in.
[smatch.git] / smatch_flow.c
blob1a158b023fa2bc5eb1dcd9db78d00e96b66084ff
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 #include <stdio.h>
11 #include "token.h"
12 #include "smatch.h"
14 int __smatch_lineno = 0;
16 static char *filename;
17 static char *cur_func;
18 static int line_func_start;
20 char *get_filename() { return filename; }
21 char *get_function() { return cur_func; }
22 int get_lineno() { return __smatch_lineno; }
23 int get_func_pos() { return __smatch_lineno - line_func_start; }
25 static void split_statements(struct statement *stmt);
26 static void split_symlist(struct symbol_list *sym_list);
27 static void split_expr_list(struct expression_list *expr_list);
29 void __split_expr(struct expression *expr)
31 if (!expr)
32 return;
34 // printf("%d Debug expr_type %d\n", get_lineno(), expr->type);
36 __smatch_lineno = expr->pos.line;
37 __pass_to_client(expr, EXPR_HOOK);
39 switch (expr->type) {
40 case EXPR_PREOP:
41 case EXPR_POSTOP:
42 __pass_to_client(expr, OP_HOOK);
43 __split_expr(expr->unop);
44 return;
45 case EXPR_STATEMENT:
46 split_statements(expr->statement);
47 return;
48 case EXPR_LOGICAL:
49 __split_whole_condition(expr);
50 __push_true_states();
51 __use_false_states();
52 __merge_true_states();
53 __pop_false_only_stack();
54 return;
56 return;
57 case EXPR_BINOP:
58 case EXPR_COMMA:
59 case EXPR_COMPARE:
60 case EXPR_ASSIGNMENT:
61 if (expr->type == EXPR_ASSIGNMENT)
62 __pass_to_client(expr, ASSIGNMENT_HOOK);
63 __split_expr(expr->left);
64 __split_expr(expr->right);
65 if (expr->type == EXPR_ASSIGNMENT)
66 __pass_to_client(expr, ASSIGNMENT_AFTER_HOOK);
67 return;
68 case EXPR_DEREF:
69 __pass_to_client(expr, DEREF_HOOK);
70 __split_expr(expr->deref);
71 return;
72 case EXPR_SLICE:
73 __split_expr(expr->base);
74 return;
75 case EXPR_CAST:
76 __split_expr(expr->cast_expression);
77 return;
78 case EXPR_SIZEOF:
79 /* there isn't anything to pass a client from inside a sizeof() */
80 return;
81 case EXPR_CONDITIONAL:
82 case EXPR_SELECT:
83 __split_whole_condition(expr->conditional);
84 __split_expr(expr->cond_true);
85 __push_true_states();
86 __use_false_states();
87 __split_expr(expr->cond_false);
88 __merge_true_states();
89 __pop_false_only_stack();
90 return;
91 case EXPR_CALL:
92 __pass_to_client(expr, FUNCTION_CALL_HOOK);
93 __split_expr(expr->fn);
94 split_expr_list(expr->args);
95 __pass_to_client(expr, FUNCTION_CALL_AFTER_HOOK);
96 #ifdef KERNEL
97 if (expr->fn->type == EXPR_SYMBOL &&
98 !strcmp(expr->fn->symbol_name->name, "panic"))
99 nullify_path();
100 #endif
101 return;
102 case EXPR_INITIALIZER:
103 split_expr_list(expr->expr_list);
104 return;
105 case EXPR_IDENTIFIER:
106 __split_expr(expr->ident_expression);
107 return;
108 case EXPR_INDEX:
109 __split_expr(expr->idx_expression);
110 return;
111 case EXPR_POS:
112 __split_expr(expr->init_expr);
113 return;
114 default:
115 return;
119 static int is_forever_loop(struct statement *stmt)
122 struct expression *expr;
124 expr = strip_expr(stmt->iterator_pre_condition);
125 if (!expr)
126 expr = stmt->iterator_post_condition;
127 if (!expr) {
128 /* this is a for(;;) loop... */
129 return 1;
132 if (expr->type == EXPR_VALUE && expr->value == 1) {
133 return 1;
136 return 0;
140 * Pre Loops are while and for loops.
143 static void handle_pre_loop(struct statement *stmt)
145 int once_through; /* we go through the loop at least once */
147 split_statements(stmt->iterator_pre_statement);
149 once_through = known_condition_true(stmt->iterator_pre_condition);
151 __push_continues();
152 __push_breaks();
154 __split_whole_condition(stmt->iterator_pre_condition);
156 split_statements(stmt->iterator_statement);
157 split_statements(stmt->iterator_post_statement);
158 if (is_forever_loop(stmt)) {
159 __pop_false_only_stack();
160 __pop_continues();
161 __pop_false_states();
162 __use_breaks();
163 } else if (once_through) {
164 __merge_continues();
165 __pop_false_states();
166 __use_false_only_stack();
167 __merge_breaks();
168 } else {
169 __merge_continues();
170 __merge_false_states();
171 __use_false_only_stack();
172 __merge_breaks();
177 * Post loops are do {} while();
179 static void handle_post_loop(struct statement *stmt)
181 __push_continues();
182 __push_breaks();
183 split_statements(stmt->iterator_statement);
185 __split_whole_condition(stmt->iterator_post_condition);
186 __use_false_states();
188 if (is_forever_loop(stmt)) {
189 __pop_continues();
190 __use_breaks();
192 } else {
193 __merge_continues();
194 __merge_breaks();
196 __pop_false_only_stack();
199 static void print_unreached(struct statement *stmt)
203 * GCC insists on a return statement even where it is never
204 * reached. Also BUG() sometimes is a forever loop and
205 * sometimes not so people put code after a BUG(). There
206 * are way to many false positives.
208 #ifdef KERNEL
209 return;
210 #endif
211 if (__path_is_null()) {
212 switch(stmt->type) {
213 case STMT_COMPOUND: /* after a switch before a case stmt */
214 case STMT_CASE:
215 case STMT_LABEL:
216 case STMT_DECLARATION: /* switch(x) { int a; case foo: ... */
217 break;
218 default:
219 smatch_msg("unreachable code. %d", stmt->type);
224 static void split_statements(struct statement *stmt)
226 if (!stmt)
227 return;
229 __smatch_lineno = stmt->pos.line;
230 print_unreached(stmt);
231 __pass_to_client(stmt, STMT_HOOK);
233 switch (stmt->type) {
234 case STMT_DECLARATION:
235 __pass_declarations_to_client(stmt->declaration);
236 split_symlist(stmt->declaration);
237 return;
238 case STMT_RETURN:
239 __pass_to_client(stmt, RETURN_HOOK);
240 __split_expr(stmt->ret_value);
241 nullify_path();
242 return;
243 case STMT_EXPRESSION:
244 __split_expr(stmt->expression);
245 return;
246 case STMT_COMPOUND: {
247 struct statement *s;
248 FOR_EACH_PTR(stmt->stmts, s) {
249 split_statements(s);
250 } END_FOR_EACH_PTR(s);
251 return;
253 case STMT_IF:
254 __split_whole_condition(stmt->if_conditional);
255 split_statements(stmt->if_true);
256 __push_true_states();
257 __use_false_states();
258 split_statements(stmt->if_false);
259 __merge_true_states();
260 __pop_false_only_stack();
261 return;
262 case STMT_ITERATOR:
263 if (stmt->iterator_pre_condition)
264 handle_pre_loop(stmt);
265 else if (stmt->iterator_post_condition)
266 handle_post_loop(stmt);
267 else {
268 // these are for(;;) type loops.
269 handle_pre_loop(stmt);
271 return;
272 case STMT_SWITCH:
273 __split_expr(stmt->switch_expression);
274 __save_switch_states();
275 __push_default();
276 __push_breaks();
277 nullify_path();
278 split_statements(stmt->switch_statement);
279 if (!__pop_default())
280 __merge_switches();
281 __pop_switches();
282 __merge_breaks();
283 return;
284 case STMT_CASE:
285 __merge_switches();
286 if (!stmt->case_expression)
287 __set_default();
288 __split_expr(stmt->case_expression);
289 __split_expr(stmt->case_to);
290 split_statements(stmt->case_statement);
291 return;
292 case STMT_LABEL:
293 if (stmt->label &&
294 stmt->label->type == SYM_LABEL &&
295 stmt->label->ident) {
296 __merge_gotos(stmt->label->ident->name);
298 split_statements(stmt->label_statement);
299 return;
300 case STMT_GOTO:
301 __split_expr(stmt->goto_expression);
302 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
303 if (!strcmp(stmt->goto_label->ident->name, "break")) {
304 __process_breaks();
305 } else if (!strcmp(stmt->goto_label->ident->name,
306 "continue")) {
307 __process_continues();
309 } else if (stmt->goto_label &&
310 stmt->goto_label->type == SYM_LABEL &&
311 stmt->goto_label->ident) {
312 __save_gotos(stmt->goto_label->ident->name);
314 nullify_path();
315 return;
316 case STMT_NONE:
317 return;
318 case STMT_ASM:
319 __split_expr(stmt->asm_string);
320 //__split_expr(stmt->asm_outputs);
321 //__split_expr(stmt->asm_inputs);
322 //__split_expr(stmt->asm_clobbers);
323 return;
324 case STMT_CONTEXT:
325 return;
326 case STMT_RANGE:
327 __split_expr(stmt->range_expression);
328 __split_expr(stmt->range_low);
329 __split_expr(stmt->range_high);
330 return;
334 static void split_expr_list(struct expression_list *expr_list)
336 struct expression *expr;
337 FOR_EACH_PTR(expr_list, expr) {
338 __split_expr(expr);
339 } END_FOR_EACH_PTR(expr);
343 static void split_sym(struct symbol *sym)
345 if (!sym)
346 return;
347 if (!(sym->namespace & NS_SYMBOL))
348 return;
350 __pass_to_client(sym, SYM_HOOK);
351 split_statements(sym->stmt);
352 __split_expr(sym->array_size);
353 split_symlist(sym->arguments);
354 split_symlist(sym->symbol_list);
355 split_statements(sym->inline_stmt);
356 split_symlist(sym->inline_symbol_list);
357 __split_expr(sym->initializer);
360 static void split_symlist(struct symbol_list *sym_list)
362 struct symbol *sym;
364 FOR_EACH_PTR(sym_list, sym) {
365 split_sym(sym);
366 } END_FOR_EACH_PTR(sym);
369 static void split_functions(struct symbol_list *sym_list)
371 struct symbol *sym;
373 FOR_EACH_PTR(sym_list, sym) {
374 struct symbol *base_type;
375 base_type = get_base_type(sym);
376 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
377 if (base_type->stmt)
378 line_func_start = base_type->stmt->pos.line;
379 if (sym->ident)
380 cur_func = sym->ident->name;
381 __smatch_lineno = sym->pos.line;
382 SM_DEBUG("new function: %s\n", cur_func);
383 __pass_to_client(sym, FUNC_DEF_HOOK);
384 __unnullify_path();
385 split_statements(base_type->stmt);
386 __pass_to_client(sym, END_FUNC_HOOK);
387 cur_func = NULL;
388 line_func_start = 0;
389 clear_all_states();
391 } else {
392 __pass_to_client(sym, BASE_HOOK);
394 } END_FOR_EACH_PTR(sym);
395 __pass_to_client_no_data(END_FILE_HOOK);
398 void smatch (int argc, char **argv)
401 struct string_list *filelist = NULL;
402 struct symbol_list *sym_list;
404 if (argc < 2) {
405 printf("Usage: smatch [--debug] <filename.c>\n");
406 exit(1);
408 sparse_initialize(argc, argv, &filelist);
409 FOR_EACH_PTR_NOTAG(filelist, filename) {
410 sym_list = __sparse(filename);
411 split_functions(sym_list);
412 } END_FOR_EACH_PTR_NOTAG(filename);