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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 - points 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: We define an entry but
54 # never use it in a proto_...add... function.
91 foreach $element (keys %elements) {
92 $state = $elements{$element};
93 $debug>=2 && print "$currfile, $element: PRINT $state\n";
94 if ($state eq "s_usedarray") {
96 } elsif ($state eq "s_used") {
97 # A value is declared and used but the hf_ array
99 print "ERROR: NO ARRAY: $currfile, $element\n";
101 } elsif ($state eq "s_array") {
102 # A value is declared, has an hf_ array entry
103 # but it is never used
104 print "Unused entry: $currfile, $element\n";
105 } elsif ($state eq "s_declared") {
106 # A value is declared but no error value exists.
107 # This doesn't matter as it isn't used either.
108 print "Declared only entry: $currfile, $element\n"
109 } elsif ($state eq "s_unknown") {
110 print "UNKNOWN: $currfile, $element\n";
112 die "Impossible: State $state for $currfile, $element\n";
118 if ($currfile !~ /$ARGV/) {
120 # New file - reset array and state
123 %skip = ( "hf_register_info" => 1 );
124 $state = "s_unknown";
126 # opening then closing comment
127 if (/(.*?)\/\*.*\*\/(.*)/) {
130 # closing then opening comment
131 } elsif (/.*?\*\/(.*?)\/\*/) {
135 } elsif (/(.*?)\/\*/) {
139 } elsif (/\*\/(.*?)/) {
142 } elsif ($comment == 1) {
145 # unhandled: more than one complete comment per line
155 if (/static\s+.*int\s+(hf_\w*)\s*=\s*-1\s*;/) {
157 $debug && print "t_declaration for $element$D\n";
158 $type = "t_declaration";
159 # ignore: declarations without any use are detected by the compiler
161 # Skip function parameter declarations with hf_ names
162 } 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(.*)/) {
165 $debug && print "Setting skip for $element$D\n";
167 # Handle functions with multiple hf_ parameters
168 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(.*)/) {
171 $debug && print "Setting skip for $element$D\n";
175 } elsif ($brace == 1 && /^\s*&\s*(hf_\w*)\W+/) {
177 $debug && print "t_array1 for $element$D\n";
179 } elsif (/^\s*\{\s*?&\s*?(hf_\w*)\W+/) {
181 $debug && print "t_array2 for $element$D\n";
183 # Order matters: catch all remaining hf_ lines
184 } elsif (/\w*\s*=\s*(hf_\w*)\s*;/) {
185 # Catches: variable = hf_*
186 # But we don't check if the variable actually gets used...
188 $debug && print "in variable usage for $element$D\n";
189 next if ($skip{$element});
190 $debug && print "set t_usage for $element$D\n";
192 } elsif (/\W(hf_\w*)\W*/) {
194 $debug && print "in usage for $element$D\n";
195 next if ($skip{$element});
196 $debug && print "set t_usage for $element$D\n";
204 $debug && print "else: $D\n";
210 if (!defined($elements{$element})) {
211 $state = "s_unknown";
213 $state = $elements{$element};
216 # current state + input ==> new state
217 # we currently ignore t_declaration
218 if ($state eq "s_error") {
220 } elsif ($state eq "s_unknown" && $type eq "t_usage") {
221 $newstate = "s_used";
222 } elsif ($state eq "s_unknown" && $type eq "t_array") {
223 $newstate = "s_array";
224 } elsif ($state eq "s_used" && $type eq "t_array") {
225 $newstate = "s_usedarray";
226 } elsif ($state eq "s_array" && $type eq "t_usage") {
227 $newstate = "s_usedarray";
231 $elements{$element} = $newstate;
232 $debug>=2 && print "$currfile, $element: SET $state + $type => $newstate$D\n";