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
19 #include "smatch_slist.h"
23 static int right_side_changes(struct expression
*expr
)
27 if (get_value(expr
->right
, &dummy
))
32 static struct expression
*get_iterator_set(struct statement
*stmt
)
34 struct expression
*expr
;
38 if (stmt
->type
!= STMT_EXPRESSION
)
40 expr
= stmt
->expression
;
41 if (expr
->type
!= EXPR_ASSIGNMENT
)
45 if (right_side_changes(expr
))
50 static struct expression
*get_iterator_tested(struct expression
*expr
)
54 if (expr
->type
!= EXPR_COMPARE
)
59 static void match_loop(struct statement
*stmt
)
61 struct expression
*iterator
;
65 if (get_macro_name(stmt
->pos
))
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
)
74 if (strcmp(iter_set
, iter_tested
))
77 /* smatch doesn't handle loops correctly so this silences some
80 if (right_side_changes(stmt
->iterator_pre_condition
))
83 if (implied_condition_false(stmt
->iterator_pre_condition
))
84 sm_msg("warn: we never enter this loop");
87 free_string(iter_set
);
88 free_string(iter_tested
);
91 void check_bogus_loop(int id
)
94 add_hook(&match_loop
, PRELOOP_HOOK
);