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.
55 static void split_conditions(struct expression
*expr
);
57 static int is_logical_and(struct expression
*expr
)
59 /* If you have if (!(a && b)) smatch translates that to
60 * if (!a || !b). Logically those are the same.
63 if ((!__negate
&& expr
->op
== SPECIAL_LOGICAL_AND
) ||
64 (__negate
&& expr
->op
== SPECIAL_LOGICAL_OR
))
69 static int is_logical_or(struct expression
*expr
)
71 if ((!__negate
&& expr
->op
== SPECIAL_LOGICAL_OR
) ||
72 (__negate
&& expr
->op
== SPECIAL_LOGICAL_AND
))
77 static void inc_ands_ors(struct expression
*expr
)
79 if (is_logical_and(expr
))
81 else if (is_logical_or(expr
))
85 static void dec_ands_ors(struct expression
*expr
)
87 if (is_logical_and(expr
))
89 else if (is_logical_or(expr
))
93 static void special_kernel_macros(struct expression
*expr
)
96 struct expression
*tmp
;
98 tmp
= first_ptr_list((struct ptr_list
*) expr
->args
);
99 if (tmp
->op
== '!' && tmp
->unop
->op
== '!'
100 && tmp
->unop
->unop
->op
== '(') {
101 split_conditions(tmp
->unop
->unop
->unop
);
103 __pass_to_client(expr
, CONDITION_HOOK
);
111 static int is_zero(struct expression
*expr
)
113 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 0)
116 return is_zero(expr
->unop
);
117 if (expr
->type
== EXPR_CAST
)
118 return is_zero(expr
->cast_expression
);
122 static int handle_zero_comparisons(struct expression
*expr
)
124 struct expression
*tmp
= NULL
;
126 // if left is zero or right is zero
127 if (is_zero(expr
->left
))
129 else if (is_zero(expr
->right
))
134 // "if (foo != 0)" is the same as "if (foo)"
135 if (expr
->op
== SPECIAL_NOTEQUAL
) {
136 split_conditions(tmp
);
140 // "if (foo == 0)" is the same as "if (!foo)"
141 if (expr
->op
== SPECIAL_EQUAL
) {
142 __negate
= (__negate
+ 1)%2;
143 split_conditions(tmp
);
144 __negate
= (__negate
+ 1)%2;
151 static void split_conditions(struct expression
*expr
)
153 static int __ors_reached
;
155 SM_DEBUG("%d in split_conditions type=%d\n", get_lineno(), expr
->type
);
157 if (expr
->type
== EXPR_COMPARE
)
158 if (handle_zero_comparisons(expr
))
161 if (expr
->type
== EXPR_LOGICAL
) {
162 unsigned int path_orig
;
165 path_orig
= __split_path_id();
166 __split_false_states_mini();
168 split_conditions(expr
->left
);
170 if (is_logical_and(expr
)) {
172 __pop_false_states_mini();
173 split_conditions(expr
->right
);
174 } else if (is_logical_or(expr
)) {
175 if (!__ors_reached
) {
182 __use_false_states_mini();
183 split_conditions(expr
->right
);
187 if (__ands
+ __ors
== 0) {
193 __restore_path_id(path_orig
);
195 } else if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!') {
196 __negate
= (__negate
+ 1)%2;
197 split_conditions(expr
->unop
);
198 __negate
= (__negate
+ 1)%2;
200 } else if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(') {
201 split_conditions(expr
->unop
);
202 } else if (expr
->type
== EXPR_CALL
) {
204 if (expr
->fn
->type
!= EXPR_SYMBOL
||
205 strcmp("__builtin_expect", expr
->fn
->symbol_name
->name
)) {
206 __pass_to_client(expr
, CONDITION_HOOK
);
210 special_kernel_macros(expr
);
212 __pass_to_client(expr
, CONDITION_HOOK
);
217 void __split_whole_condition(struct expression
*expr
)
220 split_conditions(expr
);
221 SM_DEBUG("%d __ands = %d __ors = %d __negate = %d\n", get_lineno(),
222 __ands
, __ors
, __negate
);