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

* Re: GCC 12.3 ICE: format(printf...) failing in C++ with virtual inheritance
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Amol Surati @ 2023-12-09 10:03 UTC (permalink / raw)
  To: psmith; +Cc: gcc

On Sat, 9 Dec 2023 at 03:53, Paul Smith via Gcc <gcc@gcc.gnu.org> wrote:
>
> 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:
[...]
> 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?

If it is suitable to modify the source code of the application, a workaround
is to let the constructor take a ref/ptr to va_list instead, and wrap a call to
it inside a variadic C-style function, or a variadic static member function,
that creates and returns the Exception object. An example at [1].
-----
The ICE isn't a problem here, since the string-index and first-to-check
attr-parameters of [2] should not consider any hidden func-parameters
except the 'this' pointer. The values 2 and 3 resp. are therefore the
correct arguments to pass to the attr-parameters for the given c++
code utilizing virtual inheritance.

What is missing from 12.3.0 (and also from the latest on the
releases/gcc-12 branch) is the adjustments, that the compiler must
internally make, to the arguments passed to the attr-parameters, when
virtual inheritance adds 1 or 2 additional hidden func-parameters to the
constructor.

The gcc-13 branch does contain those adjustments. See [3].
The last comment on it is:
---------------
    Marek Polacek 2022-05-07 14:59:02 UTC
    Fixed for GCC 13.  I don't have plans to backport this.
---------------
[1] https://godbolt.org/z/bh9df9xGP
[2] https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
[3] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101833

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