public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran: check for invalid uses of statement functions arguments [PR69604]
@ 2022-12-22 21:13 Harald Anlauf
  2022-12-22 21:56 ` Steve Kargl
  0 siblings, 1 reply; 2+ messages in thread
From: Harald Anlauf @ 2022-12-22 21:13 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 211 bytes --]

Dear all,

the attached patch adds a check for statement function bodies for
invalid uses of dummy arguments.  This fixes an ICE-on invalid.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr69604.diff --]
[-- Type: text/x-patch, Size: 2672 bytes --]

From 794af0d00b7086c9f0493f3a1aaac644e1fd50f6 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Thu, 22 Dec 2022 22:03:31 +0100
Subject: [PATCH] Fortran: check for invalid uses of statement functions
 arguments [PR69604]

gcc/fortran/ChangeLog:

	PR fortran/69604
	* match.cc (chk_stmt_fcn_body): New function.  Check for invalid uses
	of statement functions arguments.
	(gfc_match_st_function): Use above.

gcc/testsuite/ChangeLog:

	PR fortran/69604
	* gfortran.dg/statement_function_4.f90: New test.
---
 gcc/fortran/match.cc                          | 27 +++++++++++++++++++
 .../gfortran.dg/statement_function_4.f90      | 10 +++++++
 2 files changed, 37 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/statement_function_4.f90

diff --git a/gcc/fortran/match.cc b/gcc/fortran/match.cc
index 89fb115c0f6..3d346788416 100644
--- a/gcc/fortran/match.cc
+++ b/gcc/fortran/match.cc
@@ -5915,6 +5915,30 @@ recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
 }


+/* Check for invalid uses of statement function dummy arguments in body.  */
+
+static bool
+chk_stmt_fcn_body (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
+{
+  gfc_formal_arglist *formal;
+
+  if (e == NULL || e->symtree == NULL || e->expr_type != EXPR_FUNCTION)
+    return false;
+
+  for (formal = sym->formal; formal; formal = formal->next)
+    {
+      if (formal->sym == e->symtree->n.sym)
+	{
+	  gfc_error ("Invalid use of statement function argument at %L",
+		     &e->where);
+	  return true;
+	}
+    }
+
+  return false;
+}
+
+
 /* Match a statement function declaration.  It is so easy to match
    non-statement function statements with a MATCH_ERROR as opposed to
    MATCH_NO that we suppress error message in most cases.  */
@@ -5983,6 +6007,9 @@ gfc_match_st_function (void)
       return MATCH_ERROR;
     }

+  if (gfc_traverse_expr (expr, sym, chk_stmt_fcn_body, 0))
+    return MATCH_ERROR;
+
   sym->value = expr;

   if ((gfc_current_state () == COMP_FUNCTION
diff --git a/gcc/testsuite/gfortran.dg/statement_function_4.f90 b/gcc/testsuite/gfortran.dg/statement_function_4.f90
new file mode 100644
index 00000000000..6ce5951b53a
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/statement_function_4.f90
@@ -0,0 +1,10 @@
+! { dg-do compile }
+! PR fortran/69604
+! Contributed by G.Steinmetz
+
+program p
+  x(n) = 1 + n(2.0) ! { dg-error "Invalid use of statement function argument" }
+  y(k) = k()        ! { dg-error "Invalid use of statement function argument" }
+  z(m) = m          ! { dg-warning "Statement function" }
+  print *, x(n)
+end
--
2.35.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fortran: check for invalid uses of statement functions arguments [PR69604]
  2022-12-22 21:13 [PATCH] Fortran: check for invalid uses of statement functions arguments [PR69604] Harald Anlauf
@ 2022-12-22 21:56 ` Steve Kargl
  0 siblings, 0 replies; 2+ messages in thread
From: Steve Kargl @ 2022-12-22 21:56 UTC (permalink / raw)
  To: Harald Anlauf via Fortran; +Cc: gcc-patches

On Thu, Dec 22, 2022 at 10:13:04PM +0100, Harald Anlauf via Fortran wrote:
> 
> the attached patch adds a check for statement function bodies for
> invalid uses of dummy arguments.  This fixes an ICE-on invalid.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 

Yes. Thanks for the patch.

-- 
Steve

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-12-22 21:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-22 21:13 [PATCH] Fortran: check for invalid uses of statement functions arguments [PR69604] Harald Anlauf
2022-12-22 21:56 ` Steve Kargl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).