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

via flang-commits flang-commits at lists.llvm.org
Thu May 1 20:07:56 PDT 2025


Author: Eugene Epshteyn
Date: 2025-05-01T23:07:52-04:00
New Revision: 36541ec3ca7027aec87118262773b35964c0edec

URL: https://github.com/llvm/llvm-project/commit/36541ec3ca7027aec87118262773b35964c0edec
DIFF: https://github.com/llvm/llvm-project/commit/36541ec3ca7027aec87118262773b35964c0edec.diff

LOG: [flang] Fix #else with trailing text (#138045)

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

Added: 
    flang/test/Preprocessing/pp048.F

Modified: 
    flang/lib/Parser/preprocessor.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index a47f9c32ad27c..6e8e3aee19b09 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -684,7 +684,8 @@ 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
+


        


More information about the flang-commits mailing list