Skip to content

Commit 8ce4718

Browse files
authoredApr 22, 2023
[sipify] support multiple classes template definition (#52838)
1 parent db6cef0 commit 8ce4718

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed
 

‎scripts/sipify.pl‎

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -897,14 +897,16 @@ sub detect_non_method_member{
897897
}
898898

899899
# class declaration started
900-
# https://regex101.com/r/6FWntP/16
901-
if ( $LINE =~ m/^(\s*(class))\s+([A-Z0-9_]+_EXPORT\s+)?(Q_DECL_DEPRECATED\s+)?(?<classname>\w+)(?<domain>\s*\:\s*(public|protected|private)\s+\w+(< *(\w|::)+ *>)?(::\w+(<\w+>)?)*(,\s*(public|protected|private)\s+\w+(< *(\w|::)+ *>)?(::\w+(<\w+>)?)*)*)?(?<annot>\s*\/?\/?\s*SIP_\w+)?\s*?(\/\/.*|(?!;))$/ ){
900+
# https://regex101.com/r/KMQdF5/1 (older versions: https://regex101.com/r/6FWntP/16)
901+
if ( $LINE =~ m/^(\s*(class))\s+([A-Z0-9_]+_EXPORT\s+)?(Q_DECL_DEPRECATED\s+)?(?<classname>\w+)(?<domain>\s*\:\s*(public|protected|private)\s+\w+(< *(\w|::)+ *(, *(\w|::)+ *)*>)?(::\w+(<(\w|::)+(, *(\w|::)+)*>)?)*(,\s*(public|protected|private)\s+\w+(< *(\w|::)+ *(, *(\w|::)+)*>)?(::\w+(<\w+(, *(\w|::)+)?>)?)*)*)?(?<annot>\s*\/?\/?\s*SIP_\w+)?\s*?(\/\/.*|(?!;))$/ ){
902902
dbg_info("class definition started");
903903
push @ACCESS, PUBLIC;
904904
push @EXPORTED, 0;
905905
push @GLOB_BRACKET_NESTING_IDX, 0;
906906
my @template_inheritance_template = ();
907-
my @template_inheritance_class = ();
907+
my @template_inheritance_class1 = ();
908+
my @template_inheritance_class2 = ();
909+
my @template_inheritance_class3 = ();
908910
do {no warnings 'uninitialized';
909911
push @CLASSNAME, $+{classname};
910912
if ($#CLASSNAME == 0){
@@ -927,14 +929,23 @@ sub detect_non_method_member{
927929
$m =~ s/public +(\w+, *)*(Ui::\w+,? *)+//g; # remove Ui::xxx public inheritance as the namespace is causing troubles
928930
$m =~ s/public +//g;
929931
$m =~ s/[,:]?\s*private +\w+(::\w+)?//g;
932+
930933
# detect template based inheritance
931-
while ($m =~ /[,:]\s+((?!QList)\w+)< *((\w|::)+) *>/g){
934+
# https://regex101.com/r/9LGhyy/1
935+
while ($m =~ /[,:]\s+(?<tpl>(?!QList)\w+)< *(?<cls1>(\w|::)+) *(, *(?<cls2>(\w|::)+)? *(, *(?<cls3>(\w|::)+)? *)?)? *>/g){
932936
dbg_info("template class");
933-
push @template_inheritance_template, $1;
934-
push @template_inheritance_class, $2;
937+
push @template_inheritance_template, $+{tpl};
938+
push @template_inheritance_class1, $+{cls1};
939+
push @template_inheritance_class2, $+{cls2} // "";
940+
push @template_inheritance_class3, $+{cls3} // "";
941+
# dbg_info("template classes (max 3): $+{cls1} $+{cls2} $+{cls3}");
935942
}
936-
$m =~ s/(\b(?!QList)\w+)< *((?:\w|::)+) *>/$1${2}Base/g; # use the typeded as template inheritance
937-
$m =~ s/(\w+)< *((?:\w|::)+) *>//g; # remove remaining templates
943+
dbg_info("domain: $m");
944+
do {no warnings 'uninitialized';
945+
# https://regex101.com/r/nOLg2r/1
946+
$m =~ s/\b(?<tpl>(?!QList)\w+)< *(?<cls1>(\w|::)+) *(, *(?<cls2>(\w|::)+)? *(, *(?<cls3>(\w|::)+)? *)?)? *>/$+{tpl}$+{cls1}$+{cls2}$+{cls3}Base/g; # use the typeded as template inheritance
947+
};
948+
$m =~ s/(\w+)< *(?:\w|::)+ *>//g; # remove remaining templates
938949
$m =~ s/([:,])\s*,/$1/g;
939950
$m =~ s/(\s*[:,])?\s*$//;
940951
$LINE .= $m;
@@ -956,16 +967,30 @@ sub detect_non_method_member{
956967
# see https://www.riverbankcomputing.com/pipermail/pyqt/2015-May/035893.html
957968
while ( @template_inheritance_template ) {
958969
my $tpl = pop @template_inheritance_template;
959-
my $cls = pop @template_inheritance_class;
960-
$LINE = "\ntypedef $tpl<$cls> ${tpl}${cls}Base;\n\n$LINE";
970+
my $cls1 = pop @template_inheritance_class1;
971+
my $cls2 = pop @template_inheritance_class2;
972+
my $cls3 = pop @template_inheritance_class3;
973+
if ( $cls2 eq ""){
974+
$LINE = "\ntypedef $tpl<$cls1> ${tpl}${cls1}Base;\n\n$LINE";
975+
} elsif ( $cls3 eq ""){
976+
$LINE = "\ntypedef $tpl<$cls1,$cls2> ${tpl}${cls1}${cls2}Base;\n\n$LINE";
977+
} else {
978+
$LINE = "\ntypedef $tpl<$cls1,$cls2,$cls3> ${tpl}${cls1}${cls2}${cls3}Base;\n\n$LINE";
979+
}
961980
if ( not $tpl ~~ @DECLARED_CLASSES ){
962981
my $tpl_header = lc $tpl . ".h";
963982
if ( exists $SIP_CONFIG->{class_headerfile}->{$tpl} ){
964983
$tpl_header = $SIP_CONFIG->{class_headerfile}->{$tpl};
965984
}
966985
$LINE .= "\n#include \"" . $tpl_header . "\"";
967986
}
968-
$LINE .= "\ntypedef $tpl<$cls> ${tpl}${cls}Base;";
987+
if ( $cls2 eq ""){
988+
$LINE .= "\ntypedef $tpl<$cls1> ${tpl}${cls1}Base;";
989+
} elsif ( $cls3 eq ""){
990+
$LINE .= "\ntypedef $tpl<$cls1,$cls2> ${tpl}${cls1}${cls2}Base;";
991+
} else {
992+
$LINE .= "\ntypedef $tpl<$cls1,$cls2,$cls3> ${tpl}${cls1}${cls2}${cls3}Base;";
993+
}
969994
}
970995
if ( PRIVATE ~~ @ACCESS && $#ACCESS != 0){
971996
# do not write anything in PRIVATE context and not top level
@@ -1194,6 +1219,9 @@ sub detect_non_method_member{
11941219
# keyword fixes
11951220
do {no warnings 'uninitialized';
11961221
$LINE =~ s/^(\s*template\s*<)(?:class|typename) (\w+>)(.*)$/$1$2$3/;
1222+
$LINE =~ s/^(\s*template\s*<)(?:class|typename) (\w+) *, *(?:class|typename) (\w+>)(.*)$/$1$2,$3$4/;
1223+
# https://regex101.com/r/EB1mpx/1
1224+
$LINE =~ s/^(\s*template\s*<)(?:class|typename) (\w+) *, *(?:class|typename) (\w+) *, *(?:class|typename) (\w+>)(.*)$/$1$2,$3,$4$5/;
11971225
$LINE =~ s/\s*\boverride\b//;
11981226
$LINE =~ s/\s*\bSIP_MAKE_PRIVATE\b//;
11991227
$LINE =~ s/\s*\bFINAL\b/ \${SIP_FINAL}/;

‎tests/code_layout/sipify/sipifyheader.expected.sip‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,18 @@ class TemplateInheritance5 : SomethingElse
444444
%End
445445
}
446446

447+
typedef YourSuperTemplate<Abc,Def,Ghi> YourSuperTemplateAbcDefGhiBase;
448+
449+
class TemplateInheritance6 : SomethingElse, YourSuperTemplateAbcDefGhiBase
450+
{
451+
452+
%TypeHeaderCode
453+
#include "sipifyheader.h"
454+
#include "yoursupertemplate.h"
455+
typedef YourSuperTemplate<Abc,Def,Ghi> YourSuperTemplateAbcDefGhiBase;
456+
%End
457+
}
458+
447459

448460
/************************************************************************
449461
* This file has been generated automatically from *

‎tests/code_layout/sipify/sipifyheader.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ class CORE_EXPORT TemplateInheritance4 : public SomethingElse1, public QList<Som
523523
class CORE_EXPORT TemplateInheritance5 : public SomethingElse, public QList<Something>
524524
{
525525
}
526+
class CORE_EXPORT TemplateInheritance6 : public SomethingElse, public YourSuperTemplate<Abc, Def, Ghi>
527+
{
528+
}
526529

527530

528531
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.