implied: handle implications for if (({whatever; foo;}) < 0) {
[smatch.git] / smatch_real_absolute.c
blob3009c8faccfc0024d2ff37080ddd23270fd305f4
1 /*
2 * Copyright (C) 2015 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 we have a line like:
20 * foo = bar / 8;
21 * Assume we don't know anything about bar. Well, now we know that foo is less
22 * than UINT_MAX / 8. Which might be useful, but it probably is misleading
23 * useless knowledge. Up to now we have ignored those but now we have said to
24 * store them.
26 * It also works if you have something like "foo = (int)(char)unknown_var;".
28 * I feel like this data doesn't have to be perfect, it just has to be better
29 * than nothing and that will help eliminate some false positives.
33 #include "smatch.h"
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
37 static int my_id;
39 static void pre_merge_hook(struct sm_state *sm)
41 struct smatch_state *abs;
42 struct smatch_state *extra;
43 struct range_list *rl;
45 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
46 if (!extra || !estate_rl(extra))
47 return;
48 abs = get_state(my_id, sm->name, sm->sym);
49 if (!abs || !estate_rl(abs)) {
50 set_state(my_id, sm->name, sm->sym, clone_estate(extra));
51 return;
53 rl = rl_intersection(estate_rl(abs), estate_rl(extra));
54 set_state(my_id, sm->name, sm->sym, alloc_estate_rl(clone_rl(rl)));
57 static struct smatch_state *empty_state(struct sm_state *sm)
59 return alloc_estate_empty();
62 static void reset(struct sm_state *sm, struct expression *mod_expr)
64 set_state(my_id, sm->name, sm->sym, alloc_estate_whole(estate_type(sm->state)));
67 static void match_assign(struct expression *expr)
69 struct range_list *rl;
70 struct symbol *type;
72 if (expr->op != '=')
73 return;
74 if (is_fake_call(expr->right))
75 return;
77 get_real_absolute_rl(expr->right, &rl);
79 type = get_type(expr->left);
80 if (!type)
81 return;
83 rl = cast_rl(type, rl);
84 if (sval_cmp(rl_max(rl), sval_type_max(type)) == 0 &&
85 sval_cmp(rl_min(rl), sval_type_min(type)))
86 return;
88 set_state_expr(my_id, expr->left, alloc_estate_rl(clone_rl(rl)));
91 struct smatch_state *get_real_absolute_state(struct expression *expr)
93 return get_state_expr(my_id, expr);
96 void register_real_absolute(int id)
98 my_id = id;
100 add_pre_merge_hook(my_id, &pre_merge_hook);
101 add_unmatched_state_hook(my_id, &empty_state);
102 add_merge_hook(my_id, &merge_estates);
103 add_modification_hook(my_id, &reset);
105 add_hook(&match_assign, ASSIGNMENT_HOOK);