3 # mksigs.pl - extract signatures from C headers
5 # Copyright (C) Michael Adam 2009
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 3 of the License, or (at your option)
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 # You should have received a copy of the GNU General Public License along with
18 # this program; if not, see <http://www.gnu.org/licenses/>.
20 # USAGE: cat $header_files | mksigs.pl > $signature_file
22 # The header files to parse are read from stdin.
23 # The output is in a form as produced by gcc with the -aux-info switch
24 # and printed to stdout.
31 my $extern_C_block = 0;
33 while (my $LINE = <>) {
34 # find end of started multi-line-comment
36 if ($LINE =~ /^.*?\*\/(.*)$/) {
40 # whole line within comment
45 # find end of DOXYGEN section
47 if ($LINE =~ /^#\s*else(?:\s+.*)?$/) {
53 # strip C++-style comments
54 $LINE =~ s/^(.*?)\/\/.*$/$1/;
56 # strip in-line-comments:
57 while ($LINE =~ /\/\
*.*?\
*\
//) {
58 $LINE =~ s/\/\*.*?\*\///;
61 # find starts of multi-line-comments
62 if ($LINE =~ /^(.*)\/\
*/) {
68 next if $LINE =~ /^\s*$/;
70 # remove leading spaces
71 $LINE =~ s/^\s*(.*)$/$1/;
73 # concatenate lines split with "\" (usually macro defines)
74 while ($LINE =~ /^(.*?)\s+\\$/) {
77 $LINE2 =~ s/^\s*(.*)$/$1/;
78 $LINE .= " " . $LINE2;
81 # remove DOXYGEN sections
82 if ($LINE =~ /^#\s*ifdef\s+DOXYGEN(?:\s+.*)?$/) {
88 # remove all preprocessor directives
89 next if ($LINE =~ /^#/);
91 if ($LINE =~ /^extern\s+"C"\s+\{/) {
96 if (($LINE =~ /^[^\{]*\}/) and $extern_C_block) {
101 $LINE =~ s/^extern\s//;
103 # concatenate braces stretched over multiple lines
104 # (from structs or enums)
107 while (($REST =~ /[\{\}]/) or ($braces)) {
108 while ($REST =~ /[\{\}]/) {
110 while ($REST =~ /^[^\{\}]*\{(.*)$/) {
116 while ($REST =~ /^[^\{\}]*\}(.*)$/) {
122 # concatenate if not balanced
124 if (my $LINE2 = <>) {
125 $LINE2 =~ s/^\s*(.*)$/$1/;
127 $LINE .= " " . $LINE2;
129 $REST .= " " . $LINE2;
131 print "ERROR: unbalanced braces ($braces)\n";
137 # concetenate function prototypes that stretch over multiple lines
140 while (($REST =~ /[\(\)]/) or ($parenthesis)) {
141 while ($REST =~ /[\(\)]/) {
143 while ($REST =~ /^[^\(\)]*\((.*)$/) {
149 while ($REST =~ /^[^\(\)]*\)(.*)$/) {
155 # concatenate if not balanced
157 if (my $LINE2 = <>) {
158 $LINE2 =~ s/^\s*(.*)$/$1/;
160 $LINE .= " " . $LINE2;
162 $REST .= " " . $LINE2;
164 print "ERROR: unbalanced parantheses ($parenthesis)\n";
170 next if ($LINE =~ /^typedef\s/);
171 next if ($LINE =~ /^enum\s+[^\{\(]+\s+\{/);
172 next if ($LINE =~ /^struct\s+[^\{\(]+\s+\{.*\}\s*;/);
173 next if ($LINE =~ /^struct\s+[a-zA-Z0-9_]+\s*;/);
175 # remove trailing spaces
176 $LINE =~ s/(.*?)\s*$/$1/;
178 $LINE =~ s/^(.*\))\s+PRINTF_ATTRIBUTE\([^\)]*\)(\s*[;,])/$1$2/;
179 $LINE =~ s/^(.*\))\s*[a-zA-Z0-9_]+\s*;$/$1;/;
181 # remove parameter names - slightly too coarse probably
182 $LINE =~ s/([\s\(]\*?)[_0-9a-zA-Z]+\s*([,\)])/$1$2/g;
184 # remedy (void) from last line
185 $LINE =~ s/\(\)/(void)/g;
188 $LINE =~ s/\s*\)\s*/)/g;
189 $LINE =~ s/\s*\(\s*/ (/g;
190 $LINE =~ s/\s*,\s*/, /g;
193 $LINE =~ s/([\s,\(])unsigned([,\)])/$1unsigned int$2/g;
196 $LINE =~ s/(\b)bool(\b)/_Bool/g;