This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Atol can be strtol in disguise, so grok_atou.
[perl5.git] / autodoc.pl
CommitLineData
94bdecf9 1#!/usr/bin/perl -w
6294c161
DM
2#
3# Unconditionally regenerate:
4#
5# pod/perlintern.pod
6# pod/perlapi.pod
7#
8# from information stored in
9#
10# embed.fnc
11# plus all the .c and .h files listed in MANIFEST
12#
13# Has an optional arg, which is the directory to chdir to before reading
14# MANIFEST and *.[ch].
15#
52a9d53b 16# This script is invoked as part of 'make all'
151c3fe5 17#
f554dfc5
MH
18# '=head1' are the only headings looked for. If the first non-blank line after
19# the heading begins with a word character, it is considered to be the first
20# line of documentation that applies to the heading itself. That is, it is
21# output immediately after the heading, before the first function, and not
22# indented. The next input line that is a pod directive terminates this
23# heading-level documentation.
94bdecf9 24
56a0c332 25use strict;
a64c954a 26
7882b24a
NC
27if (@ARGV) {
28 my $workdir = shift;
29 chdir $workdir
30 or die "Couldn't chdir to '$workdir': $!";
31}
32require 'regen/regen_lib.pl';
e8e591c9 33require 'regen/embed_lib.pl';
7882b24a 34
94bdecf9 35#
346f75ff 36# See database of global and static function prototypes in embed.fnc
94bdecf9
JH
37# This is used to generate prototype headers under various configurations,
38# export symbols lists for different platforms, and macros to provide an
39# implicit interpreter context argument.
40#
41
6a235718 42my %docs;
5ce57792
NC
43my %funcflags;
44my %macro = (
45 ax => 1,
46 items => 1,
47 ix => 1,
48 svtype => 1,
49 );
50my %missing;
94bdecf9
JH
51
52my $curheader = "Unknown section";
53
54sub autodoc ($$) { # parse a file and extract documentation info
55 my($fh,$file) = @_;
151c3fe5 56 my($in, $doc, $line, $header_doc);
f554dfc5
MH
57
58 # Count lines easier
59 my $get_next_line = sub { $line++; return <$fh> };
60
94bdecf9 61FUNC:
f554dfc5 62 while (defined($in = $get_next_line->())) {
5ce57792
NC
63 if ($in =~ /^#\s*define\s+([A-Za-z_][A-Za-z_0-9]+)\(/ &&
64 ($file ne 'embed.h' || $file ne 'proto.h')) {
65 $macro{$1} = $file;
66 next FUNC;
67 }
94bdecf9
JH
68 if ($in=~ /^=head1 (.*)/) {
69 $curheader = $1;
151c3fe5 70
f554dfc5
MH
71 # If the next non-space line begins with a word char, then it is
72 # the start of heading-ldevel documentation.
73 if (defined($doc = $get_next_line->())) {
74 # Skip over empty lines
75 while ($doc =~ /^\s+$/) {
76 if (! defined($doc = $get_next_line->())) {
77 next FUNC;
78 }
79 }
80
151c3fe5
KW
81 if ($doc !~ /^\w/) {
82 $in = $doc;
83 redo FUNC;
84 }
85 $header_doc = $doc;
151c3fe5
KW
86
87 # Continue getting the heading-level documentation until read
88 # in any pod directive (or as a fail-safe, find a closing
89 # comment to this pod in a C language file
90HDR_DOC:
f554dfc5 91 while (defined($doc = $get_next_line->())) {
151c3fe5
KW
92 if ($doc =~ /^=\w/) {
93 $in = $doc;
94 redo FUNC;
95 }
151c3fe5
KW
96
97 if ($doc =~ m:^\s*\*/$:) {
98 warn "=cut missing? $file:$line:$doc";;
99 last HDR_DOC;
100 }
101 $header_doc .= $doc;
102 }
103 }
94bdecf9
JH
104 next FUNC;
105 }
78c9d763 106 if ($in =~ /^=for\s+apidoc\s+(.*?)\s*\n/) {
94bdecf9
JH
107 my $proto = $1;
108 $proto = "||$proto" unless $proto =~ /\|/;
109 my($flags, $ret, $name, @args) = split /\|/, $proto;
110 my $docs = "";
111DOC:
f554dfc5 112 while (defined($doc = $get_next_line->())) {
94bdecf9
JH
113 last DOC if $doc =~ /^=\w+/;
114 if ($doc =~ m:^\*/$:) {
115 warn "=cut missing? $file:$line:$doc";;
116 last DOC;
117 }
118 $docs .= $doc;
119 }
120 $docs = "\n$docs" if $docs and $docs !~ /^\n/;
5ce57792
NC
121
122 # Check the consistency of the flags
123 my ($embed_where, $inline_where);
124 my ($embed_may_change, $inline_may_change);
125
126 my $docref = delete $funcflags{$name};
127 if ($docref and %$docref) {
128 $embed_where = $docref->{flags} =~ /A/ ? 'api' : 'guts';
129 $embed_may_change = $docref->{flags} =~ /M/;
d4e99c76 130 $flags .= 'D' if $docref->{flags} =~ /D/;
5ce57792
NC
131 } else {
132 $missing{$name} = $file;
94bdecf9 133 }
5ce57792
NC
134 if ($flags =~ /m/) {
135 $inline_where = $flags =~ /A/ ? 'api' : 'guts';
136 $inline_may_change = $flags =~ /x/;
137
138 if (defined $embed_where && $inline_where ne $embed_where) {
139 warn "Function '$name' inconsistency: embed.fnc says $embed_where, Pod says $inline_where";
140 }
141
142 if (defined $embed_may_change
143 && $inline_may_change ne $embed_may_change) {
144 my $message = "Function '$name' inconsistency: ";
145 if ($embed_may_change) {
146 $message .= "embed.fnc says 'may change', Pod does not";
147 } else {
148 $message .= "Pod says 'may change', embed.fnc does not";
149 }
150 warn $message;
151 }
152 } elsif (!defined $embed_where) {
153 warn "Unable to place $name!\n";
154 next;
155 } else {
156 $inline_where = $embed_where;
157 $flags .= 'x' if $embed_may_change;
158 @args = @{$docref->{args}};
159 $ret = $docref->{retval};
94bdecf9 160 }
5ce57792
NC
161
162 $docs{$inline_where}{$curheader}{$name}
163 = [$flags, $docs, $ret, $file, @args];
164
151c3fe5
KW
165 # Create a special entry with an empty-string name for the
166 # heading-level documentation.
167 if (defined $header_doc) {
168 $docs{$inline_where}{$curheader}{""} = $header_doc;
169 undef $header_doc;
170 }
171
94bdecf9 172 if (defined $doc) {
e509e693 173 if ($doc =~ /^=(?:for|head)/) {
94bdecf9
JH
174 $in = $doc;
175 redo FUNC;
176 }
177 } else {
178 warn "$file:$line:$in";
179 }
180 }
181 }
182}
183
184sub docout ($$$) { # output the docs for one function
185 my($fh, $name, $docref) = @_;
186 my($flags, $docs, $ret, $file, @args) = @$docref;
d8c40edc 187 $name =~ s/\s*$//;
94bdecf9 188
d4e99c76
KW
189 if ($flags =~ /D/) {
190 $docs = "\n\nDEPRECATED! It is planned to remove this function from a
191future release of Perl. Do not use it for new code; remove it from
192existing code.\n\n$docs";
193 }
194 else {
58a428bb
KW
195 $docs = "\n\nNOTE: this function is experimental and may change or be
196removed without notice.\n\n$docs" if $flags =~ /x/;
d4e99c76 197 }
94bdecf9
JH
198 $docs .= "NOTE: the perl_ form of this function is deprecated.\n\n"
199 if $flags =~ /p/;
5afac1eb
BM
200 $docs .= "NOTE: this function must be explicitly called as Perl_$name with an aTHX_ parameter.\n\n"
201 if $flags =~ /o/;
94bdecf9 202
d8c40edc 203 print $fh "=item $name\nX<$name>\n$docs";
94bdecf9
JH
204
205 if ($flags =~ /U/) { # no usage
206 # nothing
207 } elsif ($flags =~ /s/) { # semicolon ("dTHR;")
208 print $fh "\t\t$name;\n\n";
209 } elsif ($flags =~ /n/) { # no args
210 print $fh "\t$ret\t$name\n\n";
211 } else { # full usage
dee6204d
FC
212 my $p = $flags =~ /o/; # no #define foo Perl_foo
213 my $n = "Perl_"x$p . $name;
214 my $large_ret = length $ret > 7;
215 my $indent_size = 7+8 # nroff: 7 under =head + 8 under =item
216 +8+($large_ret ? 1 + length $ret : 8)
217 +length($n) + 1;
218 my $indent;
219 print $fh "\t$ret" . ($large_ret ? ' ' : "\t") . "$n(";
06e9ce89
FC
220 my $long_args;
221 for (@args) {
d2086f64 222 if ($indent_size + 2 + length > 79) {
06e9ce89
FC
223 $long_args=1;
224 $indent_size -= length($n) - 3;
225 last;
226 }
227 }
228 my $args = '';
229 if ($p) {
230 $args = @args ? "pTHX_ " : "pTHX";
231 if ($long_args) { print $fh $args; $args = '' }
232 }
233 $long_args and print $fh "\n";
234 my $first = !$long_args;
dee6204d
FC
235 while () {
236 if (!@args or
237 length $args
d2086f64 238 && $indent_size + 3 + length($args[0]) + length $args > 79
dee6204d
FC
239 ) {
240 print $fh
241 $first ? '' : (
242 $indent //=
243 "\t".($large_ret ? " " x (1+length $ret) : "\t")
06e9ce89 244 ." "x($long_args ? 4 : 1 + length $n)
dee6204d
FC
245 ),
246 $args, (","x($args ne 'pTHX_ ') . "\n")x!!@args;
247 $args = $first = '';
248 }
249 @args or last;
250 $args .= ", "x!!(length $args && $args ne 'pTHX_ ')
251 . shift @args;
252 }
06e9ce89 253 if ($long_args) { print $fh "\n", substr $indent, 0, -4 }
dee6204d 254 print $fh ")\n\n";
94bdecf9
JH
255 }
256 print $fh "=for hackers\nFound in file $file\n\n";
257}
258
f83c6033
KW
259sub sort_helper {
260 # Do a case-insensitive dictionary sort, with only alphabetics
261 # significant, falling back to using everything for determinancy
262 return (uc($a =~ s/[[^:alpha]]//r) cmp uc($b =~ s/[[^:alpha]]//r))
263 || uc($a) cmp uc($b)
264 || $a cmp $b;
265}
266
7b73ff98 267sub output {
5a0155e6 268 my ($podname, $header, $dochash, $missing, $footer) = @_;
7882b24a
NC
269 my $fh = open_new("pod/$podname.pod", undef,
270 {by => "$0 extracting documentation",
f1f44974 271 from => 'the C source files'}, 1);
e0492643 272
7882b24a 273 print $fh $header;
e0492643 274
7b73ff98 275 my $key;
f83c6033 276 for $key (sort sort_helper keys %$dochash) {
7b73ff98 277 my $section = $dochash->{$key};
151c3fe5
KW
278 print $fh "\n=head1 $key\n\n";
279
280 # Output any heading-level documentation and delete so won't get in
281 # the way later
282 if (exists $section->{""}) {
283 print $fh $section->{""} . "\n";
284 delete $section->{""};
285 }
286 print $fh "=over 8\n\n";
287
f83c6033 288 for my $key (sort sort_helper keys %$section) {
7b73ff98
NC
289 docout($fh, $key, $section->{$key});
290 }
291 print $fh "\n=back\n";
292 }
293
5a0155e6 294 if (@$missing) {
a23e6e20 295 print $fh "\n=head1 Undocumented functions\n\n";
2616800a 296 print $fh $podname eq 'perlapi' ? <<'_EOB_' : <<'_EOB_';
474d0ac8 297The following functions have been flagged as part of the public API,
72d33970 298but are currently undocumented. Use them at your own risk, as the
ba4591a5
KW
299interfaces are subject to change. Functions that are not listed in this
300document are not intended for public use, and should NOT be used under any
301circumstances.
302
303If you use one of the undocumented functions below, you may wish to consider
72d33970
FC
304creating and submitting documentation
305for it. If your patch is accepted, this
ba4591a5
KW
306will indicate that the interface is stable (unless it is explicitly marked
307otherwise).
cf5f2f8f
KW
308
309=over
310
311_EOB_
2616800a
FC
312The following functions are currently undocumented. If you use one of
313them, you may wish to consider creating and submitting documentation for
314it.
315
316=over
317
318_EOB_
cf5f2f8f
KW
319 for my $missing (sort @$missing) {
320 print $fh "=item $missing\nX<$missing>\n\n";
5a0155e6 321 }
cf5f2f8f
KW
322 print $fh "=back\n\n";
323}
7882b24a 324 print $fh $footer, "=cut\n";
5a0155e6 325
7882b24a 326 read_only_bottom_close_and_rename($fh);
cd093254
MM
327}
328
e8e591c9
NC
329foreach (@{(setup_embed())[0]}) {
330 next if @$_ < 2;
331 my ($flags, $retval, $func, @args) = @$_;
332 s/\b(?:NN|NULLOK)\b\s+//g for @args;
bc350081 333
5ce57792
NC
334 $funcflags{$func} = {
335 flags => $flags,
336 retval => $retval,
337 args => \@args,
338 };
339}
340
5ce57792
NC
341# glob() picks up docs from extra .c or .h files that may be in unclean
342# development trees.
741c0772
NC
343open my $fh, '<', 'MANIFEST'
344 or die "Can't open MANIFEST: $!";
345while (my $line = <$fh>) {
346 next unless my ($file) = $line =~ /^(\S+\.[ch])\t/;
5ce57792 347
5ce57792
NC
348 open F, "< $file" or die "Cannot open $file for docs: $!\n";
349 $curheader = "Functions in file $file\n";
350 autodoc(\*F,$file);
351 close F or die "Error closing $file: $!\n";
352}
741c0772 353close $fh or die "Error whilst reading MANIFEST: $!";
5ce57792
NC
354
355for (sort keys %funcflags) {
356 next unless $funcflags{$_}{flags} =~ /d/;
357 warn "no docs for $_\n"
bc350081 358}
94bdecf9 359
5ce57792
NC
360foreach (sort keys %missing) {
361 next if $macro{$_};
362 # Heuristics for known not-a-function macros:
363 next if /^[A-Z]/;
364 next if /^dj?[A-Z]/;
365
366 warn "Function '$_', documented in $missing{$_}, not listed in embed.fnc";
94bdecf9
JH
367}
368
5ce57792
NC
369# walk table providing an array of components in each line to
370# subroutine, printing the result
371
8c869419
KW
372# List of funcs in the public API that aren't also marked as experimental nor
373# deprecated.
374my @missing_api = grep $funcflags{$_}{flags} =~ /A/ && $funcflags{$_}{flags} !~ /[MD]/ && !$docs{api}{$_}, keys %funcflags;
5a0155e6 375output('perlapi', <<'_EOB_', $docs{api}, \@missing_api, <<'_EOE_');
94bdecf9
JH
376=head1 NAME
377
378perlapi - autogenerated documentation for the perl public API
379
380=head1 DESCRIPTION
d8c40edc 381X<Perl API> X<API> X<api>
94bdecf9
JH
382
383This file contains the documentation of the perl public API generated by
ef9741a5 384F<embed.pl>, specifically a listing of functions, macros, flags, and variables
cf5f2f8f
KW
385that may be used by extension writers. L<At the end|/Undocumented functions>
386is a list of functions which have yet to be documented. The interfaces of
37a519b2 387those are subject to change without notice. Anything not listed here is
cf5f2f8f
KW
388not part of the public API, and should not be used by extension writers at
389all. For these reasons, blindly using functions listed in proto.h is to be
390avoided when writing extensions.
94bdecf9
JH
391
392Note that all Perl API global variables must be referenced with the C<PL_>
37a519b2
KW
393prefix. Again, those not listed here are not to be used by extension writers,
394and can be changed or removed without notice; same with macros.
395Some macros are provided for compatibility with the older,
94bdecf9
JH
396unadorned names, but this support may be disabled in a future release.
397
2bbc8d55
SP
398Perl was originally written to handle US-ASCII only (that is characters
399whose ordinal numbers are in the range 0 - 127).
400And documentation and comments may still use the term ASCII, when
401sometimes in fact the entire range from 0 - 255 is meant.
402
403Note that Perl can be compiled and run under EBCDIC (See L<perlebcdic>)
404or ASCII. Most of the documentation (and even comments in the code)
405ignore the EBCDIC possibility.
406For almost all purposes the differences are transparent.
407As an example, under EBCDIC,
408instead of UTF-8, UTF-EBCDIC is used to encode Unicode strings, and so
409whenever this documentation refers to C<utf8>
410(and variants of that name, including in function names),
411it also (essentially transparently) means C<UTF-EBCDIC>.
412But the ordinals of characters differ between ASCII, EBCDIC, and
413the UTF- encodings, and a string encoded in UTF-EBCDIC may occupy more bytes
414than in UTF-8.
415
2bbc8d55 416The listing below is alphabetical, case insensitive.
94bdecf9
JH
417
418_EOB_
419
94bdecf9
JH
420=head1 AUTHORS
421
422Until May 1997, this document was maintained by Jeff Okamoto
423<okamoto@corp.hp.com>. It is now maintained as part of Perl itself.
424
425With lots of help and suggestions from Dean Roehrich, Malcolm Beattie,
426Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, Neil
427Bowers, Matthew Green, Tim Bunce, Spider Boardman, Ulrich Pfeifer,
428Stephen McCamant, and Gurusamy Sarathy.
429
430API Listing originally by Dean Roehrich <roehrich@cray.com>.
431
432Updated to be autogenerated from comments in the source by Benjamin Stuhl.
433
434=head1 SEE ALSO
435
b92fc6c1 436L<perlguts>, L<perlxs>, L<perlxstut>, L<perlintern>
94bdecf9
JH
437
438_EOE_
439
79fc8511
FC
440# List of non-static internal functions
441my @missing_guts =
442 grep $funcflags{$_}{flags} !~ /[As]/ && !$docs{guts}{$_}, keys %funcflags;
5a0155e6
TC
443
444output('perlintern', <<'END', $docs{guts}, \@missing_guts, <<'END');
94bdecf9
JH
445=head1 NAME
446
447perlintern - autogenerated documentation of purely B<internal>
448 Perl functions
449
450=head1 DESCRIPTION
d8c40edc 451X<internal Perl functions> X<interpreter functions>
94bdecf9
JH
452
453This file is the autogenerated documentation of functions in the
454Perl interpreter that are documented using Perl's internal documentation
154e47c8 455format but are not marked as part of the Perl API. In other words,
94bdecf9
JH
456B<they are not for use in extensions>!
457
458END
459
94bdecf9
JH
460=head1 AUTHORS
461
462The autodocumentation system was originally added to the Perl core by
154e47c8 463Benjamin Stuhl. Documentation is by whoever was kind enough to
94bdecf9
JH
464document their functions.
465
466=head1 SEE ALSO
467
b92fc6c1 468L<perlguts>, L<perlapi>
94bdecf9
JH
469
470END