public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-6575] c, c++, cgraphunit: Prevent duplicated -Wunused-value warnings [PR108079]
Date: Fri, 10 Mar 2023 09:15:55 +0000 (GMT)	[thread overview]
Message-ID: <20230310091555.557153858C5E@sourceware.org> (raw)

https://gcc.gnu.org/g:2c63cc7268fd5615997989f153e9405d0f5aaa50

commit r13-6575-g2c63cc7268fd5615997989f153e9405d0f5aaa50
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Mar 10 10:10:24 2023 +0100

    c, c++, cgraphunit: Prevent duplicated -Wunused-value warnings [PR108079]
    
    On the following testcase, we warn with -Wunused-value twice, once
    in the FEs and later on cgraphunit again with slightly different
    wording.
    
    The following patch fixes that by registering a warning suppression in the
    FEs when we warn and not warning in cgraphunit anymore if that happened.
    
    2023-03-10  Jakub Jelinek  <jakub@redhat.com>
    
            PR c/108079
    gcc/
            * cgraphunit.cc (check_global_declaration): Don't warn for unused
            variables which have OPT_Wunused_variable warning suppressed.
    gcc/c/
            * c-decl.cc (pop_scope): Suppress OPT_Wunused_variable warning
            after diagnosing it.
    gcc/cp/
            * decl.cc (poplevel): Suppress OPT_Wunused_variable warning
            after diagnosing it.
    gcc/testsuite/
            * c-c++-common/Wunused-var-18.c: New test.

Diff:
---
 gcc/c/c-decl.cc                             |  5 ++++-
 gcc/cgraphunit.cc                           |  1 +
 gcc/cp/decl.cc                              |  1 +
 gcc/testsuite/c-c++-common/Wunused-var-18.c | 10 ++++++++++
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 91599658be9..e537d33f398 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 a972900900b..e12764f2f90 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 b1603859644..51199bb311a 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 00000000000..5ff772b74c1
--- /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 } */
+}

                 reply	other threads:[~2023-03-10  9:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230310091555.557153858C5E@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).