[gcc r17-2717] c++: Fix ICE with loop pragmas at class scope [PR126323]

Jason Merrill via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:cd453025faf5ab3ada2428e5fc372d9a6328c9cb

commit r17-2717-gcd453025faf5ab3ada2428e5fc372d9a6328c9cb
Author: Odysseas Georgoudis <[email protected]>
Date:   Fri Jul 24 15:17:32 2026 -0400

    c++: Fix ICE with loop pragmas at class scope [PR126323]
    
    cp_parser_pragma rejected GCC loop pragmas at namespace scope, but not
    at class scope.  Consequently, it attempted to parse the following loop
    as a statement while in a member-declaration context, leading to an ICE
    when processing the loop initializer.
    
    Accept these pragmas only in statement contexts.
    
    gcc/cp/ChangeLog:
    
            PR c++/126323
            * parser.cc (cp_parser_pragma): Reject GCC loop pragmas outside
            statement contexts.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/126323
            * g++.dg/parse/pr126323.C: New test.
    
    Reviewed-by: Jason Merrill <[email protected]>

Diff:
---
 gcc/cp/parser.cc                      |  2 +-
 gcc/testsuite/g++.dg/parse/pr126323.C | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 3f042bec1c66..13ebd64c4836 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -57597,7 +57597,7 @@ cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
 	    gcc_unreachable ();
 	}
 
-	if (context == pragma_external)
+	if (context != pragma_stmt && context != pragma_compound)
 	  {
 	    error_at (pragma_tok->location,
 		      "%<#pragma GCC %s%> must be inside a function",
diff --git a/gcc/testsuite/g++.dg/parse/pr126323.C b/gcc/testsuite/g++.dg/parse/pr126323.C
new file mode 100644
index 000000000000..2224733ccb33
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/pr126323.C
@@ -0,0 +1,10 @@
+// PR c++/126323
+
+class C {
+#pragma GCC novector	// { dg-error "must be inside a function" }
+#pragma GCC unroll(0)	// { dg-error "must be inside a function" }
+#pragma GCC ivdep	// { dg-error "must be inside a function" }
+  for (int i = 0; i < 2; i++)	// { dg-error "expected unqualified-id" }
+				// { dg-error "does not name a type" "" { target *-*-* } .-1 }
+    ;
+};
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.