]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR28901 Add two levels for -Wunused-const-variable.
authorMark Wielaard <mjw@redhat.com>
Mon, 22 Feb 2016 22:42:19 +0000 (22:42 +0000)
committerMark Wielaard <mark@gcc.gnu.org>
Mon, 22 Feb 2016 22:42:19 +0000 (22:42 +0000)
There is some controversy about enabling -Wunused-const-variable for all
unused static const variables because some feel there are too many errors
exposed in header files. Create two levels for -Wunused-const-variable.
One level to only check for unused static const variables in the main
compilation file. Which is enabled by -Wunused-variable. And a second
level that also checks for unused static const variables in included
files. Which must be explicitly enabled.

gcc/ChangeLog

PR c/28901
* cgraphunit.c (check_global_declaration): Check level of
warn_unused_const_variable and main_input_filename.
* doc/invoke.texi (Warning Options): Add -Wunused-const-variable=.
(-Wunused-variable): For C implies -Wunused-const-variable=1.
(-Wunused-const-variable): Explain levels 1 and 2.

gcc/c-family/ChangeLog

PR c/28901
* c.opt (Wunused-const-variable): Turn into Alias for...
(Wunused-const-variable=): New option.

gcc/testsuite/ChangeLog

PR c/28901
* gcc.dg/unused-variable-3.c: New test.

From-SVN: r233616

gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c.opt
gcc/cgraphunit.c
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/unused-variable-3.c [new file with mode: 0644]

index 1266d58defcd6cbdac173f4dbf3233fb2fb734d2..49f6c25399216d343cdf913978c9b1bb572c608a 100644 (file)
@@ -1,3 +1,12 @@
+2016-02-20  Mark Wielaard  <mjw@redhat.com>
+
+       PR c/28901
+       * cgraphunit.c (check_global_declaration): Check level of
+       warn_unused_const_variable and main_input_filename.
+       * doc/invoke.texi (Warning Options): Add -Wunused-const-variable=.
+       (-Wunused-variable): For C implies -Wunused-const-variable=1.
+       (-Wunused-const-variable): Explain levels 1 and 2.
+
 2016-02-22  Jakub Jelinek  <jakub@redhat.com>
 
        PR target/69888
index f3dd70965e25994a24ee798fe0198a11ae04ba8d..f11503ecd150d7e6e19330c4334164d6e5331bb6 100644 (file)
@@ -1,3 +1,9 @@
+2016-02-20  Mark Wielaard  <mjw@redhat.com>
+
+       PR c/28901
+       * c.opt (Wunused-const-variable): Turn into Alias for...
+       (Wunused-const-variable=): New option.
+
 2016-02-19  Bernd Edlinger  <bernd.edlinger@hotmail.de>
 
        PR c++/69865
index 638e9c24f819e2ee4099a232f95f1416401ede44..7c5f6c7b5ccbcbc42cde8739cc7011b3fed66fff 100644 (file)
@@ -949,7 +949,11 @@ C ObjC C++ ObjC++ LangEnabledBy(C ObjC C++ ObjC++,Wunused)
 ; documented in common.opt
 
 Wunused-const-variable
-C ObjC C++ ObjC++ Var(warn_unused_const_variable) Warning LangEnabledBy(C ObjC,Wunused-variable)
+C ObjC C++ ObjC++ Warning Alias(Wunused-const-variable=, 2, 0)
+Warn when a const variable is unused.
+
+Wunused-const-variable=
+C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_unused_const_variable) Warning LangEnabledBy(C ObjC,Wunused-variable, 1, 0)
 Warn when a const variable is unused.
 
 Wvariadic-macros
index 0a745f0f473bc02ba31bcc9cafeb05c9703d7da6..27a073a63be1df1f1a6cd7245f7c95de584d1f04 100644 (file)
@@ -942,7 +942,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 > 0 && TREE_READONLY (decl)
+               && (warn_unused_const_variable == 2
+                   || 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)
@@ -971,7 +974,7 @@ check_global_declaration (symtab_node *snode)
                (TREE_CODE (decl) == FUNCTION_DECL)
                ? OPT_Wunused_function
                : (TREE_READONLY (decl)
-                  ? OPT_Wunused_const_variable
+                  ? OPT_Wunused_const_variable_
                   : OPT_Wunused_variable),
                "%qD defined but not used", decl);
 }
