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;
31 Find small errors (nits) in documentation. Options:
33 -n Print nits in POD pages
34 -s Also print missing sections in POD pages (implies -n)
35 -u List undocumented functions
36 -h Print this help message
41 my $temp = '/tmp/docnits.txt';
44 my %mandatory_sections =
45 ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
46 1 => [ 'SYNOPSIS', 'OPTIONS' ],
47 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
51 # Cross-check functions in the NAME and SYNOPSIS section.
58 # Get NAME section and all words in it.
59 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
62 print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
66 my $dirname = dirname($filename);
67 my $simplename = basename($filename);
68 $simplename =~ s/.pod$//;
69 my $foundfilename = 0;
70 my %foundfilenames = ();
72 foreach my $n ( split ' ', $tmp ) {
74 $foundfilename++ if $n eq $simplename;
75 $foundfilenames{$n} = 1
76 if -f "$dirname/$n.pod" && $n ne $simplename;
78 print "$id the following exist as other .pod files:\n",
79 join(" ", sort keys %foundfilenames), "\n"
81 print "$id $simplename (filename) missing from NAME section\n",
82 unless $foundfilename;
84 # Find all functions in SYNOPSIS
85 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
87 foreach my $line ( split /\n+/, $syn ) {
89 $line =~ s/STACK_OF\([^)]+\)/int/g;
90 $line =~ s/__declspec\([^)]+\)//;
91 if ( $line =~ /env (\S*)=/ ) {
92 # environment variable env NAME=...
94 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
95 # a callback function: typedef ... (*NAME)(...
97 } elsif ( $line =~ /typedef.* (\S+);/ ) {
98 # a simple typedef: typedef ... NAME;
100 } elsif ( $line =~ /enum (\S*) \{/ ) {
101 # an enumeration: enum ... {
103 } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
105 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
111 print "$id $sym missing from NAME section\n"
112 unless defined $names{$sym};
115 # Do some sanity checks on the prototype.
116 print "$id prototype missing spaces around commas: $line\n"
117 if ( $line =~ /[a-z0-9],[^ ]/ );
120 foreach my $n ( keys %names ) {
121 next if $names{$n} == 2;
122 print "$id $n missing from SYNOPSIS\n";
128 my $filename = shift;
129 my $dirname = basename(dirname($filename));
134 open POD, $filename or die "Couldn't open $filename, $!";
139 my $id = "${filename}:1:";
141 &name_synopsis($id, $filename, $contents)
142 unless $contents =~ /=for comment generic/
143 or $filename =~ m@man[157]/@;
145 print "$id doesn't start with =pod\n"
146 if $contents !~ /^=pod/;
147 print "$id doesn't end with =cut\n"
148 if $contents !~ /=cut\n$/;
149 print "$id more than one cut line.\n"
150 if $contents =~ /=cut.*=cut/ms;
151 print "$id missing copyright\n"
152 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
153 print "$id copyright not last\n"
154 if $contents =~ /head1 COPYRIGHT.*=head/ms;
155 print "$id head2 in All uppercase\n"
156 if $contents =~ /head2\s+[A-Z ]+\n/;
157 print "$id extra space after head\n"
158 if $contents =~ /=head\d\s\s+/;
159 print "$id period in NAME section\n"
160 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
161 print "$id POD markup in NAME section\n"
162 if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
163 print "$id Duplicate $1 in L<>\n"
164 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
165 print "$id Bad =over $1\n"
166 if $contents =~ /=over([^ ][^24])/;
168 # Look for multiple consecutive openssl #include lines.
169 # Consecutive because of files like md5.pod. Sometimes it's okay
170 # or necessary, as in ssl/SSL_set1_host.pod
171 if ( $contents !~ /=for comment multiple includes/ ) {
172 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
174 foreach my $line ( split /\n+/, $1 ) {
175 if ( $line =~ m@include <openssl/@ ) {
176 if ( ++$count == 2 ) {
177 print "$id has multiple includes\n";
186 return unless $opt_s;
188 # Find what section this page is in. If run from "." assume
191 $section = $1 if $dirname =~ /man([1-9])/;
193 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
194 print "$id: missing $_ head1 section\n"
195 if $contents !~ /^=head1\s+${_}\s*$/m;
198 open my $OUT, '>', $temp
199 or die "Can't open $temp, $!";
200 podchecker($filename, $OUT);
202 open $OUT, '<', $temp
203 or die "Can't read $temp, $!";
205 next if /\(section\) in.*deprecated/;
209 unlink $temp || warn "Can't remove $temp, $!";
219 open my $IN, '<', $file
220 or die "Can't open $file, $!, stopped";
223 next if /\bNOEXIST\b/;
224 next if /\bEXPORT_VAR_AS_FUNC\b/;
225 push @apis, $1 if /([^\s]+).\s/;
230 print "# Found ", scalar(@apis), " in $file\n";
239 foreach my $pod ( glob("$dir/*.pod") ) {
240 my %podinfo = extract_pod_info($pod);
241 foreach my $n ( @{$podinfo{names}} ) {
243 print "# Duplicate $n in $pod and $dups{$n}\n"
244 if defined $dups{$n} && $dups{$n} ne $pod;
260 foreach my $func ( &parsenum($numfile) ) {
261 next if $docced{$func};
263 # Skip ASN1 utilities
264 next if $func =~ /^ASN1_/;
266 print "$libname:$func\n";
269 print "# Found $count missing from $numfile\n\n";
273 # Collection of links in each POD file.
274 # filename => [ "foo(1)", "bar(3)", ... ]
275 my %link_collection = ();
276 # Collection of names in each POD file.
277 # "name(s)" => filename
278 my %name_collection = ();
281 my $filename = shift;
282 $filename =~ m|man(\d)/|;
284 my $simplename = basename($filename, ".pod");
285 my $id = "${filename}:1:";
290 open POD, $filename or die "Couldn't open $filename, $!";
295 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
297 unless (defined $tmp) {
298 print "$id weird name section\n";
304 my @names = map { s/\s+//g; $_ } split(/,/, $tmp);
305 unless (grep { $simplename eq $_ } @names) {
306 print "$id missing $simplename\n";
307 push @names, $simplename;
309 foreach my $name (@names) {
311 my $name_sec = "$name($section)";
312 if (! exists $name_collection{$name_sec}) {
313 $name_collection{$name_sec} = $filename;
314 } else { #elsif ($filename ne $name_collection{$name_sec}) {
315 print "$id $name_sec also in $name_collection{$name_sec}\n";
320 map { map { s/\s+//g; $_ } split(/,/, $_) }
321 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
322 foreach (@foreign_names) {
323 $name_collection{$_} = undef; # It still exists!
326 my @links = $contents =~ /L<
327 # if the link is of the form L<something|name(s)>,
328 # then remove 'something'. Note that 'something'
329 # may contain POD codes as well...
330 (?:(?:[^\|]|<[^>]*>)*\|)?
331 # we're only interested in referenses that have
332 # a one digit section number
335 $link_collection{$filename} = [ @links ];
339 foreach my $filename (sort keys %link_collection) {
340 foreach my $link (@{$link_collection{$filename}}) {
341 print "${filename}:1: reference to non-existing $link\n"
342 unless exists $name_collection{$link};
349 &help() if ( $opt_h );
351 die "Need one of -l -n -s or -u flags.\n"
352 unless $opt_l or $opt_n or $opt_s or $opt_u;
354 if ( $opt_n or $opt_s ) {
355 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
361 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
368 my %temp = &getdocced('doc/man3');
369 foreach ( keys %temp ) {
370 $docced{$_} = $temp{$_};
372 &printem('crypto', 'util/libcrypto.num');
373 &printem('ssl', 'util/libssl.num');