Fixed anonymous type generation in C mode.

This commit is contained in:
Summpot 2023-08-09 16:33:48 +08:00
Родитель 641a0d225a
Коммит 5f9d7a955b
2 изменённых файлов: 61 добавлений и 2 удалений

Просмотреть файл

@ -2514,6 +2514,20 @@ public sealed partial class PInvokeGenerator : IDisposable
{
name = namedDecl.Name.NormalizePath();
// strip the prefix
if (name.StartsWith("enum "))
{
name = name[5..];
}
else if (name.StartsWith("struct "))
{
name = name[7..];
}
else if (name.StartsWith("union "))
{
name = name[6..];
}
if (namedDecl is CXXConstructorDecl cxxConstructorDecl)
{
var parent = cxxConstructorDecl.Parent;
@ -2534,7 +2548,8 @@ public sealed partial class PInvokeGenerator : IDisposable
name.StartsWith("(anonymous union at ") ||
name.StartsWith("(unnamed enum at ") ||
name.StartsWith("(unnamed struct at ") ||
name.StartsWith("(unnamed union at "));
name.StartsWith("(unnamed union at ") ||
name.StartsWith("(unnamed at "));
Debug.Assert(name.EndsWith(')'));
}
#endif

Просмотреть файл

@ -79,8 +79,25 @@ typedef struct _MyOtherStruct
MyStruct _field1;
MyStruct* _field2;
} MyOtherStruct;
typedef struct _MyStructWithAnonymousStruct
{
struct {
int _field;
} _anonymousStructField1;
} MyStructWithAnonymousStruct;
typedef struct _MyStructWithAnonymousUnion
{
union {
int _field1;
int* _field2;
} union1;
} MyStructWithAnonymousUnion;
";
var expectedOutputContents = @"namespace ClangSharp.Test
var expectedOutputContents = @"using System.Runtime.InteropServices;
namespace ClangSharp.Test
{
public partial struct _MyStruct
{
@ -95,6 +112,33 @@ typedef struct _MyOtherStruct
[NativeTypeName(""MyStruct *"")]
public _MyStruct* _field2;
}
public partial struct _MyStructWithAnonymousStruct
{
[NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L14_C5"")]
public __anonymousStructField1_e__Struct _anonymousStructField1;
public partial struct __anonymousStructField1_e__Struct
{
public int _field;
}
}
public partial struct _MyStructWithAnonymousUnion
{
[NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L21_C5"")]
public _union1_e__Union union1;
[StructLayout(LayoutKind.Explicit)]
public unsafe partial struct _union1_e__Union
{
[FieldOffset(0)]
public int _field1;
[FieldOffset(0)]
public int* _field2;
}
}
}
";
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, commandlineArgs: DefaultCClangCommandLineArgs, language: "c", languageStandard: DefaultCStandard);