index c1ab7880eae1f132a310849399a3b930a983b0bb..490df938bd803bc69c3152c9d2cb7bedc75013bf 100644 (file)
@@ -303,7 +303,7 @@ Objective-C and Objective-C++ Dialects}.
 -Wunsuffixed-float-constants  -Wunused  -Wunused-function @gol
 -Wunused-label  -Wunused-local-typedefs -Wunused-parameter @gol
 -Wno-unused-result -Wunused-value @gol -Wunused-variable @gol
--Wunused-const-variable @gol
+-Wunused-const-variable -Wunused-const-variable=@var{n} @gol
 -Wunused-but-set-parameter -Wunused-but-set-variable @gol
 -Wuseless-cast -Wvariadic-macros -Wvector-operation-performance @gol
 -Wvla -Wvolatile-register-var  -Wwrite-strings @gol
@@ -4231,23 +4231,39 @@ its return value. The default is @option{-Wunused-result}.
 @opindex Wunused-variable
 @opindex Wno-unused-variable
 Warn whenever a local or static variable is unused aside from its
-declaration. This option implies @option{-Wunused-const-variable} for C,
+declaration. This option implies @option{-Wunused-const-variable=1} for C,
 but not for C++. This warning is enabled by @option{-Wall}.
 
 To suppress this warning use the @code{unused} attribute
 (@pxref{Variable Attributes}).
 
 @item -Wunused-const-variable
+@itemx -Wunused-const-variable=@var{n}
 @opindex Wunused-const-variable
 @opindex Wno-unused-const-variable
 Warn whenever a constant static variable is unused aside from its declaration.
-This warning is enabled by @option{-Wunused-variable} for C, but not for C++.
-In C++ this is normally not an error since const variables take the place of
-@code{#define}s in C++.
+@option{-Wunused-const-variable=1} is enabled by @option{-Wunused-variable}
+for C, but not for C++. In C this declares variable storage, but in C++ this
+is not an error since const variables take the place of @code{#define}s.
 
 To suppress this warning use the @code{unused} attribute
 (@pxref{Variable Attributes}).
 
+@table @gcctabopt
+@item -Wunused-const-variable=1
+This is the warning level that is enabled by @option{-Wunused-variable} for
+C.  It warns only about unused static const variables defined in the main
+compilation unit, but not about static const variables declared in any
+header included.
+
+@item -Wunused-const-variable=2
+This warning level also warns for unused constant static variables in
+headers (excluding system headers).  This is the warning level of
+@option{-Wunused-const-variable} and must be explicitly requested since
+in C++ this isn't an error and in C it might be harder to clean up all
+headers included.
+@end table
+
 @item -Wunused-value
 @opindex Wunused-value
 @opindex Wno-unused-value
index a9529afae69270d0439a4b6110bc7b211ee6ee3d..1868873a914cc60734a6570c8712988e8fe9d5d5 100644 (file)
@@ -1,3 +1,8 @@
+2016-02-20  Mark Wielaard  <mjw@redhat.com>
+
+       PR c/28901
+       * gcc.dg/unused-variable-3.c: New test.
+
 2016-02-22  Jakub Jelinek  <jakub@redhat.com>
 
        PR target/69888
diff --git a/gcc/testsuite/gcc.dg/unused-variable-3.c b/gcc/testsuite/gcc.dg/unused-variable-3.c
new file mode 100644 (file)
index 0000000..6aca958
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-Wunused-variable" } */
+
+static const int cmain = 42;   /* { dg-warning "defined but not used" } */
+
+/* Don't warn for unused static consts in headers,
+   unless -Wunused-const-variable=2.  */
+#line 1 "header.h"
+static const int cheader = 42;