From 355c22219ae19010d7289e44af8f8fa29dc97618 Mon Sep 17 00:00:00 2001 From: Trung Nguyen <57174311+trungnt2910@users.noreply.github.com> Date: Sun, 9 Jul 2023 19:50:23 +0700 Subject: [PATCH] CSharpExpressionPrinter: Wrap expression in parenthesis Wrap default parameter expressions in parentheses to ensure whole expressions are casted instead of only the first operand. --- src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs | 6 +++--- tests/dotnet/Common/Common.cpp | 6 +++++- tests/dotnet/Common/Common.h | 2 ++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs b/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs index c4c2f1d5..c625164a 100644 --- a/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs @@ -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 private readonly TypePrinter typePrinter; } -} \ No newline at end of file +} diff --git a/tests/dotnet/Common/Common.cpp b/tests/dotnet/Common/Common.cpp index 04d63cdd..d985b92c 100644 --- a/tests/dotnet/Common/Common.cpp +++ b/tests/dotnet/Common/Common.cpp @@ -1273,4 +1273,8 @@ extern "C" s.field2 = 10; return s; } -} // extern "C" \ No newline at end of file +} // extern "C" + +void DLL_API FunctionWithFlagsAsDefaultParameter(int defaultParam) +{ +} diff --git a/tests/dotnet/Common/Common.h b/tests/dotnet/Common/Common.h index c018c422..3b4af82c 100644 --- a/tests/dotnet/Common/Common.h +++ b/tests/dotnet/Common/Common.h @@ -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);