2 # Copyright 2002-2016 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;
34 Find small errors (nits) in documentation. Options:
35 -d Detailed list of undocumented (implies -u)
37 -n Print nits in POD pages
38 -s Also print missing sections in POD pages (implies -n)
39 -p Warn if non-public name documented (implies -n)
40 -u List undocumented functions
41 -h Print this help message
42 -c List undocumented commands and options
47 my $temp = '/tmp/docnits.txt';
51 my %mandatory_sections =
52 ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
53 1 => [ 'SYNOPSIS', 'OPTIONS' ],
54 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
58 # Cross-check functions in the NAME and SYNOPSIS section.
65 # Get NAME section and all words in it.
66 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
69 print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
72 print "$id missing comma in NAME\n" if $tmp =~ /[^,] /;
75 my $dirname = dirname($filename);
76 my $simplename = basename($filename);
77 $simplename =~ s/.pod$//;
78 my $foundfilename = 0;
79 my %foundfilenames = ();
81 foreach my $n ( split ' ', $tmp ) {
83 $foundfilename++ if $n eq $simplename;
84 $foundfilenames{$n} = 1
85 if -f "$dirname/$n.pod" && $n ne $simplename;
87 print "$id the following exist as other .pod files:\n",
88 join(" ", sort keys %foundfilenames), "\n"
90 print "$id $simplename (filename) missing from NAME section\n"
91 unless $foundfilename;
92 foreach my $n ( keys %names ) {
93 print "$id $n is not public\n"
94 if $opt_p and !defined $public{$n};
97 # Find all functions in SYNOPSIS
98 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
100 foreach my $line ( split /\n+/, $syn ) {
102 $line =~ s/STACK_OF\([^)]+\)/int/g;
103 $line =~ s/__declspec\([^)]+\)//;
104 if ( $line =~ /env (\S*)=/ ) {
105 # environment variable env NAME=...
107 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
108 # a callback function pointer: typedef ... (*NAME)(...
110 } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
111 # a callback function signature: typedef ... NAME(...
113 } elsif ( $line =~ /typedef.* (\S+);/ ) {
114 # a simple typedef: typedef ... NAME;
116 } elsif ( $line =~ /enum (\S*) \{/ ) {
117 # an enumeration: enum ... {
119 } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
121 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
127 print "$id $sym missing from NAME section\n"
128 unless defined $names{$sym};
131 # Do some sanity checks on the prototype.
132 print "$id prototype missing spaces around commas: $line\n"
133 if ( $line =~ /[a-z0-9],[^ ]/ );
136 foreach my $n ( keys %names ) {
137 next if $names{$n} == 2;
138 print "$id $n missing from SYNOPSIS\n";
144 my $filename = shift;
145 my $dirname = basename(dirname($filename));
150 open POD, $filename or die "Couldn't open $filename, $!";
155 my $id = "${filename}:1:";
157 &name_synopsis($id, $filename, $contents)
158 unless $contents =~ /=for comment generic/
159 or $filename =~ m@man[157]/@;
161 print "$id doesn't start with =pod\n"
162 if $contents !~ /^=pod/;
163 print "$id doesn't end with =cut\n"
164 if $contents !~ /=cut\n$/;
165 print "$id more than one cut line.\n"
166 if $contents =~ /=cut.*=cut/ms;
167 print "$id missing copyright\n"
168 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
169 print "$id copyright not last\n"
170 if $contents =~ /head1 COPYRIGHT.*=head/ms;
171 print "$id head2 in All uppercase\n"
172 if $contents =~ /head2\s+[A-Z ]+\n/;
173 print "$id extra space after head\n"
174 if $contents =~ /=head\d\s\s+/;
175 print "$id period in NAME section\n"
176 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
177 print "$id POD markup in NAME section\n"
178 if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
179 print "$id Duplicate $1 in L<>\n"
180 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
181 print "$id Bad =over $1\n"
182 if $contents =~ /=over([^ ][^24])/;
183 print "$id Possible version style issue\n"
184 if $contents =~ /OpenSSL version [019]/;
186 if ( $contents !~ /=for comment multiple includes/ ) {
187 # Look for multiple consecutive openssl #include lines
188 # (non-consecutive lines are okay; see man3/MD5.pod).
189 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
191 foreach my $line ( split /\n+/, $1 ) {
192 if ( $line =~ m@include <openssl/@ ) {
193 print "$id has multiple includes\n" if ++$count == 2;
201 open my $OUT, '>', $temp
202 or die "Can't open $temp, $!";
203 podchecker($filename, $OUT);
205 open $OUT, '<', $temp
206 or die "Can't read $temp, $!";
208 next if /\(section\) in.*deprecated/;
212 unlink $temp || warn "Can't remove $temp, $!";
214 # Find what section this page is in; assume 3.
216 $section = $1 if $dirname =~ /man([1-9])/;
218 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
219 # Skip "return values" if not -s
220 next if $_ eq 'RETURN VALUES' and not $opt_s;
221 print "$id: missing $_ head1 section\n"
222 if $contents !~ /^=head1\s+${_}\s*$/m;
233 open my $IN, '<', $file
234 or die "Can't open $file, $!, stopped";
238 next if /\bNOEXIST\b/;
239 next if /\bEXPORT_VAR_AS_FUNC\b/;
240 my @fields = split();
241 die "Malformed line $_"
242 if scalar @fields != 2 && scalar @fields != 4;
243 push @apis, $fields[0];
248 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
257 foreach my $pod ( glob("$dir/*.pod") ) {
258 my %podinfo = extract_pod_info($pod);
259 foreach my $n ( @{$podinfo{names}} ) {
261 print "# Duplicate $n in $pod and $dups{$n}\n"
262 if defined $dups{$n} && $dups{$n} ne $pod;
276 print "# Checking macros (approximate)\n";
277 foreach my $f ( glob('include/openssl/*.h') ) {
278 # Skip some internals we don't want to document yet.
279 next if $f eq 'include/openssl/asn1.h';
280 next if $f eq 'include/openssl/asn1t.h';
281 next if $f eq 'include/openssl/err.h';
282 open(IN, $f) || die "Can't open $f, $!";
284 next unless /^#\s*define\s*(\S+)\(/;
286 next if $docced{$macro};
287 next if $macro =~ /i2d_/
289 || $macro =~ /DEPRECATEDIN/
290 || $macro =~ /IMPLEMENT_/
291 || $macro =~ /DECLARE_/;
292 print "$f:$macro\n" if $opt_d;
297 print "# Found $count macros missing (not all should be documented)\n"
306 foreach my $func ( &parsenum($numfile) ) {
307 next if $docced{$func};
309 # Skip ASN1 utilities
310 next if $func =~ /^ASN1_/;
312 print "$libname:$func\n" if $opt_d;
315 print "# Found $count missing from $numfile\n\n";
319 # Collection of links in each POD file.
320 # filename => [ "foo(1)", "bar(3)", ... ]
321 my %link_collection = ();
322 # Collection of names in each POD file.
323 # "name(s)" => filename
324 my %name_collection = ();
327 my $filename = shift;
328 $filename =~ m|man(\d)/|;
330 my $simplename = basename($filename, ".pod");
331 my $id = "${filename}:1:";
336 open POD, $filename or die "Couldn't open $filename, $!";
341 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
343 unless (defined $tmp) {
344 print "$id weird name section\n";
350 my @names = map { s/\s+//g; $_ } split(/,/, $tmp);
351 unless (grep { $simplename eq $_ } @names) {
352 print "$id missing $simplename\n";
353 push @names, $simplename;
355 foreach my $name (@names) {
357 my $name_sec = "$name($section)";
358 if (! exists $name_collection{$name_sec}) {
359 $name_collection{$name_sec} = $filename;
360 } else { #elsif ($filename ne $name_collection{$name_sec}) {
361 print "$id $name_sec also in $name_collection{$name_sec}\n";
366 map { map { s/\s+//g; $_ } split(/,/, $_) }
367 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
368 foreach (@foreign_names) {
369 $name_collection{$_} = undef; # It still exists!
372 my @links = $contents =~ /L<
373 # if the link is of the form L<something|name(s)>,
374 # then remove 'something'. Note that 'something'
375 # may contain POD codes as well...
376 (?:(?:[^\|]|<[^>]*>)*\|)?
377 # we're only interested in references that have
378 # a one digit section number
381 $link_collection{$filename} = [ @links ];
385 foreach my $filename (sort keys %link_collection) {
386 foreach my $link (@{$link_collection{$filename}}) {
387 print "${filename}:1: reference to non-existing $link\n"
388 unless exists $name_collection{$link};
394 foreach my $name ( &parsenum('util/libcrypto.num') ) {
397 foreach my $name ( &parsenum('util/libssl.num') ) {
400 foreach my $name ( &parsenum('util/private.num') ) {
428 # Get the list of options in the command.
429 open CFH, "./apps/openssl list --options $cmd|"
430 || die "Can list options for $cmd, $!";
438 # Get the list of flags from the synopsis
439 open CFH, "<doc/man1/$cmd.pod"
440 || die "Can't open $cmd.pod, $!";
443 last if /DESCRIPTION/;
444 next unless /\[B<-([^ >]+)/;
449 # See what's in the command not the manpage.
451 foreach my $k ( keys %cmdopts ) {
452 push @undocced, $k unless $docopts{$k};
454 if ( scalar @undocced > 0 ) {
456 foreach ( @undocced ) {
457 print "doc/man1/$cmd.pod: Missing -$_\n";
461 # See what's in the command not the manpage.
463 foreach my $k ( keys %docopts ) {
464 push @unimpl, $k unless $cmdopts{$k};
466 if ( scalar @unimpl > 0 ) {
468 foreach ( @unimpl ) {
469 next if defined $skips{$_};
470 print "doc/man1/$cmd.pod: Not implemented -$_\n";
480 $opt_n = 1 if $opt_s or $opt_p;
481 $opt_u = 1 if $opt_d;
483 die "Need one of -[cdlnspu] flags.\n"
484 unless $opt_c or $opt_l or $opt_n or $opt_u;
490 # Get list of commands.
491 open FH, "./apps/openssl list -1 -commands|"
492 || die "Can't list commands, $!";
499 # See if each has a manpage.
500 foreach ( @commands ) {
501 next if $_ eq 'help' || $_ eq 'exit';
502 if ( ! -f "doc/man1/$_.pod" ) {
503 print "doc/man1/$_.pod does not exist\n";
506 $ok = 0 if not &checkflags($_);
510 # See what help is missing.
511 open FH, "./apps/openssl list --missing-help |"
512 || die "Can't list missing help, $!";
515 my ($cmd, $flag) = split;
516 print "$cmd has no help for -$flag\n";
525 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
532 &publicize() if $opt_p;
533 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
539 my %temp = &getdocced('doc/man3');
540 foreach ( keys %temp ) {
541 $docced{$_} = $temp{$_};
543 &printem('crypto', 'util/libcrypto.num');
544 &printem('ssl', 'util/libssl.num');