sipify improvements

* add demo file and start a short doc
* fix some annotations
* do not display hidden line (SIP_SKIP, delete)
* use a defined var for SIP_SKIP
* remove constructor definition in header
* fix comment after method definition in header
* add a test for sipify itself
This commit is contained in:
Denis Rouzaud 2017-03-29 16:15:07 +02:00
parent 21be025d25
commit 15e4903890
14 changed files with 381 additions and 69 deletions

View File

@ -8,4 +8,5 @@ ENABLE_TESTING()
ADD_TEST(qgis_indentation ${CMAKE_SOURCE_DIR}/../../../scripts/verify-indentation.sh)
ADD_TEST(qgis_spelling ${CMAKE_SOURCE_DIR}/../../../scripts/spell_check/spell_test.sh)
ADD_TEST(qgis_sip ${CMAKE_SOURCE_DIR}/../../../tests/scripts/test_auto_sipfiles.sh)
ADD_TEST(qgis_sipify ${CMAKE_SOURCE_DIR}/../../../tests/scripts/test_sipify.sh)
ADD_TEST(qgis_sip_uptodate ${CMAKE_SOURCE_DIR}/../../../tests/scripts/test_sipfiles_uptodate.sh)

View File

@ -1356,7 +1356,8 @@ EXPAND_AS_DEFINED = "SIP_TRANSFER" \
"SIP_TRANSFERBACK" \
"SIP_FACTORY" \
"SIP_KEEPREFERENCE" \
"SIP_PYNAME"
"SIP_PYNAME" \
"SIP_SKIP"
# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then
# doxygen's preprocessor will remove all function-like macros that are alone

View File

@ -10,6 +10,7 @@
typedef QMap<int, QVariant> QgsAttributeMap;
typedef QMap<int, QString> QgsFieldNameMap;

View File

@ -10,6 +10,7 @@
typedef qint64 QgsFeatureId;
@ -152,14 +153,8 @@ class QgsFeature
Copy constructor
%End
// QgsFeature &operator=( const QgsFeature &rhs ); // SIP_SKIP
// bool operator==( const QgsFeature &other ) const; // SIP_SKIP
// bool operator!=( const QgsFeature &other ) const; // SIP_SKIP
virtual ~QgsFeature();
@ -437,10 +432,6 @@ Allows direct construction of QVariants from features.
}; // class QgsFeature
// CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFeature &feature ); // SIP_SKIP
// CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFeature &feature ); // SIP_SKIP
typedef QMap<qint64, QMap<int, QVariant> > QgsChangedAttributesMap;
@ -450,7 +441,5 @@ typedef QSet<qint64> QgsFeatureIds;
typedef QList<QgsFeature> QgsFeatureList;
// uint qHash( const QgsFeature &key, uint seed = 0 ); // SIP_SKIP

View File

@ -335,8 +335,6 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator
virtual ~QgsVectorLayer();
// QgsVectorLayer( const QgsVectorLayer &rhs ) = delete;
// QgsVectorLayer &operator=( QgsVectorLayer const &rhs ) = delete;
QString storageType() const;
%Docstring
@ -384,8 +382,6 @@ Returns a comment for the data in the layer
Returns the data provider
%End
// const QgsVectorDataProvider *dataProvider() const; // SIP_SKIP
void setProviderEncoding( const QString &encoding );
%Docstring
@ -630,8 +626,6 @@ Sets diagram rendering object (takes ownership)
Return renderer.
%End
// const QgsFeatureRenderer *renderer() const { return mRenderer; } // SIP_SKIP
void setRenderer( QgsFeatureRenderer *r /Transfer/ );
%Docstring
@ -973,10 +967,6 @@ TODO QGIS 3.0 returns an enum instead of a magic constant
@return 0 in case of success
%End
// const QgsAbstractVectorLayerLabeling *labeling() const { return mLabeling; } // SIP_SKIP
// void setLabeling( QgsAbstractVectorLayerLabeling *labeling ); // SIP_SKIP
virtual bool isEditable() const;
@ -1256,8 +1246,6 @@ Set annotation form for layer
Buffer with uncommitted editing operations. Only valid after editing has been turned on.
%End
// const QgsVectorLayerEditBuffer *editBuffer() const { return mEditBuffer; } // SIP_SKIP
void beginEditCommand( const QString &text );
%Docstring
@ -1512,8 +1500,6 @@ Returns the current transparency for the vector layer
QString metadata() const;
// inline QgsGeometryCache *cache() { return mCache; } // SIP_SKIP
void setSimplifyMethod( const QgsVectorSimplifyMethod &simplifyMethod );
%Docstring

