This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
autodoc.pl: Make 's' flag independent of 'n'
[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}
3d7c117d
MB
32require './regen/regen_lib.pl';
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 =~ /\|/;
73060c4e 109 my($flags, $ret, $name, @args) = split /\s*\|\s*/, $proto;
94bdecf9
JH
110 my $docs = "";
111DOC:
f554dfc5 112 while (defined($doc = $get_next_line->())) {
72d4186d
KW
113
114 # Other pod commands are considered part of the current
115 # function's docs, so can have lists, etc.
116 last DOC if $doc =~ /^=(cut|for\s+apidoc|head)/;
94bdecf9
JH
117 if ($doc =~ m:^\*/$:) {
118 warn "=cut missing? $file:$line:$doc";;
119 last DOC;
72d4186d 120 }
94bdecf9
JH
121 $docs .= $doc;
122 }
123 $docs = "\n$docs" if $docs and $docs !~ /^\n/;
5ce57792
NC
124
125 # Check the consistency of the flags
126 my ($embed_where, $inline_where);
127 my ($embed_may_change, $inline_may_change);
128
d40e888a
KW
129 my $embed_docref = delete $funcflags{$name};
130 if ($embed_docref and %$embed_docref) {
131 $embed_where = $embed_docref->{flags} =~ /A/ ? 'api' : 'guts';
e4aeb12f 132 $embed_may_change = $embed_docref->{flags} =~ /x/;
d40e888a 133 $flags .= 'D' if $embed_docref->{flags} =~ /D/;
fb7c7bc0 134 $flags .= 'O' if $embed_docref->{flags} =~ /O/;
54c193ae
KW
135 $flags .= 'p' if $embed_docref->{flags} =~ /p/;
136 $flags .= 'M' if $embed_docref->{flags} =~ /M/;
d7cc3209 137 $flags .= 'T' if $embed_docref->{flags} =~ /T/;
5ce57792
NC
138 } else {
139 $missing{$name} = $file;
94bdecf9 140 }
5ce57792
NC
141 if ($flags =~ /m/) {
142 $inline_where = $flags =~ /A/ ? 'api' : 'guts';
143 $inline_may_change = $flags =~ /x/;
144
145 if (defined $embed_where && $inline_where ne $embed_where) {
146 warn "Function '$name' inconsistency: embed.fnc says $embed_where, Pod says $inline_where";
147 }
148
149 if (defined $embed_may_change
150 && $inline_may_change ne $embed_may_change) {
151 my $message = "Function '$name' inconsistency: ";
152 if ($embed_may_change) {
153 $message .= "embed.fnc says 'may change', Pod does not";
154 } else {
155 $message .= "Pod says 'may change', embed.fnc does not";
156 }
157 warn $message;
158 }
159 } elsif (!defined $embed_where) {
160 warn "Unable to place $name!\n";
161 next;
162 } else {
163 $inline_where = $embed_where;
164 $flags .= 'x' if $embed_may_change;
d40e888a
KW
165 @args = @{$embed_docref->{args}};
166 $ret = $embed_docref->{retval};
94bdecf9 167 }
5ce57792 168
7a6610ca
DM
169 if (exists $docs{$inline_where}{$curheader}{$name}) {
170 warn "$0: duplicate API entry for '$name' in $inline_where/$curheader\n";
171 next;
172 }
5ce57792
NC
173 $docs{$inline_where}{$curheader}{$name}
174 = [$flags, $docs, $ret, $file, @args];
175
151c3fe5
KW
176 # Create a special entry with an empty-string name for the
177 # heading-level documentation.
178 if (defined $header_doc) {
179 $docs{$inline_where}{$curheader}{""} = $header_doc;
180 undef $header_doc;
181 }
182
94bdecf9 183 if (defined $doc) {
e509e693 184 if ($doc =~ /^=(?:for|head)/) {
94bdecf9
JH
185 $in = $doc;
186 redo FUNC;
187 }
188 } else {
189 warn "$file:$line:$in";
190 }
191 }
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"
fb7c7bc0 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/;
94bdecf9 225 # nothing
05ca4832 226 } else {
8b5ff177 227 if ($flags =~ /n/) { # no args
1ded1f42
KW
228 print $fh "\t$ret\t$name";
229 } else { # full usage
1ded1f42
KW
230 my $n = "Perl_"x$p . $name;
231 my $large_ret = length $ret > 7;
232 my $indent_size = 7+8 # nroff: 7 under =head + 8 under =item
233 +8+($large_ret ? 1 + length $ret : 8)
234 +length($n) + 1;
235 my $indent;
236 print $fh "\t$ret" . ($large_ret ? ' ' : "\t") . "$n(";
237 my $long_args;
238 for (@args) {
239 if ($indent_size + 2 + length > 79) {
240 $long_args=1;
241 $indent_size -= length($n) - 3;
242 last;
243 }
244 }
245 my $args = '';
d7cc3209 246 if ($p && $flags !~ /T/) {
1ded1f42
KW
247 $args = @args ? "pTHX_ " : "pTHX";
248 if ($long_args) { print $fh $args; $args = '' }
249 }
250 $long_args and print $fh "\n";
251 my $first = !$long_args;
252 while () {
253 if (!@args or
254 length $args
255 && $indent_size + 3 + length($args[0]) + length $args > 79
256 ) {
257 print $fh
258 $first ? '' : (
259 $indent //=
260 "\t".($large_ret ? " " x (1+length $ret) : "\t")
261 ." "x($long_args ? 4 : 1 + length $n)
262 ),
263 $args, (","x($args ne 'pTHX_ ') . "\n")x!!@args;
264 $args = $first = '';
265 }
266 @args or last;
267 $args .= ", "x!!(length $args && $args ne 'pTHX_ ')
268 . shift @args;
269 }
270 if ($long_args) { print $fh "\n", substr $indent, 0, -4 }
271 print $fh ")";
272 }
8b5ff177 273 print $fh ";" if $flags =~ /s/; # semicolon "dTHR;"
1ded1f42 274 print $fh "\n\n";
94bdecf9
JH
275 }
276 print $fh "=for hackers\nFound in file $file\n\n";
277}
278
f83c6033
KW
279sub sort_helper {
280 # Do a case-insensitive dictionary sort, with only alphabetics
281 # significant, falling back to using everything for determinancy
1354d57e 282 return (uc($a =~ s/[[:^alpha:]]//r) cmp uc($b =~ s/[[:^alpha:]]//r))
f83c6033
KW
283 || uc($a) cmp uc($b)
284 || $a cmp $b;
285}
286
7b73ff98 287sub output {
5a0155e6 288 my ($podname, $header, $dochash, $missing, $footer) = @_;
6a4c4cd4
DM
289 #
290 # strip leading '|' from each line which had been used to hide
291 # pod from pod checkers.
292 s/^\|//gm for $header, $footer;
293
7882b24a
NC
294 my $fh = open_new("pod/$podname.pod", undef,
295 {by => "$0 extracting documentation",
f1f44974 296 from => 'the C source files'}, 1);
e0492643 297
7882b24a 298 print $fh $header;
e0492643 299
7b73ff98 300 my $key;
f83c6033 301 for $key (sort sort_helper keys %$dochash) {
7b73ff98 302 my $section = $dochash->{$key};
151c3fe5
KW
303 print $fh "\n=head1 $key\n\n";
304
305 # Output any heading-level documentation and delete so won't get in
306 # the way later
307 if (exists $section->{""}) {
308 print $fh $section->{""} . "\n";
309 delete $section->{""};
310 }
311 print $fh "=over 8\n\n";
312
f83c6033 313 for my $key (sort sort_helper keys %$section) {
7b73ff98
NC
314 docout($fh, $key, $section->{$key});
315 }
316 print $fh "\n=back\n";
317 }
318
5a0155e6 319 if (@$missing) {
a23e6e20 320 print $fh "\n=head1 Undocumented functions\n\n";
2616800a 321 print $fh $podname eq 'perlapi' ? <<'_EOB_' : <<'_EOB_';
474d0ac8 322The following functions have been flagged as part of the public API,
72d33970 323but are currently undocumented. Use them at your own risk, as the
ba4591a5
KW
324interfaces are subject to change. Functions that are not listed in this
325document are not intended for public use, and should NOT be used under any
326circumstances.
327
5a4fed09
KW
328If you feel you need to use one of these functions, first send email to
329L<perl5-porters@perl.org|mailto:perl5-porters@perl.org>. It may be
330that there is a good reason for the function not being documented, and it
331should be removed from this list; or it may just be that no one has gotten
332around to documenting it. In the latter case, you will be asked to submit a
333patch to document the function. Once your patch is accepted, it will indicate
334that the interface is stable (unless it is explicitly marked otherwise) and
335usable by you.
cf5f2f8f 336_EOB_
2616800a
FC
337The following functions are currently undocumented. If you use one of
338them, you may wish to consider creating and submitting documentation for
339it.
2616800a 340_EOB_
6a4c4cd4
DM
341 print $fh "\n=over\n\n";
342
cf5f2f8f
KW
343 for my $missing (sort @$missing) {
344 print $fh "=item $missing\nX<$missing>\n\n";
5a0155e6 345 }
cf5f2f8f
KW
346 print $fh "=back\n\n";
347}
7882b24a 348 print $fh $footer, "=cut\n";
5a0155e6 349
7882b24a 350 read_only_bottom_close_and_rename($fh);
cd093254
MM
351}
352
e8e591c9
NC
353foreach (@{(setup_embed())[0]}) {
354 next if @$_ < 2;
355 my ($flags, $retval, $func, @args) = @$_;
356 s/\b(?:NN|NULLOK)\b\s+//g for @args;
bc350081 357
5ce57792
NC
358 $funcflags{$func} = {
359 flags => $flags,
360 retval => $retval,
361 args => \@args,
362 };
363}
364
5ce57792
NC
365# glob() picks up docs from extra .c or .h files that may be in unclean
366# development trees.
741c0772
NC
367open my $fh, '<', 'MANIFEST'
368 or die "Can't open MANIFEST: $!";
369while (my $line = <$fh>) {
370 next unless my ($file) = $line =~ /^(\S+\.[ch])\t/;
5ce57792 371
1ae6ead9 372 open F, '<', $file or die "Cannot open $file for docs: $!\n";
5ce57792
NC
373 $curheader = "Functions in file $file\n";
374 autodoc(\*F,$file);
375 close F or die "Error closing $file: $!\n";
376}
741c0772 377close $fh or die "Error whilst reading MANIFEST: $!";
5ce57792
NC
378
379for (sort keys %funcflags) {
380 next unless $funcflags{$_}{flags} =~ /d/;
381 warn "no docs for $_\n"
bc350081 382}
94bdecf9 383
5ce57792
NC
384foreach (sort keys %missing) {
385 next if $macro{$_};
386 # Heuristics for known not-a-function macros:
387 next if /^[A-Z]/;
388 next if /^dj?[A-Z]/;
389
390 warn "Function '$_', documented in $missing{$_}, not listed in embed.fnc";
94bdecf9
JH
391}
392
5ce57792
NC
393# walk table providing an array of components in each line to
394# subroutine, printing the result
395
8c869419
KW
396# List of funcs in the public API that aren't also marked as experimental nor
397# deprecated.
e4aeb12f 398my @missing_api = grep $funcflags{$_}{flags} =~ /A/ && $funcflags{$_}{flags} !~ /[xD]/ && !$docs{api}{$_}, keys %funcflags;
5a0155e6 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|
409|This file contains the documentation of the perl public API generated by
410|F<embed.pl>, specifically a listing of functions, macros, flags, and variables
411|that may be used by extension writers. L<At the end|/Undocumented functions>
412|is a list of functions which have yet to be documented. The interfaces of
413|those are subject to change without notice. Anything not listed here is
414|not part of the public API, and should not be used by extension writers at
415|all. For these reasons, blindly using functions listed in proto.h is to be
416|avoided when writing extensions.
417|
418|In Perl, unlike C, a string of characters may generally contain embedded
419|C<NUL> characters. Sometimes in the documentation a Perl string is referred
420|to as a "buffer" to distinguish it from a C string, but sometimes they are
421|both just referred to as strings.
422|
423|Note that all Perl API global variables must be referenced with the C<PL_>
424|prefix. Again, those not listed here are not to be used by extension writers,
425|and can be changed or removed without notice; same with macros.
426|Some macros are provided for compatibility with the older,
427|unadorned names, but this support may be disabled in a future release.
428|
429|Perl was originally written to handle US-ASCII only (that is characters
430|whose ordinal numbers are in the range 0 - 127).
431|And documentation and comments may still use the term ASCII, when
432|sometimes in fact the entire range from 0 - 255 is meant.
433|
434|The non-ASCII characters below 256 can have various meanings, depending on
435|various things. (See, most notably, L<perllocale>.) But usually the whole
436|range can be referred to as ISO-8859-1. Often, the term "Latin-1" (or
437|"Latin1") is used as an equivalent for ISO-8859-1. But some people treat
438|"Latin1" as referring just to the characters in the range 128 through 255, or
439|somethimes from 160 through 255.
440|This documentation uses "Latin1" and "Latin-1" to refer to all 256 characters.
441|
442|Note that Perl can be compiled and run under either ASCII or EBCDIC (See
443|L<perlebcdic>). Most of the documentation (and even comments in the code)
444|ignore the EBCDIC possibility.
445|For almost all purposes the differences are transparent.
446|As an example, under EBCDIC,
447|instead of UTF-8, UTF-EBCDIC is used to encode Unicode strings, and so
448|whenever this documentation refers to C<utf8>
449|(and variants of that name, including in function names),
450|it also (essentially transparently) means C<UTF-EBCDIC>.
451|But the ordinals of characters differ between ASCII, EBCDIC, and
452|the UTF- encodings, and a string encoded in UTF-EBCDIC may occupy a different
453|number of bytes than in UTF-8.
454|
455|The listing below is alphabetical, case insensitive.
456|
94bdecf9 457_EOB_
6a4c4cd4
DM
458|
459|=head1 AUTHORS
460|
461|Until May 1997, this document was maintained by Jeff Okamoto
462|<okamoto@corp.hp.com>. It is now maintained as part of Perl itself.
463|
464|With lots of help and suggestions from Dean Roehrich, Malcolm Beattie,
465|Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, Neil
466|Bowers, Matthew Green, Tim Bunce, Spider Boardman, Ulrich Pfeifer,
467|Stephen McCamant, and Gurusamy Sarathy.
468|
469|API Listing originally by Dean Roehrich <roehrich@cray.com>.
470|
471|Updated to be autogenerated from comments in the source by Benjamin Stuhl.
472|
473|=head1 SEE ALSO
474|
475|L<perlguts>, L<perlxs>, L<perlxstut>, L<perlintern>
476|
94bdecf9
JH
477_EOE_
478
79fc8511
FC
479# List of non-static internal functions
480my @missing_guts =
9f589e47 481 grep $funcflags{$_}{flags} !~ /[AS]/ && !$docs{guts}{$_}, keys %funcflags;
5a0155e6
TC
482
483output('perlintern', <<'END', $docs{guts}, \@missing_guts, <<'END');
6a4c4cd4
DM
484|=head1 NAME
485|
486|perlintern - autogenerated documentation of purely B<internal>
487| Perl functions
488|
489|=head1 DESCRIPTION
490|X<internal Perl functions> X<interpreter functions>
491|
492|This file is the autogenerated documentation of functions in the
493|Perl interpreter that are documented using Perl's internal documentation
494|format but are not marked as part of the Perl API. In other words,
495|B<they are not for use in extensions>!
496|
94bdecf9 497END
6a4c4cd4
DM
498|
499|=head1 AUTHORS
500|
501|The autodocumentation system was originally added to the Perl core by
502|Benjamin Stuhl. Documentation is by whoever was kind enough to
503|document their functions.
504|
505|=head1 SEE ALSO
506|
507|L<perlguts>, L<perlapi>
508|
94bdecf9 509END