]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR rtl-optimization/4330 (Optimizer generates illegal assembly code)
authorRichard Henderson <rth@redhat.com>
Wed, 3 Apr 2002 22:39:51 +0000 (14:39 -0800)
committerRichard Henderson <rth@gcc.gnu.org>
Wed, 3 Apr 2002 22:39:51 +0000 (14:39 -0800)
        PR opt/4330
        * 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.

From-SVN: r51818

13 files changed:
gcc/ChangeLog
gcc/c-lang.c
gcc/c-objc-common.c
gcc/c-tree.h
gcc/cp/ChangeLog
gcc/cp/cp-lang.c
gcc/langhooks-def.h
gcc/langhooks.c
gcc/langhooks.h
gcc/objc/objc-lang.c
gcc/testsuite/g++.dg/warn/Wunused-2.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/unused-4.c [new file with mode: 0644]
gcc/toplev.c

index a5b6c3c73ee2a96ccfb05403113b1a59fec1ee86..758e3a44c9cf00092e6f9a316bcdcb654a592a9c 100644 (file)
@@ -1,3 +1,17 @@
+2002-04-03  Richard Henderson  <rth@redhat.com>
+
+       PR opt/4330
+       * 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.
+
 2002-04-03  Neil Booth  <neil@daikokuya.demon.co.uk>
 
        * langhooks-def.h (lhd_set_decl_assembler_name,
index 278a252bffdea2f3675a1b5cb2b165766fa62ffd..f11dce4a892b19ae64ef6c4275285fe83c375cf6 100644 (file)
@@ -66,6 +66,8 @@ static void c_post_options PARAMS ((void));
 #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 5207abda080dda4cdd7fec990e5c191d971f787e..36b5db4340b9620f14611caaa8cb1c063c6e59a4 100644 (file)
@@ -209,6 +209,20 @@ c_cannot_inline_tree_fn (fnp)
   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 503e04519125f831cd8b04f7568663a411ff8ffd..eef57425d9d58b715afc82c90fc2876537546687 100644 (file)
@@ -237,6 +237,7 @@ extern const char *c_objc_common_init               PARAMS ((const char *));
 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 a7ffd32d62138935cee79147bdd73ae32b6838eb..50dfde814f16160faf04348a21edd705b72fc5c2 100644 (file)
@@ -1,3 +1,8 @@
+2002-04-03  Richard Henderson  <rth@redhat.com>
+
+       * cp-lang.c (cxx_warn_unused_global_decl): New.
+       (LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL): New.
+
 2002-04-03  Neil Booth  <neil@daikokuya.demon.co.uk>
 
        * cp-lang.c (LANG_HOOKS_SET_DECL_ASSEMBLER_NAME): Redefine.
index b7d129a7c216c742e1ac43987b8483d5696a9c66..3c086ca81a867f0b38622bea69c1f7852dbb4520 100644 (file)
@@ -30,6 +30,7 @@ Boston, MA 02111-1307, USA.  */
 
 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++"
@@ -87,6 +88,8 @@ static bool ok_to_generate_alias_set_for_type PARAMS ((tree));
 #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 \
@@ -133,6 +136,47 @@ static bool ok_to_generate_alias_set_for_type PARAMS ((tree));
 /* 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.  */
 
@@ -185,47 +229,6 @@ ok_to_generate_alias_set_for_type (t)
     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
@@ -238,3 +241,21 @@ cxx_get_alias_set (t)
 
   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 5e90a9544ee69e3c07bdf2dcd03e8c4b45eca71b..d073ddf56cab0ef2e30928a9db1fe1656dcdb10e 100644 (file)
@@ -56,6 +56,7 @@ extern rtx lhd_expand_expr PARAMS ((tree, rtx, enum machine_mode, int));
 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 *,
@@ -176,6 +177,7 @@ int lhd_tree_dump_type_quals                        PARAMS ((tree));
 #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, \
@@ -184,7 +186,8 @@ int lhd_tree_dump_type_quals                        PARAMS ((tree));
   LANG_HOOKS_INSERT_BLOCK, \
   LANG_HOOKS_SET_BLOCK, \
   LANG_HOOKS_PUSHDECL, \
-  LANG_HOOKS_GETDECLS \
+  LANG_HOOKS_GETDECLS, \
+  LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL \
 }
 
 /* The whole thing.  The structure is defined in langhooks.h.  */
index 4cdb8abee7864d1c290ff13293dd1b1628f02a8d..128e6de9188f562c327b00e680b4a3e0f04c503d 100644 (file)
@@ -113,6 +113,25 @@ lhd_staticp (exp)
   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 299efcd8a929b00e6a7cfbfc6d61e6679992a421..83e2e5abdb9f1c694c5ed6f1cb6aac730d985b10 100644 (file)
@@ -133,6 +133,10 @@ struct lang_hooks_for_decls
 
   /* 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 991ecd90060c40a124bf2b1b16a1a4eb05950ac6..b1194324f79c2bd8f7b01ba0ead8725594058500 100644 (file)
@@ -68,6 +68,9 @@ static void objc_post_options                   PARAMS ((void));
 #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 \
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-2.C b/gcc/testsuite/g++.dg/warn/Wunused-2.C
new file mode 100644 (file)
index 0000000..9cdd11e
--- /dev/null
@@ -0,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() { }
diff --git a/gcc/testsuite/gcc.dg/unused-4.c b/gcc/testsuite/gcc.dg/unused-4.c
new file mode 100644 (file)
index 0000000..5323600
--- /dev/null
@@ -0,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() { }
index 9fa22fc9cdfa5bb64b35dd70cad8ea7874d07342..b62aae35e1f3ebbfe69e79b1ac92fccc4fd69aeb 100644 (file)
@@ -1945,22 +1945,19 @@ check_global_declarations (vec, len)
          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)
+      /* 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)
-         && ! 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)))
+         /* 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);