Skip to content

Commit

Permalink
[sipify] improvements
Browse files Browse the repository at this point in the history
 * add Array and ArraySize annotations
 * also handle multiline skipped bodies
 * handle #if 0 blocks
  • Loading branch information
3nids committed Apr 26, 2017
1 parent 7c71ea6 commit 9bb0762
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 15 deletions.
73 changes: 58 additions & 15 deletions scripts/sipify.pl
Expand Up @@ -51,7 +51,7 @@ sub processDoxygenLine
my $MULTILINE_DEFINITION = 0;

my $comment = '';
my $nesting_index = 0;
my $global_nesting_index = 0;
my $private_section_line = '';
my $classname = '';
my $return_type = '';
Expand Down Expand Up @@ -96,6 +96,27 @@ sub processDoxygenLine

# Skip preprocessor stuff
if ($line =~ m/^\s*#/){

# skip #if 0 blocks
if ( $line =~ m/^\s*#if 0/){
my $nesting_index = 0;
while ($line_idx < $line_count){
$line = $lines[$line_idx];
$line_idx++;
if ( $line =~ m/^\s*#if(def)?\s+/ ){
$nesting_index++;
}
elsif ( $nesting_index == 0 && $line =~ m/^\s*#(endif|else)/ ){
$comment = '';
last;
}
elsif ( $nesting_index != 0 && $line =~ m/^\s*#(endif)/ ){
$nesting_index--;
}
}
next;
}

if ( $line =~ m/^\s*#ifdef SIP_RUN/){
$SIP_RUN = 1;
if ($ACCESS == PRIVATE){
Expand All @@ -105,34 +126,34 @@ sub processDoxygenLine
}
if ( $SIP_RUN == 1 ){
if ( $line =~ m/^\s*#endif/ ){
if ( $nesting_index == 0 ){
if ( $global_nesting_index == 0 ){
$SIP_RUN = 0;
next;
}
else {
$nesting_index--;
$global_nesting_index--;
}
}
if ( $line =~ m/^\s*#if(def)?\s+/ ){
$nesting_index++;
$global_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){
if ( $line =~ m/^\s*#else/ && $global_nesting_index == 0){
while ($line_idx < $line_count){
$line = $lines[$line_idx];
$line_idx++;
if ( $line =~ m/^\s*#if(def)?\s+/ ){
$nesting_index++;
$global_nesting_index++;
}
elsif ( $line =~ m/^\s*#endif/ ){
if ( $nesting_index == 0 ){
if ( $global_nesting_index == 0 ){
$comment = '';
$SIP_RUN = 0;
last;
}
else {
$nesting_index--;
$global_nesting_index--;
}
}
}
Expand All @@ -145,9 +166,9 @@ sub processDoxygenLine
$line = $lines[$line_idx];
$line_idx++;
if ( $line =~ m/^\s*#if(def)?\s+/ ){
$nesting_index++;
$global_nesting_index++;
}
elsif ( $line =~ m/^\s*#else/ && $nesting_index == 0 ){
elsif ( $line =~ m/^\s*#else/ && $global_nesting_index == 0 ){
# code here will be printed out
if ($ACCESS == PRIVATE){
push @output, $private_section_line."\n";
Expand All @@ -156,13 +177,13 @@ sub processDoxygenLine
last;
}
elsif ( $line =~ m/^\s*#endif/ ){
if ( $nesting_index == 0 ){
if ( $global_nesting_index == 0 ){
$comment = '';
$SIP_RUN = 0;
last;
}
else {
$nesting_index--;
$global_nesting_index--;
}
}
}
Expand Down Expand Up @@ -202,11 +223,31 @@ sub processDoxygenLine
}
# also skip method body if there is one
if ($lines[$line_idx] =~ m/^\s*\{/){
while($lines[$line_idx] !~ m/^\s*\}/){
$line_idx++;
my $nesting_index = 0;
while ($line_idx < $line_count){
$line = $lines[$line_idx];
$line_idx++;
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_idx++;
}
# line skipped, go to next iteration
next;
}

Expand Down Expand Up @@ -497,6 +538,8 @@ sub processDoxygenLine
$line =~ s/\bSIP_TRANSFERTHIS\b/\/TransferThis\//;
$line =~ s/\bSIP_TRANSFERBACK\b/\/TransferBack\//;
$line =~ s/\bSIP_RELEASEGIL\b/\/ReleaseGIL\//;
$line =~ s/\bSIP_ARRAY\b/\/Array\//;
$line =~ s/\bSIP_ARRAYSIZE\b/\/ArraySize\//;

$line =~ s/SIP_PYNAME\(\s*(\w+)\s*\)/\/PyName=$1\//;
$line =~ s/(\w+)(\<(?>[^<>]|(?2))*\>)?\s+SIP_PYTYPE\(\s*\'?([^()']+)(\(\s*(?:[^()]++|(?2))*\s*\))?\'?\s*\)/$3/g;
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgis.h
Expand Up @@ -436,6 +436,16 @@ typedef unsigned long long qgssize;
*/
#define SIP_KEEPREFERENCE

/*
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html#argument-annotation-Array
*/
#define SIP_ARRAY

/*
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html#argument-annotation-ArraySize
*/
#define SIP_ARRAYSIZE

/*
* discard line
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/scripts/sipifyheader.expected.sip
Expand Up @@ -264,6 +264,9 @@ Mulitline body
int var;
}

void ZshouldBeShown();


protected:
bool thisShouldBeListed();
%Docstring
Expand Down
15 changes: 15 additions & 0 deletions tests/scripts/sipifyheader.h
Expand Up @@ -222,6 +222,10 @@ class CORE_EXPORT QgsSipifyHeader : public QtClass<QVariant>, private Ui::QgsBas
static inline QgsMapLayer *skippedMethodWithBody() SIP_SKIP
{
OhNoYouShouldnotHaveReadThis();
if ( ThisIsTrue() )
{
return false;
}
}

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

#if 0
#if Whatever
void X();
#else
void Y();
#endif
#else
void ZshouldBeShown();
#endif


protected:
bool thisShouldBeListed();

Expand Down

0 comments on commit 9bb0762

Please sign in to comment.