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

--- Comment #21 from Mark Wielaard <mark at gcc dot gnu.org> ---
Sorry, I forgot about this bug still being open.

It isn't clear to me whether the issues seen in the kernel sources are
a) just legitimate and should be fixed
   (like comment #16 seem to imply)
b) thousand+ warnings and only a few legitimate warnings
   (as comment #17 seems to imply)
c) not quite much warnings, though some unwanted
   (like comment #19 says)

Although in C a static const is not really like a #define I suspect that there
are cases where they are used as such in header files. If that is the major
reason for why there are people opposed to the warning then what we could do is
hide such (non-)usage of static const variables from header files behind
-Wextra and only explicitly warn for static const variable defined (and then
not used) in the main file.

diff --git a/gcc/cgraphunit.c b/gcc/cgraphunit.c
index b95c172..acf8fe5 100644
--- a/gcc/cgraphunit.c
+++ b/gcc/cgraphunit.c
@@ -944,7 +944,10 @@ check_global_declaration (symtab_node *snode)
   /* Warn about static fns or vars defined but not used.  */
   if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
        || (((warn_unused_variable && ! TREE_READONLY (decl))
-           || (warn_unused_const_variable && TREE_READONLY (decl)))
+           || (warn_unused_const_variable && TREE_READONLY (decl)
+               && (extra_warnings
+                   || filename_cmp (main_input_filename,
+                                    DECL_SOURCE_FILE (decl)) == 0)))
           && TREE_CODE (decl) == VAR_DECL))
       && ! DECL_IN_SYSTEM_HEADER (decl)
       && ! snode->referred_to_p (/*include_self=*/false)

Reply via email to