From: Mark Wielaard <mjw@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: "Bernd Schmidt" <bschmidt@redhat.com>,
"Jeff Law" <law@redhat.com>, "Martin Sebor" <msebor@gmail.com>,
sellcey@imgtec.com, "Jakub Jelinek" <jakub@redhat.com>,
"Manuel López-Ibáñez" <lopezibanez@gmail.com>,
"Mark Wielaard" <mjw@redhat.com>
Subject: [PATCH] PR28901 Add two levels for -Wunused-const-variable.
Date: Sun, 21 Feb 2016 01:43:00 -0000 [thread overview]
Message-ID: <1456018952-25270-1-git-send-email-mjw@redhat.com> (raw)
In-Reply-To: <5603E4D3.4050806@redhat.com>
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
header 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.
---
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 638e9c2..7c5f6c7 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -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
diff --git a/gcc/cgraphunit.c b/gcc/cgraphunit.c
index 0a745f0..27a073a 100644
--- a/gcc/cgraphunit.c
+++ b/gcc/cgraphunit.c
@@ -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);
}
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index c1ab788..490df93 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -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
diff --git a/gcc/testsuite/gcc.dg/unused-variable-3.c b/gcc/testsuite/gcc.dg/unused-variable-3.c
new file mode 100644
index 0000000..6aca958
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/unused-variable-3.c
@@ -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;
--
1.8.3.1
next prev parent reply other threads:[~2016-02-21 1:43 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Mark Wielaard [this message]
2016-02-22 18:58 ` [PATCH] PR28901 Add two levels for -Wunused-const-variable 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1456018952-25270-1-git-send-email-mjw@redhat.com \
--to=mjw@redhat.com \
--cc=bschmidt@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
--cc=law@redhat.com \
--cc=lopezibanez@gmail.com \
--cc=msebor@gmail.com \
--cc=sellcey@imgtec.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).