This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[gfortran, PR33254] Add -fbounds-checking for character array constructors
- From: Tobias Schlüter <tobias dot schlueter at physik dot uni-muenchen dot de>
- To: Fortran List <fortran at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Wed, 10 Oct 2007 16:58:15 +0200
- Subject: [gfortran, PR33254] Add -fbounds-checking for character array constructors
Hi,
the patch is straightforward, and does exactly what is advertised in the
subject line. Unfortunately, I discovered PR33727 while preparing this
patch. One PR down, still the same number to go.
Built and tested on i386-darwin. Ok?
Cheers,
- Tobi
2007-10-10 Tobias Schlüter <tobi@gcc.gnu.org>
PR fortran/33254
fortran/
* trans-array.c (get_array_ctor_strlen): Add bounds-checking code.
testsuite/
* gfortran.dg/bounds_check_10.f90: New.
diff -r 2441ec5b11dd gcc/fortran/trans-array.c
--- a/gcc/fortran/trans-array.c Wed Oct 10 09:48:18 2007 +0000
+++ b/gcc/fortran/trans-array.c Wed Oct 10 16:54:08 2007 +0200
@@ -1413,6 +1413,7 @@ get_array_ctor_strlen (stmtblock_t *bloc
get_array_ctor_strlen (stmtblock_t *block, gfc_constructor * c, tree * len)
{
bool is_const;
+ tree first_len = NULL_TREE;
is_const = TRUE;
@@ -1447,6 +1448,20 @@ get_array_ctor_strlen (stmtblock_t *bloc
get_array_ctor_all_strlen (block, c->expr, len);
break;
}
+ if (!first_len)
+ first_len = *len;
+ else
+ {
+ /* Verify that all constructor elements are of the same length. */
+ tree cond = fold_build2 (NE_EXPR, boolean_type_node,
+ first_len, *len);
+ gfc_trans_runtime_check
+ (cond, block, &c->expr->where,
+ "Different CHARACTER lengths (%ld/%ld) in array constructor",
+ fold_convert (long_integer_type_node, first_len),
+ fold_convert (long_integer_type_node, *len));
+ }
+
}
return is_const;
diff -r 2441ec5b11dd gcc/testsuite/gfortran.dg/bounds_check_10.f90
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gcc/testsuite/gfortran.dg/bounds_check_10.f90 Wed Oct 10 16:54:08 2007 +0200
@@ -0,0 +1,14 @@
+! { dg-do run }
+! { dg-options "-fbounds-check" }
+! { dg-shouldfail "Different CHARACTER lengths" }
+! PR fortran/33254: No bounds checking for array constructors
+program array_char
+implicit none
+character (len=2) :: x, y
+character (len=2) :: z(3)
+x = "a "
+y = "cd"
+z = [trim(x), trim(y), "aaaa"] ! [ "a", "cd", "aaaa" ] shoudl catch first error
+end program array_char
+
+! { dg-output "Different CHARACTER lengths .1/2. in array constructor" }