2 * sparse/smatch_conditions.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * The simplest type of condition is
14 * The next simplest kind of conditions is
16 * In that case 'a' is true when we get to 'b' and both are true
19 * Or's are a little more complicated.
21 * We know 'a' is not true when we get to 'b' but it may be true
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
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:
43 * that's basically the same as testing for just 'a' so we simplify
44 * it before passing it to the script.
54 static int negative
= 0;
60 static void split_conditions(struct expression
*expr
);
62 static int is_logical_and(struct expression
*expr
)
64 /* If you have if (!(a && b)) smatch translates that to
65 * if (!a || !b). Logically those are the same.
68 if ((!__negate() && expr
->op
== SPECIAL_LOGICAL_AND
) ||
69 (__negate() && expr
->op
== SPECIAL_LOGICAL_OR
))
74 static int is_logical_or(struct expression
*expr
)
76 if ((!__negate() && expr
->op
== SPECIAL_LOGICAL_OR
) ||
77 (__negate() && expr
->op
== SPECIAL_LOGICAL_AND
))
82 static void inc_ands_ors(struct expression
*expr
)
84 if (is_logical_and(expr
))
86 else if (is_logical_or(expr
))
90 static void dec_ands_ors(struct expression
*expr
)
92 if (is_logical_and(expr
))
94 else if (is_logical_or(expr
))
98 static int is_zero(struct expression
*expr
)
100 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 0)
103 return is_zero(expr
->unop
);
104 if (expr
->type
== EXPR_CAST
)
105 return is_zero(expr
->cast_expression
);
109 static int handle_zero_comparisons(struct expression
*expr
)
111 struct expression
*tmp
= NULL
;
113 // if left is zero or right is zero
114 if (is_zero(expr
->left
))
116 else if (is_zero(expr
->right
))
121 // "if (foo != 0)" is the same as "if (foo)"
122 if (expr
->op
== SPECIAL_NOTEQUAL
) {
123 split_conditions(tmp
);
127 // "if (foo == 0)" is the same as "if (!foo)"
128 if (expr
->op
== SPECIAL_EQUAL
) {
129 negative
= (negative
+ 1)%2;
130 split_conditions(tmp
);
131 negative
= (negative
+ 1)%2;
139 * This function is for handling calls to likely/unlikely
142 static int ignore_builtin_expect(struct expression
*expr
)
144 if (sym_name_is("__builtin_expect", expr
->fn
)) {
145 split_conditions(first_ptr_list((struct ptr_list
*) expr
->args
));
151 static int handle_preop(struct expression
*expr
)
153 if (expr
->op
== '!') {
154 negative
= (negative
+ 1)%2;
155 split_conditions(expr
->unop
);
156 negative
= (negative
+ 1)%2;
159 if (expr
->op
== '(') {
160 split_conditions(expr
->unop
);
166 static void handle_logical(struct expression
*expr
)
168 static int __ors_reached
;
171 __split_false_states_mini();
173 split_conditions(expr
->left
);
175 if (is_logical_and(expr
)) {
176 SM_DEBUG("%d and\n", get_lineno());
177 __pop_false_states_mini();
178 split_conditions(expr
->right
);
179 } else if (is_logical_or(expr
)) {
180 SM_DEBUG("%d or\n", get_lineno());
181 if (!__ors_reached
) {
187 __use_false_states_mini();
188 split_conditions(expr
->right
);
192 if (__ands
+ __ors
== 0) {
199 static void split_conditions(struct expression
*expr
)
202 SM_DEBUG("%d in split_conditions type=%d\n", get_lineno(), expr
->type
);
206 handle_logical(expr
);
209 if (handle_zero_comparisons(expr
))
213 if (ignore_builtin_expect(expr
))
217 if (handle_preop(expr
))
222 __pass_to_client(expr
, CONDITION_HOOK
);
226 void __split_whole_condition(struct expression
*expr
)
229 __pass_to_client(expr
, WHOLE_CONDITION_HOOK
);
230 split_conditions(expr
);
231 SM_DEBUG("%d __ands = %d __ors = %d __negate() = %d\n", get_lineno(),
232 __ands
, __ors
, __negate());