2 # Copyright 2002-2018 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 =~ /, *-/;
70 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 ) {
81 $foundfilename++ if $n eq $simplename;
82 $foundfilenames{$n} = 1
83 if -f "$dirname/$n.pod" && $n ne $simplename;
85 print "$id the following exist as other .pod files:\n",
86 join(" ", sort keys %foundfilenames), "\n"
88 print "$id $simplename (filename) missing from NAME section\n"
89 unless $foundfilename;
90 foreach my $n ( keys %names ) {
91 print "$id $n is not public\n"
92 if $opt_p and !defined $public{$n};
95 # Find all functions in SYNOPSIS
96 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
98 foreach my $line ( split /\n+/, $syn ) {
99 next unless $line =~ /^\s/;
101 $line =~ s/STACK_OF\([^)]+\)/int/g;
102 $line =~ s/__declspec\([^)]+\)//;
103 if ( $line =~ /env (\S*)=/ ) {
104 # environment variable env NAME=...
106 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
107 # a callback function pointer: typedef ... (*NAME)(...
109 } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
110 # a callback function signature: typedef ... NAME(...
112 } elsif ( $line =~ /typedef.* (\S+);/ ) {
113 # a simple typedef: typedef ... NAME;
115 } elsif ( $line =~ /enum (\S*) \{/ ) {
116 # an enumeration: enum ... {
118 } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) {
120 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
126 print "$id $sym missing from NAME section\n"
127 unless defined $names{$sym};
130 # Do some sanity checks on the prototype.
131 print "$id prototype missing spaces around commas: $line\n"
132 if ( $line =~ /[a-z0-9],[^ ]/ );
135 foreach my $n ( keys %names ) {
136 next if $names{$n} == 2;
137 print "$id $n missing from SYNOPSIS\n";
143 my $filename = shift;
144 my $dirname = basename(dirname($filename));
149 open POD, $filename or die "Couldn't open $filename, $!";
154 my $id = "${filename}:1:";
156 &name_synopsis($id, $filename, $contents)
157 unless $contents =~ /=for comment generic/
158 or $filename =~ m@man[157]/@;
160 print "$id doesn't start with =pod\n"
161 if $contents !~ /^=pod/;
162 print "$id doesn't end with =cut\n"
163 if $contents !~ /=cut\n$/;
164 print "$id more than one cut line.\n"
165 if $contents =~ /=cut.*=cut/ms;
166 print "$id missing copyright\n"
167 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
168 print "$id copyright not last\n"
169 if $contents =~ /head1 COPYRIGHT.*=head/ms;
170 print "$id head2 in All uppercase\n"
171 if $contents =~ /head2\s+[A-Z ]+\n/;
172 print "$id extra space after head\n"
173 if $contents =~ /=head\d\s\s+/;
174 print "$id period in NAME section\n"
175 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
176 print "$id POD markup in NAME section\n"
177 if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
178 print "$id Duplicate $1 in L<>\n"
179 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
180 print "$id Bad =over $1\n"
181 if $contents =~ /=over([^ ][^24])/;
182 print "$id Possible version style issue\n"
183 if $contents =~ /OpenSSL version [019]/;
185 if ( $contents !~ /=for comment multiple includes/ ) {
186 # Look for multiple consecutive openssl #include lines
187 # (non-consecutive lines are okay; see man3/MD5.pod).
188 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
190 foreach my $line ( split /\n+/, $1 ) {
191 if ( $line =~ m@include <openssl/@ ) {
192 print "$id has multiple includes\n" if ++$count == 2;
200 open my $OUT, '>', $temp
201 or die "Can't open $temp, $!";
202 podchecker($filename, $OUT);
204 open $OUT, '<', $temp
205 or die "Can't read $temp, $!";
207 next if /\(section\) in.*deprecated/;
211 unlink $temp || warn "Can't remove $temp, $!";
213 # Find what section this page is in; assume 3.
215 $section = $1 if $dirname =~ /man([1-9])/;
217 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
218 # Skip "return values" if not -s
219 print "$id: missing $_ head1 section\n"
220 if $contents !~ /^=head1\s+${_}\s*$/m;
231 open my $IN, '<', $file
232 or die "Can't open $file, $!, stopped";
236 next if /\bNOEXIST\b/;
237 next if /\bEXPORT_VAR_AS_FUNC\b/;
238 my @fields = split();
239 die "Malformed line $_"
240 if scalar @fields != 2 && scalar @fields != 4;
241 push @apis, $fields[0];
246 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
255 foreach my $pod ( glob("$dir/*.pod") ) {
256 my %podinfo = extract_pod_info($pod);
257 foreach my $n ( @{$podinfo{names}} ) {
259 print "# Duplicate $n in $pod and $dups{$n}\n"
260 if defined $dups{$n} && $dups{$n} ne $pod;
275 print "# Checking macros (approximate)\n";
276 foreach my $f ( glob('include/openssl/*.h') ) {
277 # Skip some internals we don't want to document yet.
278 next if $f eq 'include/openssl/asn1.h';
279 next if $f eq 'include/openssl/asn1t.h';
280 next if $f eq 'include/openssl/err.h';
281 open(IN, $f) || die "Can't open $f, $!";
283 next unless /^#\s*define\s*(\S+)\(/;
285 next if $docced{$macro} || defined $seen{$macro};
286 next if $macro =~ /i2d_/
288 || $macro =~ /DEPRECATEDIN/
289 || $macro =~ /IMPLEMENT_/
290 || $macro =~ /DECLARE_/;
291 print "$f:$macro\n" if $opt_d;
297 print "# Found $count macros missing (not all should be documented)\n"
307 foreach my $func ( &parsenum($numfile) ) {
308 next if $docced{$func} || defined $seen{$func};
310 # Skip ASN1 utilities
311 next if $func =~ /^ASN1_/;
313 print "$libname:$func\n" if $opt_d;
317 print "# Found $count missing from $numfile\n\n";
321 # Collection of links in each POD file.
322 # filename => [ "foo(1)", "bar(3)", ... ]
323 my %link_collection = ();
324 # Collection of names in each POD file.
325 # "name(s)" => filename
326 my %name_collection = ();
329 my $filename = shift;
330 $filename =~ m|man(\d)/|;
332 my $simplename = basename($filename, ".pod");
333 my $id = "${filename}:1:";
338 open POD, $filename or die "Couldn't open $filename, $!";
343 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
345 unless (defined $tmp) {
346 print "$id weird name section\n";
352 my @names = map { s/\s+//g; $_ } split(/,/, $tmp);
353 unless (grep { $simplename eq $_ } @names) {
354 print "$id missing $simplename\n";
355 push @names, $simplename;
357 foreach my $name (@names) {
359 my $name_sec = "$name($section)";
360 if (! exists $name_collection{$name_sec}) {
361 $name_collection{$name_sec} = $filename;
362 } else { #elsif ($filename ne $name_collection{$name_sec}) {
363 print "$id $name_sec also in $name_collection{$name_sec}\n";
368 map { map { s/\s+//g; $_ } split(/,/, $_) }
369 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
370 foreach (@foreign_names) {
371 $name_collection{$_} = undef; # It still exists!
374 my @links = $contents =~ /L<
375 # if the link is of the form L<something|name(s)>,
376 # then remove 'something'. Note that 'something'
377 # may contain POD codes as well...
378 (?:(?:[^\|]|<[^>]*>)*\|)?
379 # we're only interested in references that have
380 # a one digit section number
383 $link_collection{$filename} = [ @links ];
387 foreach my $filename (sort keys %link_collection) {
388 foreach my $link (@{$link_collection{$filename}}) {
389 print "${filename}:1: reference to non-existing $link\n"
390 unless exists $name_collection{$link};
396 foreach my $name ( &parsenum('util/libcrypto.num') ) {
399 foreach my $name ( &parsenum('util/libssl.num') ) {
402 foreach my $name ( &parsenum('util/private.num') ) {
430 # Get the list of options in the command.
431 open CFH, "./apps/openssl list --options $cmd|"
432 || die "Can list options for $cmd, $!";
440 # Get the list of flags from the synopsis
441 open CFH, "<doc/man1/$cmd.pod"
442 || die "Can't open $cmd.pod, $!";
445 last if /DESCRIPTION/;
446 next unless /\[B<-([^ >]+)/;
451 # See what's in the command not the manpage.
453 foreach my $k ( keys %cmdopts ) {
454 push @undocced, $k unless $docopts{$k};
456 if ( scalar @undocced > 0 ) {
458 foreach ( @undocced ) {
459 print "doc/man1/$cmd.pod: Missing -$_\n";
463 # See what's in the command not the manpage.
465 foreach my $k ( keys %docopts ) {
466 push @unimpl, $k unless $cmdopts{$k};
468 if ( scalar @unimpl > 0 ) {
470 foreach ( @unimpl ) {
471 next if defined $skips{$_};
472 print "doc/man1/$cmd.pod: Not implemented -$_\n";
482 $opt_n = 1 if $opt_p;
483 $opt_u = 1 if $opt_d;
485 die "Need one of -[cdlnpu] flags.\n"
486 unless $opt_c or $opt_l or $opt_n or $opt_u;
492 # Get list of commands.
493 open FH, "./apps/openssl list -1 -commands|"
494 || die "Can't list commands, $!";
501 # See if each has a manpage.
502 foreach ( @commands ) {
503 next if $_ eq 'help' || $_ eq 'exit';
504 if ( ! -f "doc/man1/$_.pod" ) {
505 print "doc/man1/$_.pod does not exist\n";
508 $ok = 0 if not &checkflags($_);
512 # See what help is missing.
513 open FH, "./apps/openssl list --missing-help |"
514 || die "Can't list missing help, $!";
517 my ($cmd, $flag) = split;
518 print "$cmd has no help for -$flag\n";
527 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
534 &publicize() if $opt_p;
535 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
541 my %temp = &getdocced('doc/man3');
542 foreach ( keys %temp ) {
543 $docced{$_} = $temp{$_};
545 &printem('crypto', 'util/libcrypto.num');
546 &printem('ssl', 'util/libssl.num');