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