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 =~ /[^,] /;
72 my $dirname = dirname($filename);
73 my $simplename = basename($filename);
74 $simplename =~ s/.pod$//;
75 my $foundfilename = 0;
76 my %foundfilenames = ();
78 foreach my $n ( split ',', $tmp ) {
81 print "$id the name '$n' contains white-space\n"
84 $foundfilename++ if $n eq $simplename;
85 $foundfilenames{$n} = 1
86 if -f "$dirname/$n.pod" && $n ne $simplename;
88 print "$id the following exist as other .pod files:\n",
89 join(" ", sort keys %foundfilenames), "\n"
91 print "$id $simplename (filename) missing from NAME section\n"
92 unless $foundfilename;
93 foreach my $n ( keys %names ) {
94 print "$id $n is not public\n"
95 if $opt_p and !defined $public{$n};
98 # Find all functions in SYNOPSIS
99 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
101 foreach my $line ( split /\n+/, $syn ) {
102 next unless $line =~ /^\s/;
104 $line =~ s/STACK_OF\([^)]+\)/int/g;
105 $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g;
106 $line =~ s/__declspec\([^)]+\)//;
107 if ( $line =~ /env (\S*)=/ ) {
108 # environment variable env NAME=...
110 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
111 # a callback function pointer: typedef ... (*NAME)(...
113 } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
114 # a callback function signature: typedef ... NAME(...
116 } elsif ( $line =~ /typedef.* (\S+);/ ) {
117 # a simple typedef: typedef ... NAME;
119 } elsif ( $line =~ /enum (\S*) \{/ ) {
120 # an enumeration: enum ... {
122 } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) {
124 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
130 print "$id $sym missing from NAME section\n"
131 unless defined $names{$sym};
134 # Do some sanity checks on the prototype.
135 print "$id prototype missing spaces around commas: $line\n"
136 if ( $line =~ /[a-z0-9],[^ ]/ );
139 foreach my $n ( keys %names ) {
140 next if $names{$n} == 2;
141 print "$id $n missing from SYNOPSIS\n";
145 # Check if EXAMPLES is located after RETURN VALUES section.
146 sub check_example_location()
148 my $filename = shift;
149 my $contents = shift;
151 return unless $contents =~ /=head1 RETURN VALUES/
152 and $contents =~ /=head1 EXAMPLES/;
153 print "$filename: RETURN VAULES should be placed before EXAMPLES section\n"
154 if $contents =~ /=head1 EXAMPLES.*=head1 RETURN VALUES/ms;
159 my $filename = shift;
160 my $dirname = basename(dirname($filename));
165 open POD, $filename or die "Couldn't open $filename, $!";
170 &check_example_location($filename, $contents) if $filename =~ m|man3/|;
172 my $id = "${filename}:1:";
174 &name_synopsis($id, $filename, $contents)
175 unless $contents =~ /=for comment generic/
176 or $filename =~ m@man[157]/@;
178 print "$id doesn't start with =pod\n"
179 if $contents !~ /^=pod/;
180 print "$id doesn't end with =cut\n"
181 if $contents !~ /=cut\n$/;
182 print "$id more than one cut line.\n"
183 if $contents =~ /=cut.*=cut/ms;
184 print "$id missing copyright\n"
185 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
186 print "$id copyright not last\n"
187 if $contents =~ /head1 COPYRIGHT.*=head/ms;
188 print "$id head2 in All uppercase\n"
189 if $contents =~ /head2\s+[A-Z ]+\n/;
190 print "$id extra space after head\n"
191 if $contents =~ /=head\d\s\s+/;
192 print "$id period in NAME section\n"
193 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
194 print "$id POD markup in NAME section\n"
195 if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
196 print "$id Duplicate $1 in L<>\n"
197 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
198 print "$id Bad =over $1\n"
199 if $contents =~ /=over([^ ][^24])/;
200 print "$id Possible version style issue\n"
201 if $contents =~ /OpenSSL version [019]/;
203 if ( $contents !~ /=for comment multiple includes/ ) {
204 # Look for multiple consecutive openssl #include lines
205 # (non-consecutive lines are okay; see man3/MD5.pod).
206 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
208 foreach my $line ( split /\n+/, $1 ) {
209 if ( $line =~ m@include <openssl/@ ) {
210 print "$id has multiple includes\n" if ++$count == 2;
218 open my $OUT, '>', $temp
219 or die "Can't open $temp, $!";
220 podchecker($filename, $OUT);
222 open $OUT, '<', $temp
223 or die "Can't read $temp, $!";
225 next if /\(section\) in.*deprecated/;
229 unlink $temp || warn "Can't remove $temp, $!";
231 # Find what section this page is in; assume 3.
233 $section = $1 if $dirname =~ /man([1-9])/;
235 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
236 # Skip "return values" if not -s
237 print "$id: missing $_ head1 section\n"
238 if $contents !~ /^=head1\s+${_}\s*$/m;
249 open my $IN, '<', $file
250 or die "Can't open $file, $!, stopped";
254 next if /\bNOEXIST\b/;
255 next if /\bEXPORT_VAR_AS_FUNC\b/;
256 my @fields = split();
257 die "Malformed line $_"
258 if scalar @fields != 2 && scalar @fields != 4;
259 push @apis, $fields[0];
264 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
273 foreach my $pod ( glob("$dir/*.pod") ) {
274 my %podinfo = extract_pod_info($pod);
275 foreach my $n ( @{$podinfo{names}} ) {
277 print "# Duplicate $n in $pod and $dups{$n}\n"
278 if defined $dups{$n} && $dups{$n} ne $pod;
293 print "# Checking macros (approximate)\n";
294 foreach my $f ( glob('include/openssl/*.h') ) {
295 # Skip some internals we don't want to document yet.
296 next if $f eq 'include/openssl/asn1.h';
297 next if $f eq 'include/openssl/asn1t.h';
298 next if $f eq 'include/openssl/err.h';
299 open(IN, $f) || die "Can't open $f, $!";
301 next unless /^#\s*define\s*(\S+)\(/;
303 next if $docced{$macro} || defined $seen{$macro};
304 next if $macro =~ /i2d_/
306 || $macro =~ /DEPRECATEDIN/
307 || $macro =~ /IMPLEMENT_/
308 || $macro =~ /DECLARE_/;
309 print "$f:$macro\n" if $opt_d;
315 print "# Found $count macros missing (not all should be documented)\n"
325 foreach my $func ( &parsenum($numfile) ) {
326 next if $docced{$func} || defined $seen{$func};
328 # Skip ASN1 utilities
329 next if $func =~ /^ASN1_/;
331 print "$libname:$func\n" if $opt_d;
335 print "# Found $count missing from $numfile\n\n";
339 # Collection of links in each POD file.
340 # filename => [ "foo(1)", "bar(3)", ... ]
341 my %link_collection = ();
342 # Collection of names in each POD file.
343 # "name(s)" => filename
344 my %name_collection = ();
347 my $filename = shift;
348 $filename =~ m|man(\d)/|;
350 my $simplename = basename($filename, ".pod");
351 my $id = "${filename}:1:";
356 open POD, $filename or die "Couldn't open $filename, $!";
361 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
363 unless (defined $tmp) {
364 print "$id weird name section\n";
370 my @names = map { s/^\s+//g; s/\s+$//g; $_ } split(/,/, $tmp);
371 unless (grep { $simplename eq $_ } @names) {
372 print "$id missing $simplename\n";
373 push @names, $simplename;
375 foreach my $name (@names) {
378 print "$id '$name' contains white space\n";
380 my $name_sec = "$name($section)";
381 if (! exists $name_collection{$name_sec}) {
382 $name_collection{$name_sec} = $filename;
383 } else { #elsif ($filename ne $name_collection{$name_sec}) {
384 print "$id $name_sec also in $name_collection{$name_sec}\n";
389 map { map { s/\s+//g; $_ } split(/,/, $_) }
390 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
391 foreach (@foreign_names) {
392 $name_collection{$_} = undef; # It still exists!
395 my @links = $contents =~ /L<
396 # if the link is of the form L<something|name(s)>,
397 # then remove 'something'. Note that 'something'
398 # may contain POD codes as well...
399 (?:(?:[^\|]|<[^>]*>)*\|)?
400 # we're only interested in references that have
401 # a one digit section number
404 $link_collection{$filename} = [ @links ];
408 foreach my $filename (sort keys %link_collection) {
409 foreach my $link (@{$link_collection{$filename}}) {
410 print "${filename}:1: reference to non-existing $link\n"
411 unless exists $name_collection{$link};
417 foreach my $name ( &parsenum('util/libcrypto.num') ) {
420 foreach my $name ( &parsenum('util/libssl.num') ) {
423 foreach my $name ( &parsenum('util/private.num') ) {
451 # Get the list of options in the command.
452 open CFH, "./apps/openssl list --options $cmd|"
453 || die "Can list options for $cmd, $!";
461 # Get the list of flags from the synopsis
462 open CFH, "<doc/man1/$cmd.pod"
463 || die "Can't open $cmd.pod, $!";
466 last if /DESCRIPTION/;
467 next unless /\[B<-([^ >]+)/;
472 # See what's in the command not the manpage.
474 foreach my $k ( keys %cmdopts ) {
475 push @undocced, $k unless $docopts{$k};
477 if ( scalar @undocced > 0 ) {
479 foreach ( @undocced ) {
480 print "doc/man1/$cmd.pod: Missing -$_\n";
484 # See what's in the command not the manpage.
486 foreach my $k ( keys %docopts ) {
487 push @unimpl, $k unless $cmdopts{$k};
489 if ( scalar @unimpl > 0 ) {
491 foreach ( @unimpl ) {
492 next if defined $skips{$_};
493 print "doc/man1/$cmd.pod: Not implemented -$_\n";
503 $opt_n = 1 if $opt_p;
504 $opt_u = 1 if $opt_d;
506 die "Need one of -[cdlnpu] flags.\n"
507 unless $opt_c or $opt_l or $opt_n or $opt_u;
513 # Get list of commands.
514 open FH, "./apps/openssl list -1 -commands|"
515 || die "Can't list commands, $!";
522 # See if each has a manpage.
523 foreach ( @commands ) {
524 next if $_ eq 'help' || $_ eq 'exit';
525 if ( ! -f "doc/man1/$_.pod" ) {
526 print "doc/man1/$_.pod does not exist\n";
529 $ok = 0 if not &checkflags($_);
533 # See what help is missing.
534 open FH, "./apps/openssl list --missing-help |"
535 || die "Can't list missing help, $!";
538 my ($cmd, $flag) = split;
539 print "$cmd has no help for -$flag\n";
548 foreach (@ARGV ? @ARGV : (glob('doc/*/*.pod'),
549 glob('doc/internal/*/*.pod'))) {
556 &publicize() if $opt_p;
557 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
561 local $opt_p = undef;
562 foreach (@ARGV ? @ARGV : glob('doc/internal/*/*.pod')) {
569 my %temp = getdocced('doc/man3');
570 foreach ( keys %temp ) {
571 $docced{$_} = $temp{$_};
573 &printem('crypto', 'util/libcrypto.num');
574 &printem('ssl', 'util/libssl.num');