Remove everything to do with path_id's. I couldn't tell what this
[smatch.git] / smatch_conditions.c
blob6a1d70cedb011c4ecff72e383f620aef103228dc
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 int __ors = 0;
52 int __ands = 0;
54 static int negative = 0;
55 int __negate()
57 return negative;
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))
70 return 1;
71 return 0;
74 static int is_logical_or(struct expression *expr)
76 if ((!__negate() && expr->op == SPECIAL_LOGICAL_OR) ||
77 (__negate() && expr->op == SPECIAL_LOGICAL_AND))
78 return 1;
79 return 0;
82 static void inc_ands_ors(struct expression *expr)
84 if (is_logical_and(expr))
85 __ands++;
86 else if (is_logical_or(expr))
87 __ors++;
90 static void dec_ands_ors(struct expression *expr)
92 if (is_logical_and(expr))
93 __ands--;
94 else if (is_logical_or(expr))
95 __ors--;
98 static void special_kernel_macros(struct expression *expr)
100 #ifdef KERNEL
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);
107 } else {
108 __pass_to_client(expr, CONDITION_HOOK);
109 __split_expr(expr);
110 return;
113 #endif
116 static int is_zero(struct expression *expr)
118 if (expr->type == EXPR_VALUE && expr->value == 0)
119 return 1;
120 if (expr->op == '(')
121 return is_zero(expr->unop);
122 if (expr->type == EXPR_CAST)
123 return is_zero(expr->cast_expression);
124 return 0;
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))
133 tmp = expr->right;
134 else if (is_zero(expr->right))
135 tmp = expr->left;
136 else
137 return 0;
139 // "if (foo != 0)" is the same as "if (foo)"
140 if (expr->op == SPECIAL_NOTEQUAL) {
141 split_conditions(tmp);
142 return 1;
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;
150 return 1;
153 return 0;
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))
164 return;
166 if (expr->type == EXPR_LOGICAL) {
167 inc_ands_ors(expr);
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) {
177 __ors_reached = 1;
178 __first_and_clump();
179 } else {
180 __merge_and_clump();
182 __use_false_states_mini();
183 split_conditions(expr->right);
185 dec_ands_ors(expr);
187 if (__ands + __ors == 0) {
188 __merge_and_clump();
189 __use_and_clumps();
190 __ors_reached = 0;
193 return;
194 } else if (expr->type == EXPR_PREOP && expr->op == '!') {
195 negative = (negative + 1)%2;
196 split_conditions(expr->unop);
197 negative = (negative + 1)%2;
198 return;
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);
206 __split_expr(expr);
207 return;
209 special_kernel_macros(expr);
210 } else {
211 __pass_to_client(expr, CONDITION_HOOK);
212 __split_expr(expr);
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());