Skip to content

Commit be133ff

Browse files
authored
[flang] Fix preprocessor regression (#134405)
For numeric kind suffixes like 1_foo, the preprocessor should be able to perform macro replacement for macros named either "_foo" or "foo". Fixes #133399.
1 parent b92f4d4 commit be133ff

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

flang/lib/Parser/prescan.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,11 +892,24 @@ bool Prescanner::HandleExponent(TokenSequence &tokens) {
892892
}
893893

894894
bool Prescanner::HandleKindSuffix(TokenSequence &tokens) {
895-
if (*at_ == '_' && IsLegalInIdentifier(*SkipWhiteSpace(at_ + 1))) {
896-
EmitCharAndAdvance(tokens, *at_);
897-
if (IsLegalIdentifierStart(*at_)) {
898-
// The kind specifier might be a macro, so put it into its own token.
895+
if (*at_ == '_' && IsLegalInIdentifier(at_[1])) {
896+
// The kind specifier might be a macro (with or without its leading
897+
// underscore); put it into its own token if it has been defined.
898+
const char *p{at_ + 1};
899+
while (IsLegalInIdentifier(*++p)) {
900+
}
901+
if (CharBlock id{at_, static_cast<std::size_t>(p - at_)};
902+
preprocessor_.IsNameDefined(id)) {
903+
// In 1.0e0_foo, "_foo" is a defined name; retain the
904+
// underscore
899905
tokens.CloseToken();
906+
} else {
907+
EmitCharAndAdvance(tokens, '_');
908+
if (CharBlock id{at_, static_cast<std::size_t>(p - at_)};
909+
preprocessor_.IsNameDefined(id)) {
910+
// In 1.0e0_foo, "foo" is a defined name
911+
tokens.CloseToken();
912+
}
900913
}
901914
while (IsLegalInIdentifier(*at_)) {
902915
EmitCharAndAdvance(tokens, *at_);

flang/lib/Parser/token-sequence.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ TokenSequence &TokenSequence::ToLowerCase() {
187187
} else if (*p == 'h' || *p == 'H') {
188188
// Hollerith
189189
*p = 'h';
190-
} else if (*p == '_') {
190+
} else if (*p == '_' && p + 1 < limit && (p[1] == '"' || p[1] == '\'')) {
191191
// kind-prefixed character literal (e.g., 1_"ABC")
192192
} else {
193193
// exponent
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
! RUN: %flang -E %s 2>&1 | FileCheck %s
22
#define n k
3+
#define _m _p
34
parameter(n=4)
45
!CHECK: print *,1_k
56
print *,1_n
7+
!CHECK: print *,1_p
8+
print *,1_m
69
end

0 commit comments

Comments
 (0)