[flang-commits] [flang] 0e1fa91 - [flang] Fix character initialization after continuation

Leandro Lupori via flang-commits flang-commits at lists.llvm.org
Thu Jun 1 12:48:58 PDT 2023


Author: Leandro Lupori
Date: 2023-06-01T16:48:11-03:00
New Revision: 0e1fa9174072a3b896533d151336884e4eb1486a

URL: https://github.com/llvm/llvm-project/commit/0e1fa9174072a3b896533d151336884e4eb1486a
DIFF: https://github.com/llvm/llvm-project/commit/0e1fa9174072a3b896533d151336884e4eb1486a.diff

LOG: [flang] Fix character initialization after continuation

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 https://github.com/llvm/llvm-project/issues/62039

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D151885

Added: 
    flang/test/Parser/continuation-before-char.f90

Modified: 
    flang/lib/Parser/token-sequence.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Parser/token-sequence.cpp b/flang/lib/Parser/token-sequence.cpp
index eaa2bf3650589..f94c8142463de 100644
--- a/flang/lib/Parser/token-sequence.cpp
+++ b/flang/lib/Parser/token-sequence.cpp
@@ -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;
@@ -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);
         }
       }

diff  --git a/flang/test/Parser/continuation-before-char.f90 b/flang/test/Parser/continuation-before-char.f90
new file mode 100644
index 0000000000000..c7fb1d3e192da
--- /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


        


More information about the flang-commits mailing list