Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better support for function-like casts #196

Merged
merged 1 commit into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2913,22 +2913,16 @@ bool IsConstant(Expr initExpr)
// case CX_StmtClass.CX_StmtClass_BuiltinBitCastExpr:

case CX_StmtClass.CX_StmtClass_CStyleCastExpr:
case CX_StmtClass.CX_StmtClass_CXXStaticCastExpr:
case CX_StmtClass.CX_StmtClass_CXXFunctionalCastExpr:
{
var cStyleCastExpr = (CStyleCastExpr)initExpr;
return IsConstant(cStyleCastExpr.SubExprAsWritten);
var cxxFunctionalCastExpr = (ExplicitCastExpr)initExpr;
return IsConstant(cxxFunctionalCastExpr.SubExprAsWritten);
}

// case CX_StmtClass.CX_StmtClass_CXXFunctionalCastExpr:
// case CX_StmtClass.CX_StmtClass_CXXConstCastExpr:
// case CX_StmtClass.CX_StmtClass_CXXDynamicCastExpr:
// case CX_StmtClass.CX_StmtClass_CXXReinterpretCastExpr:

case CX_StmtClass.CX_StmtClass_CXXStaticCastExpr:
{
var cxxStaticCastExpr = (CXXStaticCastExpr)initExpr;
return IsConstant(cxxStaticCastExpr.SubExprAsWritten);
}

// case CX_StmtClass.CX_StmtClass_ObjCBridgedCastExpr:

case CX_StmtClass.CX_StmtClass_ImplicitCastExpr:
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2617,6 +2617,7 @@ private bool IsUnchecked(string targetTypeName, Stmt stmt)

case CX_StmtClass.CX_StmtClass_CStyleCastExpr:
case CX_StmtClass.CX_StmtClass_CXXStaticCastExpr:
case CX_StmtClass.CX_StmtClass_CXXFunctionalCastExpr:
{
var explicitCastExpr = (ExplicitCastExpr)stmt;
var explicitCastExprTypeName = GetRemappedTypeName(explicitCastExpr, context: null, explicitCastExpr.Type, out _);
Expand All @@ -2626,7 +2627,6 @@ private bool IsUnchecked(string targetTypeName, Stmt stmt)
|| (IsUnsigned(targetTypeName) != IsUnsigned(explicitCastExprTypeName));
}

// case CX_StmtClass.CX_StmtClass_CXXFunctionalCastExpr:
// case CX_StmtClass.CX_StmtClass_CXXConstCastExpr:
// case CX_StmtClass.CX_StmtClass_CXXDynamicCastExpr:
// case CX_StmtClass.CX_StmtClass_CXXReinterpretCastExpr:
Expand Down
20 changes: 20 additions & 0 deletions tests/ClangSharp.PInvokeGenerator.UnitTests/VarDeclarationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,26 @@ public static partial class Methods
await ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents);
}


[Fact]
public async Task UncheckedFunctionLikeCastMacroTest()
{
var inputContents = $@"#define MyMacro1 unsigned(-1)";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public static partial class Methods
{{
[NativeTypeName(""#define MyMacro1 unsigned(-1)"")]
public const uint MyMacro1 = unchecked((uint)(-1));
}}
}}
";

await ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents);
}


[Fact]
public async Task UncheckedConversionMacroTest2()
{
Expand Down