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