Bug 104693 - Can't disable "comparison between pointer and integer"
Summary: Can't disable "comparison between pointer and integer"
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 12.0
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2022-02-25 17:50 UTC by Charles Nicholson
Modified: 2022-02-26 08:51 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-02-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Charles Nicholson 2022-02-25 17:50:20 UTC
The following code causes gcc to emit a warning that does not seem to have a diagnostic name, which makes it hard (impossible?) to disable:

=========================================
#include <stddef.h>
#include <stdbool.h>

bool foo(unsigned long not_a_pointer) {
  return not_a_pointer == NULL;
}
=========================================
<source>: In function 'foo':
<source>:5:24: warning: comparison between pointer and integer
    5 |   return not_a_pointer == NULL;
      |                        ^~
=========================================
Godbolt link: https://gcc.godbolt.org/z/chcxYevvf

I don't think anyone would argue that this is "good" (portable, defined, etc) C code; it is vendor code that I'm stuck with. Additionally, on the architecture I'm targeting, the implementation does the right thing: the unsigned long is pointer-sized, and is compared against the NULL literal 0, which is used in this context as a sentinel value. The code should use the literal 0 instead of NULL, but it does not.

I have 3 suggestions, if they're helpful:

1. If this warning can be disabled, update the diagnostic message to include the name as a hint on how to disable it.

2. If this warning can not be disabled, consider giving it a formal diagnostic name and making it controllable via the standard methods (-Wno-, pragma, etc)

3. If this is truly heinous / unsafe enough, promote it to an error.

Thanks for all of your efforts on gcc!

Best,
Charles
Comment 1 Charles Nicholson 2022-02-25 17:50:47 UTC
Oh, also, this warning appears to go all the way back to gcc 4.1.2, the earliest that godbolt still supports.
Comment 2 Andrew Pinski 2022-02-26 08:51:38 UTC
Confirmed, the C++ front-end can turn off this warning with -Wno-pointer-arith so I don't see why the C front-end could be made to do the same.