Skip to content

[flang][Parser] Add whitespace token after the sentinel in fixed form #148825

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
Jul 15, 2025

Conversation

tblah
Copy link
Contributor

@tblah tblah commented Jul 15, 2025

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.

Fixes llvm#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.
@tblah tblah requested review from klausler and kparzysz July 15, 2025 11:05
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Jul 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 15, 2025

@llvm/pr-subscribers-flang-parser

Author: Tom Eccles (tblah)

Changes

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

Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great; thanks for figuring this out.

@tblah tblah merged commit c2e41be into llvm:main Jul 15, 2025
12 checks passed
@tblah tblah added this to the LLVM 21.x Release milestone Jul 15, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Jul 15, 2025
@tblah
Copy link
Contributor Author

tblah commented Jul 15, 2025

/cherry-pick c2e41be

@tru tru moved this from Needs Triage to Done in LLVM Release Status Jul 16, 2025
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
Development

Successfully merging this pull request may close these issues.

Flang OpenMP doesn't appear to correctly process metadirectives in .F files and warn the user its not yet implemented.
3 participants