-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen-declspecs.pl
executable file
·194 lines (177 loc) · 3.18 KB
/
gen-declspecs.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
#!/usr/bin/env perl
#
# Public domain.
#
# Look for __{BEGIN,END}_DECLS blocks in C header files and insert the
# "DECLSPEC" keyword required on some platforms, where appropriate.
#
# Process and output declaration block.
sub OutputDecls
{
my $inline = 0;
my $comment = 0;
foreach my $line (@_) {
# Begin/resume inline block
if ($inline) {
print $line."\n";
if ($line =~ /^}\s*/) {
$inline = 0;
}
next;
} elsif ($line =~ /^static\s+__inline__/) {
$inline = 1;
print $line."\n";
next;
}
# Begin/resume long comment
if ($comment) {
print $line."\n";
if ($line =~ /\*\/\s*$/) {
$comment = 0;
}
next;
} elsif ($line =~ /^\s*\/\*/) {
print $line."\n";
$comment = 1;
next;
}
if ($line =~ /^\s*extern DECLSPEC/) { # Already processed
print $line."\n";
next;
}
$line =~ s/\s+/ /g; # Fix whitespace
if ($line =~ /^\s*struct\s*\w+\s*;\s*$/) { # Forward decl
print $line."\n";
next;
} elsif ($line =~ /^\s*extern (.+)$/) { # Extern var
print 'extern DECLSPEC '."$1\n";
next;
} elsif ($line =~ # Function decl
/^\s*([\w\*]+)\s+
([\w\*\[\]]+)
([\w\*\s,\.\[\]\(\)]*)\s*;\s*$/x) {
print 'extern DECLSPEC '."$1 $2$3;\n";
next;
}
$line =~ s/\s+/ /g;
print $line."\n";
}
}
my @input = ();
my $decls = 0;
my @blk = ();
if (@ARGV == 0) {
print STDERR "Usage: gen-declspecs.pl [file]\n";
exit(1);
}
open(F, $ARGV[0]) || die "$ARGV[0]: $!";
#
# First pass: Process cpp long lines.
#
my $trunc = 0;
my $truncLine = '';
while (<F>) {
chop;
if ($trunc) {
$truncLine .= ' '.$_;
if (!/\\$/) {
$truncLine =~ s/\s+/ /g;
push @input, $truncLine;
$truncLine = '';
$trunc = 0;
} else {
chop($truncLine);
}
} else {
if (/\\$/) {
chop;
$truncLine = $_;
$truncLine =~ s/\s+/ /g;
$trunc = 1;
} else {
push @input, $_;
}
}
}
#
# Second pass: Process function/variable declarations.
#
my $inline = 0;
my $comment = 0;
my $stmt = 0;
my $stmtLine = '';
foreach $_ (@input) {
if (/^\s*__BEGIN_DECLS\s*$/) {
# Begin declarations block
if ($decls) {
print STDERR "Nested __BEGIN_DECLS!\n";
}
@blk = ($_);
$decls = 1;
next;
}
unless ($decls) {
# Not in declarations block
print $_."\n";
next;
}
if (/^\s*__END_DECLS\s*$/) {
# End declarations block
push @blk, $_;
print "/* Begin generated block */\n";
OutputDecls(@blk);
print "/* Close generated block */\n";
$decls = 0;
next;
}
# Strip comments in block
s/(\/\*.*\*\/)//g;
s/(\/\/.*)//g;
# Begin/resume inline block
if ($inline) {
push @blk, $_;
if (/^}\s*/) {
$inline = 0;
}
next;
} elsif (/^static __inline__ /) {
$inline = 1;
push @blk, '', $_;
next;
}
# Begin/resume comment block
if ($comment) {
if (/\s*\*\/\s*$/) {
$comment = 0;
}
push @blk, $_;
next;
} elsif (/^\s*\/\*/) {
$comment = 1;
push @blk, $_;
next;
}
# Ignore cpp directives.
if (/^\s*#/) {
push @blk, $_;
next;
}
# Begin/resume C statement
if ($stmt) {
$stmtLine .= ' '.$_;
if (/;\s*$/) {
$stmt = 0;
push @blk, $stmtLine;
$stmtLine = '';
}
next;
} else {
unless (/;\s*$/) {
$stmt = 1;
$stmtLine = $_;
next;
}
}
push @blk, $_;
}
close(F);