[flang-commits] [flang] [flang][Parser] Add whitespace token after the sentinel in fixed form (PR #148825)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Tue Jul 15 04:06:00 PDT 2025


https://github.com/tblah created https://github.com/llvm/llvm-project/pull/148825

Fixes #148386

The first time the line was classified (using
`Prescanner::ClassifyLine(const char *)`) the line was correctly classified as a compiler directive. But then later on the token form is invoked (`Prescanner::ClassifyLine(TokenSequence, Provenance)`). This one incorrectly classified the line as a comment because there was no whitespace token right after the sentinel. This fixes the issue by ensuring this whitespace is added.

>From a377e70dcc587b554041cc02f542246bb6fc143a Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Tue, 15 Jul 2025 10:59:32 +0000
Subject: [PATCH] [flang][Parser] Add whitespace token after the sentinel in
 fixed form

Fixes #148386

The first time the line was classified (using
`Prescanner::ClassifyLine(const char *)`) the line was correctly
classified as a compiler directive. But then later on the token form is
invoked (`Prescanner::ClassifyLine(TokenSequence, Provenance)`). This
one incorrectly classified the line as a comment because there was no
whitespace token right after the sentinel. This fixes the issue by
ensuring this whitespace is added.
---
 flang/lib/Parser/prescan.cpp                  |  4 +++-
 .../Preprocessing/omp-sentinel-fixed-form.F   | 21 +++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Preprocessing/omp-sentinel-fixed-form.F

diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index ec894ab8513d2..3a9a475c365ee 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -179,7 +179,9 @@ void Prescanner::Statement() {
         EmitChar(tokens, *sp);
       }
       if (inFixedForm_) {
-        while (column_ < 6) {
+        // We need to add the whitespace after the sentinel because otherwise
+        // the line cannot be re-categorised as a compiler directive.
+        while (column_ <= 6) {
           if (*at_ == '\t') {
             tabInCurrentLine_ = true;
             ++at_;
diff --git a/flang/test/Preprocessing/omp-sentinel-fixed-form.F b/flang/test/Preprocessing/omp-sentinel-fixed-form.F
new file mode 100644
index 0000000000000..c8271682a5b92
--- /dev/null
+++ b/flang/test/Preprocessing/omp-sentinel-fixed-form.F
@@ -0,0 +1,21 @@
+! RUN: %flang_fc1 -fdebug-unparse -fopenmp -fopenmp-version=50 %s | FileCheck %s
+
+#define OMP_TARGET        .true.
+#define OMP_SIMD    .false.
+      program test
+      implicit none
+      integer     i,j,n
+      n = 100
+! CHECK: !$OMP METADIRECTIVE  WHEN(USER={CONDITION(.true._4)}: TARGET TEAMS DISTRIBUTE PARALLEL&
+! CHECK: !$OMP& DO) DEFAULT(TARGET TEAMS LOOP)
+!$omp  metadirective
+!$omp& when(user={condition(OMP_TARGET.or.OMP_SIMD)}:
+!$omp&      target teams distribute parallel do )
+!$omp& default(target teams loop)
+      do i=0,n
+        do j=0,n
+          write(*,*) "Test"
+        enddo
+      enddo
+      return
+      end program



More information about the flang-commits mailing list