flow: fix segfault on parse error
[smatch.git] / smatch_extra.c
blobdb2ff3afcde8dfcd5db6b288526d7e974da9a8c7
1 /*
2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * smatch_extra.c is supposed to track the value of every variable.
15 #include <stdlib.h>
16 #include <errno.h>
17 #ifndef __USE_ISOC99
18 #define __USE_ISOC99
19 #endif
20 #include <limits.h>
21 #include "parse.h"
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 static int my_id;
28 struct string_list *__ignored_macros = NULL;
29 static int in_warn_on_macro()
31 struct statement *stmt;
32 char *tmp;
33 char *macro;
35 stmt = last_ptr_list((struct ptr_list *)big_statement_stack);
36 macro = get_macro_name(&stmt->pos);
37 if (!macro)
38 return 0;
40 FOR_EACH_PTR(__ignored_macros, tmp) {
41 if (!strcmp(tmp, macro))
42 return 1;
43 } END_FOR_EACH_PTR(tmp);
44 return 0;
47 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
49 if (in_warn_on_macro())
50 return NULL;
51 remove_from_equiv(name, sym);
52 return set_state(SMATCH_EXTRA, name, sym, state);
55 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
57 if (in_warn_on_macro())
58 return NULL;
59 remove_from_equiv_expr(expr);
60 return set_state_expr(SMATCH_EXTRA, expr, state);
64 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
66 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
68 struct relation *rel;
69 struct smatch_state *orig_state;
71 orig_state = get_state_expr(SMATCH_EXTRA, expr);
73 if (!estate_related(orig_state)) {
74 set_state_expr(SMATCH_EXTRA, expr, state);
75 return;
78 FOR_EACH_PTR(estate_related(orig_state), rel) {
79 if (option_debug_related)
80 sm_msg("updating related %s to %s", rel->name, state->name);
81 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
82 add_equiv(state, rel->name, rel->sym);
83 } END_FOR_EACH_PTR(rel);
86 static void set_extra_true_false(const char *name, struct symbol *sym,
87 struct smatch_state *true_state,
88 struct smatch_state *false_state)
90 struct relation *rel;
91 struct smatch_state *orig_state;
93 if (in_warn_on_macro())
94 return;
96 orig_state = get_state(SMATCH_EXTRA, name, sym);
98 if (!estate_related(orig_state)) {
99 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
100 return;
103 // FIXME!!!! equiv => related
104 FOR_EACH_PTR(estate_related(orig_state), rel) {
105 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
106 true_state, false_state);
107 if (true_state)
108 add_equiv(true_state, rel->name, rel->sym);
109 if (false_state)
110 add_equiv(false_state, rel->name, rel->sym);
111 } END_FOR_EACH_PTR(rel);
114 static void set_extra_expr_true_false(struct expression *expr,
115 struct smatch_state *true_state,
116 struct smatch_state *false_state)
118 char *name;
119 struct symbol *sym;
121 expr = strip_expr(expr);
122 name = get_variable_from_expr(expr, &sym);
123 if (!name || !sym)
124 goto free;
125 set_extra_true_false(name, sym, true_state, false_state);
126 free:
127 free_string(name);
130 struct smatch_state *filter_range(struct smatch_state *orig,
131 long long filter_min, long long filter_max)
133 struct range_list *rl;
135 if (!orig)
136 orig = extra_undefined();
138 rl = remove_range(estate_ranges(orig), filter_min, filter_max);
139 return alloc_estate_range_list(rl);
142 struct smatch_state *add_filter(struct smatch_state *orig, long long num)
144 return filter_range(orig, num, num);
147 static struct smatch_state *merge_func(const char *name, struct symbol *sym,
148 struct smatch_state *s1,
149 struct smatch_state *s2)
151 struct smatch_state *tmp;
152 struct range_list *value_ranges;
153 struct relation *rel;
154 struct relation *new;
156 value_ranges = range_list_union(estate_ranges(s1), estate_ranges(s2));
157 tmp = alloc_estate_range_list(value_ranges);
158 FOR_EACH_PTR(estate_related(s1), rel) {
159 new = get_common_relationship(s2, rel->op, rel->name, rel->sym);
160 if (new)
161 add_related(tmp, new->op, new->name, new->sym);
162 } END_FOR_EACH_PTR(rel);
163 return tmp;
166 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
168 struct expression *iter_var;
169 struct expression *condition;
170 struct sm_state *sm;
171 long long start;
173 condition = strip_expr(loop->iterator_pre_condition);
174 if (!condition)
175 return NULL;
176 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
177 return NULL;
178 if (condition->op != SPECIAL_DECREMENT)
179 return NULL;
181 iter_var = condition->unop;
182 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
183 if (!sm)
184 return NULL;
185 if (estate_min(sm->state) < 0)
186 return NULL;
187 start = estate_max(sm->state);
188 if (start <= 0)
189 return NULL;
190 if (start != whole_range.max)
191 start--;
193 if (condition->type == EXPR_PREOP)
194 set_extra_expr_mod(iter_var, alloc_estate_range(1, start));
195 if (condition->type == EXPR_POSTOP)
196 set_extra_expr_mod(iter_var, alloc_estate_range(0, start));
197 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
200 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
201 struct expression *condition)
203 struct expression *iter_var;
204 struct sm_state *sm;
205 long long start;
206 long long end;
208 iter_var = iter_expr->unop;
209 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
210 if (!sm)
211 return NULL;
212 if (!estate_get_single_value(sm->state, &start))
213 return NULL;
214 if (!get_implied_value(condition->right, &end))
215 end = whole_range.max;
216 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
217 return NULL;
219 switch (condition->op) {
220 case SPECIAL_NOTEQUAL:
221 case '<':
222 if (end != whole_range.max)
223 end--;
224 break;
225 case SPECIAL_LTE:
226 break;
227 default:
228 return NULL;
230 if (end < start)
231 return NULL;
232 set_extra_expr_mod(iter_var, alloc_estate_range(start, end));
233 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
236 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
237 struct expression *condition)
239 struct expression *iter_var;
240 struct sm_state *sm;
241 long long start;
242 long long end;
244 iter_var = iter_expr->unop;
245 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
246 if (!sm)
247 return NULL;
248 if (!estate_get_single_value(sm->state, &start))
249 return NULL;
250 if (!get_implied_value(condition->right, &end))
251 end = whole_range.max;
252 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
253 return NULL;
255 switch (condition->op) {
256 case SPECIAL_NOTEQUAL:
257 case '>':
258 if (end != whole_range.min)
259 end++;
260 break;
261 case SPECIAL_GTE:
262 break;
263 default:
264 return NULL;
266 if (end > start)
267 return NULL;
268 set_extra_expr_mod(iter_var, alloc_estate_range(end, start));
269 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
272 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
274 struct expression *iter_expr;
275 struct expression *condition;
277 if (!loop->iterator_post_statement)
278 return NULL;
279 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
280 return NULL;
281 iter_expr = loop->iterator_post_statement->expression;
282 if (!loop->iterator_pre_condition)
283 return NULL;
284 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
285 return NULL;
286 condition = loop->iterator_pre_condition;
288 if (iter_expr->op == SPECIAL_INCREMENT)
289 return handle_canonical_for_inc(iter_expr, condition);
290 if (iter_expr->op == SPECIAL_DECREMENT)
291 return handle_canonical_for_dec(iter_expr, condition);
292 return NULL;
295 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct state_list **slist)
297 struct sm_state *ret;
299 __push_fake_cur_slist();
300 if (!loop->iterator_post_statement)
301 ret = handle_canonical_while_count_down(loop);
302 else
303 ret = handle_canonical_for_loops(loop);
304 *slist = __pop_fake_cur_slist();
305 return ret;
308 int __iterator_unchanged(struct sm_state *sm)
310 if (!sm)
311 return 0;
312 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
313 return 1;
314 return 0;
317 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
319 long long after_value;
321 /* paranoid checking. prolly not needed */
322 condition = strip_expr(condition);
323 if (!condition)
324 return;
325 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
326 return;
327 if (condition->op != SPECIAL_DECREMENT)
328 return;
329 after_value = estate_min(sm->state);
330 after_value--;
331 set_extra_mod(sm->name, sm->sym, alloc_estate(after_value));
334 void __extra_pre_loop_hook_after(struct sm_state *sm,
335 struct statement *iterator,
336 struct expression *condition)
338 struct expression *iter_expr;
339 char *name;
340 struct symbol *sym;
341 long long value;
342 int left = 0;
343 struct smatch_state *state;
344 long long min, max;
346 if (!iterator) {
347 while_count_down_after(sm, condition);
348 return;
351 iter_expr = iterator->expression;
353 if (condition->type != EXPR_COMPARE)
354 return;
355 if (!get_value(condition->left, &value)) {
356 if (!get_value(condition->right, &value))
357 return;
358 left = 1;
360 if (left)
361 name = get_variable_from_expr(condition->left, &sym);
362 else
363 name = get_variable_from_expr(condition->right, &sym);
364 if (!name || !sym)
365 goto free;
366 if (sym != sm->sym || strcmp(name, sm->name))
367 goto free;
368 state = get_state(my_id, name, sym);
369 min = estate_min(state);
370 max = estate_max(state);
371 if (iter_expr->op == SPECIAL_INCREMENT &&
372 min != whole_range.min &&
373 max == whole_range.max) {
374 set_extra_mod(name, sym, alloc_estate(min));
375 } else if (min == whole_range.min && max != whole_range.max) {
376 set_extra_mod(name, sym, alloc_estate(max));
378 free:
379 free_string(name);
380 return;
383 static struct smatch_state *unmatched_state(struct sm_state *sm)
385 return extra_undefined();
388 static void match_function_call(struct expression *expr)
390 struct expression *tmp;
392 FOR_EACH_PTR(expr->args, tmp) {
393 if (tmp->type == EXPR_PREOP && tmp->op == '&') {
394 remove_from_equiv_expr(tmp->unop);
395 set_state_expr(SMATCH_EXTRA, tmp->unop, extra_undefined());
397 } END_FOR_EACH_PTR(tmp);
400 static void match_assign(struct expression *expr)
402 struct expression *left;
403 struct expression *right;
404 struct symbol *right_sym;
405 char *right_name;
406 struct symbol *sym;
407 char *name;
408 long long value;
409 int known;
410 long long min = whole_range.min;
411 long long max = whole_range.max;
412 long long tmp;
414 if (__is_condition_assign(expr))
415 return;
416 left = strip_expr(expr->left);
417 name = get_variable_from_expr(left, &sym);
418 if (!name)
419 return;
420 right = strip_parens(expr->right);
421 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
422 right = strip_parens(right->left);
424 if (expr->op == '=' && right->type == EXPR_CALL)
425 return; /* these are handled in match_call_assign() */
427 right_name = get_variable_from_expr(right, &right_sym);
428 if (expr->op == '=' && right_name && right_sym) {
429 set_equiv(left, right);
430 goto free;
433 known = get_implied_value(right, &value);
434 switch (expr->op) {
435 case '=': {
436 struct range_list *rl = NULL;
438 if (get_implied_range_list(right, &rl)) {
439 set_extra_mod(name, sym, alloc_estate_range_list(rl));
440 goto free;
443 if (expr_unsigned(right))
444 min = 0;
445 break;
447 case SPECIAL_ADD_ASSIGN:
448 if (get_implied_min(left, &tmp)) {
449 if (known)
450 min = tmp + value;
451 else
452 min = tmp;
454 if (!inside_loop() && known && get_implied_max(left, &tmp))
455 max = tmp + value;
456 break;
457 case SPECIAL_SUB_ASSIGN:
458 if (get_implied_max(left, &tmp)) {
459 if (known)
460 max = tmp - value;
461 else
462 max = tmp;
464 if (!inside_loop() && known && get_implied_min(left, &tmp))
465 min = tmp - value;
466 break;
468 set_extra_mod(name, sym, alloc_estate_range(min, max));
469 free:
470 free_string(right_name);
471 free_string(name);
474 static void reset_struct_members(const char *name, struct symbol *sym, struct expression *expr, void *unused)
476 struct state_list *slist;
477 struct sm_state *tmp;
478 int len;
480 len = strlen(name);
481 slist = get_all_states(SMATCH_EXTRA);
482 FOR_EACH_PTR(slist, tmp) {
483 if (sym != tmp->sym)
484 continue;
485 if (strncmp(name, tmp->name, len))
486 continue;
487 if (tmp->name[len] != '-' && tmp->name[len] != '.')
488 continue;
489 set_extra_mod(tmp->name, tmp->sym, extra_undefined());
490 } END_FOR_EACH_PTR(tmp);
493 static struct smatch_state *increment_state(struct smatch_state *state)
495 long long min = estate_min(state);
496 long long max = estate_max(state);
498 if (inside_loop())
499 max = whole_range.max;
501 if (min != whole_range.min)
502 min++;
503 if (max != whole_range.max)
504 max++;
505 return alloc_estate_range(min, max);
508 static struct smatch_state *decrement_state(struct smatch_state *state)
510 long long min = estate_min(state);
511 long long max = estate_max(state);
513 if (inside_loop())
514 min = whole_range.min;
516 if (min != whole_range.min)
517 min--;
518 if (max != whole_range.max)
519 max--;
520 return alloc_estate_range(min, max);
523 static void unop_expr(struct expression *expr)
525 struct smatch_state *state;
527 if (expr->smatch_flags & Handled)
528 return;
530 switch (expr->op) {
531 case SPECIAL_INCREMENT:
532 state = get_state_expr(SMATCH_EXTRA, expr->unop);
533 state = increment_state(state);
534 set_extra_expr_mod(expr->unop, state);
535 break;
536 case SPECIAL_DECREMENT:
537 state = get_state_expr(SMATCH_EXTRA, expr->unop);
538 state = decrement_state(state);
539 set_extra_expr_mod(expr->unop, state);
540 break;
541 default:
542 return;
546 static void delete_state_tracker(struct tracker *t)
548 remove_from_equiv(t->name, t->sym);
549 delete_state(t->owner, t->name, t->sym);
552 static void scoped_state_extra(const char *name, struct symbol *sym)
554 struct tracker *t;
556 t = alloc_tracker(SMATCH_EXTRA, name, sym);
557 add_scope_hook((scope_hook *)&delete_state_tracker, t);
560 static void match_declarations(struct symbol *sym)
562 const char *name;
564 if (sym->ident) {
565 name = sym->ident->name;
566 if (!sym->initializer) {
567 set_state(SMATCH_EXTRA, name, sym, extra_undefined());
568 scoped_state_extra(name, sym);
573 static void match_function_def(struct symbol *sym)
575 struct symbol *arg;
577 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
578 if (!arg->ident)
579 continue;
580 set_state(my_id, arg->ident->name, arg, extra_undefined());
581 } END_FOR_EACH_PTR(arg);
584 static int last_stmt_val(struct statement *stmt, long long *val)
586 struct expression *expr;
588 if (!stmt)
589 return 0;
591 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
592 if (stmt->type != STMT_EXPRESSION)
593 return 0;
594 expr = stmt->expression;
595 return get_value(expr, val);
598 static int match_func_comparison(struct expression *expr)
600 struct expression *left = strip_expr(expr->left);
601 struct expression *right = strip_expr(expr->right);
602 long long val;
604 if (left->type == EXPR_CALL) {
605 if (!get_implied_value(right, &val))
606 return 1;
607 function_comparison(expr->op, left, val, 1);
608 return 1;
611 if (right->type == EXPR_CALL) {
612 if (!get_implied_value(left, &val))
613 return 1;
614 function_comparison(expr->op, right, val, 0);
615 return 1;
618 return 0;
621 static void match_comparison(struct expression *expr)
623 struct expression *left = strip_expr(expr->left);
624 struct expression *right = strip_expr(expr->right);
625 struct range_list *left_orig;
626 struct range_list *left_true;
627 struct range_list *left_false;
628 struct range_list *right_orig;
629 struct range_list *right_true;
630 struct range_list *right_false;
631 struct smatch_state *left_true_state;
632 struct smatch_state *left_false_state;
633 struct smatch_state *right_true_state;
634 struct smatch_state *right_false_state;
635 int left_postop = 0;
636 int right_postop = 0;
638 if (match_func_comparison(expr))
639 return;
641 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
642 if (left->type == EXPR_POSTOP) {
643 left->smatch_flags |= Handled;
644 left_postop = left->op;
646 left = strip_expr(left->unop);
649 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
650 if (right->type == EXPR_POSTOP) {
651 right->smatch_flags |= Handled;
652 right_postop = right->op;
654 right = strip_expr(right->unop);
657 if (!get_implied_range_list(left, &left_orig))
658 left_orig = alloc_range_list(whole_range.min, whole_range.max);
659 if (!get_implied_range_list(right, &right_orig))
660 right_orig = alloc_range_list(whole_range.min, whole_range.max);
662 left_true = clone_range_list(left_orig);
663 left_false = clone_range_list(left_orig);
664 right_true = clone_range_list(right_orig);
665 right_false = clone_range_list(right_orig);
667 switch (expr->op) {
668 case '<':
669 case SPECIAL_UNSIGNED_LT:
670 if (rl_max(right_orig) != whole_range.max)
671 left_true = remove_range(left_orig, rl_max(right_orig), whole_range.max);
672 if (rl_min(right_orig) != whole_range.min)
673 left_false = remove_range(left_orig, whole_range.min, rl_min(right_orig) - 1);
675 if (rl_min(left_orig) != whole_range.min)
676 right_true = remove_range(right_orig, whole_range.min, rl_min(left_orig));
677 if (rl_max(left_orig) != whole_range.max)
678 right_false = remove_range(right_orig, rl_max(left_orig) + 1, whole_range.max);
679 break;
680 case SPECIAL_UNSIGNED_LTE:
681 case SPECIAL_LTE:
682 if (rl_max(right_orig) != whole_range.max)
683 left_true = remove_range(left_orig, rl_max(right_orig) + 1, whole_range.max);
684 if (rl_min(right_orig) != whole_range.min)
685 left_false = remove_range(left_orig, whole_range.min, rl_min(right_orig));
687 if (rl_min(left_orig) != whole_range.min)
688 right_true = remove_range(right_orig, whole_range.min, rl_min(left_orig) - 1);
689 if (rl_max(left_orig) != whole_range.max)
690 right_false = remove_range(right_orig, rl_max(left_orig), whole_range.max);
691 break;
692 case SPECIAL_EQUAL:
693 if (rl_max(right_orig) != whole_range.max)
694 left_true = remove_range(left_true, rl_max(right_orig) + 1, whole_range.max);
695 if (rl_min(right_orig) != whole_range.min)
696 left_true = remove_range(left_true, whole_range.min, rl_min(right_orig) - 1);
697 if (rl_min(right_orig) == rl_max(right_orig))
698 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
700 if (rl_max(left_orig) != whole_range.max)
701 right_true = remove_range(right_true, rl_max(left_orig) + 1, whole_range.max);
702 if (rl_min(left_orig) != whole_range.min)
703 right_true = remove_range(right_true, whole_range.min, rl_min(left_orig) - 1);
704 if (rl_min(left_orig) == rl_max(left_orig))
705 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
706 break;
707 case SPECIAL_UNSIGNED_GTE:
708 case SPECIAL_GTE:
709 if (rl_min(right_orig) != whole_range.min)
710 left_true = remove_range(left_orig, whole_range.min, rl_min(right_orig) - 1);
711 if (rl_max(right_orig) != whole_range.max)
712 left_false = remove_range(left_orig, rl_max(right_orig), whole_range.max);
714 if (rl_max(left_orig) != whole_range.max)
715 right_true = remove_range(right_orig, rl_max(left_orig) + 1, whole_range.max);
716 if (rl_min(left_orig) != whole_range.min)
717 right_false = remove_range(right_orig, whole_range.min, rl_min(left_orig));
718 break;
719 case '>':
720 case SPECIAL_UNSIGNED_GT:
721 if (rl_min(right_orig) != whole_range.min)
722 left_true = remove_range(left_orig, whole_range.min, rl_min(right_orig));
723 if (rl_max(right_orig) != whole_range.max)
724 left_false = remove_range(left_orig, rl_max(right_orig) + 1, whole_range.max);
726 if (rl_max(left_orig) != whole_range.max)
727 right_true = remove_range(right_orig, rl_max(left_orig), whole_range.max);
728 if (rl_min(left_orig) != whole_range.min)
729 right_false = remove_range(right_orig, whole_range.min, rl_min(left_orig) - 1);
730 break;
731 case SPECIAL_NOTEQUAL:
732 if (rl_max(right_orig) != whole_range.max)
733 left_false = remove_range(left_false, rl_max(right_orig) + 1, whole_range.max);
734 if (rl_min(right_orig) != whole_range.min)
735 left_false = remove_range(left_false, whole_range.min, rl_min(right_orig) - 1);
736 if (rl_min(right_orig) == rl_max(right_orig))
737 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
739 if (rl_max(left_orig) != whole_range.max)
740 right_false = remove_range(right_false, rl_max(left_orig) + 1, whole_range.max);
741 if (rl_min(left_orig) != whole_range.min)
742 right_false = remove_range(right_false, whole_range.min, rl_min(left_orig) - 1);
743 if (rl_min(left_orig) == rl_max(left_orig))
744 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
745 break;
746 default:
747 return;
750 left_true_state = alloc_estate_range_list(left_true);
751 left_false_state = alloc_estate_range_list(left_false);
752 right_true_state = alloc_estate_range_list(right_true);
753 right_false_state = alloc_estate_range_list(right_false);
755 if (left_postop == SPECIAL_INCREMENT) {
756 left_true_state = increment_state(left_true_state);
757 left_false_state = increment_state(left_false_state);
759 if (left_postop == SPECIAL_DECREMENT) {
760 left_true_state = decrement_state(left_true_state);
761 left_false_state = decrement_state(left_false_state);
763 if (right_postop == SPECIAL_INCREMENT) {
764 right_true_state = increment_state(right_true_state);
765 right_false_state = increment_state(right_false_state);
767 if (right_postop == SPECIAL_DECREMENT) {
768 right_true_state = decrement_state(right_true_state);
769 right_false_state = decrement_state(right_false_state);
772 set_extra_expr_true_false(left, left_true_state, left_false_state);
773 set_extra_expr_true_false(right, right_true_state, right_false_state);
776 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
777 void __extra_match_condition(struct expression *expr)
779 struct symbol *sym;
780 char *name;
781 struct smatch_state *pre_state;
782 struct smatch_state *true_state;
783 struct smatch_state *false_state;
785 expr = strip_expr(expr);
786 switch (expr->type) {
787 case EXPR_CALL:
788 function_comparison(SPECIAL_NOTEQUAL, expr, 0, 1);
789 return;
790 case EXPR_PREOP:
791 case EXPR_SYMBOL:
792 case EXPR_DEREF:
793 name = get_variable_from_expr(expr, &sym);
794 if (!name)
795 return;
796 pre_state = get_state(my_id, name, sym);
797 true_state = add_filter(pre_state, 0);
798 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
799 false_state = alloc_estate(0);
800 else
801 false_state = alloc_estate_empty();
802 set_extra_true_false(name, sym, true_state, false_state);
803 free_string(name);
804 return;
805 case EXPR_COMPARE:
806 match_comparison(expr);
807 return;
808 case EXPR_ASSIGNMENT:
809 __extra_match_condition(expr->left);
810 return;
814 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
815 int implied_not_equal(struct expression *expr, long long val)
817 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
820 int known_condition_true(struct expression *expr)
822 long long tmp;
823 struct statement *stmt;
825 if (!expr)
826 return 0;
828 if (get_value(expr, &tmp) && tmp)
829 return 1;
831 expr = strip_expr(expr);
832 switch (expr->type) {
833 case EXPR_PREOP:
834 if (expr->op == '!') {
835 if (known_condition_false(expr->unop))
836 return 1;
837 break;
839 stmt = get_expression_statement(expr);
840 if (last_stmt_val(stmt, &tmp) && tmp == 1)
841 return 1;
842 break;
843 default:
844 break;
846 return 0;
849 int known_condition_false(struct expression *expr)
851 long long tmp;
852 struct statement *stmt;
854 if (!expr)
855 return 0;
857 if (is_zero(expr))
858 return 1;
860 switch (expr->type) {
861 case EXPR_PREOP:
862 if (expr->op == '!') {
863 if (known_condition_true(expr->unop))
864 return 1;
865 break;
867 stmt = get_expression_statement(expr);
868 if (last_stmt_val(stmt, &tmp) && tmp == 0)
869 return 1;
870 break;
871 case EXPR_CALL:
872 if (sym_name_is("__builtin_constant_p", expr->fn))
873 return 1;
874 break;
875 default:
876 break;
878 return 0;
881 static int do_comparison(struct expression *expr)
883 struct range_list *left_ranges = NULL;
884 struct range_list *right_ranges = NULL;
885 int poss_true, poss_false;
887 get_implied_range_list(expr->left, &left_ranges);
888 get_implied_range_list(expr->right, &right_ranges);
890 poss_true = possibly_true_range_lists(left_ranges, expr->op, right_ranges);
891 poss_false = possibly_false_range_lists(left_ranges, expr->op, right_ranges);
893 free_range_list(&left_ranges);
894 free_range_list(&right_ranges);
896 if (!poss_true && !poss_false)
897 return 0;
898 if (poss_true && !poss_false)
899 return 1;
900 if (!poss_true && poss_false)
901 return 2;
902 return 3;
905 int implied_condition_true(struct expression *expr)
907 struct statement *stmt;
908 long long tmp;
909 long long val;
911 if (!expr)
912 return 0;
914 if (get_implied_value(expr, &tmp) && tmp)
915 return 1;
917 if (expr->type == EXPR_POSTOP)
918 return implied_condition_true(expr->unop);
920 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_DECREMENT)
921 return implied_not_equal(expr->unop, 1);
922 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_INCREMENT)
923 return implied_not_equal(expr->unop, -1);
925 expr = strip_expr(expr);
926 switch (expr->type) {
927 case EXPR_COMPARE:
928 if (do_comparison(expr) == 1)
929 return 1;
930 break;
931 case EXPR_PREOP:
932 if (expr->op == '!') {
933 if (implied_condition_false(expr->unop))
934 return 1;
935 break;
937 stmt = get_expression_statement(expr);
938 if (last_stmt_val(stmt, &val) && val == 1)
939 return 1;
940 break;
941 default:
942 if (implied_not_equal(expr, 0) == 1)
943 return 1;
944 break;
946 return 0;
949 int implied_condition_false(struct expression *expr)
951 struct statement *stmt;
952 struct expression *tmp;
953 long long val;
955 if (!expr)
956 return 0;
958 if (is_zero(expr))
959 return 1;
961 switch (expr->type) {
962 case EXPR_COMPARE:
963 if (do_comparison(expr) == 2)
964 return 1;
965 case EXPR_PREOP:
966 if (expr->op == '!') {
967 if (implied_condition_true(expr->unop))
968 return 1;
969 break;
971 stmt = get_expression_statement(expr);
972 if (last_stmt_val(stmt, &val) && val == 0)
973 return 1;
974 tmp = strip_expr(expr);
975 if (tmp != expr)
976 return implied_condition_false(tmp);
977 break;
978 default:
979 if (get_implied_value(expr, &val) && val == 0)
980 return 1;
981 break;
983 return 0;
986 int get_implied_range_list(struct expression *expr, struct range_list **rl)
988 long long val;
989 struct smatch_state *state;
990 long long min;
991 long long max;
993 expr = strip_parens(expr);
994 if (!expr)
995 return 0;
997 state = get_state_expr(my_id, expr);
998 if (state) {
999 *rl = clone_range_list(estate_ranges(state));
1000 return 1;
1003 if (get_implied_value(expr, &val)) {
1004 *rl = NULL;
1005 add_range(rl, val, val);
1006 return 1;
1009 if (expr->type != EXPR_BINOP)
1010 return 0;
1012 if (expr->type == EXPR_CALL) {
1013 *rl = db_return_vals(expr);
1014 return !!*rl;
1017 if (expr->op == '%') {
1018 if (!get_implied_value(expr->right, &val))
1019 return 0;
1020 *rl = NULL;
1021 add_range(rl, 0, val - 1);
1022 return 1;
1025 if (!get_implied_min(expr, &min))
1026 return 0;
1027 if (!get_implied_max(expr, &max))
1028 return 0;
1030 *rl = alloc_range_list(min, max);
1031 return 1;
1034 int is_whole_range(struct smatch_state *state)
1036 return is_whole_range_rl(estate_ranges(state));
1039 static void struct_member_callback(char *fn, int param, char *printed_name, struct smatch_state *state)
1041 if (is_whole_range(state))
1042 return;
1043 sm_msg("info: passes param_value '%s' %d '%s' %s", fn, param, printed_name, state->name);
1046 static void match_call_info(struct expression *expr)
1048 struct range_list *rl = NULL;
1049 struct expression *arg;
1050 char *name;
1051 int i = 0;
1053 name = get_fnptr_name(expr->fn);
1054 if (!name)
1055 return;
1057 FOR_EACH_PTR(expr->args, arg) {
1058 if (get_implied_range_list(arg, &rl) && !is_whole_range_rl(rl)) {
1059 sm_msg("info: passes param_value '%s' %d '$$' %s",
1060 name, i, show_ranges(rl));
1062 i++;
1063 } END_FOR_EACH_PTR(arg);
1065 free_string(name);
1068 void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1070 struct range_list *rl = NULL;
1071 struct smatch_state *state;
1072 char fullname[256];
1074 if (strncmp(key, "$$", 2))
1075 return;
1077 snprintf(fullname, 256, "%s%s", name, key + 2);
1078 get_value_ranges(value, &rl);
1079 char *tmp = show_ranges(rl);
1080 if (strcmp(tmp, value))
1081 sm_msg("value = %s, ranges = %s", value, tmp);
1082 state = alloc_estate_range_list(rl);
1083 set_state(SMATCH_EXTRA, fullname, sym, state);
1086 static void match_call_assign(struct expression *expr)
1088 struct expression *right;
1089 struct range_list *rl;
1091 if (expr->op != '=')
1092 return;
1094 right = strip_parens(expr->right);
1095 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1096 right = strip_parens(right->left);
1098 rl = db_return_vals(right);
1099 if (rl)
1100 set_extra_expr_mod(expr->left, alloc_estate_range_list(rl));
1101 else
1102 set_extra_expr_mod(expr->left, extra_undefined());
1105 void register_smatch_extra(int id)
1107 my_id = id;
1108 add_merge_hook(my_id, &merge_func);
1109 add_unmatched_state_hook(my_id, &unmatched_state);
1110 add_hook(&unop_expr, OP_HOOK);
1111 add_hook(&match_function_def, FUNC_DEF_HOOK);
1112 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1113 add_hook(&match_assign, ASSIGNMENT_HOOK);
1114 add_hook(&match_call_assign, CALL_ASSIGNMENT_HOOK);
1115 add_hook(&match_declarations, DECLARATION_HOOK);
1116 if (option_info) {
1117 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1118 add_member_info_callback(my_id, struct_member_callback);
1120 add_definition_db_callback(set_param_value, PARAM_VALUE);
1123 void register_smatch_extra_late(int id)
1125 set_default_modification_hook(SMATCH_EXTRA, reset_struct_members);