c: Reject gimple and rtl functions as needed functions [PR121421]

Message ID 20250916184030.1002454-1-andrew.pinski@oss.qualcomm.com
State Committed
Commit df5088e9a231159c8035debda18b997bc0eeeefb
Headers
Series c: Reject gimple and rtl functions as needed functions [PR121421] |

Commit Message

Andrew Pinski Sept. 16, 2025, 6:40 p.m. UTC
  These two don't make sense as nested functions as they both don't handle
the unnesting and/or have support for the static chain.

So let's reject them.

Bootstrapped and tested on x86_64-linux-gnu.

	PR c/121421

gcc/c/ChangeLog:

	* c-parser.cc (c_parser_declaration_or_fndef): Error out for gimple
	and rtl functions as nested functions.

gcc/testsuite/ChangeLog:

	* gcc.dg/gimplefe-error-16.c: New test.

Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
---
 gcc/c/c-parser.cc                        | 11 ++++++++++-
 gcc/testsuite/gcc.dg/gimplefe-error-16.c | 10 ++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/gimplefe-error-16.c
  

Comments

Joseph Myers Sept. 16, 2025, 8:42 p.m. UTC | #1
On Tue, 16 Sep 2025, Andrew Pinski wrote:

> These two don't make sense as nested functions as they both don't handle
> the unnesting and/or have support for the static chain.
> 
> So let's reject them.
> 
> Bootstrapped and tested on x86_64-linux-gnu.
> 
> 	PR c/121421
> 
> gcc/c/ChangeLog:
> 
> 	* c-parser.cc (c_parser_declaration_or_fndef): Error out for gimple
> 	and rtl functions as nested functions.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.dg/gimplefe-error-16.c: New test.

OK.
  

Patch

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index f1aaab12e6e..df44a915ed4 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -3222,7 +3222,7 @@  c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
       else
 	fnbody = c_parser_compound_statement (parser, &endloc);
       tree fndecl = current_function_decl;
-      if (nested)
+      if (nested && specs->declspec_il == cdil_none)
 	{
 	  tree decl = current_function_decl;
 	  /* Mark nested functions as needing static-chain initially.
@@ -3235,6 +3235,15 @@  c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
 	  c_pop_function_context ();
 	  add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
 	}
+      else if (nested)
+	{
+	  if (specs->declspec_il == cdil_rtl)
+	    error ("%<__RTL%> function cannot be a nested function");
+	  else
+	    error ("%<__GIMPLE%> function cannot be a nested function");
+	  finish_function (endloc);
+	  c_pop_function_context ();
+	}
       else
 	{
 	  if (fnbody)
diff --git a/gcc/testsuite/gcc.dg/gimplefe-error-16.c b/gcc/testsuite/gcc.dg/gimplefe-error-16.c
new file mode 100644
index 00000000000..4cdeac889c8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/gimplefe-error-16.c
@@ -0,0 +1,10 @@ 
+/* { dg-do compile } */
+/* { dg-options "-fgimple" } */
+/* PR c/121421 */
+/* Gimple functions cannot be nested functions. */
+
+
+void main(void)
+{
+    void __GIMPLE b(){} /* { dg-error "cannot be a nested" } */
+}