This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCHv2] Handle overlength string literals in the fortan FE
- From: Bernd Edlinger <bernd dot edlinger at hotmail dot de>
- To: "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>, "fortran at gcc dot gnu dot org" <fortran at gcc dot gnu dot org>, Richard Biener <rguenther at suse dot de>, Tobias Burnus <burnus at net-b dot de>, Jeff Law <law at redhat dot com>
- Date: Fri, 24 Aug 2018 20:06:47 +0000
- Subject: [PATCHv2] Handle overlength string literals in the fortan FE
- References: <AM5PR0701MB2657951582B419E5B2AFB48CE42D0@AM5PR0701MB2657.eurprd07.prod.outlook.com>
Hi!
This is an alternative approach to handle overlength strings in the Fortran FE.
The difference to the previous version is that overlength
STRING_CST never have a longer TREE_STRING_LENGTH than the TYPE_DOMAIN.
And those STRING_CSTs are thus no longer zero terminated.
And the requirement to have all sting constants internally zero-terminated
is dropped.
Bootstrapped and reg-tested on x86_64-pc-linux-gnu.
Is it OK for trunk?
Thanks
Bernd.
2018-08-01 Bernd Edlinger <bernd.edlinger@hotmail.de>
* trans-array.c (gfc_conv_array_initializer): Remove excess precision
from overlength string initializers.
Index: gcc/fortran/trans-array.c
===================================================================
--- gcc/fortran/trans-array.c (revision 263807)
+++ gcc/fortran/trans-array.c (working copy)
@@ -5964,6 +5964,26 @@ gfc_conv_array_initializer (tree type, gfc_expr *
{
case EXPR_CONSTANT:
gfc_conv_constant (&se, c->expr);
+
+ /* See gfortran.dg/charlen_15.f90 for instance. */
+ if (TREE_CODE (se.expr) == STRING_CST
+ && TREE_CODE (type) == ARRAY_TYPE)
+ {
+ tree atype = type;
+ while (TREE_CODE (TREE_TYPE (atype)) == ARRAY_TYPE)
+ atype = TREE_TYPE (atype);
+ if (TREE_CODE (TREE_TYPE (atype)) == INTEGER_TYPE
+ && tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (se.expr)))
+ > tree_to_uhwi (TYPE_SIZE_UNIT (atype)))
+ {
+ unsigned HOST_WIDE_INT size
+ = tree_to_uhwi (TYPE_SIZE_UNIT (atype));
+ const char *p = TREE_STRING_POINTER (se.expr);
+
+ se.expr = build_string (size, p);
+ TREE_TYPE (se.expr) = atype;
+ }
+ }
break;
case EXPR_STRUCTURE: