flow: re-write how struct members are initialized
[smatch.git] / smatch_param_set.c
blobd56f1b9a705d19eeecd5c3f86fe37bd86312a468
1 /*
2 * sparse/smatch_param_set.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is for functions like:
13 * int foo(int *x)
14 * {
15 * if (*x == 42) {
16 * *x = 0;
17 * return 1;
18 * }
19 * return 0;
20 * }
22 * If we return 1 that means the value of *x has been set to 0. If we return
23 * 0 then we have left *x alone.
27 #include "scope.h"
28 #include "smatch.h"
29 #include "smatch_slist.h"
30 #include "smatch_extra.h"
32 static int my_id;
34 static struct smatch_state *unmatched_state(struct sm_state *sm)
36 return alloc_estate_empty();
39 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
41 set_state(my_id, name, sym, state);
44 static void print_one_return_value_param(int return_id, char *return_ranges,
45 int param, struct sm_state *sm, char *implied_rl)
47 const char *param_name;
49 param_name = get_param_name(sm);
50 if (!param_name)
51 return;
52 if (strcmp(param_name, "$$") == 0)
53 return;
55 sql_insert_return_states(return_id, return_ranges, ADDED_VALUE, param,
56 param_name, implied_rl);
59 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
61 struct state_list *my_slist;
62 struct sm_state *sm;
63 struct smatch_state *extra;
64 int param;
65 struct range_list *rl;
67 my_slist = get_all_states(my_id);
69 FOR_EACH_PTR(my_slist, sm) {
70 if (!estate_rl(sm->state))
71 continue;
72 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
73 if (!estate_rl(extra))
74 continue;
75 rl = rl_intersection(estate_rl(sm->state), estate_rl(extra));
76 if (!rl)
77 continue;
79 param = get_param_num_from_sym(sm->sym);
80 if (param < 0)
81 continue;
82 if (!sm->sym->ident)
83 continue;
84 print_one_return_value_param(return_id, return_ranges, param, sm, show_rl(rl));
85 } END_FOR_EACH_PTR(sm);
88 void register_param_set(int id)
90 my_id = id;
92 add_extra_mod_hook(&extra_mod_hook);
93 add_unmatched_state_hook(my_id, &unmatched_state);
94 add_merge_hook(my_id, &merge_estates);
95 add_split_return_callback(&print_return_value_param);