Skip to content
The Chat Archive
gpt-4o-mini-2024-07-18· OpenAI WildChat-4.8M (AI2)

Hi, i have a compile problem with Clang 20.1 ! Older versionof Clang can compile the…

  1. User

    Hi, i have a compile problem with Clang 20.1 ! Older versionof Clang can compile the file 3rdparty\bgfx\3rdparty\dear-imgui\imgui.h. See here https://raw.githubusercontent.com/mamedev/mame/refs/heads/master/3rdparty/bgfx/3rdparty/dear-imgui/imgui.cpp

    The compile error is this here:

    Clang 20.1.0 detected Compiling 3rdparty/bgfx/3rdparty/dear-imgui/imgui.cpp... In file included from ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.cpp:856: ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.h:2451:43: error: first argument in call to 'memset' is a pointer to non-trivially copyable type 'ImDrawListSplitter' [-Werror,-Wnontrivial-memcall] 2451 | inline ImDrawListSplitter() { memset(this, 0, sizeof(this)); } | ^ ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.h:2451:43: note: explicitly cast the pointer to silence this warning 2451 | inline ImDrawListSplitter() { memset(this, 0, sizeof(this)); } | ^ | (void) ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.h:2522:60: error: first argument in call to 'memset' is a pointer to non-trivially copyable type 'ImDrawList' [-Werror,-Wnontrivial-memcall] 2522 | ImDrawList(ImDrawListSharedData shared_data) { memset(this, 0, sizeof(this)); _Data = shared_data; } | ^ ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.h:2522:60: note: explicitly cast the pointer to silence this warning 2522 | ImDrawList(ImDrawListSharedData shared_data) { memset(this, 0, sizeof(this)); _Data = shared_data; } | ^ | (void)


  2. Assistant

    The warning you are encountering in Clang 20.1 is due to the use of memset on classes or structs that are defined as non-trivially copyable. In C++, a non-trivially copyable class typically has non-POD (Plain Old Data) members, such as non-trivial constructors, destructors, or other user-defined types.

    Here's how you can address this issue:

    1. Avoid Using memset: Instead of using memset to zero-initialize your objects, you can initialize the members explicitly or use an initializer list.

    2. Use Default Member Initializers: If the members can be default initialized, you can initialize them directly in the class definition.

    3. Use std::fill or Other Initialization Methods: If your class has members that require specific initialization, do not use memset.

    Here is an example of how you might modify the constructors of ImDrawListSplitter and ImDrawList in imgui.h:

    Original Code

    inline ImDrawListSplitter()  { memset(this, 0, sizeof(*this)); }
    ImDrawList(ImDrawListSharedData* shared_data) { memset(this, 0, sizeof(*this)); _Data = shared_data; }
    

    Modified Code

    You can replace the memset calls with value initialization:

    inline ImDrawListSplitter() : SomeMember{0}, AnotherMember{0} { } // Initialize each member
    ImDrawList(ImDrawListSharedData* shared_data) : _Data(shared_data), SomeOtherMember{0} { } // Initialize members directly
    

    General Example

    Suppose ImDrawListSplitter looks like this:

    struct ImDrawListSplitter {
        int Count;
        // Other members...
    
        inline ImDrawListSplitter() : Count(0) { /* initialize other members */ }
    };
    

    Replace all instances of memset(this, 0, sizeof(*this)); with proper member initialization.

    Conclusion

    This change not only resolves the warning with memset, but it also aligns better with the C++ standards regarding object initialization, ensuring that constructors or destructors are invoked properly.

    Make sure to apply similar changes throughout your code wherever you are initializing non-trivially copyable types with memset.

  3. User

    Thx1

  4. Assistant

    You're welcome! If you have any more questions or need further assistance, feel free to ask. Happy coding!