public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR28901 -Wunused-variable ignores unused const initialised variables
@ 2015-09-11 22:29 Mark Wielaard
  2015-09-11 22:40 ` Bernd Schmidt
  0 siblings, 1 reply; 43+ messages in thread
From: Mark Wielaard @ 2015-09-11 22:29 UTC (permalink / raw)
  To: gcc-patches; +Cc: Mark Wielaard

12 years ago it was decided that -Wunused-variable shouldn't warn about
static const variables because some code used const static char rcsid[]
strings which were never used but wanted in the code anyway. But as the
bug points out this hides some real bugs. These days the usage of rcsids
is not very popular anymore. So this patch changes the default to warn
about unused static const variables with -Wunused-variable. And it adds
a new option -Wno-unused-const-variable to turn this warning off. New
testcases are included to test the new warning with -Wunused-variable
and suppressing it with -Wno-unused-const-variable or unused attribute.

gcc/ChangeLog

       PR c/28901
       * common.opt (Wunused-const-variable): New option.
       * toplev.c (check_global_declaration): Check and use
       warn_unused_const_variable.
       * doc/invoke.texi (Warning Options): Add -Wunused-const-variable.
       (-Wunused-variable): Remove non-constant. Implies
       -Wunused-const-variable.
       (-Wunused-const-variable): New.

gcc/testsuite/ChangeLog

       PR c/28901
       * gcc.dg/unused-4.c: Adjust warning for static const.
       * gcc.dg/unused-variable-1.c: New test.
       * gcc.dg/unused-variable-2.c: Likewise.

Tested on x86_64-pc-linux-gnu, no regressions.

---

diff --git a/gcc/common.opt b/gcc/common.opt
index 94d1d88..ae2fe77 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -735,6 +735,10 @@ Wunused-variable
 Common Var(warn_unused_variable) Warning EnabledBy(Wunused)
 Warn when a variable is unused
 
+Wunused-const-variable
+Common Var(warn_unused_const_variable) Warning EnabledBy(Wunused-variable)
+Warn when a const variable is unused
+
 Wcoverage-mismatch
 Common Var(warn_coverage_mismatch) Init(1) Warning
 Warn in case profiles in -fprofile-use do not match
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 518d689..211b9e1 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -290,6 +290,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-but-set-parameter -Wunused-but-set-variable @gol
 -Wuseless-cast -Wvariadic-macros -Wvector-operation-performance @gol
 -Wvla -Wvolatile-register-var  -Wwrite-strings @gol
@@ -4143,13 +4144,22 @@ its return value. The default is @option{-Wunused-result}.
 @item -Wunused-variable
 @opindex Wunused-variable
 @opindex Wno-unused-variable
-Warn whenever a local variable or non-constant static variable is unused
-aside from its declaration.
+Warn whenever a local or static variable is unused aside from its
+declaration. This option implies @option{-Wunused-const-variable}.
 This warning is enabled by @option{-Wall}.
 
 To suppress this warning use the @code{unused} attribute
 (@pxref{Variable Attributes}).
 
+@item -Wunused-const-variable
+@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}.
+
+To suppress this warning use the @code{unused} attribute
+(@pxref{Variable Attributes}).
+
 @item -Wunused-value
 @opindex Wunused-value
 @opindex Wno-unused-value

diff --git a/gcc/testsuite/gcc.dg/unused-4.c b/gcc/testsuite/gcc.dg/unused-4.c
index 99e845f..5323600 100644
--- a/gcc/testsuite/gcc.dg/unused-4.c
+++ b/gcc/testsuite/gcc.dg/unused-4.c
@@ -1,6 +1,6 @@
 /* { dg-do compile } */
 /* { dg-options "-Wunused -O3" } */
 
-static const int i = 0;
+static const int i = 0;		/* { dg-warning "defined but not used" } */
 static void f() { }		/* { dg-warning "defined but not used" } */
 static inline void g() { }
diff --git a/gcc/testsuite/gcc.dg/unused-variable-1.c b/gcc/testsuite/gcc.dg/unused-variable-1.c
new file mode 100644
index 0000000..cb86c3bc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/unused-variable-1.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-Wunused-variable" } */
+
+static int a = 0;	  /* { dg-warning "defined but not used" } */
+static const int b = 0;	  /* { dg-warning "defined but not used" } */
+static int c __attribute__ ((unused)) = 0;
+static const char rcsid[] __attribute__ ((unused)) = "version-string";
diff --git a/gcc/testsuite/gcc.dg/unused-variable-2.c b/gcc/testsuite/gcc.dg/unused-variable-2.c
new file mode 100644
index 0000000..0496466
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/unused-variable-2.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-Wunused-variable -Wno-unused-const-variable" } */
+
+static int a = 0;	  /* { dg-warning "defined but not used" } */
+static const int b = 0;
+static int c __attribute__ ((unused)) = 0;
+static const char rcsid[] = "version-string";
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 926224a..95e4c52 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -497,10 +497,9 @@ check_global_declaration (tree decl)
 
   /* Warn about static fns or vars defined but not used.  */
   if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
-       /* We don't warn about "static const" variables because the
-	  "rcs_id" idiom uses that construction.  */
-       || (warn_unused_variable
-	   && TREE_CODE (decl) == VAR_DECL && ! TREE_READONLY (decl)))
+       || (((warn_unused_variable && ! TREE_READONLY (decl))
+	    || (warn_unused_const_variable && TREE_READONLY (decl)))
+	   && TREE_CODE (decl) == VAR_DECL))
       && ! DECL_IN_SYSTEM_HEADER (decl)
       && ! snode->referred_to_p (/*include_self=*/false)
       /* This TREE_USED check is needed in addition to referred_to_p
@@ -527,7 +526,9 @@ check_global_declaration (tree decl)
     warning_at (DECL_SOURCE_LOCATION (decl),
 		(TREE_CODE (decl) == FUNCTION_DECL)
 		? OPT_Wunused_function
-		: OPT_Wunused_variable,
+		: (TREE_READONLY (decl)
+		   ? OPT_Wunused_const_variable
+		   : OPT_Wunused_variable),
 		"%qD defined but not used", decl);
 }
 

^ permalink raw reply	[flat|nested] 43+ messages in thread

end of thread, other threads:[~2016-02-23 12:10 UTC | newest]

Thread overview: 43+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-11 22:29 [PATCH] PR28901 -Wunused-variable ignores unused const initialised variables Mark Wielaard
2015-09-11 22:40 ` Bernd Schmidt
2015-09-13  5:44   ` Mark Wielaard
2015-09-13 12:51     ` Mark Wielaard
2015-09-13 13:36       ` Manuel López-Ibáñez
2015-09-13 18:50         ` Mark Wielaard
2015-09-14  7:54           ` Bernd Schmidt
2015-09-15 17:04             ` Steve Ellcey
2015-09-15 17:10               ` Jakub Jelinek
2015-09-15 17:26                 ` Steve Ellcey
2015-09-15 18:55                   ` Mark Wielaard
2015-09-15 19:21                     ` Mark Wielaard
2015-09-15 19:58                     ` Joseph Myers
2015-09-19  2:57                   ` Martin Sebor
2015-09-21 17:10                     ` Steve Ellcey
2015-09-23 18:26                     ` Jeff Law
2015-09-24 12:54                       ` Mark Wielaard
2015-09-24 13:06                         ` Bernd Schmidt
2015-09-24 16:38                           ` Steve Ellcey
2015-09-24 18:40                             ` Bernd Schmidt
2015-09-25  8:00                               ` Trevor Saunders
2015-10-06 22:44                                 ` Steve Ellcey
2015-10-07 12:00                                   ` Bernd Schmidt
2015-10-24 15:00                                     ` Gerald Pfeifer
2015-10-24 15:11                                       ` Mark Wielaard
2016-02-21  1:43                           ` [PATCH] PR28901 Add two levels for -Wunused-const-variable Mark Wielaard
2016-02-22 18:58                             ` Jeff Law
2016-02-22 19:00                               ` Jakub Jelinek
2016-02-22 19:02                                 ` Jeff Law
2016-02-22 22:43                               ` Mark Wielaard
2016-02-23  3:21                                 ` H.J. Lu
2016-02-23  7:55                                   ` Mark Wielaard
2016-02-23  8:27                                     ` Jakub Jelinek
2016-02-23  8:54                                       ` Mark Wielaard
2016-02-23  8:57                                         ` Jakub Jelinek
2016-02-23  9:51                                           ` Manuel López-Ibáñez
2016-02-23  9:55                                             ` Jakub Jelinek
2016-02-23 12:10                                             ` Mark Wielaard
2015-09-15 17:20               ` [PATCH] PR28901 -Wunused-variable ignores unused const initialised variables Joseph Myers
2015-09-17 12:41       ` Gerald Pfeifer
2015-09-17 16:27         ` Bernd Schmidt
2015-09-17 16:32           ` Mark Wielaard
2015-10-23 23:41             ` Gerald Pfeifer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).