[flang-commits] [flang] [flang][parser] handle semicolons uniformly in program unit constructs (PR #181180)

Andre Kuhlenschmidt via flang-commits flang-commits at lists.llvm.org
Wed Mar 4 15:01:25 PST 2026


https://github.com/akuhlens updated https://github.com/llvm/llvm-project/pull/181180

>From 026101b50f5d52d956474c323918d442f728235a Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Mon, 9 Feb 2026 15:20:51 -0800
Subject: [PATCH 1/3] initial commit

---
 flang/lib/Parser/program-parsers.cpp | 11 ++++++-----
 flang/test/Parser/bug176914.f90      | 16 ++++++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)
 create mode 100644 flang/test/Parser/bug176914.f90

diff --git a/flang/lib/Parser/program-parsers.cpp b/flang/lib/Parser/program-parsers.cpp
index dac377c29d9db..7aea0ad59ae96 100644
--- a/flang/lib/Parser/program-parsers.cpp
+++ b/flang/lib/Parser/program-parsers.cpp
@@ -68,9 +68,9 @@ static constexpr auto programUnit{
         construct<ProgramUnit>(indirect(functionSubprogram)) ||
     construct<ProgramUnit>(indirect(Parser<MainProgram>{}))};
 
-static constexpr auto normalProgramUnit{
-    !consumedAllInput >> StartNewSubprogram{} >> programUnit /
-        skipMany(";"_tok) / space / recovery(endOfLine, skipToNextLineIfAny)};
+static constexpr auto normalProgramUnit{!consumedAllInput >>
+    StartNewSubprogram{} >>
+    programUnit / recovery(endOfStmt, skipToNextLineIfAny)};
 
 static constexpr auto globalCompilerDirective{
     construct<ProgramUnit>(indirect(compilerDirective))};
@@ -91,8 +91,9 @@ TYPE_PARSER(
                            "nonstandard usage: empty source file"_port_en_US,
                            skipStuffBeforeStatement >> consumedAllInput >>
                                pure<std::list<ProgramUnit>>()) ||
-        some(globalCompilerDirective || globalOpenACCCompilerDirective ||
-            normalProgramUnit) /
+        some(skipStuffBeforeStatement >> (globalCompilerDirective ||
+                                             globalOpenACCCompilerDirective ||
+                                             normalProgramUnit)) /
             skipStuffBeforeStatement))
 
 // R507 declaration-construct ->
diff --git a/flang/test/Parser/bug176914.f90 b/flang/test/Parser/bug176914.f90
new file mode 100644
index 0000000000000..ee31a1d295ec8
--- /dev/null
+++ b/flang/test/Parser/bug176914.f90
@@ -0,0 +1,16 @@
+!RUN: %flang_fc1 -fdebug-dump-parse-tree-no-sema %s 2>&1 | FileCheck %s
+! CHECK: Program -> ProgramUnit -> MainProgram
+! CHECK-NEXT: | ProgramStmt -> Name = 'semicolon'
+; program semicolon
+use semi;
+IMPLICIT NONE  ;  
+testvar2 = testvar
+;; ; intvar = testvar + testvar2;intvar2=intvar;
+print *,testvar , testvar2, intvar, intvar2
+! CHECK: | EndProgramStmt
+! CHECK: ProgramUnit -> SubroutineSubprogram
+! CHECK-NEXT: | SubroutineStmt
+! CHECK-NEXT: | | Name = 'test'
+end ; ; subroutine test( arg1, arg2 );real arg1, arg2;;;; arg1=arg2; ; ; end;;;
+;;
+! CHECK: | EndSubroutineStmt
\ No newline at end of file

>From c94a659e7f813bd0860ab24d0a488e72224a24a8 Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Mon, 23 Feb 2026 14:51:47 -0800
Subject: [PATCH 2/3] refactor program parser

---
 flang/lib/Parser/program-parsers.cpp | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/flang/lib/Parser/program-parsers.cpp b/flang/lib/Parser/program-parsers.cpp
