From 52f1ef84c7c9fb8801bb02e361e2b337253bcbd6 Mon Sep 17 00:00:00 2001 From: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com> Date: Wed, 26 Jan 2022 14:20:01 -0600 Subject: [PATCH] Fixed string_view compilation in GCC 10+. (#7153) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed string_view compilation in GCC 10+. Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com> * More GCC 10+ Fixes. GCC 11 seems to have an issue with linkage regarding using a lambda as a default parameter in a function declaration. Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com> * GCC10+ Fix - Fixed binding to a temporary references. > error: loop variable ‘pathName’ of type ‘const QString&’ binds to a temporary constructed from type ‘const char* const’ [-Werror=range-loop-construct] 415 | for (const QString& pathName : { "CrySystem", Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com> --- .../AzCore/AzCore/std/string/string_view.h | 164 ++++++++++-------- .../native/utilities/ApplicationManager.cpp | 2 +- .../Include/Atom/RHI/ThreadLocalContext.h | 8 +- 3 files changed, 100 insertions(+), 74 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/std/string/string_view.h b/Code/Framework/AzCore/AzCore/std/string/string_view.h index c333e2cea1..daa00d98d3 100644 --- a/Code/Framework/AzCore/AzCore/std/string/string_view.h +++ b/Code/Framework/AzCore/AzCore/std/string/string_view.h @@ -309,78 +309,87 @@ namespace AZStd } static constexpr bool eq(char_type left, char_type right) noexcept { return left == right; } static constexpr bool lt(char_type left, char_type right) noexcept { return left < right; } - static constexpr int compare(const char_type* s1, const char_type* s2, size_t count) noexcept - { - // In GCC versions prior to major version 10, __builtin_memcmp fails in valid checks in constexpr evaluation -#if !defined(AZ_COMPILER_GCC) || AZ_COMPILER_GCC >= 100000 - if constexpr (AZStd::is_same_v) - { - return __builtin_memcmp(s1, s2, count); - } - else if constexpr (AZStd::is_same_v) - { - return __builtin_wmemcmp(s1, s2, count); - } else -#endif - { - if (az_builtin_is_constant_evaluated()) - { - for (; count; --count, ++s1, ++s2) + static constexpr int compare(const char_type* s1, const char_type* s2, size_t count) noexcept + { + // In GCC versions , __builtin_memcmp fails in valid checks in constexpr evaluation +#if !defined(AZ_COMPILER_GCC) + if constexpr (AZStd::is_same_v) + { + return __builtin_memcmp(s1, s2, count); + } + else if constexpr (AZStd::is_same_v) + { + return __builtin_wmemcmp(s1, s2, count); + } + else +#endif + { + if (az_builtin_is_constant_evaluated()) + { + for (; count; --count, ++s1, ++s2) { - if (lt(*s1, *s2)) - { - return -1; - } - else if (lt(*s2, *s1)) - { - return 1; - } - } - return 0; - } - else - { - return ::memcmp(s1, s2, count * sizeof(char_type)); - } - } + if (lt(*s1, *s2)) + { + return -1; + } + else if (lt(*s2, *s1)) + { + return 1; + } + } + return 0; + } + else + { + return ::memcmp(s1, s2, count * sizeof(char_type)); + } + } } static constexpr size_t length(const char_type* s) noexcept { // For GCC versions less than 10, __builtin_strlen and __builtin_wcslen is not supported as const expressions // so for that case it will need to manually count the characters (at compile time) instead -#if defined(AZ_COMPILER_GCC) && AZ_COMPILER_GCC < 100000 if constexpr (AZStd::is_same_v) { +#if defined(AZ_COMPILER_GCC) && AZ_COMPILER_GCC < 100000 if (!az_builtin_is_constant_evaluated()) { return strlen(s); } + else + { + size_t strLength{}; + for (; *s; ++s, ++strLength) + { + ; + } + return strLength; + } +#else + return __builtin_strlen(s); +#endif } else if constexpr (AZStd::is_same_v) { +#if defined(AZ_COMPILER_GCC) if (!az_builtin_is_constant_evaluated()) { return wcslen(s); } - } - - size_t strLength{}; - for (; *s; ++s, ++strLength) - { - ; - } - return strLength; + else + { + size_t strLength{}; + for (; *s; ++s, ++strLength) + { + ; + } + return strLength; + } #else - - if constexpr (AZStd::is_same_v) - { - return __builtin_strlen(s); - } - else if constexpr (AZStd::is_same_v) - { return __builtin_wcslen(s); +#endif } else { @@ -391,46 +400,59 @@ namespace AZStd } return strLength; } -#endif // defined(AZ_COMPILER_GCC) && AZ_COMPILER_GCC < 100000 + } static constexpr const char_type* find(const char_type* s, size_t count, const char_type& ch) noexcept { - // For GCC versions less than 10, __builtin_char_memchr and __builtin_wmemchr is not supported, and - // __builtin_memchr is not supported as const expressions. In those cases we will manually locate and + // For GCC versions less than 10, __builtin_char_memchr and __builtin_wmemchr is not supported, and + // __builtin_memchr is not supported as const expressions. In those cases we will manually locate and // return the pointer to 's' (at compile time) -#if defined(AZ_COMPILER_GCC) && AZ_COMPILER_GCC < 100000 if constexpr (AZStd::is_same_v) { +#if defined(AZ_COMPILER_GCC) if (!az_builtin_is_constant_evaluated()) { return static_cast(__builtin_memchr(s, ch, count)); } + else + { + for (; count; --count, ++s) + { + if (eq(*s, ch)) + { + return s; + } + } + + return nullptr; + } +#else + return __builtin_char_memchr(s, ch, count); +#endif // defined(AZ_COMPILER_GCC)AZ_COMPILER_GCC < 100000 } else if constexpr (AZStd::is_same_v) { +#if defined(AZ_COMPILER_GCC) if (!az_builtin_is_constant_evaluated()) { return wmemchr(s, ch, count); } - } - - for (; count; --count, ++s) - { - if (eq(*s, ch)) + else { - return s; + for (; count; --count, ++s) + { + if (eq(*s, ch)) + { + return s; + } + } + + return nullptr; } - } - return nullptr; #else - if constexpr (AZStd::is_same_v) - { - return __builtin_char_memchr(s, ch, count); - } - else if constexpr (AZStd::is_same_v) - { return __builtin_wmemchr(s, ch, count); +#endif } else { @@ -441,9 +463,9 @@ namespace AZStd return s; } } + return nullptr; } -#endif } static constexpr char_type* move(char_type* dest, const char_type* src, size_t count) noexcept { @@ -453,7 +475,7 @@ namespace AZStd return dest; } - #if az_has_builtin_memmove + #if !defined(AZ_COMPILER_GCC) && az_has_builtin_memmove __builtin_memmove(dest, src, count * sizeof(char_type)); #else auto NonBuiltinMove = [](char_type* dest1, const char_type* src1, size_t count1) constexpr @@ -506,7 +528,7 @@ namespace AZStd } static constexpr char_type* copy(char_type* dest, const char_type* src, size_t count) noexcept { - #if az_has_builtin_memcpy + #if !defined(AZ_COMPILER_GCC) && az_has_builtin_memcpy __builtin_memcpy(dest, src, count * sizeof(char_type)); #else auto NonBuiltinCopy = [](char_type* dest1, const char_type* src1, size_t count1) constexpr @@ -536,7 +558,7 @@ namespace AZStd static constexpr char_type* copy_backward(char_type* dest, const char_type* src, size_t count) noexcept { char_type* result = dest; - #if az_has_builtin_memmove + #if !defined(AZ_COMPILER_GCC) && az_has_builtin_memmove __builtin_memmove(dest, src, count * sizeof(char_type)); #else if (az_builtin_is_constant_evaluated()) diff --git a/Code/Tools/AssetProcessor/native/utilities/ApplicationManager.cpp b/Code/Tools/AssetProcessor/native/utilities/ApplicationManager.cpp index 57884b304f..06536921d9 100644 --- a/Code/Tools/AssetProcessor/native/utilities/ApplicationManager.cpp +++ b/Code/Tools/AssetProcessor/native/utilities/ApplicationManager.cpp @@ -412,7 +412,7 @@ void ApplicationManager::PopulateApplicationDependencies() // Note that its not necessary for any of these files to actually exist. It is considered a "change" if they // change their file modtime, or if they go from existing to not existing, or if they go from not existing, to existing. // any of those should cause AP to drop. - for (const QString& pathName : { "CrySystem", + for (QString pathName : { "CrySystem", "SceneCore", "SceneData", "SceneBuilder", "AzQtComponents" }) diff --git a/Gems/Atom/RHI/Code/Include/Atom/RHI/ThreadLocalContext.h b/Gems/Atom/RHI/Code/Include/Atom/RHI/ThreadLocalContext.h index 4d3534b845..d45c7bf93e 100644 --- a/Gems/Atom/RHI/Code/Include/Atom/RHI/ThreadLocalContext.h +++ b/Gems/Atom/RHI/Code/Include/Atom/RHI/ThreadLocalContext.h @@ -19,7 +19,7 @@ namespace AZ { /** * This class is a container of thread local storage. It allows for multiple instances - * of thread local storage to exist simultaneously (a property not possible with the + * of thread local storage to exist simultaneously (a property not possible with the * thread_local modifier, which is really a thread global). The context tracks AZ thread * lifetime through a bus in order to clean up storage for exiting threads. The context * allows thread-safe iteration of all thread contexts, which is also a property not possible @@ -36,7 +36,11 @@ namespace AZ public: using InitFunction = AZStd::function; - ThreadLocalContext(InitFunction initFunction = [] (Storage&) {}); + static void DefaultFunction(Storage&) + { + } + + ThreadLocalContext(InitFunction initFunction = &DefaultFunction); ~ThreadLocalContext(); // No copying or moving allowed.