db, stree: update smatch_db.c to use stree throughout
[smatch.git] / smatch_param_set.c
bloba5a747b59fed2c9bdf24fcfab5ab02a2bc54c0d5
1 /*
2 * Copyright (C) 2012 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * This is for functions like:
21 * int foo(int *x)
22 * {
23 * if (*x == 42) {
24 * *x = 0;
25 * return 1;
26 * }
27 * return 0;
28 * }
30 * If we return 1 that means the value of *x has been set to 0. If we return
31 * 0 then we have left *x alone.
35 #include "scope.h"
36 #include "smatch.h"
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
40 static int my_id;
42 static struct smatch_state *unmatched_state(struct sm_state *sm)
44 return alloc_estate_empty();
47 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
49 set_state(my_id, name, sym, state);
52 static void print_one_return_value_param(int return_id, char *return_ranges,
53 int param, struct sm_state *sm, char *implied_rl)
55 const char *param_name;
57 param_name = get_param_name(sm);
58 if (!param_name)
59 return;
60 if (strcmp(param_name, "$$") == 0)
61 return;
63 sql_insert_return_states(return_id, return_ranges, ADDED_VALUE, param,
64 param_name, implied_rl);
67 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
69 struct state_list *my_slist;
70 struct sm_state *sm;
71 struct smatch_state *extra;
72 int param;
73 struct range_list *rl;
75 my_slist = get_all_states(my_id);
77 FOR_EACH_PTR(my_slist, sm) {
78 if (!estate_rl(sm->state))
79 continue;
80 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
81 if (!estate_rl(extra))
82 continue;
83 rl = rl_intersection(estate_rl(sm->state), estate_rl(extra));
84 if (!rl)
85 continue;
87 param = get_param_num_from_sym(sm->sym);
88 if (param < 0)
89 continue;
90 if (!sm->sym->ident)
91 continue;
92 print_one_return_value_param(return_id, return_ranges, param, sm, show_rl(rl));
93 } END_FOR_EACH_PTR(sm);
96 void register_param_set(int id)
98 my_id = id;
100 add_extra_mod_hook(&extra_mod_hook);
101 add_unmatched_state_hook(my_id, &unmatched_state);
102 add_merge_hook(my_id, &merge_estates);
103 add_split_return_callback(&print_return_value_param);