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;
32 Find small errors (nits) in documentation. Options:
34 -n Print nits in POD pages
35 -s Also print missing sections in POD pages (implies -n)
36 -p Warn if non-public name documented (implies -n)
37 -u List undocumented functions
38 -h Print this help message
43 my $temp = '/tmp/docnits.txt';
47 my %mandatory_sections =
48 ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
49 1 => [ 'SYNOPSIS', 'OPTIONS' ],
50 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
54 # Cross-check functions in the NAME and SYNOPSIS section.
61 # Get NAME section and all words in it.
62 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
65 print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
68 print "$id missing comma in NAME\n" if $tmp =~ /[^,] /;
71 my $dirname = dirname($filename);
72 my $simplename = basename($filename);
73 $simplename =~ s/.pod$//;
74 my $foundfilename = 0;
75 my %foundfilenames = ();
77 foreach my $n ( split ' ', $tmp ) {
79 $foundfilename++ if $n eq $simplename;
80 $foundfilenames{$n} = 1
81 if -f "$dirname/$n.pod" && $n ne $simplename;
83 print "$id the following exist as other .pod files:\n",
84 join(" ", sort keys %foundfilenames), "\n"
86 print "$id $simplename (filename) missing from NAME section\n"
87 unless $foundfilename;
88 print "$id $simplename is not public\n"
89 if $opt_p and !defined $public{$simplename};
91 # Find all functions in SYNOPSIS
92 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
94 foreach my $line ( split /\n+/, $syn ) {
96 $line =~ s/STACK_OF\([^)]+\)/int/g;
97 $line =~ s/__declspec\([^)]+\)//;
98 if ( $line =~ /env (\S*)=/ ) {
99 # environment variable env NAME=...
101 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
102 # a callback function: typedef ... (*NAME)(...
104 } elsif ( $line =~ /typedef.* (\S+);/ ) {
105 # a simple typedef: typedef ... NAME;
107 } elsif ( $line =~ /enum (\S*) \{/ ) {
108 # an enumeration: enum ... {
110 } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
112 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
118 print "$id $sym missing from NAME section\n"
119 unless defined $names{$sym};
122 # Do some sanity checks on the prototype.
123 print "$id prototype missing spaces around commas: $line\n"
124 if ( $line =~ /[a-z0-9],[^ ]/ );
127 foreach my $n ( keys %names ) {
128 next if $names{$n} == 2;
129 print "$id $n missing from SYNOPSIS\n";
135 my $filename = shift;
136 my $dirname = basename(dirname($filename));
141 open POD, $filename or die "Couldn't open $filename, $!";
146 my $id = "${filename}:1:";
148 &name_synopsis($id, $filename, $contents)
149 unless $contents =~ /=for comment generic/
150 or $filename =~ m@man[157]/@;
152 print "$id doesn't start with =pod\n"
153 if $contents !~ /^=pod/;
154 print "$id doesn't end with =cut\n"
155 if $contents !~ /=cut\n$/;
156 print "$id more than one cut line.\n"
157 if $contents =~ /=cut.*=cut/ms;
158 print "$id missing copyright\n"
159 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
160 print "$id copyright not last\n"
161 if $contents =~ /head1 COPYRIGHT.*=head/ms;
162 print "$id head2 in All uppercase\n"
163 if $contents =~ /head2\s+[A-Z ]+\n/;
164 print "$id extra space after head\n"
165 if $contents =~ /=head\d\s\s+/;
166 print "$id period in NAME section\n"
167 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
168 print "$id POD markup in NAME section\n"
169 if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
170 print "$id Duplicate $1 in L<>\n"
171 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
172 print "$id Bad =over $1\n"
173 if $contents =~ /=over([^ ][^24])/;
175 # Look for multiple consecutive openssl #include lines.
176 # Consecutive because of files like md5.pod. Sometimes it's okay
177 # or necessary, as in ssl/SSL_set1_host.pod
178 if ( $contents !~ /=for comment multiple includes/ ) {
179 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
181 foreach my $line ( split /\n+/, $1 ) {
182 if ( $line =~ m@include <openssl/@ ) {
183 if ( ++$count == 2 ) {
184 print "$id has multiple includes\n";
193 return unless $opt_s;
195 # Find what section this page is in. If run from "." assume
198 $section = $1 if $dirname =~ /man([1-9])/;
200 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
201 print "$id: missing $_ head1 section\n"
202 if $contents !~ /^=head1\s+${_}\s*$/m;
205 open my $OUT, '>', $temp
206 or die "Can't open $temp, $!";
207 podchecker($filename, $OUT);
209 open $OUT, '<', $temp
210 or die "Can't read $temp, $!";
212 next if /\(section\) in.*deprecated/;
216 unlink $temp || warn "Can't remove $temp, $!";
226 open my $IN, '<', $file
227 or die "Can't open $file, $!, stopped";
231 next if /\bNOEXIST\b/;
232 next if /\bEXPORT_VAR_AS_FUNC\b/;
233 push @apis, $1 if /([^\s]+).\s/;
238 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
247 foreach my $pod ( glob("$dir/*.pod") ) {
248 my %podinfo = extract_pod_info($pod);
249 foreach my $n ( @{$podinfo{names}} ) {
251 print "# Duplicate $n in $pod and $dups{$n}\n"
252 if defined $dups{$n} && $dups{$n} ne $pod;
268 foreach my $func ( &parsenum($numfile) ) {
269 next if $docced{$func};
271 # Skip ASN1 utilities
272 next if $func =~ /^ASN1_/;
274 print "$libname:$func\n";
277 print "# Found $count missing from $numfile\n\n";
281 # Collection of links in each POD file.
282 # filename => [ "foo(1)", "bar(3)", ... ]
283 my %link_collection = ();
284 # Collection of names in each POD file.
285 # "name(s)" => filename
286 my %name_collection = ();
289 my $filename = shift;
290 $filename =~ m|man(\d)/|;
292 my $simplename = basename($filename, ".pod");
293 my $id = "${filename}:1:";
298 open POD, $filename or die "Couldn't open $filename, $!";
303 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
305 unless (defined $tmp) {
306 print "$id weird name section\n";
312 my @names = map { s/\s+//g; $_ } split(/,/, $tmp);
313 unless (grep { $simplename eq $_ } @names) {
314 print "$id missing $simplename\n";
315 push @names, $simplename;
317 foreach my $name (@names) {
319 my $name_sec = "$name($section)";
320 if (! exists $name_collection{$name_sec}) {
321 $name_collection{$name_sec} = $filename;
322 } else { #elsif ($filename ne $name_collection{$name_sec}) {
323 print "$id $name_sec also in $name_collection{$name_sec}\n";
328 map { map { s/\s+//g; $_ } split(/,/, $_) }
329 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
330 foreach (@foreign_names) {
331 $name_collection{$_} = undef; # It still exists!
334 my @links = $contents =~ /L<
335 # if the link is of the form L<something|name(s)>,
336 # then remove 'something'. Note that 'something'
337 # may contain POD codes as well...
338 (?:(?:[^\|]|<[^>]*>)*\|)?
339 # we're only interested in referenses that have
340 # a one digit section number
343 $link_collection{$filename} = [ @links ];
347 foreach my $filename (sort keys %link_collection) {
348 foreach my $link (@{$link_collection{$filename}}) {
349 print "${filename}:1: reference to non-existing $link\n"
350 unless exists $name_collection{$link};
356 foreach my $name ( &parsenum('util/libcrypto.num') ) {
359 foreach my $name ( &parsenum('util/libssl.num') ) {
362 foreach my $name ( &parsenum('util/private.num') ) {
371 die "Need one of -l -n -s -p or -u flags.\n"
372 unless $opt_l or $opt_n or $opt_s or $opt_p or $opt_u;
374 $opt_n = 1 if $opt_s or $opt_p;
377 &publicize() if $opt_p;
378 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
384 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
391 my %temp = &getdocced('doc/man3');
392 foreach ( keys %temp ) {
393 $docced{$_} = $temp{$_};
395 &printem('crypto', 'util/libcrypto.num');
396 &printem('ssl', 'util/libssl.num');