c++: avoid -Wredundant-tags on a first declaration in use [PR 93824]
authorMartin Sebor <msebor@gmail.com>
Fri, 27 Mar 2020 16:07:45 +0000 (12:07 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 27 Mar 2020 16:08:18 +0000 (12:08 -0400)
commit04dd734b52de121853e1ea6b3c197a598b294e23
treea616c0615d955949ed65701bfa024982698473f9
parent9dba60130dc3ebf7cce8716a36672281688693f7
c++: avoid -Wredundant-tags on a first declaration in use [PR 93824]

-Wredundant-tags doesn't consider type declarations that are also
the first uses of the type, such as in 'void f (struct S);' and
issues false positives for those.  According to the reported that's
making it harder to use the warning to clean up LibreOffice.

The attached patch extends -Wredundant-tags to avoid these false
positives by relying on the same class_decl_loc_t::class2loc mapping
as -Wmismatched-tags.  The patch also improves the detection
of both issues in template declarations.

gcc/cp/ChangeLog
2020-03-27  Martin Sebor  <msebor@redhat.com>

PR c++/94078
PR c++/93824
PR c++/93810
* cp-tree.h (most_specialized_partial_spec): Declare.
* parser.c (cp_parser_elaborated_type_specifier): Distinguish alias
from declarations.
(specialization_of): New function.
(cp_parser_check_class_key): Move code...
(class_decl_loc_t::add): ...to here.  Add parameters.  Avoid issuing
-Wredundant-tags on first-time declarations in other declarators.
Correct handling of template specializations.
(class_decl_loc_t::diag_mismatched_tags): Also expect to be called
when -Wredundant-tags is enabled.  Use primary template or partial
specialization as the guide for uses of implicit instantiations.
* pt.c (most_specialized_partial_spec): Declare extern.

gcc/testsuite/ChangeLog
2020-03-27  Martin Sebor  <msebor@redhat.com>

PR c++/94078
PR c++/93824
PR c++/93810
* g++.dg/warn/Wmismatched-tags-3.C: New test.
* g++.dg/warn/Wmismatched-tags-4.C: New test.
* g++.dg/warn/Wmismatched-tags-5.C: New test.
* g++.dg/warn/Wmismatched-tags-6.C: New test.
* g++.dg/warn/Wredundant-tags-3.C: Remove xfails.
* g++.dg/warn/Wredundant-tags-6.C: New test.
* g++.dg/warn/Wredundant-tags-7.C: New test.
12 files changed:
gcc/cp/ChangeLog
gcc/cp/cp-tree.h
gcc/cp/parser.c
gcc/cp/pt.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wmismatched-tags-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wmismatched-tags-4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wmismatched-tags-5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wmismatched-tags-6.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wredundant-tags-3.C
gcc/testsuite/g++.dg/warn/Wredundant-tags-6.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wredundant-tags-7.C [new file with mode: 0644]