Skip to content

Commit 9b97ac9

Browse files
committed
[flang] Fix preprocessor regression
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 1bef59c commit 9b97ac9

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
@@ -875,11 +875,24 @@ bool Prescanner::HandleExponent(TokenSequence &tokens) {
875875
}
876876

877877
bool Prescanner::HandleKindSuffix(TokenSequence &tokens) {
878-
if (*at_ == '_' && IsLegalInIdentifier(*SkipWhiteSpace(at_ + 1))) {
879-
EmitCharAndAdvance(tokens, *at_);
880-
if (IsLegalIdentifierStart(*at_)) {
881-
// The kind specifier might be a macro, so put it into its own token.
878+
if (*at_ == '_' && IsLegalInIdentifier(at_[1])) {
879+
// The kind specifier might be a macro (with or without its leading
880+
// underscore); put it into its own token if it has been defined.
881+
const char *p{at_ + 1};
882+
while (IsLegalInIdentifier(*++p)) {
883+
}
884+
if (CharBlock id{at_, static_cast<std::size_t>(p - at_)};
885+
preprocessor_.IsNameDefined(id)) {
886+
// In 1.0e0_foo, "_foo" is a defined name; retain the
887+
// underscore
882888
tokens.CloseToken();
889+
} else {
890+
EmitCharAndAdvance(tokens, '_');
891+
if (CharBlock id{at_, static_cast<std::size_t>(p - at_)};
892+
preprocessor_.IsNameDefined(id)) {
893+
// In 1.0e0_foo, "foo" is a defined name
894+
tokens.CloseToken();
895+
}
883896
}
884897
while (IsLegalInIdentifier(*at_)) {
885898
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)