1 /*
   2  * Copyright (C) 2012 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  * Store the states at the start of the function because this is something that
  20  * is used in a couple places.
  21  *
  22  */
  23 
  24 #include "smatch.h"
  25 #include "smatch_slist.h"
  26 
  27 static int my_id;
  28 
  29 static struct stree *start_states;
  30 static struct stree_stack *saved_stack;
  31 static void save_start_states(struct statement *stmt)
  32 {
  33         start_states = clone_stree(__get_cur_stree());
  34 }
  35 
  36 static void match_save_states(struct expression *expr)
  37 {
  38         push_stree(&saved_stack, start_states);
  39         start_states = NULL;
  40 }
  41 
  42 static void match_restore_states(struct expression *expr)
  43 {
  44         free_stree(&start_states);
  45         start_states = pop_stree(&saved_stack);
  46 }
  47 
  48 static void match_end_func(void)
  49 {
  50         free_stree(&start_states);
  51 }
  52 
  53 struct stree *get_start_states(void)
  54 {
  55         return start_states;
  56 }
  57 
  58 void register_start_states(int id)
  59 {
  60         my_id = id;
  61 
  62         add_hook(&save_start_states, AFTER_DEF_HOOK);
  63         add_hook(&match_save_states, INLINE_FN_START);
  64         add_hook(&match_restore_states, INLINE_FN_END);
  65         add_hook(&match_end_func, AFTER_FUNC_HOOK);
  66 }
  67