1*1f5207b7SJohn Levon /* 2*1f5207b7SJohn Levon * Copyright (C) 2011 Dan Carpenter. 3*1f5207b7SJohn Levon * 4*1f5207b7SJohn Levon * This program is free software; you can redistribute it and/or 5*1f5207b7SJohn Levon * modify it under the terms of the GNU General Public License 6*1f5207b7SJohn Levon * as published by the Free Software Foundation; either version 2 7*1f5207b7SJohn Levon * of the License, or (at your option) any later version. 8*1f5207b7SJohn Levon * 9*1f5207b7SJohn Levon * This program is distributed in the hope that it will be useful, 10*1f5207b7SJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*1f5207b7SJohn Levon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*1f5207b7SJohn Levon * GNU General Public License for more details. 13*1f5207b7SJohn Levon * 14*1f5207b7SJohn Levon * You should have received a copy of the GNU General Public License 15*1f5207b7SJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt 16*1f5207b7SJohn Levon */ 17*1f5207b7SJohn Levon 18*1f5207b7SJohn Levon #include "smatch.h" 19*1f5207b7SJohn Levon #include "smatch_slist.h" 20*1f5207b7SJohn Levon 21*1f5207b7SJohn Levon static int my_id; 22*1f5207b7SJohn Levon right_side_changes(struct expression * expr)23*1f5207b7SJohn Levonstatic int right_side_changes(struct expression *expr) 24*1f5207b7SJohn Levon { 25*1f5207b7SJohn Levon sval_t dummy; 26*1f5207b7SJohn Levon 27*1f5207b7SJohn Levon if (get_value(expr->right, &dummy)) 28*1f5207b7SJohn Levon return 0; 29*1f5207b7SJohn Levon return 1; 30*1f5207b7SJohn Levon } 31*1f5207b7SJohn Levon get_iterator_set(struct statement * stmt)32*1f5207b7SJohn Levonstatic struct expression *get_iterator_set(struct statement *stmt) 33*1f5207b7SJohn Levon { 34*1f5207b7SJohn Levon struct expression *expr; 35*1f5207b7SJohn Levon 36*1f5207b7SJohn Levon if (!stmt) 37*1f5207b7SJohn Levon return NULL; 38*1f5207b7SJohn Levon if (stmt->type != STMT_EXPRESSION) 39*1f5207b7SJohn Levon return NULL; 40*1f5207b7SJohn Levon expr = stmt->expression; 41*1f5207b7SJohn Levon if (expr->type != EXPR_ASSIGNMENT) 42*1f5207b7SJohn Levon return NULL; 43*1f5207b7SJohn Levon if (expr->op != '=') 44*1f5207b7SJohn Levon return NULL; 45*1f5207b7SJohn Levon if (right_side_changes(expr)) 46*1f5207b7SJohn Levon return NULL; 47*1f5207b7SJohn Levon return expr->left; 48*1f5207b7SJohn Levon } 49*1f5207b7SJohn Levon get_iterator_tested(struct expression * expr)50*1f5207b7SJohn Levonstatic struct expression *get_iterator_tested(struct expression *expr) 51*1f5207b7SJohn Levon { 52*1f5207b7SJohn Levon if (!expr) 53*1f5207b7SJohn Levon return NULL; 54*1f5207b7SJohn Levon if (expr->type != EXPR_COMPARE) 55*1f5207b7SJohn Levon return NULL; 56*1f5207b7SJohn Levon return expr->left; 57*1f5207b7SJohn Levon } 58*1f5207b7SJohn Levon match_loop(struct statement * stmt)59*1f5207b7SJohn Levonstatic void match_loop(struct statement *stmt) 60*1f5207b7SJohn Levon { 61*1f5207b7SJohn Levon struct expression *iterator; 62*1f5207b7SJohn Levon char *iter_set; 63*1f5207b7SJohn Levon char *iter_tested; 64*1f5207b7SJohn Levon 65*1f5207b7SJohn Levon if (get_macro_name(stmt->pos)) 66*1f5207b7SJohn Levon return; 67*1f5207b7SJohn Levon 68*1f5207b7SJohn Levon iterator = get_iterator_set(stmt->iterator_pre_statement); 69*1f5207b7SJohn Levon iter_set = expr_to_var(iterator); 70*1f5207b7SJohn Levon iterator = get_iterator_tested(stmt->iterator_pre_condition); 71*1f5207b7SJohn Levon iter_tested = expr_to_var(iterator); 72*1f5207b7SJohn Levon if (!iter_set || !iter_tested) 73*1f5207b7SJohn Levon goto free; 74*1f5207b7SJohn Levon if (strcmp(iter_set, iter_tested)) 75*1f5207b7SJohn Levon goto free; 76*1f5207b7SJohn Levon 77*1f5207b7SJohn Levon /* smatch doesn't handle loops correctly so this silences some 78*1f5207b7SJohn Levon * false positives. 79*1f5207b7SJohn Levon */ 80*1f5207b7SJohn Levon if (right_side_changes(stmt->iterator_pre_condition)) 81*1f5207b7SJohn Levon goto free; 82*1f5207b7SJohn Levon 83*1f5207b7SJohn Levon if (implied_condition_false(stmt->iterator_pre_condition)) 84*1f5207b7SJohn Levon sm_warning("we never enter this loop"); 85*1f5207b7SJohn Levon 86*1f5207b7SJohn Levon free: 87*1f5207b7SJohn Levon free_string(iter_set); 88*1f5207b7SJohn Levon free_string(iter_tested); 89*1f5207b7SJohn Levon } 90*1f5207b7SJohn Levon check_bogus_loop(int id)91*1f5207b7SJohn Levonvoid check_bogus_loop(int id) 92*1f5207b7SJohn Levon { 93*1f5207b7SJohn Levon my_id = id; 94*1f5207b7SJohn Levon add_hook(&match_loop, PRELOOP_HOOK); 95*1f5207b7SJohn Levon } 96