kernel.return_fixes: add mipi_dsi_device_transfer(), timer_delete() and get_device()
[smatch.git] / check_bogus_loop.c
blob70f3dfa1725135a49f7ed2230eb1046f8c29a54b
1 /*
2 * Copyright (C) 2011 Dan Carpenter.
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.
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.
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
18 #include "smatch.h"
19 #include "smatch_slist.h"
21 static int my_id;
23 static int right_side_changes(struct expression *expr)
25 sval_t dummy;
27 if (get_value(expr->right, &dummy))
28 return 0;
29 return 1;
32 static bool is_lt_ARRAY_SIZE(struct expression *expr)
34 char *macro;
37 * One cause of false positives is:
38 * for (i = 0; i < ARRAY_SIZE(); i++) {
39 * but the ARRAY_SIZE() is zero. Silence these false positives.
42 if (!expr || expr->type != EXPR_COMPARE)
43 return false;
44 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
45 return false;
46 if (!expr_is_zero(expr->right))
47 return false;
48 macro = get_macro_name(expr->right->pos);
49 if (!macro || strcmp(macro, "ARRAY_SIZE") != 0)
50 return false;
52 return true;
55 static struct expression *get_iterator_set(struct statement *stmt)
57 struct expression *expr;
59 if (!stmt)
60 return NULL;
61 if (stmt->type != STMT_EXPRESSION)
62 return NULL;
63 expr = stmt->expression;
64 if (expr->type != EXPR_ASSIGNMENT)
65 return NULL;
66 if (expr->op != '=')
67 return NULL;
68 if (right_side_changes(expr))
69 return NULL;
70 return expr->left;
73 static struct expression *get_iterator_tested(struct expression *expr)
75 if (!expr)
76 return NULL;
77 if (expr->type != EXPR_COMPARE)
78 return NULL;
79 return expr->left;
82 static void match_loop(struct statement *stmt)
84 struct expression *iterator;
85 char *iter_set;
86 char *iter_tested;
88 if (get_macro_name(stmt->pos))
89 return;
91 iterator = get_iterator_set(stmt->iterator_pre_statement);
92 iter_set = expr_to_var(iterator);
93 iterator = get_iterator_tested(stmt->iterator_pre_condition);
94 iter_tested = expr_to_var(iterator);
95 if (!iter_set || !iter_tested)
96 goto free;
97 if (strcmp(iter_set, iter_tested))
98 goto free;
100 /* smatch doesn't handle loops correctly so this silences some
101 * false positives.
103 if (right_side_changes(stmt->iterator_pre_condition))
104 goto free;
106 if (is_lt_ARRAY_SIZE(stmt->iterator_pre_condition))
107 goto free;
109 if (implied_condition_false(stmt->iterator_pre_condition))
110 sm_warning("we never enter this loop");
112 free:
113 free_string(iter_set);
114 free_string(iter_tested);
117 void check_bogus_loop(int id)
119 my_id = id;
120 add_hook(&match_loop, PRELOOP_HOOK);