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