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