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;
37 Find small errors (nits) in documentation. Options:
38 -d Detailed list of undocumented (implies -u)
39 -e Detailed list of new undocumented (implies -v)
40 -s Same as -e except no output is generated if nothing is undocumented
41 -o Causes -e/-v to count symbols added since 1.1.1 as new (implies -v)
43 -n Print nits in POD pages
44 -p Warn if non-public name documented (implies -n)
45 -u Count undocumented functions
46 -v Count new undocumented functions
47 -h Print this help message
48 -c List undocumented commands and options
53 my $temp = '/tmp/docnits.txt';
57 my %mandatory_sections =
58 ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
59 1 => [ 'SYNOPSIS', 'OPTIONS' ],
60 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
64 # Cross-check functions in the NAME and SYNOPSIS section.
71 # Get NAME section and all words in it.
72 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
75 print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
77 print "$id POD markup among the names in NAME\n" if $tmp =~ /[<>]/;
79 print "$id missing comma in NAME\n" if $tmp =~ /[^,] /;
81 my $dirname = dirname($filename);
82 my $simplename = basename(basename($filename, ".in"), ".pod");
83 my $foundfilename = 0;
84 my %foundfilenames = ();
86 foreach my $n ( split ',', $tmp ) {
89 print "$id the name '$n' contains white-space\n"
92 $foundfilename++ if $n eq $simplename;
93 $foundfilenames{$n} = 1
94 if ((-f "$dirname/$n.pod.in" || -f "$dirname/$n.pod")
95 && $n ne $simplename);
97 print "$id the following exist as other .pod or .pod.in files:\n",
98 join(" ", sort keys %foundfilenames), "\n"
100 print "$id $simplename (filename) missing from NAME section\n"
101 unless $foundfilename;
102 foreach my $n ( keys %names ) {
103 print "$id $n is not public\n"
104 if $opt_p and !defined $public{$n};
107 # Find all functions in SYNOPSIS
108 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
110 foreach my $line ( split /\n+/, $syn ) {
111 next unless $line =~ /^\s/;
113 $line =~ s/STACK_OF\([^)]+\)/int/g;
114 $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g;
115 $line =~ s/__declspec\([^)]+\)//;
116 if ( $line =~ /env (\S*)=/ ) {
117 # environment variable env NAME=...
119 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
120 # a callback function pointer: typedef ... (*NAME)(...
122 } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
123 # a callback function signature: typedef ... NAME(...
125 } elsif ( $line =~ /typedef.* (\S+);/ ) {
126 # a simple typedef: typedef ... NAME;
128 } elsif ( $line =~ /enum (\S*) \{/ ) {
129 # an enumeration: enum ... {
131 } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) {
133 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
139 print "$id $sym missing from NAME section\n"
140 unless defined $names{$sym};
143 # Do some sanity checks on the prototype.
144 print "$id prototype missing spaces around commas: $line\n"
145 if ( $line =~ /[a-z0-9],[^ ]/ );
148 foreach my $n ( keys %names ) {
149 next if $names{$n} == 2;
150 print "$id $n missing from SYNOPSIS\n";
154 # Check if SECTION ($3) is located before BEFORE ($4)
155 sub check_section_location()
158 my $contents = shift;
162 return unless $contents =~ /=head1 $section/
163 and $contents =~ /=head1 $before/;
164 print "$id $section should appear before $before section\n"
165 if $contents =~ /=head1 $before.*=head1 $section/ms;
168 # Check if a =head1 is duplicated, or a =headX is duplicated within a
169 # =head1. Treats =head2 =head3 as equivalent -- it doesn't reset the head3
170 # sets if it finds a =head2 -- but that is good enough for now. Also check
171 # for proper capitalization, trailing periods, etc.
172 sub check_head_style()
175 my $contents = shift;
179 foreach my $line ( split /\n+/, $contents ) {
180 next unless $line =~ /^=head/;
181 if ( $line =~ /head1/ ) {
182 print "$id duplicate section $line\n"
183 if defined $head1{$line};
187 print "$id duplicate subsection $line\n"
188 if defined $subheads{$line};
189 $subheads{$line} = 1;
191 print "$id period in =head\n"
192 if $line =~ /\.[^\w]/ or $line =~ /\.$/;
193 print "$id not all uppercase in =head1\n"
194 if $line =~ /head1.*[a-z]/;
195 print "$id all uppercase in subhead\n"
196 if $line =~ /head[234][ A-Z0-9]+$/;
202 my $filename = shift;
203 my $dirname = basename(dirname($filename));
208 open POD, $filename or die "Couldn't open $filename, $!";
213 my $id = "${filename}:1:";
214 &check_head_style($id, $contents);
216 # Check ordering of some sections in man3
217 if ( $filename =~ m|man3/| ) {
218 &check_section_location($id, $contents, "RETURN VALUES", "EXAMPLES");
219 &check_section_location($id, $contents, "SEE ALSO", "HISTORY");
220 &check_section_location($id, $contents, "EXAMPLES", "SEE ALSO");
223 &name_synopsis($id, $filename, $contents)
224 unless $contents =~ /=for comment generic/
225 or $filename =~ m@man[157]/@;
227 print "$id doesn't start with =pod\n"
228 if $contents !~ /^=pod/;
229 print "$id doesn't end with =cut\n"
230 if $contents !~ /=cut\n$/;
231 print "$id more than one cut line.\n"
232 if $contents =~ /=cut.*=cut/ms;
233 print "$id EXAMPLE not EXAMPLES section.\n"
234 if $contents =~ /=head1 EXAMPLE[^S]/;
235 print "$id WARNING not WARNINGS section.\n"
236 if $contents =~ /=head1 WARNING[^S]/;
237 print "$id missing copyright\n"
238 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
239 print "$id copyright not last\n"
240 if $contents =~ /head1 COPYRIGHT.*=head/ms;
241 print "$id head2 in All uppercase\n"
242 if $contents =~ /head2\s+[A-Z ]+\n/;
243 print "$id extra space after head\n"
244 if $contents =~ /=head\d\s\s+/;
245 print "$id period in NAME section\n"
246 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
247 print "$id Duplicate $1 in L<>\n"
248 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
249 print "$id Bad =over $1\n"
250 if $contents =~ /=over([^ ][^24])/;
251 print "$id Possible version style issue\n"
252 if $contents =~ /OpenSSL version [019]/;
254 if ( $contents !~ /=for comment multiple includes/ ) {
255 # Look for multiple consecutive openssl #include lines
256 # (non-consecutive lines are okay; see man3/MD5.pod).
257 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
259 foreach my $line ( split /\n+/, $1 ) {
260 if ( $line =~ m@include <openssl/@ ) {
261 print "$id has multiple includes\n" if ++$count == 2;
269 open my $OUT, '>', $temp
270 or die "Can't open $temp, $!";
271 podchecker($filename, $OUT);
273 open $OUT, '<', $temp
274 or die "Can't read $temp, $!";
276 next if /\(section\) in.*deprecated/;
280 unlink $temp || warn "Can't remove $temp, $!";
282 # Find what section this page is in; assume 3.
284 $section = $1 if $dirname =~ /man([1-9])/;
286 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
287 # Skip "return values" if not -s
288 print "$id: missing $_ head1 section\n"
289 if $contents !~ /^=head1\s+${_}\s*$/m;
300 open my $IN, '<', $file
301 or die "Can't open $file, $!, stopped";
305 next if /\bNOEXIST\b/;
306 my @fields = split();
307 die "Malformed line $_"
308 if scalar @fields != 2 && scalar @fields != 4;
309 push @apis, $fields[0];
314 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
323 foreach my $pod ( glob("$dir/*.pod"), glob("$dir/*.pod.in") ) {
324 my %podinfo = extract_pod_info($pod);
325 foreach my $n ( @{$podinfo{names}} ) {
327 print "# Duplicate $n in $pod and $dups{$n}\n"
328 if defined $dups{$n} && $dups{$n} ne $pod;
340 my $missingfile = shift;
343 open FH, $missingfile
344 || die "Can't open $missingfile";
362 @missing = loadmissing('util/missingmacro111.txt');
364 @missing = loadmissing('util/missingmacro.txt');
367 print "# Checking macros (approximate)\n" if !$opt_s;
368 foreach my $f ( glob('include/openssl/*.h') ) {
369 # Skip some internals we don't want to document yet.
370 next if $f eq 'include/openssl/asn1.h';
371 next if $f eq 'include/openssl/asn1t.h';
372 next if $f eq 'include/openssl/err.h';
373 open(IN, $f) || die "Can't open $f, $!";
375 next unless /^#\s*define\s*(\S+)\(/;
377 next if $docced{$macro} || defined $seen{$macro};
378 next if $macro =~ /i2d_/
380 || $macro =~ /DEPRECATEDIN/
381 || $macro =~ /IMPLEMENT_/
382 || $macro =~ /DECLARE_/;
384 # Skip macros known to be missing
385 next if $opt_v && grep( /^$macro$/, @missing);
387 print "$f:$macro\n" if $opt_d || $opt_e;
393 print "# Found $count macros missing\n" if !$opt_s || $count > 0;
400 my $missingfile = shift;
404 my @missing = loadmissing($missingfile) if ($opt_v);
406 foreach my $func ( &parsenum($numfile) ) {
407 next if $docced{$func} || defined $seen{$func};
409 # Skip ASN1 utilities
410 next if $func =~ /^ASN1_/;
412 # Skip functions known to be missing
413 next if $opt_v && grep( /^$func$/, @missing);
415 print "$libname:$func\n" if $opt_d || $opt_e;
419 print "# Found $count missing from $numfile\n\n" if !$opt_s || $count > 0;
423 # Collection of links in each POD file.
424 # filename => [ "foo(1)", "bar(3)", ... ]
425 my %link_collection = ();
426 # Collection of names in each POD file.
427 # "name(s)" => filename
428 my %name_collection = ();
431 my $filename = shift;
432 $filename =~ m|man(\d)/|;
434 my $simplename = basename(basename($filename, ".in"), ".pod");
435 my $id = "${filename}:1:";
440 open POD, $filename or die "Couldn't open $filename, $!";
445 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
447 unless (defined $tmp) {
448 print "$id weird name section\n";
455 map { s|/|-|g; $_ } # Treat slash as dash
456 map { s/^\s+//g; s/\s+$//g; $_ } # Trim prefix and suffix blanks
458 unless (grep { $simplename eq $_ } @names) {
459 print "$id missing $simplename\n";
460 push @names, $simplename;
462 foreach my $name (@names) {
465 print "$id '$name' contains white space\n";
467 my $name_sec = "$name($section)";
468 if (! exists $name_collection{$name_sec}) {
469 $name_collection{$name_sec} = $filename;
470 } elsif ($filename eq $name_collection{$name_sec}) {
471 print "$id $name_sec repeated in NAME section of $name_collection{$name_sec}\n"
473 print "$id $name_sec also in NAME section of $name_collection{$name_sec}\n";
478 map { map { s/\s+//g; $_ } split(/,/, $_) }
479 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
480 foreach (@foreign_names) {
481 $name_collection{$_} = undef; # It still exists!
484 my @links = $contents =~ /L<
485 # if the link is of the form L<something|name(s)>,
486 # then remove 'something'. Note that 'something'
487 # may contain POD codes as well...
488 (?:(?:[^\|]|<[^>]*>)*\|)?
489 # we're only interested in references that have
490 # a one digit section number
493 $link_collection{$filename} = [ @links ];
497 foreach my $filename (sort keys %link_collection) {
498 foreach my $link (@{$link_collection{$filename}}) {
499 print "${filename}:1: reference to non-existing $link\n"
500 unless exists $name_collection{$link};
506 foreach my $name ( &parsenum('util/libcrypto.num') ) {
509 foreach my $name ( &parsenum('util/libssl.num') ) {
512 foreach my $name ( &parsenum('util/private.num') ) {
540 # Get the list of options in the command.
541 open CFH, "./apps/openssl list --options $cmd|"
542 || die "Can list options for $cmd, $!";
550 # Get the list of flags from the synopsis
551 open CFH, "<doc/man1/$cmd.pod"
552 || die "Can't open $cmd.pod, $!";
555 last if /DESCRIPTION/;
556 next unless /\[B<-([^ >]+)/;
561 # See what's in the command not the manpage.
563 foreach my $k ( keys %cmdopts ) {
564 push @undocced, $k unless $docopts{$k};
566 if ( scalar @undocced > 0 ) {
568 foreach ( @undocced ) {
569 print "doc/man1/$cmd.pod: Missing -$_\n";
573 # See what's in the command not the manpage.
575 foreach my $k ( keys %docopts ) {
576 push @unimpl, $k unless $cmdopts{$k};
578 if ( scalar @unimpl > 0 ) {
580 foreach ( @unimpl ) {
581 next if defined $skips{$_};
582 print "doc/man1/$cmd.pod: Not implemented -$_\n";
589 getopts('cdesolnphuv');
593 $opt_n = 1 if $opt_p;
594 $opt_u = 1 if $opt_d;
595 $opt_e = 1 if $opt_s;
596 $opt_v = 1 if $opt_o || $opt_e;
598 die "Cannot use both -u and -v" if $opt_u && $opt_v;
599 die "Cannot use both -d and -e" if $opt_d && $opt_e;
601 # We only need to check c, l, n, u and v.
602 # Options d, e, s, o and p imply one of the above.
603 die "Need one of -[cdesolnpuv] flags.\n"
604 unless $opt_c or $opt_l or $opt_n or $opt_u or $opt_v;
610 # Get list of commands.
611 open FH, "./apps/openssl list -1 -commands|"
612 || die "Can't list commands, $!";
619 # See if each has a manpage.
620 foreach ( @commands ) {
621 next if $_ eq 'help' || $_ eq 'exit';
622 if ( ! -f "doc/man1/$_.pod" ) {
623 print "doc/man1/$_.pod does not exist\n";
626 $ok = 0 if not &checkflags($_);
630 # See what help is missing.
631 open FH, "./apps/openssl list --missing-help |"
632 || die "Can't list missing help, $!";
635 my ($cmd, $flag) = split;
636 print "$cmd has no help for -$flag\n";
645 foreach (@ARGV ? @ARGV : (glob('doc/*/*.pod'), glob('doc/*/*.pod.in'),
646 glob('doc/internal/*/*.pod'))) {
653 &publicize() if $opt_p;
654 foreach (@ARGV ? @ARGV : (glob('doc/*/*.pod'), glob('doc/*/*.pod.in'))) {
658 local $opt_p = undef;
659 foreach (@ARGV ? @ARGV : glob('doc/internal/*/*.pod')) {
665 if ( $opt_u || $opt_v) {
666 my %temp = getdocced('doc/man3');
667 foreach ( keys %temp ) {
668 $docced{$_} = $temp{$_};
671 &printem('crypto', 'util/libcrypto.num', 'util/missingcrypto111.txt');
672 &printem('ssl', 'util/libssl.num', 'util/missingssl111.txt');
674 &printem('crypto', 'util/libcrypto.num', 'util/missingcrypto.txt');
675 &printem('ssl', 'util/libssl.num', 'util/missingssl.txt');