Commit | Line | Data |
---|---|---|
a67b1afa | 1 | #!/usr/bin/perl -w |
f7b649f0 | 2 | |
faeaf995 FC |
3 | BEGIN { |
4 | chdir 't'; | |
5 | unshift @INC, "../lib"; | |
6 | } | |
7 | ||
a67b1afa | 8 | use strict; |
16384ac1 KW |
9 | use warnings; |
10 | use feature 'unicode_strings'; | |
11 | ||
12 | use Carp; | |
de4da25d | 13 | use Config; |
16384ac1 KW |
14 | use Digest; |
15 | use File::Find; | |
16 | use File::Spec; | |
17 | use Scalar::Util; | |
18 | use Text::Tabs; | |
19 | ||
20 | BEGIN { | |
21 | require '../regen/regen_lib.pl'; | |
22 | } | |
23 | ||
24 | sub DEBUG { 0 }; | |
25 | ||
26 | =pod | |
27 | ||
28 | =head1 NAME | |
29 | ||
30 | podcheck.t - Look for possible problems in the Perl pods | |
31 | ||
32 | =head1 SYNOPSIS | |
33 | ||
34 | cd t | |
d9e2eb4b KW |
35 | ./perl -I../lib porting/podcheck.t [--show_all] [--cpan] [--deltas] |
36 | [--counts] [ FILE ...] | |
477100f8 KW |
37 | ./perl -I../lib porting/podcheck.t --add_link MODULE ... |
38 | ||
16384ac1 KW |
39 | ./perl -I../lib porting/podcheck.t --regen |
40 | ||
41 | =head1 DESCRIPTION | |
42 | ||
43 | podcheck.t is an extension of Pod::Checker. It looks for pod errors and | |
44 | potential errors in the files given as arguments, or if none specified, in all | |
bc20e6b8 KW |
45 | pods in the distribution workspace, except certain known special ones |
46 | (specified below). It does additional checking beyond that done by | |
16384ac1 KW |
47 | Pod::Checker, and keeps a database of known potential problems, and will |
48 | fail a pod only if the number of such problems differs from that given in the | |
49 | database. It also suppresses the C<(section) deprecated> message from | |
50 | Pod::Checker, since specifying the man page section number is quite proper to do. | |
51 | ||
52 | The additional checks it makes are: | |
53 | ||
54 | =over | |
55 | ||
56 | =item Cross-pod link checking | |
57 | ||
58 | Pod::Checker verifies that links to an internal target in a pod are not | |
59 | broken. podcheck.t extends that (when called without FILE arguments) to | |
60 | external links. It does this by gathering up all the possible targets in the | |
477100f8 KW |
61 | workspace, and cross-checking them. It also checks that a non-broken link |
62 | points to just one target. (The destination pod could have two targets with | |
63 | the same name.) | |
64 | ||
65 | The way that the C<LE<lt>E<gt>> pod command works (for links outside the pod) | |
66 | is to actually create a link to C<search.cpan.org> with an embedded query for | |
67 | the desired pod or man page. That means that links outside the distribution | |
68 | are valid. podcheck.t doesn't verify the validity of such links, but instead | |
69 | keeps a data base of those known to be valid. This means that if a link to a | |
70 | target not on the list is created, the target needs to be added to the data | |
71 | base. This is accomplished via the L<--add_link|/--add_link MODULE ...> | |
72 | option to podcheck.t, described below. | |
16384ac1 KW |
73 | |
74 | =item An internal link that isn't so specified | |
75 | ||
76 | If a link is broken, but there is an existing internal target of the same | |
77 | name, it is likely that the internal target was meant, and the C<"/"> is | |
78 | missing from the C<LE<lt>E<gt>> pod command. | |
79 | ||
b879da0f | 80 | =item Verbatim paragraphs that wrap in an 80 (including 1 spare) column window |
16384ac1 KW |
81 | |
82 | It's annoying to have lines wrap when displaying pod documentation in a | |
b879da0f KW |
83 | terminal window. This checks that all verbatim lines fit in a standard 80 |
84 | column window, even when using a pager that reserves a column for its own use. | |
85 | (Thus the check is for a net of 79 columns.) | |
5b1cac40 | 86 | For those lines that don't fit, it tells you how much needs to be cut in |
b879da0f | 87 | order to fit. |
16384ac1 KW |
88 | |
89 | Often, the easiest thing to do to gain space for these is to lower the indent | |
90 | to just one space. | |
91 | ||
92 | =item Missing or duplicate NAME or missing NAME short description | |
93 | ||
94 | A pod can't be linked to unless it has a unique name. | |
95 | And a NAME should have a dash and short description after it. | |
96 | ||
97 | =item =encoding statement issues | |
98 | ||
99 | This indicates if an C<=encoding> statement should be present, or moved to the | |
100 | front of the pod. | |
101 | ||
102 | =item Items that perhaps should be links | |
103 | ||
104 | There are mentions of apparent files in the pods that perhaps should be links | |
105 | instead, using C<LE<lt>...E<gt>> | |
106 | ||
107 | =item Items that perhaps should be C<FE<lt>...E<gt>> | |
108 | ||
109 | What look like path names enclosed in C<CE<lt>...E<gt>> should perhaps have | |
110 | C<FE<lt>...E<gt>> mark-up instead. | |
111 | ||
112 | =back | |
113 | ||
114 | A number of issues raised by podcheck.t and by the base Pod::Checker are not | |
bc20e6b8 KW |
115 | really problems, but merely potential problems, that is, false positives. |
116 | After inspecting them and | |
16384ac1 | 117 | deciding that they aren't real problems, it is possible to shut up this program |
204c2383 KW |
118 | about them, unlike base Pod::Checker. For a valid link to an outside module |
119 | or man page, call podcheck.t with the C<--add_link> option to add it to the | |
120 | the database of known links; for other causes, call podcheck.t with the C<--regen> | |
121 | option to regenerate the entire database. This tells it that all existing | |
16384ac1 KW |
122 | issues are to not be mentioned again. |
123 | ||
204c2383 | 124 | C<--regen> isn't fool-proof. The database merely keeps track of the number of these |
16384ac1 KW |
125 | potential problems of each type for each pod. If a new problem of a given |
126 | type is introduced into the pod, podcheck.t will spit out all of them. You | |
127 | then have to figure out which is the new one, and should it be changed or not. | |
128 | But doing it this way insulates the database from having to keep track of line | |
129 | numbers of problems, which may change, or the exact wording of each problem | |
130 | which might also change without affecting whether it is a problem or not. | |
131 | ||
132 | Also, if the count of potential problems of a given type for a pod decreases, | |
133 | the database must be regenerated so that it knows the new number. The program | |
134 | gives instructions when this happens. | |
135 | ||
bc20e6b8 KW |
136 | Some pods will have varying numbers of problems of a given type. This can |
137 | be handled by manually editing the database file (see L</FILES>), and setting | |
138 | the number of those problems for that pod to a negative number. This will | |
139 | cause the corresponding error to always be suppressed no matter how many there | |
140 | actually are. | |
141 | ||
204c2383 KW |
142 | Another problem is that there is currently no check that modules listed as |
143 | valid in the data base | |
16384ac1 KW |
144 | actually are. Thus any errors introduced there will remain there. |
145 | ||
bc20e6b8 KW |
146 | =head2 Specially handled pods |
147 | ||
148 | =over | |
149 | ||
150 | =item perltoc | |
151 | ||
152 | This pod is generated by pasting bits from other pods. Errors in those bits | |
153 | will show up as errors here, as well as for those other pods. Therefore | |
154 | errors here are suppressed, and the pod is checked only to verify that nodes | |
204c2383 | 155 | within it actually exist that are externally linked to. |
bc20e6b8 KW |
156 | |
157 | =item perldelta | |
158 | ||
159 | The current perldelta pod is initialized from a template that contains | |
160 | placeholder text. Some of this text is in the form of links that don't really | |
161 | exist. Any such links that are listed in C<@perldelta_ignore_links> will not | |
162 | generate messages. It is presumed that these links will be cleaned up when | |
163 | the perldelta is cleaned up for release since they should be marked with | |
164 | C<XXX>. | |
165 | ||
166 | =item Porting/perldelta_template.pod | |
167 | ||
168 | This is not a pod, but a template for C<perldelta>. Any errors introduced | |
169 | here will show up when C<perldelta> is created from it. | |
170 | ||
171 | =item cpan-upstream pods | |
172 | ||
173 | See the L</--cpan> option documentation | |
174 | ||
175 | =item old perldeltas | |
176 | ||
177 | See the L</--deltas> option documentation | |
178 | ||
179 | =back | |
180 | ||
16384ac1 KW |
181 | =head1 OPTIONS |
182 | ||
183 | =over | |
184 | ||
477100f8 KW |
185 | =item --add_link MODULE ... |
186 | ||
187 | Use this option to teach podcheck.t that the C<MODULE>s or man pages actually | |
188 | exist, and to silence any messages that links to them are broken. | |
189 | ||
190 | podcheck.t checks that links within the Perl core distribution are valid, but | |
191 | it doesn't check links to man pages or external modules. When it finds | |
192 | a broken link, it checks its data base of external modules and man pages, | |
193 | and only if not found there does it raise a message. This option just adds | |
194 | the list of modules and man page references that follow it on the command line | |
195 | to that data base. | |
196 | ||
197 | For example, | |
198 | ||
199 | cd t | |
243a655d | 200 | ./perl -I../lib porting/podcheck.t --add_link Unicode::Casing |
477100f8 KW |
201 | |
202 | causes the external module "Unicode::Casing" to be added to the data base, so | |
203 | C<LE<lt>Unicode::Casing<gt>> will be considered valid. | |
204 | ||
16384ac1 KW |
205 | =item --regen |
206 | ||
207 | Regenerate the data base used by podcheck.t to include all the existing | |
208 | potential problems. Future runs of the program will not then flag any of | |
209 | these. | |
210 | ||
211 | =item --cpan | |
212 | ||
213 | Normally, all pods in the cpan directory are skipped, except to make sure that | |
214 | any blead-upstream links to such pods are valid. | |
bc20e6b8 | 215 | This option will cause cpan upstream pods to be fully checked. |
16384ac1 | 216 | |
d9e2eb4b KW |
217 | =item --deltas |
218 | ||
219 | Normally, all old perldelta pods are skipped, except to make sure that | |
220 | any links to such pods are valid. This is because they are considered | |
221 | stable, and perhaps trying to fix them will cause changes that will | |
bc20e6b8 KW |
222 | misrepresent Perl's history. But, this option will cause them to be fully |
223 | checked. | |
d9e2eb4b | 224 | |
16384ac1 KW |
225 | =item --show_all |
226 | ||
227 | Normally, if the number of potential problems of a given type found for a | |
228 | pod matches the expected value in the database, they will not be displayed. | |
229 | This option forces the database to be ignored during the run, so all potential | |
230 | problems are displayed and will fail their respective pod test. Specifying | |
231 | any particular FILES to operate on automatically selects this option. | |
232 | ||
233 | =item --counts | |
234 | ||
235 | Instead of testing, this just dumps the counts of the occurrences of the | |
236 | various types of potential problems in the data base. | |
237 | ||
238 | =back | |
239 | ||
240 | =head1 FILES | |
241 | ||
242 | The database is stored in F<t/porting/known_pod_issues.dat> | |
243 | ||
244 | =head1 SEE ALSO | |
245 | ||
246 | L<Pod::Checker> | |
247 | ||
248 | =cut | |
249 | ||
ae54198b KW |
250 | # VMS builds have a '.com' appended to utility and script names, and it adds a |
251 | # trailing dot for any other file name that doesn't have a dot in it. The db | |
252 | # is stored without those things. This regex allows for these special file | |
253 | # names to be dealt with. It needs to be interpolated into a larger regex | |
254 | # that furnishes the closing boundary. | |
255 | my $vms_re = qr/ \. (?: com )? /x; | |
256 | ||
257 | # Some filenames in the MANIFEST match $vms_re, and so must not be handled the | |
258 | # same way that that the special vms ones are. This hash lists those. | |
259 | my %special_vms_files; | |
260 | ||
21b63541 KW |
261 | # This is to get this to work across multiple file systems, including those |
262 | # that are not case sensitive. The db is stored in lower case, Un*x style, | |
263 | # and all file name comparisons are done that way. | |
264 | sub canonicalize($) { | |
265 | my $input = shift; | |
266 | my ($volume, $directories, $file) | |
267 | = File::Spec->splitpath(File::Spec->canonpath($input)); | |
268 | # Assumes $volume is constant for everything in this directory structure | |
269 | $directories = "" if ! $directories; | |
270 | $file = "" if ! $file; | |
ae54198b KW |
271 | $file = lc join '/', File::Spec->splitdir($directories), $file; |
272 | $file =~ s! / /+ !/!gx; # Multiple slashes => single slash | |
273 | ||
274 | # The db is stored without the special suffixes that are there in VMS, so | |
275 | # strip them off to get the comparable name. But some files on all | |
276 | # platforms have these suffixes, so this shouldn't happen for them, as any | |
277 | # of their db entries will have the suffixes in them. The hash has been | |
278 | # populated with these files. | |
279 | if ($^O eq 'VMS' | |
280 | && $file =~ / ( $vms_re ) $ /x | |
281 | && ! exists $special_vms_files{$file}) | |
282 | { | |
283 | $file =~ s/ $1 $ //x; | |
284 | } | |
285 | return $file; | |
21b63541 KW |
286 | } |
287 | ||
16384ac1 KW |
288 | ##################################################### |
289 | # HOW IT WORKS (in general) | |
290 | # | |
291 | # If not called with specific files to check, the directory structure is | |
292 | # examined for files that have pods in them. Files that might not have to be | |
293 | # fully parsed (e.g. in cpan) are parsed enough at this time to find their | |
294 | # pod's NAME, and to get a checksum. | |
295 | # | |
296 | # Those kinds of files are sorted last, but otherwise the pods are parsed with | |
297 | # the package coded here, My::Pod::Checker, which is an extension to | |
298 | # Pod::Checker that adds some tests and suppresses others that aren't | |
299 | # appropriate. The latter module has no provision for capturing diagnostics, | |
300 | # so a package, Tie_Array_to_FH, is used to force them to be placed into an | |
301 | # array instead of printed. | |
302 | # | |
303 | # Parsing the files builds up a list of links. The files are gone through | |
304 | # again, doing cross-link checking and outputting all saved-up problems with | |
305 | # each pod. | |
306 | # | |
307 | # Sorting the files last that potentially don't need to be fully parsed allows | |
308 | # us to not parse them unless there is a link to an internal anchor in them | |
309 | # from something that we have already parsed. Keeping checksums allows us to | |
310 | # not parse copies of other pods. | |
311 | # | |
312 | ##################################################### | |
313 | ||
314 | # 1 => Exclude low priority messages that aren't likely to be problems, and | |
315 | # has many false positives; higher numbers give more messages. | |
316 | my $Warnings_Level = 200; | |
317 | ||
b9b741c8 KW |
318 | # perldelta during construction may have place holder links. N.B. This |
319 | # variable is referred to by name in release_managers_guide.pod | |
2feebde0 | 320 | our @perldelta_ignore_links = ( "XXX", "perl5YYYdelta", "perldiag/message" ); |
1c01047d | 321 | |
16384ac1 KW |
322 | # To see if two pods with the same NAME are actually copies of the same pod, |
323 | # which is not an error, it uses a checksum to save work. | |
324 | my $digest_type = "SHA-1"; | |
325 | ||
326 | my $original_dir = File::Spec->rel2abs(File::Spec->curdir); | |
327 | my $data_dir = File::Spec->catdir($original_dir, 'porting'); | |
328 | my $known_issues = File::Spec->catfile($data_dir, 'known_pod_issues.dat'); | |
c3e757a4 | 329 | my $MANIFEST = File::Spec->catfile(File::Spec->updir($original_dir), 'MANIFEST'); |
16384ac1 KW |
330 | my $copy_fh; |
331 | ||
b879da0f | 332 | my $MAX_LINE_LENGTH = 79; # 79 columns |
46b134f4 | 333 | my $INDENT = 7; # default nroff indent |
16384ac1 KW |
334 | |
335 | # Our warning messages. Better not have [('"] in them, as those are used as | |
336 | # delimiters for variable parts of the messages by poderror. | |
337 | my $line_length = "Verbatim line length including indents exceeds $MAX_LINE_LENGTH by"; | |
338 | my $broken_link = "Apparent broken link"; | |
339 | my $broken_internal_link = "Apparent internal link is missing its forward slash"; | |
340 | my $see_not_linked = "? Should you be using L<...> instead of"; | |
341 | my $C_with_slash = "? Should you be using F<...> or maybe L<...> instead of"; | |
342 | my $multiple_targets = "There is more than one target"; | |
343 | my $duplicate_name = "Pod NAME already used"; | |
344 | my $need_encoding = "Should have =encoding statement because have non-ASCII"; | |
345 | my $encoding_first = "=encoding must be first command (if present)"; | |
346 | my $no_name = "There is no NAME"; | |
347 | my $missing_name_description = "The NAME should have a dash and short description after it"; | |
348 | ||
7f8d58fb | 349 | # objects, tests, etc can't be pods, so don't look for them. Also skip |
a71e7b2c KW |
350 | # files output by the patch program. Could also ignore most of .gitignore |
351 | # files, but not all, so don't. | |
de4da25d CB |
352 | |
353 | my $obj_ext = $Config{'obj_ext'}; $obj_ext =~ tr/.//d; # dot will be added back | |
354 | my $lib_ext = $Config{'lib_ext'}; $lib_ext =~ tr/.//d; | |
355 | my $lib_so = $Config{'so'}; $lib_so =~ tr/.//d; | |
356 | my $dl_ext = $Config{'dlext'}; $dl_ext =~ tr/.//d; | |
357 | ||
6981f6e4 KW |
358 | # Not really pods, but can look like them. |
359 | my %excluded_files = ( | |
c841a192 | 360 | canonicalize("lib/unicore/mktables") => 1, |
a9df55d5 | 361 | canonicalize("Porting/make-rmg-checklist") => 1, |
c841a192 KW |
362 | canonicalize("Porting/perldelta_template.pod") => 1, |
363 | canonicalize("autodoc.pl") => 1, | |
364 | canonicalize("configpm") => 1, | |
365 | canonicalize("miniperl") => 1, | |
366 | canonicalize("perl") => 1, | |
6981f6e4 KW |
367 | ); |
368 | ||
df80274d KW |
369 | # This list should not include anything for which case sensitivity is |
370 | # important, as it won't work on VMS, and won't show up until tested on VMS. | |
c3e757a4 KW |
371 | # All or almost all such files should be listed in the MANIFEST, so that can |
372 | # be examined for them, and each such file explicitly excluded, as is done for | |
373 | # .PL files in the loop just below this. For files not catchable this way, | |
374 | # is_pod_file() can be used to exclude these at a finer grained level. | |
a71e7b2c | 375 | my $non_pods = qr/ (?: \. |
df80274d | 376 | (?: [achot] | zip | gz | bz2 | jar | tar | tgz |
a71e7b2c KW |
377 | | orig | rej | patch # Patch program output |
378 | | sw[op] | \#.* # Editor droppings | |
a65bcb92 | 379 | | old # buildtoc output |
de4da25d CB |
380 | | xs # pod should be in the .pm file |
381 | | al # autosplit files | |
382 | | bs # bootstrap files | |
383 | | (?i:sh) # shell scripts, hints, templates | |
384 | | lst # assorted listing files | |
385 | | bat # Windows,Netware,OS2 batch files | |
386 | | cmd # Windows,Netware,OS2 command files | |
387 | | lis # VMS compiler listings | |
388 | | map # VMS linker maps | |
389 | | opt # VMS linker options files | |
390 | | mms # MM(K|S) description files | |
391 | | ts # timestamp files generated during build | |
392 | | $obj_ext # object files | |
393 | | exe # $Config{'exe_ext'} might be empty string | |
394 | | $lib_ext # object libraries | |
395 | | $lib_so # shared libraries | |
396 | | $dl_ext # dynamic libraries | |
a71e7b2c KW |
397 | ) |
398 | $ | |
e0a28d18 | 399 | ) | ~$ | \ \(Autosaved\)\.txt$ # Other editor droppings |
de4da25d CB |
400 | | ^cxx\$demangler_db\.$ # VMS name mangler database |
401 | | ^typemap\.?$ # typemap files | |
402 | | ^(?i:Makefile\.PL)$ | |
a71e7b2c | 403 | /x; |
16384ac1 | 404 | |
c3e757a4 KW |
405 | # '.PL' files should be excluded, as they aren't final pods, but often contain |
406 | # material used in generating pods, and so can look like a pod. We can't use | |
407 | # the regexp above because case sensisitivity is important for these, as some | |
408 | # '.pl' files should be examined for pods. Instead look through the MANIFEST | |
409 | # for .PL files and get their full path names, so we can exclude each such | |
410 | # file explicitly. This works because other porting tests prohibit having two | |
411 | # files with the same names except for case. | |
412 | open my $manifest_fh, '<:bytes', $MANIFEST or die "Can't open $MANIFEST"; | |
413 | while (<$manifest_fh>) { | |
ae54198b KW |
414 | |
415 | # While we have MANIFEST open, on VMS platforms, look for files that match | |
416 | # the magic VMS file names that have to be handled specially. Add these | |
417 | # to the list of them. | |
418 | if ($^O eq 'VMS' && / ^ ( [^\t]* $vms_re ) \t /x) { | |
419 | $special_vms_files{$1} = 1; | |
420 | } | |
c3e757a4 KW |
421 | if (/ ^ ( [^\t]* \. PL ) \t /x) { |
422 | $excluded_files{canonicalize($1)} = 1; | |
423 | } | |
424 | } | |
425 | close $manifest_fh, or die "Can't close $MANIFEST"; | |
426 | ||
16384ac1 KW |
427 | |
428 | # Pod::Checker messages to suppress | |
429 | my @suppressed_messages = ( | |
430 | "(section) in", # Checker is wrong to flag this | |
431 | "multiple occurrence of link target", # We catch independently the ones | |
432 | # that are real problems. | |
433 | "unescaped <>", | |
19f4a855 | 434 | "Entity number out of range", # Checker outputs this for anything above |
8505a15d | 435 | # 255, but in fact all Unicode is valid |
16384ac1 KW |
436 | ); |
437 | ||
438 | sub suppressed { | |
439 | # Returns bool as to if input message is one that is to be suppressed | |
440 | ||
441 | my $message = shift; | |
442 | return grep { $message =~ /^\Q$_/i } @suppressed_messages; | |
443 | } | |
444 | ||
445 | { # Closure to contain a simple subset of test.pl. This is to get rid of the | |
446 | # unnecessary 'failed at' messages that would otherwise be output pointing | |
447 | # to a particular line in this file. | |
a67b1afa | 448 | |
16384ac1 KW |
449 | my $current_test = 0; |
450 | my $planned; | |
451 | ||
452 | sub plan { | |
453 | my %plan = @_; | |
454 | $planned = $plan{tests}; | |
455 | print "1..$planned\n"; | |
456 | return; | |
457 | } | |
458 | ||
459 | sub ok { | |
460 | my $success = shift; | |
461 | my $message = shift; | |
462 | ||
463 | chomp $message; | |
464 | ||
465 | $current_test++; | |
466 | print "not " unless $success; | |
467 | print "ok $current_test - $message\n"; | |
468 | return; | |
469 | } | |
470 | ||
471 | sub skip { | |
472 | my $why = shift; | |
473 | my $n = @_ ? shift : 1; | |
474 | for (1..$n) { | |
475 | $current_test++; | |
476 | print "ok $current_test # skip $why\n"; | |
477 | } | |
478 | no warnings 'exiting'; | |
479 | last SKIP; | |
480 | } | |
481 | ||
482 | sub note { | |
483 | my $message = shift; | |
484 | ||
485 | chomp $message; | |
486 | ||
487 | print $message =~ s/^/# /mgr; | |
488 | print "\n"; | |
489 | return; | |
490 | } | |
491 | ||
492 | END { | |
493 | if ($planned && $planned != $current_test) { | |
494 | print STDERR | |
495 | "# Looks like you planned $planned tests but ran $current_test.\n"; | |
496 | } | |
497 | } | |
498 | } | |
499 | ||
16384ac1 KW |
500 | # List of known potential problems by pod and type. |
501 | my %known_problems; | |
502 | ||
503 | # Pods given by the keys contain an interior node that is referred to from | |
504 | # outside it. | |
505 | my %has_referred_to_node; | |
506 | ||
507 | my $show_counts = 0; | |
508 | my $regen = 0; | |
477100f8 | 509 | my $add_link = 0; |
16384ac1 KW |
510 | my $show_all = 0; |
511 | ||
d9e2eb4b KW |
512 | my $do_upstream_cpan = 0; # Assume that are to skip anything in /cpan |
513 | my $do_deltas = 0; # And stable perldeltas | |
16384ac1 KW |
514 | |
515 | while (@ARGV && substr($ARGV[0], 0, 1) eq '-') { | |
516 | my $arg = shift @ARGV; | |
517 | ||
518 | $arg =~ s/^--/-/; # Treat '--' the same as a single '-' | |
519 | if ($arg eq '-regen') { | |
520 | $regen = 1; | |
521 | } | |
477100f8 KW |
522 | elsif ($arg eq '-add_link') { |
523 | $add_link = 1; | |
524 | } | |
16384ac1 KW |
525 | elsif ($arg eq '-cpan') { |
526 | $do_upstream_cpan = 1; | |
527 | } | |
d9e2eb4b KW |
528 | elsif ($arg eq '-deltas') { |
529 | $do_deltas = 1; | |
530 | } | |
16384ac1 KW |
531 | elsif ($arg eq '-show_all') { |
532 | $show_all = 1; | |
533 | } | |
534 | elsif ($arg eq '-counts') { | |
535 | $show_counts = 1; | |
536 | } | |
537 | else { | |
538 | die <<EOF; | |
539 | Unknown option '$arg' | |
540 | ||
477100f8 KW |
541 | Usage: $0 [ --regen | --cpan | --show_all | FILE ... | --add_link MODULE ... ]\n" |
542 | --add_link -> Add the MODULE and man page references to the data base | |
16384ac1 | 543 | --regen -> Regenerate the data file for $0 |
477100f8 | 544 | --cpan -> Include files in the cpan subdirectory. |
d9e2eb4b | 545 | --deltas -> Include stable perldeltas |
16384ac1 KW |
546 | --show_all -> Show all known potential problems |
547 | --counts -> Don't test, but give summary counts of the currently | |
548 | existing database | |
549 | EOF | |
550 | } | |
551 | } | |
552 | ||
553 | my @files = @ARGV; | |
554 | ||
d9e2eb4b KW |
555 | my $cpan_or_deltas = $do_upstream_cpan || $do_deltas; |
556 | if (($regen + $show_all + $show_counts + $add_link + $cpan_or_deltas ) > 1) { | |
557 | croak "--regen, --show_all, --counts, and --add_link are mutually exclusive\n and none can be run with --cpan nor --deltas"; | |
16384ac1 KW |
558 | } |
559 | ||
560 | my $has_input_files = @files; | |
561 | ||
d9e2eb4b KW |
562 | if ($has_input_files |
563 | && ($regen || $show_counts || $do_upstream_cpan || $do_deltas)) | |
564 | { | |
565 | croak "--regen, --counts, --deltas, and --cpan can't be used since using specific files"; | |
16384ac1 KW |
566 | } |
567 | ||
477100f8 KW |
568 | if ($add_link && ! $has_input_files) { |
569 | croak "--add_link requires at least one module or man page reference"; | |
570 | } | |
571 | ||
16384ac1 KW |
572 | our %problems; # potential problems found in this run |
573 | ||
574 | package My::Pod::Checker { # Extend Pod::Checker | |
a67b1afa MM |
575 | use parent 'Pod::Checker'; |
576 | ||
16384ac1 KW |
577 | # Uses inside out hash to protect from typos |
578 | # For new fields, remember to add to destructor DESTROY() | |
579 | my %indents; # Stack of indents from =over's in effect for | |
580 | # current line | |
581 | my %current_indent; # Current line's indent | |
582 | my %filename; # The pod is store in this file | |
583 | my %skip; # is SKIP set for this pod | |
584 | my %in_NAME; # true if within NAME section | |
585 | my %in_begin; # true if within =begin section | |
586 | my %linkable_item; # Bool: if the latest =item is linkable. It isn't | |
587 | # for bullet and number lists | |
588 | my %linkable_nodes; # Pod::Checker adds all =items to its node list, | |
589 | # but not all =items are linkable to | |
590 | my %seen_encoding_cmd; # true if have =encoding earlier | |
591 | my %command_count; # Number of commands seen | |
592 | my %seen_pod_cmd; # true if have =pod earlier | |
593 | my %warned_encoding; # true if already have warned about =encoding | |
594 | # problems | |
595 | ||
596 | sub DESTROY { | |
597 | my $addr = Scalar::Util::refaddr $_[0]; | |
598 | delete $command_count{$addr}; | |
599 | delete $current_indent{$addr}; | |
600 | delete $filename{$addr}; | |
601 | delete $in_begin{$addr}; | |
602 | delete $indents{$addr}; | |
603 | delete $in_NAME{$addr}; | |
604 | delete $linkable_item{$addr}; | |
605 | delete $linkable_nodes{$addr}; | |
606 | delete $seen_encoding_cmd{$addr}; | |
607 | delete $seen_pod_cmd{$addr}; | |
608 | delete $skip{$addr}; | |
609 | delete $warned_encoding{$addr}; | |
610 | return; | |
611 | } | |
612 | ||
613 | sub new { | |
614 | my $class = shift; | |
615 | my $filename = shift; | |
616 | ||
617 | my $self = $class->SUPER::new(-quiet => 1, | |
618 | -warnings => $Warnings_Level); | |
619 | my $addr = Scalar::Util::refaddr $self; | |
620 | $command_count{$addr} = 0; | |
621 | $current_indent{$addr} = 0; | |
622 | $filename{$addr} = $filename; | |
623 | $in_begin{$addr} = 0; | |
624 | $in_NAME{$addr} = 0; | |
625 | $linkable_item{$addr} = 0; | |
626 | $seen_encoding_cmd{$addr} = 0; | |
627 | $seen_pod_cmd{$addr} = 0; | |
628 | $warned_encoding{$addr} = 0; | |
629 | return $self; | |
630 | } | |
631 | ||
632 | # re's for messages that Pod::Checker outputs | |
633 | my $location = qr/ \b (?:in|at|on|near) \s+ /xi; | |
634 | my $optional_location = qr/ (?: $location )? /xi; | |
3cb68c65 | 635 | my $line_reference = qr/ [('"]? $optional_location \b line \s+ |
16384ac1 KW |
636 | (?: \d+ | EOF | \Q???\E | - ) |
637 | [)'"]? /xi; | |
638 | ||
639 | sub poderror { # Called to register a potential problem | |
640 | ||
641 | # This adds an extra field to the parent hash, 'parameter'. It is | |
642 | # used to extract the variable parts of a message leaving just the | |
643 | # constant skeleton. This in turn allows the message to be | |
644 | # categorized better, so that it shows up as a single type in our | |
645 | # database, with the specifics of each occurrence not being stored with | |
646 | # it. | |
647 | ||
648 | my $self = shift; | |
649 | my $opts = shift; | |
650 | ||
651 | my $addr = Scalar::Util::refaddr $self; | |
652 | return if $skip{$addr}; | |
653 | ||
654 | # Input can be a string or hash. If a string, parse it to separate | |
655 | # out the line number and convert to a hash for easier further | |
656 | # processing | |
657 | my $message; | |
658 | if (ref $opts ne 'HASH') { | |
659 | $message = join "", $opts, @_; | |
660 | my $line_number; | |
661 | if ($message =~ s/\s*($line_reference)//) { | |
662 | ($line_number = $1) =~ s/\s*$optional_location//; | |
663 | } | |
664 | else { | |
665 | $line_number = '???'; | |
666 | } | |
667 | $opts = { -msg => $message, -line => $line_number }; | |
668 | } else { | |
669 | $message = $opts->{'-msg'}; | |
670 | ||
671 | } | |
672 | ||
673 | $message =~ s/^\d+\s+//; | |
674 | return if main::suppressed($message); | |
675 | ||
676 | $self->SUPER::poderror($opts, @_); | |
677 | ||
678 | $opts->{parameter} = "" unless $opts->{parameter}; | |
679 | ||
680 | # The variable parts of the message tend to be enclosed in '...', | |
681 | # "....", or (...). Extract them and put them in an extra field, | |
682 | # 'parameter'. This is trickier because the matching delimiter to a | |
683 | # '(' is its mirror, and not itself. Text::Balanced could be used | |
684 | # instead. | |
685 | while ($message =~ m/ \s* $optional_location ( [('"] )/xg) { | |
686 | my $delimiter = $1; | |
687 | my $start = $-[0]; | |
688 | $delimiter = ')' if $delimiter eq '('; | |
689 | ||
690 | # If there is no ending delimiter, don't consider it to be a | |
691 | # variable part. Most likely it is a contraction like "Don't" | |
692 | last unless $message =~ m/\G .+? \Q$delimiter/xg; | |
693 | ||
694 | my $length = $+[0] - $start; | |
695 | ||
696 | # Get the part up through the closing delimiter | |
697 | my $special = substr($message, $start, $length); | |
698 | $special =~ s/^\s+//; # No leading whitespace | |
699 | ||
700 | # And add that variable part to the parameter, while removing it | |
701 | # from the message. This isn't a foolproof way of finding the | |
702 | # variable part. For example '(s)' can occur in e.g., | |
703 | # 'paragraph(s)' | |
704 | if ($special ne '(s)') { | |
705 | substr($message, $start, $length) = ""; | |
706 | pos $message = $start; | |
707 | $opts->{-msg} = $message; | |
708 | $opts->{parameter} .= " " if $opts->{parameter}; | |
709 | $opts->{parameter} .= $special; | |
710 | } | |
711 | } | |
712 | ||
713 | # Extract any additional line number given. This is often the | |
714 | # beginning location of something whereas the main line number gives | |
715 | # the ending one. | |
716 | if ($message =~ /( $line_reference )/xi) { | |
717 | my $line_ref = $1; | |
718 | while ($message =~ s/\s*\Q$line_ref//) { | |
719 | $opts->{-msg} = $message; | |
720 | $opts->{parameter} .= " " if $opts->{parameter}; | |
721 | $opts->{parameter} .= $line_ref; | |
722 | } | |
723 | } | |
724 | ||
b3fdb838 | 725 | Carp::carp("Couldn't extract line number from '$message'") if $message =~ /line \d+/; |
16384ac1 KW |
726 | push @{$problems{$filename{$addr}}{$message}}, $opts; |
727 | #push @{$problems{$self->get_filename}{$message}}, $opts; | |
728 | } | |
729 | ||
730 | sub check_encoding { # Does it need an =encoding statement? | |
731 | my ($self, $paragraph, $line_num, $pod_para) = @_; | |
732 | ||
733 | # Do nothing if there is an =encoding in the file, or if the line | |
734 | # doesn't require an =encoding, or have already warned. | |
735 | my $addr = Scalar::Util::refaddr $self; | |
736 | return if $seen_encoding_cmd{$addr} | |
737 | || $warned_encoding{$addr} | |
738 | || $paragraph !~ /\P{ASCII}/; | |
739 | ||
740 | $warned_encoding{$addr} = 1; | |
741 | my ($file, $line) = $pod_para->file_line; | |
742 | $self->poderror({ -line => $line, -file => $file, | |
743 | -msg => $need_encoding | |
744 | }); | |
745 | return; | |
746 | } | |
747 | ||
748 | sub verbatim { | |
749 | my ($self, $paragraph, $line_num, $pod_para) = @_; | |
750 | $self->check_encoding($paragraph, $line_num, $pod_para); | |
751 | ||
752 | $self->SUPER::verbatim($paragraph, $line_num, $pod_para); | |
753 | ||
2592f3b8 DG |
754 | my $addr = Scalar::Util::refaddr $self; |
755 | ||
16384ac1 KW |
756 | # Pick up the name, since the parent class doesn't in verbatim |
757 | # NAMEs; so treat as non-verbatim. The parent class only allows one | |
758 | # paragraph in a NAME section, so if there is an extra blank line, it | |
759 | # will trigger a message, but such a blank line is harmless, so skip | |
760 | # in that case. | |
2592f3b8 | 761 | if ($in_NAME{$addr} && $paragraph =~ /\S/) { |
16384ac1 KW |
762 | $self->textblock($paragraph, $line_num, $pod_para); |
763 | } | |
764 | ||
765 | my @lines = split /^/, $paragraph; | |
766 | for my $i (0 .. @lines - 1) { | |
2592f3b8 DG |
767 | if ( my $encoding = $seen_encoding_cmd{$addr} ) { |
768 | require Encode; | |
769 | $lines[$i] = Encode::decode($encoding, $lines[$i]); | |
770 | } | |
16384ac1 KW |
771 | $lines[$i] =~ s/\s+$//; |
772 | my $indent = $self->get_current_indent; | |
773 | my $exceeds = length(Text::Tabs::expand($lines[$i])) | |
2cd46bfd | 774 | + $indent - $MAX_LINE_LENGTH; |
16384ac1 KW |
775 | next unless $exceeds > 0; |
776 | my ($file, $line) = $pod_para->file_line; | |
777 | $self->poderror({ -line => $line + $i, -file => $file, | |
778 | -msg => $line_length, | |
779 | parameter => "+$exceeds (including " . ($indent - $INDENT) . " from =over's)", | |
780 | }); | |
781 | } | |
782 | } | |
783 | ||
784 | sub textblock { | |
785 | my ($self, $paragraph, $line_num, $pod_para) = @_; | |
786 | $self->check_encoding($paragraph, $line_num, $pod_para); | |
787 | ||
788 | $self->SUPER::textblock($paragraph, $line_num, $pod_para); | |
789 | ||
790 | my ($file, $line) = $pod_para->file_line; | |
791 | my $addr = Scalar::Util::refaddr $self; | |
792 | if ($in_NAME{$addr}) { | |
793 | if (! $self->name) { | |
794 | my $text = $self->interpolate($paragraph, $line_num); | |
795 | if ($text =~ /^\s*(\S+?)\s*$/) { | |
796 | $self->name($1); | |
797 | $self->poderror({ -line => $line, -file => $file, | |
798 | -msg => $missing_name_description, | |
799 | parameter => $1}); | |
800 | } | |
801 | } | |
802 | } | |
803 | $paragraph = join " ", split /^/, $paragraph; | |
804 | ||
805 | # Matches something that looks like a file name, but is enclosed in | |
806 | # C<...> | |
807 | my $C_path_re = qr{ \b ( C< | |
a44ad494 KW |
808 | # exclude various things that have slashes |
809 | # in them but aren't paths | |
810 | (?! | |
811 | (?: (?: s | qr | m) / ) # regexes | |
812 | | \d+/\d+> # probable fractions | |
813 | | OS/2> | |
814 | | Perl/Tk> | |
815 | | origin/blead> | |
816 | | origin/maint | |
817 | | - # File names don't begin with "-" | |
818 | ) | |
819 | [-\w]+ (?: / [-\w]+ )+ (?: \. \w+ )? > ) | |
16384ac1 KW |
820 | }x; |
821 | ||
822 | # If looks like a reference to other documentation by containing the | |
823 | # word 'See' and then a likely pod directive, warn. | |
4e9f7dd4 KW |
824 | while ($paragraph =~ m{ |
825 | ( (?: \w+ \s+ )* ) # The phrase before, if any | |
826 | \b [Ss]ee \s+ | |
827 | ( ( [^L] ) | |
828 | < | |
829 | ( [^<]*? ) # The not < excludes nested C<L<... | |
830 | > | |
831 | ) | |
2ebce8af | 832 | ( \s+ (?: under | in ) \s+ L< )? |
4e9f7dd4 KW |
833 | }xg) { |
834 | my $prefix = $1 // ""; | |
835 | my $construct = $2; # The whole thing, like C<...> | |
836 | my $type = $3; | |
837 | my $interior = $4; | |
838 | my $trailing = $5; # After the whole thing ending in "L<" | |
839 | ||
840 | # If the full phrase is something like, "you might see C<", or | |
841 | # similar, it really isn't a reference to a link. The ones I saw | |
842 | # all had the word "you" in them; and the "you" wasn't the | |
843 | # beginning of a sentence. | |
844 | if ($prefix !~ / \b you \b /x) { | |
2ebce8af | 845 | |
f274fe9f KW |
846 | # Now, find what the module or man page name within the |
847 | # construct would be if it actually has L<> syntax. If it | |
848 | # doesn't have that syntax, will set the module to the entire | |
849 | # interior. | |
850 | $interior =~ m/ ^ | |
851 | (?: [^|]+ \| )? # Optional arbitrary text ending | |
852 | # in "|" | |
853 | ( .+? ) # module, etc. name | |
854 | (?: \/ .+ )? # target within module | |
855 | $ | |
856 | /xs; | |
857 | my $module = $1; | |
858 | if (! defined $trailing # not referring to something in another | |
859 | # section | |
860 | && $interior !~ /$non_pods/ | |
861 | ||
862 | # C<> that look like files have their own message below, so | |
863 | # exclude them | |
864 | && $construct !~ /$C_path_re/g | |
865 | ||
866 | # There can't be spaces (I think) in module names or man | |
867 | # pages | |
868 | && $module !~ / \s /x | |
869 | ||
870 | # F<> that end in eg \.pl are almost certainly ok, as are | |
871 | # those that look like a path with multiple "/" chars | |
872 | && ($type ne "F" | |
873 | || (! -e $interior | |
874 | && $interior !~ /\.\w+$/ | |
875 | && $interior !~ /\/.+\//) | |
876 | ) | |
877 | ) { | |
878 | $self->poderror({ -line => $line, -file => $file, | |
879 | -msg => $see_not_linked, | |
880 | parameter => $construct | |
881 | }); | |
882 | } | |
4e9f7dd4 | 883 | } |
16384ac1 KW |
884 | } |
885 | while ($paragraph =~ m/$C_path_re/g) { | |
886 | my $construct = $1; | |
887 | $self->poderror({ -line => $line, -file => $file, | |
888 | -msg => $C_with_slash, | |
889 | parameter => $construct | |
890 | }); | |
891 | } | |
892 | return; | |
893 | } | |
894 | ||
895 | sub command { | |
896 | my ($self, $cmd, $paragraph, $line_num, $pod_para) = @_; | |
897 | my $addr = Scalar::Util::refaddr $self; | |
898 | if ($cmd eq "pod") { | |
899 | $seen_pod_cmd{$addr}++; | |
900 | } | |
901 | elsif ($cmd eq "encoding") { | |
902 | my ($file, $line) = $pod_para->file_line; | |
2592f3b8 | 903 | $seen_encoding_cmd{$addr} = $paragraph; # for later decoding |
16384ac1 KW |
904 | if ($command_count{$addr} != 1 && $seen_pod_cmd{$addr}) { |
905 | $self->poderror({ -line => $line, -file => $file, | |
906 | -msg => $encoding_first | |
907 | }); | |
908 | } | |
909 | } | |
910 | $self->check_encoding($paragraph, $line_num, $pod_para); | |
911 | ||
912 | # Pod::Check treats all =items as linkable, but the bullet and | |
913 | # numbered lists really aren't. So keep our own list. This has to be | |
914 | # processed before SUPER is called so that the list is started before | |
915 | # the rest of it gets parsed. | |
916 | if ($cmd eq 'item') { # Not linkable if item begins with * or a digit | |
917 | $linkable_item{$addr} = ($paragraph !~ / ^ \s* | |
918 | (?: [*] | |
919 | | \d+ \.? (?: \$ | \s+ ) | |
920 | )/x) | |
921 | ? 1 | |
922 | : 0; | |
a67b1afa | 923 | |
16384ac1 KW |
924 | } |
925 | $self->SUPER::command($cmd, $paragraph, $line_num, $pod_para); | |
926 | ||
927 | $command_count{$addr}++; | |
928 | ||
929 | $in_NAME{$addr} = 0; # Will change to 1 below if necessary | |
930 | $in_begin{$addr} = 0; # ibid | |
931 | if ($cmd eq 'over') { | |
932 | my $text = $self->interpolate($paragraph, $line_num); | |
933 | my $indent = 4; # default | |
934 | $indent = $1 if $text && $text =~ /^\s*(\d+)\s*$/; | |
935 | push @{$indents{$addr}}, $indent; | |
936 | $current_indent{$addr} += $indent; | |
937 | } | |
938 | elsif ($cmd eq 'back') { | |
939 | if (@{$indents{$addr}}) { | |
940 | $current_indent{$addr} -= pop @{$indents{$addr}}; | |
941 | } | |
942 | else { | |
943 | # =back without corresponding =over, but should have | |
944 | # warned already | |
945 | $current_indent{$addr} = 0; | |
946 | } | |
947 | } | |
948 | elsif ($cmd =~ /^head/) { | |
949 | if (! $in_begin{$addr}) { | |
950 | ||
951 | # If a particular formatter, then this command doesn't really | |
952 | # apply | |
953 | $current_indent{$addr} = 0; | |
954 | undef @{$indents{$addr}}; | |
955 | } | |
956 | ||
957 | my $text = $self->interpolate($paragraph, $line_num); | |
958 | $in_NAME{$addr} = 1 if $cmd eq 'head1' | |
959 | && $text && $text =~ /^NAME\b/; | |
960 | } | |
961 | elsif ($cmd eq 'begin') { | |
962 | $in_begin{$addr} = 1; | |
963 | } | |
964 | ||
965 | return; | |
966 | } | |
967 | ||
968 | sub hyperlink { | |
969 | my $self = shift; | |
970 | ||
02987562 KW |
971 | my $page; |
972 | if ($_[0] && ($page = $_[0][1]{'-page'})) { | |
973 | my $node = $_[0][1]{'-node'}; | |
974 | ||
975 | # If the hyperlink is to an interior node of another page, save it | |
976 | # so that we can see if we need to parse normally skipped files. | |
977 | $has_referred_to_node{$page} = 1 if $node; | |
978 | ||
979 | # Ignore certain placeholder links in perldelta. Check if the | |
980 | # link is page-level, and also check if to a node within the page | |
981 | if ($self->name && $self->name eq "perldelta" | |
982 | && ((grep { $page eq $_ } @perldelta_ignore_links) | |
983 | || ($node | |
984 | && (grep { "$page/$node" eq $_ } @perldelta_ignore_links) | |
985 | ))) { | |
986 | return; | |
987 | } | |
988 | } | |
16384ac1 KW |
989 | return $self->SUPER::hyperlink($_[0]); |
990 | } | |
991 | ||
992 | sub node { | |
993 | my $self = shift; | |
994 | my $text = $_[0]; | |
995 | if($text) { | |
996 | $text =~ s/\s+$//s; # strip trailing whitespace | |
997 | $text =~ s/\s+/ /gs; # collapse whitespace | |
998 | my $addr = Scalar::Util::refaddr $self; | |
999 | push(@{$linkable_nodes{$addr}}, $text) if | |
1000 | ! $current_indent{$addr} | |
1001 | || $linkable_item{$addr}; | |
1002 | } | |
1003 | return $self->SUPER::node($_[0]); | |
1004 | } | |
1005 | ||
1006 | sub get_current_indent { | |
1007 | return $INDENT + $current_indent{Scalar::Util::refaddr $_[0]}; | |
1008 | } | |
1009 | ||
1010 | sub get_filename { | |
1011 | return $filename{Scalar::Util::refaddr $_[0]}; | |
1012 | } | |
1013 | ||
1014 | sub linkable_nodes { | |
1015 | my $linkables = $linkable_nodes{Scalar::Util::refaddr $_[0]}; | |
1016 | return undef unless $linkables; | |
1017 | return @$linkables; | |
1018 | } | |
1019 | ||
1020 | sub get_skip { | |
1021 | return $skip{Scalar::Util::refaddr $_[0]} // 0; | |
1022 | } | |
1023 | ||
1024 | sub set_skip { | |
1025 | my $self = shift; | |
1026 | $skip{Scalar::Util::refaddr $self} = shift; | |
1027 | ||
1028 | # If skipping, no need to keep the problems for it | |
1029 | delete $problems{$self->get_filename}; | |
1030 | return; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | package Tie_Array_to_FH { # So printing actually goes to an array | |
1035 | ||
1036 | my %array; | |
1037 | ||
1038 | sub TIEHANDLE { | |
1039 | my $class = shift; | |
1040 | my $array_ref = shift; | |
1041 | ||
1042 | my $self = bless \do{ my $anonymous_scalar }, $class; | |
1043 | $array{Scalar::Util::refaddr $self} = $array_ref; | |
1044 | ||
1045 | return $self; | |
1046 | } | |
1047 | ||
1048 | sub PRINT { | |
a67b1afa | 1049 | my $self = shift; |
16384ac1 KW |
1050 | push @{$array{Scalar::Util::refaddr $self}}, @_; |
1051 | return 1; | |
1052 | } | |
a67b1afa MM |
1053 | } |
1054 | ||
69f6a9a1 | 1055 | |
16384ac1 KW |
1056 | my %filename_to_checker; # Map a filename to it's pod checker object |
1057 | my %id_to_checker; # Map a checksum to it's pod checker object | |
1058 | my %nodes; # key is filename, values are nodes in that file. | |
1059 | my %nodes_first_word; # same, but value is first word of each node | |
1060 | my %valid_modules; # List of modules known to exist outside us. | |
1061 | my %digests; # checksums of files, whose names are the keys | |
1062 | my %filename_to_pod; # Map a filename to its pod NAME | |
1063 | my %files_with_unknown_issues; | |
1064 | my %files_with_fixes; | |
69f6a9a1 | 1065 | |
16384ac1 | 1066 | my $data_fh; |
be39e7f8 | 1067 | open $data_fh, '<:bytes', $known_issues or die "Can't open $known_issues"; |
69f6a9a1 | 1068 | |
16384ac1 KW |
1069 | my %counts; # For --counts param, count of each issue type |
1070 | my %suppressed_files; # Files with at least one issue type to suppress | |
e57d740c KW |
1071 | my $HEADER = <<END; |
1072 | # This file is the data file for $0. | |
1073 | # There are three types of lines. | |
1074 | # Comment lines are white-space only or begin with a '#', like this one. Any | |
1075 | # changes you make to the comment lines will be lost when the file is | |
1076 | # regen'd. | |
1077 | # Lines without tab characters are simply NAMES of pods that the program knows | |
1078 | # will have links to them and the program does not check if those links are | |
1079 | # valid. | |
1080 | # All other lines should have three fields, each separated by a tab. The | |
1081 | # first field is the name of a pod; the second field is an error message | |
1082 | # generated by this program; and the third field is a count of how many | |
1083 | # known instances of that message there are in the pod. -1 means that the | |
1084 | # program can expect any number of this type of message. | |
1085 | END | |
16384ac1 | 1086 | |
e57d740c | 1087 | my @existing_issues; |
477100f8 | 1088 | |
477100f8 | 1089 | |
16384ac1 | 1090 | while (<$data_fh>) { # Read the data base |
69f6a9a1 | 1091 | chomp; |
16384ac1 KW |
1092 | next if /^\s*(?:#|$)/; # Skip comment and empty lines |
1093 | if (/\t/) { | |
1094 | next if $show_all; | |
e57d740c KW |
1095 | if ($add_link) { # The issues are saved and later output unchanged |
1096 | push @existing_issues, $_; | |
1097 | next; | |
1098 | } | |
16384ac1 KW |
1099 | |
1100 | # Keep track of counts of each issue type for each file | |
1101 | my ($filename, $message, $count) = split /\t/; | |
1102 | $known_problems{$filename}{$message} = $count; | |
1103 | ||
1104 | if ($show_counts) { | |
1105 | if ($count < 0) { # -1 means to suppress this issue type | |
1106 | $suppressed_files{$filename} = $filename; | |
1107 | } | |
1108 | else { | |
1109 | $counts{$message} += $count; | |
1110 | } | |
1111 | } | |
1112 | } | |
1113 | else { # Lines without a tab are modules known to be valid | |
1114 | $valid_modules{$_} = 1 | |
1115 | } | |
1116 | } | |
1117 | close $data_fh; | |
1118 | ||
e57d740c KW |
1119 | if ($add_link) { |
1120 | $copy_fh = open_new($known_issues); | |
1121 | ||
1122 | # Check for basic sanity, and add each command line argument | |
1123 | foreach my $module (@files) { | |
1124 | die "\"$module\" does not look like a module or man page" | |
1125 | # Must look like (A or A::B or A::B::C ..., or foo(3C) | |
1126 | if $module !~ /^ (?: \w+ (?: :: \w+ )* | \w+ \( \d \w* \) ) $/x; | |
1127 | $valid_modules{$module} = 1 | |
1128 | } | |
1129 | my_safer_print($copy_fh, $HEADER); | |
1130 | foreach (sort { lc $a cmp lc $b } keys %valid_modules) { | |
1131 | my_safer_print($copy_fh, $_, "\n"); | |
1132 | } | |
1133 | ||
1134 | # The rest of the db file is output unchanged. | |
63717411 | 1135 | my_safer_print($copy_fh, join "\n", @existing_issues, ""); |
e57d740c KW |
1136 | |
1137 | close_and_rename($copy_fh); | |
1138 | exit; | |
1139 | } | |
1140 | ||
16384ac1 KW |
1141 | if ($show_counts) { |
1142 | my $total = 0; | |
1143 | foreach my $message (sort keys %counts) { | |
1144 | $total += $counts{$message}; | |
1145 | note(Text::Tabs::expand("$counts{$message}\t$message")); | |
1146 | } | |
1147 | note("-----\n" . Text::Tabs::expand("$total\tknown potential issues")); | |
1148 | if (%suppressed_files) { | |
1149 | note("\nFiles that have all messages of at least one type suppressed:"); | |
1150 | note(join ",", keys %suppressed_files); | |
1151 | } | |
1152 | exit 0; | |
1153 | } | |
1154 | ||
16384ac1 KW |
1155 | # re to match files that are to be parsed only if there is an internal link |
1156 | # to them. It does not include cpan, as whether those are parsed depends | |
0496e0bb KW |
1157 | # on a switch. Currently, only perltoc and the stable perldelta.pod's |
1158 | # are included. The latter all have characters between 'perl' and | |
1159 | # 'delta'. (Actually the currently developed one matches as well, but | |
1160 | # is a duplicate of perldelta.pod, so can be skipped, so fine for it to | |
1161 | # match this. | |
d9e2eb4b | 1162 | my $only_for_interior_links_re = qr/ ^ pod\/perltoc.pod $ |
0496e0bb | 1163 | /x; |
d9e2eb4b KW |
1164 | unless ($do_deltas) { |
1165 | $only_for_interior_links_re = qr/$only_for_interior_links_re | | |
1166 | \b perl \d+ delta \. pod \b | |
1167 | /x; | |
1168 | } | |
16384ac1 KW |
1169 | |
1170 | { # Closure | |
1171 | my $first_time = 1; | |
1172 | ||
1173 | sub output_thanks ($$$$) { # Called when an issue has been fixed | |
1174 | my $filename = shift; | |
1175 | my $original_count = shift; | |
1176 | my $current_count = shift; | |
1177 | my $message = shift; | |
1178 | ||
1179 | $files_with_fixes{$filename} = 1; | |
1180 | my $return; | |
1181 | my $fixed_count = $original_count - $current_count; | |
1182 | my $a_problem = ($fixed_count == 1) ? "a problem" : "multiple problems"; | |
1183 | my $another_problem = ($fixed_count == 1) ? "another problem" : "another set of problems"; | |
1184 | my $diff; | |
1185 | if ($message) { | |
1186 | $diff = <<EOF; | |
1187 | There were $original_count occurrences (now $current_count) in this pod of type | |
1188 | "$message", | |
1189 | EOF | |
1190 | } else { | |
1191 | $diff = <<EOF; | |
1192 | There are no longer any problems found in this pod! | |
1193 | EOF | |
1194 | } | |
1195 | ||
1196 | if ($first_time) { | |
1197 | $first_time = 0; | |
1198 | $return = <<EOF; | |
1199 | Thanks for fixing $a_problem! | |
1200 | $diff | |
1201 | Now you must teach $0 that this was fixed. | |
1202 | EOF | |
1203 | } | |
1204 | else { | |
1205 | $return = <<EOF | |
1206 | Thanks for fixing $another_problem. | |
1207 | $diff | |
1208 | EOF | |
1209 | } | |
1210 | ||
1211 | return $return; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | sub my_safer_print { # print, with error checking for outputting to db | |
1216 | my ($fh, @lines) = @_; | |
1217 | ||
1218 | if (! print $fh @lines) { | |
1219 | my $save_error = $!; | |
1220 | close($fh); | |
1221 | die "Write failure: $save_error"; | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | sub extract_pod { # Extracts just the pod from a file | |
1226 | my $filename = shift; | |
1227 | ||
1228 | my @pod; | |
1229 | ||
1230 | # Arrange for the output of Pod::Parser to be collected in an array we can | |
1231 | # look at instead of being printed | |
1232 | tie *ALREADY_FH, 'Tie_Array_to_FH', \@pod; | |
be39e7f8 | 1233 | open my $in_fh, '<:bytes', $filename |
763df156 KW |
1234 | |
1235 | # The file should already have been opened once to get here, so if | |
77b8b9ad KW |
1236 | # fails, just die. It's possible that a transitory file containing a |
1237 | # pod would get here, but not bothering to add code for that very | |
1238 | # unlikely event. | |
16384ac1 KW |
1239 | or die "Can't open '$filename': $!\n"; |
1240 | ||
1241 | my $parser = Pod::Parser->new(); | |
1242 | $parser->parse_from_filehandle($in_fh, *ALREADY_FH); | |
1243 | close $in_fh; | |
1244 | ||
1245 | return join "", @pod | |
1246 | } | |
1247 | ||
1248 | my $digest = Digest->new($digest_type); | |
1249 | ||
1250 | sub is_pod_file { | |
c1dcaaab KW |
1251 | # If $_ is a pod file, add it to the lists and do other prep work. |
1252 | ||
e42a86f0 | 1253 | if (-d) { |
16384ac1 KW |
1254 | # Don't look at files in directories that are for tests, nor those |
1255 | # beginning with a dot | |
1256 | if ($_ eq 't' || $_ =~ /^\../) { | |
1257 | $File::Find::prune = 1; | |
1258 | } | |
1259 | return; | |
1260 | } | |
1261 | ||
e42a86f0 KW |
1262 | return unless -r && -s; # Can't check it if can't read it; no need to |
1263 | # check if 0 length | |
1264 | return unless -f || -l; # Weird file types won't be pods | |
1265 | ||
de4da25d CB |
1266 | if ($_ =~ /^\./ # No hidden Unix files |
1267 | || $_ =~ $non_pods) { | |
1268 | note("Not considering $_") if DEBUG; | |
1269 | return; | |
1270 | } | |
1271 | ||
16384ac1 KW |
1272 | my $filename = $File::Find::name; |
1273 | ||
1274 | # Assumes that the path separator is exactly one character. | |
1275 | $filename =~ s/^\..//; | |
1276 | ||
b3fdb838 | 1277 | return if $excluded_files{canonicalize($filename)}; |
16384ac1 | 1278 | |
144a708b NC |
1279 | my $contents = do { |
1280 | local $/; | |
763df156 KW |
1281 | my $candidate; |
1282 | if (! open $candidate, '<:bytes', $_) { | |
1283 | ||
77b8b9ad KW |
1284 | # If a transitory file was found earlier, the open could fail |
1285 | # legitimately and we just skip the file; also skip it if it is a | |
1286 | # broken symbolic link, as it is probably just a build problem; | |
1287 | # certainly not a file that we would want to check the pod of. | |
1288 | # Otherwise fail it here and no reason to process it further. | |
1289 | # (But the test count will be off too) | |
1290 | ok(0, "Can't open '$filename': $!") | |
e42a86f0 | 1291 | if -r $filename && ! -l $filename; |
763df156 KW |
1292 | return; |
1293 | } | |
144a708b NC |
1294 | <$candidate>; |
1295 | }; | |
16384ac1 KW |
1296 | |
1297 | # If the file is a .pm or .pod, having any initial '=' on a line is | |
8505a15d KW |
1298 | # grounds for testing it. Otherwise, require a head1 NAME line to |
1299 | # consider it as a potential pod | |
144a708b NC |
1300 | if ($filename =~ /\.(?:pm|pod)/) { |
1301 | return unless $contents =~ /^=/m; | |
1302 | } else { | |
1303 | return unless $contents =~ /^=head1 +NAME/m; | |
16384ac1 | 1304 | } |
144a708b NC |
1305 | |
1306 | # Here, we know that the file is a pod. Add it to the list of files | |
1307 | # to check and create a checker object for it. | |
1308 | ||
1309 | push @files, $filename; | |
1310 | my $checker = My::Pod::Checker->new($filename); | |
1311 | $filename_to_checker{$filename} = $checker; | |
1312 | ||
1313 | # In order to detect duplicate pods and only analyze them once, we | |
1314 | # compute checksums for the file, so don't have to do an exact | |
1315 | # compare. Note that if the pod is just part of the file, the | |
1316 | # checksums can differ for the same pod. That special case is handled | |
1317 | # later, since if the checksums of the whole file are the same, that | |
1318 | # case won't even come up. We don't need the checksums for files that | |
1319 | # we parse only if there is a link to its interior, but we do need its | |
1320 | # NAME, which is also retrieved in the code below. | |
1321 | ||
1322 | if ($filename =~ / (?: ^(cpan|lib|ext|dist)\/ ) | |
1323 | | $only_for_interior_links_re | |
1324 | /x) { | |
1325 | $digest->add($contents); | |
1326 | $digests{$filename} = $digest->digest; | |
1327 | ||
1328 | # lib files aren't analyzed if they are duplicates of files copied | |
1329 | # there from some other directory. But to determine this, we need | |
1330 | # to know their NAMEs. We might as well find the NAME now while | |
1331 | # the file is open. Similarly, cpan files aren't analyzed unless | |
1332 | # we're analyzing all of them, or this particular file is linked | |
1333 | # to by a file we are analyzing, and thus we will want to verify | |
1334 | # that the target exists in it. We need to know at least the NAME | |
1335 | # to see if it's worth analyzing, or so we can determine if a lib | |
1336 | # file is a copy of a cpan one. | |
1337 | if ($filename =~ m{ (?: ^ (?: cpan | lib ) / ) | |
16384ac1 | 1338 | | $only_for_interior_links_re |
144a708b NC |
1339 | }x) { |
1340 | if ($contents =~ /^=head1 +NAME.*/mg) { | |
1341 | # The NAME is the first non-spaces on the line up to a | |
1342 | # comma, dash or end of line. Otherwise, it's invalid and | |
1343 | # this pod doesn't have a legal name that we're smart | |
1344 | # enough to find currently. But the parser will later | |
1345 | # find it if it thinks there is a legal name, and set the | |
1346 | # name | |
1347 | if ($contents =~ /\G # continue from the line after =head1 | |
1348 | \s* # ignore any empty lines | |
1349 | ^ \s* ( \S+?) \s* (?: [,-] | $ )/mx) { | |
1350 | my $name = $1; | |
1351 | $checker->name($name); | |
1352 | $id_to_checker{$name} = $checker | |
1353 | if $filename =~ m{^cpan/}; | |
16384ac1 KW |
1354 | } |
1355 | } | |
144a708b NC |
1356 | elsif ($filename =~ m{^cpan/}) { |
1357 | $id_to_checker{$digests{$filename}} = $checker; | |
1358 | } | |
16384ac1 KW |
1359 | } |
1360 | } | |
c1dcaaab KW |
1361 | |
1362 | return; | |
16384ac1 KW |
1363 | } # End of is_pod_file() |
1364 | ||
477100f8 KW |
1365 | # Start of real code that isn't processing the command line (except the |
1366 | # db is read in above, as is processing of the --add_link option). | |
16384ac1 KW |
1367 | # Here, @files contains list of files on the command line. If have any of |
1368 | # these, unconditionally test them, and show all the errors, even the known | |
1369 | # ones, and, since not testing other pods, don't do cross-pod link tests. | |
1370 | # (Could add extra code to do cross-pod tests for the ones in the list.) | |
477100f8 | 1371 | |
16384ac1 KW |
1372 | if ($has_input_files) { |
1373 | undef %known_problems; | |
d9e2eb4b KW |
1374 | $do_upstream_cpan = $do_deltas = 1; # In case one of the inputs is one |
1375 | # of these types | |
16384ac1 KW |
1376 | } |
1377 | else { # No input files -- go find all the possibilities. | |
1378 | if ($regen) { | |
1379 | $copy_fh = open_new($known_issues); | |
1380 | note("Regenerating $known_issues, please be patient..."); | |
e57d740c | 1381 | print $copy_fh $HEADER; |
16384ac1 KW |
1382 | } |
1383 | ||
1384 | # Move to the directory above us, but have to adjust @INC to account for | |
1385 | # that. | |
1386 | s{^\.\./lib$}{lib} for @INC; | |
1387 | chdir File::Spec->updir; | |
1388 | ||
1389 | # And look in this directory and all its subdirectories | |
1390 | find( \&is_pod_file, '.'); | |
1391 | ||
1392 | # Add ourselves to the test | |
b3fdb838 | 1393 | push @files, "t/porting/podcheck.t"; |
16384ac1 KW |
1394 | } |
1395 | ||
1396 | # Now we know how many tests there will be. | |
1397 | plan (tests => scalar @files) if ! $regen; | |
1398 | ||
1399 | ||
1400 | # Sort file names so we get consistent results, and to put cpan last, | |
1401 | # preceeded by the ones that we don't generally parse. This is because both | |
1402 | # these classes are generally parsed only if there is a link to the interior | |
1403 | # of them, and we have to parse all others first to guarantee that they don't | |
1404 | # have such a link. 'lib' files come just before these, as some of these are | |
1405 | # duplicates of others. We already have figured this out when gathering the | |
1406 | # data as a special case for all such files, but this, while unnecessary, | |
1407 | # puts the derived file last in the output. 'readme' files come before those, | |
1408 | # as those also could be duplicates of others, which are considered the | |
1409 | # primary ones. These currently aren't figured out when gathering data, so | |
1410 | # are done here. | |
1411 | @files = sort { if ($a =~ /^cpan/) { | |
1412 | return 1 if $b !~ /^cpan/; | |
e08998bf | 1413 | return lc $a cmp lc $b; |
16384ac1 KW |
1414 | } |
1415 | elsif ($b =~ /^cpan/) { | |
1416 | return -1; | |
1417 | } | |
1418 | elsif ($a =~ /$only_for_interior_links_re/) { | |
1419 | return 1 if $b !~ /$only_for_interior_links_re/; | |
e08998bf | 1420 | return lc $a cmp lc $b; |
16384ac1 KW |
1421 | } |
1422 | elsif ($b =~ /$only_for_interior_links_re/) { | |
1423 | return -1; | |
1424 | } | |
1425 | elsif ($a =~ /^lib/) { | |
1426 | return 1 if $b !~ /^lib/; | |
e08998bf | 1427 | return lc $a cmp lc $b; |
16384ac1 KW |
1428 | } |
1429 | elsif ($b =~ /^lib/) { | |
1430 | return -1; | |
1431 | } elsif ($a =~ /\breadme\b/i) { | |
1432 | return 1 if $b !~ /\breadme\b/i; | |
e08998bf | 1433 | return lc $a cmp lc $b; |
16384ac1 KW |
1434 | } |
1435 | elsif ($b =~ /\breadme\b/i) { | |
1436 | return -1; | |
1437 | } | |
1438 | else { | |
1439 | return lc $a cmp lc $b; | |
1440 | } | |
1441 | } | |
1442 | @files; | |
1443 | ||
1444 | # Now go through all the files and parse them | |
1445 | foreach my $filename (@files) { | |
1446 | my $parsed = 0; | |
1447 | note("parsing $filename") if DEBUG; | |
1448 | ||
1449 | # We may have already figured out some things in the process of generating | |
8505a15d | 1450 | # the file list. If so, we have a $checker object already. But if not, |
16384ac1 KW |
1451 | # generate one now. |
1452 | my $checker = $filename_to_checker{$filename}; | |
1453 | if (! $checker) { | |
1454 | $checker = My::Pod::Checker->new($filename); | |
1455 | $filename_to_checker{$filename} = $checker; | |
1456 | } | |
1457 | ||
1458 | # We have set the name in the checker object if there is a possibility | |
1459 | # that no further parsing is necessary, but otherwise do the parsing now. | |
1460 | if (! $checker->name) { | |
1461 | $parsed = 1; | |
1462 | $checker->parse_from_file($filename, undef); | |
1463 | } | |
1464 | ||
1465 | if ($checker->num_errors() < 0) { # Returns negative if not a pod | |
1466 | $checker->set_skip("$filename is not a pod"); | |
1467 | } | |
1468 | else { | |
1469 | ||
1470 | # Here, is a pod. See if it is one that has already been tested, | |
1471 | # or should be tested under another directory. Use either its NAME | |
1472 | # if it has one, or a checksum if not. | |
1473 | my $name = $checker->name; | |
1474 | my $id; | |
1475 | ||
1476 | if ($name) { | |
1477 | $id = $name; | |
1478 | } | |
1479 | else { | |
1480 | my $digest = Digest->new($digest_type); | |
1481 | $digest->add(extract_pod($filename)); | |
1482 | $id = $digest->digest; | |
1483 | } | |
1484 | ||
1485 | # If there is a match for this pod with something that we've already | |
1486 | # processed, don't process it, and output why. | |
1487 | my $prior_checker; | |
1488 | if (defined ($prior_checker = $id_to_checker{$id}) | |
1489 | && $prior_checker != $checker) # Could have defined the checker | |
1490 | # earlier without pursuing it | |
1491 | { | |
1492 | ||
1493 | # If the pods are identical, then it's just a copy, and isn't an | |
1494 | # error. First use the checksums we have already computed to see | |
1495 | # if the entire files are identical, which means that the pods are | |
1496 | # identical too. | |
1497 | my $prior_filename = $prior_checker->get_filename; | |
1498 | my $same = (! $name | |
1499 | || ($digests{$prior_filename} | |
1500 | && $digests{$filename} | |
1501 | && $digests{$prior_filename} eq $digests{$filename})); | |
1502 | ||
1503 | # If they differ, it could be that the files differ for some | |
1504 | # reason, but the pods they contain are identical. Extract the | |
1505 | # pods and do the comparisons on just those. | |
1506 | if (! $same && $name) { | |
1507 | $same = extract_pod($prior_filename) eq extract_pod($filename); | |
1508 | } | |
1509 | ||
1510 | if ($same) { | |
1511 | $checker->set_skip("The pod of $filename is a duplicate of " | |
1512 | . "the pod for $prior_filename"); | |
1513 | } elsif ($prior_filename =~ /\breadme\b/i) { | |
1514 | $checker->set_skip("$prior_filename is a README apparently for $filename"); | |
1515 | } elsif ($filename =~ /\breadme\b/i) { | |
1516 | $checker->set_skip("$filename is a README apparently for $prior_filename"); | |
44b8cd40 KW |
1517 | } elsif (! $do_upstream_cpan |
1518 | && $filename =~ /^cpan/ | |
1519 | && $prior_filename =~ /^cpan/) | |
1520 | { | |
ef906498 | 1521 | $checker->set_skip("CPAN is upstream for $filename"); |
16384ac1 KW |
1522 | } else { # Here have two pods with identical names that differ |
1523 | $prior_checker->poderror( | |
1524 | { -msg => $duplicate_name, | |
1525 | -line => "???", | |
1526 | parameter => "'$filename' also has NAME '$name'" | |
1527 | }); | |
1528 | $checker->poderror( | |
1529 | { -msg => $duplicate_name, | |
1530 | -line => "???", | |
1531 | parameter => "'$prior_filename' also has NAME '$name'" | |
1532 | }); | |
1533 | ||
1534 | # Changing the names helps later. | |
1535 | $prior_checker->name("$name version arbitrarily numbered 1"); | |
1536 | $checker->name("$name version arbitrarily numbered 2"); | |
1537 | } | |
1538 | ||
1539 | # In any event, don't process this pod that has the same name as | |
1540 | # another. | |
1541 | next; | |
1542 | } | |
1543 | ||
1544 | # A unique pod. | |
1545 | $id_to_checker{$id} = $checker; | |
1546 | ||
1547 | my $parsed_for_links = ", but parsed for its interior links"; | |
1548 | if ((! $do_upstream_cpan && $filename =~ /^cpan/) | |
1549 | || $filename =~ $only_for_interior_links_re) | |
1550 | { | |
1551 | if ($filename =~ /^cpan/) { | |
1552 | $checker->set_skip("CPAN is upstream for $filename"); | |
1553 | } | |
d9e2eb4b | 1554 | elsif ($filename =~ /perl\d+delta/ && ! $do_deltas) { |
16384ac1 KW |
1555 | $checker->set_skip("$filename is a stable perldelta"); |
1556 | } | |
0496e0bb KW |
1557 | elsif ($filename =~ /perltoc/) { |
1558 | $checker->set_skip("$filename dependent on component pods"); | |
1559 | } | |
16384ac1 KW |
1560 | else { |
1561 | croak("Unexpected file '$filename' encountered that has parsing for interior-linking only"); | |
1562 | } | |
1563 | ||
1564 | if ($name && $has_referred_to_node{$name}) { | |
1565 | $checker->set_skip($checker->get_skip() . $parsed_for_links); | |
1566 | } | |
1567 | } | |
1568 | ||
1569 | # Need a name in order to process it, because not meaningful | |
1570 | # otherwise, and also can't test links to this without a name. | |
1571 | if (!defined $name) { | |
1572 | $checker->poderror( { -msg => $no_name, | |
1573 | -line => '???' | |
1574 | }); | |
1575 | next; | |
1576 | } | |
1577 | ||
1578 | # For skipped files, just get its NAME | |
1579 | my $skip; | |
1580 | if (($skip = $checker->get_skip()) && $skip !~ /$parsed_for_links/) | |
1581 | { | |
1582 | $checker->node($name) if $name; | |
1583 | } | |
1584 | else { | |
1585 | $checker->parse_from_file($filename, undef) if ! $parsed; | |
1586 | } | |
1587 | ||
1588 | # Go through everything in the file that could be an anchor that | |
1589 | # could be a link target. Count how many there are of the same name. | |
1590 | foreach my $node ($checker->linkable_nodes) { | |
1591 | next if ! $node; # Can be empty is like '=item *' | |
1592 | if (exists $nodes{$name}{$node}) { | |
1593 | $nodes{$name}{$node}++; | |
1594 | } | |
1595 | else { | |
1596 | $nodes{$name}{$node} = 1; | |
1597 | } | |
1598 | ||
1599 | # Experiments have shown that cpan search can figure out the | |
1600 | # target of a link even if the exact wording is incorrect, as long | |
1601 | # as the first word is. This happens frequently in perlfunc.pod, | |
1602 | # where the link will be just to the function, but the target | |
1603 | # entry also includes parameters to the function. | |
1604 | my $first_word = $node; | |
1605 | if ($first_word =~ s/^(\S+)\s+\S.*/$1/) { | |
1606 | $nodes_first_word{$name}{$first_word} = $node; | |
1607 | } | |
1608 | } | |
1609 | $filename_to_pod{$filename} = $name; | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | # Here, all files have been parsed, and all links and link targets are stored. | |
1614 | # Now go through the files again and see which don't have matches. | |
1615 | if (! $has_input_files) { | |
1616 | foreach my $filename (@files) { | |
1617 | next if $filename_to_checker{$filename}->get_skip; | |
1618 | my $checker = $filename_to_checker{$filename}; | |
1619 | foreach my $link ($checker->hyperlink) { | |
1620 | my $linked_to_page = $link->[1]->page; | |
1621 | next unless $linked_to_page; # intra-file checks are handled by std | |
1622 | # Pod::Checker | |
1623 | ||
1624 | # Initialize the potential message. | |
1625 | my %problem = ( -msg => $broken_link, | |
1626 | -line => $link->[0], | |
1627 | parameter => "to \"$linked_to_page\"", | |
1628 | ); | |
1629 | ||
1630 | # See if we have found the linked-to_file in our parse | |
1631 | if (exists $nodes{$linked_to_page}) { | |
1632 | my $node = $link->[1]->node; | |
1633 | ||
1634 | # If link is only to the page-level, already have it | |
1635 | next if ! $node; | |
1636 | ||
1637 | # Transform pod language to what we are expecting | |
1638 | $node =~ s,E<sol>,/,g; | |
1639 | $node =~ s/E<verbar>/|/g; | |
1640 | ||
1641 | # If link is to a node that exists in the file, is ok | |
1642 | if ($nodes{$linked_to_page}{$node}) { | |
1643 | ||
1644 | # But if the page has multiple targets with the same name, | |
1645 | # it's ambiguous which one this should be to. | |
1646 | if ($nodes{$linked_to_page}{$node} > 1) { | |
1647 | $problem{-msg} = $multiple_targets; | |
1648 | $problem{parameter} = "in $linked_to_page that $node could be pointing to"; | |
1649 | $checker->poderror(\%problem); | |
1650 | } | |
1651 | } elsif (! $nodes_first_word{$linked_to_page}{$node}) { | |
1652 | ||
1653 | # Here the link target was not found, either exactly or to | |
1654 | # the first word. Is an error. | |
1655 | $problem{parameter} =~ s,"$,/$node",; | |
1656 | $checker->poderror(\%problem); | |
1657 | } | |
1658 | ||
1659 | } # Linked-to-file not in parse; maybe is in exception list | |
1660 | elsif (! exists $valid_modules{$link->[1]->page}) { | |
1661 | ||
1662 | # Here, is a link to a target that we can't find. Check if | |
1663 | # there is an internal link on the page with the target name. | |
1664 | # If so, it could be that they just forgot the initial '/' | |
1c01047d KW |
1665 | # But perldelta is handled specially: only do this if the |
1666 | # broken link isn't one of the known bad ones (that are | |
1667 | # placemarkers and should be removed for the final) | |
1668 | my $NAME = $filename_to_pod{$filename}; | |
1669 | if (! defined $NAME) { | |
1670 | $checker->poderror(\%problem); | |
1671 | } | |
02987562 | 1672 | else { |
1c01047d KW |
1673 | if ($nodes{$NAME}{$linked_to_page}) { |
1674 | $problem{-msg} = $broken_internal_link; | |
1675 | } | |
1676 | $checker->poderror(\%problem); | |
16384ac1 | 1677 | } |
16384ac1 KW |
1678 | } |
1679 | } | |
1680 | } | |
1681 | } | |
1682 | ||
1683 | # If regenerating the data file, start with the modules for which we don't | |
de37f83f KW |
1684 | # check targets. If you change the sort order, you need to run --regen before |
1685 | # committing so that future commits that do run regen don't show irrelevant | |
1686 | # changes. | |
16384ac1 KW |
1687 | if ($regen) { |
1688 | foreach (sort { lc $a cmp lc $b } keys %valid_modules) { | |
1689 | my_safer_print($copy_fh, $_, "\n"); | |
1690 | } | |
1691 | } | |
1692 | ||
1693 | # Now ready to output the messages. | |
1694 | foreach my $filename (@files) { | |
b3fdb838 | 1695 | my $canonical = canonicalize($filename); |
16384ac1 KW |
1696 | SKIP: { |
1697 | my $skip = $filename_to_checker{$filename}->get_skip // ""; | |
1698 | ||
1699 | if ($regen) { | |
1700 | foreach my $message ( sort keys %{$problems{$filename}}) { | |
1701 | my $count; | |
1702 | ||
1703 | # Preserve a negative setting. | |
b3fdb838 KW |
1704 | if ($known_problems{$canonical}{$message} |
1705 | && $known_problems{$canonical}{$message} < 0) | |
16384ac1 | 1706 | { |
b3fdb838 | 1707 | $count = $known_problems{$canonical}{$message}; |
16384ac1 KW |
1708 | } |
1709 | else { | |
1710 | $count = @{$problems{$filename}{$message}}; | |
1711 | } | |
24df3027 | 1712 | my_safer_print($copy_fh, $canonical . "\t$message\t$count\n"); |
16384ac1 KW |
1713 | } |
1714 | next; | |
1715 | } | |
1716 | ||
1717 | skip($skip, 1) if $skip; | |
1718 | my @diagnostics; | |
1719 | my $indent = ' '; | |
1720 | ||
16384ac1 KW |
1721 | my $total_known = 0; |
1722 | foreach my $message ( sort keys %{$problems{$filename}}) { | |
b3fdb838 KW |
1723 | $known_problems{$canonical}{$message} = 0 |
1724 | if ! $known_problems{$canonical}{$message}; | |
16384ac1 KW |
1725 | my $diagnostic = ""; |
1726 | my $problem_count = scalar @{$problems{$filename}{$message}}; | |
1727 | $total_known += $problem_count; | |
b3fdb838 KW |
1728 | next if $known_problems{$canonical}{$message} < 0; |
1729 | if ($problem_count > $known_problems{$canonical}{$message}) { | |
16384ac1 KW |
1730 | |
1731 | # Here we are about to output all the messages for this type, | |
1732 | # subtract back this number we previously added in. | |
1733 | $total_known -= $problem_count; | |
1734 | ||
1735 | $diagnostic .= $indent . $message; | |
1736 | if ($problem_count > 2) { | |
1737 | $diagnostic .= " ($problem_count occurrences)"; | |
1738 | } | |
1739 | foreach my $problem (@{$problems{$filename}{$message}}) { | |
1740 | $diagnostic .= " " if $problem_count == 1; | |
1741 | $diagnostic .= "\n$indent$indent"; | |
1742 | $diagnostic .= "$problem->{parameter}" if $problem->{parameter}; | |
1743 | $diagnostic .= " near line $problem->{-line}"; | |
1744 | $diagnostic .= " $problem->{comment}" if $problem->{comment}; | |
1745 | } | |
1746 | $diagnostic .= "\n"; | |
1747 | $files_with_unknown_issues{$filename} = 1; | |
b3fdb838 KW |
1748 | } elsif ($problem_count < $known_problems{$canonical}{$message}) { |
1749 | $diagnostic = output_thanks($filename, $known_problems{$canonical}{$message}, $problem_count, $message); | |
16384ac1 KW |
1750 | } |
1751 | push @diagnostics, $diagnostic if $diagnostic; | |
1752 | } | |
1753 | ||
09ea063a KW |
1754 | # The above loop has output messages where there are current potential |
1755 | # issues. But it misses where there were some that have been entirely | |
1756 | # fixed. For those, we need to look through the old issues | |
b3fdb838 | 1757 | foreach my $message ( sort keys %{$known_problems{$canonical}}) { |
09ea063a | 1758 | next if $problems{$filename}{$message}; |
b3fdb838 KW |
1759 | next if ! $known_problems{$canonical}{$message}; |
1760 | next if $known_problems{$canonical}{$message} < 0; # Preserve negs | |
1761 | my $diagnostic = output_thanks($filename, $known_problems{$canonical}{$message}, 0, $message); | |
09ea063a KW |
1762 | push @diagnostics, $diagnostic if $diagnostic; |
1763 | } | |
1764 | ||
16384ac1 KW |
1765 | my $output = "POD of $filename"; |
1766 | $output .= ", excluding $total_known not shown known potential problems" | |
1767 | if $total_known; | |
1768 | ok(@diagnostics == 0, $output); | |
1769 | if (@diagnostics) { | |
1770 | note(join "", @diagnostics, | |
477100f8 | 1771 | "See end of this test output for your options on silencing this"); |
16384ac1 KW |
1772 | } |
1773 | } | |
1774 | } | |
1775 | ||
1776 | my $how_to = <<EOF; | |
1777 | run this test script by hand, using the following formula (on | |
1778 | Un*x-like machines): | |
1779 | cd t | |
1780 | ./perl -I../lib porting/podcheck.t --regen | |
1781 | EOF | |
1782 | ||
1783 | if (%files_with_unknown_issues) { | |
1784 | my $were_count_files = scalar keys %files_with_unknown_issues; | |
1785 | $were_count_files = ($were_count_files == 1) | |
1786 | ? "was $were_count_files file" | |
1787 | : "were $were_count_files files"; | |
1788 | my $message = <<EOF; | |
1789 | ||
1790 | HOW TO GET THIS .t TO PASS | |
1791 | ||
477100f8 | 1792 | There $were_count_files that had new potential problems identified. |
bc20e6b8 | 1793 | Some of them may be real, and some of them may be false positives because |
968d3762 KW |
1794 | this program isn't as smart as it likes to think it is. You can teach this |
1795 | program to ignore the issues it has identified, and hence pass, by doing the | |
477100f8 | 1796 | following: |
16384ac1 | 1797 | |
477100f8 KW |
1798 | 1) If a problem is about a link to an unknown module or man page that |
1799 | you know exists, re-run the command something like: | |
1800 | ./perl -I../lib porting/podcheck.t --add_link MODULE man_page ... | |
1801 | (MODULEs should look like Foo::Bar, and man_pages should look like | |
1802 | bar(3c); don't do this for a module or man page that you aren't sure | |
1803 | about; instead treat as another type of issue and follow the | |
1804 | instructions below.) | |
16384ac1 KW |
1805 | |
1806 | 2) For other issues, decide if each should be fixed now or not. Fix the | |
1807 | ones you decided to, and rerun this test to verify that the fixes | |
1808 | worked. | |
1809 | ||
968d3762 KW |
1810 | 3) If there remain false positive or problems that you don't plan to fix right |
1811 | now, | |
16384ac1 | 1812 | $how_to |
477100f8 KW |
1813 | That should cause all current potential problems to be accepted by |
1814 | the program, so that the next time it runs, they won't be flagged. | |
16384ac1 KW |
1815 | EOF |
1816 | if (%files_with_fixes) { | |
1817 | $message .= " This step will also take care of the files that have fixes in them\n"; | |
1818 | } | |
1819 | ||
1820 | $message .= <<EOF; | |
1821 | For a few files, such as perltoc, certain issues will always be | |
1822 | expected, and more of the same will be added over time. For those, | |
1823 | before you do the regen, you can edit | |
1824 | $known_issues | |
1825 | and find the entry for the module's file and specific error message, | |
1826 | and change the count of known potential problems to -1. | |
1827 | EOF | |
1828 | ||
1829 | note($message); | |
1830 | } elsif (%files_with_fixes) { | |
1831 | note(<<EOF | |
1832 | To teach this test script that the potential problems have been fixed, | |
1833 | $how_to | |
1834 | EOF | |
1835 | ); | |
1836 | } | |
1837 | ||
1838 | if ($regen) { | |
1839 | chdir $original_dir || die "Can't change directories to $original_dir"; | |
1840 | close_and_rename($copy_fh); | |
1841 | } |