fix opt/3569
Richard Henderson
rth@redhat.com
Wed Apr 3 14:49:00 GMT 2002
The problem here is that DECL_INLINE is set whenever we wish the optimizer
to try to inline, i.e. basically always with -finline-functions. Instead
we should be testing DECL_DECLARED_INLINE_P, but that's private to the C
and C++ front ends (differently defined for the two, no less).
So I've split the bulk of the "should we warn" test out to a lang hook.
In addition, while "const int i = 1" is something you don't want to warn
about in C++, for plain C this allocates storage. In fact, I'd be willing
to lay money that it's a C++ programmer who forgot that difference between
the two languages.
The new C++ test currently fails due to the issue discussed here:
http://gcc.gnu.org/ml/gcc/2002-04/msg00106.html
I'm going to let someone else take care of that. Hopefully it's
a simple matter of forgetting to test DECL_DECLARED_INLINE_P
instead of DECL_INLINE somewhere in the C++ front end.
Applied to mainline only.
r~
* langhooks.h (lang_hooks.decls.warn_unused_global): New.
* toplev.c (check_global_declarations): Use it.
* langhooks-def.h (lhd_warn_unused_global_decl): Declare.
(LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL): New.
(LANG_HOOKS_DECLS): Add it.
* langhooks.c (lhd_warn_unused_global_decl): New.
* c-decl.c (LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL): New.
* c-objc-common.c (c_warn_unused_global_decl): New.
* c-tree.h (c_warn_unused_global_decl): Declare.
* objc/objc-lang.c (LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL): New.
* cp-lang.c (cxx_warn_unused_global_decl): New.
(LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL): New.
* g++.dg/warn/Wunused-2.C: New.
* gcc.dg/unused-4.c: New.
Index: c-lang.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-lang.c,v
retrieving revision 1.85
diff -c -p -d -r1.85 c-lang.c
*** c-lang.c 2002/04/01 20:25:57 1.85
--- c-lang.c 2002/04/03 22:30:38
*************** static void c_post_options PARAMS ((void
*** 66,71 ****
--- 66,73 ----
#define LANG_HOOKS_UNSAFE_FOR_REEVAL c_common_unsafe_for_reeval
#undef LANG_HOOKS_STATICP
#define LANG_HOOKS_STATICP c_staticp
+ #undef LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
+ #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL c_warn_unused_global_decl
#undef LANG_HOOKS_PRINT_IDENTIFIER
#define LANG_HOOKS_PRINT_IDENTIFIER c_print_identifier
#undef LANG_HOOKS_SET_YYDEBUG
Index: c-objc-common.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-objc-common.c,v
retrieving revision 1.10
diff -c -p -d -r1.10 c-objc-common.c
*** c-objc-common.c 2002/03/31 22:40:42 1.10
--- c-objc-common.c 2002/04/03 22:30:38
*************** c_cannot_inline_tree_fn (fnp)
*** 209,214 ****
--- 209,228 ----
return 0;
}
+ /* Called from check_global_declarations. */
+
+ bool
+ c_warn_unused_global_decl (decl)
+ tree decl;
+ {
+ if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
+ return false;
+ if (DECL_IN_SYSTEM_HEADER (decl))
+ return false;
+
+ return true;
+ }
+
/* Initialization common to C and Objective-C front ends. */
const char *
c_objc_common_init (filename)
Index: c-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-tree.h,v
retrieving revision 1.91
diff -c -p -d -r1.91 c-tree.h
*** c-tree.h 2002/04/01 20:25:57 1.91
--- c-tree.h 2002/04/03 22:30:38
*************** extern const char *c_objc_common_init P
*** 237,242 ****
--- 237,243 ----
extern int c_missing_noreturn_ok_p PARAMS ((tree));
extern void c_objc_common_finish_file PARAMS ((void));
extern int defer_fn PARAMS ((tree));
+ extern bool c_warn_unused_global_decl PARAMS ((tree));
#define c_build_type_variant(TYPE, CONST_P, VOLATILE_P) \
c_build_qualified_type ((TYPE), \
Index: langhooks-def.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/langhooks-def.h,v
retrieving revision 1.25
diff -c -p -d -r1.25 langhooks-def.h
*** langhooks-def.h 2002/04/03 22:26:07 1.25
--- langhooks-def.h 2002/04/03 22:30:38
*************** extern rtx lhd_expand_expr PARAMS ((tree
*** 56,61 ****
--- 56,62 ----
extern void lhd_print_error_function PARAMS ((struct diagnostic_context *,
const char *));
extern void lhd_set_decl_assembler_name PARAMS ((tree));
+ extern bool lhd_warn_unused_global_decl PARAMS ((tree));
/* Declarations of default tree inlining hooks. */
tree lhd_tree_inlining_walk_subtrees PARAMS ((tree *, int *,
*************** int lhd_tree_dump_type_quals PARAMS ((
*** 176,181 ****
--- 177,183 ----
#define LANG_HOOKS_SET_BLOCK set_block
#define LANG_HOOKS_PUSHDECL pushdecl
#define LANG_HOOKS_GETDECLS getdecls
+ #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL lhd_warn_unused_global_decl
#define LANG_HOOKS_DECLS { \
LANG_HOOKS_PUSHLEVEL, \
*************** int lhd_tree_dump_type_quals PARAMS ((
*** 184,190 ****
LANG_HOOKS_INSERT_BLOCK, \
LANG_HOOKS_SET_BLOCK, \
LANG_HOOKS_PUSHDECL, \
! LANG_HOOKS_GETDECLS \
}
/* The whole thing. The structure is defined in langhooks.h. */
--- 186,193 ----
LANG_HOOKS_INSERT_BLOCK, \
LANG_HOOKS_SET_BLOCK, \
LANG_HOOKS_PUSHDECL, \
! LANG_HOOKS_GETDECLS, \
! LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL \
}
/* The whole thing. The structure is defined in langhooks.h. */
Index: langhooks.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/langhooks.c,v
retrieving revision 1.25
diff -c -p -d -r1.25 langhooks.c
*** langhooks.c 2002/04/03 22:26:09 1.25
--- langhooks.c 2002/04/03 22:30:38
*************** lhd_staticp (exp)
*** 113,118 ****
--- 113,137 ----
return 0;
}
+ /* Called from check_global_declarations. */
+
+ bool
+ lhd_warn_unused_global_decl (decl)
+ tree decl;
+ {
+ /* This is what used to exist in check_global_declarations. Probably
+ not many of these actually apply to non-C languages. */
+
+ if (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))
+ return false;
+ if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
+ return false;
+ if (DECL_IN_SYSTEM_HEADER (decl))
+ return false;
+
+ return true;
+ }
+
/* Called when -dy is given on the command line. */
void
Index: langhooks.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/langhooks.h,v
retrieving revision 1.32
diff -c -p -d -r1.32 langhooks.h
*** langhooks.h 2002/04/03 22:26:09 1.32
--- langhooks.h 2002/04/03 22:30:38
*************** struct lang_hooks_for_decls
*** 133,138 ****
--- 133,142 ----
/* Returns the chain of decls so far in the current scope level. */
tree (*getdecls) PARAMS ((void));
+
+ /* Returns true when we should warn for an unused global DECL.
+ We will already have checked that it has static binding. */
+ bool (*warn_unused_global) PARAMS ((tree));
};
/* Language-specific hooks. See langhooks-def.h for defaults. */
Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.609
diff -c -p -d -r1.609 toplev.c
*** toplev.c 2002/04/03 07:56:47 1.609
--- toplev.c 2002/04/03 22:30:39
*************** check_global_declarations (vec, len)
*** 1945,1966 ****
assemble_external (decl);
}
! /* Warn about static fns or vars defined but not used,
! but not about inline functions or static consts
! since defining those in header files is normal practice. */
! if (((warn_unused_function
! && TREE_CODE (decl) == FUNCTION_DECL && ! DECL_INLINE (decl))
! || (warn_unused_variable
! && TREE_CODE (decl) == VAR_DECL && ! TREE_READONLY (decl)))
! && ! DECL_IN_SYSTEM_HEADER (decl)
&& ! DECL_EXTERNAL (decl)
&& ! TREE_PUBLIC (decl)
! && ! TREE_USED (decl)
! && (TREE_CODE (decl) == FUNCTION_DECL || ! DECL_REGISTER (decl))
! /* The TREE_USED bit for file-scope decls
! is kept in the identifier, to handle multiple
! external decls in different scopes. */
! && ! TREE_USED (DECL_NAME (decl)))
warning_with_decl (decl, "`%s' defined but not used");
timevar_push (TV_SYMOUT);
--- 1945,1963 ----
assemble_external (decl);
}
! /* Warn about static fns or vars defined but not used. */
! if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
! || (warn_unused_variable && TREE_CODE (decl) == VAR_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. */
! && ! TREE_USED (DECL_NAME (decl))
&& ! DECL_EXTERNAL (decl)
&& ! TREE_PUBLIC (decl)
! /* Global register variables must be declared to reserve them. */
! && ! (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
! /* Otherwise, ask the language. */
! && (*lang_hooks.decls.warn_unused_global) (decl))
warning_with_decl (decl, "`%s' defined but not used");
timevar_push (TV_SYMOUT);
Index: cp/cp-lang.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-lang.c,v
retrieving revision 1.26
diff -c -p -d -r1.26 cp-lang.c
*** cp-lang.c 2002/04/03 22:26:36 1.26
--- cp-lang.c 2002/04/03 22:30:39
*************** Boston, MA 02111-1307, USA. */
*** 30,35 ****
--- 30,36 ----
static HOST_WIDE_INT cxx_get_alias_set PARAMS ((tree));
static bool ok_to_generate_alias_set_for_type PARAMS ((tree));
+ static bool cxx_warn_unused_global_decl PARAMS ((tree));
#undef LANG_HOOKS_NAME
#define LANG_HOOKS_NAME "GNU C++"
*************** static bool ok_to_generate_alias_set_for
*** 87,92 ****
--- 88,95 ----
#define LANG_HOOKS_PRINT_ERROR_FUNCTION cxx_print_error_function
#undef LANG_HOOKS_SET_YYDEBUG
#define LANG_HOOKS_SET_YYDEBUG cxx_set_yydebug
+ #undef LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
+ #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL cxx_warn_unused_global_decl
#undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
#define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES \
*************** static bool ok_to_generate_alias_set_for
*** 133,138 ****
--- 136,182 ----
/* Each front end provides its own hooks, for toplev.c. */
const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
+ /* Tree code classes. */
+
+ #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
+
+ const char tree_code_type[] = {
+ #include "tree.def"
+ 'x',
+ #include "c-common.def"
+ 'x',
+ #include "cp-tree.def"
+ };
+ #undef DEFTREECODE
+
+ /* Table indexed by tree code giving number of expression
+ operands beyond the fixed part of the node structure.
+ Not used for types or decls. */
+
+ #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
+
+ const unsigned char tree_code_length[] = {
+ #include "tree.def"
+ 0,
+ #include "c-common.def"
+ 0,
+ #include "cp-tree.def"
+ };
+ #undef DEFTREECODE
+
+ /* Names of tree components.
+ Used for printing out the tree and error messages. */
+ #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
+
+ const char *const tree_code_name[] = {
+ #include "tree.def"
+ "@@dummy",
+ #include "c-common.def"
+ "@@dummy",
+ #include "cp-tree.def"
+ };
+ #undef DEFTREECODE
+
/* Check if a C++ type is safe for aliasing.
Return TRUE if T safe for aliasing FALSE otherwise. */
*************** ok_to_generate_alias_set_for_type (t)
*** 185,231 ****
return true;
}
- /* Tree code classes. */
-
- #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
-
- const char tree_code_type[] = {
- #include "tree.def"
- 'x',
- #include "c-common.def"
- 'x',
- #include "cp-tree.def"
- };
- #undef DEFTREECODE
-
- /* Table indexed by tree code giving number of expression
- operands beyond the fixed part of the node structure.
- Not used for types or decls. */
-
- #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
-
- const unsigned char tree_code_length[] = {
- #include "tree.def"
- 0,
- #include "c-common.def"
- 0,
- #include "cp-tree.def"
- };
- #undef DEFTREECODE
-
- /* Names of tree components.
- Used for printing out the tree and error messages. */
- #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
-
- const char *const tree_code_name[] = {
- #include "tree.def"
- "@@dummy",
- #include "c-common.def"
- "@@dummy",
- #include "cp-tree.def"
- };
- #undef DEFTREECODE
-
/* Special routine to get the alias set for C++. */
static HOST_WIDE_INT
--- 229,234 ----
*************** cxx_get_alias_set (t)
*** 237,240 ****
--- 240,261 ----
return 0;
return c_common_get_alias_set (t);
+ }
+
+ /* Called from check_global_declarations. */
+
+ static bool
+ cxx_warn_unused_global_decl (decl)
+ tree decl;
+ {
+ if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
+ return false;
+ if (DECL_IN_SYSTEM_HEADER (decl))
+ return false;
+
+ /* Const variables take the place of #defines in C++. */
+ if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
+ return false;
+
+ return true;
}
Index: objc/objc-lang.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/objc/objc-lang.c,v
retrieving revision 1.14
diff -c -p -d -r1.14 objc-lang.c
*** objc-lang.c 2002/04/01 20:26:18 1.14
--- objc-lang.c 2002/04/03 22:30:39
*************** static void objc_post_options
*** 68,73 ****
--- 68,76 ----
#define LANG_HOOKS_DECL_PRINTABLE_NAME objc_printable_name
#undef LANG_HOOKS_SET_YYDEBUG
#define LANG_HOOKS_SET_YYDEBUG c_set_yydebug
+ #undef LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
+ #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL c_warn_unused_global_decl
+
/* Inlining hooks same as the C front end. */
#undef LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN
#define LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN \
Index: testsuite/g++.dg/warn/Wunused-2.C
===================================================================
RCS file: Wunused-2.C
diff -N Wunused-2.C
*** /dev/null Tue May 5 13:32:27 1998
--- Wunused-2.C Wed Apr 3 14:30:40 2002
***************
*** 0 ****
--- 1,6 ----
+ // { dg-do compile }
+ // { dg-options "-Wunused -O3" }
+
+ static const int i = 0;
+ static void f() { } /* { dg-warning "defined but not used" } */
+ static inline void g() { }
Index: testsuite/gcc.dg/unused-4.c
===================================================================
RCS file: unused-4.c
diff -N unused-4.c
*** /dev/null Tue May 5 13:32:27 1998
--- unused-4.c Wed Apr 3 14:30:41 2002
***************
*** 0 ****
--- 1,6 ----
+ /* { dg-do compile } */
+ /* { dg-options "-Wunused -O3" } */
+
+ static const int i = 0; /* { dg-warning "defined but not used" } */
+ static void f() { } /* { dg-warning "defined but not used" } */
+ static inline void g() { }
More information about the Gcc-patches
mailing list