1 /*
   2  * Copyright (C) 2011 Dan Carpenter.
   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 #include "smatch.h"
  19 #include "smatch_slist.h"
  20 
  21 static int my_id;
  22 
  23 static int right_side_changes(struct expression *expr)
  24 {
  25         sval_t dummy;
  26 
  27         if (get_value(expr->right, &dummy))
  28                 return 0;
  29         return 1;
  30 }
  31 
  32 static struct expression *get_iterator_set(struct statement *stmt)
  33 {
  34         struct expression *expr;
  35 
  36         if (!stmt)
  37                 return NULL;
  38         if (stmt->type != STMT_EXPRESSION)
  39                 return NULL;
  40         expr = stmt->expression;
  41         if (expr->type != EXPR_ASSIGNMENT)
  42                 return NULL;
  43         if (expr->op != '=')
  44                 return NULL;
  45         if (right_side_changes(expr))
  46                 return NULL;
  47         return expr->left;
  48 }
  49 
  50 static struct expression *get_iterator_tested(struct expression *expr)
  51 {
  52         if (!expr)
  53                 return NULL;
  54         if (expr->type != EXPR_COMPARE)
  55                 return NULL;
  56         return expr->left;
  57 }
  58 
  59 static void match_loop(struct statement *stmt)
  60 {
  61         struct expression *iterator;
  62         char *iter_set;
  63         char *iter_tested;
  64 
  65         if (get_macro_name(stmt->pos))
  66                 return;
  67 
  68         iterator = get_iterator_set(stmt->iterator_pre_statement);
  69         iter_set = expr_to_var(iterator);
  70         iterator = get_iterator_tested(stmt->iterator_pre_condition);
  71         iter_tested = expr_to_var(iterator);
  72         if (!iter_set || !iter_tested)
  73                 goto free;
  74         if (strcmp(iter_set, iter_tested))
  75                 goto free;
  76 
  77         /* smatch doesn't handle loops correctly so this silences some
  78          * false positives.
  79          */
  80         if (right_side_changes(stmt->iterator_pre_condition))
  81                 goto free;
  82 
  83         if (implied_condition_false(stmt->iterator_pre_condition))
  84                 sm_warning("we never enter this loop");
  85 
  86 free:
  87         free_string(iter_set);
  88         free_string(iter_tested);
  89 }
  90 
  91 void check_bogus_loop(int id)
  92 {
  93         my_id = id;
  94         add_hook(&match_loop, PRELOOP_HOOK);
  95 }