public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Amol Surati <suratiamol@gmail.com>
To: gcc-help <gcc-help@gcc.gnu.org>
Subject: subobject init override
Date: Fri, 17 May 2024 20:32:30 +0530	[thread overview]
Message-ID: <CA+nuEB9ZB5_kFcf7nEbc-6kfFGbTVCRzybFnP9U00LUAWYCAfg@mail.gmail.com> (raw)

Hello,

[1] says

"Although any non-initialized subobjects are initialized implicitly, implicit
initialization of a subobject never overrides explicit initialization of the
same subobject if it appeared earlier in the initializer list (choose clang
to observe the correct output)".

There's a sample program attached on the website at [1] around the
statement, and the behaviour clearly differs between gcc and clang.

-----------
This difference is affecting Mesa builds with gcc, because Mesa utilizes
such subobject-init-override patterns. One such instance is within the
function blorp_params_get_clear_kernel_fs listed at [2].

The relevant parts were extracted and built into a small test program,
available at [3] (for gcc) and [4] (for clang)

gcc prints: "" ''
clang prints: "blorp" 'b'
clang does spit out warnings about -Winitializer-overrides before
allowing the compilation to succeed.

According to the cppref website, Mesa's usage is correct, and it is gcc
that's ignoring the fact that the subobject was previously *explicitly*
initialized.

The C spec says:
"The initialization shall occur in initializer list order, each initializer
provided for a particular subobject overriding any previously listed
initializer for the same subobject;170) all subobjects that are not
initialized explicitly are subject to default initialization."

Is there a way to force gcc to behave as clang does?
If not, could you please help determine who exactly is at fault here
and must receive a change/bug request, if needed?

Thank you,
Amol
-----------------------------
[1] https://en.cppreference.com/w/c/language/struct_initialization
[2] https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/intel/blorp/blorp_clear.c?ref_type=heads
[3] https://godbolt.org/z/env53Wq9M
[4] https://godbolt.org/z/8TMfbb5Ef

             reply	other threads:[~2024-05-17 15:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-17 15:02 Amol Surati [this message]
2024-06-05  5:36 ` Amol Surati
2024-06-05  6:01   ` Xi Ruoyao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+nuEB9ZB5_kFcf7nEbc-6kfFGbTVCRzybFnP9U00LUAWYCAfg@mail.gmail.com \
    --to=suratiamol@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).