function_hooks: do set_fresh_mtag_returns() later
[smatch.git] / check_platform_get_irq_return.c
blobe2597ce04ce8338eebe5bcfd952473a8ed8172ff
1 /*
2 * Copyright (C) 2022 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(get_irq);
26 static void clear_state(struct sm_state *sm, struct expression *mod_expr)
28 set_state(my_id, sm->name, sm->sym, &undefined);
31 static void match_platform_get_irq(struct expression *expr, const char *name, struct symbol *sym, void *data)
33 set_state(my_id, name, sym, &get_irq);
36 static bool assigned_zero(struct expression *expr)
38 struct sm_state *sm, *tmp;
39 struct expression *prev;
41 sm = get_assigned_sm(expr);
42 if (!sm)
43 return false;
44 FOR_EACH_PTR(sm->possible, tmp) {
45 prev = tmp->state->data;
46 if (expr_is_zero(prev))
47 return true;
48 } END_FOR_EACH_PTR(tmp);
50 return false;
53 static bool is_part_of_select(struct expression *expr)
55 struct expression *parent;
56 sval_t sval;
58 /* Code like "return irq ?: -EINVAL;" is wrong but harmless */
60 parent = expr_get_parent_expr(expr);
61 if (!parent)
62 return false;
63 if (parent->type != EXPR_CONDITIONAL &&
64 parent->type != EXPR_SELECT)
65 return false;
67 if (get_value(parent->cond_false, &sval) &&
68 sval.value >= -4095 &&
69 sval.value < 0)
70 return true;
72 return false;
75 static void match_condition(struct expression *expr)
77 struct range_list *rl;
79 if (!expr_has_possible_state(my_id, expr, &get_irq))
80 return;
82 if (get_implied_rl(expr, &rl) && rl_min(rl).value == 0)
83 return;
85 if (assigned_zero(expr))
86 return;
88 if (is_part_of_select(expr))
89 return;
91 sm_msg("warn: platform_get_irq() does not return zero");
94 void check_platform_get_irq_return(int id)
96 my_id = id;
98 add_function_param_key_hook_late("platform_get_irq", &match_platform_get_irq, -1, "$", NULL);
99 add_function_param_key_hook_late("platform_get_irq_optional", &match_platform_get_irq, -1, "$", NULL);
100 add_function_param_key_hook_late("platform_get_irq_byname", &match_platform_get_irq, -1, "$", NULL);
101 add_function_param_key_hook_late("platform_get_irq_byname_optional", &match_platform_get_irq, -1, "$", NULL);
102 add_modification_hook(my_id, &clear_state);
103 add_hook(&match_condition, CONDITION_HOOK);