kernel.return_fixes: add mipi_dsi_device_transfer(), timer_delete() and get_device()
[smatch.git] / smatch_real_absolute.c
blob2c6f1db2fa2abcd678ffcb31be6f0c561ba0d937
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 void set_real_absolute(struct expression *expr, struct smatch_state *state)
41 set_state_expr(my_id, expr, clone_estate(state));
44 static void extra_mod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
46 struct smatch_state *abs;
47 struct range_list *rl;
49 abs = get_state(my_id, name, sym);
50 if (!abs || !estate_rl(abs))
51 return;
52 rl = rl_intersection(estate_rl(abs), estate_rl(state));
53 set_state(my_id, name, sym, alloc_estate_rl(clone_rl(rl)));
56 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
58 struct smatch_state *extra;
59 struct range_list *rl;
61 extra = get_state(SMATCH_EXTRA, cur->name, cur->sym);
62 if (!extra || !estate_rl(extra))
63 return;
64 if (!estate_rl(cur->state)) {
65 set_state(my_id, cur->name, cur->sym, clone_estate(extra));
66 return;
68 rl = rl_intersection(estate_rl(cur->state), estate_rl(extra));
69 set_state(my_id, cur->name, cur->sym, alloc_estate_rl(clone_rl(rl)));
72 static struct smatch_state *empty_state(struct sm_state *sm)
74 return alloc_estate_empty();
77 static int in_iterator_pre_statement(void)
79 struct statement *stmt;
82 * we can't use __cur_stmt because that isn't set for
83 * iterator_pre_statement. Kind of a mess.
87 stmt = last_ptr_list((struct ptr_list *)big_statement_stack);
89 if (!stmt || !stmt->parent)
90 return 0;
91 if (stmt->parent->type != STMT_ITERATOR)
92 return 0;
93 if (stmt->parent->iterator_pre_statement != stmt)
94 return 0;
95 return 1;
98 static bool get_absolute(struct expression *expr, struct range_list **rl)
100 struct smatch_state *state;
102 if (__in_fake_struct_assign) {
103 state = get_state_expr(my_id, expr);
104 if (!state)
105 return false;
106 *rl = estate_rl(state);
107 return true;
109 get_real_absolute_rl(expr, rl);
110 return true;
113 static void match_assign(struct expression *expr)
115 struct expression *right;
116 struct range_list *rl;
117 struct symbol *type;
118 sval_t sval;
120 if (expr->op != '=')
121 goto clear;
122 right = strip_expr(expr->right);
123 if (right->type == EXPR_CALL)
124 goto clear;
125 if (in_iterator_pre_statement())
126 goto clear;
128 if (!get_absolute(expr->right, &rl))
129 goto clear;
131 type = get_type(expr->left);
132 if (!type)
133 goto clear;
134 if (type->type != SYM_BASETYPE && type->type != SYM_ENUM)
135 goto clear;
137 rl = cast_rl(type, rl);
138 if (is_whole_rl(rl) && !get_state_expr(my_id, expr->left))
139 goto clear;
140 /* These are handled by smatch_extra.c */
141 if (rl_to_sval(rl, &sval) && !get_state_expr(my_id, expr->left))
142 goto clear;
144 set_state_expr(my_id, expr->left, alloc_estate_rl(clone_rl(rl)));
145 return;
147 clear:
148 if (get_state_expr(my_id, expr->left))
149 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
152 struct smatch_state *get_real_absolute_state(struct expression *expr)
154 return get_state_expr(my_id, expr);
157 struct smatch_state *get_real_absolute_state_var_sym(const char *name, struct symbol *sym)
159 return __get_state(my_id, name, sym);
162 void register_real_absolute(int id)
164 my_id = id;
166 set_dynamic_states(my_id);
167 add_pre_merge_hook(my_id, &pre_merge_hook);
168 add_unmatched_state_hook(my_id, &empty_state);
169 add_merge_hook(my_id, &merge_estates);
170 add_extra_mod_hook(&extra_mod_hook);
172 add_hook(&match_assign, ASSIGNMENT_HOOK);