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 void special_kernel_macros(struct expression
*expr
)
101 struct expression
*tmp
;
103 tmp
= first_ptr_list((struct ptr_list
*) expr
->args
);
104 if (tmp
->op
== '!' && tmp
->unop
->op
== '!'
105 && tmp
->unop
->unop
->op
== '(') {
106 split_conditions(tmp
->unop
->unop
->unop
);
108 __pass_to_client(expr
, CONDITION_HOOK
);
116 static int is_zero(struct expression
*expr
)
118 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 0)
121 return is_zero(expr
->unop
);
122 if (expr
->type
== EXPR_CAST
)
123 return is_zero(expr
->cast_expression
);
127 static int handle_zero_comparisons(struct expression
*expr
)
129 struct expression
*tmp
= NULL
;
131 // if left is zero or right is zero
132 if (is_zero(expr
->left
))
134 else if (is_zero(expr
->right
))
139 // "if (foo != 0)" is the same as "if (foo)"
140 if (expr
->op
== SPECIAL_NOTEQUAL
) {
141 split_conditions(tmp
);
145 // "if (foo == 0)" is the same as "if (!foo)"
146 if (expr
->op
== SPECIAL_EQUAL
) {
147 negative
= (negative
+ 1)%2;
148 split_conditions(tmp
);
149 negative
= (negative
+ 1)%2;
156 static void split_conditions(struct expression
*expr
)
158 static int __ors_reached
;
160 SM_DEBUG("%d in split_conditions type=%d\n", get_lineno(), expr
->type
);
162 if (expr
->type
== EXPR_COMPARE
)
163 if (handle_zero_comparisons(expr
))
166 if (expr
->type
== EXPR_LOGICAL
) {
168 __split_false_states_mini();
170 split_conditions(expr
->left
);
172 if (is_logical_and(expr
)) {
173 __pop_false_states_mini();
174 split_conditions(expr
->right
);
175 } else if (is_logical_or(expr
)) {
176 if (!__ors_reached
) {
182 __use_false_states_mini();
183 split_conditions(expr
->right
);
187 if (__ands
+ __ors
== 0) {
194 } else if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!') {
195 negative
= (negative
+ 1)%2;
196 split_conditions(expr
->unop
);
197 negative
= (negative
+ 1)%2;
199 } else if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(') {
200 split_conditions(expr
->unop
);
201 } else if (expr
->type
== EXPR_CALL
) {
203 if (expr
->fn
->type
!= EXPR_SYMBOL
||
204 strcmp("__builtin_expect", expr
->fn
->symbol_name
->name
)) {
205 __pass_to_client(expr
, CONDITION_HOOK
);
209 special_kernel_macros(expr
);
211 __pass_to_client(expr
, CONDITION_HOOK
);
216 void __split_whole_condition(struct expression
*expr
)
219 __pass_to_client(expr
, WHOLE_CONDITION_HOOK
);
220 split_conditions(expr
);
221 SM_DEBUG("%d __ands = %d __ors = %d __negate() = %d\n", get_lineno(),
222 __ands
, __ors
, __negate());