public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/102909] New: Missing -Wunused-but-set-variable warning
@ 2021-10-23 15:27 mytbk920423 at gmail dot com
  2021-10-23 15:51 ` [Bug c/102909] " jakub at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: mytbk920423 at gmail dot com @ 2021-10-23 15:27 UTC (permalink / raw)
  To: gcc-bugs

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);
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/102909] Missing -Wunused-but-set-variable warning
  2021-10-23 15:27 [Bug c/102909] New: Missing -Wunused-but-set-variable warning mytbk920423 at gmail dot com
@ 2021-10-23 15:51 ` jakub at gcc dot gnu.org
  2021-10-24  1:39 ` mytbk920423 at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-10-23 15:51 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The warning works as documented.  It warns about variables that are only stored
to and never read.  new_reg in the second testcase or reg is not such a
variable, it is also read.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/102909] Missing -Wunused-but-set-variable warning
  2021-10-23 15:27 [Bug c/102909] New: Missing -Wunused-but-set-variable warning mytbk920423 at gmail dot com
  2021-10-23 15:51 ` [Bug c/102909] " jakub at gcc dot gnu.org
@ 2021-10-24  1:39 ` mytbk920423 at gmail dot com
  2021-10-24  1:47 ` mytbk920423 at gmail dot com
  2021-10-24  6:09 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mytbk920423 at gmail dot com @ 2021-10-24  1:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Iru Cai <mytbk920423 at gmail dot com> ---
So it looks something like https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677

GCC thinks ``a`` is set but not used in ``a = 1 + b;``, but is used in ``a = 1;
a += b;``.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/102909] Missing -Wunused-but-set-variable warning
  2021-10-23 15:27 [Bug c/102909] New: Missing -Wunused-but-set-variable warning mytbk920423 at gmail dot com
  2021-10-23 15:51 ` [Bug c/102909] " jakub at gcc dot gnu.org
  2021-10-24  1:39 ` mytbk920423 at gmail dot com
@ 2021-10-24  1:47 ` mytbk920423 at gmail dot com
  2021-10-24  6:09 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mytbk920423 at gmail dot com @ 2021-10-24  1:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Iru Cai <mytbk920423 at gmail dot com> ---
Looks like this kind of things are detected in the front-end. The GNAT
front-end can warn on the similar things:

procedure Main is
        A : Integer;
        B : constant Integer := 1;
begin
        A := 0;
        A := A + B;
end Main;

$ gcc -O2 -gnatwa -gnatwe -c main.adb 
main.adb:6:09: warning: possibly useless assignment to "A", value might not be
referenced

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/102909] Missing -Wunused-but-set-variable warning
  2021-10-23 15:27 [Bug c/102909] New: Missing -Wunused-but-set-variable warning mytbk920423 at gmail dot com
                   ` (2 preceding siblings ...)
  2021-10-24  1:47 ` mytbk920423 at gmail dot com
@ 2021-10-24  6:09 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-24  6:09 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 44677.

*** This bug has been marked as a duplicate of bug 44677 ***

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-10-24  6:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-23 15:27 [Bug c/102909] New: Missing -Wunused-but-set-variable warning mytbk920423 at gmail dot com
2021-10-23 15:51 ` [Bug c/102909] " jakub at gcc dot gnu.org
2021-10-24  1:39 ` mytbk920423 at gmail dot com
2021-10-24  1:47 ` mytbk920423 at gmail dot com
2021-10-24  6:09 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).