db: do the caller_info stuff earlier
[smatch.git] / check_do_while_loop_limit.c
blobcc04dfa5957add943e363506f35972d7eacfab55
1 /*
2 * Copyright (C) 2021 Oracle.
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"
20 #include "smatch_extra.h"
22 static int my_id;
24 STATE(post_minus);
26 static bool is_post_minus(struct expression *expr)
28 expr = strip_expr(expr);
29 if (!expr)
30 return false;
31 if (expr->type != EXPR_POSTOP)
32 return false;
33 if (expr->op != SPECIAL_DECREMENT)
34 return false;
35 return true;
38 static void match_post_loop(struct expression *expr)
40 struct statement *stmt;
42 expr = strip_expr(expr);
43 stmt = expr_get_parent_stmt(expr);
45 if (!stmt)
46 return;
47 if (stmt->type != STMT_ITERATOR)
48 return;
49 if (!is_post_minus(stmt->iterator_post_condition))
50 return;
51 if (is_post_minus(stmt->iterator_post_condition))
52 set_state_expr(my_id, stmt->iterator_post_condition->left, &post_minus);
55 static void match_condition(struct expression *expr)
57 if (get_state_expr(my_id, expr) != &post_minus)
58 return;
59 sm_warning("do while ends on '%s == -1'", expr_to_str(expr));
62 void check_do_while_loop_limit(int id)
64 my_id = id;
65 add_hook(&match_post_loop, CONDITION_HOOK);
66 add_hook(&match_condition, CONDITION_HOOK);