This patch fixes bugs I observed in tests for the CFI_section function
-- it turns out both the function and test cases had bugs. :-(
The bugs in CFI_section itself had to do with incorrect computation of
the base address for the result descriptor, plus an ordering problem
that caused it not to work if the source and result descriptors are the
same. I fixed this by rewriting the loop to fill in the dimension info
for the result array and reordering it with respect to the base address
computation.
Note that the old version of CFI_section attempted to preserve the lower
bounds of the section passed in as an argument. This is not actually
required by the Fortran standard, which specifies only the shape of the
result array, not its bounds. My rewritten version produces an array
with zero lower bounds, similar to what CFI_establish produces given the
shape as input. If this change is seen as undesirable, of course it can
be changed back to correctly do what it was previously unsuccessfully
trying to do. :-P
The bug in the older ISO_Fortran_binding_1.c testcase was an incorrect
assertion about the lower bound behavior, while the bugs in the
not-yet-committed TS29113 testsuite were due to me having previously
lost track of having fixed this already and just failing to save the fix
before I posted the testsuite patch. As with the other patches I've
been posting for TS29113 testsuite issues, I can refactor the testsuite
changes to lump them all in with the base testsuite patch depending on
the order that things get reviewed/committed.
-Sandra
commit a2e189aeb165781fe741f942e00bf073a496af92
Author: Sandra Loosemore
Date: Sat Jul 17 16:12:18 2021 -0700
[PR libfortran/101310] Bind(c): Fix bugs in CFI_section
CFI_section was incorrectly adjusting the base pointer for the result
array twice in different ways. It was also overwriting the array
dimension info in the result descriptor before computing the base
address offset from the source descriptor, which caused problems if
the two descriptors are the same. This patch fixes both problems and
makes the code simpler, too.
A consequence of this patch is that the result array is now 0-based in
all dimensions instead of starting at the numbering to match the first
element of the source array. The Fortran standard only specifies the
shape of the result array, not its lower bounds, so this is permitted
and probably less confusing for users as well as implementors.
2021-07-17 Sandra Loosemore
PR libfortran/101310
libgfortran/
* runtime/ISO_Fortran_binding.c (CFI_section): Fix the base
address computation and simplify the code.
gcc/testsuite/
* gfortran.dg/ISO_Fortran_binding_1.c (section_c): Remove
incorrect assertions.
* gfortran.dg/ts29113/library/section-3.f90: Fix indexing bugs.
* gfortran.dg/ts29113/library/section-3p.f90: Likewise.
* gfortran.dg/ts29113/library/section-4-c.c: New file.
* gfortran.dg/ts29113/library/section-4.f90: New file.
diff --git a/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c b/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c
index 9da5d85..bb56ca0 100644
--- a/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c
+++ b/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c
@@ -142,11 +142,12 @@ float section_c(int *std_case, CFI_cdesc_t * source, int *low, int *str)
CFI_type_float, 0, 1, NULL);
if (ind) return -1.0;
ind = CFI_section((CFI_cdesc_t *)§ion, source, lower, NULL, strides);
- assert (section.dim[0].lower_bound == lower[0]);
if (ind) return -2.0;
/* Sum over the section */
- for (idx[0] = lower[0]; idx[0] < section.dim[0].extent + lower[0]; idx[0]++)
+ for (idx[0] = section.dim[0].lower_bound;
+ idx[0] < section.dim[0].extent + section.dim[0].lower_bound;
+ idx[0]++)
ans += *(float*)CFI_address ((CFI_cdesc_t*)§ion, idx);
return ans;
}
@@ -164,11 +165,12 @@ float section_c(int *std_case, CFI_cdesc_t * source, int *low, int *str)
ind = CFI_section((CFI_cdesc_t *)§ion, source,
lower, upper, strides);
assert (section.rank == 1);
- assert (section.dim[0].lower_bound == lower[0]);
if (ind) return -2.0;
/* Sum over the section */
- for (idx[0] = lower[0]; idx[0] < section.dim[0].extent + lower[0]; idx[0]++)
+ for (idx[0] = section.dim[0].lower_bound;
+ idx[0] < section.dim[0].extent + section.dim[0].lower_bound;
+ idx[0]++)
ans += *(float*)CFI_address ((CFI_cdesc_t*)§ion, idx);
return ans;
}
diff --git a/gcc/testsuite/gfortran.dg/ts29113/library/section-3.f90 b/gcc/testsuite/gfortran.dg/ts29113/library/section-3.f90
index 6811891..e51c084 100644
--- a/gcc/testsuite/gfortran.dg/ts29113/library/section-3.f90
+++ b/gcc/testsuite/gfortran.dg/ts29113/library/section-3.f90
@@ -40,12 +40,12 @@ program testit
end do
end