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
);
49 __split_whole_condition(expr
);
52 __merge_true_states();
53 __pop_false_only_stack();
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
);
69 __pass_to_client(expr
, DEREF_HOOK
);
70 __split_expr(expr
->deref
);
73 __split_expr(expr
->base
);
76 __split_expr(expr
->cast_expression
);
79 /* there isn't anything to pass a client from inside a sizeof() */
81 case EXPR_CONDITIONAL
:
83 __split_whole_condition(expr
->conditional
);
84 __split_expr(expr
->cond_true
);
87 __split_expr(expr
->cond_false
);
88 __merge_true_states();
89 __pop_false_only_stack();
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
);
97 if (expr
->fn
->type
== EXPR_SYMBOL
&&
98 !strcmp(expr
->fn
->symbol_name
->name
, "panic"))
102 case EXPR_INITIALIZER
:
103 split_expr_list(expr
->expr_list
);
105 case EXPR_IDENTIFIER
:
106 __split_expr(expr
->ident_expression
);
109 __split_expr(expr
->idx_expression
);
112 __split_expr(expr
->init_expr
);
119 static int is_forever_loop(struct statement
*stmt
)
122 struct expression
*expr
;
124 expr
= stmt
->iterator_pre_condition
;
126 expr
= stmt
->iterator_post_condition
;
128 /* this is a for(;;) loop... */
132 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
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
);
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();
161 __pop_false_states();
163 } else if (once_through
) {
165 __pop_false_states();
166 __use_false_only_stack();
170 __merge_false_states();
171 __use_false_only_stack();
177 * Post loops are do {} while();
179 static void handle_post_loop(struct statement
*stmt
)
183 split_statements(stmt
->iterator_statement
);
185 __split_whole_condition(stmt
->iterator_post_condition
);
186 __use_false_states();
188 if (is_forever_loop(stmt
)) {
196 __pop_false_only_stack();
199 static void split_statements(struct statement
*stmt
)
204 __smatch_lineno
= stmt
->pos
.line
;
205 __pass_to_client(stmt
, STMT_HOOK
);
207 switch (stmt
->type
) {
208 case STMT_DECLARATION
:
209 __pass_declarations_to_client(stmt
->declaration
);
210 split_symlist(stmt
->declaration
);
213 __pass_to_client(stmt
, RETURN_HOOK
);
214 __split_expr(stmt
->ret_value
);
217 case STMT_EXPRESSION
:
218 __split_expr(stmt
->expression
);
220 case STMT_COMPOUND
: {
222 FOR_EACH_PTR(stmt
->stmts
, s
) {
224 } END_FOR_EACH_PTR(s
);
228 __split_whole_condition(stmt
->if_conditional
);
229 split_statements(stmt
->if_true
);
230 __push_true_states();
231 __use_false_states();
232 split_statements(stmt
->if_false
);
233 __merge_true_states();
234 __pop_false_only_stack();
237 if (stmt
->iterator_pre_condition
)
238 handle_pre_loop(stmt
);
239 else if (stmt
->iterator_post_condition
)
240 handle_post_loop(stmt
);
242 // these are for(;;) type loops.
243 handle_pre_loop(stmt
);
247 __split_expr(stmt
->switch_expression
);
248 __save_switch_states();
252 split_statements(stmt
->switch_statement
);
253 if (!__pop_default())
260 if (!stmt
->case_expression
)
262 __split_expr(stmt
->case_expression
);
263 __split_expr(stmt
->case_to
);
264 split_statements(stmt
->case_statement
);
268 stmt
->label
->type
== SYM_LABEL
&&
269 stmt
->label
->ident
) {
270 __merge_gotos(stmt
->label
->ident
->name
);
272 split_statements(stmt
->label_statement
);
275 __split_expr(stmt
->goto_expression
);
276 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
277 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
279 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
281 __process_continues();
283 } else if (stmt
->goto_label
&&
284 stmt
->goto_label
->type
== SYM_LABEL
&&
285 stmt
->goto_label
->ident
) {
286 __save_gotos(stmt
->goto_label
->ident
->name
);
293 __split_expr(stmt
->asm_string
);
294 //__split_expr(stmt->asm_outputs);
295 //__split_expr(stmt->asm_inputs);
296 //__split_expr(stmt->asm_clobbers);
301 __split_expr(stmt
->range_expression
);
302 __split_expr(stmt
->range_low
);
303 __split_expr(stmt
->range_high
);
308 static void split_expr_list(struct expression_list
*expr_list
)
310 struct expression
*expr
;
311 FOR_EACH_PTR(expr_list
, expr
) {
313 } END_FOR_EACH_PTR(expr
);
317 static void split_sym(struct symbol
*sym
)
321 if (!(sym
->namespace & NS_SYMBOL
))
324 __pass_to_client(sym
, SYM_HOOK
);
325 split_statements(sym
->stmt
);
326 __split_expr(sym
->array_size
);
327 split_symlist(sym
->arguments
);
328 split_symlist(sym
->symbol_list
);
329 split_statements(sym
->inline_stmt
);
330 split_symlist(sym
->inline_symbol_list
);
331 __split_expr(sym
->initializer
);
334 static void split_symlist(struct symbol_list
*sym_list
)
338 FOR_EACH_PTR(sym_list
, sym
) {
340 } END_FOR_EACH_PTR(sym
);
343 static void split_functions(struct symbol_list
*sym_list
)
347 FOR_EACH_PTR(sym_list
, sym
) {
348 struct symbol
*base_type
;
349 base_type
= get_base_type(sym
);
350 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
352 line_func_start
= base_type
->stmt
->pos
.line
;
354 cur_func
= sym
->ident
->name
;
355 __smatch_lineno
= sym
->pos
.line
;
356 SM_DEBUG("new function: %s\n", cur_func
);
357 __pass_to_client(sym
, FUNC_DEF_HOOK
);
359 split_statements(base_type
->stmt
);
360 __pass_to_client(sym
, END_FUNC_HOOK
);
366 __pass_to_client(sym
, BASE_HOOK
);
368 } END_FOR_EACH_PTR(sym
);
369 __pass_to_client_no_data(END_FILE_HOOK
);
372 void smatch (int argc
, char **argv
)
375 struct string_list
*filelist
= NULL
;
376 struct symbol_list
*sym_list
;
379 printf("Usage: smatch <filename.c>\n");
382 sparse_initialize(argc
, argv
, &filelist
);
383 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
384 sym_list
= __sparse(filename
);
385 split_functions(sym_list
);
386 } END_FOR_EACH_PTR_NOTAG(filename
);