11f5207bJohn Levon/*
21f5207bJohn Levon * Copyright (C) 2010 Dan Carpenter.
31f5207bJohn Levon *
41f5207bJohn Levon * This program is free software; you can redistribute it and/or
51f5207bJohn Levon * modify it under the terms of the GNU General Public License
61f5207bJohn Levon * as published by the Free Software Foundation; either version 2
71f5207bJohn Levon * of the License, or (at your option) any later version.
81f5207bJohn Levon *
91f5207bJohn Levon * This program is distributed in the hope that it will be useful,
101f5207bJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
121f5207bJohn Levon * GNU General Public License for more details.
131f5207bJohn Levon *
141f5207bJohn Levon * You should have received a copy of the GNU General Public License
151f5207bJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207bJohn Levon */
171f5207bJohn Levon
181f5207bJohn Levon#include "smatch.h"
191f5207bJohn Levon
201f5207bJohn Levonstatic int my_id;
211f5207bJohn Levonstatic struct symbol *func_sym;
221f5207bJohn Levon
231f5207bJohn LevonSTATE(argument);
241f5207bJohn LevonSTATE(ok);
251f5207bJohn Levon
261f5207bJohn Levonstatic void set_ok(struct sm_state *sm, struct expression *mod_expr)
271f5207bJohn Levon{
281f5207bJohn Levon	if (sm->state != &ok)
291f5207bJohn Levon		set_state(my_id, sm->name, sm->sym, &ok);
301f5207bJohn Levon}
311f5207bJohn Levon
321f5207bJohn Levonstatic void match_function_def(struct symbol *sym)
331f5207bJohn Levon{
341f5207bJohn Levon	struct symbol *arg;
351f5207bJohn Levon
361f5207bJohn Levon	func_sym = sym;
371f5207bJohn Levon	FOR_EACH_PTR(func_sym->ctype.base_type->arguments, arg) {
381f5207bJohn Levon		if (!arg->ident) {
391f5207bJohn Levon			continue;
401f5207bJohn Levon		}
411f5207bJohn Levon		set_state(my_id, arg->ident->name, arg, &argument);
421f5207bJohn Levon	} END_FOR_EACH_PTR(arg);
431f5207bJohn Levon}
441f5207bJohn Levon
451f5207bJohn Levonstatic int get_arg_num(struct expression *expr)
461f5207bJohn Levon{
471f5207bJohn Levon	struct smatch_state *state;
481f5207bJohn Levon	struct symbol *arg;
491f5207bJohn Levon	struct symbol *this_arg;
501f5207bJohn Levon	int i;
511f5207bJohn Levon
521f5207bJohn Levon	expr = strip_expr(expr);
531f5207bJohn Levon	if (expr->type != EXPR_SYMBOL)
541f5207bJohn Levon		return -1;
551f5207bJohn Levon	this_arg = expr->symbol;
561f5207bJohn Levon
571f5207bJohn Levon	state = get_state_expr(my_id, expr);
581f5207bJohn Levon	if (!state || state != &argument)
591f5207bJohn Levon		return -1;
601f5207bJohn Levon
611f5207bJohn Levon	i = 0;
621f5207bJohn Levon	FOR_EACH_PTR(func_sym->ctype.base_type->arguments, arg) {
631f5207bJohn Levon		if (arg == this_arg)
641f5207bJohn Levon			return i;
651f5207bJohn Levon		i++;
661f5207bJohn Levon	} END_FOR_EACH_PTR(arg);
671f5207bJohn Levon
681f5207bJohn Levon	return -1;
691f5207bJohn Levon}
701f5207bJohn Levon
711f5207bJohn Levonstatic void match_is_err(const char *fn, struct expression *expr, void *unused)
721f5207bJohn Levon{
731f5207bJohn Levon	struct expression *arg;
741f5207bJohn Levon	int arg_num;
751f5207bJohn Levon
761f5207bJohn Levon	arg = get_argument_from_call_expr(expr->args, 0);
771f5207bJohn Levon	arg_num = get_arg_num(arg);
781f5207bJohn Levon	if (arg_num < 0)
791f5207bJohn Levon		return;
801f5207bJohn Levon	sm_msg("info: expects ERR_PTR %d", arg_num);
811f5207bJohn Levon}
821f5207bJohn Levon
831f5207bJohn Levonvoid check_expects_err_ptr(int id)
841f5207bJohn Levon{
851f5207bJohn Levon	if (option_project != PROJ_KERNEL)
861f5207bJohn Levon		return;
871f5207bJohn Levon	if (!option_info)
881f5207bJohn Levon		return;
891f5207bJohn Levon
901f5207bJohn Levon	my_id = id;
911f5207bJohn Levon	add_hook(&match_function_def, FUNC_DEF_HOOK);
921f5207bJohn Levon	add_modification_hook(my_id, &set_ok);
931f5207bJohn Levon	add_function_hook("IS_ERR", &match_is_err, NULL);
941f5207bJohn Levon}