public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108269] New: Incorrect __PRETTY_FUNCTION__ with std::array<char> in C++20 NTTP literal
@ 2023-01-02 21:43 camden.mannett at gmail dot com
  2023-01-03  1:16 ` [Bug c++/108269] __PRETTY_FUNCTION__ for NTTP literal intiailization containing a char array is assumed to ending in a null character pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: camden.mannett at gmail dot com @ 2023-01-02 21:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108269

            Bug ID: 108269
           Summary: Incorrect __PRETTY_FUNCTION__ with std::array<char> in
                    C++20 NTTP literal
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: camden.mannett at gmail dot com
  Target Milestone: ---

Created attachment 54176
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54176&action=edit
Source code

When std::array<char, N> is used within a literal type that is used as an NTTP,
it _looks_ like the __PRETTY_FUNCTION__ output always assumes that it is a
null-terminated string which is then displayed without the last character - but
it is not always a null!

It is simplest to demonstrate with a compile-time string implementation
(https://godbolt.org/z/8EnPfeWq7):

#include <algorithm>
#include <array>
#include <iostream>

namespace {
template<typename T>
class type_string_t
{
    [[nodiscard]] constexpr static std::string_view generate() noexcept
    {
        constexpr auto sig =
          std::string_view{ static_cast<const char*>(__PRETTY_FUNCTION__) };
        return sig;
    }

  public:
    constexpr static auto value = generate();
};

template<typename T>
constexpr auto type_string = type_string_t<T>::value;

template<std::size_t N>
class str_storage
{
  public:
    constexpr str_storage(const char (&str)[N])
    {
        std::copy_n(&str[0], N, value.begin());
    }

    constexpr str_storage(char c)
      : value{ c }
    {
    }

    std::array<char, N> value;
};
str_storage(char) -> str_storage<1>;

template<str_storage S>
struct str
{};
}

int
main()
{
    std::cout << type_string<str<'a'>> << "\n"
              << type_string<str<"a">> << "\n"
              << type_string<str<"hello">> << std::endl;
    return 0;
}

This yields pretty much what you would expect when built via Clang (after
removal of cruft):

(anonymous namespace)::str<{{{97}}}>
(anonymous namespace)::str<{{"a"}}>
(anonymous namespace)::str<{{"hello"}}>

However gcc does something different when the std::array contains only a single
char:

{anonymous}::str<{anonymous}::str_storage<1>{std::array<char, 1>{""}}>
{anonymous}::str<{anonymous}::str_storage<2>{std::array<char, 2>{"a"}}>
{anonymous}::str<{anonymous}::str_storage<6>{std::array<char, 6>{"hello"}}>

It seems to always represent the char array as a null-terminated string
literal, which isn't always true.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/108269] __PRETTY_FUNCTION__  for NTTP literal intiailization containing a char array is assumed to ending in a null character
  2023-01-02 21:43 [Bug c++/108269] New: Incorrect __PRETTY_FUNCTION__ with std::array<char> in C++20 NTTP literal camden.mannett at gmail dot com
@ 2023-01-03  1:16 ` pinskia at gcc dot gnu.org
  2023-01-03  1:18 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-03  1:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108269

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-01-03
     Ever confirmed|0                           |1
            Summary|Incorrect                   |__PRETTY_FUNCTION__  for
                   |__PRETTY_FUNCTION__ with    |NTTP literal intiailization
                   |std::array<char> in C++20   |containing a char array is
                   |NTTP literal                |assumed to ending in a null
                   |                            |character

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
type_string<str<"hello\0\1">>

Works though:
static constexpr std::string_view {anonymous}::type_string_t<T>::generate()
[with T = {anonymous}::str<{anonymous}::str_storage<8>{std::array<char,
8>{"hello\000\001"}}>; std::string_view = std::basic_string_view<char>]

....

Oh the problem is just char ending not in a null character.
Reduced testcase:

