Bug 108079 - [11 Regression] -Wunused-variable gives misleading duplicate warning for unused static local variable
Summary: [11 Regression] -Wunused-variable gives misleading duplicate warning for unus...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 13.0
: P2 normal
Target Milestone: 12.3
Assignee: Jakub Jelinek
URL:
Keywords: diagnostic
Depends on:
Blocks: Wunused
  Show dependency treegraph
 
Reported: 2022-12-13 04:50 UTC by Stephen Heumann
Modified: 2024-07-19 12:12 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 12.3.0, 5.5.0
Known to fail: 11.5.0, 12.2.0, 6.1.0
Last reconfirmed: 2022-12-13 00:00:00


Attachments
gcc13-pr108079.patch (1.02 KB, patch)
2022-12-21 14:40 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stephen Heumann 2022-12-13 04:50:32 UTC
For this program:

int main(void) {
        static int x;
}

gcc -Wunused-variable reports:

<source>: In function 'main':
<source>:2:20: warning: unused variable 'x' [-Wunused-variable]
    2 |         static int x;
      |                    ^
<source>: At top level:
<source>:2:20: warning: 'x' defined but not used [-Wunused-variable]

These two warnings are about the same thing, except the second one is incorrectly labeled "At top level." There should just be one warning, without "At top level."

C++ does the same thing, except it says "At global scope" instead of "At top level."

This seems to be a regression that occurred in GCC 6: 5.4 just gives one warning, but 6.1 also gives the extra one with "At top level."
Comment 1 Richard Biener 2022-12-13 12:16:37 UTC
Confirmed.
Comment 2 Jakub Jelinek 2022-12-21 14:14:51 UTC
Seems to have started with r6-1401-gd7438551ff5ffa0afeca2aa3efd13035b26bee34
One of the warnings is diagnosed by the FE (from pop_scope/poplevel),
the other by cgraphunit (check_global_declaration).
Comment 3 Jakub Jelinek 2022-12-21 14:40:08 UTC
Created attachment 54143 [details]
gcc13-pr108079.patch

Untested fix.
Comment 4 GCC Commits 2023-03-10 09:15:57 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:2c63cc7268fd5615997989f153e9405d0f5aaa50

commit r13-6575-g2c63cc7268fd5615997989f153e9405d0f5aaa50
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Mar 10 10:10:24 2023 +0100

    c, c++, cgraphunit: Prevent duplicated -Wunused-value warnings [PR108079]
    
    On the following testcase, we warn with -Wunused-value twice, once
    in the FEs and later on cgraphunit again with slightly different
    wording.
    
    The following patch fixes that by registering a warning suppression in the
    FEs when we warn and not warning in cgraphunit anymore if that happened.
    
    2023-03-10  Jakub Jelinek  <jakub@redhat.com>
    
            PR c/108079
    gcc/
            * cgraphunit.cc (check_global_declaration): Don't warn for unused
            variables which have OPT_Wunused_variable warning suppressed.
    gcc/c/
            * c-decl.cc (pop_scope): Suppress OPT_Wunused_variable warning
            after diagnosing it.
    gcc/cp/
            * decl.cc (poplevel): Suppress OPT_Wunused_variable warning
            after diagnosing it.
    gcc/testsuite/
            * c-c++-common/Wunused-var-18.c: New test.
Comment 5 Jakub Jelinek 2023-03-10 09:19:52 UTC
Fixed on the trunk so far.
Comment 6 GCC Commits 2023-03-19 05:30:44 UTC
The releases/gcc-12 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:e4984838f6af868399ef9d885377199c38907ee1

commit r12-9287-ge4984838f6af868399ef9d885377199c38907ee1
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Mar 10 10:10:24 2023 +0100

    c, c++, cgraphunit: Prevent duplicated -Wunused-value warnings [PR108079]
    
    On the following testcase, we warn with -Wunused-value twice, once
    in the FEs and later on cgraphunit again with slightly different
    wording.
    
    The following patch fixes that by registering a warning suppression in the
    FEs when we warn and not warning in cgraphunit anymore if that happened.
    
    2023-03-10  Jakub Jelinek  <jakub@redhat.com>
    
            PR c/108079
    gcc/
            * cgraphunit.cc (check_global_declaration): Don't warn for unused
            variables which have OPT_Wunused_variable warning suppressed.
    gcc/c/
            * c-decl.cc (pop_scope): Suppress OPT_Wunused_variable warning
            after diagnosing it.
    gcc/cp/
            * decl.cc (poplevel): Suppress OPT_Wunused_variable warning
            after diagnosing it.
    gcc/testsuite/
            * c-c++-common/Wunused-var-18.c: New test.
    
    (cherry picked from commit 2c63cc7268fd5615997989f153e9405d0f5aaa50)
Comment 7 Jakub Jelinek 2023-03-20 10:30:00 UTC
Fixed for 12.3 too.
Comment 8 Richard Biener 2023-07-07 10:44:34 UTC
GCC 10 branch is being closed.
Comment 9 Richard Biener 2024-07-19 12:12:34 UTC
Fixed in GCC 12.3.