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

Re: [PATCH 4/3] pack-bitmap.h: fix unused variable warning

From
Jeff King <peff@peff.net>
Date
Sep 24, 2019, 21:34 UTC
Message-ID
<20190924213408.GD20858@sigill.intra.peff.net>
In-Reply-To
<a3a3357925cb144deb35b298cbe2796934fa0673.1569315905.git.liu.denton@gmail.com>
On Tue, Sep 24, 2019 at 02:08:53AM -0700, Denton Liu wrote:
Show 11 quoted lines
> When we ran `make hdr-check`, we got the following warning on Arch Linux:
> 
> 	pack-bitmap.h:20:19: error: ‘BITMAP_IDX_SIGNATURE’ defined but not used [-Werror=unused-const-variable=]
> 	   20 | static const char BITMAP_IDX_SIGNATURE[] = {'B', 'I', 'T', 'M'};
> 	      |                   ^~~~~~~~~~~~~~~~~~~~
> 	cc1: all warnings being treated as errors
> 
> "Use" the BITMAP_IDX_SIGNATURE variable by making the size of
> bitmap_disk_header.magic equal to the size of BITMAP_IDX_SIGNATURE. An
> alternative was to simply add MAYBE_UNUSED. However, this design was
> chosen because we eliminate the magic number (4) in the process.

Yeah, I think this is a fine fix. Since it's needed in only a few files, the more usual fix would be to not define it in the header in the first place. Something like:

  extern const char BITMAP_IDX_SIGNATURE[4];
or whatever. But I think it's nice to keep it all together.
> I'm tacking this patch on since this warning didn't show up until I
> compiled it on gcc 9.1.0.

Curiously, I _don't_ see the warning with gcc 9.2.1. By my reading of the manpage, this should be triggered by -Wunused-const-variable=2, but not by "1" (the difference being whether it triggers for stuff in header files). And only the latter is triggered by -Wall or -Wextra.

But another weirdness is that hdr-check is directly compiling the header files. So I guess that fools it. But we don't pass any of the extra diagnostic options there. Have you put "-Wall" into your $(CC)?

Perhaps a more realistic hdr-check would be:
  {
    echo '#include "git-compat-util.h"'
    echo '#include "$<"'
  } >$*.hcc
  $(QUIET_HDR)$(CC) $(ALL_CFLAGS) -o /dev/null -c -xc $*.hcc
-Peff
Previous: Denton LiuNext: Jeff King
Message 10 of 18 in “fixes related to `make hdr-check`”
  1. 0/3 fixes related to `make hdr-check`Denton Liu, Sep 23, 2019
  2. 1/3 Makefile: use $(ALL_CFLAGS) in $(HCO) targetDenton Liu, Sep 23, 2019
  3. Johannes SchindelinSep 26, 2019
  4. Denton LiuSep 26, 2019
  5. Johannes SchindelinSep 26, 2019
  6. Junio C HamanoSep 28, 2019
  7. 2/3 apply.h: include missing headerDenton Liu, Sep 23, 2019
  8. 3/3 promisor-remote.h: include missing headerDenton Liu, Sep 23, 2019
  9. 4/3 pack-bitmap.h: fix unused variable warningDenton Liu, Sep 24, 2019
  10. Jeff KingSep 24, 2019
  11. Jeff KingSep 24, 2019
  12. 0/4 fixes related to `make hdr-check`Denton Liu, Sep 25, 2019
  13. 1/4 apply.h: include missing headerDenton Liu, Sep 25, 2019
  14. 2/4 promisor-remote.h: include missing headerDenton Liu, Sep 25, 2019
  15. 3/4 pack-bitmap.h: remove magic numberDenton Liu, Sep 25, 2019
  16. 4/4 Makefile: emulate compile in $(HCO) target betterDenton Liu, Sep 25, 2019
  17. Jeff KingOct 2, 2019
  18. Johannes SchindelinSep 26, 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.