param_key: fix container of when no struct member is referenced
[smatch.git] / check_resource_size.c
blob393eb12674ed598829494ff82817b7ee3d128b24
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
18 #include "smatch.h"
20 static int my_id;
22 static int is_probably_ok(struct expression *expr)
24 expr = strip_expr(expr);
26 if (expr->type == EXPR_BINOP)
27 return 1;
28 if (expr->type == EXPR_SIZEOF)
29 return 1;
31 return 0;
34 static void verify_size_expr(struct expression *expr)
36 if (expr->type != EXPR_BINOP)
37 return;
38 if (expr->op != '-')
39 return;
40 if (is_probably_ok(expr->left))
41 return;
42 if (is_probably_ok(expr->right))
43 return;
44 sm_warning("consider using resource_size() here");
47 static void handle_assigned_expr(struct expression *expr)
49 expr = get_assigned_expr(expr);
50 if (!expr)
51 return;
52 verify_size_expr(expr);
55 static void match_resource(const char *fn, struct expression *expr, void *_arg_no)
57 struct expression *arg_expr;
58 int arg_no = PTR_INT(_arg_no);
60 arg_expr = get_argument_from_call_expr(expr->args, arg_no);
61 arg_expr = strip_expr(arg_expr);
62 if (!arg_expr)
63 return;
65 if (arg_expr->type == EXPR_SYMBOL) {
66 handle_assigned_expr(arg_expr);
67 return;
69 verify_size_expr(arg_expr);
72 void check_resource_size(int id)
74 my_id = id;
76 if (option_project != PROJ_KERNEL)
77 return;
79 add_function_hook("ioremap_nocache", &match_resource, (void *)1);
80 add_function_hook("ioremap", &match_resource, (void *)1);
81 add_function_hook("__request_region", &match_resource, (void *)2);
82 add_function_hook("__release_region", &match_resource, (void *)2);
83 add_function_hook("__devm_request_region", &match_resource, (void *)3);
84 add_function_hook("__devm_release_region", &match_resource, (void *)3);