diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 91599658be9650a887e88774a5dc0c5502bb2301..e537d33f3986083c039f671bcf5eb4c6f0408300 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -1310,7 +1310,10 @@ pop_scope (void) && scope != external_scope) { if (!TREE_USED (p)) - warning (OPT_Wunused_variable, "unused variable %q+D", p); + { + warning (OPT_Wunused_variable, "unused variable %q+D", p); + suppress_warning (p, OPT_Wunused_variable); + } else if (DECL_CONTEXT (p) == current_function_decl) warning_at (DECL_SOURCE_LOCATION (p), OPT_Wunused_but_set_variable, diff --git a/gcc/cgraphunit.cc b/gcc/cgraphunit.cc index a972900900b71bfe48db39783f0b17a3fb2d6ec7..e12764f2f906ec3f605ca43d9d1870d68179def9 100644 --- a/gcc/cgraphunit.cc +++ b/gcc/cgraphunit.cc @@ -1120,6 +1120,7 @@ check_global_declaration (symtab_node *snode) && (TREE_CODE (decl) != FUNCTION_DECL || (!DECL_STATIC_CONSTRUCTOR (decl) && !DECL_STATIC_DESTRUCTOR (decl))) + && (! VAR_P (decl) || !warning_suppressed_p (decl, OPT_Wunused_variable)) /* Otherwise, ask the language. */ && lang_hooks.decls.warn_unused_global (decl)) warning_at (DECL_SOURCE_LOCATION (decl), diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index b1603859644fe40a989bbfebacb9aa29584847ff..51199bb311a89d2ab1805faf168499f61a49dbc3 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -693,6 +693,7 @@ poplevel (int keep, int reverse, int functionbody) else warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_variable, "unused variable %qD", decl); + suppress_warning (decl, OPT_Wunused_variable); } else if (DECL_CONTEXT (decl) == current_function_decl // For -Wunused-but-set-variable leave references alone. diff --git a/gcc/testsuite/c-c++-common/Wunused-var-18.c b/gcc/testsuite/c-c++-common/Wunused-var-18.c new file mode 100644 index 0000000000000000000000000000000000000000..5ff772b74c1d18a07418aebfb4fb8dcc196375c6 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wunused-var-18.c @@ -0,0 +1,10 @@ +/* PR c/108079 */ +/* { dg-do compile } */ +/* { dg-options "-Wunused-variable" } */ + +int +main () +{ + static int x; /* { dg-warning "unused variable 'x'" } */ + /* { dg-bogus "'x' defined but not used" "" { target *-*-* } .-1 } */ +}