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