[flang-commits] [flang] [flang] Handle continuation line edge case (PR #74751)
via flang-commits
flang-commits at lists.llvm.org
Thu Dec 7 11:22:08 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-parser
Author: Peter Klausler (klausler)
<details>
<summary>Changes</summary>
For repeated quote marks in a character literal that are separated by free form line continuation without a leading ampersand character on the continuation line, also handle the case of spaces at the beginning of the continuation line.
For example,
PRINT *, 'don'&
't poke the bear'
now prints "don't poke the bear", like nearly all other Fortran compilers do.
This is not strictly standard conforming behavior, and the compiler emits a portability warning with -pedantic.
Fixes llvm-test-suite/Fortran/gfortran/regression/continuation_12.f90 and .../continuation_13.f90.
---
Full diff: https://github.com/llvm/llvm-project/pull/74751.diff
3 Files Affected:
- (modified) flang/lib/Parser/prescan.cpp (+11-9)
- (modified) flang/lib/Parser/prescan.h (+2-1)
- (modified) flang/test/Parser/continuation-before-quote.f90 (+3)
``````````diff
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 449ea60144424a..ac68b54a50ca18 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -706,6 +706,7 @@ void Prescanner::QuotedCharacterLiteral(
char quote{*at_};
const char *end{at_ + 1};
inCharLiteral_ = true;
+ charLiteralQuote_ = quote;
const auto emit{[&](char ch) { EmitChar(tokens, ch); }};
const auto insert{[&](char ch) { EmitInsertedChar(tokens, ch); }};
bool isEscaped{false};
@@ -749,17 +750,10 @@ void Prescanner::QuotedCharacterLiteral(
break;
}
inCharLiteral_ = true;
- if (insertASpace_) {
- if (features_.ShouldWarn(
- common::LanguageFeature::MiscSourceExtensions)) {
- Say(GetProvenanceRange(at_, end),
- "Repeated quote mark in character literal continuation line should have been preceded by '&'"_port_en_US);
- }
- insertASpace_ = false;
- }
}
}
inCharLiteral_ = false;
+ charLiteralQuote_.reset();
}
void Prescanner::Hollerith(
@@ -1122,7 +1116,15 @@ const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
} else if (*p == '!' || *p == '\n' || *p == '#') {
return nullptr;
} else if (ampersand || IsImplicitContinuation()) {
- if (p > nextLine_) {
+ if (charLiteralQuote_ && *p == *charLiteralQuote_) {
+ // 'a'& -> 'a''b' == "a'b"
+ // 'b'
+ if (features_.ShouldWarn(
+ common::LanguageFeature::MiscSourceExtensions)) {
+ Say(GetProvenanceRange(p, p + 1),
+ "Repeated quote mark in character literal continuation line should have been preceded by '&'"_port_en_US);
+ }
+ } else if (p > nextLine_) {
--p;
} else {
insertASpace_ = true;
diff --git a/flang/lib/Parser/prescan.h b/flang/lib/Parser/prescan.h
index 16b2c6165f611c..efeda592813ea7 100644
--- a/flang/lib/Parser/prescan.h
+++ b/flang/lib/Parser/prescan.h
@@ -217,7 +217,8 @@ class Prescanner {
bool tabInCurrentLine_{false};
bool slashInCurrentStatement_{false};
bool preventHollerith_{false}; // CHARACTER*4HIMOM not Hollerith
- bool inCharLiteral_{false};
+ bool inCharLiteral_;
+ std::optional<char> charLiteralQuote_;
bool inPreprocessorDirective_{false};
// In some edge cases of compiler directive continuation lines, it
diff --git a/flang/test/Parser/continuation-before-quote.f90 b/flang/test/Parser/continuation-before-quote.f90
index 66252010d89c46..1cc44d29c87463 100644
--- a/flang/test/Parser/continuation-before-quote.f90
+++ b/flang/test/Parser/continuation-before-quote.f90
@@ -4,6 +4,9 @@ subroutine test
!CHECK: portability: Repeated quote mark in character literal continuation line should have been preceded by '&'
print *, 'needs an '&
'ampersand'''
+!CHECK: portability: Repeated quote mark in character literal continuation line should have been preceded by '&'
+ print *, 'also needs an '&
+ 'ampersand'''
!CHECK-NOT: portability: Repeated quote mark in character literal continuation line should have been preceded by '&'
print *, 'has an '&
&'ampersand'''
``````````
</details>
https://github.com/llvm/llvm-project/pull/74751
More information about the flang-commits
mailing list