helper: keep a small cache of recently used variable names
[smatch.git] / smatch_imaginary_absolute.c
blob6532dfc3978b70bede61dfc1b4be167eac372d45
1 /*
2 * Copyright (C) 2016 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 * Say you have a condition like:
21 * if (foo < 0)
22 * return foo;
24 * But we actually know that foo is zero. Then in smatch_extra.c we set "foo"
25 * to the empty state and then for the return statement we say that "foo" is
26 * s32min-s32max because we can't return the empty state.
28 * This file is supposed to provide an alternative to say that actually "foo" is
29 * less than zero.
33 #include "smatch.h"
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
37 static int my_id;
39 static struct smatch_state *empty_state(struct sm_state *sm)
41 return alloc_estate_empty();
44 struct smatch_state *merge_is_empty(struct smatch_state *s1, struct smatch_state *s2)
46 return alloc_estate_empty();
49 static void reset(struct sm_state *sm, struct expression *mod_expr)
51 set_state(my_id, sm->name, sm->sym, alloc_estate_empty());
54 void __save_imaginary_state(struct expression *expr, struct range_list *true_rl, struct range_list *false_rl)
56 set_true_false_states_expr(my_id, expr, alloc_estate_rl(true_rl), alloc_estate_rl(false_rl));
59 int get_imaginary_absolute(struct expression *expr, struct range_list **rl)
61 struct smatch_state *state;
63 *rl = NULL;
65 state = get_state_expr(my_id, expr);
66 if (!state || !estate_rl(state))
67 return 0;
69 *rl = estate_rl(state);
70 return 1;
73 void register_imaginary_absolute(int id)
75 my_id = id;
77 add_unmatched_state_hook(my_id, &empty_state);
78 add_merge_hook(my_id, &merge_is_empty);
79 add_modification_hook(my_id, &reset);