2 # Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (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 List 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 ) {
100 $line =~ s/STACK_OF\([^)]+\)/int/g;
101 $line =~ s/__declspec\([^)]+\)//;
102 if ( $line =~ /env (\S*)=/ ) {
103 # environment variable env NAME=...
105 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
106 # a callback function pointer: typedef ... (*NAME)(...
108 } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
109 # a callback function signature: typedef ... NAME(...
111 } elsif ( $line =~ /typedef.* (\S+);/ ) {
112 # a simple typedef: typedef ... NAME;
114 } elsif ( $line =~ /enum (\S*) \{/ ) {
115 # an enumeration: enum ... {
117 } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
119 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
125 print "$id $sym missing from NAME section\n"
126 unless defined $names{$sym};
129 # Do some sanity checks on the prototype.
130 print "$id prototype missing spaces around commas: $line\n"
131 if ( $line =~ /[a-z0-9],[^ ]/ );
134 foreach my $n ( keys %names ) {
135 next if $names{$n} == 2;
136 print "$id $n missing from SYNOPSIS\n";
142 my $filename = shift;
143 my $dirname = basename(dirname($filename));
148 open POD, $filename or die "Couldn't open $filename, $!";
153 my $id = "${filename}:1:";
155 &name_synopsis($id, $filename, $contents)
156 unless $contents =~ /=for comment generic/
157 or $filename =~ m@man[157]/@;
159 print "$id doesn't start with =pod\n"
160 if $contents !~ /^=pod/;
161 print "$id doesn't end with =cut\n"
162 if $contents !~ /=cut\n$/;
163 print "$id more than one cut line.\n"
164 if $contents =~ /=cut.*=cut/ms;
165 print "$id missing copyright\n"
166 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
167 print "$id copyright not last\n"
168 if $contents =~ /head1 COPYRIGHT.*=head/ms;
169 print "$id head2 in All uppercase\n"
170 if $contents =~ /head2\s+[A-Z ]+\n/;
171 print "$id extra space after head\n"
172 if $contents =~ /=head\d\s\s+/;
173 print "$id period in NAME section\n"
174 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
175 print "$id POD markup in NAME section\n"
176 if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
177 print "$id Duplicate $1 in L<>\n"
178 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
179 print "$id Bad =over $1\n"
180 if $contents =~ /=over([^ ][^24])/;
181 print "$id Possible version style issue\n"
182 if $contents =~ /OpenSSL version [019]/;
184 if ( $contents !~ /=for comment multiple includes/ ) {
185 # Look for multiple consecutive openssl #include lines
186 # (non-consecutive lines are okay; see man3/MD5.pod).
187 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
189 foreach my $line ( split /\n+/, $1 ) {
190 if ( $line =~ m@include <openssl/@ ) {
191 print "$id has multiple includes\n" if ++$count == 2;
199 open my $OUT, '>', $temp
200 or die "Can't open $temp, $!";
201 podchecker($filename, $OUT);
203 open $OUT, '<', $temp
204 or die "Can't read $temp, $!";
206 next if /\(section\) in.*deprecated/;
210 unlink $temp || warn "Can't remove $temp, $!";
212 # Find what section this page is in; assume 3.
214 $section = $1 if $dirname =~ /man([1-9])/;
216 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
217 # Skip "return values" if not -s
218 print "$id: missing $_ head1 section\n"
219 if $contents !~ /^=head1\s+${_}\s*$/m;
230 open my $IN, '<', $file
231 or die "Can't open $file, $!, stopped";
235 next if /\bNOEXIST\b/;
236 next if /\bEXPORT_VAR_AS_FUNC\b/;
237 my @fields = split();
238 die "Malformed line $_"
239 if scalar @fields != 2 && scalar @fields != 4;
240 push @apis, $fields[0];
245 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
254 foreach my $pod ( glob("$dir/*.pod") ) {
255 my %podinfo = extract_pod_info($pod);
256 foreach my $n ( @{$podinfo{names}} ) {
258 print "# Duplicate $n in $pod and $dups{$n}\n"
259 if defined $dups{$n} && $dups{$n} ne $pod;
273 print "# Checking macros (approximate)\n";
274 foreach my $f ( glob('include/openssl/*.h') ) {
275 # Skip some internals we don't want to document yet.
276 next if $f eq 'include/openssl/asn1.h';
277 next if $f eq 'include/openssl/asn1t.h';
278 next if $f eq 'include/openssl/err.h';
279 open(IN, $f) || die "Can't open $f, $!";
281 next unless /^#\s*define\s*(\S+)\(/;
283 next if $docced{$macro};
284 next if $macro =~ /i2d_/
286 || $macro =~ /DEPRECATEDIN/
287 || $macro =~ /IMPLEMENT_/
288 || $macro =~ /DECLARE_/;
289 print "$f:$macro\n" if $opt_d;
294 print "# Found $count macros missing (not all should be documented)\n"
303 foreach my $func ( &parsenum($numfile) ) {
304 next if $docced{$func};
306 # Skip ASN1 utilities
307 next if $func =~ /^ASN1_/;
309 print "$libname:$func\n" if $opt_d;
312 print "# Found $count missing from $numfile\n\n";
316 # Collection of links in each POD file.
317 # filename => [ "foo(1)", "bar(3)", ... ]
318 my %link_collection = ();
319 # Collection of names in each POD file.
320 # "name(s)" => filename
321 my %name_collection = ();
324 my $filename = shift;
325 $filename =~ m|man(\d)/|;
327 my $simplename = basename($filename, ".pod");
328 my $id = "${filename}:1:";
333 open POD, $filename or die "Couldn't open $filename, $!";
338 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
340 unless (defined $tmp) {
341 print "$id weird name section\n";
347 my @names = map { s/\s+//g; $_ } split(/,/, $tmp);
348 unless (grep { $simplename eq $_ } @names) {
349 print "$id missing $simplename\n";
350 push @names, $simplename;
352 foreach my $name (@names) {
354 my $name_sec = "$name($section)";
355 if (! exists $name_collection{$name_sec}) {
356 $name_collection{$name_sec} = $filename;
357 } else { #elsif ($filename ne $name_collection{$name_sec}) {
358 print "$id $name_sec also in $name_collection{$name_sec}\n";
363 map { map { s/\s+//g; $_ } split(/,/, $_) }
364 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
365 foreach (@foreign_names) {
366 $name_collection{$_} = undef; # It still exists!
369 my @links = $contents =~ /L<
370 # if the link is of the form L<something|name(s)>,
371 # then remove 'something'. Note that 'something'
372 # may contain POD codes as well...
373 (?:(?:[^\|]|<[^>]*>)*\|)?
374 # we're only interested in references that have
375 # a one digit section number
378 $link_collection{$filename} = [ @links ];
382 foreach my $filename (sort keys %link_collection) {
383 foreach my $link (@{$link_collection{$filename}}) {
384 print "${filename}:1: reference to non-existing $link\n"
385 unless exists $name_collection{$link};
391 foreach my $name ( &parsenum('util/libcrypto.num') ) {
394 foreach my $name ( &parsenum('util/libssl.num') ) {
397 foreach my $name ( &parsenum('util/private.num') ) {
425 # Get the list of options in the command.
426 open CFH, "./apps/openssl list --options $cmd|"
427 || die "Can list options for $cmd, $!";
435 # Get the list of flags from the synopsis
436 open CFH, "<doc/man1/$cmd.pod"
437 || die "Can't open $cmd.pod, $!";
440 last if /DESCRIPTION/;
441 next unless /\[B<-([^ >]+)/;
446 # See what's in the command not the manpage.
448 foreach my $k ( keys %cmdopts ) {
449 push @undocced, $k unless $docopts{$k};
451 if ( scalar @undocced > 0 ) {
453 foreach ( @undocced ) {
454 print "doc/man1/$cmd.pod: Missing -$_\n";
458 # See what's in the command not the manpage.
460 foreach my $k ( keys %docopts ) {
461 push @unimpl, $k unless $cmdopts{$k};
463 if ( scalar @unimpl > 0 ) {
465 foreach ( @unimpl ) {
466 next if defined $skips{$_};
467 print "doc/man1/$cmd.pod: Not implemented -$_\n";
477 $opt_n = 1 if $opt_p;
478 $opt_u = 1 if $opt_d;
480 die "Need one of -[cdlnpu] flags.\n"
481 unless $opt_c or $opt_l or $opt_n or $opt_u;
487 # Get list of commands.
488 open FH, "./apps/openssl list -1 -commands|"
489 || die "Can't list commands, $!";
496 # See if each has a manpage.
497 foreach ( @commands ) {
498 next if $_ eq 'help' || $_ eq 'exit';
499 if ( ! -f "doc/man1/$_.pod" ) {
500 print "doc/man1/$_.pod does not exist\n";
503 $ok = 0 if not &checkflags($_);
507 # See what help is missing.
508 open FH, "./apps/openssl list --missing-help |"
509 || die "Can't list missing help, $!";
512 my ($cmd, $flag) = split;
513 print "$cmd has no help for -$flag\n";
522 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
529 &publicize() if $opt_p;
530 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
536 my %temp = &getdocced('doc/man3');
537 foreach ( keys %temp ) {
538 $docced{$_} = $temp{$_};
540 &printem('crypto', 'util/libcrypto.num');
541 &printem('ssl', 'util/libssl.num');