Skip to content

Commit 9bb0762

Browse files
committed
[sipify] improvements
* add Array and ArraySize annotations * also handle multiline skipped bodies * handle #if 0 blocks
1 parent 7c71ea6 commit 9bb0762

File tree

4 files changed

+86
-15
lines changed

4 files changed

+86
-15
lines changed

scripts/sipify.pl

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ sub processDoxygenLine
5151
my $MULTILINE_DEFINITION = 0;
5252

5353
my $comment = '';
54-
my $nesting_index = 0;
54+
my $global_nesting_index = 0;
5555
my $private_section_line = '';
5656
my $classname = '';
5757
my $return_type = '';
@@ -96,6 +96,27 @@ sub processDoxygenLine
9696

9797
# Skip preprocessor stuff
9898
if ($line =~ m/^\s*#/){
99+
100+
# skip #if 0 blocks
101+
if ( $line =~ m/^\s*#if 0/){
102+
my $nesting_index = 0;
103+
while ($line_idx < $line_count){
104+
$line = $lines[$line_idx];
105+
$line_idx++;
106+
if ( $line =~ m/^\s*#if(def)?\s+/ ){
107+
$nesting_index++;
108+
}
109+
elsif ( $nesting_index == 0 && $line =~ m/^\s*#(endif|else)/ ){
110+
$comment = '';
111+
last;
112+
}
113+
elsif ( $nesting_index != 0 && $line =~ m/^\s*#(endif)/ ){
114+
$nesting_index--;
115+
}
116+
}
117+
next;
118+
}
119+
99120
if ( $line =~ m/^\s*#ifdef SIP_RUN/){
100121
$SIP_RUN = 1;
101122
if ($ACCESS == PRIVATE){
@@ -105,34 +126,34 @@ sub processDoxygenLine
105126
}
106127
if ( $SIP_RUN == 1 ){
107128
if ( $line =~ m/^\s*#endif/ ){
108-
if ( $nesting_index == 0 ){
129+
if ( $global_nesting_index == 0 ){
109130
$SIP_RUN = 0;
110131
next;
111132
}
112133
else {
113-
$nesting_index--;
134+
$global_nesting_index--;
114135
}
115136
}
116137
if ( $line =~ m/^\s*#if(def)?\s+/ ){
117-
$nesting_index++;
138+
$global_nesting_index++;
118139
}
119140

120141
# if there is an else at this level, code will be ignored i.e. not SIP_RUN
121-
if ( $line =~ m/^\s*#else/ && $nesting_index == 0){
142+
if ( $line =~ m/^\s*#else/ && $global_nesting_index == 0){
122143
while ($line_idx < $line_count){
123144
$line = $lines[$line_idx];
124145
$line_idx++;
125146
if ( $line =~ m/^\s*#if(def)?\s+/ ){
126-
$nesting_index++;
147+
$global_nesting_index++;
127148
}
128149
elsif ( $line =~ m/^\s*#endif/ ){
129-
if ( $nesting_index == 0 ){
150+
if ( $global_nesting_index == 0 ){
130151
$comment = '';
131152
$SIP_RUN = 0;
132153
last;
133154
}
134155
else {
135-
$nesting_index--;
156+
$global_nesting_index--;
136157
}
137158
}
138159
}
@@ -145,9 +166,9 @@ sub processDoxygenLine
145166
$line = $lines[$line_idx];
146167
$line_idx++;
147168
if ( $line =~ m/^\s*#if(def)?\s+/ ){
148-
$nesting_index++;
169+
$global_nesting_index++;
149170
}
150-
elsif ( $line =~ m/^\s*#else/ && $nesting_index == 0 ){
171+
elsif ( $line =~ m/^\s*#else/ && $global_nesting_index == 0 ){
151172
# code here will be printed out
152173
if ($ACCESS == PRIVATE){
153174
push @output, $private_section_line."\n";
@@ -156,13 +177,13 @@ sub processDoxygenLine
156177
last;
157178
}
158179
elsif ( $line =~ m/^\s*#endif/ ){
159-
if ( $nesting_index == 0 ){
180+
if ( $global_nesting_index == 0 ){
160181
$comment = '';
161182
$SIP_RUN = 0;
162183
last;
163184
}
164185
else {
165-
$nesting_index--;
186+
$global_nesting_index--;
166187
}
167188
}
168189
}
@@ -202,11 +223,31 @@ sub processDoxygenLine
202223
}
203224
# also skip method body if there is one
204225
if ($lines[$line_idx] =~ m/^\s*\{/){
205-
while($lines[$line_idx] !~ m/^\s*\}/){
206-
$line_idx++;
226+
my $nesting_index = 0;
227+
while ($line_idx < $line_count){
228+
$line = $lines[$line_idx];
229+
$line_idx++;
230+
if ( $nesting_index == 0 ){
231+
if ( $line =~ m/^\s*(:|,)/ ){
232+
next;
233+
}
234+
$line =~ m/^\s*\{/ or die 'Constructor definition misses {';
235+
if ( $line =~ m/^\s*\{.*?\}/ ){
236+
last;
237+
}
238+
$nesting_index = 1;
239+
next;
240+
}
241+
else {
242+
$nesting_index += $line =~ tr/\{//;
243+
$nesting_index -= $line =~ tr/\}//;
244+
if ($nesting_index eq 0){
245+
last;
246+
}
247+
}
207248
}
208-
$line_idx++;
209249
}
250+
# line skipped, go to next iteration
210251
next;
211252
}
212253

@@ -497,6 +538,8 @@ sub processDoxygenLine
497538
$line =~ s/\bSIP_TRANSFERTHIS\b/\/TransferThis\//;
498539
$line =~ s/\bSIP_TRANSFERBACK\b/\/TransferBack\//;
499540
$line =~ s/\bSIP_RELEASEGIL\b/\/ReleaseGIL\//;
541+
$line =~ s/\bSIP_ARRAY\b/\/Array\//;
542+
$line =~ s/\bSIP_ARRAYSIZE\b/\/ArraySize\//;
500543

501544
$line =~ s/SIP_PYNAME\(\s*(\w+)\s*\)/\/PyName=$1\//;
502545
$line =~ s/(\w+)(\<(?>[^<>]|(?2))*\>)?\s+SIP_PYTYPE\(\s*\'?([^()']+)(\(\s*(?:[^()]++|(?2))*\s*\))?\'?\s*\)/$3/g;

src/core/qgis.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ typedef unsigned long long qgssize;
436436
*/
437437
#define SIP_KEEPREFERENCE
438438

439+
/*
440+
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html#argument-annotation-Array
441+
*/
442+
#define SIP_ARRAY
443+
444+
/*
445+
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html#argument-annotation-ArraySize
446+
*/
447+
#define SIP_ARRAYSIZE
448+
439449
/*
440450
* discard line
441451
*/

tests/scripts/sipifyheader.expected.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ Mulitline body
264264
int var;
265265
}
266266

267+
void ZshouldBeShown();
268+
269+
267270
protected:
268271
bool thisShouldBeListed();
269272
%Docstring

tests/scripts/sipifyheader.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ class CORE_EXPORT QgsSipifyHeader : public QtClass<QVariant>, private Ui::QgsBas
222222
static inline QgsMapLayer *skippedMethodWithBody() SIP_SKIP
223223
{
224224
OhNoYouShouldnotHaveReadThis();
225+
if ( ThisIsTrue() )
226+
{
227+
return false;
228+
}
225229
}
226230

227231
//! Removing function body with namespaced return value
@@ -275,6 +279,17 @@ class CORE_EXPORT QgsSipifyHeader : public QtClass<QVariant>, private Ui::QgsBas
275279
int var;
276280
}
277281

282+
#if 0
283+
#if Whatever
284+
void X();
285+
#else
286+
void Y();
287+
#endif
288+
#else
289+
void ZshouldBeShown();
290+
#endif
291+
292+
278293
protected:
279294
bool thisShouldBeListed();
280295

0 commit comments

Comments
 (0)