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