Browse Source

Fixed the generated C# when an unsigned enum is assigned a negative value.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1166/head
Dimitar Dobrev 6 years ago
parent
commit
12642f9a42
  1. 2
      build/InstallMono.sh
  2. 34
      src/Generator/Passes/HandleDefaultParamValuesPass.cs
  3. 1
      tests/CSharp/CSharp.Tests.cs
  4. 4
      tests/CSharp/CSharp.cpp
  5. 5
      tests/CSharp/CSharp.h

2
build/InstallMono.sh

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
OS=$(uname -s)
if [ "$OS" == "Darwin" ]; then
wget -O mono.pkg https://download.mono-project.com/archive/5.2.0/macos-10-universal/MonoFramework-MDK-5.2.0.215.macos10.xamarin.universal.pkg
wget -O mono.pkg https://download.mono-project.com/archive/5.18.0/macos-10-universal/MonoFramework-MDK-5.18.0.240.macos10.xamarin.universal.pkg
sudo installer -pkg mono.pkg -target /
export PATH=$PATH:/Library/Frameworks/Mono.framework/Versions/Current/bin
elif [ "$OS" == "Linux" ]; then

34
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -129,8 +129,7 @@ namespace CppSharp.Passes @@ -129,8 +129,7 @@ namespace CppSharp.Passes
Class @class;
if (desugared.GetFinalPointee().TryGetClass(out @class) && @class.IsValueType)
{
result = string.Format("new {0}()",
new CSharpTypePrinter(Context).VisitClassDecl(@class));
result = $"new {@class.Visit(new CSharpTypePrinter(Context))}()";
return true;
}
@ -253,15 +252,40 @@ namespace CppSharp.Passes @@ -253,15 +252,40 @@ namespace CppSharp.Passes
if (call != null && statement.String != "0")
{
var @params = regexFunctionParams.Match(statement.String).Groups[1].Value;
result = TranslateEnumExpression(call, desugared, @params);
result = TranslateEnumExpression(desugared, @params);
return true;
}
if (desugared.TryGetEnum(out Enumeration @enum) &&
int.TryParse(statement.String, out int value))
{
var typePrinter = new CSharpTypePrinter(Context);
var printedEnum = @enum.Visit(typePrinter);
if (value < 0)
switch (@enum.BuiltinType.Type)
{
case PrimitiveType.UShort:
case PrimitiveType.UInt:
case PrimitiveType.ULong:
case PrimitiveType.ULongLong:
case PrimitiveType.UInt128:
result = $@"({printedEnum}) unchecked(({
@enum.BuiltinType.Visit(typePrinter)}) {
statement.String})";
break;
default:
result = $"({printedEnum}) ({statement.String})";
break;
}
else
result = $"({printedEnum}) {statement.String}";
return true;
}
return false;
}
private string TranslateEnumExpression(Function function,
Type desugared, string @params)
private string TranslateEnumExpression(Type desugared, string @params)
{
if (@params.Contains("::"))
return regexDoubleColon.Replace(@params, desugared + ".");

1
tests/CSharp/CSharp.Tests.cs

@ -229,6 +229,7 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -229,6 +229,7 @@ public unsafe class CSharpTests : GeneratorTestFixture
methodsWithDefaultValues.DefaultValueType();
methodsWithDefaultValues.DefaultChar();
methodsWithDefaultValues.DefaultEmptyChar();
methodsWithDefaultValues.DefaultEmptyEnum();
methodsWithDefaultValues.DefaultRefTypeBeforeOthers();
methodsWithDefaultValues.DefaultRefTypeAfterOthers();
methodsWithDefaultValues.DefaultRefTypeBeforeAndAfterOthers(0, null);

4
tests/CSharp/CSharp.cpp

@ -584,6 +584,10 @@ void MethodsWithDefaultValues::defaultEmptyChar(char c) @@ -584,6 +584,10 @@ void MethodsWithDefaultValues::defaultEmptyChar(char c)
{
}
void MethodsWithDefaultValues::defaultEmptyEnum(Empty e)
{
}
void MethodsWithDefaultValues::defaultRefTypeBeforeOthers(Foo foo, int i, Bar::Items item)
{
}

5
tests/CSharp/CSharp.h

@ -379,6 +379,10 @@ public: @@ -379,6 +379,10 @@ public:
DefaultZeroMappedToEnum(int* = 0);
};
enum class Empty : unsigned long long int
{
};
class DLL_API MethodsWithDefaultValues : public Quux
{
public:
@ -404,6 +408,7 @@ public: @@ -404,6 +408,7 @@ public:
void defaultValueType(QGenericArgument valueType = QGenericArgument());
void defaultChar(char c = 'a');
void defaultEmptyChar(char c = 0);
void defaultEmptyEnum(Empty e = Empty(-1));
void defaultRefTypeBeforeOthers(Foo foo = Foo(), int i = 5, Bar::Items item = Bar::Item2);
void defaultRefTypeAfterOthers(int i = 5, Bar::Items item = Bar::Item2, Foo foo = Foo());
void defaultRefTypeBeforeAndAfterOthers(int i = 5, Foo foo = Foo(), Bar::Items item = Bar::Item2, Baz baz = Baz());

Loading…
Cancel
Save