4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
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
)
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
);
42 __pass_to_client(expr
, OP_HOOK
);
43 __split_expr(expr
->unop
);
46 split_statements(expr
->statement
);
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
);
61 __pass_to_client(expr
, DEREF_HOOK
);
62 __split_expr(expr
->deref
);
65 __split_expr(expr
->base
);
68 __split_expr(expr
->cast_expression
);
71 /* there isn't anything to pass a client from inside a sizeof() */
73 case EXPR_CONDITIONAL
:
75 __split_whole_condition(expr
->conditional
);
76 __split_expr(expr
->cond_true
);
79 __split_expr(expr
->cond_false
);
80 __merge_true_states();
81 __pop_false_only_stack();
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
);
89 if (expr
->fn
->type
== EXPR_SYMBOL
&&
90 !strcmp(expr
->fn
->symbol_name
->name
, "panic"))
94 case EXPR_INITIALIZER
:
95 split_expr_list(expr
->expr_list
);
98 __split_expr(expr
->ident_expression
);
101 __split_expr(expr
->idx_expression
);
104 __split_expr(expr
->init_expr
);
111 static int is_forever_loop(struct statement
*stmt
)
114 struct expression
*expr
;
116 expr
= stmt
->iterator_pre_condition
;
118 expr
= stmt
->iterator_post_condition
;
120 /* this is a for(;;) loop... */
124 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
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
);
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();
153 __pop_false_states();
155 } else if (once_through
) {
157 __pop_false_states();
158 __use_false_only_stack();
162 __merge_false_states();
163 __use_false_only_stack();
169 * Post loops are do {} while();
171 static void handle_post_loop(struct statement
*stmt
)
175 split_statements(stmt
->iterator_statement
);
177 __split_whole_condition(stmt
->iterator_post_condition
);
178 __use_false_states();
180 if (is_forever_loop(stmt
)) {
188 __pop_false_only_stack();
191 static void split_statements(struct statement
*stmt
)
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
);
205 __pass_to_client(stmt
, RETURN_HOOK
);
206 __split_expr(stmt
->ret_value
);
209 case STMT_EXPRESSION
:
210 __split_expr(stmt
->expression
);
212 case STMT_COMPOUND
: {
214 FOR_EACH_PTR(stmt
->stmts
, s
) {
216 } END_FOR_EACH_PTR(s
);
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();
229 if (stmt
->iterator_pre_condition
)
230 handle_pre_loop(stmt
);
231 else if (stmt
->iterator_post_condition
)
232 handle_post_loop(stmt
);
234 // these are for(;;) type loops.
235 handle_pre_loop(stmt
);
239 __split_expr(stmt
->switch_expression
);
240 __save_switch_states();
244 split_statements(stmt
->switch_statement
);
245 if (!__pop_default())
252 if (!stmt
->case_expression
)
254 __split_expr(stmt
->case_expression
);
255 __split_expr(stmt
->case_to
);
256 split_statements(stmt
->case_statement
);
260 stmt
->label
->type
== SYM_LABEL
&&
261 stmt
->label
->ident
) {
262 __merge_gotos(stmt
->label
->ident
->name
);
264 split_statements(stmt
->label_statement
);
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")) {
271 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
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
);
285 __split_expr(stmt
->asm_string
);
286 //__split_expr(stmt->asm_outputs);
287 //__split_expr(stmt->asm_inputs);
288 //__split_expr(stmt->asm_clobbers);
293 __split_expr(stmt
->range_expression
);
294 __split_expr(stmt
->range_low
);
295 __split_expr(stmt
->range_high
);
300 static void split_expr_list(struct expression_list
*expr_list
)
302 struct expression
*expr
;
303 FOR_EACH_PTR(expr_list
, expr
) {
305 } END_FOR_EACH_PTR(expr
);
309 static void split_sym(struct symbol
*sym
)
313 if (!(sym
->namespace & NS_SYMBOL
))
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
)
330 FOR_EACH_PTR(sym_list
, sym
) {
332 } END_FOR_EACH_PTR(sym
);
335 static void split_functions(struct symbol_list
*sym_list
)
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
) {
344 line_func_start
= base_type
->stmt
->pos
.line
;
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
);
351 split_statements(base_type
->stmt
);
352 __pass_to_client(sym
, END_FUNC_HOOK
);
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
;
370 printf("Usage: smatch <filename.c>\n");
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
);