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