-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
sipify.pl
executable file
·380 lines (343 loc) · 11.8 KB
/
sipify.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
# TODO add contexts for
# "multiline function signatures"
# docustrings for QgsFeature::QgsAttributes
sub processDoxygenLine
{
if ( $_[0] =~ m/[\\@](ingroup|class)/ ) {
return ""
}
if ( $_[0] =~ m/\\since .*?([\d\.]+)/i ) {
return ".. versionadded:: $1\n";
}
if ( $_[0] =~ m/[\\@]note (.*)/ ) {
return ".. note::\n\n $1\n";
}
if ( $_[0] =~ m/[\\@]brief (.*)/ ) {
return " $1\n";
}
return $_[0];
}
my $headerfile = $ARGV[0];
open(my $header, "<", $headerfile) || die "Couldn't open '".$headerfile."' for reading because: ".$!;
# contexts
my $SIP_RUN = 0;
my $HEADER_CODE = 0;
my $PRIVATE_SECTION = 0;
my $MULTILINE_DEFINITION = 0;
my $comment = '';
my $nesting_index = 0;
my $private_section_line = '';
my $line;
print "/************************************************************************\n";
print " * This file has been generated automatically from *\n";
print " * *\n";
printf " * %-*s *\n", 68, $headerfile;
print " * *\n";
print " * Do not edit manually ! Edit header and run scripts/sipify.pl again *\n";
print " ************************************************************************/\n";
while(!eof $header){
$line = readline $header;
#print $line;
if ($line =~ m/^\s*SIP_FEATURE\( (\w+) \)(.*)$/){
print "%Feature $1$2";
next;
}
if ($line =~ m/^\s*SIP_IF_FEATURE\( (\!?\w+) \)(.*)$/){
print "%If ($1)$2";
next;
}
if ($line =~ m/^\s*SIP_CONVERT_TO_SUBCLASS_CODE(.*)$/){
print "%ConvertToSubClassCode$1\n";
next;
}
if ($line =~ m/^\s*SIP_END(.*)$/){
print "%End$1";
next;
}
# Skip preprocessor stuff
if ($line =~ m/^\s*#/){
if ( $line =~ m/^\s*#ifdef SIP_RUN/){
$SIP_RUN = 1;
if ($PRIVATE_SECTION == 1){
print $private_section_line;
}
next;
}
if ( $SIP_RUN == 1 ){
if ( $line =~ m/^\s*#endif/ ){
if ( $nesting_index == 0 ){
$SIP_RUN = 0;
next;
}
else {
$nesting_index--;
}
}
if ( $line =~ m/^\s*#if(def)?\s+/ ){
$nesting_index++;
}
# if there is an else at this level, code will be ignored i.e. not SIP_RUN
if ( $line =~ m/^\s*#else/ && $nesting_index == 0){
while(!eof $header){
$line = readline $header;
if ( $line =~ m/^\s*#if(def)?\s+/ ){
$nesting_index++;
}
elsif ( $line =~ m/^\s*#endif/ ){
if ( $nesting_index == 0 ){
$SIP_RUN = 0;
last;
}
else {
$nesting_index--;
}
}
}
next;
}
}
elsif ( $line =~ m/^\s*#ifndef SIP_RUN/){
# code is ignored here
while(!eof $header){
$line = readline $header;
if ( $line =~ m/^\s*#if(def)?\s+/ ){
$nesting_index++;
}
elsif ( $line =~ m/^\s*#else/ && $nesting_index == 0 ){
# code here will be printed out
if ($PRIVATE_SECTION == 1){
print $private_section_line;
}
$SIP_RUN = 1;
last;
}
elsif ( $line =~ m/^\s*#endif/ ){
if ( $nesting_index == 0 ){
$SIP_RUN = 0;
last;
}
else {
$nesting_index--;
}
}
}
next;
}
else {
next;
}
}
# TYPE HEADER CODE
if ( $HEADER_CODE && $SIP_RUN == 0 ){
$HEADER_CODE = 0;
print "%End\n";
}
# Skip forward declarations
if ($line =~ m/^\s*class \w+;$/){
next;
}
# Skip Q_OBJECT, Q_PROPERTY, Q_ENUM, Q_GADGET
if ($line =~ m/^\s*Q_(OBJECT|ENUMS|PROPERTY|GADGET|DECLARE_METATYPE).*?$/){
next;
}
# SIP_SKIP
if ( $line =~ m/SIP_SKIP/ ){
next;
}
# Private members (exclude SIP_RUN)
if ( $line =~ m/^\s*private( slots)?:/ ){
$PRIVATE_SECTION = 1;
$private_section_line = $line;
next;
}
if ( $PRIVATE_SECTION == 1 ){
if ( $SIP_RUN == 0){
if ( $line =~ m/^\s*(public|protected)( slots)?:.*$/ || $line =~ m/^\s*\};.*$/){
$PRIVATE_SECTION = 0;
}
else {
next;
}
}
}
# Skip assignment operator
if ( $line =~ m/operator=\s*\(/ ){
print "// $line";
next;
}
# Detect comment block
if ($line =~ m/^\s*\/\*/){
do {no warnings 'uninitialized';
$comment = processDoxygenLine( $line =~ s/^\s*\/\*(\*)?(.*)$/$2/r );
};
$comment =~ s/^\s*$//;
while(!eof $header){
$line = readline $header;
$comment .= processDoxygenLine( $line =~ s/\s*\*?(.*?)(\/)?$/$1/r );
if ( $line =~ m/\*\/$/ ){
last;
}
}
$comment =~ s/(\n)+$//;
#print $comment;
next;
}
# save comments and do not print them, except in SIP_RUN
if ( $SIP_RUN == 0 ){
if ( $line =~ m/^\s*\/\// ){
$line =~ s/^\s*\/\/\!*\s*(.*)\n?$/$1/;
$comment = processDoxygenLine( $line );
next;
}
}
# class declaration started
if ( $line =~ m/^(\s*class)\s*([A-Z]+_EXPORT)?(\s+\w+)(\s*\:.*)?$/ ){
do {no warnings 'uninitialized';
my $classname = $3;
$line =~ m/\b[A-Z]+_EXPORT\b/ or die "Class$classname in $headerfile should be exported with appropriate [LIB]_EXPORT macro. If this should not be available in python, wrap it in a `#ifndef SIP_RUN` block.";
};
$line = "$1$3";
# Inheritance
if ($4){
my $m = $4;
$m =~ s/public //g;
$m =~ s/,?\s*private \w+//;
$m =~ s/(\s*:)?\s*$//;
$line .= $m;
}
$line .= "\n{\n";
if ( $comment !~ m/^\s*$/ ){
$line .= "%Docstring\n$comment\n%End\n";
}
$line .= "\n%TypeHeaderCode\n#include \"" . basename($headerfile) . "\"\n";
print $line;
my $skip;
# Skip opening curly bracket, we already added that above
$skip = readline $header;
$skip =~ m/^\s*{\s$/ || die "Unexpected content on line $line";
$comment = '';
$HEADER_CODE = 1;
next;
}
# Enum declaration
if ( $line =~ m/^\s*enum\s+\w+.*?$/ ){
print $line;
$line = readline $header;
$line =~ m/^\s*\{\s*$/ || die 'Unexpected content: enum should be followed by {';
print $line;
while(!eof $header){
$line = readline $header;
if ($line =~ m/\};/){
last;
}
$line =~ s/(\s*\w+)(\s*=\s*\w+.*?)?(,?).*$/$1$3/;
print $line;
}
print $line;
# enums don't have Docstring apparently
next;
}
do {no warnings 'uninitialized';
# remove keywords
$line =~ s/\s*override( SIP_\w+(\(.+\))?)?;/$1;/;
$line =~ s/^(\s*)?(const )?(virtual |static )?inline /$1$2$3/;
$line =~ s/\bnullptr\b/0/g;
$line =~ s/\s*=\s*default\b//g;
# remove constructor definition
if ( $line =~ m/^(\s*)?(explicit )?(\w+)\([\w\=\(\)\s\,\&\*\<\>]*\)(?!;)$/ ){
my $newline = $line =~ s/\n/;\n/r;
my $nesting_index = 0;
while(!eof $header){
$line = readline $header;
if ( $nesting_index == 0 ){
if ( $line =~ m/^\s*(:|,)/ ){
next;
}
$line =~ m/^\s*\{/ or die 'Constructor definition misses {';
if ( $line =~ m/^\s*\{.*?\}/ ){
last;
}
$nesting_index = 1;
next;
}
else {
$nesting_index += $line =~ tr/\{//;
$nesting_index -= $line =~ tr/\}//;
if ($nesting_index eq 0){
last;
}
}
}
$line = $newline;
}
# remove function bodies
if ( $line =~ m/^(\s*)?(const )?(virtual |static )?((\w+(<.*?>)?\s+(\*|&)?)?(\w+|operator.)\(.*?(\(.*\))*.*\)( (?:const|SIP_[A-Z_]*?))*)\s*(override)?\s*(\{.*\})?(?!;)(\s*\/\/.*)?$/ ){
my $newline = "$1$2$3$4;\n";
if ($line !~ m/\{.*?\}$/){
$line = readline $header;
if ( $line =~ m/^\s*\{\s*$/ ){
while(!eof $header){
$line = readline $header;
if ( $line =~ m/\}\s*$/ ){
last;
}
}
}
}
$line = $newline;
}
};
# deleted functions
if ( $line =~ m/^(\s*)?(const )?(virtual |static )?((\w+(<.*?>)?\s+(\*|&)?)?(\w+|operator.)\(.*?(\(.*\))*.*\)( const)?)\s*= delete;(\s*\/\/.*)?$/ ){
$comment = '';
next;
}
$line =~ s/\bSIP_FACTORY\b/\/Factory\//;
$line =~ s/\bSIP_OUT\b/\/Out\//g;
$line =~ s/\bSIP_INOUT\b/\/In,Out\//g;
$line =~ s/\bSIP_TRANSFER\b/\/Transfer\//g;
$line =~ s/\bSIP_KEEPREFERENCE\b/\/KeepReference\//;
$line =~ s/\bSIP_TRANSFERTHIS\b/\/TransferThis\//;
$line =~ s/\bSIP_TRANSFERBACK\b/\/TransferBack\//;
$line =~ s/\bSIP_RELEASEGIL\b/\/ReleaseGIL\//;
$line =~ s/SIP_PYNAME\(\s*(\w+)\s*\)/\/PyName=$1\//;
$line =~ s/(\w+)(\<(?>[^<>]|(?2))*\>)?\s+SIP_PYTYPE\(\s*\'?([^()']+)(\(\s*(?:[^()]++|(?2))*\s*\))?\'?\s*\)/$3/g;
$line =~ s/=\s+[^=]*?\s+SIP_PYDEFAULTVALUE\(\s*\'?([^()']+)(\(\s*(?:[^()]++|(?2))*\s*\))?\'?\s*\)/= $1/g;
# fix astyle placing space after % character
$line =~ s/\s*% (MappedType|TypeHeaderCode|ConvertFromTypeCode|ConvertToTypeCode|MethodCode|End)/%$1/;
$line =~ s/\/\s+GetWrapper\s+\//\/GetWrapper\//;
print $line;
# multiline definition (parenthesis left open)
if ( $MULTILINE_DEFINITION == 1 ){
if ( $line =~ m/^[^()]*([^()]*\([^()]*\)[^()]*)*\)[^()]*$/){
$MULTILINE_DEFINITION = 0;
}
else
{
next;
}
}
elsif ( $line =~ m/^[^()]+\([^()]*([^()]*\([^()]*\)[^()]*)*[^)]*$/ ){
$MULTILINE_DEFINITION = 1;
next;
}
# write comment
if ( $line =~ m/^\s*$/ || $line =~ m/\/\// || $line =~ m/\s*typedef / ){
$comment = '';
}
elsif ( $comment !~ m/^\s*$/ ){
print "%Docstring\n$comment\n%End\n";
$comment = '';
}
}
print "/************************************************************************\n";
print " * This file has been generated automatically from *\n";
print " * *\n";
printf " * %-*s *\n", 68, $headerfile;
print " * *\n";
print " * Do not edit manually ! Edit header and run scripts/sipify.pl again *\n";
print " ************************************************************************/\n";