Skip to content

Commit

Permalink
Merge pull request #467 from Summpot/main
Browse files Browse the repository at this point in the history
Fixed anonymous type generation in C mode.
  • Loading branch information
johnfrench3 committed Aug 22, 2023
2 parents eb044fe + be04062 commit a45dbe8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
17 changes: 16 additions & 1 deletion sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2514,6 +2514,20 @@ private string GetCursorName(NamedDecl namedDecl)
{
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;
Expand All @@ -2534,7 +2548,8 @@ private string GetCursorName(NamedDecl namedDecl)
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
Expand Down
46 changes: 45 additions & 1 deletion tests/ClangSharp.PInvokeGenerator.UnitTests/CTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,25 @@ public Task StructTest()
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
{
Expand All @@ -95,6 +112,33 @@ public unsafe partial 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);
Expand Down

0 comments on commit a45dbe8

Please sign in to comment.