Browse Source

CSharpExpressionPrinter: Wrap expression in parenthesis (#1741)

Wrap default parameter expressions in parentheses to ensure whole expressions are casted instead of only the first operand.
pull/1745/head
Trung Nguyen 2 years ago committed by GitHub
parent
commit
169f8686a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs
  2. 6
      tests/dotnet/Common/Common.cpp
  3. 2
      tests/dotnet/Common/Common.h

6
src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs

@ -27,11 +27,11 @@ namespace CppSharp.Generators.CSharp @@ -27,11 +27,11 @@ namespace CppSharp.Generators.CSharp
if (desugared.IsPrimitiveType() &&
(parameter.DefaultArgument.Declaration != null ||
parameter.DefaultArgument.Class == StatementClass.BinaryOperator))
return $"({desugared.Visit(typePrinter)}) {expression}";
return $"({desugared.Visit(typePrinter)}) ({expression})";
var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar();
if (finalType.TryGetClass(out var @class) && @class.IsInterface)
return $@"({@class.Visit(typePrinter)}) ({
@class.OriginalClass.Visit(typePrinter)}) {expression}";
@class.OriginalClass.Visit(typePrinter)}) ({expression})";
return expression;
}
@ -88,4 +88,4 @@ namespace CppSharp.Generators.CSharp @@ -88,4 +88,4 @@ namespace CppSharp.Generators.CSharp
private readonly TypePrinter typePrinter;
}
}
}

6
tests/dotnet/Common/Common.cpp

@ -1273,4 +1273,8 @@ extern "C" @@ -1273,4 +1273,8 @@ extern "C"
s.field2 = 10;
return s;
}
} // extern "C"
} // extern "C"
void DLL_API FunctionWithFlagsAsDefaultParameter(int defaultParam)
{
}

2
tests/dotnet/Common/Common.h

@ -1567,3 +1567,5 @@ extern "C" @@ -1567,3 +1567,5 @@ extern "C"
DLL_API void takeConflictName(struct system* self);
DLL_API struct system freeFunctionReturnByValue();
} // extern "C"
void DLL_API FunctionWithFlagsAsDefaultParameter(int defaultParam = A | B);

Loading…
Cancel
Save