]>
Commit | Line | Data |
---|---|---|
1bc74519 | 1 | #! /usr/bin/env perl |
05ea606a RS |
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 | ||
1bc74519 RS |
9 | |
10 | require 5.10.0; | |
11 | use warnings; | |
12 | use strict; | |
13 | use Pod::Checker; | |
14 | use File::Find; | |
169a8e39 | 15 | use File::Basename; |
71a8b855 | 16 | use File::Spec::Functions; |
35ea640a | 17 | use Getopt::Std; |
71a8b855 RS |
18 | use lib catdir(dirname($0), "perl"); |
19 | use OpenSSL::Util::Pod; | |
35ea640a | 20 | |
71a8b855 | 21 | # Options. |
8d50b9c1 | 22 | our($opt_d); |
71a8b855 | 23 | our($opt_h); |
9e183d22 | 24 | our($opt_l); |
8d50b9c1 | 25 | our($opt_n); |
274d1bee | 26 | our($opt_p); |
8d50b9c1 RS |
27 | our($opt_s); |
28 | our($opt_u); | |
71a8b855 RS |
29 | |
30 | sub help() | |
31 | { | |
32 | print <<EOF; | |
33 | Find small errors (nits) in documentation. Options: | |
8d50b9c1 | 34 | -d Detailed list of undocumented (implies -u) |
9e183d22 | 35 | -l Print bogus links |
71a8b855 RS |
36 | -n Print nits in POD pages |
37 | -s Also print missing sections in POD pages (implies -n) | |
274d1bee | 38 | -p Warn if non-public name documented (implies -n) |
71a8b855 RS |
39 | -u List undocumented functions |
40 | -h Print this help message | |
41 | EOF | |
42 | exit; | |
43 | } | |
1bc74519 | 44 | |
05ea606a RS |
45 | my $temp = '/tmp/docnits.txt'; |
46 | my $OUT; | |
274d1bee | 47 | my %public; |
05ea606a | 48 | |
169a8e39 RL |
49 | my %mandatory_sections = |
50 | ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ], | |
3dfda1a6 RS |
51 | 1 => [ 'SYNOPSIS', 'OPTIONS' ], |
52 | 3 => [ 'SYNOPSIS', 'RETURN VALUES' ], | |
169a8e39 RL |
53 | 5 => [ ], |
54 | 7 => [ ] ); | |
169a8e39 | 55 | |
35ea640a RS |
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 | ||
35ea640a RS |
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/ /; | |
3ba4dac6 | 67 | print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/; |
2bcb232e RS |
68 | $tmp =~ s/ -.*//g; |
69 | $tmp =~ s/ */ /g; | |
70 | print "$id missing comma in NAME\n" if $tmp =~ /[^,] /; | |
35ea640a | 71 | $tmp =~ s/,//g; |
fbba5d11 RS |
72 | |
73 | my $dirname = dirname($filename); | |
74 | my $simplename = basename($filename); | |
75 | $simplename =~ s/.pod$//; | |
76 | my $foundfilename = 0; | |
77 | my %foundfilenames = (); | |
35ea640a RS |
78 | my %names; |
79 | foreach my $n ( split ' ', $tmp ) { | |
80 | $names{$n} = 1; | |
fbba5d11 RS |
81 | $foundfilename++ if $n eq $simplename; |
82 | $foundfilenames{$n} = 1 | |
83 | if -f "$dirname/$n.pod" && $n ne $simplename; | |
35ea640a | 84 | } |
fbba5d11 RS |
85 | print "$id the following exist as other .pod files:\n", |
86 | join(" ", sort keys %foundfilenames), "\n" | |
87 | if %foundfilenames; | |
274d1bee | 88 | print "$id $simplename (filename) missing from NAME section\n" |
fbba5d11 | 89 | unless $foundfilename; |
1722496f RS |
90 | foreach my $n ( keys %names ) { |
91 | print "$id $n is not public\n" | |
92 | if $opt_p and !defined $public{$n}; | |
93 | } | |
35ea640a RS |
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 ) { | |
8162f6f5 | 99 | my $sym; |
c952780c RS |
100 | $line =~ s/STACK_OF\([^)]+\)/int/g; |
101 | $line =~ s/__declspec\([^)]+\)//; | |
121677b4 RS |
102 | if ( $line =~ /env (\S*)=/ ) { |
103 | # environment variable env NAME=... | |
104 | $sym = $1; | |
105 | } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) { | |
106 | # a callback function: typedef ... (*NAME)(... | |
107 | $sym = $1; | |
108 | } elsif ( $line =~ /typedef.* (\S+);/ ) { | |
109 | # a simple typedef: typedef ... NAME; | |
8162f6f5 | 110 | $sym = $1; |
5d583521 | 111 | } elsif ( $line =~ /enum (\S*) \{/ ) { |
d4ea9659 RS |
112 | # an enumeration: enum ... { |
113 | $sym = $1; | |
c952780c | 114 | } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) { |
8162f6f5 RS |
115 | $sym = $1; |
116 | } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) { | |
117 | $sym = $1; | |
118 | } | |
119 | else { | |
120 | next; | |
121 | } | |
122 | print "$id $sym missing from NAME section\n" | |
123 | unless defined $names{$sym}; | |
124 | $names{$sym} = 2; | |
aebb9aac RS |
125 | |
126 | # Do some sanity checks on the prototype. | |
127 | print "$id prototype missing spaces around commas: $line\n" | |
128 | if ( $line =~ /[a-z0-9],[^ ]/ ); | |
35ea640a RS |
129 | } |
130 | ||
131 | foreach my $n ( keys %names ) { | |
132 | next if $names{$n} == 2; | |
133 | print "$id $n missing from SYNOPSIS\n"; | |
134 | } | |
135 | } | |
136 | ||
1bc74519 RS |
137 | sub check() |
138 | { | |
169a8e39 RL |
139 | my $filename = shift; |
140 | my $dirname = basename(dirname($filename)); | |
843666ff | 141 | |
1bc74519 RS |
142 | my $contents = ''; |
143 | { | |
144 | local $/ = undef; | |
169a8e39 | 145 | open POD, $filename or die "Couldn't open $filename, $!"; |
1bc74519 RS |
146 | $contents = <POD>; |
147 | close POD; | |
148 | } | |
843666ff RS |
149 | |
150 | my $id = "${filename}:1:"; | |
35ea640a | 151 | |
4692340e | 152 | &name_synopsis($id, $filename, $contents) |
8162f6f5 | 153 | unless $contents =~ /=for comment generic/ |
99d63d46 | 154 | or $filename =~ m@man[157]/@; |
35ea640a RS |
155 | |
156 | print "$id doesn't start with =pod\n" | |
05ea606a | 157 | if $contents !~ /^=pod/; |
35ea640a | 158 | print "$id doesn't end with =cut\n" |
05ea606a | 159 | if $contents !~ /=cut\n$/; |
35ea640a | 160 | print "$id more than one cut line.\n" |
05ea606a | 161 | if $contents =~ /=cut.*=cut/ms; |
35ea640a | 162 | print "$id missing copyright\n" |
05ea606a | 163 | if $contents !~ /Copyright .* The OpenSSL Project Authors/; |
35ea640a | 164 | print "$id copyright not last\n" |
05ea606a | 165 | if $contents =~ /head1 COPYRIGHT.*=head/ms; |
35ea640a | 166 | print "$id head2 in All uppercase\n" |
843666ff | 167 | if $contents =~ /head2\s+[A-Z ]+\n/; |
35ea640a RS |
168 | print "$id extra space after head\n" |
169 | if $contents =~ /=head\d\s\s+/; | |
170 | print "$id period in NAME section\n" | |
171 | if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms; | |
172 | print "$id POD markup in NAME section\n" | |
173 | if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms; | |
5a3371e2 RS |
174 | print "$id Duplicate $1 in L<>\n" |
175 | if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2; | |
e1271ac2 | 176 | print "$id Bad =over $1\n" |
2f61bc2e | 177 | if $contents =~ /=over([^ ][^24])/; |
e90fc053 RS |
178 | print "$id Possible version style issue\n" |
179 | if $contents =~ /OpenSSL version [019]/; | |
843666ff | 180 | |
843666ff | 181 | if ( $contents !~ /=for comment multiple includes/ ) { |
a95d7574 RS |
182 | # Look for multiple consecutive openssl #include lines |
183 | # (non-consecutive lines are okay; see man3/MD5.pod). | |
843666ff RS |
184 | if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) { |
185 | my $count = 0; | |
186 | foreach my $line ( split /\n+/, $1 ) { | |
187 | if ( $line =~ m@include <openssl/@ ) { | |
a95d7574 | 188 | print "$id has multiple includes\n" if ++$count == 2; |
843666ff RS |
189 | } else { |
190 | $count = 0; | |
191 | } | |
192 | } | |
193 | } | |
194 | } | |
05ea606a | 195 | |
35ea640a RS |
196 | open my $OUT, '>', $temp |
197 | or die "Can't open $temp, $!"; | |
169a8e39 | 198 | podchecker($filename, $OUT); |
35ea640a RS |
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, $!"; | |
a95d7574 RS |
208 | |
209 | # Find what section this page is in; assume 3. | |
210 | my $section = 3; | |
211 | $section = $1 if $dirname =~ /man([1-9])/; | |
212 | ||
213 | foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) { | |
214 | # Skip "return values" if not -s | |
215 | next if $_ eq 'RETURN VALUES' and not $opt_s; | |
216 | print "$id: missing $_ head1 section\n" | |
217 | if $contents !~ /^=head1\s+${_}\s*$/m; | |
218 | } | |
05ea606a | 219 | } |
1bc74519 | 220 | |
71a8b855 RS |
221 | my %dups; |
222 | ||
223 | sub parsenum() | |
224 | { | |
225 | my $file = shift; | |
226 | my @apis; | |
227 | ||
228 | open my $IN, '<', $file | |
229 | or die "Can't open $file, $!, stopped"; | |
230 | ||
231 | while ( <$IN> ) { | |
274d1bee | 232 | next if /^#/; |
71a8b855 RS |
233 | next if /\bNOEXIST\b/; |
234 | next if /\bEXPORT_VAR_AS_FUNC\b/; | |
1722496f RS |
235 | my @fields = split(); |
236 | die "Malformed line $_" | |
237 | if scalar @fields != 2 && scalar @fields != 4; | |
238 | push @apis, $fields[0]; | |
71a8b855 RS |
239 | } |
240 | ||
241 | close $IN; | |
242 | ||
274d1bee | 243 | print "# Found ", scalar(@apis), " in $file\n" unless $opt_p; |
71a8b855 RS |
244 | return sort @apis; |
245 | } | |
246 | ||
247 | sub getdocced() | |
248 | { | |
249 | my $dir = shift; | |
250 | my %return; | |
251 | ||
252 | foreach my $pod ( glob("$dir/*.pod") ) { | |
253 | my %podinfo = extract_pod_info($pod); | |
254 | foreach my $n ( @{$podinfo{names}} ) { | |
255 | $return{$n} = $pod; | |
256 | print "# Duplicate $n in $pod and $dups{$n}\n" | |
257 | if defined $dups{$n} && $dups{$n} ne $pod; | |
258 | $dups{$n} = $pod; | |
259 | } | |
260 | } | |
261 | ||
262 | return %return; | |
263 | } | |
264 | ||
265 | my %docced; | |
266 | ||
9a2dfc0f RS |
267 | sub checkmacros() |
268 | { | |
269 | my $count = 0; | |
270 | ||
271 | print "# Checking macros (approximate)\n"; | |
272 | foreach my $f ( glob('include/openssl/*.h') ) { | |
273 | # Skip some internals we don't want to document yet. | |
274 | next if $f eq 'include/openssl/asn1.h'; | |
275 | next if $f eq 'include/openssl/asn1t.h'; | |
276 | next if $f eq 'include/openssl/err.h'; | |
277 | open(IN, $f) || die "Can't open $f, $!"; | |
278 | while ( <IN> ) { | |
279 | next unless /^#\s*define\s*(\S+)\(/; | |
280 | my $macro = $1; | |
281 | next if $docced{$macro}; | |
282 | next if $macro =~ /i2d_/ | |
283 | || $macro =~ /d2i_/ | |
284 | || $macro =~ /DEPRECATEDIN/ | |
285 | || $macro =~ /IMPLEMENT_/ | |
286 | || $macro =~ /DECLARE_/; | |
8d50b9c1 | 287 | print "$f:$macro\n" if $opt_d; |
9a2dfc0f RS |
288 | $count++; |
289 | } | |
290 | close(IN); | |
291 | } | |
44e69951 | 292 | print "# Found $count macros missing (not all should be documented)\n" |
9a2dfc0f RS |
293 | } |
294 | ||
71a8b855 RS |
295 | sub printem() |
296 | { | |
297 | my $libname = shift; | |
298 | my $numfile = shift; | |
299 | my $count = 0; | |
300 | ||
301 | foreach my $func ( &parsenum($numfile) ) { | |
302 | next if $docced{$func}; | |
303 | ||
304 | # Skip ASN1 utilities | |
305 | next if $func =~ /^ASN1_/; | |
306 | ||
8d50b9c1 | 307 | print "$libname:$func\n" if $opt_d; |
71a8b855 RS |
308 | $count++; |
309 | } | |
310 | print "# Found $count missing from $numfile\n\n"; | |
311 | } | |
312 | ||
313 | ||
9e183d22 RS |
314 | # Collection of links in each POD file. |
315 | # filename => [ "foo(1)", "bar(3)", ... ] | |
316 | my %link_collection = (); | |
317 | # Collection of names in each POD file. | |
318 | # "name(s)" => filename | |
319 | my %name_collection = (); | |
320 | ||
321 | sub collectnames { | |
322 | my $filename = shift; | |
323 | $filename =~ m|man(\d)/|; | |
324 | my $section = $1; | |
325 | my $simplename = basename($filename, ".pod"); | |
326 | my $id = "${filename}:1:"; | |
327 | ||
328 | my $contents = ''; | |
329 | { | |
330 | local $/ = undef; | |
331 | open POD, $filename or die "Couldn't open $filename, $!"; | |
332 | $contents = <POD>; | |
333 | close POD; | |
334 | } | |
335 | ||
336 | $contents =~ /=head1 NAME([^=]*)=head1 /ms; | |
337 | my $tmp = $1; | |
338 | unless (defined $tmp) { | |
339 | print "$id weird name section\n"; | |
340 | return; | |
341 | } | |
342 | $tmp =~ tr/\n/ /; | |
343 | $tmp =~ s/-.*//g; | |
344 | ||
345 | my @names = map { s/\s+//g; $_ } split(/,/, $tmp); | |
346 | unless (grep { $simplename eq $_ } @names) { | |
347 | print "$id missing $simplename\n"; | |
348 | push @names, $simplename; | |
349 | } | |
350 | foreach my $name (@names) { | |
351 | next if $name eq ""; | |
352 | my $name_sec = "$name($section)"; | |
353 | if (! exists $name_collection{$name_sec}) { | |
354 | $name_collection{$name_sec} = $filename; | |
355 | } else { #elsif ($filename ne $name_collection{$name_sec}) { | |
356 | print "$id $name_sec also in $name_collection{$name_sec}\n"; | |
357 | } | |
358 | } | |
359 | ||
360 | my @foreign_names = | |
361 | map { map { s/\s+//g; $_ } split(/,/, $_) } | |
362 | $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/; | |
363 | foreach (@foreign_names) { | |
364 | $name_collection{$_} = undef; # It still exists! | |
365 | } | |
366 | ||
367 | my @links = $contents =~ /L< | |
368 | # if the link is of the form L<something|name(s)>, | |
369 | # then remove 'something'. Note that 'something' | |
370 | # may contain POD codes as well... | |
371 | (?:(?:[^\|]|<[^>]*>)*\|)? | |
372 | # we're only interested in referenses that have | |
373 | # a one digit section number | |
374 | ([^\/>\(]+\(\d\)) | |
375 | /gx; | |
376 | $link_collection{$filename} = [ @links ]; | |
377 | } | |
378 | ||
379 | sub checklinks { | |
380 | foreach my $filename (sort keys %link_collection) { | |
381 | foreach my $link (@{$link_collection{$filename}}) { | |
382 | print "${filename}:1: reference to non-existing $link\n" | |
383 | unless exists $name_collection{$link}; | |
384 | } | |
385 | } | |
386 | } | |
387 | ||
274d1bee RS |
388 | sub publicize() { |
389 | foreach my $name ( &parsenum('util/libcrypto.num') ) { | |
390 | $public{$name} = 1; | |
391 | } | |
392 | foreach my $name ( &parsenum('util/libssl.num') ) { | |
393 | $public{$name} = 1; | |
394 | } | |
395 | foreach my $name ( &parsenum('util/private.num') ) { | |
396 | $public{$name} = 1; | |
397 | } | |
398 | } | |
399 | ||
8d50b9c1 | 400 | getopts('dlnsphu'); |
274d1bee RS |
401 | |
402 | &help() if $opt_h; | |
8d50b9c1 RS |
403 | $opt_n = 1 if $opt_s or $opt_p; |
404 | $opt_u = 1 if $opt_d; | |
35ea640a | 405 | |
8d50b9c1 RS |
406 | die "Need one of -[dlnspu] flags.\n" |
407 | unless $opt_l or $opt_n or $opt_u; | |
71a8b855 | 408 | |
3dfda1a6 | 409 | |
274d1bee RS |
410 | if ( $opt_n ) { |
411 | &publicize() if $opt_p; | |
71a8b855 RS |
412 | foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) { |
413 | &check($_); | |
414 | } | |
415 | } | |
9e183d22 RS |
416 | |
417 | if ( $opt_l ) { | |
418 | foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) { | |
419 | collectnames($_); | |
420 | } | |
421 | checklinks(); | |
422 | } | |
423 | ||
71a8b855 RS |
424 | if ( $opt_u ) { |
425 | my %temp = &getdocced('doc/man3'); | |
426 | foreach ( keys %temp ) { | |
427 | $docced{$_} = $temp{$_}; | |
428 | } | |
429 | &printem('crypto', 'util/libcrypto.num'); | |
430 | &printem('ssl', 'util/libssl.num'); | |
9a2dfc0f | 431 | &checkmacros(); |
1bc74519 | 432 | } |
05ea606a | 433 | |
35ea640a | 434 | exit; |