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

            Bug ID: 102909
           Summary: Missing -Wunused-but-set-variable warning
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mytbk920423 at gmail dot com
  Target Milestone: ---

GCC misses the -Wunused-but-set-variable in the some code (GCC and Clang output
can be seen in https://godbolt.org/z/7658M4qra).

I first found a bug caused by the following code, in which no compilers warn:

void ioapic_set_max_vectors(void *ioapic_base, int mre_count)
{
        u32 reg;
        u8 count;
        reg = io_apic_read(ioapic_base, 0x01);
        count = reg >> 16;
        if (mre_count > 0)
                count = mre_count - 1;

        // reg is defined but not used after this
        reg &= ~(0xff << 16);
        reg |= count << 16;
        // ``count`` should be ``reg`` in the following line
        io_apic_write(ioapic_base, 0x01, count);
}

I think that's because the variable ``reg`` is only unused in part of the
control flow, so I change the code as following. However, GCC doesn't warn on
this either.

void ioapic_set_max_vectors(void *ioapic_base, int mre_count)
{
        u32 reg;
        u32 new_reg;
        u8 count;
        reg = io_apic_read(ioapic_base, 0x01);
        count = reg >> 16;
        if (mre_count > 0)
                count = mre_count - 1;

        // new_reg is defined but not used
        new_reg = reg & (~(0xff << 16));
        new_reg |= count << 16;
        // ``count`` should be ``new_reg`` in the following
        io_apic_write(ioapic_base, 0x01, count);
}

Reply via email to