diff options
author | Thomas Koenig <tkoenig@gcc.gnu.org> | 2018-02-20 18:57:34 +0000 |
---|---|---|
committer | Thomas Koenig <tkoenig@gcc.gnu.org> | 2018-02-20 18:57:34 +0000 |
commit | 04946c6b905572f35f06de34460d20f05203a033 (patch) | |
tree | 7520b2aad9e38ebed2f705c5a4633c769ef8849e /gcc/fortran/primary.c | |
parent | 5a54a15e30542c3cad550e08f3fd6cfcd4969e30 (diff) | |
download | gcc-04946c6b905572f35f06de34460d20f05203a033.zip gcc-04946c6b905572f35f06de34460d20f05203a033.tar.gz gcc-04946c6b905572f35f06de34460d20f05203a033.tar.bz2 |
re PR fortran/48890 ([F95] Wrong length of a character component of named constant derived-type)
2018-02-20 Thomas Koenig <tkoenig@gcc.gnu.org>
PR fortran/48890
PR fortran/83823
* primary.c (gfc_convert_to_structure_constructor):
For a constant string constructor, make sure the length
is correct.
2018-02-20 Thomas Koenig <tkoenig@gcc.gnu.org>
PR fortran/48890
PR fortran/83823
* gfortran.dg/structure_constructor_14.f90: New test.
From-SVN: r257856
Diffstat (limited to 'gcc/fortran/primary.c')
-rw-r--r-- | gcc/fortran/primary.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/gcc/fortran/primary.c b/gcc/fortran/primary.c index 9e6a8fe..d889ed1 100644 --- a/gcc/fortran/primary.c +++ b/gcc/fortran/primary.c @@ -2879,6 +2879,38 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c if (!this_comp) goto cleanup; + /* For a constant string constructor, make sure the length is + correct; truncate of fill with blanks if needed. */ + if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable + && this_comp->ts.u.cl && this_comp->ts.u.cl->length + && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT + && actual->expr->expr_type == EXPR_CONSTANT) + { + ptrdiff_t c, e; + c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer); + e = actual->expr->value.character.length; + + if (c != e) + { + ptrdiff_t i, to; + gfc_char_t *dest; + dest = gfc_get_wide_string (c + 1); + + to = e < c ? e : c; + for (i = 0; i < to; i++) + dest[i] = actual->expr->value.character.string[i]; + + for (i = e; i < c; i++) + dest[i] = ' '; + + dest[c] = '\0'; + free (actual->expr->value.character.string); + + actual->expr->value.character.length = c; + actual->expr->value.character.string = dest; + } + } + comp_tail->val = actual->expr; if (actual->expr != NULL) comp_tail->where = actual->expr->where; |