public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Saagar Jha <saagar@saagarjha.com>
To: Hans-Peter Nilsson <hp@bitrange.com>
Cc: Alan Modra <amodra@gmail.com>, binutils@sourceware.org
Subject: Re: [PATCH] Fix a warning when initializing symbol_flags
Date: Sat, 26 Sep 2020 15:30:53 -0700	[thread overview]
Message-ID: <40A73F70-3061-4094-AFEF-0A3337250B3C@saagarjha.com> (raw)
In-Reply-To: <alpine.BSF.2.20.16.2009261518560.40513@arjuna.pair.com>

Ah, so I just realized that this file is actually compiled as C and not a C++ file in .c clothing, so even if this didn’t warn in Clang the patch was still incorrect as {} is an invalid initializer in C and GCC is correct in flagging it (though perhaps not for the right reasons). Newer versions of Clang do correctly identify { 0 } as the correct zero initialization idiom: https://github.com/llvm/llvm-project/commit/817a3bfcdd0bbac8d74fdfdb83a08484d8f63a30, although most distributions seem to carry a version older than this.

Eleven zeroes is, as far as I can tell, the only widely-compatible, guaranteed-to-work way to initialize this without warnings. memset would generally “work” but if we’re being pedantic I believe this has only been guaranteed as of a recent C standard (C11?) So I guess I’ll have to defer to whatever the decision is here on portability versus readability. (Third option: disable the warning around this code, perhaps after checking theg version.)

> On Sep 26, 2020, at 12:21, Hans-Peter Nilsson <hp@bitrange.com> wrote:
> 
> On Sat, 26 Sep 2020, Alan Modra via Binutils wrote:
> 
>> On Fri, Sep 25, 2020 at 05:42:24PM -0700, Saagar Jha wrote:
>>> Clang on certain platforms (such as arm64) will warn if a struct is
>>> partially initialized:
>>> 
>>> symbols.c:199:35: error: missing field 'written' initializer [-Werror,-Wmissing-field-initializers]
>>> 2895  symbol_entry_t needle = { { { 0 }, hash, name, 0, 0, 0 } };
>>> 
>>> If we use empty braces we have the same effect but avoid the warning.
>> 
>> But on gcc-4.9 this results in:
>> 
>> symbols.c: In function ?symbol_entry_find?:
>> symbols.c:199:3: error: missing initializer for field ?local_symbol? of ?struct symbol_flags? [-Werror=missing-field-initializers]
>>   symbol_entry_t needle = { { { }, hash, name, 0, 0, 0 } };
>>   ^
>> symbols.c:39:16: note: ?local_symbol? declared here
>>   unsigned int local_symbol : 1;
>>                ^
>> 
>> So I don't think we should apply your patch.
> 
> How about a memset 0 and setting .hash and .name?
> 
> I had to do that locally for an older gcc, that had a similar
> warning.  I prefered that over 11 (IIRC) "0," and having to
> keep them straight. :)
> 
> brgds, H-P


  reply	other threads:[~2020-09-26 22:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-26  0:42 Saagar Jha
2020-09-26 13:06 ` Alan Modra
2020-09-26 19:21   ` Hans-Peter Nilsson
2020-09-26 22:30     ` Saagar Jha [this message]
2020-09-30  6:12       ` Alan Modra
2020-09-30  6:22         ` Saagar Jha

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=40A73F70-3061-4094-AFEF-0A3337250B3C@saagarjha.com \
    --to=saagar@saagarjha.com \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=hp@bitrange.com \
    /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).