buf_size: allow strncmp("foo", bar, 100) where 100 is larger than "foo"
[smatch.git] / check_propagate.c
blob7b17123819b41105c21ac6df2be8d82cff353e75
1 /*
2 * Copyright (C) 2010 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 * People should just return the error codes they received
20 * instead of making up there own dumb error codes all the time.
23 #include "smatch.h"
25 static int my_id;
27 static struct expression *last_return;
28 static struct expression *last_func;
30 static char *get_fn_name(struct expression *expr)
32 if (expr->type != EXPR_CALL)
33 return NULL;
34 if (expr->fn->type != EXPR_SYMBOL)
35 return NULL;
36 return expr_to_var(expr->fn);
39 static void match_call_assignment(struct expression *expr)
41 if (get_macro_name(expr->left->pos))
42 return;
43 last_return = expr->left;
44 last_func = expr->right;
47 static void match_unset(struct expression *expr)
49 last_return = NULL;
52 static void match_return(struct expression *ret_value)
54 sval_t rval;
55 sval_t lret;
56 char *name;
58 if (!get_value(ret_value, &rval) || rval.value >= 0)
59 return;
60 if (get_implied_value(last_return, &lret))
61 return;
62 if (!get_implied_max(last_return, &lret) || lret.value >= 0)
63 return;
64 if (get_implied_min(last_return, &lret) && !sval_is_min(lret))
65 return;
66 name = expr_to_var(last_return);
67 sm_msg("info: why not propagate '%s' from %s() instead of %s?",
68 name, get_fn_name(last_func), sval_to_str(rval));
69 free_string(name);
72 void check_propagate(int id)
74 if (option_project != PROJ_KERNEL)
75 return;
77 my_id = id;
78 add_hook(&match_unset, ASSIGNMENT_HOOK);
79 add_hook(&match_call_assignment, CALL_ASSIGNMENT_HOOK);
80 add_hook(&match_return, RETURN_HOOK);