]>
git.ipfire.org Git - thirdparty/openssl.git/blob - util/find-doc-nits
2 # Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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
16 use File
::Spec
::Functions
;
18 use lib catdir
(dirname
($0), "perl");
19 use OpenSSL
::Util
::Pod
;
21 my $debug = 0; # Set to 1 for debug output
36 # Print usage message and exit.
39 Find small errors (nits) in documentation. Options:
40 -c List undocumented commands and options
41 -d Detailed list of undocumented (implies -u)
42 -e Detailed list of new undocumented (implies -v)
43 -h Print this help message
45 -n Print nits in POD pages
46 -o Causes -e/-v to count symbols added since 1.1.1 as new (implies -v)
47 -u Count undocumented functions
48 -v Count new undocumented functions
57 $opt_v = 1 if $opt_o || $opt_e;
58 die "Cannot use both -u and -v"
60 die "Cannot use both -d and -e"
63 # We only need to check c, l, n, u and v.
64 # Options d, e, o imply one of the above.
65 die "Need one of -[cdehlnouv] flags.\n"
66 unless $opt_c or $opt_l or $opt_n or $opt_u or $opt_v;
69 my $temp = '/tmp/docnits.txt';
74 my %mandatory_sections =
75 ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
76 1 => [ 'SYNOPSIS', 'OPTIONS' ],
77 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
81 # Print error message, set $status.
83 print join(" ", @_), "\n";
87 # Cross-check functions in the NAME and SYNOPSIS section.
93 # Get NAME section and all words in it.
94 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
97 err
($id, "trailing comma before - in NAME")
100 err
($id, "POD markup among the names in NAME")
103 err
($id, "missing comma in NAME")
106 my $dirname = dirname
($filename);
107 my $simplename = basename
(basename
($filename, ".in"), ".pod");
108 my $foundfilename = 0;
109 my %foundfilenames = ();
111 foreach my $n ( split ',', $tmp ) {
114 err
($id, "the name '$n' contains white-space")
117 $foundfilename++ if $n eq $simplename;
118 $foundfilenames{$n} = 1
119 if ((-f
"$dirname/$n.pod.in" || -f
"$dirname/$n.pod")
120 && $n ne $simplename);
122 err
($id, "the following exist as other .pod or .pod.in files:",
123 sort keys %foundfilenames)
125 err
($id, "$simplename (filename) missing from NAME section")
126 unless $foundfilename;
127 if ( $filename !~ /internal/ ) {
128 foreach my $n ( keys %names ) {
129 err
($id, "$n is not public")
130 if !defined $public{$n};
134 # Find all functions in SYNOPSIS
135 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
137 foreach my $line ( split /\n+/, $syn ) {
138 next unless $line =~ /^\s/;
140 $line =~ s/STACK_OF\([^)]+\)/int/g;
141 $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g;
142 $line =~ s/__declspec\([^)]+\)//;
143 if ( $line =~ /env (\S*)=/ ) {
144 # environment variable env NAME=...
146 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
147 # a callback function pointer: typedef ... (*NAME)(...
149 } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
150 # a callback function signature: typedef ... NAME(...
152 } elsif ( $line =~ /typedef.* (\S+);/ ) {
153 # a simple typedef: typedef ... NAME;
155 } elsif ( $line =~ /enum (\S*) \{/ ) {
156 # an enumeration: enum ... {
158 } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) {
160 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
166 err
($id, "$sym missing from NAME section")
167 unless defined $names{$sym};
170 # Do some sanity checks on the prototype.
171 err
($id, "prototype missing spaces around commas: $line")
172 if ( $line =~ /[a-z0-9],[^ ]/ );
175 foreach my $n ( keys %names ) {
176 next if $names{$n} == 2;
177 err
($id, "$n missing from SYNOPSIS")
181 # Check if SECTION ($3) is located before BEFORE ($4)
182 sub check_section_location
{
184 my $contents = shift;
188 return unless $contents =~ /=head1 $section/
189 and $contents =~ /=head1 $before/;
190 err
($id, "$section should appear before $before section")
191 if $contents =~ /=head1 $before.*=head1 $section/ms;
194 # Check if a =head1 is duplicated, or a =headX is duplicated within a
195 # =head1. Treats =head2 =head3 as equivalent -- it doesn't reset the head3
196 # sets if it finds a =head2 -- but that is good enough for now. Also check
197 # for proper capitalization, trailing periods, etc.
198 sub check_head_style
{
200 my $contents = shift;
204 foreach my $line ( split /\n+/, $contents ) {
205 next unless $line =~ /^=head/;
206 if ( $line =~ /head1/ ) {
207 err
($id, "duplicate section $line")
208 if defined $head1{$line};
212 err
($id, "duplicate subsection $line")
213 if defined $subheads{$line};
214 $subheads{$line} = 1;
216 err
($id, "period in =head")
217 if $line =~ /\.[^\w]/ or $line =~ /\.$/;
218 err
($id, "not all uppercase in =head1")
219 if $line =~ /head1.*[a-z]/;
220 err
($id, "all uppercase in subhead")
221 if $line =~ /head[234][ A-Z0-9]+$/;
225 # Because we have options and symbols with extra markup, we need
226 # to take that into account, so we need a regexp that extracts
227 # markup chunks, including recursive markup.
228 # please read up on /(?R)/ in perlre(1)
229 # (note: order is important, (?R) needs to come before .)
230 # (note: non-greedy is important, or something like 'B<foo> and B<bar>'
231 # will be captured as one item)
234 [BIL
]< # The start of what we recurse on
235 (?
:(?
-1)|.)*?
# recurse the whole regexp (refering to
236 # the last opened capture group, i.e. the
237 # start of this regexp), or pick next
238 # character. Do NOT be greedy!
239 > # The end of what we recurse on
240 )/x
; # (the x allows this sort of split up regexp)
242 # Options must start with a dash, followed by a letter, possibly
243 # followed by letters, digits, dashes and underscores, and the last
244 # character must be a letter or a digit.
245 # We do also accept the single -? or -n, where n is a digit
248 \? # Single question mark
254 [[:alpha
:]](?
:[-_
[:alnum
:]]*?
[[:alnum
:]])?
257 # Helper function to check if a given $thing is properly marked up
258 # option. It returns one of these values:
260 # undef if it's not an option
261 # "" if it's a malformed option
262 # $unwrapped the option with the outermost B<> wrapping removed.
263 sub normalise_option
{
265 my $filename = shift;
268 my $unwrapped = $thing;
269 my $unmarked = $thing;
271 # $unwrapped is the option with the outer B<> markup removed
272 $unwrapped =~ s/^B<//;
273 $unwrapped =~ s/>$//;
274 # $unmarked is the option with *all* markup removed
275 $unmarked =~ s/[BIL]<|>//msg;
278 # If we found an option, check it, collect it
279 if ( $unwrapped =~ /^\s*-/ ) {
280 return $unwrapped # return option with outer B<> removed
281 if $unmarked =~ /^-${option_re}$/;
282 return ""; # Malformed option
284 return undef; # Something else
287 # Checks of command option (man1) formatting. The man1 checks are
288 # restricted to the SYNOPSIS and OPTIONS sections, the rest is too
289 # free form, we simply cannot be too strict there.
293 my $filename = shift;
294 my $contents = shift;
296 my $synopsis = ($contents =~ /=head1\s+SYNOPSIS(.*?)=head1/s, $1);
298 # Some pages have more than one OPTIONS section, let's make sure
301 while ( $contents =~ /=head1\s+[A-Z ]*?OPTIONS$(.*?)(?==head1)/msg ) {
305 # Look for options with no or incorrect markup
307 /(?<![-<[:alnum:]])-(?:$markup_re|.)*(?![->[:alnum:]])/msg ) {
308 err
($id, "Malformed option [1] in SYNOPSIS: $&");
311 while ( $synopsis =~ /$markup_re/msg ) {
313 print STDERR
"$id:DEBUG[option_check] SYNOPSIS: found $found\n"
315 my $option_uw = normalise_option
($id, $filename, $found);
316 err
($id, "Malformed option [2] in SYNOPSIS: $found")
317 if defined $option_uw && $option_uw eq '';
320 # In OPTIONS, we look for =item paragraphs.
321 # (?=^\s*$) detects an empty line.
322 while ( $options =~ /=item\s+(.*?)(?=^\s*$)/msg ) {
325 while ( $item =~ /(\[\s*)?($markup_re)/msg ) {
327 print STDERR
"$id:DEBUG[option_check] OPTIONS: found $&\n"
329 err
($id, "Unexpected bracket in OPTIONS =item: $item")
330 if ($1 // '') ne '' && $found =~ /^B<\s*-/;
332 my $option_uw = normalise_option
($id, $filename, $found);
333 err
($id, "Malformed option in OPTIONS: $found")
334 if defined $option_uw && $option_uw eq '';
340 my $symbol_re = qr/[[:alpha:]_][_[:alnum:]]*?/;
342 # Checks of function name (man3) formatting. The man3 checks are
343 # easier than the man1 checks, we only check the names followed by (),
344 # and only the names that have POD markup.
346 sub functionname_check
{
348 my $filename = shift;
349 my $contents = shift;
351 while ( $contents =~ /($markup_re)\(\)/msg ) {
352 print STDERR
"$id:DEBUG[functionname_check] SYNOPSIS: found $&\n"
356 my $unmarked = $symbol;
357 $unmarked =~ s/[BIL]<|>//msg;
359 err
($id, "Malformed symbol: $symbol")
360 unless $symbol =~ /^B<.*>$/ && $unmarked =~ /^${symbol_re}$/
363 # We can't do the kind of collecting coolness that option_check()
364 # does, because there are too many things that can't be found in
365 # name repositories like the NAME sections, such as symbol names
366 # with a variable part (typically marked up as B<foo_I<TYPE>_bar>
369 # This is from http://man7.org/linux/man-pages/man7/man-pages.7.html
370 my %preferred_words = (
371 'bitmask' => 'bit mask',
372 'builtin' => 'built-in',
373 #'epoch' => 'Epoch', # handled specially, below
374 'file name' => 'filename',
375 'file system' => 'filesystem',
376 'host name' => 'hostname',
378 'lower case' => 'lowercase',
379 'lower-case' => 'lowercase',
380 'non-zero' => 'nonzero',
381 'path name' => 'pathname',
382 'pseudo-terminal' => 'pseudoterminal',
383 'reserved port' => 'privileged port',
384 'system port' => 'privileged port',
385 'realtime' => 'real-time',
386 'real time' => 'real-time',
387 'runtime' => 'run time',
388 'saved group ID'=> 'saved set-group-ID',
389 'saved set-GID' => 'saved set-group-ID',
390 'saved user ID' => 'saved set-user-ID',
391 'saved set-UID' => 'saved set-user-ID',
392 'set-GID' => 'set-group-ID',
393 'setgid' => 'set-group-ID',
394 'set-UID' => 'set-user-ID',
395 'setuid' => 'set-user-ID',
396 'super user' => 'superuser',
397 'super-user' => 'superuser',
398 'super block' => 'superblock',
399 'super-block' => 'superblock',
400 'time stamp' => 'timestamp',
401 'time zone' => 'timezone',
402 'upper case' => 'uppercase',
403 'upper-case' => 'uppercase',
404 'useable' => 'usable',
405 'userspace' => 'user space',
406 'user name' => 'username',
412 my $contents = shift;
414 foreach my $k ( keys %preferred_words ) {
416 next if $k eq 'file system'
417 and $contents =~ /Microsoft Encrypted File System/;
418 err
($id, "found '$k' should use '$preferred_words{$k}'")
419 if $contents =~ /\b\Q$k\E\b/i;
421 err
($id, "found 'epoch' should use 'Epoch'")
422 if $contents =~ /\bepoch\b/;
426 my $filename = shift;
427 my $dirname = basename
(dirname
($filename));
432 open POD
, $filename or die "Couldn't open $filename, $!";
437 my $id = "${filename}:1:";
438 check_head_style
($id, $contents);
440 # Check ordering of some sections in man3
441 if ( $filename =~ m
|man3
/| ) {
442 check_section_location
($id, $contents, "RETURN VALUES", "EXAMPLES");
443 check_section_location
($id, $contents, "SEE ALSO", "HISTORY");
444 check_section_location
($id, $contents, "EXAMPLES", "SEE ALSO");
447 unless ( $contents =~ /=for openssl generic/ ) {
448 if ( $filename =~ m
|man3
/| ) {
449 name_synopsis
($id, $filename, $contents);
450 functionname_check
($id, $filename, $contents);
451 } elsif ( $filename =~ m
|man1
/| ) {
452 option_check
($id, $filename, $contents)
456 wording
($id, $contents);
458 err
($id, "doesn't start with =pod")
459 if $contents !~ /^=pod/;
460 err
($id, "doesn't end with =cut")
461 if $contents !~ /=cut\n$/;
462 err
($id, "more than one cut line.")
463 if $contents =~ /=cut.*=cut/ms;
464 err
($id, "EXAMPLE not EXAMPLES section.")
465 if $contents =~ /=head1 EXAMPLE[^S]/;
466 err
($id, "WARNING not WARNINGS section.")
467 if $contents =~ /=head1 WARNING[^S]/;
468 err
($id, "missing copyright")
469 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
470 err
($id, "copyright not last")
471 if $contents =~ /head1 COPYRIGHT.*=head/ms;
472 err
($id, "head2 in All uppercase")
473 if $contents =~ /head2\s+[A-Z ]+\n/;
474 err
($id, "extra space after head")
475 if $contents =~ /=head\d\s\s+/;
476 err
($id, "period in NAME section")
477 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
478 err
($id, "Duplicate $1 in L<>")
479 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
480 err
($id, "Bad =over $1")
481 if $contents =~ /=over([^ ][^24])/;
482 err
($id, "Possible version style issue")
483 if $contents =~ /OpenSSL version [019]/;
485 if ( $contents !~ /=for openssl multiple includes/ ) {
486 # Look for multiple consecutive openssl #include lines
487 # (non-consecutive lines are okay; see man3/MD5.pod).
488 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
490 foreach my $line ( split /\n+/, $1 ) {
491 if ( $line =~ m
@include <openssl
/@
) {
492 err
($id, "has multiple includes")
501 open my $OUT, '>', $temp
502 or die "Can't open $temp, $!";
503 podchecker
($filename, $OUT);
505 open $OUT, '<', $temp
506 or die "Can't read $temp, $!";
508 next if /\(section\) in.*deprecated/;
512 unlink $temp || warn "Can't remove $temp, $!";
514 # Find what section this page is in; assume 3.
516 $section = $1 if $dirname =~ /man([1-9])/;
518 foreach ((@
{$mandatory_sections{'*'}}, @
{$mandatory_sections{$section}})) {
519 # Skip "return values" if not -s
520 err
($id, "missing $_ head1 section")
521 if $contents !~ /^=head1\s+${_}\s*$/m;
531 open my $IN, '<', $file
532 or die "Can't open $file, $!, stopped";
536 next if /\bNOEXIST\b/;
537 my @fields = split();
538 die "Malformed line $_"
539 if scalar @fields != 2 && scalar @fields != 4;
540 push @apis, $fields[0];
553 foreach my $pod ( glob("$dir/*.pod"), glob("$dir/*.pod.in") ) {
554 my %podinfo = extract_pod_info
($pod);
555 foreach my $n ( @
{$podinfo{names
}} ) {
557 err
("# Duplicate $n in $pod and $dups{$n}")
558 if defined $dups{$n} && $dups{$n} ne $pod;
570 my $missingfile = shift;
573 open FH
, $missingfile
574 || die "Can't open $missingfile";
591 @missing = loadmissing
('util/missingmacro111.txt');
593 @missing = loadmissing
('util/missingmacro.txt');
596 foreach my $f ( glob('include/openssl/*.h') ) {
597 # Skip some internals we don't want to document yet.
598 next if $f eq 'include/openssl/asn1.h';
599 next if $f eq 'include/openssl/asn1t.h';
600 next if $f eq 'include/openssl/err.h';
601 open(IN
, $f) || die "Can't open $f, $!";
603 next unless /^#\s*define\s*(\S+)\(/;
605 next if $docced{$macro} || defined $seen{$macro};
606 next if $macro =~ /i2d_/
608 || $macro =~ /DEPRECATEDIN/
609 || $macro =~ /IMPLEMENT_/
610 || $macro =~ /DECLARE_/;
612 # Skip macros known to be missing
613 next if $opt_v && grep( /^$macro$/, @missing);
615 err
("$f:", "macro $macro undocumented")
622 err
("# $count macros undocumented (count is approximate)")
629 my $missingfile = shift;
633 my @missing = loadmissing
($missingfile) if ($opt_v);
635 foreach my $func ( parsenum
($numfile) ) {
636 next if $docced{$func} || defined $seen{$func};
638 # Skip ASN1 utilities
639 next if $func =~ /^ASN1_/;
641 # Skip functions known to be missing
642 next if $opt_v && grep( /^$func$/, @missing);
644 err
("$libname:", "function $func undocumented")
649 err
("# $count in $numfile are not documented")
654 # Collection of links in each POD file.
655 # filename => [ "foo(1)", "bar(3)", ... ]
656 my %link_collection = ();
657 # Collection of names in each POD file.
658 # "name(s)" => filename
659 my %name_collection = ();
662 my $filename = shift;
663 $filename =~ m
|man
(\d
)/|;
665 my $simplename = basename
(basename
($filename, ".in"), ".pod");
666 my $id = "${filename}:1:";
671 open POD
, $filename or die "Couldn't open $filename, $!";
676 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
678 unless (defined $tmp) {
679 err
($id, "weird name section");
686 map { s
|/|-|g
; $_ } # Treat slash as dash
687 map { s/^\s+//g; s/\s+$//g; $_ } # Trim prefix and suffix blanks
689 unless (grep { $simplename eq $_ } @names) {
690 err
($id, "missing $simplename");
691 push @names, $simplename;
693 foreach my $name (@names) {
696 err
($id, "'$name' contains white space")
698 my $name_sec = "$name($section)";
699 if (! exists $name_collection{$name_sec}) {
700 $name_collection{$name_sec} = $filename;
701 } elsif ($filename eq $name_collection{$name_sec}) {
702 err
($id, "$name_sec repeated in NAME section of",
703 $name_collection{$name_sec});
705 err
($id, "$name_sec also in NAME section of",
706 $name_collection{$name_sec});
711 map { map { s/\s+//g; $_ } split(/,/, $_) }
712 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
713 foreach (@foreign_names) {
714 $name_collection{$_} = undef; # It still exists!
717 my @links = $contents =~ /L
<
718 # if the link is of the form L<something|name(s)>,
719 # then remove 'something'. Note that 'something'
720 # may contain POD codes as well...
721 (?
:(?
:[^\
|]|<[^>]*>)*\
|)?
722 # we're only interested in references that have
723 # a one digit section number
726 $link_collection{$filename} = [ @links ];
730 foreach my $filename (sort keys %link_collection) {
731 foreach my $link (@
{$link_collection{$filename}}) {
732 err
("${filename}:1:", "reference to non-existing $link")
733 unless exists $name_collection{$link};
738 # Load the public symbol/macro names
740 foreach my $name ( parsenum
('util/libcrypto.num') ) {
743 foreach my $name ( parsenum
('util/libssl.num') ) {
746 foreach my $name ( parsenum
('util/other.syms') ) {
751 # Cipher/digests to skip if not documented
776 # Get the list of options in the command.
777 open CFH
, "./apps/openssl list --options $cmd|"
778 || die "Can list options for $cmd, $!";
786 # Get the list of flags from the synopsis
788 || die "Can't open $doc, $!";
791 last if /DESCRIPTION/;
792 if ( /=for comment ifdef (.*)/ ) {
793 foreach my $f ( split / /, $1 ) {
798 next unless /\[B<-([^ >]+)/;
800 $opt = $1 if $opt =~ /I<(.*)/;
805 # See what's in the command not the manpage.
807 foreach my $k ( keys %cmdopts ) {
808 push @undocced, $k unless $docopts{$k};
810 if ( scalar @undocced > 0 ) {
811 foreach ( @undocced ) {
812 next if /-/; # Skip the -- end-of-flags marker
813 err
("$doc: undocumented option -$_");
817 # See what's in the command not the manpage.
819 foreach my $k ( keys %docopts ) {
820 push @unimpl, $k unless $cmdopts{$k};
822 if ( scalar @unimpl > 0 ) {
823 foreach ( @unimpl ) {
824 next if defined $skips{$_} || defined $localskips{$_};
825 err
("$cmd documented but not implemented -$_");
833 # Get list of commands.
834 open FH
, "./apps/openssl list -1 -commands|"
835 || die "Can't list commands, $!";
842 # See if each has a manpage.
843 foreach my $cmd ( @commands ) {
844 next if $cmd eq 'help' || $cmd eq 'exit';
845 my $doc = "doc/man1/$cmd.pod";
846 $doc = "doc/man1/openssl-$cmd.pod" if -f
"doc/man1/openssl-$cmd.pod";
848 err
("$doc does not exist");
850 checkflags
($cmd, $doc);
854 # See what help is missing.
855 open FH
, "./apps/openssl list --missing-help |"
856 || die "Can't list missing help, $!";
859 my ($cmd, $flag) = split;
860 err
("$cmd has no help for -$flag");
868 foreach (@ARGV ?
@ARGV : (glob('doc/*/*.pod'), glob('doc/*/*.pod.in'),
869 glob('doc/internal/*/*.pod'))) {
877 foreach (@ARGV ?
@ARGV : (glob('doc/*/*.pod'), glob('doc/*/*.pod.in'))) {
880 foreach (@ARGV ?
@ARGV : glob('doc/internal/*/*.pod')) {
884 # If not given args, check that all man1 commands are named properly.
885 if ( scalar @ARGV == 0 ) {
886 foreach (glob('doc/man1/*.pod')) {
887 next if /CA.pl/ || /openssl\.pod/ || /tsget\.pod/;
888 err
("$_ doesn't start with openssl-") unless /openssl-/;
893 if ( $opt_u || $opt_v) {
894 my %temp = getdocced
('doc/man3');
895 foreach ( keys %temp ) {
896 $docced{$_} = $temp{$_};
899 printem
('crypto', 'util/libcrypto.num', 'util/missingcrypto111.txt');
900 printem
('ssl', 'util/libssl.num', 'util/missingssl111.txt');
902 printem
('crypto', 'util/libcrypto.num', 'util/missingcrypto.txt');
903 printem
('ssl', 'util/libssl.num', 'util/missingssl.txt');