Skip to content

Commit

Permalink
[flang] Fix character initialization after continuation
Browse files Browse the repository at this point in the history
The insertion of a space on a line continuation right before
a character literal was confusing TokenSequence::ToLowerCase(),
that was unable to identify the character literal as such,
causing it to be converted to lower case.

Fix this by skipping spaces in the beginning and end of each
token, before testing for token type.

Fixes #62039

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D151885
  • Loading branch information
luporl committed Jun 1, 2023
1 parent 81ea6b7 commit 0e1fa91
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 13 additions & 4 deletions flang/lib/Parser/token-sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,16 @@ TokenSequence &TokenSequence::ToLowerCase() {
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;
Expand All @@ -172,17 +181,17 @@ TokenSequence &TokenSequence::ToLowerCase() {
*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);
}
}
Expand Down
7 changes: 7 additions & 0 deletions flang/test/Parser/continuation-before-char.f90
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0e1fa91

Please sign in to comment.