renatoGarcia / icecream-cpp

🍦 Never use cout/printf to debug again

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clang dump struct type info limitation

renatoGarcia opened this issue · comments

Due to limited information provided by Clang when using __builtin_dump_struct function, some struct members types can not be printed. Enums, none of them can be printed. With arrays, only the ones with elements of fundamental types, fixed width integers, std::size_t, std::ptrdiff_t, and any pointer, declared using canonical names (i.e.: not type aliases), can be printed.

When using the __builtin_dump_struct function, both enums and arrays are dumped to some_printf_func using the %p specifier, regardless of its elements type. With only that information would not be possible recover and print neither the enum values nor any array element.

However, preceding each call to some_printf_func with a %p specifier by __builtin_dump_struct, a call to that same function will be done having as argument a string with the member signature (type and name). To enums that is not of any help, but to arrays, inspecting that string is possible deduce the array size and its elements types.

Knowing that information, an array dumped with a preceding "short [50] bla" string can be deduced as being an array of 50 short int. That works identically for any fundamental type, but is not possible infer the array elements type if an alias is used. With the code:

using Integer = int;

struct Point
{
    double x;
    double y;
};

struct S
{
    Integer integers[10];
    Point coords[5];
};

there is no way to know what fundamental type Integer is an alias to, neither what are the internal members of Point struct. Consequently, both integers and coords members can not be printed.

To enums, its values are represented using an integral type defined at enum declaration, like:

enum EN : std::uint64_t
{
    UM,
    DOIS,
    TRES
};

However, inside of some_printf_func it's not possible to get that information. Therefore, not kowning the bit size of each enum value it's not possible print them.