ALSA: control: Hardening for potential Spectre v1
[linux/fpc-iii.git] / Documentation / sphinx / parse-headers.pl
blobdb0186a7618f2a9ca9b712db1c9a40d4bb6e3d7c
1 #!/usr/bin/perl
2 use strict;
3 use Text::Tabs;
5 my $debug = 0;
7 while ($ARGV[0] =~ m/^-(.*)/) {
8 my $cmd = shift @ARGV;
9 if ($cmd eq "--debug") {
10 require Data::Dumper;
11 $debug = 1;
12 next;
14 die "argument $cmd unknown";
17 if (scalar @ARGV < 2 || scalar @ARGV > 3) {
18 die "Usage:\n\t$0 <file in> <file out> [<exceptions file>]\n";
21 my ($file_in, $file_out, $file_exceptions) = @ARGV;
23 my $data;
24 my %ioctls;
25 my %defines;
26 my %typedefs;
27 my %enums;
28 my %enum_symbols;
29 my %structs;
32 # read the file and get identifiers
35 my $is_enum = 0;
36 my $is_comment = 0;
37 open IN, $file_in or die "Can't open $file_in";
38 while (<IN>) {
39 $data .= $_;
41 my $ln = $_;
42 if (!$is_comment) {
43 $ln =~ s,/\*.*(\*/),,g;
45 $is_comment = 1 if ($ln =~ s,/\*.*,,);
46 } else {
47 if ($ln =~ s,^(.*\*/),,) {
48 $is_comment = 0;
49 } else {
50 next;
54 if ($is_enum && $ln =~ m/^\s*([_\w][\w\d_]+)\s*[\,=]?/) {
55 my $s = $1;
56 my $n = $1;
57 $n =~ tr/A-Z/a-z/;
58 $n =~ tr/_/-/;
60 $enum_symbols{$s} = "\\ :ref:`$s <$n>`\\ ";
62 $is_enum = 0 if ($is_enum && m/\}/);
63 next;
65 $is_enum = 0 if ($is_enum && m/\}/);
67 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+_IO/) {
68 my $s = $1;
69 my $n = $1;
70 $n =~ tr/A-Z/a-z/;
72 $ioctls{$s} = "\\ :ref:`$s <$n>`\\ ";
73 next;
76 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+/) {
77 my $s = $1;
78 my $n = $1;
79 $n =~ tr/A-Z/a-z/;
80 $n =~ tr/_/-/;
82 $defines{$s} = "\\ :ref:`$s <$n>`\\ ";
83 next;
86 if ($ln =~ m/^\s*typedef\s+([_\w][\w\d_]+)\s+(.*)\s+([_\w][\w\d_]+);/) {
87 my $s = $2;
88 my $n = $3;
90 $typedefs{$n} = "\\ :c:type:`$n <$s>`\\ ";
91 next;
93 if ($ln =~ m/^\s*enum\s+([_\w][\w\d_]+)\s+\{/
94 || $ln =~ m/^\s*enum\s+([_\w][\w\d_]+)$/
95 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)\s+\{/
96 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)$/) {
97 my $s = $1;
99 $enums{$s} = "enum :c:type:`$s`\\ ";
101 $is_enum = $1;
102 next;
104 if ($ln =~ m/^\s*struct\s+([_\w][\w\d_]+)\s+\{/
105 || $ln =~ m/^\s*struct\s+([[_\w][\w\d_]+)$/
106 || $ln =~ m/^\s*typedef\s*struct\s+([_\w][\w\d_]+)\s+\{/
107 || $ln =~ m/^\s*typedef\s*struct\s+([[_\w][\w\d_]+)$/
109 my $s = $1;
111 $structs{$s} = "struct :c:type:`$s`\\ ";
112 next;
115 close IN;
118 # Handle multi-line typedefs
121 my @matches = ($data =~ m/typedef\s+struct\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,
122 $data =~ m/typedef\s+enum\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,);
123 foreach my $m (@matches) {
124 my $s = $m;
126 $typedefs{$s} = "\\ :c:type:`$s`\\ ";
127 next;
131 # Handle exceptions, if any
134 my %def_reftype = (
135 "ioctl" => ":ref",
136 "define" => ":ref",
137 "symbol" => ":ref",
138 "typedef" => ":c:type",
139 "enum" => ":c:type",
140 "struct" => ":c:type",
143 if ($file_exceptions) {
144 open IN, $file_exceptions or die "Can't read $file_exceptions";
145 while (<IN>) {
146 next if (m/^\s*$/ || m/^\s*#/);
148 # Parsers to ignore a symbol
150 if (m/^ignore\s+ioctl\s+(\S+)/) {
151 delete $ioctls{$1} if (exists($ioctls{$1}));
152 next;
154 if (m/^ignore\s+define\s+(\S+)/) {
155 delete $defines{$1} if (exists($defines{$1}));
156 next;
158 if (m/^ignore\s+typedef\s+(\S+)/) {
159 delete $typedefs{$1} if (exists($typedefs{$1}));
160 next;
162 if (m/^ignore\s+enum\s+(\S+)/) {
163 delete $enums{$1} if (exists($enums{$1}));
164 next;
166 if (m/^ignore\s+struct\s+(\S+)/) {
167 delete $structs{$1} if (exists($structs{$1}));
168 next;
170 if (m/^ignore\s+symbol\s+(\S+)/) {
171 delete $enum_symbols{$1} if (exists($enum_symbols{$1}));
172 next;
175 # Parsers to replace a symbol
176 my ($type, $old, $new, $reftype);
178 if (m/^replace\s+(\S+)\s+(\S+)\s+(\S+)/) {
179 $type = $1;
180 $old = $2;
181 $new = $3;
182 } else {
183 die "Can't parse $file_exceptions: $_";
186 if ($new =~ m/^\:c\:(data|func|macro|type)\:\`(.+)\`/) {
187 $reftype = ":c:$1";
188 $new = $2;
189 } elsif ($new =~ m/\:ref\:\`(.+)\`/) {
190 $reftype = ":ref";
191 $new = $1;
192 } else {
193 $reftype = $def_reftype{$type};
195 $new = "$reftype:`$old <$new>`";
197 if ($type eq "ioctl") {
198 $ioctls{$old} = $new if (exists($ioctls{$old}));
199 next;
201 if ($type eq "define") {
202 $defines{$old} = $new if (exists($defines{$old}));
203 next;
205 if ($type eq "symbol") {
206 $enum_symbols{$old} = $new if (exists($enum_symbols{$old}));
207 next;
209 if ($type eq "typedef") {
210 $typedefs{$old} = $new if (exists($typedefs{$old}));
211 next;
213 if ($type eq "enum") {
214 $enums{$old} = $new if (exists($enums{$old}));
215 next;
217 if ($type eq "struct") {
218 $structs{$old} = $new if (exists($structs{$old}));
219 next;
222 die "Can't parse $file_exceptions: $_";
226 if ($debug) {
227 print Data::Dumper->Dump([\%ioctls], [qw(*ioctls)]) if (%ioctls);
228 print Data::Dumper->Dump([\%typedefs], [qw(*typedefs)]) if (%typedefs);
229 print Data::Dumper->Dump([\%enums], [qw(*enums)]) if (%enums);
230 print Data::Dumper->Dump([\%structs], [qw(*structs)]) if (%structs);
231 print Data::Dumper->Dump([\%defines], [qw(*defines)]) if (%defines);
232 print Data::Dumper->Dump([\%enum_symbols], [qw(*enum_symbols)]) if (%enum_symbols);
236 # Align block
238 $data = expand($data);
239 $data = " " . $data;
240 $data =~ s/\n/\n /g;
241 $data =~ s/\n\s+$/\n/g;
242 $data =~ s/\n\s+\n/\n\n/g;
245 # Add escape codes for special characters
247 $data =~ s,([\_\`\*\<\>\&\\\\:\/\|\%\$\#\{\}\~\^]),\\$1,g;
249 $data =~ s,DEPRECATED,**DEPRECATED**,g;
252 # Add references
255 my $start_delim = "[ \n\t\(\=\*\@]";
256 my $end_delim = "(\\s|,|\\\\=|\\\\:|\\;|\\\)|\\}|\\{)";
258 foreach my $r (keys %ioctls) {
259 my $s = $ioctls{$r};
261 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
263 print "$r -> $s\n" if ($debug);
265 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
268 foreach my $r (keys %defines) {
269 my $s = $defines{$r};
271 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
273 print "$r -> $s\n" if ($debug);
275 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
278 foreach my $r (keys %enum_symbols) {
279 my $s = $enum_symbols{$r};
281 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
283 print "$r -> $s\n" if ($debug);
285 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
288 foreach my $r (keys %enums) {
289 my $s = $enums{$r};
291 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
293 print "$r -> $s\n" if ($debug);
295 $data =~ s/enum\s+($r)$end_delim/$s$2/g;
298 foreach my $r (keys %structs) {
299 my $s = $structs{$r};
301 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
303 print "$r -> $s\n" if ($debug);
305 $data =~ s/struct\s+($r)$end_delim/$s$2/g;
308 foreach my $r (keys %typedefs) {
309 my $s = $typedefs{$r};
311 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
313 print "$r -> $s\n" if ($debug);
314 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
317 $data =~ s/\\ ([\n\s])/\1/g;
320 # Generate output file
323 my $title = $file_in;
324 $title =~ s,.*/,,;
326 open OUT, "> $file_out" or die "Can't open $file_out";
327 print OUT ".. -*- coding: utf-8; mode: rst -*-\n\n";
328 print OUT "$title\n";
329 print OUT "=" x length($title);
330 print OUT "\n\n.. parsed-literal::\n\n";
331 print OUT $data;
332 close OUT;