Make cmp_tracker() accessible to outside files.
[smatch.git] / smatch_conditions.c
blobeac650b2701b03c433fea8035a60337fdb6609ec
1 /*
2 * sparse/smatch_conditions.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * The simplest type of condition is
12 * if (a) { ...
14 * The next simplest kind of conditions is
15 * if (a && b) { c;
16 * In that case 'a' is true when we get to 'b' and both are true
17 * when we get to c.
19 * Or's are a little more complicated.
20 * if (a || b) { c;
21 * We know 'a' is not true when we get to 'b' but it may be true
22 * when we get to c.
24 * If we mix and's and or's that's even more complicated.
25 * if (a && b && c || a && d) { d ;
26 * 'a' is true when we get to 'b', 'c' and 'd'.
27 * 'b' is true when we reach 'c' but otherwise we don't know.
29 * The other thing that complicates matters is if we negate
30 * some if conditions.
31 * if (!a) { ...
32 * We pass the un-negated version to the client and flip the true
33 * and false values internally.
35 * And negations can be part of a compound.
36 * if (a && !(b || c)) { d;
37 * In that situation we multiply the negative through to simplify
38 * stuff so that we can remove the parens like this:
39 * if (a && !b && !c) { d;
41 * One other thing is that:
42 * if ((a) != 0){ ...
43 * that's basically the same as testing for just 'a' so we simplify
44 * it before passing it to the script.
47 #include "smatch.h"
49 #define KERNEL
51 static void split_conditions(struct expression *expr);
53 static int is_logical_and(struct expression *expr)
55 if (expr->op == SPECIAL_LOGICAL_AND)
56 return 1;
57 return 0;
60 static int handle_zero_comparisons(struct expression *expr)
62 struct expression *tmp = NULL;
64 // if left is zero or right is zero
65 if (is_zero(expr->left))
66 tmp = expr->right;
67 else if (is_zero(expr->right))
68 tmp = expr->left;
69 else
70 return 0;
72 // "if (foo != 0)" is the same as "if (foo)"
73 if (expr->op == SPECIAL_NOTEQUAL) {
74 split_conditions(tmp);
75 return 1;
78 // "if (foo == 0)" is the same as "if (!foo)"
79 if (expr->op == SPECIAL_EQUAL) {
80 split_conditions(tmp);
81 __negate_cond_stacks();
82 return 1;
85 return 0;
89 * This function is for handling calls to likely/unlikely
92 static int ignore_builtin_expect(struct expression *expr)
94 if (sym_name_is("__builtin_expect", expr->fn)) {
95 split_conditions(first_ptr_list((struct ptr_list *) expr->args));
96 return 1;
98 if (sym_name_is("__builtin_constant_p", expr->fn)) {
99 split_conditions(first_ptr_list((struct ptr_list *) expr->args));
100 return 1;
102 return 0;
105 static void handle_compound_stmt(struct statement *stmt)
107 struct expression *expr = NULL;
108 struct statement *last;
109 struct statement *s;
111 last = last_ptr_list((struct ptr_list *)stmt->stmts);
112 if (last->type != STMT_EXPRESSION) {
113 last = NULL;
114 } else {
115 expr = last->expression;
117 FOR_EACH_PTR(stmt->stmts, s) {
118 if (s != last)
119 __split_statements(s);
120 } END_FOR_EACH_PTR(s);
121 split_conditions(expr);
122 return;
125 static int handle_preop(struct expression *expr)
127 struct statement *stmt;
129 if (expr->op == '!') {
130 split_conditions(expr->unop);
131 __negate_cond_stacks();
132 return 1;
134 stmt = get_block_thing(expr);
135 if (stmt) {
136 handle_compound_stmt(stmt);
137 return 1;
139 return 0;
142 static void handle_logical(struct expression *expr)
145 * If we come to an "and" expr then:
146 * We split the left side.
147 * We keep all the current states.
148 * We split the right side.
149 * We keep all the states from both true sides.
151 * If it's an "or" expr then:
152 * We save the current slist.
153 * We split the left side.
154 * We use the false states for the right side.
155 * We split the right side.
156 * We save all the states that are the same on both sides.
159 split_conditions(expr->left);
161 if (is_logical_and(expr)) {
162 __use_cond_true_states();
163 } else {
164 __use_cond_false_states();
167 __save_pre_cond_states();
168 __push_cond_stacks();
170 split_conditions(expr->right);
172 if (is_logical_and(expr)) {
173 __and_cond_states();
174 } else {
175 __or_cond_states();
178 __pop_pre_cond_states();
179 __use_cond_true_states();
182 static void handle_select(struct expression *expr)
185 * if ((aaa()?bbb():ccc())) { ...
187 * This is almost the same as:
188 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
190 * It's a bit complicated because we shouldn't pass aaa()
191 * to the clients more than once.
194 split_conditions(expr->conditional);
196 __save_false_states_for_later();
197 __use_cond_true_states();
199 if (known_condition_true(expr->cond_true)) {
200 __split_expr(expr->cond_true);
201 } else {
202 __push_cond_stacks();
203 split_conditions(expr->cond_true);
204 __and_cond_states();
207 if (known_condition_false(expr->cond_false)) {
208 __pop_pre_cond_states();
209 __use_cond_true_states();
210 return;
213 __use_previously_stored_false_states();
215 __save_pre_cond_states();
216 __push_cond_stacks();
217 split_conditions(expr->cond_false);
218 __or_cond_states();
219 __pop_pre_cond_states();
221 __use_cond_true_states();
224 static void split_conditions(struct expression *expr)
227 SM_DEBUG("%d in split_conditions type=%d\n", get_lineno(), expr->type);
229 expr = strip_expr(expr);
230 if (!expr)
231 return;
233 switch(expr->type) {
234 case EXPR_LOGICAL:
235 handle_logical(expr);
236 return;
237 case EXPR_COMPARE:
238 if (handle_zero_comparisons(expr))
239 return;
240 break;
241 case EXPR_CALL:
242 if (ignore_builtin_expect(expr))
243 return;
244 break;
245 case EXPR_PREOP:
246 if (handle_preop(expr))
247 return;
248 break;
249 case EXPR_CONDITIONAL:
250 case EXPR_SELECT:
251 handle_select(expr);
252 return;
255 __pass_to_client(expr, CONDITION_HOOK);
256 __split_expr(expr);
259 static int inside_condition;
260 void __split_whole_condition(struct expression *expr)
262 SM_DEBUG("%d in __split_whole_condition\n", get_lineno());
263 inside_condition++;
264 __save_pre_cond_states();
265 __push_cond_stacks();
266 if (expr)
267 split_conditions(expr);
268 __use_cond_states();
269 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
270 inside_condition--;
273 int in_condition()
275 return inside_condition;