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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-parser

Author: Tom Eccles (tblah)

<details>
<summary>Changes</summary>

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.

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


2 Files Affected:

- (modified) flang/lib/Parser/prescan.cpp (+3-1) 
- (added) flang/test/Preprocessing/omp-sentinel-fixed-form.F (+21) 


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

``````````

</details>


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


More information about the flang-commits mailing list