2 * smatch/check_bogus_for_loop.c
4 * Copyright (C) 2011 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 #include "smatch_slist.h"
15 static int right_side_changes(struct expression
*expr
)
19 if (get_value(expr
->right
, &dummy
))
24 static struct expression
*get_iterator_set(struct statement
*stmt
)
26 struct expression
*expr
;
30 if (stmt
->type
!= STMT_EXPRESSION
)
32 expr
= stmt
->expression
;
33 if (expr
->type
!= EXPR_ASSIGNMENT
)
37 if (right_side_changes(expr
))
42 static struct expression
*get_iterator_tested(struct expression
*expr
)
46 if (expr
->type
!= EXPR_COMPARE
)
51 static void match_loop(struct statement
*stmt
)
53 struct expression
*iterator
;
57 if (get_macro_name(&stmt
->pos
))
60 iterator
= get_iterator_set(stmt
->iterator_pre_statement
);
61 iter_set
= get_variable_from_expr(iterator
, NULL
);
62 iterator
= get_iterator_tested(stmt
->iterator_pre_condition
);
63 iter_tested
= get_variable_from_expr(iterator
, NULL
);
64 if (!iter_set
|| !iter_tested
)
66 if (strcmp(iter_set
, iter_tested
))
69 /* smatch doesn't handle loops correctly so this silences some
72 if (right_side_changes(stmt
->iterator_pre_condition
))
75 if (implied_condition_false(stmt
->iterator_pre_condition
))
76 sm_msg("warn: we never enter this loop");
79 free_string(iter_set
);
80 free_string(iter_tested
);
83 void check_bogus_loop(int id
)
86 add_hook(&match_loop
, PRELOOP_HOOK
);