Skip to content

Commit ab261eb

Browse files
authored
[flang] Do not stop on mismatched DATA substring length (#69336)
https://reviews.llvm.org/D143819 turned mismatched DATA substring from a crash into a warning. However, the resulting DATA was incorrect when there were subsequent DATA values after the mismatch because the DATA to init conversion stopped. This change let the DATA to init continue. I added a LengthMismatch tag instead of using SizeMismatch because the other situation where SizeMismatch is returned seem like bug situations to me (the DATA value is dropped and the offset is not advanced), so I did not want to continue DATA processing in these cases.
1 parent 5c3ed39 commit ab261eb

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

flang/include/flang/Evaluate/initial-image.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Fortran::evaluate {
2222

2323
class InitialImage {
2424
public:
25-
enum Result { Ok, NotAConstant, OutOfRange, SizeMismatch };
25+
enum Result { Ok, NotAConstant, OutOfRange, SizeMismatch, LengthMismatch };
2626

2727
explicit InitialImage(std::size_t bytes) : data_(bytes) {}
2828
InitialImage(InitialImage &&that) = default;
@@ -72,7 +72,7 @@ class InitialImage {
7272
auto scalar{x.At(at)}; // this is a std string; size() in chars
7373
auto scalarBytes{scalar.size() * KIND};
7474
if (scalarBytes != elementBytes) {
75-
result = SizeMismatch;
75+
result = LengthMismatch;
7676
}
7777
// Blank padding when short
7878
for (; scalarBytes < elementBytes; scalarBytes += KIND) {

flang/lib/Semantics/data-to-inits.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@ bool DataInitializationCompiler<DSV>::InitElement(
457457
folded.AsFortran(), DescribeElement());
458458
} else if (status == evaluate::InitialImage::OutOfRange) {
459459
OutOfRangeError();
460-
} else if (status == evaluate::InitialImage::SizeMismatch) {
460+
} else if (status == evaluate::InitialImage::LengthMismatch) {
461461
exprAnalyzer_.Say(
462462
"DATA statement value '%s' for '%s' has the wrong length"_warn_en_US,
463463
folded.AsFortran(), DescribeElement());
464+
return true;
464465
} else {
465466
CHECK(exprAnalyzer_.context().AnyFatalError());
466467
}

flang/test/Semantics/data19.f90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! RUN: %flang_fc1 -fdebug-dump-symbols %s 2>&1 | FileCheck %s
2+
! Test truncation/padding in DATA statement.
3+
4+
character(len=3) :: c1, c2, c3(2), c4(2)
5+
data c1(1:2), c1(3:3) /'123', '4'/
6+
data c2(1:2), c2(3:3) /'1', '2'/
7+
data c3(:)(1:2), c3(:)(3:3) /'123', '678', '4', '9'/
8+
data c4(:)(1:2), c4(:)(3:3) /'1', '6', '2', '7'/
9+
end
10+
!CHECK: c1 (InDataStmt) size=3 offset=0: ObjectEntity type: CHARACTER(3_4,1) init:"124"
11+
!CHECK: c2 (InDataStmt) size=3 offset=3: ObjectEntity type: CHARACTER(3_4,1) init:"1 2"
12+
!CHECK: c3 (InDataStmt) size=6 offset=6: ObjectEntity type: CHARACTER(3_4,1) shape: 1_8:2_8 init:[CHARACTER(KIND=1,LEN=3)::"124","679"]
13+
!CHECK: c4 (InDataStmt) size=6 offset=12: ObjectEntity type: CHARACTER(3_4,1) shape: 1_8:2_8 init:[CHARACTER(KIND=1,LEN=3)::"1 2","6 7"]

0 commit comments

Comments
 (0)