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

via flang-commits flang-commits at lists.llvm.org
Wed Apr 30 15:26:22 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-parser

Author: Eugene Epshteyn (eugeneepshteyn)

<details>
<summary>Changes</summary>

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.
```
#ifndef XYZ42 
      PARAMETER(A=2)
#else Z
      PARAMETER(A=3)
#endif
      end
```

---
Full diff: https://github.com/llvm/llvm-project/pull/138045.diff


2 Files Affected:

- (modified) flang/lib/Parser/preprocessor.cpp (+3-1) 
- (added) flang/test/Preprocessing/pp048.F (+11) 


``````````diff
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) {
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
+

``````````

</details>


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


More information about the flang-commits mailing list