aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2010-10-18 10:26:53 -0200
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:07:21 -0300
commit63fddfeb50bbd786826dd18be8851d0567cce041 (patch)
treeda8bd7e380c731057ff78232e30e76750bb83e97
parentc1ef5a79d14b598e7b0027be2922b23a78e57c02 (diff)
Fix bug#411 - "Incorrect return statement generated in a function that returns a reference"
Reviewer: Luciano Wolf <luciano.wolf@openbossa.org> Marcelo Lira <marcelo.lira@openbossa.org>
-rw-r--r--generator/cppgenerator.cpp43
-rw-r--r--tests/libsample/reference.cpp3
-rw-r--r--tests/libsample/reference.h11
-rw-r--r--tests/samplebinding/CMakeLists.txt1
-rw-r--r--tests/samplebinding/global.h1
-rw-r--r--tests/samplebinding/typesystem_sample.xml30
6 files changed, 75 insertions, 14 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp
index 79cabeadf..e031f39a3 100644
--- a/generator/cppgenerator.cpp
+++ b/generator/cppgenerator.cpp
@@ -490,14 +490,36 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, const AbstractMetaFu
Indentation indentation(INDENT);
+ QString defaultReturnExpr;
+ if (func->type()) {
+ foreach (FunctionModification mod, func->modifications()) {
+ foreach (ArgumentModification argMod, mod.argument_mods) {
+ if (argMod.index == 0 && !argMod.replacedDefaultExpression.isEmpty()) {
+ QRegExp regex("%(\\d+)");
+ defaultReturnExpr = argMod.replacedDefaultExpression;
+ int offset = 0;
+ while ((offset = regex.indexIn(defaultReturnExpr, offset)) != -1) {
+ int argId = regex.cap(1).toInt() - 1;
+ if (argId < 0 || argId > func->arguments().count()) {
+ ReportHandler::warning("The expression used in return value contains an invalid index.");
+ break;
+ }
+ defaultReturnExpr.replace(regex.cap(0), func->arguments()[argId]->name());
+ }
+ }
+ }
+ }
+ if (defaultReturnExpr.isEmpty()) {
+ QTextStream s(&defaultReturnExpr);
+ writeMinimalConstructorCallArguments(s, func->type());
+ }
+ }
+
if (func->isAbstract() && func->isModifiedRemoved()) {
ReportHandler::warning("Pure virtual method \"" + func->ownerClass()->name() + "::" + func->minimalSignature() + "\" must be implement but was completely removed on typesystem.");
s << INDENT << "return";
- if (func->type()) {
- s << ' ';
- writeMinimalConstructorCallArguments(s, func->type());
- }
- s << ';' << endl;
+
+ s << ' ' << defaultReturnExpr << ';' << endl;
s << '}' << endl << endl;
return;
}
@@ -525,7 +547,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, const AbstractMetaFu
s << "()' not implemented.\");" << endl;
s << INDENT << "return ";
if (func->type()) {
- writeMinimalConstructorCallArguments(s, func->type());
+ s << defaultReturnExpr;
}
} else {
if (func->allowThread()) {
@@ -630,10 +652,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, const AbstractMetaFu
{
Indentation indent(INDENT);
s << INDENT << "PyErr_Print();" << endl;
- s << INDENT << "return ";
- if (type)
- writeMinimalConstructorCallArguments(s, func->type());
- s << ';' << endl;
+ s << INDENT << "return " << defaultReturnExpr << ';' << endl;
}
s << INDENT << '}' << endl;
@@ -676,9 +695,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, const AbstractMetaFu
s << INDENT << "PyErr_Format(PyExc_TypeError, \"Invalid return value in function %s, expected %s, got %s.\", \""
<< func->ownerClass()->name() << '.' << func->name() << "\", " << desiredType
<< ", " PYTHON_RETURN_VAR "->ob_type->tp_name);" << endl;
- s << INDENT << "return ";
- writeMinimalConstructorCallArguments(s, func->type());
- s << ';' << endl;
+ s << INDENT << "return " << defaultReturnExpr << ';' << endl;
}
s << INDENT << "}" << endl;
}
diff --git a/tests/libsample/reference.cpp b/tests/libsample/reference.cpp
index 670395ff6..1456b1898 100644
--- a/tests/libsample/reference.cpp
+++ b/tests/libsample/reference.cpp
@@ -67,3 +67,6 @@ Reference::callAlterReferenceIdVirtual(Reference& r)
alterReferenceIdVirtual(r);
}
+ObjTypeReference::~ObjTypeReference()
+{
+}
diff --git a/tests/libsample/reference.h b/tests/libsample/reference.h
index 046a1518e..cc9fcc91e 100644
--- a/tests/libsample/reference.h
+++ b/tests/libsample/reference.h
@@ -51,9 +51,20 @@ public:
inline static int multiplier() { return 10; }
+ virtual Reference& returnMyFirstArg(Reference& ref) { return ref; }
+ virtual Reference& returnMySecondArg(int a, Reference& ref) { return ref; }
private:
int m_objId;
};
+class LIBSAMPLE_API ObjTypeReference
+{
+public:
+ virtual ~ObjTypeReference();
+ virtual ObjTypeReference& returnMyFirstArg(ObjTypeReference& ref) { return ref; }
+ virtual ObjTypeReference& returnMySecondArg(int a, ObjTypeReference& ref) { return ref; }
+ virtual ObjTypeReference& justAPureVirtualFunc(ObjTypeReference& ref) = 0;
+};
+
#endif // REFERENCE_H
diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt
index 3767a3a36..a0f68c543 100644
--- a/tests/samplebinding/CMakeLists.txt
+++ b/tests/samplebinding/CMakeLists.txt
@@ -43,6 +43,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/nondefaultctor_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/objecttype_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/objecttypelayout_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/objectview_wrapper.cpp
+${CMAKE_CURRENT_BINARY_DIR}/sample/objtypereference_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/oddbooluser_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/overload_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/pairuser_wrapper.cpp
diff --git a/tests/samplebinding/global.h b/tests/samplebinding/global.h
index 54a9795fa..8fae4ddf4 100644
--- a/tests/samplebinding/global.h
+++ b/tests/samplebinding/global.h
@@ -21,6 +21,7 @@
#include "nondefaultctor.h"
#include "objecttype.h"
#include "objecttypelayout.h"
+#include "objecttypereference.h"
#include "objectview.h"
#include "oddbool.h"
#include "overload.h"
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index 9d14681a2..2960ec734 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -623,7 +623,35 @@
-->
</object-type>
- <value-type name="Reference"/>
+ <value-type name="Reference">
+ <modify-function signature="returnMyFirstArg(Reference&amp;)">
+ <modify-argument index="return">
+ <replace-default-expression with="%1"/>
+ </modify-argument>
+ </modify-function>
+ <modify-function signature="returnMySecondArg(int, Reference&amp;)">
+ <modify-argument index="return">
+ <replace-default-expression with="%2"/>
+ </modify-argument>
+ </modify-function>
+ </value-type>
+ <object-type name="ObjTypeReference">
+ <modify-function signature="returnMyFirstArg(ObjTypeReference&amp;)">
+ <modify-argument index="return">
+ <replace-default-expression with="%1"/>
+ </modify-argument>
+ </modify-function>
+ <modify-function signature="returnMySecondArg(int, ObjTypeReference&amp;)">
+ <modify-argument index="return">
+ <replace-default-expression with="%2"/>
+ </modify-argument>
+ </modify-function>
+ <modify-function signature="justAPureVirtualFunc(ObjTypeReference&amp;)">
+ <modify-argument index="return">
+ <replace-default-expression with="%1"/>
+ </modify-argument>
+ </modify-function>
+ </object-type>
<value-type name="ImplicitConv">
<enum-type name="CtorEnum"/>
<enum-type name="ICOverloadedFuncEnum"/>