find-doc-nits revision 1.1.1.1 1 #! /usr/bin/env perl
2 # Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9
10 require 5.10.0;
11 use warnings;
12 use strict;
13 use Pod::Checker;
14 use File::Find;
15 use File::Basename;
16 use File::Spec::Functions;
17 use Getopt::Std;
18 use lib catdir(dirname($0), "perl");
19 use OpenSSL::Util::Pod;
20
21 # Options.
22 our($opt_d);
23 our($opt_h);
24 our($opt_l);
25 our($opt_n);
26 our($opt_p);
27 our($opt_s);
28 our($opt_u);
29 our($opt_c);
30
31 sub help()
32 {
33 print <<EOF;
34 Find small errors (nits) in documentation. Options:
35 -d Detailed list of undocumented (implies -u)
36 -l Print bogus links
37 -n Print nits in POD pages
38 -s Also print missing sections in POD pages (implies -n)
39 -p Warn if non-public name documented (implies -n)
40 -u List undocumented functions
41 -h Print this help message
42 -c List undocumented commands and options
43 EOF
44 exit;
45 }
46
47 my $temp = '/tmp/docnits.txt';
48 my $OUT;
49 my %public;
50
51 my %mandatory_sections =
52 ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
53 1 => [ 'SYNOPSIS', 'OPTIONS' ],
54 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
55 5 => [ ],
56 7 => [ ] );
57
58 # Cross-check functions in the NAME and SYNOPSIS section.
59 sub name_synopsis()
60 {
61 my $id = shift;
62 my $filename = shift;
63 my $contents = shift;
64
65 # Get NAME section and all words in it.
66 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
67 my $tmp = $1;
68 $tmp =~ tr/\n/ /;
69 print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
70 $tmp =~ s/ -.*//g;
71 $tmp =~ s/ */ /g;
72 print "$id missing comma in NAME\n" if $tmp =~ /[^,] /;
73 $tmp =~ s/,//g;
74
75 my $dirname = dirname($filename);
76 my $simplename = basename($filename);
77 $simplename =~ s/.pod$//;
78 my $foundfilename = 0;
79 my %foundfilenames = ();
80 my %names;
81 foreach my $n ( split ' ', $tmp ) {
82 $names{$n} = 1;
83 $foundfilename++ if $n eq $simplename;
84 $foundfilenames{$n} = 1
85 if -f "$dirname/$n.pod" && $n ne $simplename;
86 }
87 print "$id the following exist as other .pod files:\n",
88 join(" ", sort keys %foundfilenames), "\n"
89 if %foundfilenames;
90 print "$id $simplename (filename) missing from NAME section\n"
91 unless $foundfilename;
92 foreach my $n ( keys %names ) {
93 print "$id $n is not public\n"
94 if $opt_p and !defined $public{$n};
95 }
96
97 # Find all functions in SYNOPSIS
98 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
99 my $syn = $1;
100 foreach my $line ( split /\n+/, $syn ) {
101 my $sym;
102 $line =~ s/STACK_OF\([^)]+\)/int/g;
103 $line =~ s/__declspec\([^)]+\)//;
104 if ( $line =~ /env (\S*)=/ ) {
105 # environment variable env NAME=...
106 $sym = $1;
107 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
108 # a callback function pointer: typedef ... (*NAME)(...
109 $sym = $1;
110 } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
111 # a callback function signature: typedef ... NAME(...
112 $sym = $1;
113 } elsif ( $line =~ /typedef.* (\S+);/ ) {
114 # a simple typedef: typedef ... NAME;
115 $sym = $1;
116 } elsif ( $line =~ /enum (\S*) \{/ ) {
117 # an enumeration: enum ... {
118 $sym = $1;
119 } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
120 $sym = $1;
121 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
122 $sym = $1;
123 }
124 else {
125 next;
126 }
127 print "$id $sym missing from NAME section\n"
128 unless defined $names{$sym};
129 $names{$sym} = 2;
130
131 # Do some sanity checks on the prototype.
132 print "$id prototype missing spaces around commas: $line\n"
133 if ( $line =~ /[a-z0-9],[^ ]/ );
134 }
135
136 foreach my $n ( keys %names ) {
137 next if $names{$n} == 2;
138 print "$id $n missing from SYNOPSIS\n";
139 }
140 }
141
142 sub check()
143 {
144 my $filename = shift;
145 my $dirname = basename(dirname($filename));
146
147 my $contents = '';
148 {
149 local $/ = undef;
150 open POD, $filename or die "Couldn't open $filename, $!";
151 $contents = <POD>;
152 close POD;
153 }
154
155 my $id = "${filename}:1:";
156
157 # Find what section this page is in; assume 3.
158 my $section = 3;
159 $section = 1 if $dirname eq 'apps';
160 $section = $1 if ( $contents =~ /=for comment openssl_manual_section:(\d)/);
161
162 &name_synopsis($id, $filename, $contents)
163 unless $contents =~ /=for comment generic/
164 or $section != 3;
165
166 print "$id doesn't start with =pod\n"
167 if $contents !~ /^=pod/;
168 print "$id doesn't end with =cut\n"
169 if $contents !~ /=cut\n$/;
170 print "$id more than one cut line.\n"
171 if $contents =~ /=cut.*=cut/ms;
172 print "$id missing copyright\n"
173 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
174 print "$id copyright not last\n"
175 if $contents =~ /head1 COPYRIGHT.*=head/ms;
176 print "$id head2 in All uppercase\n"
177 if $contents =~ /head2\s+[A-Z ]+\n/;
178 print "$id extra space after head\n"
179 if $contents =~ /=head\d\s\s+/;
180 print "$id period in NAME section\n"
181 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
182 print "$id POD markup in NAME section\n"
183 if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
184 print "$id Duplicate $1 in L<>\n"
185 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
186 print "$id Bad =over $1\n"
187 if $contents =~ /=over([^ ][^24])/;
188 print "$id Possible version style issue\n"
189 if $contents =~ /OpenSSL version [019]/;
190
191 if ( $contents !~ /=for comment multiple includes/ ) {
192 # Look for multiple consecutive openssl #include lines
193 # (non-consecutive lines are okay; see crypto/MD5.pod).
194 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
195 my $count = 0;
196 foreach my $line ( split /\n+/, $1 ) {
197 if ( $line =~ m@include <openssl/@ ) {
198 print "$id has multiple includes\n" if ++$count == 2;
199 } else {
200 $count = 0;
201 }
202 }
203 }
204 }
205
206 open my $OUT, '>', $temp
207 or die "Can't open $temp, $!";
208 podchecker($filename, $OUT);
209 close $OUT;
210 open $OUT, '<', $temp
211 or die "Can't read $temp, $!";
212 while ( <$OUT> ) {
213 next if /\(section\) in.*deprecated/;
214 print;
215 }
216 close $OUT;
217 unlink $temp || warn "Can't remove $temp, $!";
218
219 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
220 # Skip "return values" if not -s
221 next if $_ eq 'RETURN VALUES' and not $opt_s;
222 print "$id: missing $_ head1 section\n"
223 if $contents !~ /^=head1\s+${_}\s*$/m;
224 }
225 }
226
227 my %dups;
228
229 sub parsenum()
230 {
231 my $file = shift;
232 my @apis;
233
234 open my $IN, '<', $file
235 or die "Can't open $file, $!, stopped";
236
237 while ( <$IN> ) {
238 next if /^#/;
239 next if /\bNOEXIST\b/;
240 next if /\bEXPORT_VAR_AS_FUNC\b/;
241 my @fields = split();
242 die "Malformed line $_"
243 if scalar @fields != 2 && scalar @fields != 4;
244 push @apis, $fields[0];
245 }
246
247 close $IN;
248
249 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
250 return sort @apis;
251 }
252
253 sub getdocced()
254 {
255 my $dir = shift;
256 my %return;
257
258 foreach my $pod ( glob("$dir/*.pod") ) {
259 my %podinfo = extract_pod_info($pod);
260 foreach my $n ( @{$podinfo{names}} ) {
261 $return{$n} = $pod;
262 print "# Duplicate $n in $pod and $dups{$n}\n"
263 if defined $dups{$n} && $dups{$n} ne $pod;
264 $dups{$n} = $pod;
265 }
266 }
267
268 return %return;
269 }
270
271 my %docced;
272
273 sub checkmacros()
274 {
275 my $count = 0;
276
277 print "# Checking macros (approximate)\n";
278 foreach my $f ( glob('include/openssl/*.h') ) {
279 # Skip some internals we don't want to document yet.
280 next if $f eq 'include/openssl/asn1.h';
281 next if $f eq 'include/openssl/asn1t.h';
282 next if $f eq 'include/openssl/err.h';
283 open(IN, $f) || die "Can't open $f, $!";
284 while ( <IN> ) {
285 next unless /^#\s*define\s*(\S+)\(/;
286 my $macro = $1;
287 next if $docced{$macro};
288 next if $macro =~ /i2d_/
289 || $macro =~ /d2i_/
290 || $macro =~ /DEPRECATEDIN/
291 || $macro =~ /IMPLEMENT_/
292 || $macro =~ /DECLARE_/;
293 print "$f:$macro\n" if $opt_d;
294 $count++;
295 }
296 close(IN);
297 }
298 print "# Found $count macros missing (not all should be documented)\n"
299 }
300
301 sub printem()
302 {
303 my $libname = shift;
304 my $numfile = shift;
305 my $count = 0;
306
307 foreach my $func ( &parsenum($numfile) ) {
308 next if $docced{$func};
309
310 # Skip ASN1 utilities
311 next if $func =~ /^ASN1_/;
312
313 print "$libname:$func\n" if $opt_d;
314 $count++;
315 }
316 print "# Found $count missing from $numfile\n\n";
317 }
318
319
320 # Collection of links in each POD file.
321 # filename => [ "foo(1)", "bar(3)", ... ]
322 my %link_collection = ();
323 # Collection of names in each POD file.
324 # "name(s)" => filename
325 my %name_collection = ();
326
327 sub collectnames {
328 my $filename = shift;
329 $filename =~ m|man(\d)/|;
330 my $section = $1;
331 my $simplename = basename($filename, ".pod");
332 my $id = "${filename}:1:";
333
334 my $contents = '';
335 {
336 local $/ = undef;
337 open POD, $filename or die "Couldn't open $filename, $!";
338 $contents = <POD>;
339 close POD;
340 }
341
342 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
343 my $tmp = $1;
344 unless (defined $tmp) {
345 print "$id weird name section\n";
346 return;
347 }
348 $tmp =~ tr/\n/ /;
349 $tmp =~ s/-.*//g;
350
351 my @names = map { s/\s+//g; $_ } split(/,/, $tmp);
352 unless (grep { $simplename eq $_ } @names) {
353 print "$id missing $simplename\n";
354 push @names, $simplename;
355 }
356 foreach my $name (@names) {
357 next if $name eq "";
358 my $name_sec = "$name($section)";
359 if (! exists $name_collection{$name_sec}) {
360 $name_collection{$name_sec} = $filename;
361 } else { #elsif ($filename ne $name_collection{$name_sec}) {
362 print "$id $name_sec also in $name_collection{$name_sec}\n";
363 }
364 }
365
366 my @foreign_names =
367 map { map { s/\s+//g; $_ } split(/,/, $_) }
368 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
369 foreach (@foreign_names) {
370 $name_collection{$_} = undef; # It still exists!
371 }
372
373 my @links = $contents =~ /L<
374 # if the link is of the form L<something|name(s)>,
375 # then remove 'something'. Note that 'something'
376 # may contain POD codes as well...
377 (?:(?:[^\|]|<[^>]*>)*\|)?
378 # we're only interested in referenses that have
379 # a one digit section number
380 ([^\/>\(]+\(\d\))
381 /gx;
382 $link_collection{$filename} = [ @links ];
383 }
384
385 sub checklinks {
386 foreach my $filename (sort keys %link_collection) {
387 foreach my $link (@{$link_collection{$filename}}) {
388 print "${filename}:1: reference to non-existing $link\n"
389 unless exists $name_collection{$link};
390 }
391 }
392 }
393
394 sub publicize() {
395 foreach my $name ( &parsenum('util/libcrypto.num') ) {
396 $public{$name} = 1;
397 }
398 foreach my $name ( &parsenum('util/libssl.num') ) {
399 $public{$name} = 1;
400 }
401 foreach my $name ( &parsenum('util/private.num') ) {
402 $public{$name} = 1;
403 }
404 }
405
406 my %skips = (
407 'aes128' => 1,
408 'aes192' => 1,
409 'aes256' => 1,
410 'aria128' => 1,
411 'aria192' => 1,
412 'aria256' => 1,
413 'camellia128' => 1,
414 'camellia192' => 1,
415 'camellia256' => 1,
416 'des' => 1,
417 'des3' => 1,
418 'idea' => 1,
419 '[cipher]' => 1,
420 '[digest]' => 1,
421 );
422
423 sub checkflags() {
424 my $cmd = shift;
425 my %cmdopts;
426 my %docopts;
427 my $ok = 1;
428
429 # Get the list of options in the command.
430 open CFH, "./apps/openssl list --options $cmd|"
431 || die "Can list options for $cmd, $!";
432 while ( <CFH> ) {
433 chop;
434 s/ .$//;
435 $cmdopts{$_} = 1;
436 }
437 close CFH;
438
439 # Get the list of flags from the synopsis
440 open CFH, "<doc/apps/$cmd.pod"
441 || die "Can't open $cmd.pod, $!";
442 while ( <CFH> ) {
443 chop;
444 last if /DESCRIPTION/;
445 next unless /\[B<-([^ >]+)/;
446 $docopts{$1} = 1;
447 }
448 close CFH;
449
450 # See what's in the command not the manpage.
451 my @undocced = ();
452 foreach my $k ( keys %cmdopts ) {
453 push @undocced, $k unless $docopts{$k};
454 }
455 if ( scalar @undocced > 0 ) {
456 $ok = 0;
457 foreach ( @undocced ) {
458 print "doc/apps/$cmd.pod: Missing -$_\n";
459 }
460 }
461
462 # See what's in the command not the manpage.
463 my @unimpl = ();
464 foreach my $k ( keys %docopts ) {
465 push @unimpl, $k unless $cmdopts{$k};
466 }
467 if ( scalar @unimpl > 0 ) {
468 $ok = 0;
469 foreach ( @unimpl ) {
470 next if defined $skips{$_};
471 print "doc/apps/$cmd.pod: Not implemented -$_\n";
472 }
473 }
474
475 return $ok;
476 }
477
478 getopts('cdlnsphu');
479
480 &help() if $opt_h;
481 $opt_n = 1 if $opt_s or $opt_p;
482 $opt_u = 1 if $opt_d;
483
484 die "Need one of -[cdlnspu] flags.\n"
485 unless $opt_c or $opt_l or $opt_n or $opt_u;
486
487 if ( $opt_c ) {
488 my $ok = 1;
489 my @commands = ();
490
491 # Get list of commands.
492 open FH, "./apps/openssl list -1 -commands|"
493 || die "Can't list commands, $!";
494 while ( <FH> ) {
495 chop;
496 push @commands, $_;
497 }
498 close FH;
499
500 # See if each has a manpage.
501 foreach ( @commands ) {
502 next if $_ eq 'help' || $_ eq 'exit';
503 if ( ! -f "doc/apps/$_.pod" ) {
504 print "doc/apps/$_.pod does not exist\n";
505 $ok = 0;
506 } else {
507 $ok = 0 if not &checkflags($_);
508 }
509 }
510
511 # See what help is missing.
512 open FH, "./apps/openssl list --missing-help |"
513 || die "Can't list missing help, $!";
514 while ( <FH> ) {
515 chop;
516 my ($cmd, $flag) = split;
517 print "$cmd has no help for -$flag\n";
518 $ok = 0;
519 }
520 close FH;
521
522 exit 1 if not $ok;
523 }
524
525 if ( $opt_l ) {
526 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
527 collectnames($_);
528 }
529 checklinks();
530 }
531
532 if ( $opt_n ) {
533 &publicize() if $opt_p;
534 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
535 &check($_);
536 }
537 }
538
539 if ( $opt_u ) {
540 my %temp = &getdocced('doc/crypto');
541 foreach ( keys %temp ) {
542 $docced{$_} = $temp{$_};
543 }
544 &printem('crypto', 'util/libcrypto.num');
545 &printem('ssl', 'util/libssl.num');
546 &checkmacros();
547 }
548
549 exit;
550