11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2015 Oracle.
31f5207b7SJohn Levon  *
41f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon  * of the License, or (at your option) any later version.
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
121f5207b7SJohn Levon  * GNU General Public License for more details.
131f5207b7SJohn Levon  *
141f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon  */
171f5207b7SJohn Levon 
181f5207b7SJohn Levon #include "smatch.h"
191f5207b7SJohn Levon #include "smatch_slist.h"
201f5207b7SJohn Levon #include "smatch_extra.h"
211f5207b7SJohn Levon 
221f5207b7SJohn Levon static int my_id;
231f5207b7SJohn Levon 
241f5207b7SJohn Levon STATE(positive);
251f5207b7SJohn Levon STATE(ok);
261f5207b7SJohn Levon 
ok_to_use(struct sm_state * sm,struct expression * mod_expr)271f5207b7SJohn Levon static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
281f5207b7SJohn Levon {
291f5207b7SJohn Levon 	if (sm->state != &ok)
301f5207b7SJohn Levon 		set_state(my_id, sm->name, sm->sym, &ok);
311f5207b7SJohn Levon }
321f5207b7SJohn Levon 
match_assign(const char * fn,struct expression * expr,void * unused)331f5207b7SJohn Levon static void match_assign(const char *fn, struct expression *expr, void *unused)
341f5207b7SJohn Levon {
35*efe51d0cSJohn Levon 	struct range_list *rl;
36*efe51d0cSJohn Levon 
37*efe51d0cSJohn Levon 	if (!get_implied_rl(expr->right, &rl))
38*efe51d0cSJohn Levon 		return;
39*efe51d0cSJohn Levon 	if (rl_max(rl).value != 1)
40*efe51d0cSJohn Levon 		return;
411f5207b7SJohn Levon 	set_state_expr(my_id, expr->left, &positive);
421f5207b7SJohn Levon }
431f5207b7SJohn Levon 
match_condition(struct expression * expr)441f5207b7SJohn Levon static void match_condition(struct expression *expr)
451f5207b7SJohn Levon {
461f5207b7SJohn Levon 	if (!get_state_expr(my_id, expr))
471f5207b7SJohn Levon 		return;
481f5207b7SJohn Levon 	/* If the variable is zero that's ok */
491f5207b7SJohn Levon 	set_true_false_states_expr(my_id, expr, NULL, &ok);
501f5207b7SJohn Levon }
511f5207b7SJohn Levon 
match_return(struct expression * ret_value)521f5207b7SJohn Levon static void match_return(struct expression *ret_value)
531f5207b7SJohn Levon {
541f5207b7SJohn Levon 	struct smatch_state *state;
551f5207b7SJohn Levon 	struct sm_state *sm;
561f5207b7SJohn Levon 	sval_t min;
571f5207b7SJohn Levon 
581f5207b7SJohn Levon 	sm = get_sm_state_expr(my_id, ret_value);
591f5207b7SJohn Levon 	if (!sm)
601f5207b7SJohn Levon 		return;
611f5207b7SJohn Levon 	if (!slist_has_state(sm->possible, &positive))
621f5207b7SJohn Levon 		return;
631f5207b7SJohn Levon 	state = get_state_expr(SMATCH_EXTRA, ret_value);
641f5207b7SJohn Levon 	if (!state)
651f5207b7SJohn Levon 		return;
661f5207b7SJohn Levon 	if (!get_absolute_min(ret_value, &min))
671f5207b7SJohn Levon 		return;
681f5207b7SJohn Levon 	if (min.value == 0)
691f5207b7SJohn Levon 		return;
701f5207b7SJohn Levon 	sm_warning("dma_mapping_error() doesn't return an error code");
711f5207b7SJohn Levon }
721f5207b7SJohn Levon 
check_dma_mapping_error(int id)731f5207b7SJohn Levon void check_dma_mapping_error(int id)
741f5207b7SJohn Levon {
751f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
761f5207b7SJohn Levon 		return;
771f5207b7SJohn Levon 
781f5207b7SJohn Levon 	my_id = id;
791f5207b7SJohn Levon 	add_function_assign_hook("dma_mapping_error", &match_assign, NULL);
801f5207b7SJohn Levon 	add_function_assign_hook("pci_dma_mapping_error", &match_assign, NULL);
811f5207b7SJohn Levon 	add_hook(&match_condition, CONDITION_HOOK);
821f5207b7SJohn Levon 	add_hook(&match_return, RETURN_HOOK);
831f5207b7SJohn Levon 	add_modification_hook(my_id, &ok_to_use);
841f5207b7SJohn Levon }
85