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