From 31515a9147459d29f871d8dedfdc9a4072a9900d Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 17 Mar 2022 14:53:10 -0400 Subject: [PATCH] AK: Mark the StringView user-defined literal as consteval Even though the StringView(char*, size_t) constructor only runs its overflow check when evaluated in a runtime context, the code generated here could prevent the compiler from optimizing invocations from the StringView user-defined literal (verified on Compiler Explorer). This changes the user-defined literal declaration to be consteval to ensure it is evaluated at compile time. --- AK/StringView.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/AK/StringView.h b/AK/StringView.h index 783a50ff480..821b706b902 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -301,7 +301,15 @@ struct CaseInsensitiveStringViewTraits : public Traits { } -[[nodiscard]] ALWAYS_INLINE constexpr AK::StringView operator"" sv(const char* cstring, size_t length) +// FIXME: Remove this when clang fully supports consteval (specifically in the context of default parameter initialization). +// See: https://stackoverflow.com/questions/68789984/immediate-function-as-default-function-argument-initializer-in-clang +#if defined(__clang__) +# define AK_STRING_VIEW_LITERAL_CONSTEVAL constexpr +#else +# define AK_STRING_VIEW_LITERAL_CONSTEVAL consteval +#endif + +[[nodiscard]] ALWAYS_INLINE AK_STRING_VIEW_LITERAL_CONSTEVAL AK::StringView operator"" sv(const char* cstring, size_t length) { return AK::StringView(cstring, length); }