[flang-commits] [flang] [flang] Fix #else with trailing text (PR #138045)

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Thu May 1 17:01:18 PDT 2025


https://github.com/eugeneepshteyn updated https://github.com/llvm/llvm-project/pull/138045

>From e3f856c692eec2d5e82116e369bd51f3964256fa Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Wed, 30 Apr 2025 18:07:41 -0400
Subject: [PATCH 1/3] [flang] Fix #else with trailing text

Fixed the issue, where the extra text on #else line (' Z' in the example
below) caused the data from the "else" clause to be processed together
with the data of "then" clause.

```
      PARAMETER(A=2)
      PARAMETER(A=3)
      end
```
---
 flang/lib/Parser/preprocessor.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index a47f9c32ad27c..1e984896ea4ed 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -684,7 +684,9 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner &prescanner) {
             dir.GetIntervalProvenanceRange(j, tokens - j),
             "#else: excess tokens at end of directive"_port_en_US);
       }
-    } else if (ifStack_.empty()) {
+    }
+
+    if (ifStack_.empty()) {
       prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
           "#else: not nested within #if, #ifdef, or #ifndef"_err_en_US);
     } else if (ifStack_.top() != CanDeadElseAppear::Yes) {

>From c7daada0d3a684d7e5c9ff8c484b1d110ed17843 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Wed, 30 Apr 2025 18:23:55 -0400
Subject: [PATCH 2/3] Test

---
 flang/test/Preprocessing/pp048.F | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 flang/test/Preprocessing/pp048.F

diff --git a/flang/test/Preprocessing/pp048.F b/flang/test/Preprocessing/pp048.F
new file mode 100644
index 0000000000000..121262c1840f9
--- /dev/null
+++ b/flang/test/Preprocessing/pp048.F
@@ -0,0 +1,11 @@
+! RUN: %flang -E %s 2>&1 | FileCheck %s
+#ifndef XYZ42 
+      PARAMETER(A=2)
+#else Z
+      PARAMETER(A=3)
+#endif
+! Ensure that "PARAMETER(A" is printed only once
+! CHECK: PARAMETER(A
+! CHECK-NOT: PARAMETER(A
+      end
+

>From 17f057b31e857c0bd58c3017559db0e2913e1da5 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Wed, 30 Apr 2025 18:34:01 -0400
Subject: [PATCH 3/3] Removed the blank line

---
 flang/lib/Parser/preprocessor.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index 1e984896ea4ed..6e8e3aee19b09 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -685,7 +685,6 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner &prescanner) {
             "#else: excess tokens at end of directive"_port_en_US);
       }
     }
-
     if (ifStack_.empty()) {
       prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
           "#else: not nested within #if, #ifdef, or #ifndef"_err_en_US);



More information about the flang-commits mailing list