https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81484

            Bug ID: 81484
           Summary: incorrect -Wint-in-bool-context warning
           Product: gcc
           Version: 7.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arnd at linaro dot org
  Target Milestone: ---

I came across a warning in the linux kernel with gcc-7.1.1:

    arch/x86/math-emu/reg_add_sub.c: In function 'FPU_add':
    arch/x86/math-emu/reg_add_sub.c:80:48: error: ?: using integer constants in
boolean context [-Werror=int-in-bool-context]

After analyzing it further, I reduced it to this test program, please compile
with "gcc-7.1.1 -Wall -O2":

#define signbyte(a)     (((unsigned char *)(a))[9])
#define getsign(a)      (signbyte(a) & 0x80)
#define setsign(a,b)    do { if (b) signbyte(a) |= 0x80; else signbyte(a) &=
0x7f; } while (0)
#define CW_RC           0x0C00
#define RC_DOWN         0x0400
#define SIGN_POS        0u
#define SIGN_NEG        0x80u
int FPU_add(void *dest, int control_w)
{
        /* a) correct code, bogus warning */
        setsign(dest, ((control_w & CW_RC) != RC_DOWN) ? SIGN_POS : SIGN_NEG);

        /* b) false-positive warning */
        setsign(dest, (control_w & CW_RC) != RC_DOWN ? SIGN_POS : SIGN_NEG);

        /* c) wrong code, no warning */
        setsign(dest, (control_w & CW_RC) != (RC_DOWN ? SIGN_POS : SIGN_NEG));

        return 0;
}

The warning we get is about case a), which as far as I can tell is written
correctly and unambiguous, and should not trigger a warning.

It appears that the compiler mistakes this for code that is written in a
somewhat ambiguous way like the (still-correct) variant b) when the author
actually meant to write it like c). The result is that we also get a
false-positive warning for b) that makes a lot of sense, while the incorrect
case c) is a false-negative here, as we don't get a warning.

Reply via email to