]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c,c++: Fix incorrect warning with asm defined symbols
authorMichal Jires <mjires@suse.cz>
Fri, 30 Jan 2026 15:42:48 +0000 (16:42 +0100)
committerMichal Jires <mjires@suse.cz>
Thu, 5 Feb 2026 14:42:35 +0000 (15:42 +0100)
Static symbols defined in assembly cause wrong "used but never defined"
warning.

static void asm_fn();
asm("%cc0:" :: ":"(&asm_fn));

This happens in C,C++ frontends before cgraph is created where the
relevant flags are located.

We can suppress these warnings with OPT_Wunused.
C,C++ frontends and cgraphunit suppressed OPT_Wunused and
OPT_Wunused_function interchangeably, so we unify suppression to
only OPT_Wunused.

PR testsuite/123559

gcc/c/ChangeLog:

* c-decl.cc (c_write_global_declarations_1): Check and suppress
OPT_Wunused.
* c-typeck.cc (build_asm_expr): Suppress OPT_Wunused.

gcc/ChangeLog:

* cgraphunit.cc (check_global_declaration): Suppress OPT_Wunused.

gcc/cp/ChangeLog:

* decl.cc (wrapup_namespace_globals): Check and suppress OPT_Wunused.
* semantics.cc (finish_asm_stmt): Suppress OPT_Wunused.

gcc/testsuite/ChangeLog:

* c-c++-common/toplevel-extended-asm-1.c: New test.

gcc/c/c-decl.cc
gcc/c/c-typeck.cc
gcc/cgraphunit.cc
gcc/cp/decl.cc
gcc/cp/semantics.cc
gcc/testsuite/c-c++-common/toplevel-extended-asm-1.c [new file with mode: 0644]

index 7dddc23b5e1785d9ea27f732626c25b9581f3108..ea0eaec0bb5d31bfb45ca49f6f9525f28d57551a 100644 (file)
@@ -13679,25 +13679,22 @@ c_write_global_declarations_1 (tree globals)
       if (TREE_CODE (decl) == FUNCTION_DECL
          && DECL_INITIAL (decl) == NULL_TREE
          && DECL_EXTERNAL (decl)
-         && !TREE_PUBLIC (decl))
+         && !TREE_PUBLIC (decl)
+         && !warning_suppressed_p (decl, OPT_Wunused))
        {
          if (C_DECL_USED (decl))
            {
-             /* TODO: Add OPT_Wundefined-inline.  */
              if (pedwarn (input_location, 0, "%q+F used but never defined",
                           decl))
-               suppress_warning (decl /* OPT_Wundefined-inline.  */);
+               suppress_warning (decl, OPT_Wunused);
            }
          /* For -Wunused-function warn about unused static prototypes.  */
          else if (warn_unused_function
                   && ! DECL_ARTIFICIAL (decl)
-                  && ! warning_suppressed_p (decl, OPT_Wunused_function))
-           {
-             if (warning (OPT_Wunused_function,
-                          "%q+F declared %<static%> but never defined",
-                          decl))
-               suppress_warning (decl, OPT_Wunused_function);
-           }
+                  && warning (OPT_Wunused_function,
+                              "%q+F declared %<static%> but never defined",
+                              decl))
+           suppress_warning (decl, OPT_Wunused);
        }
 
       wrapup_global_declaration_1 (decl);
index 098a4b553399d2e20ae8d833b8225e760de77874..dc803dc6388a013a324ac9572d10206ca5a1ae14 100644 (file)
@@ -13170,6 +13170,8 @@ build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
                                 "of a function or non-automatic variable");
                  input = error_mark_node;
                }
+             else if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
+               suppress_warning (TREE_OPERAND (t, 0), OPT_Wunused);
            }
        }
       else
index 88c1071c8de918e1e5e80953f014cd53dd72c9bc..b0a0061abb5d02aa9a38aed52f712f471f0c2871 100644 (file)
@@ -1111,10 +1111,13 @@ check_global_declaration (symtab_node *snode)
       if (warning_suppressed_p (decl, OPT_Wunused))
        ;
       else if (snode->referred_to_p (/*include_self=*/false))
-       pedwarn (input_location, 0, "%q+F used but never defined", decl);
-      else
-       warning (OPT_Wunused_function, "%q+F declared %<static%> but never "
-                                      "defined", decl);
+       {
+         if (pedwarn (input_location, 0, "%q+F used but never defined", decl))
+           suppress_warning (decl, OPT_Wunused);
+       }
+      else if (warning (OPT_Wunused_function,
+                       "%q+F declared %<static%> but never defined", decl))
+       suppress_warning (decl, OPT_Wunused);
     }
 
   /* Warn about static fns or vars defined but not used.  */
index 6dabf349befb725b577265aae846f3cbd15458b1..c6aac6a8779e8148c0247420e03be7ab17ef04b7 100644 (file)
@@ -1015,10 +1015,11 @@ wrapup_namespace_globals ()
              && !TREE_PUBLIC (decl)
              && !DECL_ARTIFICIAL (decl)
              && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
-             && !warning_suppressed_p (decl, OPT_Wunused_function))
-           warning_at (DECL_SOURCE_LOCATION (decl),
-                       OPT_Wunused_function,
-                       "%qF declared %<static%> but never defined", decl);
+             && !warning_suppressed_p (decl, OPT_Wunused)
+             && warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_function,
+                            "%qF declared %<static%> but never defined",
+                            decl))
+               suppress_warning (decl, OPT_Wunused);
 
          if (VAR_P (decl)
              && DECL_EXTERNAL (decl)
index fb1be25edf5f5201aa5eb1b50b54d6bb39f6bdeb..236bc625c25d204083971d36770b63eceb65ee46 100644 (file)
@@ -2495,6 +2495,8 @@ finish_asm_stmt (location_t loc, int volatile_p, tree string,
                                "of a function or non-automatic variable");
                      operand = error_mark_node;
                    }
+                 else if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
+                   suppress_warning (TREE_OPERAND (t, 0), OPT_Wunused);
                }
            }
          else
diff --git a/gcc/testsuite/c-c++-common/toplevel-extended-asm-1.c b/gcc/testsuite/c-c++-common/toplevel-extended-asm-1.c
new file mode 100644 (file)
index 0000000..531c942
--- /dev/null
@@ -0,0 +1,5 @@
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+
+static void asm_fn(); /* { dg-bogus "but never defined" } */
+asm("%cc0:" :: ":"(&asm_fn));