user_data, db: improve how returned user data is handled
[smatch.git] / check_db_info.c
blob7aca83ca368cd3188c1c7784bd75dd32e6031bc8
1 /*
2 * smatch/check_db_info.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
11 #include "smatch_extra.h"
13 static int my_id;
15 static struct range_list *return_ranges;
16 static struct ptr_list *backup;
18 static void add_return_range(struct range_list *rl)
20 rl = cast_rl(cur_func_return_type(), rl);
21 if (!return_ranges) {
22 return_ranges = rl;
23 return;
25 return_ranges = rl_union(return_ranges, rl);
28 static void match_return(struct expression *ret_value)
30 struct range_list *rl;
32 ret_value = strip_expr(ret_value);
33 if (!ret_value)
34 return;
36 if (get_implied_rl(ret_value, &rl)) {
37 add_return_range(rl);
38 } else {
39 struct symbol *data_type = get_type(ret_value);
40 struct symbol *func_type = cur_func_return_type();
42 add_return_range(cast_rl(func_type, alloc_whole_rl(data_type)));
46 static void match_end_func(struct symbol *sym)
48 if (!return_ranges)
49 return;
50 sql_insert_return_values(show_rl(return_ranges));
51 return_ranges = NULL;
54 static void match_save_states(struct expression *expr)
56 __add_ptr_list(&backup, return_ranges, 0);
57 return_ranges = NULL;
60 static void match_restore_states(struct expression *expr)
62 return_ranges = last_ptr_list(backup);
63 delete_ptr_list_last(&backup);
66 void check_db_info(int id)
68 my_id = id;
69 add_hook(&match_return, RETURN_HOOK);
70 add_hook(&match_end_func, END_FUNC_HOOK);
71 add_hook(&match_save_states, INLINE_FN_START);
72 add_hook(&match_restore_states, INLINE_FN_END);