git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] range-diff: fix some 'hdr-check' and sparse warnings

From
Jeff King <peff@peff.net>
Date
Jul 14, 2019, 00:51 UTC
Message-ID
<20190714005129.GA4525@sigill.intra.peff.net>
In-Reply-To
<CAPUEspgD8E02FcNvBx96neGEejdqMDWoH8NgpsHyGPo_KM09FA@mail.gmail.com>
On Sat, Jul 13, 2019 at 03:22:03PM -0700, Carlo Arenas wrote:
Show 13 quoted lines
> On Sat, Jul 13, 2019 at 2:29 PM Junio C Hamano <gitster@pobox.com> wrote:
> >
> > I wish if we could say
> >
> >         struct patch patch = {};
> 
> that is actually a GNU extension that is supported by gcc and clang (at least)
> and that sparse also recognizes as valid; it is also valid C++ so it might be
> even supported by MSVC.
> 
> it will obviously trigger warnings if using pedantic mode and is IMHO not worth
> the hassle to maintain in a portable codebase like git, but also wish could be
> added to C2X
I wonder if we could come up with a definition of INIT_ZERO such that:
  struct foo bar = { INIT_ZERO };

worked everywhere. IMHO that is more readable than "{}" anyway. In GNU-compatible compilers, it is just:

  #define INIT_ZERO

Unfortunately I can't think of a fallback that would work universally. It cannot just be "0", as we run into the NULL thing (not to mention when the first member is itself a struct). It does not help us to add an explicit cast, because the type of the cast is dependent on the context in which the macro is used. Nor do I think typeof() could save us (if we could even assume it exists everywhere we need the fallback, which we almost certainly can't).

But it does seem a real shame there is no way to say "zero-initialize this struct" in C without providing at least a single member value. Ordering struct definitions to put an arithmetic type at the start is an OK workaround (to just let "0" work everywhere). But it does fall down when the first element _has_ to be a struct (like, say, any user of our hashmap.[ch] interface).

-Peff
Previous: Carlo ArenasNext: Johannes Sixt
Message 9 of 23 in “range-diff: fix some 'hdr-check' and sparse warnings”
  1. range-diff: fix some 'hdr-check' and sparse warningsRamsay Jones, Jul 11, 2019
  2. Johannes SixtJul 12, 2019
  3. Junio C HamanoJul 12, 2019
  4. Johannes SixtJul 13, 2019
  5. Johannes SixtJul 13, 2019
  6. Carlo ArenasJul 13, 2019
  7. Junio C HamanoJul 13, 2019
  8. Carlo ArenasJul 13, 2019
  9. Jeff KingJul 14, 2019
  10. Johannes SixtJul 14, 2019
  11. Jeff KingJul 15, 2019
  12. Johannes SixtJul 15, 2019
  13. Jeff KingJul 15, 2019
  14. Junio C HamanoJul 16, 2019
  15. Jeff KingJul 16, 2019
  16. Junio C HamanoJul 17, 2019
  17. Jeff KingJul 17, 2019
  18. Junio C HamanoJul 17, 2019
  19. Johannes SixtJul 17, 2019
  20. Johannes SchindelinJul 15, 2019
  21. Junio C HamanoOct 2, 2020
  22. Johannes SchindelinOct 4, 2020
  23. Johannes SixtJul 14, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.