Doc nits: callback function typedefs
[oweals/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 Getopt::Std;
17
18 our($opt_s);
19
20 my $temp = '/tmp/docnits.txt';
21 my $OUT;
22
23 my %mandatory_sections =
24     ( '*'    => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
25       1      => [ 'SYNOPSIS', 'OPTIONS' ],
26       3      => [ 'SYNOPSIS', 'RETURN VALUES' ],
27       5      => [ ],
28       7      => [ ] );
29 my %default_sections =
30     ( apps   => 1,
31       crypto => 3,
32       ssl    => 3 );
33
34 # Cross-check functions in the NAME and SYNOPSIS section.
35 sub name_synopsis()
36 {
37     my $id = shift;
38     my $filename = shift;
39     my $contents = shift;
40
41     # Get NAME section and all words in it.
42     return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
43     my $tmp = $1;
44     $tmp =~ tr/\n/ /;
45     $tmp =~ s/-.*//g;
46     $tmp =~ s/,//g;
47
48     my $dirname = dirname($filename);
49     my $simplename = basename($filename);
50     $simplename =~ s/.pod$//;
51     my $foundfilename = 0;
52     my %foundfilenames = ();
53     my %names;
54     foreach my $n ( split ' ', $tmp ) {
55         $names{$n} = 1;
56         $foundfilename++ if $n eq $simplename;
57         $foundfilenames{$n} = 1
58             if -f "$dirname/$n.pod" && $n ne $simplename;
59     }
60     print "$id the following exist as other .pod files:\n",
61         join(" ", sort keys %foundfilenames), "\n"
62         if %foundfilenames;
63     print "$id $simplename (filename) missing from NAME section\n",
64         unless $foundfilename;
65
66     # Find all functions in SYNOPSIS
67     return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
68     my $syn = $1;
69     foreach my $line ( split /\n+/, $syn ) {
70         my $sym;
71         $line =~ s/STACK_OF\([^)]+\)/int/g;
72         $line =~ s/__declspec\([^)]+\)//;
73         if ( $line =~ /env (\S*)=/ ) {
74             # environment variable env NAME=...
75             $sym = $1;
76         } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
77             # a callback function: typedef ... (*NAME)(...
78             $sym = $1;
79         } elsif ( $line =~ /typedef.* (\S+);/ ) {
80             # a simple typedef: typedef ... NAME;
81             $sym = $1;
82         } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
83             $sym = $1;
84         } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
85             $sym = $1;
86         }
87         else {
88             next;
89         }
90         print "$id $sym missing from NAME section\n"
91             unless defined $names{$sym};
92         $names{$sym} = 2;
93
94         # Do some sanity checks on the prototype.
95         print "$id prototype missing spaces around commas: $line\n"
96             if ( $line =~ /[a-z0-9],[^ ]/ );
97     }
98
99     foreach my $n ( keys %names ) {
100         next if $names{$n} == 2;
101         print "$id $n missing from SYNOPSIS\n";
102     }
103 }
104
105 sub check()
106 {
107     my $filename = shift;
108     my $dirname = basename(dirname($filename));
109
110     my $contents = '';
111     {
112         local $/ = undef;
113         open POD, $filename or die "Couldn't open $filename, $!";
114         $contents = <POD>;
115         close POD;
116     }
117
118     my $id = "${filename}:1:";
119
120     &name_synopsis($id, $filename, $contents)
121         unless $contents =~ /=for comment generic/
122             or $contents =~ /=for comment openssl_manual_section:7/
123             or $filename =~ m@/apps/@;
124
125     print "$id doesn't start with =pod\n"
126         if $contents !~ /^=pod/;
127     print "$id doesn't end with =cut\n"
128         if $contents !~ /=cut\n$/;
129     print "$id more than one cut line.\n"
130         if $contents =~ /=cut.*=cut/ms;
131     print "$id missing copyright\n"
132         if $contents !~ /Copyright .* The OpenSSL Project Authors/;
133     print "$id copyright not last\n"
134         if $contents =~ /head1 COPYRIGHT.*=head/ms;
135     print "$id head2 in All uppercase\n"
136         if $contents =~ /head2\s+[A-Z ]+\n/;
137     print "$id extra space after head\n"
138         if $contents =~ /=head\d\s\s+/;
139     print "$id period in NAME section\n"
140         if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
141     print "$id POD markup in NAME section\n"
142         if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
143
144     # Look for multiple consecutive openssl #include lines.
145     # Consecutive because of files like md5.pod. Sometimes it's okay
146     # or necessary, as in ssl/SSL_set1_host.pod
147     if ( $contents !~ /=for comment multiple includes/ ) {
148         if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
149             my $count = 0;
150             foreach my $line ( split /\n+/, $1 ) {
151                 if ( $line =~ m@include <openssl/@ ) {
152                     if ( ++$count == 2 ) {
153                         print "$id has multiple includes\n";
154                     }
155                 } else {
156                     $count = 0;
157                 }
158             }
159         }
160     }
161
162     return unless $opt_s;
163
164     # Find what section this page is in.  If run from "." assume
165     # section 3.
166     my $section = $default_sections{$dirname} || 3;
167     if ($contents =~ /^=for\s+comment\s+openssl_manual_section:\s*(\d+)\s*$/m) {
168         $section = $1;
169     }
170
171     foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
172         print "$id: missing $_ head1 section\n"
173             if $contents !~ /^=head1\s+${_}\s*$/m;
174     }
175
176     open my $OUT, '>', $temp
177         or die "Can't open $temp, $!";
178     podchecker($filename, $OUT);
179     close $OUT;
180     open $OUT, '<', $temp
181         or die "Can't read $temp, $!";
182     while ( <$OUT> ) {
183         next if /\(section\) in.*deprecated/;
184         print;
185     }
186     close $OUT;
187     unlink $temp || warn "Can't remove $temp, $!";
188 }
189
190 getopts('s');
191
192 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
193     &check($_);
194 }
195
196 exit;