1 /*
   2  * Copyright (C) 2016 Oracle.
   3  *
   4  * This program is free software; you can redistribute it and/or
   5  * modify it under the terms of the GNU General Public License
   6  * as published by the Free Software Foundation; either version 2
   7  * of the License, or (at your option) any later version.
   8  *
   9  * This program is distributed in the hope that it will be useful,
  10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  * GNU General Public License for more details.
  13  *
  14  * You should have received a copy of the GNU General Public License
  15  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
  16  */
  17 
  18 /*
  19  * Say you have a condition like:
  20  *
  21  *     if (foo < 0)
  22  *         return foo;
  23  *
  24  * But we actually know that foo is zero.  Then in smatch_extra.c we set "foo"
  25  * to the empty state and then for the return statement we say that "foo" is
  26  * s32min-s32max because we can't return the empty state.
  27  *
  28  * This file is supposed to provide an alternative to say that actually "foo" is
  29  * less than zero.
  30  *
  31  */
  32 
  33 #include "smatch.h"
  34 #include "smatch_slist.h"
  35 #include "smatch_extra.h"
  36 
  37 static int my_id;
  38 
  39 static struct smatch_state *empty_state(struct sm_state *sm)
  40 {
  41         return alloc_estate_empty();
  42 }
  43 
  44 struct smatch_state *merge_is_empty(struct smatch_state *s1, struct smatch_state *s2)
  45 {
  46         return alloc_estate_empty();
  47 }
  48 
  49 static void reset(struct sm_state *sm, struct expression *mod_expr)
  50 {
  51         set_state(my_id, sm->name, sm->sym, alloc_estate_empty());
  52 }
  53 
  54 void __save_imaginary_state(struct expression *expr, struct range_list *true_rl, struct range_list *false_rl)
  55 {
  56         set_true_false_states_expr(my_id, expr, alloc_estate_rl(true_rl), alloc_estate_rl(false_rl));
  57 }
  58 
  59 int get_imaginary_absolute(struct expression *expr, struct range_list **rl)
  60 {
  61         struct smatch_state *state;
  62 
  63         *rl = NULL;
  64 
  65         state = get_state_expr(my_id, expr);
  66         if (!state || !estate_rl(state))
  67                 return 0;
  68 
  69         *rl = estate_rl(state);
  70         return 1;
  71 }
  72 
  73 void register_imaginary_absolute(int id)
  74 {
  75         my_id = id;
  76 
  77         add_unmatched_state_hook(my_id, &empty_state);
  78         add_merge_hook(my_id, &merge_is_empty);
  79         add_modification_hook(my_id, &reset);
  80 }
  81