[flang-commits] [flang] [flang] Fix preprocessor regression (PR #134405)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Apr 4 09:02:51 PDT 2025


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/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 https://github.com/llvm/llvm-project/issues/133399.

>From 9b97ac910970a200941ec423da73a69132bca799 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 1 Apr 2025 13:29:31 -0700
Subject: [PATCH] [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 https://github.com/llvm/llvm-project/issues/133399.
---
 flang/lib/Parser/prescan.cpp             | 21 +++++++++++++++++----
 flang/lib/Parser/token-sequence.cpp      |  2 +-
 flang/test/Preprocessing/kind-suffix.F90 |  3 +++
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 755cb18cb8caf..91c10b2e48cf7 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -875,11 +875,24 @@ bool Prescanner::HandleExponent(TokenSequence &tokens) {
 }
 
 bool Prescanner::HandleKindSuffix(TokenSequence &tokens) {
-  if (*at_ == '_' && IsLegalInIdentifier(*SkipWhiteSpace(at_ + 1))) {
-    EmitCharAndAdvance(tokens, *at_);
-    if (IsLegalIdentifierStart(*at_)) {
-      // The kind specifier might be a macro, so put it into its own token.
+  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();
+      }
     }
     while (IsLegalInIdentifier(*at_)) {
       EmitCharAndAdvance(tokens, *at_);
diff --git a/flang/lib/Parser/token-sequence.cpp b/flang/lib/Parser/token-sequence.cpp
index cdbe89b1eb441..c3016707b8ebf 100644
--- a/flang/lib/Parser/token-sequence.cpp
+++ b/flang/lib/Parser/token-sequence.cpp
@@ -187,7 +187,7 @@ TokenSequence &TokenSequence::ToLowerCase() {
       } else if (*p == 'h' || *p == 'H') {
         // Hollerith
         *p = 'h';
-      } else if (*p == '_') {
+      } else if (*p == '_' && p + 1 < limit && (p[1] == '"' || p[1] == '\'')) {
         // kind-prefixed character literal (e.g., 1_"ABC")
       } else {
         // exponent
diff --git a/flang/test/Preprocessing/kind-suffix.F90 b/flang/test/Preprocessing/kind-suffix.F90
index 36aa323630c6c..ea2a3daaed40d 100644
--- a/flang/test/Preprocessing/kind-suffix.F90
+++ b/flang/test/Preprocessing/kind-suffix.F90
@@ -1,6 +1,9 @@
 ! RUN: %flang -E %s 2>&1 | FileCheck %s
 #define n k
+#define _m _p
 parameter(n=4)
 !CHECK: print *,1_k
 print *,1_n
+!CHECK: print *,1_p
+print *,1_m
 end



More information about the flang-commits mailing list