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