[flang-commits] [flang] [flang] Don't insert spaces in -E output after line continuation (PR #135063)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Apr 9 11:06:44 PDT 2025


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/135063

See test case.  When Fortran line continuation has been used, don't insert spaces in -E formatted output to put things into the right column, as this can break up a token.

Fixes https://github.com/llvm/llvm-project/issues/134986.

>From a09ccae1f46011d14e1c2da320ac42bf3249cfb2 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 9 Apr 2025 11:03:40 -0700
Subject: [PATCH] [flang] Don't insert spaces in -E output after line
 continuation

See test case.  When Fortran line continuation has been used,
don't insert spaces in -E formatted output to put things into
the right column, as this can break up a token.

Fixes https://github.com/llvm/llvm-project/issues/134986.
---
 flang/lib/Parser/parsing.cpp           | 34 ++++++++++++++------------
 flang/test/Preprocessing/bug134986.F90 |  5 ++++
 2 files changed, 23 insertions(+), 16 deletions(-)
 create mode 100644 flang/test/Preprocessing/bug134986.F90

diff --git a/flang/lib/Parser/parsing.cpp b/flang/lib/Parser/parsing.cpp
index 5f486cbf8e4c8..17f544194de02 100644
--- a/flang/lib/Parser/parsing.cpp
+++ b/flang/lib/Parser/parsing.cpp
@@ -155,7 +155,7 @@ void Parsing::EmitPreprocessedSource(
       const auto getOriginalChar{[&](char ch) {
         if (IsLetter(ch) && provenance && provenance->size() == 1) {
           if (const char *orig{allSources.GetSource(*provenance)}) {
-            const char upper{ToUpperCaseLetter(ch)};
+            char upper{ToUpperCaseLetter(ch)};
             if (*orig == upper) {
               return upper;
             }
@@ -184,21 +184,23 @@ void Parsing::EmitPreprocessedSource(
       std::optional<SourcePosition> position{provenance
               ? allSources.GetSourcePosition(provenance->start())
               : std::nullopt};
-      if (lineDirectives && column == 1 && position) {
-        if (&*position->path != sourcePath) {
-          out << "#line \"" << *position->path << "\" " << position->line
-              << '\n';
-        } else if (position->line != sourceLine) {
-          if (sourceLine < position->line &&
-              sourceLine + 10 >= position->line) {
-            // Emit a few newlines to catch up when they'll likely
-            // require fewer bytes than a #line directive would have
-            // occupied.
-            while (sourceLine++ < position->line) {
-              out << '\n';
+      if (column == 1 && position) {
+        if (lineDirectives) {
+          if (&*position->path != sourcePath) {
+            out << "#line \"" << *position->path << "\" " << position->line
+                << '\n';
+          } else if (position->line != sourceLine) {
+            if (sourceLine < position->line &&
+                sourceLine + 10 >= position->line) {
+              // Emit a few newlines to catch up when they'll likely
+              // require fewer bytes than a #line directive would have
+              // occupied.
+              while (sourceLine++ < position->line) {
+                out << '\n';
+              }
+            } else {
+              out << "#line " << position->line << '\n';
             }
-          } else {
-            out << "#line " << position->line << '\n';
           }
         }
         sourcePath = &*position->path;
@@ -244,7 +246,7 @@ void Parsing::EmitPreprocessedSource(
             }
           }
         } else if (!inContinuation && !inDirectiveSentinel && position &&
-            position->column <= 72) {
+            position->line == sourceLine && position->column < 72) {
           // Preserve original indentation
           for (; column < position->column; ++column) {
             out << ' ';
diff --git a/flang/test/Preprocessing/bug134986.F90 b/flang/test/Preprocessing/bug134986.F90
new file mode 100644
index 0000000000000..c97353e95b240
--- /dev/null
+++ b/flang/test/Preprocessing/bug134986.F90
@@ -0,0 +1,5 @@
+! RUN: %flang -E %s 2>&1 | FileCheck %s
+! CHECK: print *, "HELLO "//" WORLD"
+print *, "HELLO "/&
+                       &/" WORLD"
+end



More information about the flang-commits mailing list