#!/usr/bin/perl -w
#
#  Gnumeric
#
#  Copyright (C) 2006 Morten Welinder.
#
#  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 library 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 library; if not, see <https://www.gnu.org/licenses/>.
#
#  Author: Morten Welinder <terra@gnome.org>

use strict;

my $exitcode = 0;

my @free_funcs = qw(g_free
		    g_list_free g_slist_free
		    gnm_expr_list_free gnm_expr_list_unref
		    go_format_unref value_release
		    gnm_action_unref gnm_criteria_unref gnm_expr_sharer_unref
		    gnm_expr_deriv_info_unref gnm_matrix_unref
		    workbook_sheet_state_unref gnm_named_expr_collection_unref
		    go_string_unref style_color_unref gnm_style_border_unref
		    gnm_expr_top_unref);
my @value_refs_funcs = qw(g_strdup go_string_ref
			  style_color_ref gnm_style_border_ref gnm_action_ref 
			  gnm_expr_deriv_info_ref gnm_matrix_ref
			  gnm_expr_top_ref
			  value_dup);
my @list_funcs = qw(g_slist_free g_slist_free_full g_slist_foreach
		    g_list_free g_list_free_full g_list_foreach GO_LIST_FOREACH
		    go_list_free_custom go_slist_free_custom
		    colrow_index_list_destroy colrow_index_list_destroy
		    colrow_index_list_destroy);

my $MATCH1 = join('|', @free_funcs, @value_refs_funcs);
my $MATCH2 = join('|', @value_refs_funcs);
my $MATCH3 = join('|', @list_funcs);

warn "$0: should be run from top-level directory.\n"
    unless -r "configure.ac" && -r 'ChangeLog';

my %base_exceptions =
    ();

my %exceptions =
    ();

{
    local (*FIND);
    open (*FIND, "find . '(' -type f -name '*.c' -print ')' -o '(' -type d '(' -name intl -o -name macros -o -name .git -o -name win32 ')' -prune ')' |")
	or die "$0: cannot execute find: $!\n";
  FILE:
    foreach my $filename (<FIND>) {
	chomp $filename;
	$filename =~ s|^\./||;

	next if $exceptions{$filename};
	my $basename = $filename;
	$basename =~ s|^.*/||;
	next if $base_exceptions{$basename};

	local (*FIL);
	if (open (*FIL, "< $filename")) {
	    # print STDERR "Checking $filename...\n";
	    my $lineno = 0;
	    my @lines;
	  LINE:
	    while (<FIL>) {
		$lineno++;

		push @lines, $_;

		if ($lineno >= 2 &&
		    ($lines[-2] . $lines[-1]) =~
		    /^\s*if\s*\(\s*(NULL\s*!=\s*)?([^ 	()]+)\s*(!=\s*NULL\s*)?\)\s*($MATCH1)\s*\(\s*\2\s*\)\s*;/) {
		    print STDERR "$0: Checked $4 at $filename:$lineno\n";
		    next LINE;
		}

		if ($lineno >= 2 &&
		    ($lines[-2] . $lines[-1]) =~
		    /^\s*if\s*\(\s*(NULL\s*!=\s*)?([^ 	()]+)\s*(!=\s*NULL\s*)?\)\s*($MATCH3)\s*\(\s*\2\s*,.*\)\s*;/) {
		    print STDERR "$0: Checked $4 at $filename:$lineno\n";
		    next LINE;
		}

		if ($lineno >= 4 &&
		    ($lines[-4] . $lines[-3] . $lines[-2] . $lines[-1] ) =~
		    /^\s*if\s*\(\s*(NULL\s*!=\s*)?([^ 	()]+)\s*(!=\s*NULL\s*)?\)\s*{\s*($MATCH1)\s*\(\s*\2\s*\)\s*;\s*\2\s*=\s*(0|NULL)\s*;\s*}/) {
		    print STDERR "$0: Checked $4 at $filename:$lineno\n";
		    next LINE;
		}

		if ($lineno >= 4 &&
		    ($lines[-4] . $lines[-3] . $lines[-2] . $lines[-1] ) =~
		    /^\s*if\s*\(\s*(NULL\s*!=\s*)?([^ 	()]+)\s*(!=\s*NULL\s*)?\)\s*{\s*($MATCH3)\s*\(\s*\2\s*,.*\)\s*;\s*\2\s*=\s*(0|NULL)\s*;\s*}/) {
		    print STDERR "$0: Checked $4 at $filename:$lineno\n";
		    next LINE;
		}

		if ($lineno >= 3 &&
		    ($lines[-3] . $lines[-2] . $lines[-1]) =~
		    /^[^\n]*([^ 	()]+)(\s*!=\s*NULL)?\s*\?\s*($MATCH2)\s*\(\s*\1\s*\)\s*:\s*NULL/) {
		    print STDERR "$0: Checked $3 at $filename:$lineno\n";
		    next LINE;
		}

		if ($lineno >= 3 &&
		    ($lines[-3] . $lines[-2] . $lines[-1]) =~
		    /^[^\n]*(\s*NULL\s*!=\s*)([^ 	()]+)\s*\?\s*($MATCH2)\s*\(\s*\2\s*\)\s*:\s*NULL/) {
		    print STDERR "$0: Checked $3 at $filename:$lineno\n";
		    next LINE;
		}
	    }
	    close (*FIL);
	} else {
	    print STDERR "$0: Cannot read `$filename': $!\b";
	    $exitcode = 1;
	}
    }
    close (*FIND);
}

exit $exitcode;
