Skip to content

[flang][preprocessor] Expand some keyword macros in quoted character … #96987

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
Jun 28, 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
35 changes: 35 additions & 0 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,41 @@ void Prescanner::QuotedCharacterLiteral(
}
break;
}
// Here's a weird edge case. When there's a two or more following
// continuation lines at this point, and the entire significant part of
// the next continuation line is the name of a keyword macro, replace
// it in the character literal with its definition. Example:
// #define FOO foo
// subroutine subr() bind(c, name="my_&
// &FOO&
// &_bar") ...
// produces a binding name of "my_foo_bar".
while (at_[1] == '&' && nextLine_ < limit_ && !InFixedFormSource()) {
const char *idStart{nextLine_};
if (const char *amper{SkipWhiteSpace(nextLine_)}; *amper == '&') {
idStart = amper + 1;
}
if (IsLegalIdentifierStart(*idStart)) {
std::size_t idLen{1};
for (; IsLegalInIdentifier(idStart[idLen]); ++idLen) {
}
if (idStart[idLen] == '&') {
CharBlock id{idStart, idLen};
if (preprocessor_.IsNameDefined(id)) {
TokenSequence ppTokens;
ppTokens.Put(id, GetProvenance(idStart));
if (auto replaced{
preprocessor_.MacroReplacement(ppTokens, *this)}) {
tokens.Put(*replaced);
at_ = &idStart[idLen - 1];
NextLine();
continue; // try again on the next line
}
}
}
}
break;
}
end = at_ + 1;
NextChar();
if (*at_ == quote && !isEscaped) {
Expand Down
14 changes: 14 additions & 0 deletions flang/test/Preprocessing/kw-in-char.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
! RUN: %flang -E %s 2>&1 | FileCheck %s
! CHECK: subroutine test_b_wrapper_c() bind(C, name="test_b_c_f")
#define TEMP_LETTER b
#define VAL c
subroutine test_&
TEMP_LETTER&
_wrapper_&
VAL&
() bind(C, name="test_&
&TEMP_LETTER&
&_&
&VAL&
&_f")
end
Loading