From 33bf7d81b77f37e7681d280949f25d0c1e11c928 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Wed, 6 May 2026 20:58:47 +0800
Subject: [PATCH] c: Don't warn the builtin declaration of stack protection
 guard

__stack_chk_guard is the builtin declaration of stack protection guard.
Since its DECL_CONTEXT is set to current_function_decl, -Wnested-externs
warns:

cc1: warning: nested extern declaration of '__stack_chk_guard' [-Wnested-externs]

Update pushdecl not to warn it.

gcc/

	PR c/121911
	* c/c-decl.cc (pushdecl): Don't warn the builtin declaration of
	stack protection guard.

gcc/testsuite/

	PR c/121911
	* gcc.dg/Wnested-externs-1.c: Add -mstack-protector-guard=global
	for x86.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 gcc/c/c-decl.cc                          | 5 ++++-
 gcc/testsuite/gcc.dg/Wnested-externs-1.c | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index cc07e05e336..a4f13dd26fe 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -3551,8 +3551,11 @@ pushdecl (tree x)
 	  visdecl = b->decl;
 	  vistype = TREE_TYPE (visdecl);
 	}
+      /* Don't warn the builtin declaration of stack protection guard.  */
       if (scope != file_scope
-	  && !DECL_IN_SYSTEM_HEADER (x))
+	  && !DECL_IN_SYSTEM_HEADER (x)
+	  && (!targetm.stack_protect_guard_symbol_p ()
+	      || x != targetm.stack_protect_guard ()))
 	warning_at (locus, OPT_Wnested_externs,
 		    "nested extern declaration of %qD", x);
 
diff --git a/gcc/testsuite/gcc.dg/Wnested-externs-1.c b/gcc/testsuite/gcc.dg/Wnested-externs-1.c
index 5c4b5ddd8e2..a93c6f6006a 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 <joseph@codesourcery.com> */
 /* { dg-do compile } */
 /* { dg-options "-Wnested-externs" } */
+/* { dg-additional-options "-mstack-protector-guard=global" { target x86_64-*-* i?86-*-* } } */
 
 int a;
 static int b;
-- 
2.54.0

