Skip to content

[flang] Fix prescanner bug w/ empty macros in line continuation #141274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025

Conversation

klausler
Copy link
Contributor

When processing free form source line continuation, the prescanner treats empty keyword macros as if they were spaces or tabs. After skipping over them, however, there's code that only works if the skipped characters ended with an actual space or tab. If the last skipped item was an empty keyword macro's name, the last character of that name would end up being the first character of the continuation line. Fix.

When processing free form source line continuation, the prescanner
treats empty keyword macros as if they were spaces or tabs.  After
skipping over them, however, there's code that only works if the
skipped characters ended with an actual space or tab.  If the
last skipped item was an empty keyword macro's name, the last
character of that name would end up being the first character of
the continuation line.  Fix.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels May 23, 2025
@llvmbot
Copy link
Member

llvmbot commented May 23, 2025

@llvm/pr-subscribers-flang-parser

Author: Peter Klausler (klausler)

Changes

When processing free form source line continuation, the prescanner treats empty keyword macros as if they were spaces or tabs. After skipping over them, however, there's code that only works if the skipped characters ended with an actual space or tab. If the last skipped item was an empty keyword macro's name, the last character of that name would end up being the first character of the continuation line. Fix.


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

2 Files Affected:

  • (modified) flang/lib/Parser/prescan.cpp (+1-1)
  • (added) flang/test/Preprocessing/bug890.F90 (+6)
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 3bc2ea0b37508..9aef0c9981e3c 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -1473,7 +1473,7 @@ const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
             GetProvenanceRange(p, p + 1),
             "Character literal continuation line should have been preceded by '&'"_port_en_US);
       }
-    } else if (p > lineStart) {
+    } else if (p > lineStart && IsSpaceOrTab(p - 1)) {
       --p;
     } else {
       insertASpace_ = true;
diff --git a/flang/test/Preprocessing/bug890.F90 b/flang/test/Preprocessing/bug890.F90
new file mode 100644
index 0000000000000..0ce2d8c3f1569
--- /dev/null
+++ b/flang/test/Preprocessing/bug890.F90
@@ -0,0 +1,6 @@
+! RUN: %flang -E %s 2>&1 | FileCheck %s
+!CHECK: subroutine sub()
+#define empty
+subroutine sub ( &
+  empty)
+end

@klausler klausler merged commit f48f844 into llvm:main May 28, 2025
10 of 12 checks passed
google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
…#141274)

When processing free form source line continuation, the prescanner
treats empty keyword macros as if they were spaces or tabs. After
skipping over them, however, there's code that only works if the skipped
characters ended with an actual space or tab. If the last skipped item
was an empty keyword macro's name, the last character of that name would
end up being the first character of the continuation line. Fix.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
…#141274)

When processing free form source line continuation, the prescanner
treats empty keyword macros as if they were spaces or tabs. After
skipping over them, however, there's code that only works if the skipped
characters ended with an actual space or tab. If the last skipped item
was an empty keyword macro's name, the last character of that name would
end up being the first character of the continuation line. Fix.
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