fc795b8789b6149d584a4bfcfb51f8bc4bb8db83
[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 =~ /typedef.* (\S+);/ ) {
89             $sym = $1;
90         } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
91             $sym = $1;
92         } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
93             $sym = $1;
94         }
95         else {
96             next;
97         }
98         print "$id $sym missing from NAME section\n"
99             unless defined $names{$sym};
100         $names{$sym} = 2;
101
102         # Do some sanity checks on the prototype.
103         print "$id prototype missing spaces around commas: $line\n"
104             if ( $line =~ /[a-z0-9],[^ ]/ );
105     }
106
107     foreach my $n ( keys %names ) {
108         next if $names{$n} == 2;
109         print "$id $n missing from SYNOPSIS\n";
110     }
111 }
112
113 sub check()
114 {
115     my $filename = shift;
116     my $dirname = basename(dirname($filename));
117
118     my $contents = '';
119     {
120         local $/ = undef;
121         open POD, $filename or die "Couldn't open $filename, $!";
122         $contents = <POD>;
123         close POD;
124     }
125
126     my $id = "${filename}:1:";
127
128     &name_synopsis($id, $filename, $contents)
129         unless $contents =~ /=for comment generic/
130             or $filename =~ m@man[157]/@;
131
132     print "$id doesn't start with =pod\n"
133         if $contents !~ /^=pod/;
134     print "$id doesn't end with =cut\n"
135         if $contents !~ /=cut\n$/;
136     print "$id more than one cut line.\n"
137         if $contents =~ /=cut.*=cut/ms;
138     print "$id missing copyright\n"
139         if $contents !~ /Copyright .* The OpenSSL Project Authors/;
140     print "$id copyright not last\n"
141         if $contents =~ /head1 COPYRIGHT.*=head/ms;
142     print "$id head2 in All uppercase\n"
143         if $contents =~ /head2\s+[A-Z ]+\n/;
144     print "$id extra space after head\n"
145         if $contents =~ /=head\d\s\s+/;
146     print "$id period in NAME section\n"
147         if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
148     print "$id POD markup in NAME section\n"
149         if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
150
151     # Look for multiple consecutive openssl #include lines.
152     # Consecutive because of files like md5.pod. Sometimes it's okay
153     # or necessary, as in ssl/SSL_set1_host.pod
154     if ( $contents !~ /=for comment multiple includes/ ) {
155         if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
156             my $count = 0;
157             foreach my $line ( split /\n+/, $1 ) {
158                 if ( $line =~ m@include <openssl/@ ) {
159                     if ( ++$count == 2 ) {
160                         print "$id has multiple includes\n";
161                     }
162                 } else {
163                     $count = 0;
164                 }
165             }
166         }
167     }
168
169     return unless $opt_s;
170
171     # Find what section this page is in.  If run from "." assume
172     # section 3.
173     my $section = 3;
174     $section = $1 if $dirname =~ /man([1-9])/;
175
176     foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
177         print "$id: missing $_ head1 section\n"
178             if $contents !~ /^=head1\s+${_}\s*$/m;
179     }
180
181     open my $OUT, '>', $temp
182         or die "Can't open $temp, $!";
183     podchecker($filename, $OUT);
184     close $OUT;
185     open $OUT, '<', $temp
186         or die "Can't read $temp, $!";
187     while ( <$OUT> ) {
188         next if /\(section\) in.*deprecated/;
189         print;
190     }
191     close $OUT;
192     unlink $temp || warn "Can't remove $temp, $!";
193 }
194
195 my %dups;
196
197 sub parsenum()
198 {
199     my $file = shift;
200     my @apis;
201
202     open my $IN, '<', $file
203         or die "Can't open $file, $!, stopped";
204
205     while ( <$IN> ) {
206         next if /\bNOEXIST\b/;
207         next if /\bEXPORT_VAR_AS_FUNC\b/;
208         push @apis, $1 if /([^\s]+).\s/;
209     }
210
211     close $IN;
212
213     print "# Found ", scalar(@apis), " in $file\n";
214     return sort @apis;
215 }
216
217 sub getdocced()
218 {
219     my $dir = shift;
220     my %return;
221
222     foreach my $pod ( glob("$dir/*.pod") ) {
223         my %podinfo = extract_pod_info($pod);
224         foreach my $n ( @{$podinfo{names}} ) {
225             $return{$n} = $pod;
226             print "# Duplicate $n in $pod and $dups{$n}\n"
227                 if defined $dups{$n} && $dups{$n} ne $pod;
228             $dups{$n} = $pod;
229         }
230     }
231
232     return %return;
233 }
234
235 my %docced;
236
237 sub printem()
238 {
239     my $libname = shift;
240     my $numfile = shift;
241     my $count = 0;
242
243     foreach my $func ( &parsenum($numfile) ) {
244         next if $docced{$func};
245
246         # Skip ASN1 utilities
247         next if $func =~ /^ASN1_/;
248
249         print "$libname:$func\n";
250         $count++;
251     }
252     print "# Found $count missing from $numfile\n\n";
253 }
254
255
256 getopts('nshu');
257
258 &help() if ( $opt_h );
259
260 die "Need one of -n -s or -u flags.\n"
261     unless $opt_n or $opt_s or $opt_u;
262
263 if ( $opt_n or $opt_s ) {
264     foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
265         &check($_);
266     }
267 }
268 if ( $opt_u ) {
269     my %temp = &getdocced('doc/man3');
270     foreach ( keys %temp ) {
271         $docced{$_} = $temp{$_};
272     }
273     &printem('crypto', 'util/libcrypto.num');
274     &printem('ssl', 'util/libssl.num');
275 }
276
277 exit;