forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autodoc.pl
2069 lines (1775 loc) · 80.3 KB
/
autodoc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
use Text::Tabs;
#
# Unconditionally regenerate:
#
# pod/perlintern.pod
# pod/perlapi.pod
#
# from information stored in
#
# embed.fnc
# plus all the core .c, .h, and .pod files listed in MANIFEST
# plus %extra_input_pods
my %extra_input_pods = ( 'dist/ExtUtils-ParseXS/lib/perlxs.pod' => 1 );
# Has an optional arg, which is the directory to chdir to before reading
# MANIFEST and the files
#
# This script is invoked as part of 'make all'
#
# The generated pod consists of sections of related elements, functions,
# macros, and variables. The keys of %valid_sections give the current legal
# ones. Just add a new key to add a section.
#
# Throughout the files read by this script are lines like
#
# =for apidoc_section Section Name
# =for apidoc_section $section_name_variable
#
# "Section Name" (after having been stripped of leading space) must be one of
# the legal section names, or an error is thrown. $section_name_variable must
# be one of the legal section name variables defined below; these expand to
# legal section names. This form is used so that minor wording changes in
# these titles can be confined to this file. All the names of the variables
# end in '_scn'; this suffix is optional in the apidoc_section lines.
#
# All API elements defined between this line and the next 'apidoc_section'
# line will go into the section "Section Name" (or $section_name_variable),
# sorted by dictionary order within it. perlintern and perlapi are parallel
# documents, each potentially with a section "Section Name". Each element is
# marked as to which document it goes into. If there are none for a
# particular section in perlapi, that section is omitted.
#
# Also, in .[ch] files, there may be
#
# =head1 Section Name
#
# lines in comments. These are also used by this program to switch to section
# "Section Name". The difference is that if there are any lines after the
# =head1, inside the same comment, and before any =for apidoc-ish lines, they
# are used as a heading for section "Section Name" (in both perlintern and
# perlapi). This includes any =head[2-5]. If more than one '=head1 Section
# Name' line has content, they appear in the generated pod in an undefined
# order. Note that you can't use a $section_name_variable in =head1 lines
#
# The next =head1, =for apidoc_section, or file end terminates what goes into
# the current section
#
# The %valid_sections hash below also can have header content, which will
# appear before any =head1 content. The hash can also have footer content
# content, which will appear at the end of the section, after all the
# elements.
#
# The lines that define the actual functions, etc are documented in embed.fnc,
# because they have flags which must be kept in sync with that file.
use strict;
use warnings;
my $nroff_min_indent = 4; # for non-heading lines
# 80 column terminal - 2 for pager adding 2 columns;
my $max_width = 80 - 2 - $nroff_min_indent;
my $standard_indent = 4; # Any additional indentations
if (@ARGV) {
my $workdir = shift;
chdir $workdir
or die "Couldn't chdir to '$workdir': $!";
}
require './regen/regen_lib.pl';
require './regen/embed_lib.pl';
my %described_elsewhere;
#
# See database of global and static function prototypes in embed.fnc
# This is used to generate prototype headers under various configurations,
# export symbols lists for different platforms, and macros to provide an
# implicit interpreter context argument.
#
my %docs;
my %seen;
my %funcflags;
my %missing;
my %missing_macros;
my $link_text = "Described in";
my $description_indent = 4;
my $usage_indent = 3; # + initial blank yields 4 total
my $AV_scn = 'AV Handling';
my $callback_scn = 'Callback Functions';
my $casting_scn = 'Casting';
my $casing_scn = 'Character case changing';
my $classification_scn = 'Character classification';
my $names_scn = 'Character names';
my $scope_scn = 'Compile-time scope hooks';
my $compiler_scn = 'Compiler and Preprocessor information';
my $directives_scn = 'Compiler directives';
my $concurrency_scn = 'Concurrency';
my $COP_scn = 'COPs and Hint Hashes';
my $CV_scn = 'CV Handling';
my $custom_scn = 'Custom Operators';
my $debugging_scn = 'Debugging';
my $display_scn = 'Display functions';
my $embedding_scn = 'Embedding, Threads, and Interpreter Cloning';
my $errno_scn = 'Errno';
my $exceptions_scn = 'Exception Handling (simple) Macros';
my $filesystem_scn = 'Filesystem configuration values';
my $filters_scn = 'Source Filters';
my $floating_scn = 'Floating point';
my $genconfig_scn = 'General Configuration';
my $globals_scn = 'Global Variables';
my $GV_scn = 'GV Handling and Stashes';
my $hook_scn = 'Hook manipulation';
my $HV_scn = 'HV Handling';
my $io_scn = 'Input/Output';
my $io_formats_scn = 'I/O Formats';
my $integer_scn = 'Integer';
my $lexer_scn = 'Lexer interface';
my $locale_scn = 'Locales';
my $magic_scn = 'Magic';
my $memory_scn = 'Memory Management';
my $MRO_scn = 'MRO';
my $multicall_scn = 'Multicall Functions';
my $numeric_scn = 'Numeric Functions';
# Now combined, as unclear which functions go where, but separate names kept
# to avoid 1) other code changes; 2) in case it seems better to split again
my $optrees_scn = 'Optrees';
my $optree_construction_scn = $optrees_scn; # Was 'Optree construction';
my $optree_manipulation_scn = $optrees_scn; # Was 'Optree Manipulation Functions'
my $pack_scn = 'Pack and Unpack';
my $pad_scn = 'Pad Data Structures';
my $password_scn = 'Password and Group access';
my $reports_scn = 'Reports and Formats';
my $paths_scn = 'Paths to system commands';
my $prototypes_scn = 'Prototype information';
my $regexp_scn = 'REGEXP Functions';
my $signals_scn = 'Signals';
my $site_scn = 'Site configuration';
my $sockets_scn = 'Sockets configuration values';
my $stack_scn = 'Stack Manipulation Macros';
my $string_scn = 'String Handling';
my $SV_flags_scn = 'SV Flags';
my $SV_scn = 'SV Handling';
my $tainting_scn = 'Tainting';
my $time_scn = 'Time';
my $typedefs_scn = 'Typedef names';
my $unicode_scn = 'Unicode Support';
my $utility_scn = 'Utility Functions';
my $versioning_scn = 'Versioning';
my $warning_scn = 'Warning and Dieing';
my $XS_scn = 'XS';
# Kept separate at end
my $undocumented_scn = 'Undocumented elements';
my %valid_sections = (
$AV_scn => {},
$callback_scn => {},
$casting_scn => {},
$casing_scn => {},
$classification_scn => {},
$scope_scn => {},
$compiler_scn => {},
$directives_scn => {},
$concurrency_scn => {},
$COP_scn => {},
$CV_scn => {
header => <<~'EOT',
This section documents functions to manipulate CVs which are
code-values, meaning subroutines. For more information, see
L<perlguts>.
EOT
},
$custom_scn => {},
$debugging_scn => {},
$display_scn => {},
$embedding_scn => {},
$errno_scn => {},
$exceptions_scn => {},
$filesystem_scn => {
header => <<~'EOT',
Also see L</List of capability HAS_foo symbols>.
EOT
},
$filters_scn => {},
$floating_scn => {
header => <<~'EOT',
Also L</List of capability HAS_foo symbols> lists capabilities
that arent in this section. For example C<HAS_ASINH>, for the
hyperbolic sine function.
EOT
},
$genconfig_scn => {
header => <<~'EOT',
This section contains configuration information not otherwise
found in the more specialized sections of this document. At the
end is a list of C<#defines> whose name should be enough to tell
you what they do, and a list of #defines which tell you if you
need to C<#include> files to get the corresponding functionality.
EOT
footer => <<~EOT,
=head2 List of capability C<HAS_I<foo>> symbols
This is a list of those symbols that dont appear elsewhere in ths
document that indicate if the current platform has a certain
capability. Their names all begin with C<HAS_>. Only those
symbols whose capability is directly derived from the name are
listed here. All others have their meaning expanded out elsewhere
in this document. This (relatively) compact list is because we
think that the expansion would add little or no value and take up
a lot of space (because there are so many). If you think certain
ones should be expanded, send email to
L<perl5-porters\@perl.org|mailto:perl5-porters\@perl.org>.
Each symbol here will be C<#define>d if and only if the platform
has the capability. If you need more detail, see the
corresponding entry in F<config.h>. For convenience, the list is
split so that the ones that indicate there is a reentrant version
of a capability are listed separately
__HAS_LIST__
And, the reentrant capabilities:
__HAS_R_LIST__
Example usage:
=over $standard_indent
#ifdef HAS_STRNLEN
use strnlen()
#else
use an alternative implementation
#endif
=back
=head2 List of C<#include> needed symbols
This list contains symbols that indicate if certain C<#include>
files are present on the platform. If your code accesses the
functionality that one of these is for, you will need to
C<#include> it if the symbol on this list is C<#define>d. For
more detail, see the corresponding entry in F<config.h>.
__INCLUDE_LIST__
Example usage:
=over $standard_indent
#ifdef I_WCHAR
#include <wchar.h>
#endif
=back
EOT
},
$globals_scn => {},
$GV_scn => {},
$hook_scn => {},
$HV_scn => {},
$io_scn => {},
$io_formats_scn => {
header => <<~'EOT',
These are used for formatting the corresponding type For example,
instead of saying
Perl_newSVpvf(pTHX_ "Create an SV with a %d in it\n", iv);
use
Perl_newSVpvf(pTHX_ "Create an SV with a " IVdf " in it\n", iv);
This keeps you from having to know if, say an IV, needs to be
printed as C<%d>, C<%ld>, or something else.
EOT
},
$integer_scn => {},
$lexer_scn => {},
$locale_scn => {},
$magic_scn => {},
$memory_scn => {},
$MRO_scn => {},
$multicall_scn => {},
$numeric_scn => {},
$optrees_scn => {},
$optree_construction_scn => {},
$optree_manipulation_scn => {},
$pack_scn => {},
$pad_scn => {},
$password_scn => {},
$paths_scn => {},
$prototypes_scn => {},
$regexp_scn => {},
$reports_scn => {
header => <<~"EOT",
These are used in the simple report generation feature of Perl.
See L<perlform>.
EOT
},
$signals_scn => {},
$site_scn => {
header => <<~'EOT',
These variables give details as to where various libraries,
installation destinations, I<etc.>, go, as well as what various
installation options were selected
EOT
},
$sockets_scn => {},
$stack_scn => {},
$string_scn => {
header => <<~EOT,
See also C<L</$unicode_scn>>.
EOT
},
$SV_flags_scn => {},
$SV_scn => {},
$tainting_scn => {},
$time_scn => {},
$typedefs_scn => {},
$unicode_scn => {
header => <<~EOT,
L<perlguts/Unicode Support> has an introduction to this API.
See also C<L</$classification_scn>>,
C<L</$casing_scn>>,
and C<L</$string_scn>>.
Various functions outside this section also work specially with
Unicode. Search for the string "utf8" in this document.
EOT
},
$utility_scn => {},
$versioning_scn => {},
$warning_scn => {},
$XS_scn => {},
);
# Somewhat loose match for an apidoc line so we can catch minor typos.
# Parentheses are used to capture portions so that below we verify
# that things are the actual correct syntax.
my $apidoc_re = qr/ ^ (\s*) # $1
(=?) # $2
(\s*) # $3
for (\s*) # $4
apidoc (_item)? # $5
(\s*) # $6
(.*?) # $7
\s* \n /x;
# Only certain flags, dealing with display, are acceptable for apidoc_item
my $display_flags = "fFnDopTx;";
sub check_api_doc_line ($$) {
my ($file, $in) = @_;
return unless $in =~ $apidoc_re;
my $is_item = defined $5;
my $is_in_proper_form = length $1 == 0
&& length $2 > 0
&& length $3 == 0
&& length $4 > 0
&& length $7 > 0
&& ( length $6 > 0
|| ($is_item && substr($7, 0, 1) eq '|'));
my $proto_in_file = $7;
my $proto = $proto_in_file;
$proto = "||$proto" if $proto !~ /\|/;
my ($flags, $ret_type, $name, @args) = split /\s*\|\s*/, $proto;
$name && $is_in_proper_form or die <<EOS;
Bad apidoc at $file line $.:
$in
Expected:
=for apidoc flags|returntype|name|arg|arg|...
=for apidoc flags|returntype|name
=for apidoc name
(or 'apidoc_item')
EOS
die "Only [$display_flags] allowed in apidoc_item:\n$in"
if $is_item && $flags =~ /[^$display_flags]/;
return ($name, $flags, $ret_type, $is_item, $proto_in_file, @args);
}
sub embed_override($) {
my ($element_name) = shift;
# If the entry is also in embed.fnc, it should be defined
# completely there, but not here
my $embed_docref = delete $funcflags{$element_name};
return unless $embed_docref and %$embed_docref;
my $flags = $embed_docref->{'flags'};
warn "embed.fnc entry '$element_name' missing 'd' flag"
unless $flags =~ /d/;
return ($flags, $embed_docref->{'ret_type'}, $embed_docref->{args}->@*);
}
# The section that is in effect at the beginning of the given file. If not
# listed here, an apidoc_section line must precede any apidoc lines.
# This allows the files listed here that generally are single-purpose, to not
# have to worry about the autodoc section
my %initial_file_section = (
'av.c' => $AV_scn,
'av.h' => $AV_scn,
'cv.h' => $CV_scn,
'deb.c' => $debugging_scn,
'dist/ExtUtils-ParseXS/lib/perlxs.pod' => $XS_scn,
'doio.c' => $io_scn,
'gv.c' => $GV_scn,
'gv.h' => $GV_scn,
'hv.h' => $HV_scn,
'locale.c' => $locale_scn,
'malloc.c' => $memory_scn,
'numeric.c' => $numeric_scn,
'opnames.h' => $optree_construction_scn,
'pad.h'=> $pad_scn,
'patchlevel.h' => $versioning_scn,
'perlio.h' => $io_scn,
'pod/perlapio.pod' => $io_scn,
'pod/perlcall.pod' => $callback_scn,
'pod/perlembed.pod' => $embedding_scn,
'pod/perlfilter.pod' => $filters_scn,
'pod/perliol.pod' => $io_scn,
'pod/perlmroapi.pod' => $MRO_scn,
'pod/perlreguts.pod' => $regexp_scn,
'pp_pack.c' => $pack_scn,
'pp_sort.c' => $SV_scn,
'regcomp.c' => $regexp_scn,
'regexp.h' => $regexp_scn,
'sv.h' => $SV_scn,
'sv.c' => $SV_scn,
'sv_inline.h' => $SV_scn,
'taint.c' => $tainting_scn,
'unicode_constants.h' => $unicode_scn,
'utf8.c' => $unicode_scn,
'utf8.h' => $unicode_scn,
'vutil.c' => $versioning_scn,
);
sub autodoc ($$) { # parse a file and extract documentation info
my($fh,$file) = @_;
my($in, $line_num, $header, $section);
$section = $initial_file_section{$file}
if defined $initial_file_section{$file};
my $file_is_C = $file =~ / \. [ch] $ /x;
# Count lines easier
my $get_next_line = sub { $line_num++; return <$fh> };
# Read the file
while ($in = $get_next_line->()) {
last unless defined $in;
next unless ( $in =~ / ^ =for [ ]+ apidoc /x
# =head1 lines only have effect in C files
|| ($file_is_C && $in =~ /^=head1/));
# Here, the line introduces a portion of the input that we care about.
# Either it is for an API element, or heading text which we expect
# will be used for elements later in the file
my ($text, $element_name, $flags, $ret_type, $is_item, $proto_in_file);
my (@args, @items);
# If the line starts a new section ...
if ($in=~ /^ = (?: for [ ]+ apidoc_section | head1 ) [ ]+ (.*) /x) {
$section = $1;
if ($section =~ / ^ \$ /x) {
$section .= '_scn' unless $section =~ / _scn $ /;
$section = eval "$section";
die "Unknown \$section variable '$section' in $file: $@" if $@;
}
die "Unknown section name '$section' in $file near line $.\n"
unless defined $valid_sections{$section};
}
elsif ($in=~ /^ =for [ ]+ apidoc \B /x) { # Otherwise better be a
# plain apidoc line
die "Unkown apidoc-type line '$in'" unless $in=~ /^=for apidoc_item/;
die "apidoc_item doesn't immediately follow an apidoc entry: '$in'";
}
else { # Plain apidoc
($element_name, $flags, $ret_type, $is_item, $proto_in_file, @args)
= check_api_doc_line($file, $in);
# Override this line with any info in embed.fnc
my ($embed_flags, $embed_ret_type, @embed_args)
= embed_override($element_name);
if ($embed_ret_type) {
warn "embed.fnc entry overrides redundant information in"
. " '$proto_in_file' in $file"
if $flags || $ret_type || @args;
$flags = $embed_flags;
$ret_type = $embed_ret_type;
@args = @embed_args;
}
elsif ($flags !~ /[my]/) { # Not in embed.fnc, is missing if not
# a macro or typedef
$missing{$element_name} = $file;
}
die "flag '$1' is not legal (for function $element_name (from $file))"
if $flags =~ / ( [^AabCDdEeFfGhiIMmNnTOoPpRrSsUuWXxy;#] ) /x;
die "'u' flag must also have 'm' or 'y' flags' for $element_name"
if $flags =~ /u/ && $flags !~ /[my]/;
warn ("'$element_name' not \\w+ in '$proto_in_file' in $file")
if $flags !~ /N/ && $element_name !~ / ^ [_[:alpha:]] \w* $ /x;
if ($flags =~ /#/) {
die "Return type must be empty for '$element_name'"
if $ret_type;
$ret_type = '#ifdef';
}
if (exists $seen{$element_name} && $flags !~ /h/) {
die ("'$element_name' in $file was already documented in $seen{$element_name}");
}
else {
$seen{$element_name} = $file;
}
}
# Here we have processed the initial line in the heading text or API
# element, and have saved the important information from it into the
# corresponding variables. Now accumulate the text that applies to it
# up to a terminating line, which is one of:
# 1) =cut
# 2) =head (in a C file only =head1)
# 3) an end comment line in a C file: m:^\s*\*/:
# 4) =for apidoc... (except apidoc_item lines)
$text = "";
my $head_ender_num = ($file_is_C) ? 1 : "";
while (defined($in = $get_next_line->())) {
last if $in =~ /^=cut/x;
last if $in =~ /^=head$head_ender_num/;
if ($file_is_C && $in =~ m: ^ \s* \* / $ :x) {
# End of comment line in C files is a fall-back terminator,
# but warn only if there actually is some accumulated text
warn "=cut missing? $file:$line_num:$in" if $text =~ /\S/;
last;
}
if ($in !~ / ^ =for [ ]+ apidoc /x) {
$text .= $in;
next;
}
# Here, the line is an apidoc line. All but apidoc_item terminate
# the text being accumulated.
last if $in =~ / ^ =for [ ]+ apidoc_section /x;
my ($item_name, $item_flags, $item_ret_type, $is_item,
$item_proto, @item_args) = check_api_doc_line($file, $in);
last unless $is_item;
# Here, is an apidoc_item_line; They can only come within apidoc
# paragraphs.
die "Unexpected api_doc_item line '$item_proto'"
unless $element_name;
# We accept blank lines between these, but nothing else;
die "apidoc_item lines must immediately follow apidoc lines for "
. " '$element_name' in $file"
if $text =~ /\S/;
# Override this line with any info in embed.fnc
my ($embed_flags, $embed_ret_type, @embed_args)
= embed_override($item_name);
if ($embed_ret_type) {
warn "embed.fnc entry overrides redundant information in"
. " '$item_proto' in $file"
if $item_flags || $item_ret_type || @item_args;
$item_flags = $embed_flags;
$item_ret_type = $embed_ret_type;
@item_args = @embed_args;
}
# Use the base entry flags if none for this item; otherwise add in
# any non-display base entry flags.
if ($item_flags) {
$item_flags .= $flags =~ s/[$display_flags]//rg;
}
else {
$item_flags = $flags;
}
$item_ret_type = $ret_type unless $item_ret_type;
@item_args = @args unless @item_args;
push @items, { name => $item_name,
ret_type => $item_ret_type,
flags => $item_flags,
args => [ @item_args ],
};
# This line shows that this element is documented.
delete $funcflags{$item_name};
}
# Here, are done accumulating the text for this item. Trim it
$text =~ s/ ^ \s* //x;
$text =~ s/ \s* $ //x;
$text .= "\n" if $text ne "";
# And treat all-spaces as nothing at all
undef $text unless $text =~ /\S/;
if ($element_name) {
# Here, we have accumulated into $text, the pod for $element_name
my $where = $flags =~ /A/ ? 'api' : 'intern';
die "No =for apidoc_section nor =head1 in $file for '$element_name'\n"
unless defined $section;
my $is_link_only = ($flags =~ /h/);
if (! $is_link_only && exists $docs{$where}{$section}{$element_name}) {
warn "$0: duplicate API entry for '$element_name' in"
. " $where/$section\n";
next;
}
# Override the text with just a link if the flags call for that
if ($is_link_only) {
if ($file_is_C) {
die "Can't currently handle link with items to it:\n$in"
if @items;
$docs{$where}{$section}{X_tags}{$element_name} = $file;
redo; # Don't put anything if C source
}
# Here, is an 'h' flag in pod. We add a reference to the pod (and
# nothing else) to perlapi/intern. (It would be better to add a
# reference to the correct =item,=header, but something that makes
# it harder is that it that might be a duplicate, like '=item *';
# so that is a future enhancement XXX. Another complication is
# there might be more than one deserving candidates.)
my $podname = $file =~ s!.*/!!r; # Rmv directory name(s)
$podname =~ s/\.pod//;
$text = "Described in L<$podname>.\n";
# Don't output a usage example for linked to documentation if
# it is trivial (has no arguments) and we aren't to add a
# semicolon
$flags .= 'U' if $flags =~ /n/ && $flags !~ /[U;]/;
# Keep track of all the pod files that we refer to.
push $described_elsewhere{$podname}->@*, $podname;
}
$docs{$where}{$section}{$element_name}{flags} = $flags;
$docs{$where}{$section}{$element_name}{pod} = $text;
$docs{$where}{$section}{$element_name}{file} = $file;
$docs{$where}{$section}{$element_name}{ret_type} = $ret_type;
push $docs{$where}{$section}{$element_name}{args}->@*, @args;
push $docs{$where}{$section}{$element_name}{items}->@*, @items;
}
elsif ($text) {
$valid_sections{$section}{header} = "" unless
defined $valid_sections{$section}{header};
$valid_sections{$section}{header} .= "\n$text";
}
# We already have the first line of what's to come in $in
redo;
} # End of loop through input
}
my %configs;
my @has_defs;
my @has_r_defs; # Reentrant symbols
my @include_defs;
sub parse_config_h {
use re '/aa'; # Everthing is ASCII in this file
# Process config.h
my $config_h = 'config.h';
$config_h = 'win32/config.h' unless -e $config_h;
die "Can't find $config_h" unless -e $config_h;
open my $fh, '<', $config_h or die "Can't open $config_h: $!";
while (<$fh>) {
# Look for lines like /* FOO_BAR:
# By convention all config.h descriptions begin like that
if (m[ ^ /\* [ ] ( [[:alpha:]] \w+ ) : \s* $ ]ax) {
my $name = $1;
# Here we are starting the description for $name in config.h. We
# accumulate the entire description for it into @description.
# Flowing text from one input line to another is appended into the
# same array element to make a single flowing line element, but
# verbatim lines are kept as separate elements in @description.
# This will facilitate later doing pattern matching without regard
# to line boundaries on non-verbatim text.
die "Multiple config.h entries for '$name'"
if defined $configs{$name}{description};
# Get first line of description
$_ = <$fh>;
# Each line in the description begins with blanks followed by '/*'
# and some spaces.
die "Unexpected config.h initial line for $name: '$_'"
unless s/ ^ ( \s* \* \s* ) //x;
my $initial_text = $1;
# Initialize the description with this first line (after having
# stripped the prefix text)
my @description = $_;
# The first line is used as a template for how much indentation
# each normal succeeding line has. Lines indented further
# will be considered as intended to be verbatim. But, empty lines
# likely won't have trailing blanks, so just strip the whole thing
# for them.
my $strip_initial_qr = qr! \s* \* \s* $
| \Q$initial_text\E
!x;
$configs{$name}{verbatim} = 0;
# Read in the remainder of the description
while (<$fh>) {
last if s| ^ \s* \* / ||x; # A '*/' ends it
die "Unexpected config.h description line for $name: '$_'"
unless s/$strip_initial_qr//;
# Fix up the few flawed lines in config.h wherein a new
# sentence begins with a tab (and maybe a space after that).
# Although none of them currently do, let it recognize
# something like
#
# "... text"). The next sentence ...
#
s/ ( \w "? \)? \. ) \t \s* ( [[:alpha:]] ) /$1 $2/xg;
# If this line has extra indentation or looks to have columns,
# it should be treated as verbatim. Columns are indicated by
# use of interior: tabs, 3 spaces in a row, or even 2 spaces
# not preceded by punctuation.
if ($_ !~ m/ ^ \s
| \S (?: \t
| \s{3}
| (*nlb:[[:punct:]]) \s{2}
)
/x)
{
# But here, is not a verbatim line. Add an empty line if
# this is the first non-verbatim after a run of verbatims
if ($description[-1] =~ /^\s/) {
push @description, "\n", $_;
}
else { # Otherwise, append this flowing line to the
# current flowing line
$description[-1] .= $_;
}
}
else {
$configs{$name}{verbatim} = 1;
# The first verbatim line in a run of them is separated by an
# empty line from the flowing lines above it
push @description, "\n" if $description[-1] =~ /^\S/;
$_ = Text::Tabs::expand($_);
# Only a single space so less likely to wrap
s/ ^ \s* / /x;
push @description, $_;
}
}
push $configs{$name}{description}->@*, @description
} # Not a description; see if it is a macro definition.
elsif (m! ^
(?: / \* )? # Optional commented-out
# indication
\# \s* define \s+ ( \w+ ) # $1 is the name
( \s* ) # $2 indicates if args or not
( .*? ) # $3 is any definition
(?: / \s* \* \* / )? # Optional trailing /**/ or / **/
$
!x)
{
my $name = $1;
# There can be multiple definitions for a name. We want to know
# if any of them has arguments, and if any has a body.
$configs{$name}{has_args} //= $2 eq "";
$configs{$name}{has_args} ||= $2 eq "";
$configs{$name}{has_defn} //= $3 ne "";
$configs{$name}{has_defn} ||= $3 ne "";
}
}
# We now have stored the description and information about every #define
# in the file. The description is in a form convenient to operate on to
# convert to pod. Do that now.
foreach my $name (keys %configs) {
next unless defined $configs{$name}{description};
# All adjacent non-verbatim lines of the description are appended
# together in a single element in the array. This allows the patterns
# to work across input line boundaries.
my $pod = "";
while (defined ($_ = shift $configs{$name}{description}->@*)) {
chomp;
if (/ ^ \S /x) { # Don't edit verbatim lines
# Enclose known file/path names not already so enclosed
# with <...>. (Some entries in config.h are already
# '<path/to/file>')
my $file_name_qr = qr! [ \w / ]+ \.
(?: c | h | xs | p [lm] | pmc | PL
| sh | SH | exe ) \b
!xx;
my $path_name_qr = qr! (?: / \w+ )+ !x;
for my $re ($file_name_qr, $path_name_qr) {
s! (*nlb:[ < \w / ]) ( $re ) !<$1>!gxx;
}
# Enclose <... file/path names with F<...> (but no double
# angle brackets)
for my $re ($file_name_qr, $path_name_qr) {
s! < ( $re ) > !F<$1>!gxx;
}
# Explain metaconfig units
s/ ( \w+ \. U \b ) /$1 (part of metaconfig)/gx;
# Convert "See foo" to "See C<L</foo>>" if foo is described in
# this file. Also create a link to the known file INSTALL.
# And, to be more general, handle "See also foo and bar", and
# "See also foo, bar, and baz"
while (m/ \b [Ss]ee \s+
(?: also \s+ )? ( \w+ )
(?: , \s+ ( \w+ ) )?
(?: ,? \s+ and \s+ ( \w+ ) )? /xg) {
my @links = $1;
push @links, $2 if defined $2;
push @links, $3 if defined $3;
foreach my $link (@links) {
if ($link eq 'INSTALL') {
s/ \b INSTALL \b /C<L<INSTALL>>/xg;
}
elsif (grep { $link =~ / \b $_ \b /x } keys %configs) {
s| \b $link \b |C<L</$link>>|xg;
$configs{$link}{linked} = 1;
$configs{$name}{linked} = 1;
}
}
}
# Enclose what we think are symbols with C<...>.
no warnings 'experimental::vlb';
s/ (*nlb:<)
(
# Any word followed immediately with parens or
# brackets
\b \w+ (?: \( [^)]* \) # parameter list
| \[ [^]]* \] # or array reference
)
| (*plb: ^ | \s ) -D \w+ # Also -Dsymbols.
| \b (?: struct | union ) \s \w+
# Words that contain underscores (which are
# definitely not text) or three uppercase letters in
# a row. Length two ones, like IV, aren't enclosed,
# because they often don't look as nice.
| \b \w* (?: _ | [[:upper:]]{3,} ) \w* \b
)
(*nla:>)
/C<$1>/xg;
# These include foo when the name is HAS_foo. This is a
# heuristic which works in most cases.
if ($name =~ / ^ HAS_ (.*) /x) {
my $symbol = lc $1;
# Don't include path components, nor things already in
# <>, or with trailing '(', '['
s! \b (*nlb:[/<]) $symbol (*nla:[[/>(]) \b !C<$symbol>!xg;
}
}
$pod .= "$_\n";
}
delete $configs{$name}{description};
$configs{$name}{pod} = $pod;
}
# Now have converted the description to pod. We also now have enough
# information that we can do cross checking to find definitions without
# corresponding pod, and see if they are mentioned in some description;
# otherwise they aren't documented.
NAME:
foreach my $name (keys %configs) {
# A definition without pod
if (! defined $configs{$name}{pod}) {
# Leading/trailing underscore means internal to config.h, e.g.,
# _GNU_SOURCE
next if $name =~ / ^ _ /x;
next if $name =~ / _ $ /x;
# MiXeD case names are internal to config.h; the first 4
# characters are sufficient to determine this
next if $name =~ / ^ [[:upper:]] [[:lower:]]
[[:upper:]] [[:lower:]]
/x;
# Here, not internal to config.h. Look to see if this symbol is
# mentioned in the pod of some other. If so, assume it is
# documented.
foreach my $check_name (keys %configs) {
my $this_element = $configs{$check_name};
my $this_pod = $this_element->{pod};
if (defined $this_pod) {
next NAME if $this_pod =~ / \b $name \b /x;
}
}
warn "$name has no documentation\n";
$missing_macros{$name} = 'config.h';
next;
}
my $has_defn = $configs{$name}{has_defn};
my $has_args = $configs{$name}{has_args};
# Check if any section already has an entry for this element.
# If so, it better be a placeholder, in which case we replace it
# with this entry.
foreach my $section (keys $docs{'api'}->%*) {
if (exists $docs{'api'}{$section}{$name}) {
my $was = $docs{'api'}{$section}{$name}->{pod};
$was = "" unless $was;
chomp $was;
if ($was ne "" && $was !~ m/$link_text/) {
die "Multiple descriptions for $name\n"
. "$section contained '$was'";
}
$docs{'api'}{$section}{$name}->{pod} = $configs{$name}{pod};
$configs{$name}{section} = $section;
last;
}
}
my $handled = 0; # Haven't handled this yet
if (defined $configs{$name}{'section'}) {
# This has been taken care of elsewhere.
$handled = 1;
}
else {
my $flags = "";
if ($has_defn && ! $has_args) {
$configs{$name}{args} = 1;
}