Skip to content

Commit 11450c0

Browse files
klausleryuxuanchen1997
authored andcommitted
[flang][preprocessor] Handle initial "MACRO&" with no space (#98684)
Summary: The prescanner checks lines that begin with a keyword macro name to see whether they should be treated as a comment or compiler directive instead of a source line. This fails when the potential keyword macro name is extended with identifier characters via Fortran line continuation. Disable line continuation during this check. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250881
1 parent 55a4e7e commit 11450c0

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

flang/lib/Parser/prescan.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,19 @@ void Prescanner::Statement() {
189189
// a comment marker or directive sentinel. If so, disable line
190190
// continuation, so that NextToken() won't consume anything from
191191
// following lines.
192-
if (IsLegalIdentifierStart(*at_) && NextToken(tokens) &&
193-
tokens.SizeInTokens() > 0) {
194-
if (CharBlock id{tokens.TokenAt(0)}; preprocessor_.IsNameDefined(id) &&
192+
if (IsLegalIdentifierStart(*at_)) {
193+
// TODO: Only bother with these cases when any keyword macro has
194+
// been defined with replacement text that could begin a comment
195+
// or directive sentinel.
196+
const char *p{at_};
197+
while (IsLegalInIdentifier(*++p)) {
198+
}
199+
CharBlock id{at_, static_cast<std::size_t>(p - at_)};
200+
if (preprocessor_.IsNameDefined(id) &&
195201
!preprocessor_.IsFunctionLikeDefinition(id)) {
196-
if (auto replaced{preprocessor_.MacroReplacement(tokens, *this)}) {
202+
TokenSequence toks;
203+
toks.Put(id, GetProvenance(at_));
204+
if (auto replaced{preprocessor_.MacroReplacement(toks, *this)}) {
197205
auto newLineClass{ClassifyLine(*replaced, GetCurrentProvenance())};
198206
disableSourceContinuation_ =
199207
newLineClass.kind != LineClassification::Kind::Source;

flang/test/Preprocessing/directive-contin-with-pp.F90

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
module m
1313
contains
14-
subroutine s(x1, x2, x3, x4, x5, x6, x7)
14+
subroutine s1(x1, x2, x3, x4, x5, x6, x7)
1515

1616
!dir$ ignore_tkr x1
1717

@@ -51,11 +51,18 @@ subroutine s(x1, x2, x3, x4, x5, x6, x7)
5151
do j3 = 1, n
5252
end do
5353
end
54-
end
54+
55+
COMMENT &
56+
subroutine s2
57+
end subroutine
58+
COMMENT&
59+
subroutine s3
60+
end subroutine
61+
end module
5562

5663
!CHECK: MODULE m
5764
!CHECK: CONTAINS
58-
!CHECK: SUBROUTINE s (x1, x2, x3, x4, x5, x6, x7)
65+
!CHECK: SUBROUTINE s1 (x1, x2, x3, x4, x5, x6, x7)
5966
!CHECK: !DIR$ IGNORE_TKR x1
6067
!CHECK: !DIR$ IGNORE_TKR x2
6168
!CHECK: !DIR$ IGNORE_TKR x3
@@ -73,4 +80,8 @@ subroutine s(x1, x2, x3, x4, x5, x6, x7)
7380
!CHECK: DO j3=1_4,n
7481
!CHECK: END DO
7582
!CHECK: END SUBROUTINE
83+
!CHECK: SUBROUTINE s2
84+
!CHECK: END SUBROUTINE
85+
!CHECK: SUBROUTINE s3
86+
!CHECK: END SUBROUTINE
7687
!CHECK: END MODULE

0 commit comments

Comments
 (0)