]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - Documentation/sphinx/parse-headers.pl
docs-rst: fix media cleandocs target
[mirror_ubuntu-bionic-kernel.git] / Documentation / sphinx / parse-headers.pl
CommitLineData
dabf8be3
MCC
1#!/usr/bin/perl
2use strict;
3use Text::Tabs;
327f5a75
MCC
4use Getopt::Long;
5use Pod::Usage;
dabf8be3 6
327f5a75
MCC
7my $debug;
8my $help;
9my $man;
dabf8be3 10
327f5a75
MCC
11GetOptions(
12 "debug" => \$debug,
13 'help|?' => \$help,
14 man => \$man
15) or pod2usage(2);
bcec7c21 16
327f5a75
MCC
17pod2usage(1) if $help;
18pod2usage(-exitstatus => 0, -verbose => 2) if $man;
19pod2usage(2) if (scalar @ARGV < 2 || scalar @ARGV > 3);
dabf8be3
MCC
20
21my ($file_in, $file_out, $file_exceptions) = @ARGV;
22
23my $data;
24my %ioctls;
25my %defines;
26my %typedefs;
27my %enums;
28my %enum_symbols;
29my %structs;
30
327f5a75
MCC
31require Data::Dumper if ($debug);
32
dabf8be3
MCC
33#
34# read the file and get identifiers
35#
36
37my $is_enum = 0;
034e6c8e 38my $is_comment = 0;
dabf8be3
MCC
39open IN, $file_in or die "Can't open $file_in";
40while (<IN>) {
034e6c8e
MCC
41 $data .= $_;
42
9afe5112 43 my $ln = $_;
034e6c8e
MCC
44 if (!$is_comment) {
45 $ln =~ s,/\*.*(\*/),,g;
9afe5112 46
034e6c8e
MCC
47 $is_comment = 1 if ($ln =~ s,/\*.*,,);
48 } else {
49 if ($ln =~ s,^(.*\*/),,) {
50 $is_comment = 0;
51 } else {
52 next;
53 }
54 }
dabf8be3 55
9c80c745 56 if ($is_enum && $ln =~ m/^\s*([_\w][\w\d_]+)\s*[\,=]?/) {
dabf8be3
MCC
57 my $s = $1;
58 my $n = $1;
59 $n =~ tr/A-Z/a-z/;
60 $n =~ tr/_/-/;
61
22c40033 62 $enum_symbols{$s} = "\\ :ref:`$s <$n>`\\ ";
dabf8be3
MCC
63
64 $is_enum = 0 if ($is_enum && m/\}/);
65 next;
66 }
67 $is_enum = 0 if ($is_enum && m/\}/);
68
9c80c745 69 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+_IO/) {
dabf8be3
MCC
70 my $s = $1;
71 my $n = $1;
72 $n =~ tr/A-Z/a-z/;
73
22c40033 74 $ioctls{$s} = "\\ :ref:`$s <$n>`\\ ";
dabf8be3
MCC
75 next;
76 }
77
9c80c745 78 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+/) {
dabf8be3
MCC
79 my $s = $1;
80 my $n = $1;
81 $n =~ tr/A-Z/a-z/;
82 $n =~ tr/_/-/;
83
22c40033 84 $defines{$s} = "\\ :ref:`$s <$n>`\\ ";
dabf8be3
MCC
85 next;
86 }
87
22c40033
MCC
88 if ($ln =~ m/^\s*typedef\s+([_\w][\w\d_]+)\s+(.*)\s+([_\w][\w\d_]+);/) {
89 my $s = $2;
90 my $n = $3;
dabf8be3 91
22c40033 92 $typedefs{$n} = "\\ :c:type:`$n <$s>`\\ ";
dabf8be3
MCC
93 next;
94 }
9c80c745 95 if ($ln =~ m/^\s*enum\s+([_\w][\w\d_]+)\s+\{/
6c4c7dad
MCC
96 || $ln =~ m/^\s*enum\s+([_\w][\w\d_]+)$/
97 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)\s+\{/
98 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)$/) {
dabf8be3 99 my $s = $1;
dabf8be3 100
22c40033 101 $enums{$s} = "enum :c:type:`$s`\\ ";
dabf8be3
MCC
102
103 $is_enum = $1;
104 next;
105 }
9c80c745 106 if ($ln =~ m/^\s*struct\s+([_\w][\w\d_]+)\s+\{/
6c4c7dad
MCC
107 || $ln =~ m/^\s*struct\s+([[_\w][\w\d_]+)$/
108 || $ln =~ m/^\s*typedef\s*struct\s+([_\w][\w\d_]+)\s+\{/
109 || $ln =~ m/^\s*typedef\s*struct\s+([[_\w][\w\d_]+)$/
110 ) {
dabf8be3 111 my $s = $1;
dabf8be3 112
22c40033 113 $structs{$s} = "struct :c:type:`$s`\\ ";
dabf8be3
MCC
114 next;
115 }
116}
117close IN;
118
119#
120# Handle multi-line typedefs
121#
122
4ff916a0
MCC
123my @matches = ($data =~ m/typedef\s+struct\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,
124 $data =~ m/typedef\s+enum\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,);
dabf8be3 125foreach my $m (@matches) {
22c40033 126 my $s = $m;
dabf8be3 127
22c40033 128 $typedefs{$s} = "\\ :c:type:`$s`\\ ";
dabf8be3
MCC
129 next;
130}
131
132#
133# Handle exceptions, if any
134#
135
22c40033
MCC
136my %def_reftype = (
137 "ioctl" => ":ref",
138 "define" => ":ref",
139 "symbol" => ":ref",
140 "typedef" => ":c:type",
141 "enum" => ":c:type",
142 "struct" => ":c:type",
143);
144
dabf8be3
MCC
145if ($file_exceptions) {
146 open IN, $file_exceptions or die "Can't read $file_exceptions";
147 while (<IN>) {
148 next if (m/^\s*$/ || m/^\s*#/);
149
150 # Parsers to ignore a symbol
151
152 if (m/^ignore\s+ioctl\s+(\S+)/) {
153 delete $ioctls{$1} if (exists($ioctls{$1}));
154 next;
155 }
156 if (m/^ignore\s+define\s+(\S+)/) {
157 delete $defines{$1} if (exists($defines{$1}));
158 next;
159 }
160 if (m/^ignore\s+typedef\s+(\S+)/) {
161 delete $typedefs{$1} if (exists($typedefs{$1}));
162 next;
163 }
164 if (m/^ignore\s+enum\s+(\S+)/) {
165 delete $enums{$1} if (exists($enums{$1}));
166 next;
167 }
168 if (m/^ignore\s+struct\s+(\S+)/) {
169 delete $structs{$1} if (exists($structs{$1}));
170 next;
171 }
526b8848
MCC
172 if (m/^ignore\s+symbol\s+(\S+)/) {
173 delete $enum_symbols{$1} if (exists($enum_symbols{$1}));
174 next;
175 }
dabf8be3
MCC
176
177 # Parsers to replace a symbol
22c40033 178 my ($type, $old, $new, $reftype);
dabf8be3 179
22c40033
MCC
180 if (m/^replace\s+(\S+)\s+(\S+)\s+(\S+)/) {
181 $type = $1;
182 $old = $2;
183 $new = $3;
184 } else {
185 die "Can't parse $file_exceptions: $_";
186 }
187
188 if ($new =~ m/^\:c\:(data|func|macro|type)\:\`(.+)\`/) {
189 $reftype = ":c:$1";
190 $new = $2;
191 } elsif ($new =~ m/\:ref\:\`(.+)\`/) {
192 $reftype = ":ref";
193 $new = $1;
194 } else {
195 $reftype = $def_reftype{$type};
196 }
197 $new = "$reftype:`$old <$new>`";
198
199 if ($type eq "ioctl") {
200 $ioctls{$old} = $new if (exists($ioctls{$old}));
dabf8be3
MCC
201 next;
202 }
22c40033
MCC
203 if ($type eq "define") {
204 $defines{$old} = $new if (exists($defines{$old}));
dabf8be3
MCC
205 next;
206 }
22c40033
MCC
207 if ($type eq "symbol") {
208 $enum_symbols{$old} = $new if (exists($enum_symbols{$old}));
dabf8be3
MCC
209 next;
210 }
22c40033
MCC
211 if ($type eq "typedef") {
212 $typedefs{$old} = $new if (exists($typedefs{$old}));
dabf8be3
MCC
213 next;
214 }
22c40033
MCC
215 if ($type eq "enum") {
216 $enums{$old} = $new if (exists($enums{$old}));
dabf8be3
MCC
217 next;
218 }
22c40033
MCC
219 if ($type eq "struct") {
220 $structs{$old} = $new if (exists($structs{$old}));
dabf8be3
MCC
221 next;
222 }
223
224 die "Can't parse $file_exceptions: $_";
225 }
226}
227
228if ($debug) {
229 print Data::Dumper->Dump([\%ioctls], [qw(*ioctls)]) if (%ioctls);
230 print Data::Dumper->Dump([\%typedefs], [qw(*typedefs)]) if (%typedefs);
231 print Data::Dumper->Dump([\%enums], [qw(*enums)]) if (%enums);
232 print Data::Dumper->Dump([\%structs], [qw(*structs)]) if (%structs);
233 print Data::Dumper->Dump([\%defines], [qw(*defines)]) if (%defines);
234 print Data::Dumper->Dump([\%enum_symbols], [qw(*enum_symbols)]) if (%enum_symbols);
235}
236
237#
238# Align block
239#
240$data = expand($data);
241$data = " " . $data;
242$data =~ s/\n/\n /g;
243$data =~ s/\n\s+$/\n/g;
244$data =~ s/\n\s+\n/\n\n/g;
245
246#
247# Add escape codes for special characters
248#
999d998e 249$data =~ s,([\_\`\*\<\>\&\\\\:\/\|\%\$\#\{\}\~\^]),\\$1,g;
dabf8be3 250
7d95fa8d
MCC
251$data =~ s,DEPRECATED,**DEPRECATED**,g;
252
dabf8be3
MCC
253#
254# Add references
255#
256
6fe79d1e
MCC
257my $start_delim = "[ \n\t\(\=\*\@]";
258my $end_delim = "(\\s|,|\\\\=|\\\\:|\\;|\\\)|\\}|\\{)";
dabf8be3
MCC
259
260foreach my $r (keys %ioctls) {
22c40033 261 my $s = $ioctls{$r};
dabf8be3
MCC
262
263 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
264
265 print "$r -> $s\n" if ($debug);
266
6fe79d1e 267 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
dabf8be3
MCC
268}
269
270foreach my $r (keys %defines) {
22c40033 271 my $s = $defines{$r};
dabf8be3
MCC
272
273 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
274
275 print "$r -> $s\n" if ($debug);
276
6fe79d1e 277 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
dabf8be3
MCC
278}
279
280foreach my $r (keys %enum_symbols) {
22c40033 281 my $s = $enum_symbols{$r};
dabf8be3
MCC
282
283 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
284
285 print "$r -> $s\n" if ($debug);
286
6fe79d1e 287 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
dabf8be3
MCC
288}
289
290foreach my $r (keys %enums) {
22c40033 291 my $s = $enums{$r};
dabf8be3
MCC
292
293 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
294
295 print "$r -> $s\n" if ($debug);
296
6fe79d1e 297 $data =~ s/enum\s+($r)$end_delim/$s$2/g;
dabf8be3
MCC
298}
299
300foreach my $r (keys %structs) {
22c40033 301 my $s = $structs{$r};
dabf8be3
MCC
302
303 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
304
305 print "$r -> $s\n" if ($debug);
306
6fe79d1e 307 $data =~ s/struct\s+($r)$end_delim/$s$2/g;
dabf8be3
MCC
308}
309
310foreach my $r (keys %typedefs) {
22c40033 311 my $s = $typedefs{$r};
dabf8be3
MCC
312
313 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
314
315 print "$r -> $s\n" if ($debug);
6fe79d1e 316 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
dabf8be3
MCC
317}
318
22c40033 319$data =~ s/\\ ([\n\s])/\1/g;
fb6fc6c9 320
dabf8be3
MCC
321#
322# Generate output file
323#
324
325my $title = $file_in;
326$title =~ s,.*/,,;
327
328open OUT, "> $file_out" or die "Can't open $file_out";
329print OUT ".. -*- coding: utf-8; mode: rst -*-\n\n";
330print OUT "$title\n";
331print OUT "=" x length($title);
332print OUT "\n\n.. parsed-literal::\n\n";
333print OUT $data;
334close OUT;
327f5a75
MCC
335
336__END__
337
338=head1 NAME
339
340parse_headers.pl - parse a C file, in order to identify functions, structs,
341enums and defines and create cross-references to a Sphinx book.
342
343=head1 SYNOPSIS
344
345B<parse_headers.pl> [<options>] <C_FILE> <OUT_FILE> [<EXCEPTIONS_FILE>]
346
347Where <options> can be: --debug, --help or --man.
348
349=head1 OPTIONS
350
351=over 8
352
353=item B<--debug>
354
355Put the script in verbose mode, useful for debugging.
356
357=item B<--help>
358
359Prints a brief help message and exits.
360
361=item B<--man>
362
363Prints the manual page and exits.
364
365=back
366
367=head1 DESCRIPTION
368
369Convert a C header or source file (C_FILE), into a ReStructured Text
370included via ..parsed-literal block with cross-references for the
371documentation files that describe the API. It accepts an optional
372EXCEPTIONS_FILE with describes what elements will be either ignored or
373be pointed to a non-default reference.
374
375The output is written at the (OUT_FILE).
376
377It is capable of identifying defines, functions, structs, typedefs,
378enums and enum symbols and create cross-references for all of them.
379It is also capable of distinguish #define used for specifying a Linux
380ioctl.
381
382The EXCEPTIONS_FILE contain two types of statements: B<ignore> or B<replace>.
383
384The syntax for the ignore tag is:
385
386=over 8
387
388ignore B<type> B<name>
389
390=back
391
392The B<ignore> means that it won't generate cross references for a
393B<name> symbol of type B<type>.
394
395The syntax for the replace tag is:
396
397=over 8
398
399replace B<type> B<name> B<new_value>
400
401=back
402
403The B<replace> means that it will generate cross references for a
404B<name> symbol of type B<type>, but, instead of using the default
405replacement rule, it will use B<new_value>.
406
407For both statements, B<type> can be either one of the following:
408
409=over 8
410
411=item B<ioctl>
412
413The ignore or replace statement will apply to ioctl definitions like:
414
415#define VIDIOC_DBG_S_REGISTER _IOW('V', 79, struct v4l2_dbg_register)
416
417=item B<define>
418
419The ignore or replace statement will apply to any other #define found
420at C_FILE.
421
422=item B<typedef>
423
424The ignore or replace statement will apply to typedef statements at C_FILE.
425
426=item B<struct>
427
428The ignore or replace statement will apply to the name of struct statements
429at C_FILE.
430
431=item B<enum>
432
433The ignore or replace statement will apply to the name of enum statements
434at C_FILE.
435
436=item B<symbol>
437
438The ignore or replace statement will apply to the name of enum statements
439at C_FILE.
440
441
442For replace statements, B<new_value> will automatically use :c:type:
443references for B<typedef>, B<enum> and B<struct> types. It will use :ref:
444for B<ioctl>, B<define> and B<symbol> types. The type of reference can
445also be explicitly defined at the replace statement.
446
447=back
448
449=head1 EXAMPLES
450
451ignore define _VIDEODEV2_H
452
453=over 8
454
455
456Ignore a #define _VIDEODEV2_H at the C_FILE.
457
458=back
459
460ignore symbol PRIVATE
461
462=over 8
463
464On a struct like:
465
466enum foo { BAR1, BAR2, PRIVATE };
467
468It won't generate cross-references for B<PRIVATE>.
469
470=back
471
472replace symbol BAR1 :c:type:`foo`
473replace symbol BAR2 :c:type:`foo`
474
475=over 8
476
477On a struct like:
478
479enum foo { BAR1, BAR2, PRIVATE };
480
481It will make the BAR1 and BAR2 enum symbols to cross reference the foo
482symbol at the C domain.
483
484=back
485
486=head1 BUGS
487
488Report bugs to Mauro Carvalho Chehab <mchehab@s-opensource.com>
489
490=head1 COPYRIGHT
491
492Copyright (c) 2016 by Mauro Carvalho Chehab <mchehab@s-opensource.com>.
493
494License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
495
496This is free software: you are free to change and redistribute it.
497There is NO WARRANTY, to the extent permitted by law.
498
499=cut