From cf015ca24045bf34c6f22951d2d190eb31248d21 Mon Sep 17 00:00:00 2001 From: Thomas Koenig Date: Tue, 1 Jan 2019 21:19:53 +0000 Subject: [PATCH] re PR fortran/82743 (uncaught character truncation in derived type initialization) 2019-01-01 Thomas Koenig PR fortran/82743 * primary.c (gfc_convert_to_structure_constructor): If a character in a constructor is too long, add a warning with -Wcharacter-truncation. 2019-01-01 Thomas Koenig PR fortran/82743 * gfortran.dg/structure_constructor_16.f90: New test. From-SVN: r267499 --- gcc/fortran/ChangeLog | 7 +++++++ gcc/fortran/primary.c | 6 ++++++ gcc/testsuite/ChangeLog | 5 +++++ .../gfortran.dg/structure_constructor_16.f90 | 14 ++++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/structure_constructor_16.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 3af551012b9..245772de244 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,10 @@ +2019-01-01 Thomas Koenig + + PR fortran/82743 + * primary.c (gfc_convert_to_structure_constructor): If a character + in a constructor is too long, add a warning with + -Wcharacter-truncation. + 2019-01-01 Jakub Jelinek Update copyright years. diff --git a/gcc/fortran/primary.c b/gcc/fortran/primary.c index d7bd6d6cd9c..19f97d1cb19 100644 --- a/gcc/fortran/primary.c +++ b/gcc/fortran/primary.c @@ -3074,6 +3074,12 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c actual->expr->value.character.length = c; actual->expr->value.character.string = dest; + + if (warn_line_truncation && c < e) + gfc_warning_now (OPT_Wcharacter_truncation, + "CHARACTER expression will be truncated " + "in constructor (%ld/%ld) at %L", (long int) c, + (long int) e, &actual->expr->where); } } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a309a9567a5..22534b93ecb 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-01-01 Thomas Koenig + + PR fortran/82743 + * gfortran.dg/structure_constructor_16.f90: New test. + 2019-01-01 Jan Hubicka * g++.dg/ipa/devirt-36.C: Add dg-do-compile. diff --git a/gcc/testsuite/gfortran.dg/structure_constructor_16.f90 b/gcc/testsuite/gfortran.dg/structure_constructor_16.f90 new file mode 100644 index 00000000000..93e8a3f7763 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/structure_constructor_16.f90 @@ -0,0 +1,14 @@ +! { dg-do compile } +! { dg-additional-options "-Wcharacter-truncation" } +! PR 82743 - warnings were missing on truncation of structure +! constructors. +! Original test case by Simon Klüpfel +PROGRAM TEST + TYPE A + CHARACTER(LEN=1) :: C + END TYPE A + TYPE(A) :: A1 + A1=A("123") ! { dg-warning "CHARACTER expression will be truncated" } + A1=A(C="123") ! { dg-warning "CHARACTER expression will be truncated" } + A1%C="123" ! { dg-warning "CHARACTER expression will be truncated" } +END PROGRAM TEST -- 2.30.2