Add stricter checking in NAME section
[openssl.git] / util / find-doc-nits
1 #! /usr/bin/env perl
2 # Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
8
9
10 require 5.10.0;
11 use warnings;
12 use strict;
13 use Pod::Checker;
14 use File::Find;
15 use File::Basename;
16 use File::Spec::Functions;
17 use Getopt::Std;
18 use lib catdir(dirname($0), "perl");
19 use OpenSSL::Util::Pod;
20
21 # Options.
22 our($opt_s);
23 our($opt_u);
24 our($opt_h);
25 our($opt_n);
26 our($opt_l);
27
28 sub help()
29 {
30     print <<EOF;
31 Find small errors (nits) in documentation.  Options:
32     -l Print bogus links
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
37 EOF
38     exit;
39 }
40
41 my $temp = '/tmp/docnits.txt';
42 my $OUT;
43
44 my %mandatory_sections =
45     ( '*'    => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
46       1      => [ 'SYNOPSIS', 'OPTIONS' ],
47       3      => [ 'SYNOPSIS', 'RETURN VALUES' ],
48       5      => [ ],
49       7      => [ ] );
50
51 # Cross-check functions in the NAME and SYNOPSIS section.
52 sub name_synopsis()
53 {
54     my $id = shift;
55     my $filename = shift;
56     my $contents = shift;
57
58     # Get NAME section and all words in it.
59     return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
60     my $tmp = $1;
61     $tmp =~ tr/\n/ /;
62     print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
63     $tmp =~ s/ -.*//g;
64     $tmp =~ s/  */ /g;
65     print "$id missing comma in NAME\n" if $tmp =~ /[^,] /;
66     $tmp =~ s/,//g;
67
68     my $dirname = dirname($filename);
69     my $simplename = basename($filename);
70     $simplename =~ s/.pod$//;
71     my $foundfilename = 0;
72     my %foundfilenames = ();
73     my %names;
74     foreach my $n ( split ' ', $tmp ) {
75         $names{$n} = 1;
76         $foundfilename++ if $n eq $simplename;
77         $foundfilenames{$n} = 1
78             if -f "$dirname/$n.pod" && $n ne $simplename;
79     }
80     print "$id the following exist as other .pod files:\n",
81         join(" ", sort keys %foundfilenames), "\n"
82         if %foundfilenames;
83     print "$id $simplename (filename) missing from NAME section\n",
84         unless $foundfilename;
85
86     # Find all functions in SYNOPSIS
87     return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
88     my $syn = $1;
89     foreach my $line ( split /\n+/, $syn ) {
90         my $sym;
91         $line =~ s/STACK_OF\([^)]+\)/int/g;
92         $line =~ s/__declspec\([^)]+\)//;
93         if ( $line =~ /env (\S*)=/ ) {
94             # environment variable env NAME=...
95             $sym = $1;
96         } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
97             # a callback function: typedef ... (*NAME)(...
98             $sym = $1;
99         } elsif ( $line =~ /typedef.* (\S+);/ ) {
100             # a simple typedef: typedef ... NAME;
101             $sym = $1;
102         } elsif ( $line =~ /enum (\S*) \{/ ) {
103             # an enumeration: enum ... {
104             $sym = $1;
105         } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
106             $sym = $1;
107         } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
108             $sym = $1;
109         }
110         else {
111             next;
112         }
113         print "$id $sym missing from NAME section\n"
114             unless defined $names{$sym};
115         $names{$sym} = 2;
116
117         # Do some sanity checks on the prototype.
118         print "$id prototype missing spaces around commas: $line\n"
119             if ( $line =~ /[a-z0-9],[^ ]/ );
120     }
121
122     foreach my $n ( keys %names ) {
123         next if $names{$n} == 2;
124         print "$id $n missing from SYNOPSIS\n";
125     }
126 }
127
128 sub check()
129 {
130     my $filename = shift;
131     my $dirname = basename(dirname($filename));
132
133     my $contents = '';
134     {
135         local $/ = undef;
136         open POD, $filename or die "Couldn't open $filename, $!";
137         $contents = <POD>;
138         close POD;
139     }
140
141     my $id = "${filename}:1:";
142
143     &name_synopsis($id, $filename, $contents)
144         unless $contents =~ /=for comment generic/
145             or $filename =~ m@man[157]/@;
146
147     print "$id doesn't start with =pod\n"
148         if $contents !~ /^=pod/;
149     print "$id doesn't end with =cut\n"
150         if $contents !~ /=cut\n$/;
151     print "$id more than one cut line.\n"
152         if $contents =~ /=cut.*=cut/ms;
153     print "$id missing copyright\n"
154         if $contents !~ /Copyright .* The OpenSSL Project Authors/;
155     print "$id copyright not last\n"
156         if $contents =~ /head1 COPYRIGHT.*=head/ms;
157     print "$id head2 in All uppercase\n"
158         if $contents =~ /head2\s+[A-Z ]+\n/;
159     print "$id extra space after head\n"
160         if $contents =~ /=head\d\s\s+/;
161     print "$id period in NAME section\n"
162         if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
163     print "$id POD markup in NAME section\n"
164         if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
165     print "$id Duplicate $1 in L<>\n"
166         if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
167     print "$id Bad =over $1\n"
168         if $contents =~ /=over([^ ][^24])/;
169
170     # Look for multiple consecutive openssl #include lines.
171     # Consecutive because of files like md5.pod. Sometimes it's okay
172     # or necessary, as in ssl/SSL_set1_host.pod
173     if ( $contents !~ /=for comment multiple includes/ ) {
174         if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
175             my $count = 0;
176             foreach my $line ( split /\n+/, $1 ) {
177                 if ( $line =~ m@include <openssl/@ ) {
178                     if ( ++$count == 2 ) {
179                         print "$id has multiple includes\n";
180                     }
181                 } else {
182                     $count = 0;
183                 }
184             }
185         }
186     }
187
188     return unless $opt_s;
189
190     # Find what section this page is in.  If run from "." assume
191     # section 3.
192     my $section = 3;
193     $section = $1 if $dirname =~ /man([1-9])/;
194
195     foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
196         print "$id: missing $_ head1 section\n"
197             if $contents !~ /^=head1\s+${_}\s*$/m;
198     }
199
200     open my $OUT, '>', $temp
201         or die "Can't open $temp, $!";
202     podchecker($filename, $OUT);
203     close $OUT;
204     open $OUT, '<', $temp
205         or die "Can't read $temp, $!";
206     while ( <$OUT> ) {
207         next if /\(section\) in.*deprecated/;
208         print;
209     }
210     close $OUT;
211     unlink $temp || warn "Can't remove $temp, $!";
212 }
213
214 my %dups;
215
216 sub parsenum()
217 {
218     my $file = shift;
219     my @apis;
220
221     open my $IN, '<', $file
222         or die "Can't open $file, $!, stopped";
223
224     while ( <$IN> ) {
225         next if /\bNOEXIST\b/;
226         next if /\bEXPORT_VAR_AS_FUNC\b/;
227         push @apis, $1 if /([^\s]+).\s/;
228     }
229
230     close $IN;
231
232     print "# Found ", scalar(@apis), " in $file\n";
233     return sort @apis;
234 }
235
236 sub getdocced()
237 {
238     my $dir = shift;
239     my %return;
240
241     foreach my $pod ( glob("$dir/*.pod") ) {
242         my %podinfo = extract_pod_info($pod);
243         foreach my $n ( @{$podinfo{names}} ) {
244             $return{$n} = $pod;
245             print "# Duplicate $n in $pod and $dups{$n}\n"
246                 if defined $dups{$n} && $dups{$n} ne $pod;
247             $dups{$n} = $pod;
248         }
249     }
250
251     return %return;
252 }
253
254 my %docced;
255
256 sub printem()
257 {
258     my $libname = shift;
259     my $numfile = shift;
260     my $count = 0;
261
262     foreach my $func ( &parsenum($numfile) ) {
263         next if $docced{$func};
264
265         # Skip ASN1 utilities
266         next if $func =~ /^ASN1_/;
267
268         print "$libname:$func\n";
269         $count++;
270     }
271     print "# Found $count missing from $numfile\n\n";
272 }
273
274
275 # Collection of links in each POD file.
276 # filename => [ "foo(1)", "bar(3)", ... ]
277 my %link_collection = ();
278 # Collection of names in each POD file.
279 # "name(s)" => filename
280 my %name_collection = ();
281
282 sub collectnames {
283     my $filename = shift;
284     $filename =~ m|man(\d)/|;
285     my $section = $1;
286     my $simplename = basename($filename, ".pod");
287     my $id = "${filename}:1:";
288
289     my $contents = '';
290     {
291         local $/ = undef;
292         open POD, $filename or die "Couldn't open $filename, $!";
293         $contents = <POD>;
294         close POD;
295     }
296
297     $contents =~ /=head1 NAME([^=]*)=head1 /ms;
298     my $tmp = $1;
299     unless (defined $tmp) {
300         print "$id weird name section\n";
301         return;
302     }
303     $tmp =~ tr/\n/ /;
304     $tmp =~ s/-.*//g;
305
306     my @names = map { s/\s+//g; $_ } split(/,/, $tmp);
307     unless (grep { $simplename eq $_ } @names) {
308         print "$id missing $simplename\n";
309         push @names, $simplename;
310     }
311     foreach my $name (@names) {
312         next if $name eq "";
313         my $name_sec = "$name($section)";
314         if (! exists $name_collection{$name_sec}) {
315             $name_collection{$name_sec} = $filename;
316         } else { #elsif ($filename ne $name_collection{$name_sec}) {
317             print "$id $name_sec also in $name_collection{$name_sec}\n";
318         }
319     }
320
321     my @foreign_names =
322         map { map { s/\s+//g; $_ } split(/,/, $_) }
323         $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
324     foreach (@foreign_names) {
325         $name_collection{$_} = undef; # It still exists!
326     }
327
328     my @links = $contents =~ /L<
329                               # if the link is of the form L<something|name(s)>,
330                               # then remove 'something'.  Note that 'something'
331                               # may contain POD codes as well...
332                               (?:(?:[^\|]|<[^>]*>)*\|)?
333                               # we're only interested in referenses that have
334                               # a one digit section number
335                               ([^\/>\(]+\(\d\))
336                              /gx;
337     $link_collection{$filename} = [ @links ];
338 }
339
340 sub checklinks {
341     foreach my $filename (sort keys %link_collection) {
342         foreach my $link (@{$link_collection{$filename}}) {
343             print "${filename}:1: reference to non-existing $link\n"
344                 unless exists $name_collection{$link};
345         }
346     }
347 }
348
349 getopts('lnshu');
350
351 &help() if ( $opt_h );
352
353 die "Need one of -l -n -s or -u flags.\n"
354     unless $opt_l or $opt_n or $opt_s or $opt_u;
355
356 if ( $opt_n or $opt_s ) {
357     foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
358         &check($_);
359     }
360 }
361
362 if ( $opt_l ) {
363     foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
364         collectnames($_);
365     }
366     checklinks();
367 }
368
369 if ( $opt_u ) {
370     my %temp = &getdocced('doc/man3');
371     foreach ( keys %temp ) {
372         $docced{$_} = $temp{$_};
373     }
374     &printem('crypto', 'util/libcrypto.num');
375     &printem('ssl', 'util/libssl.num');
376 }
377
378 exit;