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
|
#!/usr/bin/perl
# mksigs.pl - extract signatures from C headers
#
# Copyright (C) Michael Adam 2009
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>.
# USAGE: cat $header_files | mksigs.pl > $signature_file
#
# The header files to parse are read from stdin.
# The output is in a form as produced by gcc with the -aux-info switch
# and printed to stdout.
use strict;
use warnings;
my $in_comment = 0;
my $in_doxygen = 0;
my $extern_C_block = 0;
while (my $LINE = <>) {
# find end of started multi-line-comment
if ($in_comment) {
if ($LINE =~ /^.*?\*\/(.*)$/) {
$LINE = $1;
$in_comment = 0;
} else {
# whole line within comment
next;
}
}
# find end of DOXYGEN section
if ($in_doxygen) {
if ($LINE =~ /^#\s*else(?:\s+.*)?$/) {
$in_doxygen = 0;
}
next;
}
# strip C++-style comments
$LINE =~ s/^(.*?)\/\/.*$/$1/;
# strip in-line-comments:
while ($LINE =~ /\/\*.*?\*\//) {
$LINE =~ s/\/\*.*?\*\///;
}
# find starts of multi-line-comments
if ($LINE =~ /^(.*)\/\*/) {
$in_comment = 1;
$LINE = $1;
}
# skip empty lines
next if $LINE =~ /^\s*$/;
# remove leading spaces
$LINE =~ s/^\s*(.*)$/$1/;
# concatenate lines split with "\" (usually macro defines)
while ($LINE =~ /^(.*?)\s+\\$/) {
my $LINE2 = <>;
$LINE = $1;
$LINE2 =~ s/^\s*(.*)$/$1/;
$LINE .= " " . $LINE2;
}
# remove DOXYGEN sections
if ($LINE =~ /^#\s*ifdef\s+DOXYGEN(?:\s+.*)?$/) {
$in_doxygen = 1;
next;
}
# remove all preprocessor directives
next if ($LINE =~ /^#/);
if ($LINE =~ /^extern\s+"C"\s+\{/) {
$extern_C_block = 1;
next;
}
if (($LINE =~ /^[^\{]*\}/) and $extern_C_block) {
$extern_C_block = 0;
next;
}
$LINE =~ s/^extern\s//;
# concatenate braces stretched over multiple lines
# (from structs or enums)
my $REST = $LINE;
my $braces = 0;
while (($REST =~ /[\{\}]/) or ($braces)) {
while ($REST =~ /[\{\}]/) {
# collect opening
while ($REST =~ /^[^\{\}]*\{(.*)$/) {
$braces++;
$REST = $1;
}
# collect closing
while ($REST =~ /^[^\{\}]*\}(.*)$/) {
$braces--;
$REST = $1;
}
}
# concatenate if not balanced
if ($braces) {
if (my $LINE2 = <>) {
$LINE2 =~ s/^\s*(.*)$/$1/;
chomp($LINE);
$LINE .= " " . $LINE2;
chomp $REST;
$REST .= " " . $LINE2;
} else {
print "ERROR: unbalanced braces ($braces)\n";
last;
}
}
}
# concetenate function prototypes that stretch over multiple lines
$REST = $LINE;
my $parenthesis = 0;
while (($REST =~ /[\(\)]/) or ($parenthesis)) {
while ($REST =~ /[\(\)]/) {
# collect opening
while ($REST =~ /^[^\(\)]*\((.*)$/) {
$parenthesis++;
$REST = $1;
}
# collect closing
while ($REST =~ /^[^\(\)]*\)(.*)$/) {
$parenthesis--;
$REST = $1;
}
}
# concatenate if not balanced
if ($parenthesis) {
if (my $LINE2 = <>) {
$LINE2 =~ s/^\s*(.*)$/$1/;
chomp($LINE);
$LINE .= " " . $LINE2;
chomp($REST);
$REST .= " " . $LINE2;
} else {
print "ERROR: unbalanced parantheses ($parenthesis)\n";
last;
}
}
}
next if ($LINE =~ /^typedef\s/);
next if ($LINE =~ /^enum\s+[^\{\(]+\s+\{/);
next if ($LINE =~ /^struct\s+[^\{\(]+\s+\{.*\}\s*;/);
next if ($LINE =~ /^struct\s+[a-zA-Z0-9_]+\s*;/);
# remove trailing spaces
$LINE =~ s/(.*?)\s*$/$1/;
$LINE =~ s/^(.*\))\s+PRINTF_ATTRIBUTE\([^\)]*\)(\s*[;,])/$1$2/;
$LINE =~ s/^(.*\))\s*[a-zA-Z0-9_]+\s*;$/$1;/;
# remove parameter names - slightly too coarse probably
$LINE =~ s/([\s\(]\*?)[_0-9a-zA-Z]+\s*([,\)])/$1$2/g;
# remedy (void) from last line
$LINE =~ s/\(\)/(void)/g;
# normalize spaces
$LINE =~ s/\s*\)\s*/)/g;
$LINE =~ s/\s*\(\s*/ (/g;
$LINE =~ s/\s*,\s*/, /g;
# normalize unsigned
$LINE =~ s/([\s,\(])unsigned([,\)])/$1unsigned int$2/g;
# normalize bool
$LINE =~ s/(\b)bool(\b)/_Bool/g;
print $LINE . "\n";
}
|