aboutsummaryrefslogtreecommitdiffstats
path: root/tools/checkhf.pl
blob: da169edbf9c1bcdc721cd56c68ad28c8c26f6b2f (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/perl -w

my $debug = 0;
# 0: off
# 1: specific debug
# 2: full debug

#
# find unbalanced hf_ variables: Compare hf_ variable usage with the hf_ variables
#  declared in the hf_register_info array.
#
# Usage: checkhf.pl <file or files>

# $Id$

#
# Copyright 2005 Joerg Mayer (see AUTHORS file)
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#
# Example:
# ~/work/wireshark/trunk/epan/dissectors> ../../tools/checkhf.pl packet-afs.c
# Unused entry: packet-afs.c, hf_afs_ubik_voteend
# Unused entry: packet-afs.c, hf_afs_ubik_errcode
# Unused entry: packet-afs.c, hf_afs_ubik_votetype
# NO ARRAY: packet-afs.c, hf_afs_fs_ipaddr
#
# or checkhf.pl packet-*.c, which will check all the dissector files.
#
# NOTE: This tool currently generates false positives!
#
# The "NO ARRAY" messages - if accurate - point to an error that will
# cause (t)(wire)shark to terminate with an assertion when a packet containing
# this particular element is being dissected.
#
# The "Unused entry" message indicates the opposite: 

use strict;

my $D;

my %elements;
my $element;
my %skip;

my $state;
my $newstate;
# "s_unknown",
# "s_declared",
# "s_used",
# "s_array",
# "s_usedarray",
# "s_error"

my $type;
# "t_declaration";
# "t_usage";
# "t_array";

my $restofline;
my $currfile = "";

my $comment = 0;
my $brace = 0;

sub printprevfile {
	my $state;

	foreach $element (keys %elements) {
		$state = $elements{$element};
		$debug>=2 && print "$currfile, $element: PRINT $state\n";
		if ($state eq "s_usedarray") {
			# Everything is fine
		} elsif ($state eq "s_used") {
			print "NO ARRAY: $currfile, $element\n"
		} elsif ($state eq "s_array") {
			print "Unused entry: $currfile, $element\n"
		} elsif ($state eq "s_declared") {
			print "Declared only entry: $currfile, $element\n"
		} elsif ($state eq "s_unknown") {
			print "UNKNOWN: $currfile, $element\n"
		} else {
			die "Impossible: State $state for $currfile, $element\n";
		}
	}
}

while (<>) {
	if ($currfile !~ /$ARGV/) {
		&printprevfile();
		# New file - reset array and state
		$currfile = $ARGV;
		%elements = ( );
		%skip = ( "hf_register_info" => 1 );
		$state = "s_unknown";
	}
	# opening then closing comment
	if (/(.*?)\/\*.*\*\/(.*)/) {
		$comment = 0;
		$_ = "$1$2";
	# closing then opening comment
	} elsif (/.*?\*\/(.*?)\/\*/) {
		$comment = 1;
		$_ = "$1";
	# opening comment
	} elsif (/(.*?)\/\*/) {
		$comment = 1;
		$_ = "$1";
	# closing comment
	} elsif (/\*\/(.*?)/) {
		$comment = 0;
		$_ = "$1";
	} elsif ($comment == 1) {
		next;
	}
	# unhandled: more than one complete comment per line

	chomp;
	if ($debug) {
		$D = " ($_)";
	} else {
		$D = "";
	}

	# Read input
	if (/static\s+.*int\s+(hf_\w*)\s*=\s*-1\s*;/) {
		$element = $1;
		$type = "t_declaration";
		# ignore: declarations without any use are detected by the compiler
		next;
	# Skip function parameter declarations with hf_ names
	} elsif (/(int\s+?|int\s*?\*\s*?|header_field_info\s+?|header_field_info\s*?\*\s*?|hf_register_info\s+?|hf_register_info\s*?\*\s*?|->\s*?)(hf_\w*)\W(.*)/) {
		$element = $2;
		$restofline = $3;
		$debug && print "Setting skip for $element$D\n";
		$skip{$element} = 1;
		# Handle functions with multiple hf_ parameters
		while ($restofline =~ /(int\s+?|int\s*?\*\s*?|header_field_info\s+?|header_field_info\s*?\*\s*?|hf_register_info\s+?|hf_register_info\s*?\*\s*?|->\s*?)(hf_\w*)\W(.*)/) {
			$element = $2;
			$restofline = $3;
			$debug && print "Setting skip for $element$D\n";
			$skip{$element} = 1;
		}
		next;
	} elsif ($brace == 1 && /^\s*?&\s*?(hf_\w*)\W+/) {
		$element = $1;
		$type = "t_array";
	} elsif (/^\s*\{\s*?&\s*?(hf_\w*)\W+/) {
		$element = $1;
		$type = "t_array";
	# Order matters: catch all remaining hf_ lines
	} elsif (/\W(hf_\w*)\W/) {
		$element = $1;
		next if ($skip{$element});
		$type = "t_usage";
	} else {
		# current line is not relevant
		next;
	}
	# Line with only a {
	if (/^\s+\{\s*$/) {
		$brace = 1;
		next;
	} else {
		$brace = 0;
	}

	# Get current state
	if (!defined($elements{$element})) {
		$state = "s_unknown";
	} else {
		$state = $elements{$element};
	}

	# current state + input ==> new state
	# we currently ignore t_declaration
	if ($state eq "s_error") {
		$newstate = $state;
	} elsif ($state eq "s_unknown" && $type eq "t_usage") {
			$newstate = "s_used";
	} elsif ($state eq "s_unknown" && $type eq "t_array") {
			$newstate = "s_array";
	} elsif ($state eq "s_used" && $type eq "t_array") {
			$newstate = "s_usedarray";
	} elsif ($state eq "s_array" && $type eq "t_usage") {
			$newstate = "s_usedarray";
	} else {
		$newstate = $state;
	}
	$elements{$element} = $newstate;
	$debug>=2 && print "$currfile, $element: SET $state + $type => $newstate$D\n";
}
&printprevfile();

exit 0;

__END__