Handle assignmeents inside conditions better.
[smatch.git] / smatch_flow.c
blobd7402091635302787dad0658c468ce1e0294c340
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_BINOP:
49 case EXPR_COMMA:
50 case EXPR_COMPARE:
51 case EXPR_LOGICAL:
52 case EXPR_ASSIGNMENT:
53 if (expr->type == EXPR_ASSIGNMENT)
54 __pass_to_client(expr, ASSIGNMENT_HOOK);
55 __split_expr(expr->left);
56 __split_expr(expr->right);
57 if (expr->type == EXPR_ASSIGNMENT)
58 __pass_to_client(expr, ASSIGNMENT_AFTER_HOOK);
59 return;
60 case EXPR_DEREF:
61 __pass_to_client(expr, DEREF_HOOK);
62 __split_expr(expr->deref);
63 return;
64 case EXPR_SLICE:
65 __split_expr(expr->base);
66 return;
67 case EXPR_CAST:
68 __split_expr(expr->cast_expression);
69 return;
70 case EXPR_SIZEOF:
71 /* there isn't anything to pass a client from inside a sizeof() */
72 return;
73 case EXPR_CONDITIONAL:
74 case EXPR_SELECT:
75 __split_whole_condition(expr->conditional);
76 __split_expr(expr->cond_true);
77 __push_true_states();
78 __use_false_states();
79 __split_expr(expr->cond_false);
80 __merge_true_states();
81 __pop_false_only_stack();
82 return;
83 case EXPR_CALL:
84 __pass_to_client(expr, FUNCTION_CALL_HOOK);
85 __split_expr(expr->fn);
86 split_expr_list(expr->args);
87 __pass_to_client(expr, FUNCTION_CALL_AFTER_HOOK);
88 #ifdef KERNEL
89 if (expr->fn->type == EXPR_SYMBOL &&
90 !strcmp(expr->fn->symbol_name->name, "panic"))
91 nullify_path();
92 #endif
93 return;
94 case EXPR_INITIALIZER:
95 split_expr_list(expr->expr_list);
96 return;
97 case EXPR_IDENTIFIER:
98 __split_expr(expr->ident_expression);
99 return;
100 case EXPR_INDEX:
101 __split_expr(expr->idx_expression);
102 return;
103 case EXPR_POS:
104 __split_expr(expr->init_expr);
105 return;
106 default:
107 return;
111 static int is_forever_loop(struct statement *stmt)
114 struct expression *expr;
116 expr = stmt->iterator_pre_condition;
117 if (!expr)
118 expr = stmt->iterator_post_condition;
119 if (!expr) {
120 /* this is a for(;;) loop... */
121 return 1;
124 if (expr->type == EXPR_VALUE && expr->value == 1) {
125 return 1;
128 return 0;
132 * Pre Loops are while and for loops.
135 static void handle_pre_loop(struct statement *stmt)
137 int once_through; /* we go through the loop at least once */
139 split_statements(stmt->iterator_pre_statement);
141 once_through = known_condition_true(stmt->iterator_pre_condition);
143 __push_continues();
144 __push_breaks();
146 __split_whole_condition(stmt->iterator_pre_condition);
148 split_statements(stmt->iterator_statement);
149 split_statements(stmt->iterator_post_statement);
150 if (is_forever_loop(stmt)) {
151 __pop_false_only_stack();
152 __pop_continues();
153 __pop_false_states();
154 __use_breaks();
155 } else if (once_through) {
156 __merge_continues();
157 __pop_false_states();
158 __use_false_only_stack();
159 __merge_breaks();
160 } else {
161 __merge_continues();
162 __merge_false_states();
163 __use_false_only_stack();
164 __merge_breaks();
169 * Post loops are do {} while();
171 static void handle_post_loop(struct statement *stmt)
173 __push_continues();
174 __push_breaks();
175 split_statements(stmt->iterator_statement);
177 __split_whole_condition(stmt->iterator_post_condition);
178 __use_false_states();
180 if (is_forever_loop(stmt)) {
181 __pop_continues();
182 __use_breaks();
184 } else {
185 __merge_continues();
186 __merge_breaks();
188 __pop_false_only_stack();
191 static void split_statements(struct statement *stmt)
193 if (!stmt)
194 return;
196 __smatch_lineno = stmt->pos.line;
197 __pass_to_client(stmt, STMT_HOOK);
199 switch (stmt->type) {
200 case STMT_DECLARATION:
201 __pass_declarations_to_client(stmt->declaration);
202 split_symlist(stmt->declaration);
203 return;
204 case STMT_RETURN:
205 __pass_to_client(stmt, RETURN_HOOK);
206 __split_expr(stmt->ret_value);
207 nullify_path();
208 return;
209 case STMT_EXPRESSION:
210 __split_expr(stmt->expression);
211 return;
212 case STMT_COMPOUND: {
213 struct statement *s;
214 FOR_EACH_PTR(stmt->stmts, s) {
215 split_statements(s);
216 } END_FOR_EACH_PTR(s);
217 return;
219 case STMT_IF:
220 __split_whole_condition(stmt->if_conditional);
221 split_statements(stmt->if_true);
222 __push_true_states();
223 __use_false_states();
224 split_statements(stmt->if_false);
225 __merge_true_states();
226 __pop_false_only_stack();
227 return;
228 case STMT_ITERATOR:
229 if (stmt->iterator_pre_condition)
230 handle_pre_loop(stmt);
231 else if (stmt->iterator_post_condition)
232 handle_post_loop(stmt);
233 else {
234 // these are for(;;) type loops.
235 handle_pre_loop(stmt);
237 return;
238 case STMT_SWITCH:
239 __split_expr(stmt->switch_expression);
240 __save_switch_states();
241 __push_default();
242 __push_breaks();
243 nullify_path();
244 split_statements(stmt->switch_statement);
245 if (!__pop_default())
246 __merge_switches();
247 __pop_switches();
248 __merge_breaks();
249 return;
250 case STMT_CASE:
251 __merge_switches();
252 if (!stmt->case_expression)
253 __set_default();
254 __split_expr(stmt->case_expression);
255 __split_expr(stmt->case_to);
256 split_statements(stmt->case_statement);
257 return;
258 case STMT_LABEL:
259 if (stmt->label &&
260 stmt->label->type == SYM_LABEL &&
261 stmt->label->ident) {
262 __merge_gotos(stmt->label->ident->name);
264 split_statements(stmt->label_statement);
265 return;
266 case STMT_GOTO:
267 __split_expr(stmt->goto_expression);
268 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
269 if (!strcmp(stmt->goto_label->ident->name, "break")) {
270 __process_breaks();
271 } else if (!strcmp(stmt->goto_label->ident->name,
272 "continue")) {
273 __process_continues();
275 } else if (stmt->goto_label &&
276 stmt->goto_label->type == SYM_LABEL &&
277 stmt->goto_label->ident) {
278 __save_gotos(stmt->goto_label->ident->name);
280 nullify_path();
281 return;
282 case STMT_NONE:
283 return;
284 case STMT_ASM:
285 __split_expr(stmt->asm_string);
286 //__split_expr(stmt->asm_outputs);
287 //__split_expr(stmt->asm_inputs);
288 //__split_expr(stmt->asm_clobbers);
289 return;
290 case STMT_CONTEXT:
291 return;
292 case STMT_RANGE:
293 __split_expr(stmt->range_expression);
294 __split_expr(stmt->range_low);
295 __split_expr(stmt->range_high);
296 return;
300 static void split_expr_list(struct expression_list *expr_list)
302 struct expression *expr;
303 FOR_EACH_PTR(expr_list, expr) {
304 __split_expr(expr);
305 } END_FOR_EACH_PTR(expr);
309 static void split_sym(struct symbol *sym)
311 if (!sym)
312 return;
313 if (!(sym->namespace & NS_SYMBOL))
314 return;
316 __pass_to_client(sym, SYM_HOOK);
317 split_statements(sym->stmt);
318 __split_expr(sym->array_size);
319 split_symlist(sym->arguments);
320 split_symlist(sym->symbol_list);
321 split_statements(sym->inline_stmt);
322 split_symlist(sym->inline_symbol_list);
323 __split_expr(sym->initializer);
326 static void split_symlist(struct symbol_list *sym_list)
328 struct symbol *sym;
330 FOR_EACH_PTR(sym_list, sym) {
331 split_sym(sym);
332 } END_FOR_EACH_PTR(sym);
335 static void split_functions(struct symbol_list *sym_list)
337 struct symbol *sym;
339 FOR_EACH_PTR(sym_list, sym) {
340 struct symbol *base_type;
341 base_type = get_base_type(sym);
342 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
343 if (base_type->stmt)
344 line_func_start = base_type->stmt->pos.line;
345 if (sym->ident)
346 cur_func = sym->ident->name;
347 __smatch_lineno = sym->pos.line;
348 SM_DEBUG("new function: %s\n", cur_func);
349 __pass_to_client(sym, FUNC_DEF_HOOK);
350 __unnullify_path();
351 split_statements(base_type->stmt);
352 __pass_to_client(sym, END_FUNC_HOOK);
353 cur_func = NULL;
354 line_func_start = 0;
355 clear_all_states();
357 } else {
358 __pass_to_client(sym, BASE_HOOK);
360 } END_FOR_EACH_PTR(sym);
363 void smatch (int argc, char **argv)
366 struct string_list *filelist = NULL;
367 struct symbol_list *sym_list;
369 if (argc < 2) {
370 printf("Usage: smatch <filename.c>\n");
371 exit(1);
373 sparse_initialize(argc, argv, &filelist);
374 FOR_EACH_PTR_NOTAG(filelist, filename) {
375 sym_list = __sparse(filename);
376 split_functions(sym_list);
377 } END_FOR_EACH_PTR_NOTAG(filename);