libstdc++: Redefine __from_chars_alnum_to_val's table
Checks
Commit Message
It looks like the constexpr <charconv> commit r13-3313-g378a0f1840e694
caused some modules regressions:
FAIL: g++.dg/modules/xtreme-header-4_b.C -std=c++2b (test for excess errors)
FAIL: g++.dg/modules/xtreme-header_b.C -std=c++2b (test for excess errors)
Like PR105297, the problem seems to be the local class from
__from_chars_alnum_to_val ending up as the type of a namespace-scope
entity (the variable template __detail::__table in this case).
This patch works around this modules issue by using an ordinary class
instead of a local class. Also, I suppose we might as well use a static
data member to define the table once for all dialects instead of having
to define it twice in C++23 mode, once as a static local variable (which
isn't usable during constexpr evaluation) and again as a variable template
(which is).
Tested on x86_64-pc-linux-gnu, does this look OK for trunk? Diff
generated with -w to ignore noisy whitespace changes.
libstdc++-v3/ChangeLog:
* include/std/charconv (__detail::__from_chars_alnum_to_val_table):
Redefine as a class template containing type, value and _S_table
members. Don't use a local class as the table type.
(__detail::__table): Remove.
(__detail::__from_chars_alnum_to_val): Adjust after the above.
---
libstdc++-v3/include/std/charconv | 31 ++++++++++++++-----------------
1 file changed, 14 insertions(+), 17 deletions(-)
Comments
On Mon, 17 Oct 2022 at 17:27, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> It looks like the constexpr <charconv> commit r13-3313-g378a0f1840e694
> caused some modules regressions:
>
> FAIL: g++.dg/modules/xtreme-header-4_b.C -std=c++2b (test for excess errors)
> FAIL: g++.dg/modules/xtreme-header_b.C -std=c++2b (test for excess errors)
>
> Like PR105297, the problem seems to be the local class from
> __from_chars_alnum_to_val ending up as the type of a namespace-scope
> entity (the variable template __detail::__table in this case).
>
> This patch works around this modules issue by using an ordinary class
> instead of a local class. Also, I suppose we might as well use a static
> data member to define the table once for all dialects instead of having
> to define it twice in C++23 mode, once as a static local variable (which
> isn't usable during constexpr evaluation) and again as a variable template
> (which is).
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk? Diff
> generated with -w to ignore noisy whitespace changes.
OK, thanks.
>
> libstdc++-v3/ChangeLog:
>
> * include/std/charconv (__detail::__from_chars_alnum_to_val_table):
> Redefine as a class template containing type, value and _S_table
> members. Don't use a local class as the table type.
> (__detail::__table): Remove.
> (__detail::__from_chars_alnum_to_val): Adjust after the above.
> ---
> libstdc++-v3/include/std/charconv | 31 ++++++++++++++-----------------
> 1 file changed, 14 insertions(+), 17 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/charconv b/libstdc++-v3/include/std/charconv
> index 7aefdd3298c..c157d4c74ab 100644
> --- a/libstdc++-v3/include/std/charconv
> +++ b/libstdc++-v3/include/std/charconv
> @@ -413,14 +413,19 @@ namespace __detail
> return true;
> }
>
> + template<bool _DecOnly>
> + struct __from_chars_alnum_to_val_table
> + {
> + struct type { unsigned char __data[1u << __CHAR_BIT__] = {}; };
> +
> // Construct and return a lookup table that maps 0-9, A-Z and a-z to their
> // corresponding base-36 value and maps all other characters to 127.
> - constexpr auto
> - __from_chars_alnum_to_val_table()
> + static constexpr type
> + _S_table()
> {
> constexpr unsigned char __lower_letters[27] = "abcdefghijklmnopqrstuvwxyz";
> constexpr unsigned char __upper_letters[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
> - struct { unsigned char __data[1u << __CHAR_BIT__] = {}; } __table;
> + type __table;
> for (auto& __entry : __table.__data)
> __entry = 127;
> for (int __i = 0; __i < 10; ++__i)
> @@ -433,10 +438,11 @@ namespace __detail
> return __table;
> }
>
> -#if __cpp_lib_constexpr_charconv
> - template<bool _DecOnly>
> - inline constexpr auto __table = __from_chars_alnum_to_val_table();
> -#endif
> + // This initializer is made superficially dependent in order
> + // to prevent the compiler from wastefully constructing the
> + // table ahead of time when it's not needed.
> + static constexpr type value = (_DecOnly, _S_table());
> + };
>
> // If _DecOnly is true: if the character is a decimal digit, then
> // return its corresponding base-10 value, otherwise return a value >= 127.
> @@ -449,16 +455,7 @@ namespace __detail
> if _GLIBCXX17_CONSTEXPR (_DecOnly)
> return static_cast<unsigned char>(__c - '0');
> else
> - {
> -#if __cpp_lib_constexpr_charconv
> - if (std::__is_constant_evaluated())
> - return __table<_DecOnly>.__data[__c];
> -#endif
> - // This initializer is deliberately made dependent in order to work
> - // around modules bug PR105322.
> - static constexpr auto __table = (_DecOnly, __from_chars_alnum_to_val_table());
> - return __table.__data[__c];
> - }
> + return __from_chars_alnum_to_val_table<_DecOnly>::value.__data[__c];
> }
>
> /// std::from_chars implementation for integers in a power-of-two base.
> --
> 2.38.0.68.ge85701b4af
>
@@ -413,14 +413,19 @@ namespace __detail
return true;
}
+ template<bool _DecOnly>
+ struct __from_chars_alnum_to_val_table
+ {
+ struct type { unsigned char __data[1u << __CHAR_BIT__] = {}; };
+
// Construct and return a lookup table that maps 0-9, A-Z and a-z to their
// corresponding base-36 value and maps all other characters to 127.
- constexpr auto
- __from_chars_alnum_to_val_table()
+ static constexpr type
+ _S_table()
{
constexpr unsigned char __lower_letters[27] = "abcdefghijklmnopqrstuvwxyz";
constexpr unsigned char __upper_letters[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- struct { unsigned char __data[1u << __CHAR_BIT__] = {}; } __table;
+ type __table;
for (auto& __entry : __table.__data)
__entry = 127;
for (int __i = 0; __i < 10; ++__i)
@@ -433,10 +438,11 @@ namespace __detail
return __table;
}
-#if __cpp_lib_constexpr_charconv
- template<bool _DecOnly>
- inline constexpr auto __table = __from_chars_alnum_to_val_table();
-#endif
+ // This initializer is made superficially dependent in order
+ // to prevent the compiler from wastefully constructing the
+ // table ahead of time when it's not needed.
+ static constexpr type value = (_DecOnly, _S_table());
+ };
// If _DecOnly is true: if the character is a decimal digit, then
// return its corresponding base-10 value, otherwise return a value >= 127.
@@ -449,16 +455,7 @@ namespace __detail
if _GLIBCXX17_CONSTEXPR (_DecOnly)
return static_cast<unsigned char>(__c - '0');
else
- {
-#if __cpp_lib_constexpr_charconv
- if (std::__is_constant_evaluated())
- return __table<_DecOnly>.__data[__c];
-#endif
- // This initializer is deliberately made dependent in order to work
- // around modules bug PR105322.
- static constexpr auto __table = (_DecOnly, __from_chars_alnum_to_val_table());
- return __table.__data[__c];
- }
+ return __from_chars_alnum_to_val_table<_DecOnly>::value.__data[__c];
}
/// std::from_chars implementation for integers in a power-of-two base.