View File

@ -35,7 +35,6 @@ while(!eof $header){
# 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;
@ -125,13 +124,11 @@ while(!eof $header){
# SIP_SKIP
if ( $line =~ m/SIP_SKIP/ ){
$line =~ s/^(\s*)(\w.*)$/$1\/\/ $2\n/;
print $line;
next;
}
# Private members (exclude SIP_RUN)
if ( $line =~ m/^\s*private( slots)?:.*$/ ){
if ( $line =~ m/^\s*private( slots)?:/ ){
$PRIVATE_SECTION = 1;
$private_section_line = $line;
next;
@ -148,18 +145,20 @@ while(!eof $header){
}
# Detect comment block
if ($line =~ m/\s*\/\*\*/){
$comment = $line =~ s/\s*\/\*\*(.*)$/$1/r;
if ($line =~ m/^\s*\/\*/){
do {no warnings 'uninitialized';
$comment = $line =~ s/^\s*\/\*(\*)?(.*)$/$2/r;
};
$comment =~ s/^\s*$//;
while(!eof $header){
$line = readline $header;
$line =~ m/\s*\*?(.*?)(\/)?$/;
$comment .= "$1\n";
$comment .= $line =~ s/\s*\*?(.*?)(\/)?$/$1/r;
if ( $line =~ m/\*\/$/ ){
last;
}
}
$comment =~ s/(\n)+$//;
#print $comment;
next;
}
@ -177,14 +176,17 @@ while(!eof $header){
$line = "$1$3";
# Inheritance
if ($4){
my $m;
$m = $4;
my $m = $4;
$m =~ s/public //g;
$m =~ s/\s*private \w+,?//;
$m =~ s/,?\s*private \w+//;
$m =~ s/(\s*:)?\s*$//;
$line .= $m;
}
$line .= "\n{\n%Docstring\n$comment\n%End\n";
$line .= "\n{\n";
if ( $comment !~ m/^\s*$/ ){
$line .= "%Docstring\n$comment\n%End\n";
}
$line .= "\n%TypeHeaderCode\n#include \"" . basename($headerfile) . "\"\n";
print $line;
@ -214,17 +216,46 @@ while(!eof $header){
print $line;
}
print $line;
# enums don't have Docstring apparently
next;
}
# remove keywords
do {no warnings 'uninitialized';
$line =~ s/\s*override( SIP_FACTORY)?;/$1;/;
# 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;
# remove constructor definition
if ( $line =~ m/^(\s*)?(explicit )?(\w+)\(([^()]*\([^()]*\)[^()]*)*\)(?!;)$/ ){
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)?)\s*(\{.*\})?(?!;)$/ ){
if ( $line =~ m/^(\s*)?(const )?(virtual |static )?((\w+(<.*?>)?\s+(\*|&)?)?(\w+|operator.)\(.*?(\(.*\))*.*\)( const)?)\s*(\{.*\})?(?!;)(\s*\/\/.*)?$/ ){
my $newline = "$1$2$3$4;\n";
if ($line !~ m/\{.*?\}$/){
$line = readline $header;
@ -242,18 +273,20 @@ while(!eof $header){
};
# deleted functions
if ( $line =~ m/^(\s*)?(const )?(virtual |static )?((\w+(<.*?>)?\s+(\*|&)?)?(\w+|operator.)\(.*?(\(.*\))*.*\)( const)?)\s*= delete;$/ ){
$line =~ s/^/\/\//;
if ( $line =~ m/^(\s*)?(const )?(virtual |static )?((\w+(<.*?>)?\s+(\*|&)?)?(\w+|operator.)\(.*?(\(.*\))*.*\)( const)?)\s*= delete;(\s*\/\/.*)?$/ ){
$comment = '';
next;
}
$line =~ s/SIP_FACTORY/\/Factory\//;
$line =~ s/SIP_OUT/\/Out\//g;
$line =~ s/SIP_INOUT/\/In,Out\//g;
$line =~ s/SIP_TRANSFER/\/Transfer\//g;
$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/SIP_PYNAME\(\s*(\w+)\s*\)/\/PyName=$1\//;
$line =~ s/SIP_KEEPREFERENCE\((\w+)\)/\/KeepReference\//;
$line =~ s/SIP_TRANSFERTHIS\((\w+)\)/\/TransferThis\//;
$line =~ s/SIP_TRANSFERBACK\((\w+)\)/\/TransferBack\//;
# fix astyle placing space after % character
$line =~ s/\s*% (MappedType|TypeHeaderCode|ConvertFromTypeCode|ConvertToTypeCode|MethodCode|End)/%$1/;
@ -280,7 +313,7 @@ while(!eof $header){
if ( $line =~ m/^\s*$/ || $line =~ m/\/\// || $line =~ m/\s*typedef / ){
$comment = '';
}
elsif ( $comment ne '' ){
elsif ( $comment !~ m/^\s*$/ ){
print "%Docstring\n$comment\n%End\n";
$comment = '';
}

View File

@ -416,7 +416,7 @@ typedef unsigned long long qgssize;
* and
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#argument-annotation-Out
*/
#define SIP_IN_OUT
#define SIP_INOUT
/**
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#function-annotation-Factory
@ -432,3 +432,8 @@ typedef unsigned long long qgssize;
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#argument-annotation-KeepReference
*/
#define SIP_KEEPREFERENCE
/**
* discard line
*/
#define SIP_SKIP

View File

@ -19,6 +19,8 @@
#define QGSATTRIBUTES_H
#include "qgis_core.h"
#include "qgis.h"
#include <QMap>
#include <QString>
#include <QVariant>
@ -113,7 +115,7 @@ class CORE_EXPORT QgsAttributes : public QVector<QVariant>
* @note added in QGIS 3.0
* @note not available in Python bindings
*/
QgsAttributeMap toMap() const; // SIP_SKIP
QgsAttributeMap toMap() const SIP_SKIP;
inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
};

View File

@ -17,6 +17,8 @@ email : sherman at mrcc.com
#define QGSFEATURE_H
#include "qgis_core.h"
#include "qgis.h"
#include <QExplicitlySharedDataPointer>
#include <QList>
#include <QMap>
@ -200,17 +202,17 @@ class CORE_EXPORT QgsFeature
/** Assignment operator
*/
QgsFeature &operator=( const QgsFeature &rhs ); // SIP_SKIP
QgsFeature &operator=( const QgsFeature &rhs ) SIP_SKIP;
/**
* Compares two features
*/
bool operator==( const QgsFeature &other ) const; // SIP_SKIP
bool operator==( const QgsFeature &other ) const SIP_SKIP;
/**
* Compares two features
*/
bool operator!=( const QgsFeature &other ) const; // SIP_SKIP
bool operator!=( const QgsFeature &other ) const SIP_SKIP;
virtual ~QgsFeature();
@ -498,9 +500,9 @@ class CORE_EXPORT QgsFeature
}; // class QgsFeature
//! Writes the feature to stream out. QGIS version compatibility is not guaranteed.
CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFeature &feature ); // SIP_SKIP
CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFeature &feature ) SIP_SKIP;
//! Reads a feature from stream in into feature. QGIS version compatibility is not guaranteed.
CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFeature &feature ); // SIP_SKIP
CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFeature &feature ) SIP_SKIP;
// key = feature id, value = changed attributes
#ifndef SIP_RUN
@ -524,7 +526,7 @@ typedef QSet<qint64> QgsFeatureIds;
typedef QList<QgsFeature> QgsFeatureList;
uint qHash( const QgsFeature &key, uint seed = 0 ); // SIP_SKIP
uint qHash( const QgsFeature &key, uint seed = 0 ) SIP_SKIP;
Q_DECLARE_METATYPE( QgsFeature )
Q_DECLARE_METATYPE( QgsFeatureList )

View File

@ -443,7 +443,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
/** Returns the data provider in a const-correct manner
* @note not available in python bindings
*/
const QgsVectorDataProvider *dataProvider() const; // SIP_SKIP
const QgsVectorDataProvider *dataProvider() const SIP_SKIP;
//! Sets the textencoding of the data provider
void setProviderEncoding( const QString &encoding );
@ -667,7 +667,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
/** Return const renderer.
* @note not available in python bindings
*/
const QgsFeatureRenderer *renderer() const { return mRenderer; } // SIP_SKIP
const QgsFeatureRenderer *renderer() const SIP_SKIP { return mRenderer; }
/**
* Set renderer which will be invoked to represent this layer.
@ -1045,13 +1045,13 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* @note added in 2.12
* @note not available in Python bindings
*/
const QgsAbstractVectorLayerLabeling *labeling() const { return mLabeling; } // SIP_SKIP
const QgsAbstractVectorLayerLabeling *labeling() const SIP_SKIP { return mLabeling; }
/** Set labeling configuration. Takes ownership of the object.
* @note added in 2.12
* @note not available in Python bindings
*/
void setLabeling( QgsAbstractVectorLayerLabeling *labeling ); // SIP_SKIP
void setLabeling( QgsAbstractVectorLayerLabeling *labeling ) SIP_SKIP;
//! Returns true if the provider is in editing mode
virtual bool isEditable() const override;
@ -1297,7 +1297,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! Buffer with uncommitted editing operations. Only valid after editing has been turned on.
//! @note not available in python bindings
const QgsVectorLayerEditBuffer *editBuffer() const { return mEditBuffer; } // SIP_SKIP
const QgsVectorLayerEditBuffer *editBuffer() const SIP_SKIP { return mEditBuffer; }
/**
* Create edit command for undo/redo operations
@ -1529,7 +1529,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
QString metadata() const override;
//! @note not available in python bindings
inline QgsGeometryCache *cache() { return mCache; } // SIP_SKIP
inline QgsGeometryCache *cache() SIP_SKIP { return mCache; }
/** Set the simplification settings for fast rendering of features
* @note added in 2.2

View File

@ -0,0 +1,117 @@
/******************************************************************
* This file has been generated automatically by sipify.pl *
* Do not edit manually ! Edit header file and generate it again. *
*****************************************************************/
typedef qint64 QgsFeatureId;
typedef QVector<QVariant> QgsSuperClass;
%MappedType QgsSuperClass
{
// The annotations are modified by astyle (these will be fixed by sipify.pl)
%TypeHeaderCode
#include <qgssipifyheader.h>
%End
%ConvertFromTypeCode
// Create the list.
PyObject *l;
return l;
%End
}
class QgsSipifyHeader : QtClass<QVariant>
{
%Docstring
\ingroup core
Documentation goes here
%End
%TypeHeaderCode
#include "sipifyheader.h"
%End
public:
enum MyEnum
{
Success,
NoSuccess,
};
explicit QgsSipifyHeader();
%Docstring
A constructor with definition in header
%End
QgsSipifyHeader( QWidget *parent /TransferThis/ = 0 );
%Docstring
A classic constructor with arguments
%End
void multilineMethod( const QgsPoint &startPoint,
QgsFeatureId featureId,
QgsVectorLayer *vl,
QgsSnapper::SnappingType snap_to ) const;
%Docstring
A multiline method signature
%End
virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) /Factory/;
%Docstring
Factory annotation
%End
void LongDocStringMethod();
%Docstring
My long doc string
is not very interesting!
%End
bool isOKwithErrMesg( QString &ErrMsg /Out/ );
void InOutParam( bool &ok = true /In,Out/ );
void setDiagramRenderer( QgsDiagramRenderer *r /Transfer/ );
inlineKeyWordShouldNotAppear();
QString labelForRange( double lower, double upper ) const /PyName=labelForLowerUpper/;
void setComposition( QgsComposition *c /KeepReference/ );
void removeProxyFactory( QNetworkProxyFactory *factory /TransferBack/ );
bool removeFunctionBody( const QList<int, QString> &list, QgsVectorLayer *vl );
protected:
bool thisShouldBeListed();
private:
void privateMethodSIPRUNareShown();
public:
void FallBackToPublic();
};
class ClassWithPrivateInheritanceOnly
{
%TypeHeaderCode
#include "sipifyheader.h"
%End
explicit ClassWithPrivateInheritanceOnly();
%Docstring
A constructor with definition in header on several lines
%End
}

View File

@ -0,0 +1,164 @@
/***************************************************************************
sipifyheader.h - Demo for sipify.pl
--------------------------------------
Date : 28.03.2017
Copyright : (C) 2017 Denis Rouzaud
email : denis.rouzaud@gmail.com
***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef SIPIFYHEADER_H
#define SIPIFYHEADER_H
#include "qgis_core.h"
#include <QtClass>
#include "sipifyheader.h"
// one shall include qgis.h to use SIP annotations
#include "qgis.h"
class QgsForwardDeclaration;
/***************************************************************************
* This is some random block comment that will not be displayed.
* Block comments shall will placed upon class, method, enum without
* any blank lines in between.
****************************************************************************/
// typedef have no Docstring, so commenting here will not be used
#ifdef SIP_RUN
typedef qint64 QgsFeatureId;
#else
typedef WhatEver ShouldNotBeDisplayed;
#endif
/** \ingroup core
* A super QGIS class
*/
#ifndef SIP_RUN // following will be hidden
class CORE_EXPORT QgsSuperClass : public QtClass<QVariant>
{
public:
//! A constructor with definition in header
QgsSuperClass()
: QtClass<QVariant>()
{}
};
#else // following will be displayed in generated file
typedef QVector<QVariant> QgsSuperClass;
% MappedType QgsSuperClass
{
// The annotations are modified by astyle (these will be fixed by sipify.pl)
% TypeHeaderCode
#include <qgssipifyheader.h>
% End
% ConvertFromTypeCode
// Create the list.
PyObject *l;
return l;
% End
}
#endif
/** \ingroup core
* Documentation goes here
*/
class CORE_EXPORT QgsSipifyHeader : public QtClass<QVariant>, private QgsBaseClass
{
public:
//! This is an enum
enum MyEnum
{
Success = 0, //!< Edit operation was successful
NoSuccess = 1, //!< Edit operation resulted in an empty geometry
};
//! A constructor with definition in header
explicit QgsSipifyHeader()
: QtClass<QVariant>()
, QgsBaseClass()
{}
/*
* A classic constructor with arguments
*/
QgsSipifyHeader( QWidget *parent SIP_TRANSFERTHIS = nullptr );
//! A multiline method signature
void multilineMethod( const QgsPoint &startPoint,
QgsFeatureId featureId,
QgsVectorLayer *vl,
QgsSnapper::SnappingType snap_to ) const;
// Adding SIP_SKIP at the end of a line will discard this MethodCode
bool thisShouldBeSkipped() const SIP_SKIP;
//! Factory annotation
virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY;
/**
* My long doc string
* is not very interesting!
*/
void LongDocStringMethod();
bool isOKwithErrMesg( QString &ErrMsg SIP_OUT );
void InOutParam( bool &ok = true SIP_INOUT );
void setDiagramRenderer( QgsDiagramRenderer *r SIP_TRANSFER );
inline inlineKeyWordShouldNotAppear();
QString labelForRange( double lower, double upper ) const SIP_PYNAME( labelForLowerUpper );
void setComposition( QgsComposition *c SIP_KEEPREFERENCE );
void removeProxyFactory( QNetworkProxyFactory *factory SIP_TRANSFERBACK );
bool removeFunctionBody( const QList<int, QString> &list, QgsVectorLayer *vl ) { doSomething; return true; } // some comments
bool deletedFunction() = delete; // some comments
protected:
bool thisShouldBeListed();
private:
void privateMethodAreNotShown();
#ifdef SIP_RUN
void privateMethodSIPRUNareShown();
#endif
public:
void FallBackToPublic();
private:
void PrivateAgain();
};
class CORE_EXPORT ClassWithPrivateInheritanceOnly : private QgsBaseClass
{
//! A constructor with definition in header on several lines
explicit ClassWithPrivateInheritanceOnly()
: QtClass<QVariant>()
, QgsBaseClass()
{
doWhatYouLike();
haveFun();
}
}
#endif

View File

@ -1,7 +1,5 @@
#!/usr/bin/env bash
set -e
DIR=$(git rev-parse --show-toplevel)
# GNU prefix command for mac os support (gsed, gsplit)

13
tests/scripts/test_sipify.sh Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
# This runs sipify on the demo header and checks output
DIR=$(git rev-parse --show-toplevel)
outdiff=$(${DIR}/scripts/sipify.pl ${DIR}/tests/scripts/sipifyheader.h | diff ${DIR}/tests/scripts/sipifyheader.expected.sip -)
if [[ $outdiff ]]; then
echo " *** sipify.pl did not output expected file"
echo "$outdiff"
exit 1
fi