On 19/02/2020 01:02, Martin Sebor wrote:
PR 93804 points out that issuing -Wredundant-tags for declarations
in C headers included in C++ isn't helpful because the tags cannot
be removed without breaking C programs that depend on the headers.
Attached is a patch that avoids the warning in these cases tested
on x86_64-linux. While strictly not a regression (and even though
I initially considered it a GCC 11 enhancement), since most C++
code includes some C headers, without the patch the warning would
likely cause too much noise to be widely useful.
Warnings about redundant enum tags in shared C/C++ code should likewise
be suppressed. Something like the attached patch.
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 2c6f5522bf3..fda2a6f3d5c 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -30828,6 +30828,20 @@ cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
itself. */
if (decl == type_decl)
{
+ if (current_lang_name != lang_name_cplusplus
+ && current_namespace == global_namespace)
+ {
+ /* Avoid issuing the diagnostic for apparently redundant struct
+ and union class-keys in shared C/C++ code in files (such as
+ headers) included in the main source file. */
+ const line_map_ordinary *map = NULL;
+ linemap_resolve_location (line_table, key_loc,
+ LRK_MACRO_DEFINITION_LOCATION,
+ &map);
+ if (!MAIN_FILE_P (map))
+ return;
+ }
+
gcc_rich_location richloc (key_loc);
richloc.add_fixit_remove (key_loc);
warning_at (&richloc, OPT_Wredundant_tags,