Skip to content

Broken code generation for structs with arrays #1120

Open
@Alovchin91

Description

@Alovchin91
Brief Description

CppSharp generates incompilable code for structs with arrays of some types. Please see the used test header.

OS: Windows 10 RS4 x64

Used headers
struct test_struct_t
{
	const char* strings_array[1];
	const char **string_pointers_array;
	struct { int dummy; } structs_array[1];
};
Used settings

Target: MSVC (I think)

public void Setup(Driver driver)
{
    driver.Options.GeneratorKind = GeneratorKind.CSharp;
    var module = driver.Options.AddModule("TestModule");
    module.IncludeDirs.Add(Environment.CurrentDirectory);
    module.Headers.Add("TestHeader.h");
}
Stack trace or incompilable generated code
  1. const char* strings_array[1]; generates this code:

    private bool __strings_arrayInitialised;
    public string[] StringsArray
    {
        get
        {
            if (!__strings_arrayInitialised)
            {
                __strings_array = null;
                __strings_arrayInitialised = true;
            }
            return __strings_array;
        }
    
        set
        {
            __strings_array = value;
            if (!__strings_arrayInitialised)
            {
                __strings_arrayInitialised = true;
            }
        }
    }

    The problem with this code is that it has nothing to do with the actual strings_array in the native struct, so it's basically useless.

  2. const char **string_pointers_array; generates this code:

    public sbyte** StringPointersArray
    {
        get
        {
            return (string*) ((global::TestModule.TestStructT.__Internal*) __Instance)->string_pointers_array;
        }
    
        set
        {
            ((global::TestModule.TestStructT.__Internal*)__Instance)->string_pointers_array = (global::System.IntPtr) value;
        }
    }

    This one is almost correct, except that it tries to cast the result to string* in the getter, though it should be cast to sbyte**.

  3. struct { int dummy; } structs_array[1]; generates this code:

    public global::TestModule.TestStructT._[] StructsArray
    {
        get
        {
            global::TestModule.TestStructT._[] __value = null;
            ...
        }
    
        set
        {
            ...
        }
    }

    The code is correct here, but unlike the case without an array, it does not generate an anonymous struct TestStructT._. Thus this code ends up saying that there is no such type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions