2 * sparse/check_string_ret.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
16 int __smatch_lineno
= 0;
21 static char *filename
;
22 static char *cur_func
;
23 static int line_func_start
;
24 static unsigned int path_id
= 0;
25 static unsigned int path_id_next
= 1;
27 char *get_filename() { return filename
; }
28 char *get_function() { return cur_func
; }
29 int get_lineno() { return __smatch_lineno
; }
30 int get_func_pos() { return __smatch_lineno
- line_func_start
; }
32 static void split_statements(struct statement
*stmt
);
33 static void split_symlist(struct symbol_list
*sym_list
);
34 static void split_expr_list(struct expression_list
*expr_list
);
35 static void split_expr(struct expression
*expr
);
37 unsigned int get_path_id()
42 static unsigned int split_path_id()
46 path_id
= path_id_next
++;
50 static void restore_path_id(int old_id
)
56 static int is_logical_and(struct expression
*expr
)
58 /* If you have if (!(a && b)) smatch translates that to
59 * if (!a || !b). Logically those are the same.
62 if ((!__negate
&& expr
->op
== SPECIAL_LOGICAL_AND
) ||
63 (__negate
&& expr
->op
== SPECIAL_LOGICAL_OR
))
68 static int is_logical_or(struct expression
*expr
)
70 if ((!__negate
&& expr
->op
== SPECIAL_LOGICAL_OR
) ||
71 (__negate
&& expr
->op
== SPECIAL_LOGICAL_AND
))
76 static void inc_ands_ors(struct expression
*expr
)
78 if (is_logical_and(expr
))
80 else if (is_logical_or(expr
))
84 static void dec_ands_ors(struct expression
*expr
)
86 if (is_logical_and(expr
))
88 else if (is_logical_or(expr
))
92 static void special_kernel_macros(struct expression
*expr
)
95 struct expression
*tmp
;
97 tmp
= first_ptr_list((struct ptr_list
*) expr
->args
);
98 if (tmp
->op
== '!' && tmp
->unop
->op
== '!'
99 && tmp
->unop
->unop
->op
== '(') {
100 split_conditions(tmp
->unop
->unop
->unop
);
102 __pass_to_client(expr
, CONDITION_HOOK
);
110 static int is_zero(struct expression
*expr
)
112 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 0)
115 return is_zero(expr
->unop
);
116 if (expr
->type
== EXPR_CAST
)
117 return is_zero(expr
->cast_expression
);
121 static int handle_zero_comparisons(struct expression
*expr
)
123 struct expression
*tmp
= NULL
;
125 // if left is zero or right is zero
126 if (is_zero(expr
->left
))
128 else if (is_zero(expr
->right
))
133 // "if (foo != 0)" is the same as "if (foo)"
134 if (expr
->op
== SPECIAL_NOTEQUAL
) {
135 split_conditions(tmp
);
139 // "if (foo == 0)" is the same as "if (!foo)"
140 if (expr
->op
== SPECIAL_EQUAL
) {
141 __negate
= (__negate
+ 1)%2;
142 split_conditions(tmp
);
143 __negate
= (__negate
+ 1)%2;
150 void split_conditions(struct expression
*expr
)
153 * If you have if ((a || a = foo()), we know a is
154 * non-null on the true state because 'a' is checked in both and
155 * groups. __ors_reached helps with that. Unfortunately
156 * smatch doesn't handle stuff like if (a && a = foo())
157 * because it never occured to me that people would do that...
160 static int __ors_reached
;
162 SM_DEBUG("%d in split_conditions type=%d\n", get_lineno(), expr
->type
);
164 if (expr
->type
== EXPR_COMPARE
)
165 if (handle_zero_comparisons(expr
))
168 if (expr
->type
== EXPR_LOGICAL
) {
169 unsigned int path_orig
;
172 path_orig
= split_path_id();
173 __split_false_states_mini();
175 split_conditions(expr
->left
);
177 if (is_logical_and(expr
)) {
179 __pop_false_states_mini();
180 split_conditions(expr
->right
);
181 } else if (is_logical_or(expr
)) {
182 if (!__ors_reached
) {
189 __use_false_states_mini();
190 split_conditions(expr
->right
);
194 if (__ands
+ __ors
== 0) {
200 restore_path_id(path_orig
);
202 } else if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!') {
203 __negate
= (__negate
+ 1)%2;
204 split_conditions(expr
->unop
);
205 __negate
= (__negate
+ 1)%2;
207 } else if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(') {
208 split_conditions(expr
->unop
);
209 } else if (expr
->type
== EXPR_CALL
) {
211 if (expr
->fn
->type
!= EXPR_SYMBOL
||
212 strcmp("__builtin_expect", expr
->fn
->symbol_name
->name
)) {
213 __pass_to_client(expr
, CONDITION_HOOK
);
217 special_kernel_macros(expr
);
219 __pass_to_client(expr
, CONDITION_HOOK
);
224 static void split_whole_condition(struct expression
*expr
)
227 split_conditions(expr
);
228 SM_DEBUG("%d __ands = %d __ors = %d __negate = %d\n", get_lineno(),
229 __ands
, __ors
, __negate
);
232 static void split_expr(struct expression
*expr
)
237 __smatch_lineno
= expr
->pos
.line
;
238 __pass_to_client(expr
, EXPR_HOOK
);
240 // printf("%d Debug expr_type %d\n", get_lineno(), expr->type);
242 switch (expr
->type
) {
246 __pass_to_client(expr
, DEREF_HOOK
);
247 split_expr(expr
->unop
);
250 split_statements(expr
->statement
);
256 case EXPR_ASSIGNMENT
:
257 if (expr
->type
== EXPR_ASSIGNMENT
)
258 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
259 split_expr(expr
->left
);
260 split_expr(expr
->right
);
261 if (expr
->type
== EXPR_ASSIGNMENT
)
262 __pass_to_client(expr
, ASSIGNMENT_AFTER_HOOK
);
265 __pass_to_client(expr
, DEREF_HOOK
);
266 split_expr(expr
->deref
);
269 split_expr(expr
->base
);
272 split_expr(expr
->cast_expression
);
275 /* there isn't anything to pass a client from inside a sizeof() */
277 case EXPR_CONDITIONAL
:
279 __split_true_false_paths();
280 __pass_to_client(expr
->conditional
, WHOLE_CONDITION_HOOK
);
281 split_whole_condition(expr
->conditional
);
283 split_expr(expr
->cond_true
);
284 __use_false_states();
285 split_expr(expr
->cond_false
);
286 __merge_true_states();
289 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
290 split_expr(expr
->fn
);
291 split_expr_list(expr
->args
);
292 __pass_to_client(expr
, FUNCTION_CALL_AFTER_HOOK
);
294 if (expr
->fn
->type
== EXPR_SYMBOL
&&
295 !strcmp(expr
->fn
->symbol_name
->name
, "panic"))
299 case EXPR_INITIALIZER
:
300 split_expr_list(expr
->expr_list
);
302 case EXPR_IDENTIFIER
:
303 split_expr(expr
->ident_expression
);
306 split_expr(expr
->idx_expression
);
309 split_expr(expr
->init_expr
);
316 static int is_forever_loop(struct statement
*stmt
)
319 struct expression
*expr
;
321 expr
= stmt
->iterator_pre_condition
;
323 expr
= stmt
->iterator_post_condition
;
325 // this is a for(;;) loop...
329 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
337 * Pre Loops are while and for loops.
340 static void handle_pre_loop(struct statement
*stmt
)
342 unsigned int path_orig
;
344 split_statements(stmt
->iterator_pre_statement
);
346 __split_true_false_paths();
349 if (stmt
->iterator_pre_condition
) {
350 __pass_to_client(stmt
->iterator_pre_condition
, WHOLE_CONDITION_HOOK
);
351 split_whole_condition(stmt
->iterator_pre_condition
);
353 path_orig
= split_path_id();
356 split_statements(stmt
->iterator_statement
);
357 split_statements(stmt
->iterator_post_statement
);
358 if (is_forever_loop(stmt
)) {
360 __pop_false_states();
363 __merge_false_states();
367 restore_path_id(path_orig
);
371 * Post loops are do {} while();
373 static void handle_post_loop(struct statement
*stmt
)
377 split_statements(stmt
->iterator_statement
);
379 __split_true_false_paths();
380 __pass_to_client(stmt
->iterator_post_condition
, WHOLE_CONDITION_HOOK
);
381 split_whole_condition(stmt
->iterator_post_condition
);
382 /* It would prossibly be cleaner to make this all one function */
384 __use_false_states();
387 if (is_forever_loop(stmt
)) {
396 static void split_statements(struct statement
*stmt
)
398 unsigned int path_orig
;
403 __smatch_lineno
= stmt
->pos
.line
;
404 __pass_to_client(stmt
, STMT_HOOK
);
406 switch (stmt
->type
) {
407 case STMT_DECLARATION
:
408 __pass_declarations_to_client(stmt
->declaration
);
409 split_symlist(stmt
->declaration
);
412 __pass_to_client(stmt
, RETURN_HOOK
);
413 split_expr(stmt
->ret_value
);
416 case STMT_EXPRESSION
:
417 split_expr(stmt
->expression
);
419 case STMT_COMPOUND
: {
421 FOR_EACH_PTR(stmt
->stmts
, s
) {
423 } END_FOR_EACH_PTR(s
);
427 __split_true_false_paths();
428 __pass_to_client(stmt
->if_conditional
, WHOLE_CONDITION_HOOK
);
429 split_whole_condition(stmt
->if_conditional
);
430 path_orig
= split_path_id();
432 split_statements(stmt
->if_true
);
434 __use_false_states();
435 split_statements(stmt
->if_false
);
436 __merge_true_states();
437 restore_path_id(path_orig
);
440 if (stmt
->iterator_pre_condition
)
441 handle_pre_loop(stmt
);
442 else if (stmt
->iterator_post_condition
)
443 handle_post_loop(stmt
);
445 // these are for(;;) type loops.
446 handle_pre_loop(stmt
);
450 split_expr(stmt
->switch_expression
);
451 __save_switch_states();
455 path_orig
= get_path_id();
456 split_statements(stmt
->switch_statement
);
457 if (!__pop_default())
461 restore_path_id(path_orig
);
466 if (!stmt
->case_expression
)
468 split_expr(stmt
->case_expression
);
469 split_expr(stmt
->case_to
);
470 split_statements(stmt
->case_statement
);
474 stmt
->label
->type
== SYM_LABEL
&&
475 stmt
->label
->ident
) {
476 __merge_gotos(stmt
->label
->ident
->name
);
478 split_statements(stmt
->label_statement
);
481 split_expr(stmt
->goto_expression
);
482 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
483 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
485 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
487 __process_continues();
489 } else if (stmt
->goto_label
&&
490 stmt
->goto_label
->type
== SYM_LABEL
&&
491 stmt
->goto_label
->ident
) {
492 __save_gotos(stmt
->goto_label
->ident
->name
);
499 split_expr(stmt
->asm_string
);
500 //split_expr(stmt->asm_outputs);
501 //split_expr(stmt->asm_inputs);
502 //split_expr(stmt->asm_clobbers);
507 split_expr(stmt
->range_expression
);
508 split_expr(stmt
->range_low
);
509 split_expr(stmt
->range_high
);
514 static void split_expr_list(struct expression_list
*expr_list
)
516 struct expression
*expr
;
517 FOR_EACH_PTR(expr_list
, expr
) {
519 } END_FOR_EACH_PTR(expr
);
523 static void split_sym(struct symbol
*sym
)
527 if (!(sym
->namespace & NS_SYMBOL
))
530 __pass_to_client(sym
, SYM_HOOK
);
531 split_statements(sym
->stmt
);
532 split_expr(sym
->array_size
);
533 split_symlist(sym
->arguments
);
534 split_symlist(sym
->symbol_list
);
535 split_statements(sym
->inline_stmt
);
536 split_symlist(sym
->inline_symbol_list
);
537 split_expr(sym
->initializer
);
540 static void split_symlist(struct symbol_list
*sym_list
)
544 FOR_EACH_PTR(sym_list
, sym
) {
546 } END_FOR_EACH_PTR(sym
);
549 static void split_functions(struct symbol_list
*sym_list
)
553 FOR_EACH_PTR(sym_list
, sym
) {
554 struct symbol
*base_type
;
555 base_type
= get_base_type(sym
);
556 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
558 line_func_start
= base_type
->stmt
->pos
.line
;
560 cur_func
= sym
->ident
->name
;
561 __smatch_lineno
= sym
->pos
.line
;
562 __pass_to_client(sym
, FUNC_DEF_HOOK
);
563 split_statements(base_type
->stmt
);
564 __pass_to_client(sym
, END_FUNC_HOOK
);
570 __pass_to_client(sym
, BASE_HOOK
);
572 } END_FOR_EACH_PTR(sym
);
575 void smatch (int argc
, char **argv
)
578 struct string_list
*filelist
= NULL
;
579 struct symbol_list
*sym_list
;
582 printf("Usage: smatch <filename.c>\n");
585 sparse_initialize(argc
, argv
, &filelist
);
586 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
587 sym_list
= __sparse(filename
);
588 split_functions(sym_list
);
589 } END_FOR_EACH_PTR_NOTAG(filename
);