2 # Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
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
16 use File::Spec::Functions;
18 use lib catdir(dirname($0), "perl");
19 use OpenSSL::Util::Pod;
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
39 my $temp = '/tmp/docnits.txt';
42 my %mandatory_sections =
43 ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
44 1 => [ 'SYNOPSIS', 'OPTIONS' ],
45 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
49 # Cross-check functions in the NAME and SYNOPSIS section.
56 # Get NAME section and all words in it.
57 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
63 my $dirname = dirname($filename);
64 my $simplename = basename($filename);
65 $simplename =~ s/.pod$//;
66 my $foundfilename = 0;
67 my %foundfilenames = ();
69 foreach my $n ( split ' ', $tmp ) {
71 $foundfilename++ if $n eq $simplename;
72 $foundfilenames{$n} = 1
73 if -f "$dirname/$n.pod" && $n ne $simplename;
75 print "$id the following exist as other .pod files:\n",
76 join(" ", sort keys %foundfilenames), "\n"
78 print "$id $simplename (filename) missing from NAME section\n",
79 unless $foundfilename;
81 # Find all functions in SYNOPSIS
82 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
84 foreach my $line ( split /\n+/, $syn ) {
86 $line =~ s/STACK_OF\([^)]+\)/int/g;
87 $line =~ s/__declspec\([^)]+\)//;
88 if ( $line =~ /env (\S*)=/ ) {
89 # environment variable env NAME=...
91 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
92 # a callback function: typedef ... (*NAME)(...
94 } elsif ( $line =~ /typedef.* (\S+);/ ) {
95 # a simple typedef: typedef ... NAME;
97 } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
99 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
105 print "$id $sym missing from NAME section\n"
106 unless defined $names{$sym};
109 # Do some sanity checks on the prototype.
110 print "$id prototype missing spaces around commas: $line\n"
111 if ( $line =~ /[a-z0-9],[^ ]/ );
114 foreach my $n ( keys %names ) {
115 next if $names{$n} == 2;
116 print "$id $n missing from SYNOPSIS\n";
122 my $filename = shift;
123 my $dirname = basename(dirname($filename));
128 open POD, $filename or die "Couldn't open $filename, $!";
133 my $id = "${filename}:1:";
135 &name_synopsis($id, $filename, $contents)
136 unless $contents =~ /=for comment generic/
137 or $filename =~ m@man[157]/@;
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;
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 ) {
164 foreach my $line ( split /\n+/, $1 ) {
165 if ( $line =~ m@include <openssl/@ ) {
166 if ( ++$count == 2 ) {
167 print "$id has multiple includes\n";
176 return unless $opt_s;
178 # Find what section this page is in. If run from "." assume
181 $section = $1 if $dirname =~ /man([1-9])/;
183 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
184 print "$id: missing $_ head1 section\n"
185 if $contents !~ /^=head1\s+${_}\s*$/m;
188 open my $OUT, '>', $temp
189 or die "Can't open $temp, $!";
190 podchecker($filename, $OUT);
192 open $OUT, '<', $temp
193 or die "Can't read $temp, $!";
195 next if /\(section\) in.*deprecated/;
199 unlink $temp || warn "Can't remove $temp, $!";
209 open my $IN, '<', $file
210 or die "Can't open $file, $!, stopped";
213 next if /\bNOEXIST\b/;
214 next if /\bEXPORT_VAR_AS_FUNC\b/;
215 push @apis, $1 if /([^\s]+).\s/;
220 print "# Found ", scalar(@apis), " in $file\n";
229 foreach my $pod ( glob("$dir/*.pod") ) {
230 my %podinfo = extract_pod_info($pod);
231 foreach my $n ( @{$podinfo{names}} ) {
233 print "# Duplicate $n in $pod and $dups{$n}\n"
234 if defined $dups{$n} && $dups{$n} ne $pod;
250 foreach my $func ( &parsenum($numfile) ) {
251 next if $docced{$func};
253 # Skip ASN1 utilities
254 next if $func =~ /^ASN1_/;
256 print "$libname:$func\n";
259 print "# Found $count missing from $numfile\n\n";
265 &help() if ( $opt_h );
267 die "Need one of -n -s or -u flags.\n"
268 unless $opt_n or $opt_s or $opt_u;
270 if ( $opt_n or $opt_s ) {
271 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
276 my %temp = &getdocced('doc/man3');
277 foreach ( keys %temp ) {
278 $docced{$_} = $temp{$_};
280 &printem('crypto', 'util/libcrypto.num');
281 &printem('ssl', 'util/libssl.num');