From 3c705edffcdcc650105427aeecd128e0903e48d7 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Sun, 26 Mar 2023 16:28:32 -0700 Subject: [PATCH] #940 Fix component registration issue with binaries compiled by different compilers --- flecs.c | 29 +++++++++++++++---------- flecs.h | 12 ++++++---- include/flecs/addons/cpp/component.hpp | 3 ++- include/flecs/addons/cpp/utils/enum.hpp | 3 ++- include/flecs/addons/flecs_cpp.h | 6 +++-- src/addons/flecs_cpp.c | 29 +++++++++++++++---------- 6 files changed, 52 insertions(+), 30 deletions(-) diff --git a/flecs.c b/flecs.c index 98a2eaab76..ccafbc7c82 100644 --- a/flecs.c +++ b/flecs.c @@ -18602,9 +18602,10 @@ void ecs_cpp_trim_type_name( char* ecs_cpp_get_type_name( char *type_name, const char *func_name, - size_t len) + size_t len, + size_t front_len) { - memcpy(type_name, func_name + ECS_FUNC_NAME_FRONT(const char*, type_name), len); + memcpy(type_name, func_name + front_len, len); type_name[len] = '\0'; ecs_cpp_trim_type_name(type_name); return type_name; @@ -18635,20 +18636,21 @@ char* ecs_cpp_get_symbol_name( } static -const char* cpp_func_rchr( +const char* flecs_cpp_func_rchr( const char *func_name, ecs_size_t func_name_len, + ecs_size_t func_back_len, char ch) { const char *r = strrchr(func_name, ch); - if ((r - func_name) >= (func_name_len - flecs_uto(ecs_size_t, ECS_FUNC_NAME_BACK))) { + if ((r - func_name) >= (func_name_len - flecs_uto(ecs_size_t, func_back_len))) { return NULL; } return r; } static -const char* cpp_func_max( +const char* flecs_cpp_func_max( const char *a, const char *b) { @@ -18659,18 +18661,23 @@ const char* cpp_func_max( char* ecs_cpp_get_constant_name( char *constant_name, const char *func_name, - size_t func_name_len) + size_t func_name_len, + size_t func_back_len) { ecs_size_t f_len = flecs_uto(ecs_size_t, func_name_len); - const char *start = cpp_func_rchr(func_name, f_len, ' '); - start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ')')); - start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ':')); - start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ',')); + ecs_size_t fb_len = flecs_uto(ecs_size_t, func_back_len); + const char *start = flecs_cpp_func_rchr(func_name, f_len, fb_len, ' '); + start = flecs_cpp_func_max(start, flecs_cpp_func_rchr( + func_name, f_len, fb_len, ')')); + start = flecs_cpp_func_max(start, flecs_cpp_func_rchr( + func_name, f_len, fb_len, ':')); + start = flecs_cpp_func_max(start, flecs_cpp_func_rchr( + func_name, f_len, fb_len, ',')); ecs_assert(start != NULL, ECS_INVALID_PARAMETER, func_name); start ++; ecs_size_t len = flecs_uto(ecs_size_t, - (f_len - (start - func_name) - flecs_uto(ecs_size_t, ECS_FUNC_NAME_BACK))); + (f_len - (start - func_name) - fb_len)); ecs_os_memcpy_n(constant_name, start, char, len); constant_name[len] = '\0'; return constant_name; diff --git a/flecs.h b/flecs.h index 103db03552..414c7dc747 100644 --- a/flecs.h +++ b/flecs.h @@ -14449,7 +14449,8 @@ FLECS_API char* ecs_cpp_get_type_name( char *type_name, const char *func_name, - size_t len); + size_t len, + size_t front_len); FLECS_API char* ecs_cpp_get_symbol_name( @@ -14461,7 +14462,8 @@ FLECS_API char* ecs_cpp_get_constant_name( char *constant_name, const char *func_name, - size_t len); + size_t len, + size_t back_len); FLECS_API const char* ecs_cpp_trim_module( @@ -15257,7 +15259,8 @@ static const char* enum_constant_to_name() { static const size_t len = ECS_FUNC_TYPE_LEN(const char*, enum_constant_to_name, ECS_FUNC_NAME); static char result[len + 1] = {}; return ecs_cpp_get_constant_name( - result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME)); + result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME), + ECS_FUNC_NAME_BACK); } /** Enumeration constant data */ @@ -23282,7 +23285,8 @@ template inline static const char* type_name() { static const size_t len = ECS_FUNC_TYPE_LEN(const char*, type_name, ECS_FUNC_NAME); static char result[len + 1] = {}; - return ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len); + static const size_t front_len = ECS_FUNC_NAME_FRONT(const char*, type_name); + return ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len, front_len); } #else #error "implicit component registration not supported" diff --git a/include/flecs/addons/cpp/component.hpp b/include/flecs/addons/cpp/component.hpp index 79da00ba8b..246d6da780 100644 --- a/include/flecs/addons/cpp/component.hpp +++ b/include/flecs/addons/cpp/component.hpp @@ -33,7 +33,8 @@ template inline static const char* type_name() { static const size_t len = ECS_FUNC_TYPE_LEN(const char*, type_name, ECS_FUNC_NAME); static char result[len + 1] = {}; - return ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len); + static const size_t front_len = ECS_FUNC_NAME_FRONT(const char*, type_name); + return ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len, front_len); } #else #error "implicit component registration not supported" diff --git a/include/flecs/addons/cpp/utils/enum.hpp b/include/flecs/addons/cpp/utils/enum.hpp index 2aef4c9e3d..fea9037b16 100644 --- a/include/flecs/addons/cpp/utils/enum.hpp +++ b/include/flecs/addons/cpp/utils/enum.hpp @@ -131,7 +131,8 @@ static const char* enum_constant_to_name() { static const size_t len = ECS_FUNC_TYPE_LEN(const char*, enum_constant_to_name, ECS_FUNC_NAME); static char result[len + 1] = {}; return ecs_cpp_get_constant_name( - result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME)); + result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME), + ECS_FUNC_NAME_BACK); } /** Enumeration constant data */ diff --git a/include/flecs/addons/flecs_cpp.h b/include/flecs/addons/flecs_cpp.h index 4ceeffdd10..c44bfc559a 100644 --- a/include/flecs/addons/flecs_cpp.h +++ b/include/flecs/addons/flecs_cpp.h @@ -39,7 +39,8 @@ FLECS_API char* ecs_cpp_get_type_name( char *type_name, const char *func_name, - size_t len); + size_t len, + size_t front_len); FLECS_API char* ecs_cpp_get_symbol_name( @@ -51,7 +52,8 @@ FLECS_API char* ecs_cpp_get_constant_name( char *constant_name, const char *func_name, - size_t len); + size_t len, + size_t back_len); FLECS_API const char* ecs_cpp_trim_module( diff --git a/src/addons/flecs_cpp.c b/src/addons/flecs_cpp.c index 43ac6576fd..144b8535af 100644 --- a/src/addons/flecs_cpp.c +++ b/src/addons/flecs_cpp.c @@ -84,9 +84,10 @@ void ecs_cpp_trim_type_name( char* ecs_cpp_get_type_name( char *type_name, const char *func_name, - size_t len) + size_t len, + size_t front_len) { - memcpy(type_name, func_name + ECS_FUNC_NAME_FRONT(const char*, type_name), len); + memcpy(type_name, func_name + front_len, len); type_name[len] = '\0'; ecs_cpp_trim_type_name(type_name); return type_name; @@ -117,20 +118,21 @@ char* ecs_cpp_get_symbol_name( } static -const char* cpp_func_rchr( +const char* flecs_cpp_func_rchr( const char *func_name, ecs_size_t func_name_len, + ecs_size_t func_back_len, char ch) { const char *r = strrchr(func_name, ch); - if ((r - func_name) >= (func_name_len - flecs_uto(ecs_size_t, ECS_FUNC_NAME_BACK))) { + if ((r - func_name) >= (func_name_len - flecs_uto(ecs_size_t, func_back_len))) { return NULL; } return r; } static -const char* cpp_func_max( +const char* flecs_cpp_func_max( const char *a, const char *b) { @@ -141,18 +143,23 @@ const char* cpp_func_max( char* ecs_cpp_get_constant_name( char *constant_name, const char *func_name, - size_t func_name_len) + size_t func_name_len, + size_t func_back_len) { ecs_size_t f_len = flecs_uto(ecs_size_t, func_name_len); - const char *start = cpp_func_rchr(func_name, f_len, ' '); - start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ')')); - start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ':')); - start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ',')); + ecs_size_t fb_len = flecs_uto(ecs_size_t, func_back_len); + const char *start = flecs_cpp_func_rchr(func_name, f_len, fb_len, ' '); + start = flecs_cpp_func_max(start, flecs_cpp_func_rchr( + func_name, f_len, fb_len, ')')); + start = flecs_cpp_func_max(start, flecs_cpp_func_rchr( + func_name, f_len, fb_len, ':')); + start = flecs_cpp_func_max(start, flecs_cpp_func_rchr( + func_name, f_len, fb_len, ',')); ecs_assert(start != NULL, ECS_INVALID_PARAMETER, func_name); start ++; ecs_size_t len = flecs_uto(ecs_size_t, - (f_len - (start - func_name) - flecs_uto(ecs_size_t, ECS_FUNC_NAME_BACK))); + (f_len - (start - func_name) - fb_len)); ecs_os_memcpy_n(constant_name, start, char, len); constant_name[len] = '\0'; return constant_name;