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;
33 Find small errors (nits) in documentation. Options:
34 -d Detailed list of undocumented (implies -u)
36 -n Print nits in POD pages
37 -p Warn if non-public name documented (implies -n)
38 -u Count undocumented functions
39 -h Print this help message
40 -c List undocumented commands and options
45 my $temp = '/tmp/docnits.txt';
49 my %mandatory_sections =
50 ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
51 1 => [ 'SYNOPSIS', 'OPTIONS' ],
52 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
56 # Cross-check functions in the NAME and SYNOPSIS section.
63 # Get NAME section and all words in it.
64 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
67 print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
69 print "$id POD markup among the names in NAME\n" if $tmp =~ /[<>]/;
71 print "$id missing comma in NAME\n" if $tmp =~ /[^,] /;
73 my $dirname = dirname($filename);
74 my $simplename = basename($filename);
75 $simplename =~ s/.pod$//;
76 my $foundfilename = 0;
77 my %foundfilenames = ();
79 foreach my $n ( split ',', $tmp ) {
82 print "$id the name '$n' contains white-space\n"
85 $foundfilename++ if $n eq $simplename;
86 $foundfilenames{$n} = 1
87 if -f "$dirname/$n.pod" && $n ne $simplename;
89 print "$id the following exist as other .pod files:\n",
90 join(" ", sort keys %foundfilenames), "\n"
92 print "$id $simplename (filename) missing from NAME section\n"
93 unless $foundfilename;
94 foreach my $n ( keys %names ) {
95 print "$id $n is not public\n"
96 if $opt_p and !defined $public{$n};
99 # Find all functions in SYNOPSIS
100 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
102 foreach my $line ( split /\n+/, $syn ) {
103 next unless $line =~ /^\s/;
105 $line =~ s/STACK_OF\([^)]+\)/int/g;
106 $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g;
107 $line =~ s/__declspec\([^)]+\)//;
108 if ( $line =~ /env (\S*)=/ ) {
109 # environment variable env NAME=...
111 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
112 # a callback function pointer: typedef ... (*NAME)(...
114 } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
115 # a callback function signature: typedef ... NAME(...
117 } elsif ( $line =~ /typedef.* (\S+);/ ) {
118 # a simple typedef: typedef ... NAME;
120 } elsif ( $line =~ /enum (\S*) \{/ ) {
121 # an enumeration: enum ... {
123 } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) {
125 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
131 print "$id $sym missing from NAME section\n"
132 unless defined $names{$sym};
135 # Do some sanity checks on the prototype.
136 print "$id prototype missing spaces around commas: $line\n"
137 if ( $line =~ /[a-z0-9],[^ ]/ );
140 foreach my $n ( keys %names ) {
141 next if $names{$n} == 2;
142 print "$id $n missing from SYNOPSIS\n";
146 # Check if SECTION is located before BEFORE
147 sub check_section_location()
149 my $filename = shift;
150 my $contents = shift;
154 return unless $contents =~ /=head1 $section/
155 and $contents =~ /=head1 $before/;
156 print "$filename: $section should be placed before $before section\n"
157 if $contents =~ /=head1 $before.*=head1 $section/ms;
162 my $filename = shift;
163 my $dirname = basename(dirname($filename));
168 open POD, $filename or die "Couldn't open $filename, $!";
173 # Check if EXAMPLES is located after RETURN VALUES section.
174 &check_section_location($filename, $contents, "RETURN VALUES", "EXAMPLES") if $filename =~ m|man3/|;
175 # Check if HISTORY is located after SEE ALSO
176 &check_section_location($filename, $contents, "SEE ALSO", "HISTORY") if $filename =~ m|man3/|;
177 # Check if SEE ALSO is located after EXAMPLES
178 &check_section_location($filename, $contents, "EXAMPLES", "SEE ALSO") if $filename =~ m|man3/|;
180 my $id = "${filename}:1:";
182 &name_synopsis($id, $filename, $contents)
183 unless $contents =~ /=for comment generic/
184 or $filename =~ m@man[157]/@;
186 print "$id doesn't start with =pod\n"
187 if $contents !~ /^=pod/;
188 print "$id doesn't end with =cut\n"
189 if $contents !~ /=cut\n$/;
190 print "$id more than one cut line.\n"
191 if $contents =~ /=cut.*=cut/ms;
192 print "$id missing copyright\n"
193 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
194 print "$id copyright not last\n"
195 if $contents =~ /head1 COPYRIGHT.*=head/ms;
196 print "$id head2 in All uppercase\n"
197 if $contents =~ /head2\s+[A-Z ]+\n/;
198 print "$id extra space after head\n"
199 if $contents =~ /=head\d\s\s+/;
200 print "$id period in NAME section\n"
201 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
202 print "$id Duplicate $1 in L<>\n"
203 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
204 print "$id Bad =over $1\n"
205 if $contents =~ /=over([^ ][^24])/;
206 print "$id Possible version style issue\n"
207 if $contents =~ /OpenSSL version [019]/;
209 if ( $contents !~ /=for comment multiple includes/ ) {
210 # Look for multiple consecutive openssl #include lines
211 # (non-consecutive lines are okay; see man3/MD5.pod).
212 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
214 foreach my $line ( split /\n+/, $1 ) {
215 if ( $line =~ m@include <openssl/@ ) {
216 print "$id has multiple includes\n" if ++$count == 2;
224 open my $OUT, '>', $temp
225 or die "Can't open $temp, $!";
226 podchecker($filename, $OUT);
228 open $OUT, '<', $temp
229 or die "Can't read $temp, $!";
231 next if /\(section\) in.*deprecated/;
235 unlink $temp || warn "Can't remove $temp, $!";
237 # Find what section this page is in; assume 3.
239 $section = $1 if $dirname =~ /man([1-9])/;
241 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
242 # Skip "return values" if not -s
243 print "$id: missing $_ head1 section\n"
244 if $contents !~ /^=head1\s+${_}\s*$/m;
255 open my $IN, '<', $file
256 or die "Can't open $file, $!, stopped";
260 next if /\bNOEXIST\b/;
261 next if /\bEXPORT_VAR_AS_FUNC\b/;
262 my @fields = split();
263 die "Malformed line $_"
264 if scalar @fields != 2 && scalar @fields != 4;
265 push @apis, $fields[0];
270 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
279 foreach my $pod ( glob("$dir/*.pod") ) {
280 my %podinfo = extract_pod_info($pod);
281 foreach my $n ( @{$podinfo{names}} ) {
283 print "# Duplicate $n in $pod and $dups{$n}\n"
284 if defined $dups{$n} && $dups{$n} ne $pod;
299 print "# Checking macros (approximate)\n";
300 foreach my $f ( glob('include/openssl/*.h') ) {
301 # Skip some internals we don't want to document yet.
302 next if $f eq 'include/openssl/asn1.h';
303 next if $f eq 'include/openssl/asn1t.h';
304 next if $f eq 'include/openssl/err.h';
305 open(IN, $f) || die "Can't open $f, $!";
307 next unless /^#\s*define\s*(\S+)\(/;
309 next if $docced{$macro} || defined $seen{$macro};
310 next if $macro =~ /i2d_/
312 || $macro =~ /DEPRECATEDIN/
313 || $macro =~ /IMPLEMENT_/
314 || $macro =~ /DECLARE_/;
315 print "$f:$macro\n" if $opt_d;
321 print "# Found $count macros missing (not all should be documented)\n"
331 foreach my $func ( &parsenum($numfile) ) {
332 next if $docced{$func} || defined $seen{$func};
334 # Skip ASN1 utilities
335 next if $func =~ /^ASN1_/;
337 print "$libname:$func\n" if $opt_d;
341 print "# Found $count missing from $numfile\n\n";
345 # Collection of links in each POD file.
346 # filename => [ "foo(1)", "bar(3)", ... ]
347 my %link_collection = ();
348 # Collection of names in each POD file.
349 # "name(s)" => filename
350 my %name_collection = ();
353 my $filename = shift;
354 $filename =~ m|man(\d)/|;
356 my $simplename = basename($filename, ".pod");
357 my $id = "${filename}:1:";
362 open POD, $filename or die "Couldn't open $filename, $!";
367 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
369 unless (defined $tmp) {
370 print "$id weird name section\n";
376 my @names = map { s/^\s+//g; s/\s+$//g; $_ } split(/,/, $tmp);
377 unless (grep { $simplename eq $_ } @names) {
378 print "$id missing $simplename\n";
379 push @names, $simplename;
381 foreach my $name (@names) {
384 print "$id '$name' contains white space\n";
386 my $name_sec = "$name($section)";
387 if (! exists $name_collection{$name_sec}) {
388 $name_collection{$name_sec} = $filename;
389 } else { #elsif ($filename ne $name_collection{$name_sec}) {
390 print "$id $name_sec also in $name_collection{$name_sec}\n";
395 map { map { s/\s+//g; $_ } split(/,/, $_) }
396 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
397 foreach (@foreign_names) {
398 $name_collection{$_} = undef; # It still exists!
401 my @links = $contents =~ /L<
402 # if the link is of the form L<something|name(s)>,
403 # then remove 'something'. Note that 'something'
404 # may contain POD codes as well...
405 (?:(?:[^\|]|<[^>]*>)*\|)?
406 # we're only interested in references that have
407 # a one digit section number
410 $link_collection{$filename} = [ @links ];
414 foreach my $filename (sort keys %link_collection) {
415 foreach my $link (@{$link_collection{$filename}}) {
416 print "${filename}:1: reference to non-existing $link\n"
417 unless exists $name_collection{$link};
423 foreach my $name ( &parsenum('util/libcrypto.num') ) {
426 foreach my $name ( &parsenum('util/libssl.num') ) {
429 foreach my $name ( &parsenum('util/private.num') ) {
457 # Get the list of options in the command.
458 open CFH, "./apps/openssl list --options $cmd|"
459 || die "Can list options for $cmd, $!";
467 # Get the list of flags from the synopsis
468 open CFH, "<doc/man1/$cmd.pod"
469 || die "Can't open $cmd.pod, $!";
472 last if /DESCRIPTION/;
473 next unless /\[B<-([^ >]+)/;
478 # See what's in the command not the manpage.
480 foreach my $k ( keys %cmdopts ) {
481 push @undocced, $k unless $docopts{$k};
483 if ( scalar @undocced > 0 ) {
485 foreach ( @undocced ) {
486 print "doc/man1/$cmd.pod: Missing -$_\n";
490 # See what's in the command not the manpage.
492 foreach my $k ( keys %docopts ) {
493 push @unimpl, $k unless $cmdopts{$k};
495 if ( scalar @unimpl > 0 ) {
497 foreach ( @unimpl ) {
498 next if defined $skips{$_};
499 print "doc/man1/$cmd.pod: Not implemented -$_\n";
509 $opt_n = 1 if $opt_p;
510 $opt_u = 1 if $opt_d;
512 die "Need one of -[cdlnpu] flags.\n"
513 unless $opt_c or $opt_l or $opt_n or $opt_u;
519 # Get list of commands.
520 open FH, "./apps/openssl list -1 -commands|"
521 || die "Can't list commands, $!";
528 # See if each has a manpage.
529 foreach ( @commands ) {
530 next if $_ eq 'help' || $_ eq 'exit';
531 if ( ! -f "doc/man1/$_.pod" ) {
532 print "doc/man1/$_.pod does not exist\n";
535 $ok = 0 if not &checkflags($_);
539 # See what help is missing.
540 open FH, "./apps/openssl list --missing-help |"
541 || die "Can't list missing help, $!";
544 my ($cmd, $flag) = split;
545 print "$cmd has no help for -$flag\n";
554 foreach (@ARGV ? @ARGV : (glob('doc/*/*.pod'),
555 glob('doc/internal/*/*.pod'))) {
562 &publicize() if $opt_p;
563 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
567 local $opt_p = undef;
568 foreach (@ARGV ? @ARGV : glob('doc/internal/*/*.pod')) {
575 my %temp = getdocced('doc/man3');
576 foreach ( keys %temp ) {
577 $docced{$_} = $temp{$_};
579 &printem('crypto', 'util/libcrypto.num');
580 &printem('ssl', 'util/libssl.num');