Skip to content

[flang] Accomodate historic preprocessing usage #78868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ void Prescanner::NextChar() {
// character is reached; handles C-style comments in preprocessing
// directives, Fortran ! comments, stuff after the right margin in
// fixed form, and all forms of line continuation.
void Prescanner::SkipToNextSignificantCharacter() {
bool Prescanner::SkipToNextSignificantCharacter() {
auto anyContinuationLine{false};
if (inPreprocessorDirective_) {
SkipCComments();
} else {
Expand All @@ -449,6 +450,7 @@ void Prescanner::SkipToNextSignificantCharacter() {
mightNeedSpace = *at_ == '\n';
}
for (; Continuation(mightNeedSpace); mightNeedSpace = false) {
anyContinuationLine = true;
++continuationLines_;
if (MustSkipToEndOfLine()) {
SkipToEndOfLine();
Expand All @@ -458,6 +460,7 @@ void Prescanner::SkipToNextSignificantCharacter() {
tabInCurrentLine_ = true;
}
}
return anyContinuationLine;
}

void Prescanner::SkipCComments() {
Expand Down Expand Up @@ -625,7 +628,23 @@ bool Prescanner::NextToken(TokenSequence &tokens) {
}
preventHollerith_ = false;
} else if (IsLegalInIdentifier(*at_)) {
while (IsLegalInIdentifier(EmitCharAndAdvance(tokens, *at_))) {
int parts{1};
do {
EmitChar(tokens, *at_);
++at_, ++column_;
if (SkipToNextSignificantCharacter() && IsLegalIdentifierStart(*at_)) {
tokens.CloseToken();
++parts;
}
} while (IsLegalInIdentifier(*at_));
if (parts >= 3) {
// Subtlety: When an identifier is split across three or more continuation
// lines, its parts are kept as distinct pp-tokens so that macro
// operates on them independently. This trick accommodates the historic
// practice of using line continuation for token pasting after
// replacement.
} else if (parts == 2) {
tokens.ReopenLastToken();
}
if (InFixedFormSource()) {
SkipSpaces();
Expand Down
3 changes: 2 additions & 1 deletion flang/lib/Parser/prescan.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ class Prescanner {
void SkipToEndOfLine();
bool MustSkipToEndOfLine() const;
void NextChar();
void SkipToNextSignificantCharacter();
// True when input flowed to a continuation line
bool SkipToNextSignificantCharacter();
void SkipCComments();
void SkipSpaces();
static const char *SkipWhiteSpace(const char *);
Expand Down
9 changes: 9 additions & 0 deletions flang/test/Preprocessing/pp133.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
! RUN: %flang -E %s 2>&1 | FileCheck %s
! CHECK: print *, ADC
#define B D
implicit none
real ADC
print *, A&
&B&
&C
end