Browse Source

Fixed the generated code when a default parameter of type char or wide char is assigned an int.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/681/head
Dimitar Dobrev 9 years ago
parent
commit
0ea801f7b9
  1. 15
      src/Generator/Passes/HandleDefaultParamValuesPass.cs
  2. 2
      tests/CSharp/CSharp.Tests.cs
  3. 4
      tests/CSharp/CSharp.cpp
  4. 1
      tests/CSharp/CSharp.h

15
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -84,7 +84,7 @@ namespace CppSharp.Passes
return CheckFloatSyntax(desugared, expression, ref result) || return CheckFloatSyntax(desugared, expression, ref result) ||
CheckForBinaryOperator(desugared, expression, ref result) || CheckForBinaryOperator(desugared, expression, ref result) ||
CheckForEnumValue(desugared, expression, ref result) || CheckForEnumValue(desugared, expression, ref result) ||
CheckForDefaultEmptyChar(desugared, expression, ref result); CheckForDefaultChar(desugared, ref result);
} }
private bool CheckForDefaultPointer(Type desugared, ref string result) private bool CheckForDefaultPointer(Type desugared, ref string result)
@ -306,14 +306,15 @@ namespace CppSharp.Passes
((BuiltinTypeExpression) defaultArgument).Value == 0; ((BuiltinTypeExpression) defaultArgument).Value == 0;
} }
private bool CheckForDefaultEmptyChar(Type desugared, Statement statement, private bool CheckForDefaultChar(Type desugared, ref string result)
ref string result)
{ {
if (statement.String == "0" && int value;
Driver.Options.MarshalCharAsManagedChar && if (int.TryParse(result, out value) &&
desugared.IsPrimitiveType(PrimitiveType.Char)) ((Driver.Options.MarshalCharAsManagedChar &&
desugared.IsPrimitiveType(PrimitiveType.Char)) ||
desugared.IsPrimitiveType(PrimitiveType.WideChar)))
{ {
result = "'\\0'"; result = value == 0 ? "'\\0'" : ("(char) " + result);
return true; return true;
} }

2
tests/CSharp/CSharp.Tests.cs

@ -213,6 +213,8 @@ public unsafe class CSharpTests : GeneratorTestFixture
methodsWithDefaultValues.DefaultWithGetPropertyCall(); methodsWithDefaultValues.DefaultWithGetPropertyCall();
methodsWithDefaultValues.DefaultWithIndirectStringConstant(); methodsWithDefaultValues.DefaultWithIndirectStringConstant();
methodsWithDefaultValues.DefaultWithDirectIntConstant(); methodsWithDefaultValues.DefaultWithDirectIntConstant();
methodsWithDefaultValues.DefaultWithEnumInLowerCasedNameSpace();
methodsWithDefaultValues.DefaultWithCharFromInt();
} }
} }

4
tests/CSharp/CSharp.cpp

@ -583,6 +583,10 @@ void MethodsWithDefaultValues::defaultWithEnumInLowerCasedNameSpace(lowerCaseNam
{ {
} }
void MethodsWithDefaultValues::defaultWithCharFromInt(char c)
{
}
int MethodsWithDefaultValues::getA() int MethodsWithDefaultValues::getA()
{ {
return m_foo.A; return m_foo.A;

1
tests/CSharp/CSharp.h

@ -411,6 +411,7 @@ public:
void defaultWithIndirectStringConstant(const Foo& arg = Foo(stringConstant)); void defaultWithIndirectStringConstant(const Foo& arg = Foo(stringConstant));
void defaultWithDirectIntConstant(int arg = intConstant); void defaultWithDirectIntConstant(int arg = intConstant);
void defaultWithEnumInLowerCasedNameSpace(lowerCaseNameSpace::Enum e = lowerCaseNameSpace::Enum::Item2); void defaultWithEnumInLowerCasedNameSpace(lowerCaseNameSpace::Enum e = lowerCaseNameSpace::Enum::Item2);
void defaultWithCharFromInt(char c = 32);
int getA(); int getA();
private: private:
Foo m_foo; Foo m_foo;

Loading…
Cancel
Save