Skip to content

Conversation

@eugeneepshteyn
Copy link
Contributor

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

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
```
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Apr 30, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 30, 2025

@llvm/pr-subscribers-flang-parser

Author: Eugene Epshteyn (eugeneepshteyn)

Changes

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 --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
+

Copy link
Contributor

@akuhlens akuhlens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eugeneepshteyn eugeneepshteyn merged commit 36541ec into llvm:main May 2, 2025
11 checks passed
@eugeneepshteyn eugeneepshteyn deleted the fix-else branch May 2, 2025 03:07
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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
```
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
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
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:parser flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants