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