Rename SSL_write_early() to SSL_write_early_data()
[openssl.git] / util / find-doc-nits.pl
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
27 sub help()
28 {
29     print <<EOF;
30 Find small errors (nits) in documentation.  Options:
31     -n Print nits in POD pages
32     -s Also print missing sections in POD pages (implies -n)
33     -u List undocumented functions
34     -h Print this help message
35 EOF
36     exit;
37 }
38
39 my $temp = '/tmp/docnits.txt';
40 my $OUT;
41
42 my %mandatory_sections =
43     ( '*'    => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
44       1      => [ 'SYNOPSIS', 'OPTIONS' ],
45       3      => [ 'SYNOPSIS', 'RETURN VALUES' ],
46       5      => [ ],
47       7      => [ ] );
48
49 # Cross-check functions in the NAME and SYNOPSIS section.
50 sub name_synopsis()
51 {
52     my $id = shift;
53     my $filename = shift;
54     my $contents = shift;
55
56     # Get NAME section and all words in it.
57     return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
58     my $tmp = $1;
59     $tmp =~ tr/\n/ /;
60     $tmp =~ s/-.*//g;
61     $tmp =~ s/,//g;
62
63     my $dirname = dirname($filename);
64     my $simplename = basename($filename);
65     $simplename =~ s/.pod$//;
66     my $foundfilename = 0;
67     my %foundfilenames = ();
68     my %names;
69     foreach my $n ( split ' ', $tmp ) {
70         $names{$n} = 1;
71         $foundfilename++ if $n eq $simplename;
72         $foundfilenames{$n} = 1
73             if -f "$dirname/$n.pod" && $n ne $simplename;
74     }
75     print "$id the following exist as other .pod files:\n",
76         join(" ", sort keys %foundfilenames), "\n"
77         if %foundfilenames;
78     print "$id $simplename (filename) missing from NAME section\n",
79         unless $foundfilename;
80
81     # Find all functions in SYNOPSIS
82     return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
83     my $syn = $1;
84     foreach my $line ( split /\n+/, $syn ) {
85         my $sym;
86         $line =~ s/STACK_OF\([^)]+\)/int/g;
87         $line =~ s/__declspec\([^)]+\)//;
88         if ( $line =~ /env (\S*)=/ ) {
89             # environment variable env NAME=...
90             $sym = $1;
91         } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
92             # a callback function: typedef ... (*NAME)(...
93             $sym = $1;
94         } elsif ( $line =~ /typedef.* (\S+);/ ) {
95             # a simple typedef: typedef ... NAME;
96             $sym = $1;
97         } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
98             $sym = $1;
99         } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
100             $sym = $1;
101         }
102         else {
103             next;
104         }
105         print "$id $sym missing from NAME section\n"
106             unless defined $names{$sym};
107         $names{$sym} = 2;
108
109         # Do some sanity checks on the prototype.
110         print "$id prototype missing spaces around commas: $line\n"
111             if ( $line =~ /[a-z0-9],[^ ]/ );
112     }
113
114     foreach my $n ( keys %names ) {
115         next if $names{$n} == 2;
116         print "$id $n missing from SYNOPSIS\n";
117     }
118 }
119
120 sub check()
121 {
122     my $filename = shift;
123     my $dirname = basename(dirname($filename));
124
125     my $contents = '';
126     {
127         local $/ = undef;
128         open POD, $filename or die "Couldn't open $filename, $!";
129         $contents = <POD>;
130         close POD;
131     }
132
133     my $id = "${filename}:1:";
134
135     &name_synopsis($id, $filename, $contents)
136         unless $contents =~ /=for comment generic/
137             or $filename =~ m@man[157]/@;
138
139     print "$id doesn't start with =pod\n"
140         if $contents !~ /^=pod/;
141     print "$id doesn't end with =cut\n"
142         if $contents !~ /=cut\n$/;
143     print "$id more than one cut line.\n"
144         if $contents =~ /=cut.*=cut/ms;
145     print "$id missing copyright\n"
146         if $contents !~ /Copyright .* The OpenSSL Project Authors/;
147     print "$id copyright not last\n"
148         if $contents =~ /head1 COPYRIGHT.*=head/ms;
149     print "$id head2 in All uppercase\n"
150         if $contents =~ /head2\s+[A-Z ]+\n/;
151     print "$id extra space after head\n"
152         if $contents =~ /=head\d\s\s+/;
153     print "$id period in NAME section\n"
154         if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
155     print "$id POD markup in NAME section\n"
156         if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
157
158     # Look for multiple consecutive openssl #include lines.
159     # Consecutive because of files like md5.pod. Sometimes it's okay
160     # or necessary, as in ssl/SSL_set1_host.pod
161     if ( $contents !~ /=for comment multiple includes/ ) {
162         if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
163             my $count = 0;
164             foreach my $line ( split /\n+/, $1 ) {
165                 if ( $line =~ m@include <openssl/@ ) {
166                     if ( ++$count == 2 ) {
167                         print "$id has multiple includes\n";
168                     }
169                 } else {
170                     $count = 0;
171                 }
172             }
173         }
174     }
175
176     return unless $opt_s;
177
178     # Find what section this page is in.  If run from "." assume
179     # section 3.
180     my $section = 3;
181     $section = $1 if $dirname =~ /man([1-9])/;
182
183     foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
184         print "$id: missing $_ head1 section\n"
185             if $contents !~ /^=head1\s+${_}\s*$/m;
186     }
187
188     open my $OUT, '>', $temp
189         or die "Can't open $temp, $!";
190     podchecker($filename, $OUT);
191     close $OUT;
192     open $OUT, '<', $temp
193         or die "Can't read $temp, $!";
194     while ( <$OUT> ) {
195         next if /\(section\) in.*deprecated/;
196         print;
197     }
198     close $OUT;
199     unlink $temp || warn "Can't remove $temp, $!";
200 }
201
202 my %dups;
203
204 sub parsenum()
205 {
206     my $file = shift;
207     my @apis;
208
209     open my $IN, '<', $file
210         or die "Can't open $file, $!, stopped";
211
212     while ( <$IN> ) {
213         next if /\bNOEXIST\b/;
214         next if /\bEXPORT_VAR_AS_FUNC\b/;
215         push @apis, $1 if /([^\s]+).\s/;
216     }
217
218     close $IN;
219
220     print "# Found ", scalar(@apis), " in $file\n";
221     return sort @apis;
222 }
223
224 sub getdocced()
225 {
226     my $dir = shift;
227     my %return;
228
229     foreach my $pod ( glob("$dir/*.pod") ) {
230         my %podinfo = extract_pod_info($pod);
231         foreach my $n ( @{$podinfo{names}} ) {
232             $return{$n} = $pod;
233             print "# Duplicate $n in $pod and $dups{$n}\n"
234                 if defined $dups{$n} && $dups{$n} ne $pod;
235             $dups{$n} = $pod;
236         }
237     }
238
239     return %return;
240 }
241
242 my %docced;
243
244 sub printem()
245 {
246     my $libname = shift;
247     my $numfile = shift;
248     my $count = 0;
249
250     foreach my $func ( &parsenum($numfile) ) {
251         next if $docced{$func};
252
253         # Skip ASN1 utilities
254         next if $func =~ /^ASN1_/;
255
256         print "$libname:$func\n";
257         $count++;
258     }
259     print "# Found $count missing from $numfile\n\n";
260 }
261
262
263 getopts('nshu');
264
265 &help() if ( $opt_h );
266
267 die "Need one of -n -s or -u flags.\n"
268     unless $opt_n or $opt_s or $opt_u;
269
270 if ( $opt_n or $opt_s ) {
271     foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
272         &check($_);
273     }
274 }
275 if ( $opt_u ) {
276     my %temp = &getdocced('doc/man3');
277     foreach ( keys %temp ) {
278         $docced{$_} = $temp{$_};
279     }
280     &printem('crypto', 'util/libcrypto.num');
281     &printem('ssl', 'util/libssl.num');
282 }
283
284 exit;