Skip to content

[flang] Rework preprocessor fix for replacement in kind suffixes #135406

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
Apr 14, 2025
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
70 changes: 38 additions & 32 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,51 +891,57 @@ bool Prescanner::HandleExponent(TokenSequence &tokens) {
if (char ed{ToLowerCaseLetter(*at_)}; ed == 'e' || ed == 'd') {
// Do some look-ahead to ensure that this 'e'/'d' is an exponent,
// not the start of an identifier that could be a macro.
const char *p{SkipWhiteSpace(at_ + 1)};
if (*p == '+' || *p == '-') {
p = SkipWhiteSpace(p + 1);
}
if (IsDecimalDigit(*p)) { // it's an exponent
EmitCharAndAdvance(tokens, ed);
if (*at_ == '+' || *at_ == '-') {
EmitCharAndAdvance(tokens, *at_);
}
const char *startAt{at_};
int startColumn{column_};
TokenSequence possible;
EmitCharAndAdvance(possible, *at_);
if (*at_ == '+' || *at_ == '-') {
EmitCharAndAdvance(possible, *at_);
}
if (IsDecimalDigit(*at_)) { // it's an exponent; scan it
while (IsDecimalDigit(*at_)) {
EmitCharAndAdvance(tokens, *at_);
EmitCharAndAdvance(possible, *at_);
}
possible.CloseToken();
tokens.CloseToken();
tokens.Put(possible);
return true;
}
// Not an exponent; backtrack
at_ = startAt;
column_ = startColumn;
}
return false;
}

bool Prescanner::HandleKindSuffix(TokenSequence &tokens) {
if (*at_ == '_' && IsLegalInIdentifier(at_[1])) {
// The kind specifier might be a macro (with or without its leading
// underscore); put it into its own token if it has been defined.
const char *p{at_ + 1};
while (IsLegalInIdentifier(*++p)) {
}
if (CharBlock id{at_, static_cast<std::size_t>(p - at_)};
preprocessor_.IsNameDefined(id)) {
// In 1.0e0_foo, "_foo" is a defined name; retain the
// underscore
tokens.CloseToken();
} else {
EmitCharAndAdvance(tokens, '_');
if (CharBlock id{at_, static_cast<std::size_t>(p - at_)};
preprocessor_.IsNameDefined(id)) {
// In 1.0e0_foo, "foo" is a defined name
tokens.CloseToken();
}
}
if (*at_ != '_') {
return false;
}
TokenSequence withUnderscore, separate;
EmitChar(withUnderscore, '_');
EmitCharAndAdvance(separate, '_');
if (IsLegalInIdentifier(*at_)) {
separate.CloseToken();
EmitChar(withUnderscore, *at_);
EmitCharAndAdvance(separate, *at_);
while (IsLegalInIdentifier(*at_)) {
EmitCharAndAdvance(tokens, *at_);
EmitChar(withUnderscore, *at_);
EmitCharAndAdvance(separate, *at_);
}
return true;
}
withUnderscore.CloseToken();
separate.CloseToken();
tokens.CloseToken();
if (separate.SizeInTokens() == 2 &&
preprocessor_.IsNameDefined(separate.TokenAt(1)) &&
!preprocessor_.IsNameDefined(withUnderscore.ToCharBlock())) {
// "_foo" is not defined, but "foo" is
tokens.Put(separate); // '_' "foo"
} else {
return false;
tokens.Put(withUnderscore); // "_foo"
}
return true;
}

bool Prescanner::HandleExponentAndOrKindSuffix(TokenSequence &tokens) {
Expand Down
5 changes: 5 additions & 0 deletions flang/test/Preprocessing/bug518.F
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
! RUN: %flang -fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
! CHECK: k=1_4
k= 1_99999999
&4
end