File: check-null-false-returns

package info (click to toggle)
gnumeric 1.10.8-1squeeze5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 90,968 kB
  • ctags: 23,303
  • sloc: ansic: 248,235; xml: 51,894; sh: 10,491; makefile: 2,822; perl: 2,466; yacc: 1,272; python: 205
file content (147 lines) | stat: -rw-r--r-- 3,594 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/perl -w
#
#  Gnumeric
#
#  Copyright (C) 2003 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, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  Author: Morten Welinder <terra@gnome.org>

use strict;

my $exitcode = 0;
my $verbose = 0;
my $strict = 0;

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

my %base_exceptions =
    ();

my %exceptions =
    ();

{
    local (*FIND);
    open (*FIND, "find . '(' -type f -name '*.c' -print ')' -o '(' -type d '(' -name .git -o -name intl -o -name macros -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 "Checking $filename...\n";
	    my $lineno = 0;
	    my $state = 0;
	    my ($good,$dubious,$bad,$functype);
	    my ($seen_good,$seen_dubious,$seen_bad);
	    my %values;
	  LINE:
	    while (<FIL>) {
		$lineno++;

		if (/^(static\s+)?(inline\s+)?(unsigned\s+|signed\s+|const\s+)*(int|gu?int(|8|16|32|64)|char|gu?char)\b[^*]*$/) {
		    $state = 1;
		    $good = '[-+]?\d+';
		    $dubious = 'FALSE|TRUE';
		    $bad = 'NULL';
		    $functype = 'integer';
		} elsif (/^(static\s+)?(inline\s+)?gboolean\b[^*]*$/) {
		    $state = 1;
		    $good = 'FALSE|TRUE';
		    $dubious = '0|1';
		    $bad = 'NULL|[-+]\d+|[2-9]\d*';
		    $functype = 'boolean';
		} elsif (/^(static\s+)?(inline\s+)?(const\s+)?(gpointer|\S[ a-zA-Z0-9]+\*)/) {
		    $state = 1;
		    $good = 'NULL';
		    $dubious = '0';
		    $bad = 'FALSE';
		    $functype = 'pointer';
		}

		if ($state == 1) {
		    if (/;\s*$/) {
			$state = 0;
			next;
		    }

		    if (/^\{/) {
			$state++;
			$seen_good = $seen_dubious = $seen_bad = 0;
			%values = ();
			next;
		    }
		}

		if (/^\}/) {
		    if ($state == 2 && !$strict && $seen_good && $seen_dubious) {
			print "$filename:$lineno: mixture of good and dubious return values: ";
			print join (', ', sort keys %values), ".\n";
		    }

		    $state = 0;
		    next;
		}

		if ($state == 2) {
		    my $val;

		    if (/\breturn(\s|\()\s*($good|$dubious|$bad)\s*\)*\s*;/) {
			$val = $2;
		    } elsif (/\bg_return_val_if_fail.*,\s*($good|$dubious|$bad)\s*\)\s*;\s*$/) {
			$val = $1;
		    } else {
			next;
		    }

		    my $barf = 0;
		    if ($val =~ /^($bad)$/) {
			$seen_bad = 1;
			$barf = 1;
		    } elsif ($val =~ /^($dubious)$/) {
			$seen_dubious = 1;
			$barf = 1 if $strict;
			$values{$val} = 1;
		    } else {
			$seen_good = 1;
			$values{$val} = 1;
		    }

		    print "$filename:$lineno: returning $val in a $functype function.\n"
			if $barf;

		    next;
		}
	    }
	    close (*FIL);
	} else {
	    print STDERR "$0: Cannot read `$filename': $!\b";
	    $exitcode = 1;
	}
    }
    close (*FIND);
}

exit $exitcode;