Print fewer messages about unreached code
[smatch.git] / smatch_conditions.c
blob5285006eec27e111fe4fb38d1389437de5894f65
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 evaluate 'b', and 'd'.
27 * 'b' is true when we evaluate 'c' but otherwise we don't.
29 * The other thing that complicates matters is if we negate
30 * some if conditions.
31 * if (!a) { ...
32 * Smatch has passes the un-negated version to the client and flip
33 * the true and false values internally. This makes it easier
34 * to write checks.
36 * And negations can be part of a compound.
37 * if (a && !(b || c)) { d;
38 * In that situation we multiply the negative through to simplify
39 * stuff so that we can remove the parens like this:
40 * if (a && !b && !c) { d;
42 * One other thing is that:
43 * if ((a) != 0){ ...
44 * that's basically the same as testing for just 'a' and we simplify
45 * comparisons with zero before passing it to the script.
49 #include "smatch.h"
50 #include "smatch_slist.h"
51 #include "smatch_extra.h"
52 #include "smatch_expression_stacks.h"
54 static void split_conditions(struct expression *expr);
56 static int is_logical_and(struct expression *expr)
58 if (expr->op == SPECIAL_LOGICAL_AND)
59 return 1;
60 return 0;
63 static int handle_zero_comparisons(struct expression *expr)
65 struct expression *tmp = NULL;
67 // if left is zero or right is zero
68 if (is_zero(expr->left))
69 tmp = expr->right;
70 else if (is_zero(expr->right))
71 tmp = expr->left;
72 else
73 return 0;
75 // "if (foo != 0)" is the same as "if (foo)"
76 if (expr->op == SPECIAL_NOTEQUAL) {
77 split_conditions(tmp);
78 return 1;
81 // "if (foo == 0)" is the same as "if (!foo)"
82 if (expr->op == SPECIAL_EQUAL) {
83 split_conditions(tmp);
84 __negate_cond_stacks();
85 return 1;
88 return 0;
92 * This function is for handling calls to likely/unlikely
95 static int ignore_builtin_expect(struct expression *expr)
97 if (sym_name_is("__builtin_expect", expr->fn)) {
98 split_conditions(first_ptr_list((struct ptr_list *) expr->args));
99 return 1;
101 if (sym_name_is("__builtin_constant_p", expr->fn)) {
102 split_conditions(first_ptr_list((struct ptr_list *) expr->args));
103 return 1;
105 return 0;
109 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
112 static void handle_compound_stmt(struct statement *stmt)
114 struct expression *expr = NULL;
115 struct statement *last;
116 struct statement *s;
118 last = last_ptr_list((struct ptr_list *)stmt->stmts);
119 if (last->type != STMT_EXPRESSION) {
120 last = NULL;
121 } else {
122 expr = last->expression;
124 FOR_EACH_PTR(stmt->stmts, s) {
125 if (s != last)
126 __split_statements(s);
127 } END_FOR_EACH_PTR(s);
128 split_conditions(expr);
129 return;
132 static int handle_preop(struct expression *expr)
134 struct statement *stmt;
136 if (expr->op == '!') {
137 split_conditions(expr->unop);
138 __negate_cond_stacks();
139 return 1;
141 stmt = get_block_thing(expr);
142 if (stmt) {
143 handle_compound_stmt(stmt);
144 return 1;
146 return 0;
149 static void handle_logical(struct expression *expr)
152 * If we come to an "and" expr then:
153 * We split the left side.
154 * We keep all the current states.
155 * We split the right side.
156 * We keep all the states from both true sides.
158 * If it's an "or" expr then:
159 * We save the current slist.
160 * We split the left side.
161 * We use the false states for the right side.
162 * We split the right side.
163 * We save all the states that are the same on both sides.
166 split_conditions(expr->left);
168 if (!is_logical_and(expr))
169 __use_cond_false_states();
171 __push_cond_stacks();
173 __save_pre_cond_states();
174 split_conditions(expr->right);
175 __pop_pre_cond_states();
177 if (is_logical_and(expr)) {
178 __and_cond_states();
179 } else {
180 __or_cond_states();
182 __use_cond_true_states();
185 static void move_cur_to_tf(int tf)
187 struct sm_state *sm;
189 FOR_EACH_PTR(__fake_cur_slist, sm) {
190 if (tf)
191 __set_true_false_sm(sm, NULL);
192 else
193 __set_true_false_sm(NULL, sm);
194 } END_FOR_EACH_PTR(sm);
195 free_slist(&__fake_cur_slist);
200 * handle_select()
201 * if ((aaa()?bbb():ccc())) { ...
203 * This is almost the same as:
204 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
206 * It's a bit complicated because we shouldn't pass aaa()
207 * to the clients more than once.
210 static void handle_select(struct expression *expr)
212 split_conditions(expr->conditional);
214 __save_false_states_for_later();
216 __fake_cur++;
217 if (implied_condition_true(expr->cond_true)) {
218 __split_expr(expr->cond_true);
219 } else {
220 __push_cond_stacks();
221 split_conditions(expr->cond_true);
222 __and_cond_states();
224 move_cur_to_tf(TRUE);
226 if (implied_condition_false(expr->cond_false)) {
227 __split_expr(expr->cond_false);
228 __pop_pre_cond_states();
229 move_cur_to_tf(FALSE);
230 __fake_cur--;
231 return;
234 __use_previously_stored_false_states();
236 __save_pre_cond_states();
237 __push_cond_stacks();
238 split_conditions(expr->cond_false);
239 move_cur_to_tf(FALSE);
240 __fake_cur--;
241 __or_cond_states();
242 __pop_pre_cond_states();
244 __use_cond_true_states();
247 static void split_conditions(struct expression *expr)
250 sm_debug("%d in split_conditions type=%d\n", get_lineno(), expr->type);
252 expr = strip_expr(expr);
253 if (!expr)
254 return;
256 switch (expr->type) {
257 case EXPR_LOGICAL:
258 handle_logical(expr);
259 return;
260 case EXPR_COMPARE:
261 if (handle_zero_comparisons(expr))
262 return;
263 break;
264 case EXPR_CALL:
265 if (ignore_builtin_expect(expr))
266 return;
267 break;
268 case EXPR_PREOP:
269 if (handle_preop(expr))
270 return;
271 break;
272 case EXPR_CONDITIONAL:
273 case EXPR_SELECT:
274 handle_select(expr);
275 return;
278 /* fixme: this should be in smatch_flow.c
279 but because of the funny stuff we do with conditions
280 it's awkward to put it there. We would need to
281 call CONDITION_HOOK in smatch_flow as well.
283 push_expression(&big_expression_stack, expr);
284 if (expr->type == EXPR_COMPARE) {
285 if (expr->left->type != EXPR_POSTOP)
286 __split_expr(expr->left);
287 if (expr->right->type != EXPR_POSTOP)
288 __split_expr(expr->right);
289 } else {
290 __split_expr(expr);
292 __pass_to_client(expr, CONDITION_HOOK);
293 if (expr->type == EXPR_COMPARE) {
294 if (expr->left->type == EXPR_POSTOP)
295 __split_expr(expr->left);
296 if (expr->right->type == EXPR_POSTOP)
297 __split_expr(expr->right);
299 pop_expression(&big_expression_stack);
302 static int inside_condition;
303 void __split_whole_condition(struct expression *expr)
305 sm_debug("%d in __split_whole_condition\n", get_lineno());
306 inside_condition++;
307 __save_pre_cond_states();
308 __push_cond_stacks();
309 /* it's a hack, but it's sometimes handy to have this stuff
310 on the big_expression_stack. */
311 push_expression(&big_expression_stack, expr);
312 if (expr)
313 split_conditions(expr);
314 __use_cond_states();
315 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
316 pop_expression(&big_expression_stack);
317 inside_condition--;
318 sm_debug("%d done __split_whole_condition\n", get_lineno());
321 static int is_condition_assign(struct expression *expr)
323 struct expression *right;
325 right = strip_expr(expr->right);
326 switch(right->type) {
327 case EXPR_LOGICAL:
328 case EXPR_COMPARE:
329 break;
330 case EXPR_PREOP:
331 if (right->op == '!')
332 break;
333 return 0;
334 default:
335 return 0;
337 return 1;
340 int __handle_condition_assigns(struct expression *expr)
342 struct expression *right;
344 right = strip_expr(expr->right);
345 if (!is_condition_assign(expr))
346 return 0;
348 sm_debug("%d in __handle_condition_assigns\n", get_lineno());
349 inside_condition++;
350 __save_pre_cond_states();
351 __push_cond_stacks();
352 /* it's a hack, but it's sometimes handy to have this stuff
353 on the big_expression_stack. */
354 push_expression(&big_expression_stack, right);
355 split_conditions(right);
356 set_true_false_states_expr(SMATCH_EXTRA, expr->left, alloc_extra_state(1), alloc_extra_state(0));
357 __use_cond_states();
358 __pass_to_client(right, WHOLE_CONDITION_HOOK);
359 pop_expression(&big_expression_stack);
360 inside_condition--;
361 sm_debug("%d done __handle_condition_assigns\n", get_lineno());
363 __push_true_states();
364 __use_false_states();
365 __merge_true_states();
366 return 1;
369 int in_condition(void)
371 return inside_condition;