9 # find unbalanced hf_ variables: Compare hf_ variable usage with the hf_ variables
10 # declared in the hf_register_info array.
12 # Usage: checkhf.pl <file or files>
17 # Copyright 2005 Joerg Mayer (see AUTHORS file)
19 # Wireshark - Network traffic analyzer
20 # By Gerald Combs <gerald@wireshark.org>
21 # Copyright 1998 Gerald Combs
23 # This program is free software; you can redistribute it and/or
24 # modify it under the terms of the GNU General Public License
25 # as published by the Free Software Foundation; either version 2
26 # of the License, or (at your option) any later version.
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details.
33 # You should have received a copy of the GNU General Public License
34 # along with this program; if not, write to the Free Software
35 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 # ~/work/wireshark/trunk/epan/dissectors> ../../tools/checkhf.pl packet-afs.c
40 # Unused entry: packet-afs.c, hf_afs_ubik_voteend
41 # Unused entry: packet-afs.c, hf_afs_ubik_errcode
42 # Unused entry: packet-afs.c, hf_afs_ubik_votetype
43 # NO ARRAY: packet-afs.c, hf_afs_fs_ipaddr
45 # or checkhf.pl packet-*.c, which will check all the dissector files.
47 # NOTE: This tool currently generates false positives!
49 # The "NO ARRAY" messages - if accurate - point to an error that will
50 # cause (t)(wire)shark to terminate with an assertion when a packet containing
51 # this particular element is being dissected.
53 # The "Unused entry" message indicates the opposite:
86 foreach $element (keys %elements) {
87 $state = $elements{$element};
88 $debug>=2 && print "$currfile, $element: PRINT $state\n";
89 if ($state eq "s_usedarray") {
91 } elsif ($state eq "s_used") {
92 print "NO ARRAY: $currfile, $element\n"
93 } elsif ($state eq "s_array") {
94 print "Unused entry: $currfile, $element\n"
95 } elsif ($state eq "s_declared") {
96 print "Declared only entry: $currfile, $element\n"
97 } elsif ($state eq "s_unknown") {
98 print "UNKNOWN: $currfile, $element\n"
100 die "Impossible: State $state for $currfile, $element\n";
106 if ($currfile !~ /$ARGV/) {
108 # New file - reset array and state
111 %skip = ( "hf_register_info" => 1 );
112 $state = "s_unknown";
114 # opening then closing comment
115 if (/(.*?)\/\*.*\*\/(.*)/) {
118 # closing then opening comment
119 } elsif (/.*?\*\/(.*?)\/\*/) {
123 } elsif (/(.*?)\/\*/) {
127 } elsif (/\*\/(.*?)/) {
130 } elsif ($comment == 1) {
133 # unhandled: more than one complete comment per line
143 if (/static\s+.*int\s+(hf_\w*)\s*=\s*-1\s*;/) {
145 $type = "t_declaration";
146 # ignore: declarations without any use are detected by the compiler
148 # Skip function parameter declarations with hf_ names
149 } 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(.*)/) {
152 $debug && print "Setting skip for $element$D\n";
154 # Handle functions with multiple hf_ parameters
155 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(.*)/) {
158 $debug && print "Setting skip for $element$D\n";
162 } elsif ($brace == 1 && /^\s*?&\s*?(hf_\w*)\W+/) {
165 } elsif (/^\s*\{\s*?&\s*?(hf_\w*)\W+/) {
168 # Order matters: catch all remaining hf_ lines
169 } elsif (/\W(hf_\w*)\W/) {
171 next if ($skip{$element});
174 # current line is not relevant
186 if (!defined($elements{$element})) {
187 $state = "s_unknown";
189 $state = $elements{$element};
192 # current state + input ==> new state
193 # we currently ignore t_declaration
194 if ($state eq "s_error") {
196 } elsif ($state eq "s_unknown" && $type eq "t_usage") {
197 $newstate = "s_used";
198 } elsif ($state eq "s_unknown" && $type eq "t_array") {
199 $newstate = "s_array";
200 } elsif ($state eq "s_used" && $type eq "t_array") {
201 $newstate = "s_usedarray";
202 } elsif ($state eq "s_array" && $type eq "t_usage") {
203 $newstate = "s_usedarray";
207 $elements{$element} = $newstate;
208 $debug>=2 && print "$currfile, $element: SET $state + $type => $newstate$D\n";