Skip to content

Commit 4a3e4b9

Browse files
authored
[flang] Adjust prescanner fix for preprocessing (#122779)
Commas being optional in FORMAT statements, the tokenization of things like 3I9HHOLLERITH is tricky. After tokenizing the initial '3', we don't want to then take apparent identifier "I9HHOLLERITH" as the next token. So the prescanner just consumes the letter ("I") as its own token in this context. A recent bug report complained that this can lead to incorrect results when (in this case) the letter is a defined preprocessing macro. I updated the prescanner to check that the letter is actually followed by an instance of a problematic Hollerith literal. And this broke two tests in the Fujitsu Fortran test suite that Linaro runs, as it couldn't detect a following Hollerith literal that wasn't on the same source line. We can't do look-ahead line continuation processing in NextToken(), either. So here's a second attempt at fixing the original problem: namely, the letter that follows a decimal integer token is checked to see whether it's the name of a defined macro.
1 parent 9f0f54a commit 4a3e4b9

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

flang/lib/Parser/prescan.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -708,23 +708,11 @@ bool Prescanner::NextToken(TokenSequence &tokens) {
708708
EmitCharAndAdvance(tokens, *at_);
709709
QuotedCharacterLiteral(tokens, start);
710710
} else if (IsLetter(*at_) && !preventHollerith_ &&
711-
parenthesisNesting_ > 0) {
712-
const char *p{at_};
713-
int digits{0};
714-
for (;; ++digits) {
715-
++p;
716-
if (InFixedFormSource()) {
717-
p = SkipWhiteSpace(p);
718-
}
719-
if (!IsDecimalDigit(*p)) {
720-
break;
721-
}
722-
}
723-
if (digits > 0 && (*p == 'h' || *p == 'H')) {
724-
// Handles FORMAT(3I9HHOLLERITH) by skipping over the first I so that
725-
// we don't misrecognize I9HOLLERITH as an identifier in the next case.
726-
EmitCharAndAdvance(tokens, *at_);
727-
}
711+
parenthesisNesting_ > 0 &&
712+
!preprocessor_.IsNameDefined(CharBlock{at_, 1})) {
713+
// Handles FORMAT(3I9HHOLLERITH) by skipping over the first I so that
714+
// we don't misrecognize I9HHOLLERITH as an identifier in the next case.
715+
EmitCharAndAdvance(tokens, *at_);
728716
}
729717
preventHollerith_ = false;
730718
} else if (*at_ == '.') {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
! RUN: %flang -fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
22
! CHECK: PRINT *, 2_4
3+
! CHECK: PRINT *, 1_4
34
#define a ,3
45
print *, mod(5 a)
6+
print *, mod(4 a
7+
+)
58
end

0 commit comments

Comments
 (0)