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."
Confirmed.
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).
Created attachment 54143 [details] gcc13-pr108079.patch Untested fix.
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.
Fixed on the trunk so far.
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)
Fixed for 12.3 too.
GCC 10 branch is being closed.
Fixed in GCC 12.3.