template<auto b>
struct type_string_t
{
    constexpr static auto generate() noexcept
    {
        return __PRETTY_FUNCTION__;
    }
};
template<int N>
struct t
{
    char a[N];
};

int
main()
{
    __builtin_printf("%s\n", type_string_t<t{{'a', 'b'}}>::generate());
    return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/108269] __PRETTY_FUNCTION__  for NTTP literal intiailization containing a char array is assumed to ending in a null character
  2023-01-02 21:43 [Bug c++/108269] New: Incorrect __PRETTY_FUNCTION__ with std::array<char> in C++20 NTTP literal camden.mannett at gmail dot com
  2023-01-03  1:16 ` [Bug c++/108269] __PRETTY_FUNCTION__ for NTTP literal intiailization containing a char array is assumed to ending in a null character pinskia at gcc dot gnu.org
@ 2023-01-03  1:18 ` pinskia at gcc dot gnu.org
  2023-01-03  1:20 ` pinskia at gcc dot gnu.org
  2023-01-03 14:19 ` [Bug c++/108269] __PRETTY_FUNCTION__ for NTTP literal initialization " camden.mannett at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-03  1:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108269

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note this is just a problem with __PRETTY_FUNCTION__/__func__ and not an ABI
issue as it is done correctly for the mangling.
So it is just a minor issue dealing with debugging really because
__PRETTY_FUNCTION__/__func__ format does not have any standard way of
describing what it is.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/108269] __PRETTY_FUNCTION__  for NTTP literal intiailization containing a char array is assumed to ending in a null character
  2023-01-02 21:43 [Bug c++/108269] New: Incorrect __PRETTY_FUNCTION__ with std::array<char> in C++20 NTTP literal camden.mannett at gmail dot com
  2023-01-03  1:16 ` [Bug c++/108269] __PRETTY_FUNCTION__ for NTTP literal intiailization containing a char array is assumed to ending in a null character pinskia at gcc dot gnu.org
  2023-01-03  1:18 ` pinskia at gcc dot gnu.org
@ 2023-01-03  1:20 ` pinskia at gcc dot gnu.org
  2023-01-03 14:19 ` [Bug c++/108269] __PRETTY_FUNCTION__ for NTTP literal initialization " camden.mannett at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-03  1:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108269

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
__PRETTY_FUNCTION__ is also a GCC extension too so ...

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/108269] __PRETTY_FUNCTION__  for NTTP literal initialization containing a char array is assumed to ending in a null character
  2023-01-02 21:43 [Bug c++/108269] New: Incorrect __PRETTY_FUNCTION__ with std::array<char> in C++20 NTTP literal camden.mannett at gmail dot com
                   ` (2 preceding siblings ...)
  2023-01-03  1:20 ` pinskia at gcc dot gnu.org
@ 2023-01-03 14:19 ` camden.mannett at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: camden.mannett at gmail dot com @ 2023-01-03 14:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108269

--- Comment #4 from Camden Mannett <camden.mannett at gmail dot com> ---
> So it is just a minor issue dealing with debugging really ...
__PRETTY_FUNCTION__ is (mis)used for more than that though; it's often used as
an compile-time version of RTTI:
https://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname/66551751#66551751

There's a whole library based around the concept:
https://github.com/Manu343726/ctti

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-01-03 14:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-02 21:43 [Bug c++/108269] New: Incorrect __PRETTY_FUNCTION__ with std::array<char> in C++20 NTTP literal camden.mannett at gmail dot com
2023-01-03  1:16 ` [Bug c++/108269] __PRETTY_FUNCTION__ for NTTP literal intiailization containing a char array is assumed to ending in a null character pinskia at gcc dot gnu.org
2023-01-03  1:18 ` pinskia at gcc dot gnu.org
2023-01-03  1:20 ` pinskia at gcc dot gnu.org
2023-01-03 14:19 ` [Bug c++/108269] __PRETTY_FUNCTION__ for NTTP literal initialization " camden.mannett at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).