]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
c, c++, cgraphunit: Prevent duplicated -Wunused-value warnings [PR108079]
authorJakub Jelinek <jakub@redhat.com>
Fri, 10 Mar 2023 09:10:24 +0000 (10:10 +0100)
committerJakub Jelinek <jakub@redhat.com>
Sun, 19 Mar 2023 05:28:56 +0000 (06:28 +0100)
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.

(cherry picked from commit 2c63cc7268fd5615997989f153e9405d0f5aaa50)

gcc/c/c-decl.cc
gcc/cgraphunit.cc
gcc/cp/decl.cc
gcc/testsuite/c-c++-common/Wunused-var-18.c [new file with mode: 0644]

index c701f07befe687ecc5364f5b1e00343db402b8f0..619a20909379e6de0b8217801dba0b47e7ebc32e 100644 (file)
@@ -1306,7 +1306,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,
index 76d541755b80d6d28ba29289508a3458110d5cc7..5aa7b57c9e15325053ba1883320ff4455a6f98a6 100644 (file)
@@ -1123,6 +1123,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),
index 012eac5096d61c4a3ba5895a8a974fa9840b0f1f..0f25bb5794673c5070841ef3c7a07f19e7757852 100644 (file)
@@ -691,6 +691,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 (file)
index 0000000..5ff772b
--- /dev/null
@@ -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 } */
+}