/*
 * Copyright (C) 2023 Oracle.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
 */

#include "smatch.h"
#include "smatch_slist.h"

static int my_id;

static void check_non_null(struct expression *expr, const char *name, struct symbol *sym, void *data)
{
	struct sm_state *sm, *tmp;
	sval_t sval;

	sm = get_sm_state(SMATCH_EXTRA, name, sym);
	if (!sm)
		return;

	FOR_EACH_PTR(sm->possible, tmp) {
		if (!estate_get_single_value(tmp->state, &sval) ||
		    sval.value != 0)
			continue;
		sm_warning("'%s' potentially NULL", name);
		return;
	} END_FOR_EACH_PTR(tmp);
}

void check_passing_possible_null(int id)
{
	my_id = id;

	add_function_param_key_hook_early("iounmap", &check_non_null, 0, "$", NULL);
}