index 7aea0ad59ae96..1dd932ec79aca 100644
--- a/flang/lib/Parser/program-parsers.cpp
+++ b/flang/lib/Parser/program-parsers.cpp
@@ -86,15 +86,14 @@ static constexpr auto globalOpenACCCompilerDirective{
 // Consequently, a program unit END statement should be the last statement
 // on its line.  We parse those END statements via unterminatedStatement()
 // and then skip over the end of the line here.
-TYPE_PARSER(
-    construct<Program>(extension<LanguageFeature::EmptySourceFile>(
-                           "nonstandard usage: empty source file"_port_en_US,
-                           skipStuffBeforeStatement >> consumedAllInput >>
-                               pure<std::list<ProgramUnit>>()) ||
+TYPE_PARSER(construct<Program>(skipStuffBeforeStatement >>
+    (extension<LanguageFeature::EmptySourceFile>(
+         "nonstandard usage: empty source file"_port_en_US,
+         consumedAllInput >> pure<std::list<ProgramUnit>>()) ||
         some(skipStuffBeforeStatement >> (globalCompilerDirective ||
                                              globalOpenACCCompilerDirective ||
                                              normalProgramUnit)) /
-            skipStuffBeforeStatement))
+            skipStuffBeforeStatement)))
 
 // R507 declaration-construct ->
 //        specification-construct | data-stmt | format-stmt |

>From 4cdaac36c54653adb291a72db283eb2988fc4551 Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Wed, 4 Mar 2026 15:00:00 -0800
Subject: [PATCH 3/3] add tests

---
 flang/test/Parser/bug176914.did-work.0.f90   |  2 ++
 flang/test/Parser/bug176914.did-work.1.f90   | 11 +++++++++++
 flang/test/Parser/bug176914.did-work.2.f90   |  9 +++++++++
 flang/test/Parser/bug176914.did-work.3.f90   |  3 +++
 flang/test/Parser/bug176914.was-broken.0.f90 |  4 ++++
 flang/test/Parser/bug176914.was-broken.1.f90 |  5 +++++
 flang/test/Parser/bug176914.was-broken.2.f90 |  6 ++++++
 7 files changed, 40 insertions(+)
 create mode 100644 flang/test/Parser/bug176914.did-work.0.f90
 create mode 100644 flang/test/Parser/bug176914.did-work.1.f90
 create mode 100644 flang/test/Parser/bug176914.did-work.2.f90
 create mode 100644 flang/test/Parser/bug176914.did-work.3.f90
 create mode 100644 flang/test/Parser/bug176914.was-broken.0.f90
 create mode 100644 flang/test/Parser/bug176914.was-broken.1.f90
 create mode 100644 flang/test/Parser/bug176914.was-broken.2.f90

diff --git a/flang/test/Parser/bug176914.did-work.0.f90 b/flang/test/Parser/bug176914.did-work.0.f90
new file mode 100644
index 0000000000000..4435300fc7bf5
--- /dev/null
+++ b/flang/test/Parser/bug176914.did-work.0.f90
@@ -0,0 +1,2 @@
+!RUN: %flang_fc1 -fdebug-dump-parse-tree-no-sema %s 2>&1 | FileCheck %s
+! CHECK: Program
\ No newline at end of file
diff --git a/flang/test/Parser/bug176914.did-work.1.f90 b/flang/test/Parser/bug176914.did-work.1.f90
new file mode 100644
index 0000000000000..05f55ac37032b
--- /dev/null
+++ b/flang/test/Parser/bug176914.did-work.1.f90
@@ -0,0 +1,11 @@
+!RUN: %flang_fc1 -fdebug-dump-parse-tree-no-sema %s 2>&1 | FileCheck %s
+;
+! CHECK: Program -> ProgramUnit -> CompilerDirective
+!DIR$ unknown_directive empty statement
+;
+! CHECK: ProgramUnit -> MainProgram
+program semicolon; end;
+! CHECK: ProgramUnit -> CompilerDirective
+    !DIR$ unknown_directive leading space
+! CHECK: ProgramUnit -> CompilerDirective
+;   !DIR$ unknown_directive leading empty statement
\ No newline at end of file
diff --git a/flang/test/Parser/bug176914.did-work.2.f90 b/flang/test/Parser/bug176914.did-work.2.f90
new file mode 100644
index 0000000000000..0215794bd1599
--- /dev/null
+++ b/flang/test/Parser/bug176914.did-work.2.f90
@@ -0,0 +1,9 @@
+!RUN: %flang_fc1 -fdebug-dump-parse-tree-no-sema %s 2>&1 | FileCheck %s
+! CHECK: Program -> ProgramUnit -> MainProgram
+! CHECK-NEXT: | ProgramStmt -> Name = 'semicolon'
+program semicolon; end
+;
+subroutine sub; end;
+function fn(); end;;
+; ;
+module mod; end;
\ No newline at end of file
diff --git a/flang/test/Parser/bug176914.did-work.3.f90 b/flang/test/Parser/bug176914.did-work.3.f90
new file mode 100644
index 0000000000000..cd38283a5e118
--- /dev/null
+++ b/flang/test/Parser/bug176914.did-work.3.f90
@@ -0,0 +1,3 @@
+!RUN: %flang_fc1 -fdebug-dump-parse-tree-no-sema %s 2>&1 | FileCheck %s
+! CHECK: Program
+;;
diff --git a/flang/test/Parser/bug176914.was-broken.0.f90 b/flang/test/Parser/bug176914.was-broken.0.f90
new file mode 100644
index 0000000000000..6cc6eff981cde
--- /dev/null
+++ b/flang/test/Parser/bug176914.was-broken.0.f90
@@ -0,0 +1,4 @@
+!RUN: %flang_fc1 -fdebug-dump-parse-tree-no-sema %s 2>&1 | FileCheck %s
+! CHECK: Program -> ProgramUnit -> MainProgram
+program semicolon; end
+;
diff --git a/flang/test/Parser/bug176914.was-broken.1.f90 b/flang/test/Parser/bug176914.was-broken.1.f90
new file mode 100644
index 0000000000000..a7ce66b88d98c
--- /dev/null
+++ b/flang/test/Parser/bug176914.was-broken.1.f90
@@ -0,0 +1,5 @@
+!RUN: %flang_fc1 -fdebug-dump-parse-tree-no-sema %s 2>&1 | FileCheck %s
+! CHECK: Program -> ProgramUnit -> MainProgram
+! CHECK: ProgramUnit -> SubroutineSubprogram
+program semicolon; end; subroutine sub; end;
+
diff --git a/flang/test/Parser/bug176914.was-broken.2.f90 b/flang/test/Parser/bug176914.was-broken.2.f90
new file mode 100644
index 0000000000000..6cb832d5f926b
--- /dev/null
+++ b/flang/test/Parser/bug176914.was-broken.2.f90
@@ -0,0 +1,6 @@
+!RUN: %flang_fc1 -fdebug-dump-parse-tree-no-sema %s 2>&1 | FileCheck %s
+! CHECK: Program -> ProgramUnit -> SubroutineSubprogram
+subroutine sub; end
+;
+! CHECK: ProgramUnit -> FunctionSubprogram
+function fn(); end



More information about the flang-commits mailing list