public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* GCC 12.3 ICE: format(printf...) failing in C++ with virtual inheritance
@ 2023-12-08 22:21 Paul Smith
  2023-12-09 10:03 ` Amol Surati
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Smith @ 2023-12-08 22:21 UTC (permalink / raw)
  To: gcc

I've tried this with both older versions as well as GCC 12.3 (latest I
have access to).  This is on GNU/Linux on x86_64.


I have the following code:

  #include <exception>

  class Exception : public std::exception
  {
  public:
      Exception(const char* text, ...)
        __attribute__((format(printf, 2, 3)));
  };

  void foo()
  {
      throw Exception("something bad");
  }

this works fine.

However, due to some diamond inheritance I need to switch to virtual
inheritance here:

  #include <exception>

  class Exception : public /**/ virtual /**/ std::exception
  {
  public:
      Exception(const char* text, ...)
          __attribute__((format(printf, 2, 3)));
  };

  void foo()
  {
      throw Exception("something bad");
  }

this does not work AT ALL.

First, the extra virtual infrastructure means that the offsets we use
(note we need to add one to the format parameters anyway due to the
this pointer I assume) are wrong:

  /tmp/virt.cpp:7:45: error: 'format' attribute argument 2 value '2'  
refers to parameter type 'int'
      7 |         __attribute__((format(printf, 2, 3)));
        |                                             ^

Just adding 1 to this, for 3, 4, doesn't help:

  /tmp/virt.cpp:7:45: error: 'format' attribute argument 2 value '3'  
refers to parameter type 'const void**'
      7 |         __attribute__((format(printf, 3, 4)));
        |                                             ^

And if I add 2 instead so it's 4,5 instead, I get an ICE:

  /tmp/virt.cpp: In function 'void foo()':
  /tmp/virt.cpp:12:36: error: 'format' attribute argument 2 value '4'   exceeds the number of function parameters 2 [-Werror=attributes]
     12 |     throw Exception("something bad");
        |                                    ^
  /tmp/virt.cpp:12:36: internal compiler error: in get_constant, at c-family/c-format.cc:323
  0x7fba675a3082 __libc_start_main
          ../csu/libc-start.c:308
  Please submit a full bug report, with preprocessed source (by using -freport-bug).
  Please include the complete backtrace with any bug report.
  See <https://gcc.gnu.org/bugs/> for instructions.

I'm assuming a bug should be filed for this ICE (can anyone repro it in
the current release?), but does anyone know if there's any way to make
it work in GCC 12.3?

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

end of thread, other threads:[~2023-12-09 10:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-08 22:21 GCC 12.3 ICE: format(printf...) failing in C++ with virtual inheritance Paul Smith
2023-12-09 10:03 ` Amol Surati

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).