2 # Copyright 2002-2019 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";
140 # Check if SECTION is located before BEFORE
141 sub check_section_location()
143 my $filename = shift;
144 my $contents = shift;
148 return unless $contents =~ /=head1 $section/
149 and $contents =~ /=head1 $before/;
150 print "$filename: $section should be placed before $before section\n"
151 if $contents =~ /=head1 $before.*=head1 $section/ms;
156 my $filename = shift;
157 my $dirname = basename(dirname($filename));
162 open POD, $filename or die "Couldn't open $filename, $!";
167 # Check if EXAMPLES is located after RETURN VALUES section.
168 &check_section_location($filename, $contents, "RETURN VALUES", "EXAMPLES") if $filename =~ m|man3/|;
169 # Check if HISTORY is located after SEE ALSO
170 &check_section_location($filename, $contents, "SEE ALSO", "HISTORY") if $filename =~ m|man3/|;
171 # Check if SEE ALSO is located after EXAMPLES
172 &check_section_location($filename, $contents, "EXAMPLES", "SEE ALSO") if $filename =~ m|man3/|;
174 my $id = "${filename}:1:";
176 &name_synopsis($id, $filename, $contents)
177 unless $contents =~ /=for comment generic/
178 or $filename =~ m@man[157]/@;
180 print "$id doesn't start with =pod\n"
181 if $contents !~ /^=pod/;
182 print "$id doesn't end with =cut\n"
183 if $contents !~ /=cut\n$/;
184 print "$id more than one cut line.\n"
185 if $contents =~ /=cut.*=cut/ms;
186 print "$id missing copyright\n"
187 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
188 print "$id copyright not last\n"
189 if $contents =~ /head1 COPYRIGHT.*=head/ms;
190 print "$id head2 in All uppercase\n"
191 if $contents =~ /head2\s+[A-Z ]+\n/;
192 print "$id extra space after head\n"
193 if $contents =~ /=head\d\s\s+/;
194 print "$id period in NAME section\n"
195 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
196 print "$id POD markup in NAME section\n"
197 if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
198 print "$id Duplicate $1 in L<>\n"
199 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
200 print "$id Bad =over $1\n"
201 if $contents =~ /=over([^ ][^24])/;
202 print "$id Possible version style issue\n"
203 if $contents =~ /OpenSSL version [019]/;
205 if ( $contents !~ /=for comment multiple includes/ ) {
206 # Look for multiple consecutive openssl #include lines
207 # (non-consecutive lines are okay; see man3/MD5.pod).
208 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
210 foreach my $line ( split /\n+/, $1 ) {
211 if ( $line =~ m@include <openssl/@ ) {
212 print "$id has multiple includes\n" if ++$count == 2;
220 open my $OUT, '>', $temp
221 or die "Can't open $temp, $!";
222 podchecker($filename, $OUT);
224 open $OUT, '<', $temp
225 or die "Can't read $temp, $!";
227 next if /\(section\) in.*deprecated/;
231 unlink $temp || warn "Can't remove $temp, $!";
233 # Find what section this page is in; assume 3.
235 $section = $1 if $dirname =~ /man([1-9])/;
237 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
238 # Skip "return values" if not -s
239 print "$id: missing $_ head1 section\n"
240 if $contents !~ /^=head1\s+${_}\s*$/m;
251 open my $IN, '<', $file
252 or die "Can't open $file, $!, stopped";
256 next if /\bNOEXIST\b/;
257 next if /\bEXPORT_VAR_AS_FUNC\b/;
258 my @fields = split();
259 die "Malformed line $_"
260 if scalar @fields != 2 && scalar @fields != 4;
261 push @apis, $fields[0];
266 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
275 foreach my $pod ( glob("$dir/*.pod") ) {
276 my %podinfo = extract_pod_info($pod);
277 foreach my $n ( @{$podinfo{names}} ) {
279 print "# Duplicate $n in $pod and $dups{$n}\n"
280 if defined $dups{$n} && $dups{$n} ne $pod;
294 print "# Checking macros (approximate)\n";
295 foreach my $f ( glob('include/openssl/*.h') ) {
296 # Skip some internals we don't want to document yet.
297 next if $f eq 'include/openssl/asn1.h';
298 next if $f eq 'include/openssl/asn1t.h';
299 next if $f eq 'include/openssl/err.h';
300 open(IN, $f) || die "Can't open $f, $!";
302 next unless /^#\s*define\s*(\S+)\(/;
304 next if $docced{$macro};
305 next if $macro =~ /i2d_/
307 || $macro =~ /DEPRECATEDIN/
308 || $macro =~ /IMPLEMENT_/
309 || $macro =~ /DECLARE_/;
310 print "$f:$macro\n" if $opt_d;
315 print "# Found $count macros missing (not all should be documented)\n"
324 foreach my $func ( &parsenum($numfile) ) {
325 next if $docced{$func};
327 # Skip ASN1 utilities
328 next if $func =~ /^ASN1_/;
330 print "$libname:$func\n" if $opt_d;
333 print "# Found $count missing from $numfile\n\n";
337 # Collection of links in each POD file.
338 # filename => [ "foo(1)", "bar(3)", ... ]
339 my %link_collection = ();
340 # Collection of names in each POD file.
341 # "name(s)" => filename
342 my %name_collection = ();
345 my $filename = shift;
346 $filename =~ m|man(\d)/|;
348 my $simplename = basename($filename, ".pod");
349 my $id = "${filename}:1:";
354 open POD, $filename or die "Couldn't open $filename, $!";
359 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
361 unless (defined $tmp) {
362 print "$id weird name section\n";
368 my @names = map { s/\s+//g; $_ } split(/,/, $tmp);
369 unless (grep { $simplename eq $_ } @names) {
370 print "$id missing $simplename\n";
371 push @names, $simplename;
373 foreach my $name (@names) {
375 my $name_sec = "$name($section)";
376 if (! exists $name_collection{$name_sec}) {
377 $name_collection{$name_sec} = $filename;
378 } else { #elsif ($filename ne $name_collection{$name_sec}) {
379 print "$id $name_sec also in $name_collection{$name_sec}\n";
384 map { map { s/\s+//g; $_ } split(/,/, $_) }
385 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
386 foreach (@foreign_names) {
387 $name_collection{$_} = undef; # It still exists!
390 my @links = $contents =~ /L<
391 # if the link is of the form L<something|name(s)>,
392 # then remove 'something'. Note that 'something'
393 # may contain POD codes as well...
394 (?:(?:[^\|]|<[^>]*>)*\|)?
395 # we're only interested in references that have
396 # a one digit section number
399 $link_collection{$filename} = [ @links ];
403 foreach my $filename (sort keys %link_collection) {
404 foreach my $link (@{$link_collection{$filename}}) {
405 print "${filename}:1: reference to non-existing $link\n"
406 unless exists $name_collection{$link};
412 foreach my $name ( &parsenum('util/libcrypto.num') ) {
415 foreach my $name ( &parsenum('util/libssl.num') ) {
418 foreach my $name ( &parsenum('util/private.num') ) {
446 # Get the list of options in the command.
447 open CFH, "./apps/openssl list --options $cmd|"
448 || die "Can list options for $cmd, $!";
456 # Get the list of flags from the synopsis
457 open CFH, "<doc/man1/$cmd.pod"
458 || die "Can't open $cmd.pod, $!";
461 last if /DESCRIPTION/;
462 next unless /\[B<-([^ >]+)/;
467 # See what's in the command not the manpage.
469 foreach my $k ( keys %cmdopts ) {
470 push @undocced, $k unless $docopts{$k};
472 if ( scalar @undocced > 0 ) {
474 foreach ( @undocced ) {
475 print "doc/man1/$cmd.pod: Missing -$_\n";
479 # See what's in the command not the manpage.
481 foreach my $k ( keys %docopts ) {
482 push @unimpl, $k unless $cmdopts{$k};
484 if ( scalar @unimpl > 0 ) {
486 foreach ( @unimpl ) {
487 next if defined $skips{$_};
488 print "doc/man1/$cmd.pod: Not implemented -$_\n";
498 $opt_n = 1 if $opt_p;
499 $opt_u = 1 if $opt_d;
501 die "Need one of -[cdlnpu] flags.\n"
502 unless $opt_c or $opt_l or $opt_n or $opt_u;
508 # Get list of commands.
509 open FH, "./apps/openssl list -1 -commands|"
510 || die "Can't list commands, $!";
517 # See if each has a manpage.
518 foreach ( @commands ) {
519 next if $_ eq 'help' || $_ eq 'exit';
520 if ( ! -f "doc/man1/$_.pod" ) {
521 print "doc/man1/$_.pod does not exist\n";
524 $ok = 0 if not &checkflags($_);
528 # See what help is missing.
529 open FH, "./apps/openssl list --missing-help |"
530 || die "Can't list missing help, $!";
533 my ($cmd, $flag) = split;
534 print "$cmd has no help for -$flag\n";
543 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
550 &publicize() if $opt_p;
551 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
557 my %temp = &getdocced('doc/man3');
558 foreach ( keys %temp ) {
559 $docced{$_} = $temp{$_};
561 &printem('crypto', 'util/libcrypto.num');
562 &printem('ssl', 'util/libssl.num');