https://gcc.gnu.org/g:4b39b84072e9a57e19761583950fe96b2611720e

commit r17-1153-g4b39b84072e9a57e19761583950fe96b2611720e
Author: H.J. Lu <[email protected]>
Date:   Wed May 6 20:58:47 2026 +0800

    c/c++: Declare stack protection guard in file scope
    
    When stack protection guard is declared in c_common_nodes_and_builtins,
    it is put in the externals scope outside file scope which triggers
    -Wnested-externs warning in C:
    
    cc1: warning: nested extern declaration of '__stack_chk_guard' 
[-Wnested-externs]
    
    Declare stack protection guard in file scope instead.
    
    gcc/c-family/
    
            PR c/121911
            * c-common.cc (c_common_nodes_and_builtins): Don't declare stack
            protection guard here.
    
    gcc/c/
    
            PR c/121911
            * c-decl.cc (push_file_scope): Add stack protection guard to
            visible_builtins.
    
    gcc/cp/
    
            PR c/121911
            * decl.cc (cxx_init_decl_processing ): Declare stack protection
            guard after calling c_common_nodes_and_builtins.
    
    gcc/testsuite/
    
            PR c/121911
            * gcc.dg/Wnested-externs-1.c: Add -mstack-protector-guard=global
            for x86.
    
    Signed-off-by: H.J. Lu <[email protected]>

Diff:
---
 gcc/c-family/c-common.cc                 | 5 -----
 gcc/c/c-decl.cc                          | 9 +++++++++
 gcc/cp/decl.cc                           | 5 +++++
 gcc/testsuite/gcc.dg/Wnested-externs-1.c | 1 +
 4 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index c410db9fde47..a98f9b50ece8 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -4920,11 +4920,6 @@ c_common_nodes_and_builtins (void)
        }
     }
 
-  /* Call the target stack_protect_guard hook if the stack protection
-     guard is declared as a global symbol.  */
-  if (targetm.stack_protect_guard_symbol_p ())
-    lang_hooks.decls.pushdecl (targetm.stack_protect_guard ());
-
   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
     {
       va_list_arg_type_node = va_list_ref_type_node =
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index ae29c3338b74..60660a7f329d 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -1495,6 +1495,15 @@ push_file_scope (void)
   if (file_scope)
     return;
 
+  /* Call the target stack_protect_guard hook if the stack protection
+     guard is declared as a global symbol.  */
+  if (targetm.stack_protect_guard_symbol_p ())
+    {
+      tree decl = targetm.stack_protect_guard ();
+      DECL_CHAIN (decl) = visible_builtins;
+      visible_builtins = decl;
+    }
+
   push_scope ();
   file_scope = current_scope;
 
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index f5fe8673b774..84aa9a72fabf 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -5588,6 +5588,11 @@ cxx_init_decl_processing (void)
 
   c_common_nodes_and_builtins ();
 
+  /* Call the target stack_protect_guard hook if the stack protection
+     guard is declared as a global symbol.  */
+  if (targetm.stack_protect_guard_symbol_p ())
+    pushdecl (targetm.stack_protect_guard ());
+
   tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
   tree decl
     = add_builtin_function ("__builtin_is_constant_evaluated",
diff --git a/gcc/testsuite/gcc.dg/Wnested-externs-1.c 
b/gcc/testsuite/gcc.dg/Wnested-externs-1.c
index 5c4b5ddd8e23..a93c6f6006a4 100644
--- a/gcc/testsuite/gcc.dg/Wnested-externs-1.c
+++ b/gcc/testsuite/gcc.dg/Wnested-externs-1.c
@@ -2,6 +2,7 @@
 /* Origin: Joseph Myers <[email protected]> */
 /* { dg-do compile } */
 /* { dg-options "-Wnested-externs" } */
+/* { dg-additional-options "-mstack-protector-guard=global" { target 
x86_64-*-* i?86-*-* } } */
 
 int a;
 static int b;

Reply via email to