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