]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: SIZE optional DIM argument having OPTIONAL+VALUE attributes [PR113245]
authorHarald Anlauf <anlauf@gmx.de>
Sun, 7 Jan 2024 21:24:25 +0000 (22:24 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Mon, 8 Jan 2024 17:42:39 +0000 (18:42 +0100)
gcc/fortran/ChangeLog:

PR fortran/113245
* trans-intrinsic.cc (gfc_conv_intrinsic_size): Use
gfc_conv_expr_present() for proper check of optional DIM argument.

gcc/testsuite/ChangeLog:

PR fortran/113245
* gfortran.dg/size_optional_dim_2.f90: New test.

gcc/fortran/trans-intrinsic.cc
gcc/testsuite/gfortran.dg/size_optional_dim_2.f90 [new file with mode: 0644]

index d973c49380c2221da00ddc494de48dddf0638ffd..74139262657b8fa4723f4bfce86b1f7783e14eff 100644 (file)
@@ -8025,9 +8025,6 @@ gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
          argse.data_not_needed = 1;
          gfc_conv_expr (&argse, actual->expr);
          gfc_add_block_to_block (&se->pre, &argse.pre);
-         cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
-                                 argse.expr, null_pointer_node);
-         cond = gfc_evaluate_now (cond, &se->pre);
          /* 'block2' contains the arg2 absent case, 'block' the arg2 present
              case; size_var can be used in both blocks. */
          tree size_var = gfc_create_var (TREE_TYPE (size), "size");
@@ -8038,6 +8035,7 @@ gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
          tmp = fold_build2_loc (input_location, MODIFY_EXPR,
                                 TREE_TYPE (size_var), size_var, size);
          gfc_add_expr_to_block (&block2, tmp);
+         cond = gfc_conv_expr_present (actual->expr->symtree->n.sym);
          tmp = build3_v (COND_EXPR, cond, gfc_finish_block (&block),
                          gfc_finish_block (&block2));
          gfc_add_expr_to_block (&se->pre, tmp);
diff --git a/gcc/testsuite/gfortran.dg/size_optional_dim_2.f90 b/gcc/testsuite/gfortran.dg/size_optional_dim_2.f90
new file mode 100644 (file)
index 0000000..698702b
--- /dev/null
@@ -0,0 +1,31 @@
+! { dg-do run }
+! { dg-additional-options "-fdump-tree-original" }
+! PR fortran/113245 - SIZE, optional DIM argument, w/ OPTIONAL+VALUE attributes
+
+program p
+  implicit none
+  real    :: a(2,3)
+  integer :: expect
+  expect = size (a,2)
+  call ref (a,2)
+  call val (a,2)
+  expect = size (a)
+  call ref (a)
+  call val (a)
+contains
+  subroutine ref (x, dim)
+    real,              intent(in) :: x(:,:)
+    integer, optional, intent(in) :: dim
+    print *, "present(dim), size(a,dim) =", present (dim), size (x,dim=dim)
+    if (size (x,dim=dim) /= expect) stop 1
+  end
+  subroutine val (x, dim)
+    real,              intent(in) :: x(:,:)
+    integer, optional, value      :: dim
+    print *, "present(dim), size(a,dim) =", present (dim), size (x,dim=dim)
+    if (size (x,dim=dim) /= expect) stop 2
+  end
+end
+
+! Ensure inline code is generated:
+! { dg-final { scan-tree-dump-not "_gfortran_size" "original" } }