util/find-docs-nits: Extend to handle internal documentation
[openssl.git] / util / find-doc-nits
1 #! /usr/bin/env perl
2 # Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
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_d);
23 our($opt_h);
24 our($opt_l);
25 our($opt_n);
26 our($opt_p);
27 our($opt_u);
28 our($opt_c);
29
30 sub help()
31 {
32     print <<EOF;
33 Find small errors (nits) in documentation.  Options:
34     -d Detailed list of undocumented (implies -u)
35     -l Print bogus links
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
41 EOF
42     exit;
43 }
44
45 my $temp = '/tmp/docnits.txt';
46 my $OUT;
47 my %public;
48
49 my %mandatory_sections =
50     ( '*'    => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
51       1      => [ 'SYNOPSIS', 'OPTIONS' ],
52       3      => [ 'SYNOPSIS', 'RETURN VALUES' ],
53       5      => [ ],
54       7      => [ ] );
55
56 # Cross-check functions in the NAME and SYNOPSIS section.
57 sub name_synopsis()
58 {
59     my $id = shift;
60     my $filename = shift;
61     my $contents = shift;
62
63     # Get NAME section and all words in it.
64     return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
65     my $tmp = $1;
66     $tmp =~ tr/\n/ /;
67     print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
68     $tmp =~ s/ -.*//g;
69     $tmp =~ s/  */ /g;
70     print "$id missing comma in NAME\n" if $tmp =~ /[^,] /;
71
72     my $dirname = dirname($filename);
73     my $simplename = basename($filename);
74     $simplename =~ s/.pod$//;
75     my $foundfilename = 0;
76     my %foundfilenames = ();
77     my %names;
78     foreach my $n ( split ',', $tmp ) {
79         $n =~ s/^\s+//;
80         $n =~ s/\s+$//;
81         print "$id the name '$n' contains white-space\n"
82             if $n =~ /\s/;
83         $names{$n} = 1;
84         $foundfilename++ if $n eq $simplename;
85         $foundfilenames{$n} = 1
86             if -f "$dirname/$n.pod" && $n ne $simplename;
87     }
88     print "$id the following exist as other .pod files:\n",
89         join(" ", sort keys %foundfilenames), "\n"
90         if %foundfilenames;
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};
96     }
97
98     # Find all functions in SYNOPSIS
99     return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
100     my $syn = $1;
101     foreach my $line ( split /\n+/, $syn ) {
102         next unless $line =~ /^\s/;
103         my $sym;
104         $line =~ s/STACK_OF\([^)]+\)/int/g;
105         $line =~ s/__declspec\([^)]+\)//;
106         if ( $line =~ /env (\S*)=/ ) {
107             # environment variable env NAME=...
108             $sym = $1;
109         } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
110             # a callback function pointer: typedef ... (*NAME)(...
111             $sym = $1;
112         } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
113             # a callback function signature: typedef ... NAME(...
114             $sym = $1;
115         } elsif ( $line =~ /typedef.* (\S+);/ ) {
116             # a simple typedef: typedef ... NAME;
117             $sym = $1;
118         } elsif ( $line =~ /enum (\S*) \{/ ) {
119             # an enumeration: enum ... {
120             $sym = $1;
121         } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) {
122             $sym = $1;
123         } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
124             $sym = $1;
125         }
126         else {
127             next;
128         }
129         print "$id $sym missing from NAME section\n"
130             unless defined $names{$sym};
131         $names{$sym} = 2;
132
133         # Do some sanity checks on the prototype.
134         print "$id prototype missing spaces around commas: $line\n"
135             if ( $line =~ /[a-z0-9],[^ ]/ );
136     }
137
138     foreach my $n ( keys %names ) {
139         next if $names{$n} == 2;
140         print "$id $n missing from SYNOPSIS\n";
141     }
142 }
143
144 sub check()
145 {
146     my $filename = shift;
147     my $dirname = basename(dirname($filename));
148
149     my $contents = '';
150     {
151         local $/ = undef;
152         open POD, $filename or die "Couldn't open $filename, $!";
153         $contents = <POD>;
154         close POD;
155     }
156
157     my $id = "${filename}:1:";
158
159     &name_synopsis($id, $filename, $contents)
160         unless $contents =~ /=for comment generic/
161             or $filename =~ m@man[157]/@;
162
163     print "$id doesn't start with =pod\n"
164         if $contents !~ /^=pod/;
165     print "$id doesn't end with =cut\n"
166         if $contents !~ /=cut\n$/;
167     print "$id more than one cut line.\n"
168         if $contents =~ /=cut.*=cut/ms;
169     print "$id missing copyright\n"
170         if $contents !~ /Copyright .* The OpenSSL Project Authors/;
171     print "$id copyright not last\n"
172         if $contents =~ /head1 COPYRIGHT.*=head/ms;
173     print "$id head2 in All uppercase\n"
174         if $contents =~ /head2\s+[A-Z ]+\n/;
175     print "$id extra space after head\n"
176         if $contents =~ /=head\d\s\s+/;
177     print "$id period in NAME section\n"
178         if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
179     print "$id POD markup in NAME section\n"
180         if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
181     print "$id Duplicate $1 in L<>\n"
182         if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
183     print "$id Bad =over $1\n"
184         if $contents =~ /=over([^ ][^24])/;
185     print "$id Possible version style issue\n"
186         if $contents =~ /OpenSSL version [019]/;
187
188     if ( $contents !~ /=for comment multiple includes/ ) {
189         # Look for multiple consecutive openssl #include lines
190         # (non-consecutive lines are okay; see man3/MD5.pod).
191         if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
192             my $count = 0;
193             foreach my $line ( split /\n+/, $1 ) {
194                 if ( $line =~ m@include <openssl/@ ) {
195                     print "$id has multiple includes\n" if ++$count == 2;
196                 } else {
197                     $count = 0;
198                 }
199             }
200         }
201     }
202
203     open my $OUT, '>', $temp
204         or die "Can't open $temp, $!";
205     podchecker($filename, $OUT);
206     close $OUT;
207     open $OUT, '<', $temp
208         or die "Can't read $temp, $!";
209     while ( <$OUT> ) {
210         next if /\(section\) in.*deprecated/;
211         print;
212     }
213     close $OUT;
214     unlink $temp || warn "Can't remove $temp, $!";
215
216     # Find what section this page is in; assume 3.
217     my $section = 3;
218     $section = $1 if $dirname =~ /man([1-9])/;
219
220     foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
221         # Skip "return values" if not -s
222         print "$id: missing $_ head1 section\n"
223             if $contents !~ /^=head1\s+${_}\s*$/m;
224     }
225 }
226
227 my %dups;
228
229 sub parsenum()
230 {
231     my $file = shift;
232     my @apis;
233
234     open my $IN, '<', $file
235         or die "Can't open $file, $!, stopped";
236
237     while ( <$IN> ) {
238         next if /^#/;
239         next if /\bNOEXIST\b/;
240         next if /\bEXPORT_VAR_AS_FUNC\b/;
241         my @fields = split();
242         die "Malformed line $_"
243             if scalar @fields != 2 && scalar @fields != 4;
244         push @apis, $fields[0];
245     }
246
247     close $IN;
248
249     print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
250     return sort @apis;
251 }
252
253 sub getdocced
254 {
255     my $dir = shift;
256     my %return;
257
258     foreach my $pod ( glob("$dir/*.pod") ) {
259         my %podinfo = extract_pod_info($pod);
260         foreach my $n ( @{$podinfo{names}} ) {
261             $return{$n} = $pod;
262             print "# Duplicate $n in $pod and $dups{$n}\n"
263                 if defined $dups{$n} && $dups{$n} ne $pod;
264             $dups{$n} = $pod;
265         }
266     }
267
268     return %return;
269 }
270
271 my %docced;
272
273 sub checkmacros()
274 {
275     my $count = 0;
276     my %seen;
277
278     print "# Checking macros (approximate)\n";
279     foreach my $f ( glob('include/openssl/*.h') ) {
280         # Skip some internals we don't want to document yet.
281         next if $f eq 'include/openssl/asn1.h';
282         next if $f eq 'include/openssl/asn1t.h';
283         next if $f eq 'include/openssl/err.h';
284         open(IN, $f) || die "Can't open $f, $!";
285         while ( <IN> ) {
286             next unless /^#\s*define\s*(\S+)\(/;
287             my $macro = $1;
288             next if $docced{$macro} || defined $seen{$macro};
289             next if $macro =~ /i2d_/
290                 || $macro =~ /d2i_/
291                 || $macro =~ /DEPRECATEDIN/
292                 || $macro =~ /IMPLEMENT_/
293                 || $macro =~ /DECLARE_/;
294             print "$f:$macro\n" if $opt_d;
295             $count++;
296             $seen{$macro} = 1;
297         }
298         close(IN);
299     }
300     print "# Found $count macros missing (not all should be documented)\n"
301 }
302
303 sub printem()
304 {
305     my $libname = shift;
306     my $numfile = shift;
307     my $count = 0;
308     my %seen;
309
310     foreach my $func ( &parsenum($numfile) ) {
311         next if $docced{$func} || defined $seen{$func};
312
313         # Skip ASN1 utilities
314         next if $func =~ /^ASN1_/;
315
316         print "$libname:$func\n" if $opt_d;
317         $count++;
318         $seen{$func} = 1;
319     }
320     print "# Found $count missing from $numfile\n\n";
321 }
322
323
324 # Collection of links in each POD file.
325 # filename => [ "foo(1)", "bar(3)", ... ]
326 my %link_collection = ();
327 # Collection of names in each POD file.
328 # "name(s)" => filename
329 my %name_collection = ();
330
331 sub collectnames {
332     my $filename = shift;
333     $filename =~ m|man(\d)/|;
334     my $section = $1;
335     my $simplename = basename($filename, ".pod");
336     my $id = "${filename}:1:";
337
338     my $contents = '';
339     {
340         local $/ = undef;
341         open POD, $filename or die "Couldn't open $filename, $!";
342         $contents = <POD>;
343         close POD;
344     }
345
346     $contents =~ /=head1 NAME([^=]*)=head1 /ms;
347     my $tmp = $1;
348     unless (defined $tmp) {
349         print "$id weird name section\n";
350         return;
351     }
352     $tmp =~ tr/\n/ /;
353     $tmp =~ s/-.*//g;
354
355     my @names = map { s/^\s+//g; s/\s+$//g; $_ } split(/,/, $tmp);
356     unless (grep { $simplename eq $_ } @names) {
357         print "$id missing $simplename\n";
358         push @names, $simplename;
359     }
360     foreach my $name (@names) {
361         next if $name eq "";
362         if ($name =~ /\s/) {
363             print "$id '$name' contains white space\n";
364         }
365         my $name_sec = "$name($section)";
366         if (! exists $name_collection{$name_sec}) {
367             $name_collection{$name_sec} = $filename;
368         } else { #elsif ($filename ne $name_collection{$name_sec}) {
369             print "$id $name_sec also in $name_collection{$name_sec}\n";
370         }
371     }
372
373     my @foreign_names =
374         map { map { s/\s+//g; $_ } split(/,/, $_) }
375         $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
376     foreach (@foreign_names) {
377         $name_collection{$_} = undef; # It still exists!
378     }
379
380     my @links = $contents =~ /L<
381                               # if the link is of the form L<something|name(s)>,
382                               # then remove 'something'.  Note that 'something'
383                               # may contain POD codes as well...
384                               (?:(?:[^\|]|<[^>]*>)*\|)?
385                               # we're only interested in references that have
386                               # a one digit section number
387                               ([^\/>\(]+\(\d\))
388                              /gx;
389     $link_collection{$filename} = [ @links ];
390 }
391
392 sub checklinks {
393     foreach my $filename (sort keys %link_collection) {
394         foreach my $link (@{$link_collection{$filename}}) {
395             print "${filename}:1: reference to non-existing $link\n"
396                 unless exists $name_collection{$link};
397         }
398     }
399 }
400
401 sub publicize() {
402     foreach my $name ( &parsenum('util/libcrypto.num') ) {
403         $public{$name} = 1;
404     }
405     foreach my $name ( &parsenum('util/libssl.num') ) {
406         $public{$name} = 1;
407     }
408     foreach my $name ( &parsenum('util/private.num') ) {
409         $public{$name} = 1;
410     }
411 }
412
413 my %skips = (
414     'aes128' => 1,
415     'aes192' => 1,
416     'aes256' => 1,
417     'aria128' => 1,
418     'aria192' => 1,
419     'aria256' => 1,
420     'camellia128' => 1,
421     'camellia192' => 1,
422     'camellia256' => 1,
423     'des' => 1,
424     'des3' => 1,
425     'idea' => 1,
426     '[cipher]' => 1,
427     '[digest]' => 1,
428 );
429
430 sub checkflags() {
431     my $cmd = shift;
432     my %cmdopts;
433     my %docopts;
434     my $ok = 1;
435
436     # Get the list of options in the command.
437     open CFH, "./apps/openssl list --options $cmd|"
438         || die "Can list options for $cmd, $!";
439     while ( <CFH> ) {
440         chop;
441         s/ .$//;
442         $cmdopts{$_} = 1;
443     }
444     close CFH;
445
446     # Get the list of flags from the synopsis
447     open CFH, "<doc/man1/$cmd.pod"
448         || die "Can't open $cmd.pod, $!";
449     while ( <CFH> ) {
450         chop;
451         last if /DESCRIPTION/;
452         next unless /\[B<-([^ >]+)/;
453         $docopts{$1} = 1;
454     }
455     close CFH;
456
457     # See what's in the command not the manpage.
458     my @undocced = ();
459     foreach my $k ( keys %cmdopts ) {
460         push @undocced, $k unless $docopts{$k};
461     }
462     if ( scalar @undocced > 0 ) {
463         $ok = 0;
464         foreach ( @undocced ) {
465             print "doc/man1/$cmd.pod: Missing -$_\n";
466         }
467     }
468
469     # See what's in the command not the manpage.
470     my @unimpl = ();
471     foreach my $k ( keys %docopts ) {
472         push @unimpl, $k unless $cmdopts{$k};
473     }
474     if ( scalar @unimpl > 0 ) {
475         $ok = 0;
476         foreach ( @unimpl ) {
477             next if defined $skips{$_};
478             print "doc/man1/$cmd.pod: Not implemented -$_\n";
479         }
480     }
481
482     return $ok;
483 }
484
485 getopts('cdlnphu');
486
487 &help() if $opt_h;
488 $opt_n = 1 if $opt_p;
489 $opt_u = 1 if $opt_d;
490
491 die "Need one of -[cdlnpu] flags.\n"
492     unless $opt_c or $opt_l or $opt_n or $opt_u;
493
494 if ( $opt_c ) {
495     my $ok = 1;
496     my @commands = ();
497
498     # Get list of commands.
499     open FH, "./apps/openssl list -1 -commands|"
500         || die "Can't list commands, $!";
501     while ( <FH> ) {
502         chop;
503         push @commands, $_;
504     }
505     close FH;
506
507     # See if each has a manpage.
508     foreach ( @commands ) {
509         next if $_ eq 'help' || $_ eq 'exit';
510         if ( ! -f "doc/man1/$_.pod" ) {
511             print "doc/man1/$_.pod does not exist\n";
512             $ok = 0;
513         } else {
514             $ok = 0 if not &checkflags($_);
515         }
516     }
517
518     # See what help is missing.
519     open FH, "./apps/openssl list --missing-help |"
520         || die "Can't list missing help, $!";
521     while ( <FH> ) {
522         chop;
523         my ($cmd, $flag) = split;
524         print "$cmd has no help for -$flag\n";
525         $ok = 0;
526     }
527     close FH;
528
529     exit 1 if not $ok;
530 }
531
532 if ( $opt_l ) {
533     foreach (@ARGV ? @ARGV : (glob('doc/*/*.pod'),
534                               glob('doc/internal/*/*.pod'))) {
535         collectnames($_);
536     }
537     checklinks();
538 }
539
540 if ( $opt_n ) {
541     &publicize() if $opt_p;
542     foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
543         &check($_);
544     }
545     {
546         local $opt_p = undef;
547         foreach (@ARGV ? @ARGV : glob('doc/internal/*/*.pod')) {
548             &check($_);
549         }
550     }
551 }
552
553 if ( $opt_u ) {
554     my %temp = getdocced('doc/man3');
555     foreach ( keys %temp ) {
556         $docced{$_} = $temp{$_};
557     }
558     &printem('crypto', 'util/libcrypto.num');
559     &printem('ssl', 'util/libssl.num');
560     &checkmacros();
561 }
562
563 exit;