smatch.h: introduce sm_pedantic()
[smatch.git] / smatch_imaginary_absolute.c
blob3b401bd965f92b83ff79e7d8c3430bb118acad53
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 if (__in_pre_condition)
57 return;
58 set_true_false_states_expr(my_id, expr, alloc_estate_rl(true_rl), alloc_estate_rl(false_rl));
61 int get_imaginary_absolute(struct expression *expr, struct range_list **rl)
63 struct smatch_state *state;
65 *rl = NULL;
67 state = get_state_expr(my_id, expr);
68 if (!state || !estate_rl(state))
69 return 0;
71 *rl = estate_rl(state);
72 return 1;
75 void register_imaginary_absolute(int id)
77 my_id = id;
79 set_dynamic_states(my_id);
80 add_unmatched_state_hook(my_id, &empty_state);
81 add_merge_hook(my_id, &merge_is_empty);
82 add_modification_hook(my_id, &reset);