core: silence some false positives from parsing invalid code
[smatch.git] / check_resource_size.c
blob3a32581673fcc97f3fac832b4507e7da864a951c
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;
21 extern int check_assigned_expr_id;
23 static int is_probably_ok(struct expression *expr)
25 expr = strip_expr(expr);
27 if (expr->type == EXPR_BINOP)
28 return 1;
29 if (expr->type == EXPR_SIZEOF)
30 return 1;
32 return 0;
35 static void verify_size_expr(struct expression *expr)
37 if (expr->type != EXPR_BINOP)
38 return;
39 if (expr->op != '-')
40 return;
41 if (is_probably_ok(expr->left))
42 return;
43 if (is_probably_ok(expr->right))
44 return;
45 sm_msg("warn: consider using resource_size() here");
48 static void handle_assigned_expr(struct expression *expr)
50 struct smatch_state *state;
52 state = get_state_expr(check_assigned_expr_id, expr);
53 if (!state || !state->data)
54 return;
55 expr = (struct expression *)state->data;
56 verify_size_expr(expr);
59 static void match_resource(const char *fn, struct expression *expr, void *_arg_no)
61 struct expression *arg_expr;
62 int arg_no = PTR_INT(_arg_no);
64 arg_expr = get_argument_from_call_expr(expr->args, arg_no);
65 arg_expr = strip_expr(arg_expr);
66 if (!arg_expr)
67 return;
69 if (arg_expr->type == EXPR_SYMBOL) {
70 handle_assigned_expr(arg_expr);
71 return;
73 verify_size_expr(arg_expr);
76 void check_resource_size(int id)
78 my_id = id;
80 if (option_project != PROJ_KERNEL)
81 return;
83 add_function_hook("ioremap_nocache", &match_resource, (void *)1);
84 add_function_hook("ioremap", &match_resource, (void *)1);
85 add_function_hook("__request_region", &match_resource, (void *)2);
86 add_function_hook("__release_region", &match_resource, (void *)2);
87 add_function_hook("__devm_request_region", &match_resource, (void *)3);
88 add_function_hook("__devm_release_region", &match_resource, (void *)3);