1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | # use diagnostics; |
---|
6 | |
---|
7 | # my $debug_verboose; |
---|
8 | |
---|
9 | # BEGIN { |
---|
10 | # $debug_verboose = 1; |
---|
11 | # $SIG{__DIE__} = sub { |
---|
12 | # require Carp; |
---|
13 | # if ($debug_verboose>0) { Carp::confess(@_); } # with backtrace |
---|
14 | # else { Carp::croak(@_); } |
---|
15 | # } |
---|
16 | # } |
---|
17 | |
---|
18 | |
---|
19 | # -------------------------------------------------------------------------------- |
---|
20 | |
---|
21 | my $ARBHOME = $ENV{ARBHOME}; |
---|
22 | if (not defined $ARBHOME) { die "Environmentvariable ARBHOME has be defined"; } |
---|
23 | if (not -d $ARBHOME) { die "ARBHOME ('$ARBHOME') does not point to a valid directory"; } |
---|
24 | |
---|
25 | # -------------------------------------------------------------------------------- |
---|
26 | |
---|
27 | my @pictures = (); # contains all .fig |
---|
28 | my @pixmaps = (); # contains all .xpm |
---|
29 | my @helpfiles = (); # contains all .help, .pdf, .pdf.gz, .ps, .ps.gz |
---|
30 | |
---|
31 | my %known = (); # contains all files contained in arrays above |
---|
32 | my %unknown = (); # contains all other files found in scanned directories |
---|
33 | |
---|
34 | my %picture = (); # key=subdir/name (as used in code), value=index into @pictures |
---|
35 | my %pixmap = (); # key=subdir/name (as used in code), value=index into @pixmaps |
---|
36 | my %helpfile = (); # key=subdir/name (as used in code), value=index into @helpfiles |
---|
37 | |
---|
38 | my %used = (); # key=file, value=1 -> used in code, value=2 -> used in helpfile |
---|
39 | |
---|
40 | my %full2rel = (); # key=full ressource, value=relative ressource (w/o rootdir) |
---|
41 | my %rel2full = (); # opposite |
---|
42 | |
---|
43 | # -------------------------------------------------------------------------------- |
---|
44 | |
---|
45 | sub scanFiles(\@$$$$); |
---|
46 | sub scanFiles(\@$$$$) { |
---|
47 | my ($files_r,$dir,$mask,$recurse,$ignoreLinks) = @_; |
---|
48 | |
---|
49 | my $reg = qr/$mask/; |
---|
50 | |
---|
51 | my @subdirs = (); |
---|
52 | opendir(DIR,$dir) || die "can't read directory '$dir' (Reason: $!)"; |
---|
53 | foreach (readdir(DIR)) { |
---|
54 | if ($_ ne '.' and $_ ne '..') { |
---|
55 | my $full = $dir.'/'.$_; |
---|
56 | if (-d $full) { |
---|
57 | if (/unused/o) { |
---|
58 | print "Ignoring ressource directory '$full' (assuming it contains unused things)\n"; |
---|
59 | } |
---|
60 | elsif (not (/^.svn$/o or /^CVS$/o)) { |
---|
61 | push @subdirs, $full; |
---|
62 | } |
---|
63 | } |
---|
64 | else { |
---|
65 | if ($ignoreLinks==0 || not -l $full) { |
---|
66 | if ($_ =~ $reg) { |
---|
67 | push @$files_r, $full; |
---|
68 | $known{$full} = 1; |
---|
69 | if (defined $unknown{$full}) { delete $unknown{$full}; } |
---|
70 | } |
---|
71 | elsif (/^Makefile$/o or /\.pl$/o) { |
---|
72 | ; # ignore |
---|
73 | } |
---|
74 | elsif (not defined $known{$full}) { |
---|
75 | $unknown{$full} = 1; |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | closedir(DIR); |
---|
82 | |
---|
83 | if ($recurse==1) { |
---|
84 | foreach (@subdirs) { |
---|
85 | scanFiles(@$files_r, $_, $mask, $recurse, $ignoreLinks); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | sub scanFilesAndIndex(\%\@$$$$) { |
---|
91 | my ($index_r,$files_r,$dir,$mask,$recurse,$ignoreLinks) = @_; |
---|
92 | scanFiles(@$files_r,$dir,$mask,$recurse,$ignoreLinks); |
---|
93 | |
---|
94 | my $len = length($dir)+1; # plus '/' |
---|
95 | my $count = scalar(@$files_r); |
---|
96 | for (my $c=0; $c<$count; $c++) { |
---|
97 | my $rel = substr($$files_r[$c], $len); |
---|
98 | $$index_r{$rel} = $c; |
---|
99 | # print "full='".$$files_r[$c]."' idx='$c' rel='$rel'\n"; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | sub scanExistingRessources() { |
---|
104 | scanFilesAndIndex(%picture, @pictures, $ARBHOME.'/lib/pictures', '.*\.(fig|vfont)$', 1, 0); |
---|
105 | scanFilesAndIndex(%pixmap, @pixmaps, $ARBHOME.'/lib/pixmaps', '.*\.(xpm)$', 1, 0); |
---|
106 | scanFilesAndIndex(%helpfile, @helpfiles, $ARBHOME.'/HELP_SOURCE/oldhelp', '.*\.(hlp|ps|pdf|ps\.gz|pdf\.gz)$', 1, 0); |
---|
107 | scanFilesAndIndex(%helpfile, @helpfiles, $ARBHOME.'/HELP_SOURCE/genhelp', '.*\.(hlp|ps|pdf|ps\.gz|pdf\.gz)$', 1, 0); |
---|
108 | |
---|
109 | foreach (sort keys %unknown) { |
---|
110 | if (/readme[^\/]*$/i) { ; } # ignore readme files |
---|
111 | elsif (/\/genhelp\/.*(footer|header)$/o) { ; } # ignore files used for help generation |
---|
112 | else { |
---|
113 | print "$_:0: Unhandled file in ressource directory\n"; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | foreach (keys %picture) { my $full = $pictures [$picture{$_}]; $full2rel{$full} = $_; } |
---|
118 | foreach (keys %pixmap) { my $full = $pixmaps [$pixmap{$_}]; $full2rel{$full} = $_; } |
---|
119 | foreach (keys %helpfile) { my $full = $helpfiles[$helpfile{$_}]; $full2rel{$full} = $_; } |
---|
120 | |
---|
121 | foreach (keys %full2rel) { $rel2full{$full2rel{$_}} = $_; } |
---|
122 | } |
---|
123 | |
---|
124 | # -------------------------------------------------------------------------------- |
---|
125 | |
---|
126 | my $reg_parser = qr/([\(\),\\\"\'\;\{]|\/\*|\/\/)/; |
---|
127 | my $reg_parse_dquotes = qr/(\\.|\")/; |
---|
128 | my $reg_parse_squotes = qr/(\\.|\')/; |
---|
129 | my $reg_parse_eoc = qr/\*\//; |
---|
130 | |
---|
131 | sub scanNextToken(\$\@\$); |
---|
132 | sub scanNextToken(\$\@\$) { |
---|
133 | my ($rest_r,$file_r,$lineNr_r) = @_; |
---|
134 | # scans for the next token (tokens are '(', ')', ',', ';' and '{') |
---|
135 | # and returns it together with it's prefix |
---|
136 | # modifies rest_r and lineNr_r |
---|
137 | # if no token is found until EOF, token will be 'undef' |
---|
138 | # reads over comments |
---|
139 | |
---|
140 | my $prefix = ''; |
---|
141 | my $match = undef; |
---|
142 | if ($$rest_r =~ $reg_parser) { |
---|
143 | my ($preTok,$tok) = ($`,$&); |
---|
144 | $$rest_r = $'; |
---|
145 | |
---|
146 | if ($tok eq '(' or $tok eq ')' or $tok eq ',' or $tok eq ';' or $tok eq '{') { # accept wanted tokens |
---|
147 | $prefix .= $preTok; |
---|
148 | $match = $tok; |
---|
149 | } |
---|
150 | elsif ($tok eq '\\') { # skip escaped chars |
---|
151 | $prefix .= $preTok.$tok.substr($$rest_r,0,1); |
---|
152 | $$rest_r = substr($$rest_r,1); |
---|
153 | } |
---|
154 | elsif ($tok eq '/*') { # skip /**/-comments |
---|
155 | $prefix .= $preTok; |
---|
156 | # print "prefix='$prefix' preTok='$preTok' rest_r='$$rest_r'\n"; |
---|
157 | my $found = 0; |
---|
158 | while ($found==0) { |
---|
159 | if ($$rest_r =~ $reg_parse_eoc) { |
---|
160 | # print "\$`='$`' \$&='$&' \$'='$''\n"; |
---|
161 | if (not $& eq '*/') { die "expected to see '*/', parsed '$&' from '$$rest_r' (this is a bug in check_ressources.pl!)"; } |
---|
162 | $$rest_r = $'; |
---|
163 | $found = 1; |
---|
164 | } |
---|
165 | else { |
---|
166 | $$rest_r = $$file_r[$$lineNr_r++]; |
---|
167 | chomp($$rest_r); |
---|
168 | # print "Continue in next line (while searching '*/'): '$$rest_r'\n"; |
---|
169 | if (not defined $$rest_r) { die "Unclosed '/*'"; } |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | elsif ($tok eq '//') { |
---|
174 | $prefix .= $preTok; |
---|
175 | $$rest_r = $$file_r[$$lineNr_r++]; |
---|
176 | chomp($$rest_r); |
---|
177 | # print "Continue in next line (skipping '//'): '$$rest_r'\n"; |
---|
178 | } |
---|
179 | elsif ($tok eq '"') { |
---|
180 | $prefix .= $preTok.$tok; |
---|
181 | my $closed_dquote = 0; |
---|
182 | while ($closed_dquote==0) { |
---|
183 | if ($$rest_r =~ $reg_parse_dquotes) { |
---|
184 | $prefix .= $`.$&; |
---|
185 | $$rest_r = $'; |
---|
186 | if ($& eq '"') { $closed_dquote = 1; } |
---|
187 | } |
---|
188 | else { die "Missing '\"'"; } |
---|
189 | } |
---|
190 | } |
---|
191 | elsif ($tok eq '\'') { |
---|
192 | $prefix .= $preTok.$tok; |
---|
193 | my $closed_squote = 0; |
---|
194 | while ($closed_squote==0) { |
---|
195 | if ($$rest_r =~ $reg_parse_squotes) { |
---|
196 | $prefix .= $`.$&; |
---|
197 | $$rest_r = $'; |
---|
198 | if ($& eq '\'') { $closed_squote = 1; } |
---|
199 | } |
---|
200 | else { die "Missing '\''"; } |
---|
201 | } |
---|
202 | } |
---|
203 | else { |
---|
204 | die "Unknown token '$tok'"; |
---|
205 | } |
---|
206 | |
---|
207 | if (not defined $match) { # continue |
---|
208 | my $nextPrefix; |
---|
209 | ($nextPrefix,$match) = scanNextToken($$rest_r, @$file_r, $$lineNr_r); |
---|
210 | $prefix .= $nextPrefix; |
---|
211 | } |
---|
212 | } |
---|
213 | else { |
---|
214 | $prefix .= $$rest_r; |
---|
215 | $$rest_r = $$file_r[$$lineNr_r++]; |
---|
216 | chomp($$rest_r); |
---|
217 | # print "Continue in next line: '$$rest_r'\n"; |
---|
218 | if (defined $$rest_r) { # not EOF yet |
---|
219 | my $p; |
---|
220 | ($p,$match) = scanNextToken($$rest_r,@$file_r,$$lineNr_r); |
---|
221 | $prefix .= $p; |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | return ($prefix,$match); |
---|
226 | } |
---|
227 | |
---|
228 | sub scanParams($\@$\$) { |
---|
229 | my ($rest,$file_r,$lineNr,$calltype_r) = @_; |
---|
230 | |
---|
231 | $$calltype_r = 0; # no params |
---|
232 | |
---|
233 | my ($prefix,$token); |
---|
234 | my @params = (); |
---|
235 | eval { |
---|
236 | ($prefix,$token) = scanNextToken($rest,@$file_r,$lineNr); |
---|
237 | }; |
---|
238 | if (!$@ and $token eq '(') { |
---|
239 | if (trim($prefix) ne '') { |
---|
240 | # print "Found prefix '$prefix' before potential parameter list - assume it's sth else\n"; |
---|
241 | } |
---|
242 | else { |
---|
243 | my $openParens = 1; |
---|
244 | my $prevPrefix = ''; |
---|
245 | while ($openParens>0) { |
---|
246 | ($prefix,$token) = scanNextToken($rest,@$file_r,$lineNr); |
---|
247 | my $seen_eop = 0; |
---|
248 | |
---|
249 | if (not defined $token) { die "EOF reached while scanning parameter list"; } |
---|
250 | elsif ($token eq ')') { $openParens--; if ($openParens==0) { $seen_eop = 1; } } |
---|
251 | elsif ($token eq '(') { $openParens++; } |
---|
252 | elsif ($token eq ',') { if ($openParens==1) { $seen_eop = 1; } } |
---|
253 | else { die "Unexpected token '$token' (behind '$prefix')"; } |
---|
254 | |
---|
255 | $prevPrefix .= $prefix; |
---|
256 | if ($seen_eop==1) { push @params, $prevPrefix; $prevPrefix = ''; } |
---|
257 | else { $prevPrefix .= $token; } |
---|
258 | } |
---|
259 | |
---|
260 | $$calltype_r = 1; |
---|
261 | eval { |
---|
262 | ($prefix,$token) = scanNextToken($rest,@$file_r,$lineNr); |
---|
263 | }; |
---|
264 | if ($@) { |
---|
265 | @params = (); |
---|
266 | } |
---|
267 | else { |
---|
268 | if ($token eq ';') { $$calltype_r = 2; } # accepted call |
---|
269 | elsif ($token eq '{') { $$calltype_r = 3; } # function def |
---|
270 | elsif ($token eq ')') { $$calltype_r = 2; } # accepted ("othercall(call())") |
---|
271 | elsif ($token eq ',') { $$calltype_r = 2; } # accepted ("othercall(call(),...)") |
---|
272 | else { |
---|
273 | if ($prefix =~ /__ATTR_/o) { |
---|
274 | $$calltype_r = 3; # function def |
---|
275 | } |
---|
276 | else { |
---|
277 | die "unknown token behind call: '$token' (possible call; ignored due to this error; prefix='$prefix')\n"; |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | return @params; |
---|
285 | } |
---|
286 | |
---|
287 | sub trim($) { |
---|
288 | my ($str) = @_; |
---|
289 | $str =~ s/^\s+//g; |
---|
290 | $str =~ s/\s+$//g; |
---|
291 | return $str; |
---|
292 | } |
---|
293 | |
---|
294 | sub isQuoted($); |
---|
295 | sub isQuoted($) { |
---|
296 | my ($str) = @_; |
---|
297 | if ($str =~ /^\"(.*)\"$/o) { return $1; } |
---|
298 | if ($str =~ /^\(\s*AW_CL\s*\)\s*/o) { |
---|
299 | return isQuoted($'); |
---|
300 | } |
---|
301 | if ($str =~ 'AW_POPUP_HELP') { return 'AW_POPUP_HELP'; } |
---|
302 | return undef; |
---|
303 | } |
---|
304 | |
---|
305 | # -------------------------------------------------------------------------------- |
---|
306 | |
---|
307 | sub acceptAll($) { |
---|
308 | my ($res_param) = @_; |
---|
309 | return ($res_param); |
---|
310 | } |
---|
311 | sub isPixmapRef($) { |
---|
312 | my ($res_param) = @_; |
---|
313 | if ($res_param =~ /^#/) { return ($'); } |
---|
314 | return (); |
---|
315 | } |
---|
316 | sub isIconRes($) { |
---|
317 | my ($res_param) = @_; |
---|
318 | my $base = 'icons/'.$res_param; |
---|
319 | return ($base.'.xpm'); |
---|
320 | } |
---|
321 | sub isHelpRef($) { |
---|
322 | my ($res_param) = @_; |
---|
323 | if ($res_param =~ /\.(hlp|ps|pdf)$/o) { return ($res_param); } |
---|
324 | return (); |
---|
325 | } |
---|
326 | |
---|
327 | my @defs = |
---|
328 | ( |
---|
329 | # regexp for function, param numbers, expectInIndex, isRessource, |
---|
330 | [ qr/\b(AW_help_popup)\b/, [ 2 ], \%helpfile, \&isHelpRef, ], |
---|
331 | [ qr/\b(create_button)\b/, [ 2 ], \%pixmap, \&isPixmapRef, ], |
---|
332 | [ qr/\b(create_mode)\b/, [ 1 ], \%pixmap, \&acceptAll, ], |
---|
333 | [ qr/\b(create_mode)\b/, [ 2 ], \%helpfile, \&isHelpRef, ], |
---|
334 | [ qr/\b(create_toggle)\b/, [ -2, -3 ], \%pixmap, \&isPixmapRef, ], |
---|
335 | [ qr/\b(help_text)\b/, [ 1 ], \%helpfile, \&isHelpRef, ], |
---|
336 | [ qr/\b(makeHelpCallback)\b/, [ 1 ], \%helpfile, \&isHelpRef, ], |
---|
337 | [ qr/\b(AWT_create_root)\b/, [ 2 ], \%pixmap, \&isIconRes, ], |
---|
338 | [ qr/\b(insert_help_topic)\b/, [ 3 ], \%helpfile, \&isHelpRef, ], |
---|
339 | [ qr/\b(insert_menu_topic)\b/, [ 2 ], \%pixmap, \&isPixmapRef, ], |
---|
340 | [ qr/\b(insert_menu_topic)\b/, [ 4 ], \%helpfile, \&isHelpRef, ], |
---|
341 | [ qr/\b(insert_mark_topic)\b/, [ 7 ], \%helpfile, \&isHelpRef, ], |
---|
342 | [ qr/\b(GEN_insert_extract_submenu)\b/, [ 6 ], \%helpfile, \&isHelpRef, ], |
---|
343 | [ qr/\b(GEN_insert_mark_submenu)\b/, [ 6 ], \%helpfile, \&isHelpRef, ], |
---|
344 | [ qr/\b(AW_advice)\b/, [ -4 ], \%helpfile, \&isHelpRef, ], |
---|
345 | [ qr/\b(add_help)\b/, [ 1 ], \%helpfile, \&isHelpRef, ], |
---|
346 | [ qr/\b(insert_toggle)\b/, [ 1 ], \%pixmap, \&isPixmapRef, ], |
---|
347 | [ qr/\b(load_xfig)\b/, [ 1 ], \%picture, \&acceptAll, ], |
---|
348 | |
---|
349 | # pseudos (used in comment to mark a ressource as used) |
---|
350 | [ qr/\b(uses_hlp_res)\b/, [ 1, -2, -3, -4, -5 ], \%helpfile, \&isHelpRef, ], |
---|
351 | # [ qr/\b(uses_pic_res)\b/, [ 1, -2, -3, -4, -5 ], \%picture, \&acceptAll, ], |
---|
352 | [ qr/\b(uses_pix_res)\b/, [ 1, -2, -3, -4, -5 ], \%pixmap, \&acceptAll, ], |
---|
353 | ); |
---|
354 | |
---|
355 | # - param numbers is [1..n] or [-1..-n] for optional params |
---|
356 | # - isRessource gets the unquoted potential ressource (without possible '(AW_CL)'-cast) and |
---|
357 | # returns the plain ressource name or undef (if it's definitely no ressource) |
---|
358 | |
---|
359 | my $defs = scalar(@defs); |
---|
360 | my $errors = 0; |
---|
361 | my $LOC = 0; |
---|
362 | my $showSpecialWarnings = 0; |
---|
363 | |
---|
364 | my @ruleMatched = (); |
---|
365 | |
---|
366 | sub scanCodeFile($) { |
---|
367 | my ($file) = @_; |
---|
368 | open(FILE,'<'.$file) || die "can't read '$file' (Reason: $!)"; |
---|
369 | |
---|
370 | my @file = <FILE>; |
---|
371 | my $flines = scalar(@file); |
---|
372 | unshift @file, undef; # line 0 |
---|
373 | |
---|
374 | my $lineNr = 0; |
---|
375 | eval { |
---|
376 | for ($lineNr=1; $lineNr<=$flines; $lineNr++) { |
---|
377 | my $line = $file[$lineNr]; |
---|
378 | # if ($line =~ /kernlin/io) { print "$file:$lineNr: Custom-Search: ".trim($line)."\n"; } # used to test this script |
---|
379 | |
---|
380 | for (my $d=0; $d<$defs; $d++) { |
---|
381 | my $def_r = $defs[$d]; |
---|
382 | my $reg = $$def_r[0]; |
---|
383 | if ($line =~ $reg) { |
---|
384 | my $rest = $'; |
---|
385 | my $match = $&; |
---|
386 | # print "reg='$reg'\n"; |
---|
387 | # print "$file:$lineNr: Match def #$d: $line"; |
---|
388 | |
---|
389 | chomp($rest); |
---|
390 | my $calltype; |
---|
391 | my @params = scanParams($rest,@file,$lineNr+1,$calltype); |
---|
392 | |
---|
393 | if ($calltype==2) { |
---|
394 | my $pnum_r = $$def_r[1]; |
---|
395 | my $pis = scalar(@$pnum_r); |
---|
396 | for (my $pi=0; $pi<$pis; $pi++) { # for all params referencing a ressource |
---|
397 | my $pnum = $$pnum_r[$pi]; |
---|
398 | my $param = $params[$pnum<0 ? -$pnum-1 : $pnum-1]; |
---|
399 | |
---|
400 | if (defined $param) { |
---|
401 | $param = trim($param); |
---|
402 | my $unquoted = isQuoted($param); |
---|
403 | if (defined $unquoted) { |
---|
404 | my $test_r = $$def_r[3]; |
---|
405 | my @unquoted = &$test_r($unquoted); # test if definitely NO ressource |
---|
406 | |
---|
407 | if (scalar(@unquoted)>0) { # possible ressource(s) |
---|
408 | my $idx_r = $$def_r[2]; # ressource index |
---|
409 | my $used = 0; |
---|
410 | UNQUOTED: foreach my $unquoted (@unquoted) { |
---|
411 | my $full_ressource_idx = $$idx_r{$unquoted}; |
---|
412 | if (not defined $full_ressource_idx and $unquoted =~ /\.(ps|pdf)$/o) { |
---|
413 | $unquoted .= '.gz'; # try zipped version |
---|
414 | $full_ressource_idx = $$idx_r{$unquoted}; |
---|
415 | } |
---|
416 | if (defined $full_ressource_idx) { # existing ressource |
---|
417 | my $full_ressource = $rel2full{$unquoted}; |
---|
418 | if (not defined $full_ressource) { die "expected ressource '$unquoted' to be defined"; } |
---|
419 | $used{$full_ressource} = 1; |
---|
420 | $ruleMatched[$d] = 1; |
---|
421 | $used = 1; |
---|
422 | last UNQUOTED; |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | if ($used==0) { |
---|
427 | print "$file:$lineNr: Error: Ressource '".$unquoted[0]."' is missing\n"; |
---|
428 | $errors++; |
---|
429 | } |
---|
430 | } |
---|
431 | } |
---|
432 | else { |
---|
433 | if ($showSpecialWarnings==1) { |
---|
434 | print "$file:$lineNr: Warning: Param '$param' is not an explicit ressource, can't check\n"; |
---|
435 | # print "Params:\n"; foreach (@params) { print "- param='$_'\n"; } |
---|
436 | } |
---|
437 | } |
---|
438 | } |
---|
439 | else { |
---|
440 | if ($pnum>0) { |
---|
441 | print "$file:$lineNr: Warning: Param #$pnum is missing, can't check (maybe param should be optional)\n"; |
---|
442 | } |
---|
443 | } |
---|
444 | } |
---|
445 | } |
---|
446 | else { |
---|
447 | if ($showSpecialWarnings==1 and $calltype!=3) { # don't warn about function definition |
---|
448 | print "$file:$lineNr: Warning: Matched '$match', but wrong calltype (=$calltype)\n"; |
---|
449 | } |
---|
450 | } |
---|
451 | } |
---|
452 | } |
---|
453 | } |
---|
454 | }; |
---|
455 | if ($@) { |
---|
456 | print "$file:$lineNr: Error: $@\n"; |
---|
457 | $errors++; |
---|
458 | # if ($@ =~ /enough/) { die "enough"; } |
---|
459 | } |
---|
460 | close(FILE); |
---|
461 | $LOC += $flines; |
---|
462 | } |
---|
463 | |
---|
464 | sub autouse($) { |
---|
465 | my ($res) = @_; |
---|
466 | if (not defined $known{$res}) { |
---|
467 | print "Warning: Invalid autouse($res) -- unknown ressource\n"; |
---|
468 | } |
---|
469 | else { |
---|
470 | $used{$res} = 1; |
---|
471 | } |
---|
472 | } |
---|
473 | |
---|
474 | sub scanCodeFile_forUnuseds($\$\%) { |
---|
475 | my ($file,$reg_r,$seen_r) = @_; |
---|
476 | open(FILE,'<'.$file) || die "can't read '$file' (Reason: $!)"; |
---|
477 | |
---|
478 | my @file = <FILE>; |
---|
479 | my $flines = scalar(@file); |
---|
480 | unshift @file, undef; # line 0 |
---|
481 | |
---|
482 | my $lineNr = 0; |
---|
483 | for ($lineNr=1; $lineNr<=$flines; $lineNr++) { |
---|
484 | my $line = $file[$lineNr]; |
---|
485 | while ($line =~ $$reg_r) { |
---|
486 | my $res = $&; |
---|
487 | # print "$file:$lineNr: Warning: Checker failed to detect potential usage of ressource '$res'\n"; |
---|
488 | my $seen = $$seen_r{$res}; |
---|
489 | if (defined $seen) { $seen .= ",$file:$lineNr"; } |
---|
490 | else { $seen = "$file:$lineNr"; } |
---|
491 | $$seen_r{$res} = $seen; |
---|
492 | $line = $'; |
---|
493 | } |
---|
494 | } |
---|
495 | close(FILE); |
---|
496 | } |
---|
497 | |
---|
498 | my %helpScanned = (); |
---|
499 | my $newHelpRef = 0; |
---|
500 | |
---|
501 | sub referenceHelp($); |
---|
502 | sub referenceHelp($) { |
---|
503 | my ($referred) = @_; |
---|
504 | |
---|
505 | my $full_ressource_idx = $helpfile{$referred}; |
---|
506 | if (defined $full_ressource_idx) { # existing ressource |
---|
507 | my $full_ressource = $rel2full{$referred}; |
---|
508 | if (not defined $full_ressource) { die "expected ressource '$referred' to be defined"; } |
---|
509 | $used{$full_ressource} = 1; |
---|
510 | $newHelpRef++; |
---|
511 | } |
---|
512 | else { |
---|
513 | if ($referred =~ /\.(pdf|ps)$/) { |
---|
514 | referenceHelp($referred.'.gz'); |
---|
515 | } |
---|
516 | elsif ($referred =~ /\@/) { |
---|
517 | ; # ignore mail addresses |
---|
518 | } |
---|
519 | elsif ($referred =~ /^(http|file):\/\//o) { |
---|
520 | ; # ignore urls |
---|
521 | } |
---|
522 | else { |
---|
523 | die "Ressource '".$referred."' is missing\n"; |
---|
524 | } |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | sub scanHelpFile($) { |
---|
529 | my ($file) = @_; |
---|
530 | if ($file =~ /\.hlp$/o) { |
---|
531 | if (defined $used{$file} and not defined $helpScanned{$file}) { |
---|
532 | open(FILE,'<'.$file) || die "can't read '$file' (Reason: $!)"; |
---|
533 | my @file = <FILE>; |
---|
534 | my $flines = scalar(@file); |
---|
535 | unshift @file, undef; # line 0 |
---|
536 | my $lineNr = 0; |
---|
537 | for ($lineNr=1; $lineNr<=$flines; $lineNr++) { |
---|
538 | eval { |
---|
539 | $_ = $file[$lineNr]; |
---|
540 | if (/#/) { $_ = $`; } # skip comments |
---|
541 | if (/^\s*(SUB|UP)\s+(.*)$/o) { |
---|
542 | referenceHelp($2); |
---|
543 | } |
---|
544 | else { |
---|
545 | while (/LINK{([^\}]*)}/o) { |
---|
546 | my $rest = $'; |
---|
547 | referenceHelp($1); |
---|
548 | $_ = $rest; |
---|
549 | } |
---|
550 | } |
---|
551 | }; |
---|
552 | if ($@) { |
---|
553 | chomp($@); |
---|
554 | print "$file:$lineNr: Error: $@\n"; |
---|
555 | $errors++; |
---|
556 | # if ($@ =~ /enough/) { die "enough"; } |
---|
557 | } |
---|
558 | } |
---|
559 | close(FILE); |
---|
560 | $helpScanned{$file} = 1; |
---|
561 | $LOC += $flines; |
---|
562 | } |
---|
563 | } |
---|
564 | } |
---|
565 | |
---|
566 | |
---|
567 | sub scanCode() { |
---|
568 | my @sources = (); |
---|
569 | { |
---|
570 | my %oldKnown = %known; |
---|
571 | scanFiles(@sources, $ARBHOME, '\.[ch](xx|pp){0,1}$', 1, 1); # destroys %known and %unknown |
---|
572 | print 'Checking '.scalar(@sources)." source files.\n"; |
---|
573 | |
---|
574 | %known = %oldKnown; |
---|
575 | %unknown = (); |
---|
576 | } |
---|
577 | |
---|
578 | for (my $d=0; $d<scalar(@defs); ++$d) { $ruleMatched[$d] = 0; } |
---|
579 | @sources = sort @sources; |
---|
580 | foreach (@sources) { scanCodeFile($_); } |
---|
581 | |
---|
582 | { |
---|
583 | my $intro = 0; |
---|
584 | for (my $d=0; $d<scalar(@defs); ++$d) { |
---|
585 | if ($ruleMatched[$d] == 0) { |
---|
586 | if ($intro==0) { print "Some code-rules never applied:"; $intro = 1; } |
---|
587 | print " $d"; |
---|
588 | } |
---|
589 | } |
---|
590 | if ($intro==1) { print "\n"; } |
---|
591 | } |
---|
592 | |
---|
593 | $newHelpRef = 1; |
---|
594 | while ($newHelpRef>0) { |
---|
595 | $newHelpRef = 0; |
---|
596 | foreach (@helpfiles) { scanHelpFile($_); } |
---|
597 | } |
---|
598 | |
---|
599 | print "Scanned $LOC LOC.\n"; |
---|
600 | |
---|
601 | autouse($ARBHOME.'/HELP_SOURCE/oldhelp/unittest.hlp'); |
---|
602 | |
---|
603 | my %unused = (); |
---|
604 | foreach (sort keys %known) { |
---|
605 | if (not defined $used{$_}) { $unused{$_} = 1; } |
---|
606 | } |
---|
607 | |
---|
608 | my $unused = scalar(keys %unused); |
---|
609 | |
---|
610 | if ($unused>0) { |
---|
611 | print "Detected $unused unused ressources.\nRunning brute force scan..\n"; |
---|
612 | my $reg_unused = ''; |
---|
613 | foreach (keys %unused) { $reg_unused .= '|'.quotemeta($full2rel{$_}); } |
---|
614 | $reg_unused = substr($reg_unused,1); |
---|
615 | print "reg_unused='$reg_unused'\n"; |
---|
616 | $reg_unused = qr/$reg_unused/; |
---|
617 | |
---|
618 | my %seen_unused = (); |
---|
619 | foreach (@sources) { |
---|
620 | scanCodeFile_forUnuseds($_,$reg_unused, %seen_unused); |
---|
621 | } |
---|
622 | |
---|
623 | foreach (sort keys %unused) { |
---|
624 | my $rel = $full2rel{$_}; |
---|
625 | my $seen = $seen_unused{$rel}; |
---|
626 | if (defined $seen) { |
---|
627 | print "$_:0: Warning: Checker failed to detect ressource usage\n"; |
---|
628 | my @seen = split(',',$seen); |
---|
629 | my %seen = map { $_ => 1; } @seen; |
---|
630 | foreach (sort keys %seen) { |
---|
631 | print "$_: Warning: '$rel' possibly used here\n"; |
---|
632 | } |
---|
633 | } |
---|
634 | else { |
---|
635 | print "$_:0: Error: Ressource is most likely unused\n"; |
---|
636 | $errors++; |
---|
637 | } |
---|
638 | } |
---|
639 | } |
---|
640 | } |
---|
641 | |
---|
642 | sub main() { |
---|
643 | print "Checking ARB ressources\n"; |
---|
644 | |
---|
645 | scanExistingRessources(); |
---|
646 | print ' - '.scalar(@pictures)." pictures\n"; |
---|
647 | print ' - '.scalar(@pixmaps)." images\n"; |
---|
648 | print ' - '.scalar(@helpfiles)." helpfiles\n"; |
---|
649 | |
---|
650 | scanCode(); |
---|
651 | if ($errors>0) { die "$errors errors detected by ressource checker\n"; } |
---|
652 | } |
---|
653 | |
---|
654 | main(); |
---|