2 * sparse/check_dereferences_param.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
11 * This is an --info recipe. The goal is to print a message for every parameter
12 * which we can not avoid dereferencing. This is maybe a bit restrictive but it
13 * avoids some false positives.
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
26 static int is_arg(struct expression
*expr
)
30 if (expr
->type
!= EXPR_SYMBOL
)
33 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
34 if (arg
== expr
->symbol
)
36 } END_FOR_EACH_PTR(arg
);
40 static void set_ignore(struct sm_state
*sm
)
42 if (sm
->state
== &derefed
)
44 set_state(my_id
, sm
->name
, sm
->sym
, &ignore
);
47 static void match_function_def(struct symbol
*sym
)
53 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
57 set_state(my_id
, arg
->ident
->name
, arg
, ¶m
);
58 } END_FOR_EACH_PTR(arg
);
61 static void check_deref(struct expression
*expr
)
63 struct expression
*tmp
;
66 tmp
= get_assigned_expr(expr
);
69 expr
= strip_expr(expr
);
73 if (implied_not_equal(expr
, 0))
76 sm
= get_sm_state_expr(my_id
, expr
);
77 if (sm
&& slist_has_state(sm
->possible
, &ignore
))
79 set_state_expr(my_id
, expr
, &derefed
);
82 static void match_dereference(struct expression
*expr
)
84 if (expr
->type
!= EXPR_PREOP
)
86 if (getting_address())
88 check_deref(expr
->unop
);
91 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
96 static void process_states(struct state_list
*slist
)
102 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
106 if (get_state_slist(slist
, my_id
, arg
->ident
->name
, arg
) == &derefed
)
107 sm_msg("info: dereferences_param %d %s", i
, global_static());
108 } END_FOR_EACH_PTR(arg
);
111 void check_dereferences_param(int id
)
118 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
120 add_hook(&match_dereference
, DEREF_HOOK
);
121 add_db_fn_call_callback(DEREFERENCE
, &set_param_dereferenced
);
122 add_modification_hook(my_id
, &set_ignore
);
124 all_return_states_hook(&process_states
);