diff --git a/flang/lib/Parser/token-sequence.cpp b/flang/lib/Parser/token-sequence.cpp --- a/flang/lib/Parser/token-sequence.cpp +++ b/flang/lib/Parser/token-sequence.cpp @@ -155,7 +155,16 @@ std::size_t nextStart{atToken + 1 < tokens ? start_[++atToken] : chars}; char *p{&char_[j]}; char const *limit{char_.data() + nextStart}; + const char *lastChar{limit - 1}; j = nextStart; + // Skip leading whitespaces + while (p < limit - 1 && *p == ' ') { + ++p; + } + // Find last non-whitespace char + while (lastChar > p + 1 && *lastChar == ' ') { + --lastChar; + } if (IsDecimalDigit(*p)) { while (p < limit && IsDecimalDigit(*p)) { ++p; @@ -172,17 +181,17 @@ *p = ToLowerCaseLetter(*p); } } - } else if (limit[-1] == '\'' || limit[-1] == '"') { - if (*p == limit[-1]) { + } else if (*lastChar == '\'' || *lastChar == '"') { + if (*p == *lastChar) { // Character literal without prefix - } else if (p[1] == limit[-1]) { + } else if (p[1] == *lastChar) { // BOZX-prefixed constant for (; p < limit; ++p) { *p = ToLowerCaseLetter(*p); } } else { // Literal with kind-param prefix name (e.g., K_"ABC"). - for (; *p != limit[-1]; ++p) { + for (; *p != *lastChar; ++p) { *p = ToLowerCaseLetter(*p); } } diff --git a/flang/test/Parser/continuation-before-char.f90 b/flang/test/Parser/continuation-before-char.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Parser/continuation-before-char.f90 @@ -0,0 +1,7 @@ +! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s +! Continuation right before character literal. +subroutine test() +! CHECK: CHARACTER(LEN=3_4) :: a = "ABC" + character(len=3) :: a =& +"ABC" +end subroutine