Skip to content

Commit 7fb5e06

Browse files
committed
Fix invalid enum base types being generated in C# source
1 parent 4a3ad65 commit 7fb5e06

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/Generator/Passes/CheckEnumsPass.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using CppSharp.AST;
2+
using CppSharp.Extensions;
23

34
namespace CppSharp.Passes
45
{
56
/// <summary>
6-
/// Checks for enumerations that should be treated as a collection
7-
/// of flags (and annotated with the .NET [Flags] when generated).
7+
/// Validates enumerations and checks if any should be treated as a collection
8+
/// of flags (and annotate them with the .NET [Flags] when generated).
89
/// </summary>
910
public class CheckEnumsPass : TranslationUnitPass
1011
{
@@ -38,15 +39,39 @@ private static bool IsFlagEnum(Enumeration @enum)
3839
return isFlags && hasBigRange;
3940
}
4041

42+
private bool IsValidEnumBaseType(Enumeration @enum)
43+
{
44+
if (Options.IsCSharpGenerator)
45+
return @enum.BuiltinType.Type.IsIntegerType();
46+
47+
return @enum.BuiltinType.Type.IsIntegerType() || @enum.BuiltinType.Type == PrimitiveType.Bool;
48+
}
49+
50+
4151
public override bool VisitEnumDecl(Enumeration @enum)
4252
{
43-
if (IsFlagEnum(@enum))
53+
if (!base.VisitEnumDecl(@enum))
54+
return false;
55+
56+
if (!IsValidEnumBaseType(@enum))
4457
{
45-
@enum.Modifiers |= Enumeration.EnumModifiers.Flags;
46-
return true;
58+
if (@enum.BuiltinType.Type == PrimitiveType.Bool)
59+
{
60+
@enum.BuiltinType = new BuiltinType(PrimitiveType.UChar);
61+
}
62+
else
63+
{
64+
Diagnostics.Warning(
65+
"The enum `{0}` has a base type of `{1}`, which is currently not supported. The base type will be ignored.",
66+
@enum, @enum.BuiltinType);
67+
@enum.BuiltinType = new BuiltinType(PrimitiveType.Int);
68+
}
4769
}
4870

49-
return base.VisitEnumDecl(@enum);
71+
if (IsFlagEnum(@enum))
72+
@enum.Modifiers |= Enumeration.EnumModifiers.Flags;
73+
74+
return true;
5075
}
5176
}
5277
}

0 commit comments

Comments
 (0)