Print this page
new smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_conditions.c
+++ new/usr/src/tools/smatch/src/smatch_conditions.c
1 1 /*
2 2 * Copyright (C) 2006,2008 Dan Carpenter.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 /*
19 19 * The simplest type of condition is
20 20 * if (a) { ...
21 21 *
22 22 * The next simplest kind of conditions is
23 23 * if (a && b) { c;
24 24 * In that case 'a' is true when we get to 'b' and both are true
25 25 * when we get to c.
26 26 *
27 27 * Or's are a little more complicated.
28 28 * if (a || b) { c;
29 29 * We know 'a' is not true when we get to 'b' but it may be true
30 30 * when we get to c.
31 31 *
32 32 * If we mix and's and or's that's even more complicated.
33 33 * if (a && b && c || a && d) { d ;
34 34 * 'a' is true when we evaluate 'b', and 'd'.
35 35 * 'b' is true when we evaluate 'c' but otherwise we don't.
36 36 *
37 37 * The other thing that complicates matters is if we negate
38 38 * some if conditions.
39 39 * if (!a) { ...
40 40 * Smatch has passes the un-negated version to the client and flip
41 41 * the true and false values internally. This makes it easier
42 42 * to write checks.
43 43 *
44 44 * And negations can be part of a compound.
45 45 * if (a && !(b || c)) { d;
46 46 * In that situation we multiply the negative through to simplify
47 47 * stuff so that we can remove the parens like this:
48 48 * if (a && !b && !c) { d;
49 49 *
50 50 * One other thing is that:
51 51 * if ((a) != 0){ ...
52 52 * that's basically the same as testing for just 'a' and we simplify
53 53 * comparisons with zero before passing it to the script.
54 54 *
55 55 */
56 56
57 57 #include "smatch.h"
58 58 #include "smatch_slist.h"
59 59 #include "smatch_extra.h"
60 60 #include "smatch_expression_stacks.h"
61 61
62 62 extern int __expr_stmt_count;
63 63
64 64 struct expression_list *big_condition_stack;
65 65
66 66 static void split_conditions(struct expression *expr);
67 67
68 68 static int is_logical_and(struct expression *expr)
69 69 {
70 70 if (expr->op == SPECIAL_LOGICAL_AND)
↓ open down ↓ |
70 lines elided |
↑ open up ↑ |
71 71 return 1;
72 72 return 0;
73 73 }
74 74
75 75 static int handle_zero_comparisons(struct expression *expr)
76 76 {
77 77 struct expression *tmp = NULL;
78 78 struct expression *zero;
79 79
80 80 // if left is zero or right is zero
81 - if (is_zero(expr->left)) {
81 + if (expr_is_zero(expr->left)) {
82 82 zero = strip_expr(expr->left);
83 83 if (zero->type != EXPR_VALUE)
84 84 __split_expr(expr->left);
85 85 tmp = expr->right;
86 - } else if (is_zero(expr->right)) {
86 + } else if (expr_is_zero(expr->right)) {
87 87 zero = strip_expr(expr->left);
88 88 if (zero->type != EXPR_VALUE)
89 89 __split_expr(expr->right);
90 90 tmp = expr->left;
91 91 } else {
92 92 return 0;
93 93 }
94 94
95 95 // "if (foo != 0)" is the same as "if (foo)"
96 96 if (expr->op == SPECIAL_NOTEQUAL) {
97 97 split_conditions(tmp);
98 98 return 1;
99 99 }
100 100
101 101 // "if (foo == 0)" is the same as "if (!foo)"
102 102 if (expr->op == SPECIAL_EQUAL) {
103 103 split_conditions(tmp);
104 104 __negate_cond_stacks();
105 105 return 1;
106 106 }
107 107
108 108 return 0;
109 109 }
110 110
111 111 /*
112 112 * This function is for handling calls to likely/unlikely
113 113 */
114 114
115 115 static int ignore_builtin_expect(struct expression *expr)
116 116 {
117 117 if (sym_name_is("__builtin_expect", expr->fn)) {
118 118 split_conditions(first_ptr_list((struct ptr_list *) expr->args));
119 119 return 1;
120 120 }
121 121 return 0;
122 122 }
123 123
124 124 /*
125 125 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
126 126 */
127 127
128 128 static void handle_compound_stmt(struct statement *stmt)
129 129 {
130 130 struct expression *expr = NULL;
131 131 struct statement *last;
132 132 struct statement *s;
133 133
134 134 last = last_ptr_list((struct ptr_list *)stmt->stmts);
135 135 if (last->type == STMT_LABEL) {
136 136 if (last->label_statement &&
137 137 last->label_statement->type == STMT_EXPRESSION)
138 138 expr = last->label_statement->expression;
139 139 else
140 140 last = NULL;
141 141 } else if (last->type != STMT_EXPRESSION) {
142 142 last = NULL;
143 143 } else {
144 144 expr = last->expression;
145 145 }
146 146
147 147 FOR_EACH_PTR(stmt->stmts, s) {
148 148 if (s != last)
149 149 __split_stmt(s);
150 150 } END_FOR_EACH_PTR(s);
151 151 if (last && last->type == STMT_LABEL)
152 152 __split_label_stmt(last);
153 153 split_conditions(expr);
154 154 }
155 155
156 156 static int handle_preop(struct expression *expr)
157 157 {
158 158 struct statement *stmt;
159 159
160 160 if (expr->op == '!') {
161 161 split_conditions(expr->unop);
162 162 __negate_cond_stacks();
163 163 return 1;
164 164 }
165 165 stmt = get_expression_statement(expr);
166 166 if (stmt) {
167 167 handle_compound_stmt(stmt);
168 168 return 1;
169 169 }
170 170 return 0;
171 171 }
172 172
173 173 static void handle_logical(struct expression *expr)
174 174 {
175 175 /*
176 176 * If we come to an "and" expr then:
177 177 * We split the left side.
178 178 * We keep all the current states.
179 179 * We split the right side.
180 180 * We keep all the states from both true sides.
181 181 *
182 182 * If it's an "or" expr then:
183 183 * We save the current slist.
184 184 * We split the left side.
185 185 * We use the false states for the right side.
186 186 * We split the right side.
187 187 * We save all the states that are the same on both sides.
188 188 */
189 189
190 190 split_conditions(expr->left);
191 191
192 192 if (is_logical_and(expr))
193 193 __use_cond_true_states();
194 194 else
195 195 __use_cond_false_states();
196 196
197 197 __push_cond_stacks();
198 198
199 199 __save_pre_cond_states();
200 200 split_conditions(expr->right);
201 201 __discard_pre_cond_states();
202 202
203 203 if (is_logical_and(expr))
204 204 __and_cond_states();
205 205 else
206 206 __or_cond_states();
207 207
208 208 __use_cond_true_states();
209 209 }
210 210
211 211 static struct stree *combine_strees(struct stree *orig, struct stree *fake, struct stree *new)
212 212 {
213 213 struct stree *ret = NULL;
214 214
215 215 overwrite_stree(orig, &ret);
216 216 overwrite_stree(fake, &ret);
217 217 overwrite_stree(new, &ret);
218 218 free_stree(&new);
219 219
220 220 return ret;
221 221 }
222 222
223 223 /*
224 224 * handle_select()
225 225 * if ((aaa()?bbb():ccc())) { ...
226 226 *
227 227 * This is almost the same as:
228 228 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
229 229 *
230 230 * It's a bit complicated because we shouldn't pass aaa()
231 231 * to the clients more than once.
232 232 */
233 233
234 234 static void handle_select(struct expression *expr)
235 235 {
236 236 struct stree *a_T = NULL;
237 237 struct stree *a_F = NULL;
238 238 struct stree *a_T_b_T = NULL;
239 239 struct stree *a_T_b_F = NULL;
240 240 struct stree *a_T_b_fake = NULL;
241 241 struct stree *a_F_c_T = NULL;
242 242 struct stree *a_F_c_F = NULL;
243 243 struct stree *a_F_c_fake = NULL;
244 244 struct stree *tmp;
245 245 struct sm_state *sm;
246 246
247 247 /*
248 248 * Imagine we have this: if (a ? b : c) { ...
249 249 *
250 250 * The condition is true if "a" is true and "b" is true or
251 251 * "a" is false and "c" is true. It's false if "a" is true
252 252 * and "b" is false or "a" is false and "c" is false.
253 253 *
254 254 * The variable name "a_T_b_T" stands for "a true b true" etc.
255 255 *
256 256 * But if we know "b" is true then we can simpilify things.
257 257 * The condition is true if "a" is true or if "a" is false and
258 258 * "c" is true. The only way the condition can be false is if
259 259 * "a" is false and "c" is false.
260 260 *
261 261 * The remaining thing is the "a_T_b_fake". When we simplify
262 262 * the equations we have to take into consideration that other
263 263 * states may have changed that don't play into the true false
264 264 * equation. Take the following example:
265 265 * if ({
266 266 * (flags) = __raw_local_irq_save();
267 267 * _spin_trylock(lock) ? 1 :
268 268 * ({ raw_local_irq_restore(flags); 0; });
269 269 * })
270 270 * Smatch has to record that the irq flags were restored on the
271 271 * false path.
272 272 *
273 273 */
274 274
275 275 __save_pre_cond_states();
276 276
277 277 split_conditions(expr->conditional);
278 278
279 279 a_T = __copy_cond_true_states();
280 280 a_F = __copy_cond_false_states();
281 281
282 282 __use_cond_true_states();
283 283
284 284 __push_cond_stacks();
285 285 __push_fake_cur_stree();
286 286 split_conditions(expr->cond_true);
287 287 __process_post_op_stack();
288 288 a_T_b_fake = __pop_fake_cur_stree();
289 289 a_T_b_T = combine_strees(a_T, a_T_b_fake, __pop_cond_true_stack());
290 290 a_T_b_F = combine_strees(a_T, a_T_b_fake, __pop_cond_false_stack());
291 291
292 292 __use_cond_false_states();
293 293
294 294 __push_cond_stacks();
295 295 __push_fake_cur_stree();
296 296 split_conditions(expr->cond_false);
297 297 a_F_c_fake = __pop_fake_cur_stree();
298 298 a_F_c_T = combine_strees(a_F, a_F_c_fake, __pop_cond_true_stack());
299 299 a_F_c_F = combine_strees(a_F, a_F_c_fake, __pop_cond_false_stack());
300 300
301 301 /* We have to restore the pre condition states so that
302 302 implied_condition_true() will use the right cur_stree */
303 303 __use_pre_cond_states();
304 304
305 305 if (implied_condition_true(expr->cond_true)) {
306 306 free_stree(&a_T_b_T);
307 307 free_stree(&a_T_b_F);
308 308 a_T_b_T = clone_stree(a_T);
309 309 overwrite_stree(a_T_b_fake, &a_T_b_T);
310 310 }
311 311 if (implied_condition_false(expr->cond_true)) {
312 312 free_stree(&a_T_b_T);
313 313 free_stree(&a_T_b_F);
314 314 a_T_b_F = clone_stree(a_T);
315 315 overwrite_stree(a_T_b_fake, &a_T_b_F);
316 316 }
317 317 if (implied_condition_true(expr->cond_false)) {
318 318 free_stree(&a_F_c_T);
319 319 free_stree(&a_F_c_F);
320 320 a_F_c_T = clone_stree(a_F);
321 321 overwrite_stree(a_F_c_fake, &a_F_c_T);
322 322 }
323 323 if (implied_condition_false(expr->cond_false)) {
324 324 free_stree(&a_F_c_T);
325 325 free_stree(&a_F_c_F);
326 326 a_F_c_F = clone_stree(a_F);
327 327 overwrite_stree(a_F_c_fake, &a_F_c_F);
328 328 }
329 329
330 330 merge_stree(&a_T_b_T, a_F_c_T);
331 331 merge_stree(&a_T_b_F, a_F_c_F);
332 332
333 333 tmp = __pop_cond_true_stack();
334 334 free_stree(&tmp);
335 335 tmp = __pop_cond_false_stack();
336 336 free_stree(&tmp);
337 337
338 338 __push_cond_stacks();
339 339 FOR_EACH_SM(a_T_b_T, sm) {
340 340 __set_true_false_sm(sm, NULL);
341 341 } END_FOR_EACH_SM(sm);
342 342 FOR_EACH_SM(a_T_b_F, sm) {
343 343 __set_true_false_sm(NULL, sm);
344 344 } END_FOR_EACH_SM(sm);
345 345 __free_set_states();
346 346
347 347 free_stree(&a_T_b_fake);
348 348 free_stree(&a_F_c_fake);
349 349 free_stree(&a_F_c_T);
350 350 free_stree(&a_F_c_F);
351 351 free_stree(&a_T_b_T);
352 352 free_stree(&a_T_b_F);
353 353 free_stree(&a_T);
354 354 free_stree(&a_F);
355 355 }
356 356
357 357 static void handle_comma(struct expression *expr)
358 358 {
359 359 __split_expr(expr->left);
360 360 split_conditions(expr->right);
361 361 }
362 362
363 363 static int make_op_unsigned(int op)
364 364 {
365 365 switch (op) {
366 366 case '<':
367 367 return SPECIAL_UNSIGNED_LT;
368 368 case SPECIAL_LTE:
369 369 return SPECIAL_UNSIGNED_LTE;
370 370 case '>':
371 371 return SPECIAL_UNSIGNED_GT;
372 372 case SPECIAL_GTE:
373 373 return SPECIAL_UNSIGNED_GTE;
374 374 }
375 375 return op;
376 376 }
377 377
378 378 static void hackup_unsigned_compares(struct expression *expr)
379 379 {
380 380 if (expr->type != EXPR_COMPARE)
381 381 return;
382 382
383 383 if (type_unsigned(get_type(expr)))
384 384 expr->op = make_op_unsigned(expr->op);
385 385 }
386 386
387 387 static void do_condition(struct expression *expr)
388 388 {
389 389 __fold_in_set_states();
390 390 __push_fake_cur_stree();
391 391 __pass_to_client(expr, CONDITION_HOOK);
392 392 __fold_in_set_states();
393 393 }
394 394
395 395 static void split_conditions(struct expression *expr)
396 396 {
397 397 if (option_debug) {
398 398 char *cond = expr_to_str(expr);
399 399
400 400 sm_msg("%d in split_conditions (%s)", get_lineno(), cond);
401 401 free_string(cond);
402 402 }
403 403
404 404 expr = strip_expr_set_parent(expr);
405 405 if (!expr) {
406 406 __fold_in_set_states();
407 407 return;
408 408 }
409 409
410 410 /*
411 411 * On fast paths (and also I guess some people think it's cool) people
412 412 * sometimes use | instead of ||. It works the same basically except
413 413 * that || implies a memory barrier between conditions. The easiest way
414 414 * to handle it is by pretending that | also has a barrier and re-using
415 415 * all the normal condition code. This potentially hides some bugs, but
416 416 * people who write code like this should just be careful or they
417 417 * deserve bugs.
418 418 *
419 419 * We could potentially treat boolean bitwise & this way but that seems
420 420 * too complicated to deal with.
421 421 */
422 422 if (expr->type == EXPR_BINOP && expr->op == '|') {
423 423 expr_set_parent_expr(expr->left, expr);
424 424 expr_set_parent_expr(expr->right, expr);
425 425 handle_logical(expr);
426 426 return;
427 427 }
428 428
429 429 switch (expr->type) {
430 430 case EXPR_LOGICAL:
431 431 expr_set_parent_expr(expr->left, expr);
432 432 expr_set_parent_expr(expr->right, expr);
433 433 __pass_to_client(expr, LOGIC_HOOK);
434 434 handle_logical(expr);
435 435 return;
436 436 case EXPR_COMPARE:
437 437 expr_set_parent_expr(expr->left, expr);
438 438 expr_set_parent_expr(expr->right, expr);
439 439 hackup_unsigned_compares(expr);
440 440 if (handle_zero_comparisons(expr))
441 441 return;
442 442 break;
443 443 case EXPR_CALL:
444 444 if (ignore_builtin_expect(expr))
445 445 return;
446 446 break;
447 447 case EXPR_PREOP:
448 448 expr_set_parent_expr(expr->unop, expr);
449 449 if (handle_preop(expr))
450 450 return;
451 451 break;
452 452 case EXPR_CONDITIONAL:
453 453 case EXPR_SELECT:
454 454 expr_set_parent_expr(expr->conditional, expr);
455 455 expr_set_parent_expr(expr->cond_true, expr);
456 456 expr_set_parent_expr(expr->cond_false, expr);
457 457 handle_select(expr);
458 458 return;
459 459 case EXPR_COMMA:
460 460 expr_set_parent_expr(expr->left, expr);
461 461 expr_set_parent_expr(expr->right, expr);
462 462 handle_comma(expr);
463 463 return;
464 464 }
465 465
466 466 /* fixme: this should be in smatch_flow.c
467 467 but because of the funny stuff we do with conditions
468 468 it's awkward to put it there. We would need to
469 469 call CONDITION_HOOK in smatch_flow as well.
470 470 */
471 471 push_expression(&big_expression_stack, expr);
472 472 push_expression(&big_condition_stack, expr);
473 473
474 474 if (expr->type == EXPR_COMPARE) {
475 475 if (expr->left->type != EXPR_POSTOP)
476 476 __split_expr(expr->left);
477 477 if (expr->right->type != EXPR_POSTOP)
478 478 __split_expr(expr->right);
479 479 } else if (expr->type != EXPR_POSTOP) {
480 480 __split_expr(expr);
481 481 }
482 482 do_condition(expr);
483 483 if (expr->type == EXPR_COMPARE) {
484 484 if (expr->left->type == EXPR_POSTOP)
485 485 __split_expr(expr->left);
486 486 if (expr->right->type == EXPR_POSTOP)
487 487 __split_expr(expr->right);
488 488 } else if (expr->type == EXPR_POSTOP) {
489 489 __split_expr(expr);
490 490 }
491 491 __push_fake_cur_stree();
492 492 __process_post_op_stack();
493 493 __fold_in_set_states();
494 494 pop_expression(&big_condition_stack);
495 495 pop_expression(&big_expression_stack);
496 496 }
497 497
498 498 static int inside_condition;
499 499 void __split_whole_condition(struct expression *expr)
500 500 {
501 501 sm_debug("%d in __split_whole_condition\n", get_lineno());
502 502 inside_condition++;
503 503 __save_pre_cond_states();
504 504 __push_cond_stacks();
505 505 /* it's a hack, but it's sometimes handy to have this stuff
506 506 on the big_expression_stack. */
507 507 push_expression(&big_expression_stack, expr);
508 508 split_conditions(expr);
509 509 __use_cond_states();
510 510 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
511 511 pop_expression(&big_expression_stack);
512 512 inside_condition--;
513 513 sm_debug("%d done __split_whole_condition\n", get_lineno());
514 514 }
515 515
516 516 void __handle_logic(struct expression *expr)
517 517 {
518 518 sm_debug("%d in __handle_logic\n", get_lineno());
519 519 inside_condition++;
520 520 __save_pre_cond_states();
521 521 __push_cond_stacks();
522 522 /* it's a hack, but it's sometimes handy to have this stuff
523 523 on the big_expression_stack. */
524 524 push_expression(&big_expression_stack, expr);
525 525 if (expr)
526 526 split_conditions(expr);
527 527 __use_cond_states();
528 528 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
529 529 pop_expression(&big_expression_stack);
530 530 __merge_false_states();
531 531 inside_condition--;
532 532 sm_debug("%d done __handle_logic\n", get_lineno());
533 533 }
534 534
535 535 int is_condition(struct expression *expr)
536 536 {
537 537
538 538 expr = strip_expr(expr);
539 539 if (!expr)
540 540 return 0;
541 541
542 542 switch (expr->type) {
543 543 case EXPR_LOGICAL:
544 544 case EXPR_COMPARE:
545 545 return 1;
546 546 case EXPR_PREOP:
547 547 if (expr->op == '!')
548 548 return 1;
549 549 }
550 550 return 0;
551 551 }
552 552
553 553 int __handle_condition_assigns(struct expression *expr)
554 554 {
555 555 struct expression *right;
556 556 struct stree *true_stree, *false_stree, *fake_stree;
557 557 struct sm_state *sm;
558 558
559 559 if (expr->op != '=')
560 560 return 0;
561 561 right = strip_expr(expr->right);
562 562 if (!is_condition(expr->right))
563 563 return 0;
564 564
565 565 sm_debug("%d in __handle_condition_assigns\n", get_lineno());
566 566 inside_condition++;
567 567 __save_pre_cond_states();
568 568 __push_cond_stacks();
569 569 /* it's a hack, but it's sometimes handy to have this stuff
570 570 on the big_expression_stack. */
571 571 push_expression(&big_expression_stack, right);
572 572 split_conditions(right);
573 573 true_stree = __get_true_states();
574 574 false_stree = __get_false_states();
575 575 __use_cond_states();
576 576 __push_fake_cur_stree();
577 577 set_extra_expr_mod(expr->left, alloc_estate_sval(sval_type_val(get_type(expr->left), 1)));
578 578 __pass_to_client(right, WHOLE_CONDITION_HOOK);
579 579
580 580 fake_stree = __pop_fake_cur_stree();
581 581 FOR_EACH_SM(fake_stree, sm) {
582 582 overwrite_sm_state_stree(&true_stree, sm);
583 583 } END_FOR_EACH_SM(sm);
584 584 free_stree(&fake_stree);
585 585
586 586 pop_expression(&big_expression_stack);
587 587 inside_condition--;
588 588
589 589 __push_true_states();
590 590
591 591 __use_false_states();
592 592 __push_fake_cur_stree();
593 593 set_extra_expr_mod(expr->left, alloc_estate_sval(sval_type_val(get_type(expr->left), 0)));
594 594
595 595 fake_stree = __pop_fake_cur_stree();
596 596 FOR_EACH_SM(fake_stree, sm) {
597 597 overwrite_sm_state_stree(&false_stree, sm);
598 598 } END_FOR_EACH_SM(sm);
599 599 free_stree(&fake_stree);
600 600
601 601 __merge_true_states();
602 602 merge_fake_stree(&true_stree, false_stree);
603 603 free_stree(&false_stree);
604 604 FOR_EACH_SM(true_stree, sm) {
605 605 __set_sm(sm);
606 606 } END_FOR_EACH_SM(sm);
607 607
608 608 __pass_to_client(expr, ASSIGNMENT_HOOK);
609 609 sm_debug("%d done __handle_condition_assigns\n", get_lineno());
610 610 return 1;
611 611 }
612 612
613 613 static int is_select_assign(struct expression *expr)
614 614 {
615 615 struct expression *right;
616 616
617 617 if (expr->op != '=')
618 618 return 0;
619 619 right = strip_expr(expr->right);
620 620 if (right->type == EXPR_CONDITIONAL)
621 621 return 1;
622 622 if (right->type == EXPR_SELECT)
623 623 return 1;
624 624 return 0;
625 625 }
626 626
627 627 int __handle_select_assigns(struct expression *expr)
628 628 {
629 629 struct expression *right;
630 630 struct stree *final_states = NULL;
631 631 struct sm_state *sm;
632 632 int is_true;
633 633 int is_false;
634 634
635 635 if (!is_select_assign(expr))
636 636 return 0;
637 637 sm_debug("%d in __handle_ternary_assigns\n", get_lineno());
638 638 right = strip_expr(expr->right);
639 639 __pass_to_client(right, SELECT_HOOK);
640 640
641 641 is_true = implied_condition_true(right->conditional);
642 642 is_false = implied_condition_false(right->conditional);
643 643
644 644 /* hah hah. the ultra fake out */
645 645 __save_pre_cond_states();
646 646 __split_whole_condition(right->conditional);
647 647
648 648 if (!is_false) {
649 649 struct expression *fake_expr;
650 650
651 651 if (right->cond_true)
652 652 fake_expr = assign_expression(expr->left, expr->op, right->cond_true);
653 653 else
654 654 fake_expr = assign_expression(expr->left, expr->op, right->conditional);
655 655 __split_expr(fake_expr);
656 656 final_states = clone_stree(__get_cur_stree());
657 657 }
658 658
659 659 __use_false_states();
660 660 if (!is_true) {
661 661 struct expression *fake_expr;
662 662
663 663 fake_expr = assign_expression(expr->left, expr->op, right->cond_false);
664 664 __split_expr(fake_expr);
665 665 merge_stree(&final_states, __get_cur_stree());
666 666 }
667 667
668 668 __use_pre_cond_states();
669 669
670 670 FOR_EACH_SM(final_states, sm) {
671 671 __set_sm(sm);
672 672 } END_FOR_EACH_SM(sm);
673 673
674 674 free_stree(&final_states);
675 675
676 676 sm_debug("%d done __handle_ternary_assigns\n", get_lineno());
677 677
678 678 return 1;
679 679 }
680 680
681 681 static struct statement *split_then_return_last(struct statement *stmt)
682 682 {
683 683 struct statement *tmp;
684 684 struct statement *last_stmt;
685 685
686 686 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
687 687 if (!last_stmt)
688 688 return NULL;
689 689
690 690 __push_scope_hooks();
691 691 FOR_EACH_PTR(stmt->stmts, tmp) {
692 692 if (tmp == last_stmt) {
693 693 if (tmp->type == STMT_LABEL) {
694 694 __split_label_stmt(tmp);
695 695 return tmp->label_statement;
696 696 }
697 697 return last_stmt;
698 698 }
699 699 __split_stmt(tmp);
700 700 } END_FOR_EACH_PTR(tmp);
701 701 return NULL;
702 702 }
703 703
704 704 int __handle_expr_statement_assigns(struct expression *expr)
705 705 {
706 706 struct expression *right;
707 707 struct statement *stmt;
708 708
709 709 right = expr->right;
710 710 if (right->type == EXPR_PREOP && right->op == '(')
711 711 right = right->unop;
712 712 if (right->type != EXPR_STATEMENT)
713 713 return 0;
714 714
715 715 __expr_stmt_count++;
716 716 stmt = right->statement;
717 717 if (stmt->type == STMT_COMPOUND) {
718 718 struct statement *last_stmt;
719 719 struct expression *fake_assign;
720 720 struct expression fake_expr_stmt = { .smatch_flags = Fake, };
721 721
722 722 last_stmt = split_then_return_last(stmt);
723 723 if (!last_stmt) {
724 724 __expr_stmt_count--;
725 725 return 0;
726 726 }
727 727
728 728 fake_expr_stmt.pos = last_stmt->pos;
729 729 fake_expr_stmt.type = EXPR_STATEMENT;
730 730 fake_expr_stmt.op = 0;
731 731 fake_expr_stmt.statement = last_stmt;
732 732
733 733 fake_assign = assign_expression(expr->left, expr->op, &fake_expr_stmt);
734 734 __split_expr(fake_assign);
735 735
736 736 __pass_to_client(stmt, STMT_HOOK_AFTER);
737 737 __call_scope_hooks();
738 738 } else if (stmt->type == STMT_EXPRESSION) {
739 739 struct expression *fake_assign;
740 740
741 741 fake_assign = assign_expression(expr->left, expr->op, stmt->expression);
742 742 __split_expr(fake_assign);
743 743
744 744 } else {
745 745 __split_stmt(stmt);
746 746 }
747 747 __expr_stmt_count--;
748 748 return 1;
749 749 }
750 750
751 751 int in_condition(void)
752 752 {
753 753 return inside_condition;
754 754 }
↓ open down ↓ |
658 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX