This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
PR 34985: Warning "defined but not used" despite __attribute__((__used__))
- From: "Manuel López-Ibáñez" <lopezibanez at gmail dot com>
- To: "GCC Patches" <gcc-patches at gcc dot gnu dot org>
- Date: Sat, 2 Feb 2008 13:49:24 +0100
- Subject: PR 34985: Warning "defined but not used" despite __attribute__((__used__))
It seems we create two different FUNCTION_DECL trees for the same
function, one for the declaration and another for the definition. When
we check for Wunused warnings, we test decl_minimal.name (DECL_NAME
(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)))
&& ! DECL_IN_SYSTEM_HEADER (decl)
&& ! TREE_USED (decl)
/* The TREE_USED bit for file-scope decls is kept in the identifier,
to handle multiple external decls in different scopes. */
&& ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
[snip]
warning ((TREE_CODE (decl) == FUNCTION_DECL)
? OPT_Wunused_function
: OPT_Wunused_variable,
"%q+D defined but not used", decl);
However, handle_used_attribute only marks whatever tree it receives.
This patch marks also decl_minimal.name to fix PR34985 for both C and
C++.
Bootstrapped and regression tested with --enable-languages=all on
x86_64-unknown-linux-gnu
OK for 4.4 ?
Cheers,
Manuel.
2008-02-02 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
* c-common.c (handle_used_attribute): Mark also the identifier.
testsuite/
* gcc.dg/pr34985.c: New.
* g++.dg/warn/pr34985.C: New.
Index: gcc/testsuite/gcc.dg/pr34985.c
===================================================================
--- gcc/testsuite/gcc.dg/pr34985.c (revision 0)
+++ gcc/testsuite/gcc.dg/pr34985.c (revision 0)
@@ -0,0 +1,7 @@
+/* PR34985: Warning "defined but not used" despite __attribute__((__used__)) */
+/* { dg-do compile } */
+/* { dg-options "-Wall -Wextra" } */
+static void remove_one(void);
+static void __attribute__((__used__)) remove_one(void)
+{
+}
Index: gcc/testsuite/g++.dg/warn/pr34985.C
===================================================================
--- gcc/testsuite/g++.dg/warn/pr34985.C (revision 0)
+++ gcc/testsuite/g++.dg/warn/pr34985.C (revision 0)
@@ -0,0 +1,7 @@
+/* PR34985: Warning "defined but not used" despite __attribute__((__used__)) */
+/* { dg-do compile } */
+/* { dg-options "-Wall -Wextra" } */
+static void remove_one(void);
+static void __attribute__((__used__)) remove_one(void)
+{
+}
Index: gcc/c-common.c
===================================================================
--- gcc/c-common.c (revision 131974)
+++ gcc/c-common.c (working copy)
@@ -4903,10 +4903,14 @@ handle_used_attribute (tree *pnode, tree
if (TREE_CODE (node) == FUNCTION_DECL
|| (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)))
{
TREE_USED (node) = 1;
+ /* The TREE_USED bit for file-scope decls is kept in the identifier,
+ to handle multiple external decls in different scopes. */
+ if (DECL_NAME (node))
+ TREE_USED (DECL_NAME (node)) = 1;
DECL_PRESERVE_P (node) = 1;
}
else
{
warning (OPT_Wattributes, "%qE attribute ignored", name);