Skip to content

Conversation

@luporl
Copy link
Contributor

@luporl luporl commented Feb 7, 2025

In fixed form, an initial line can have an OpenMP conditional
compilation sentinel only if columns 3 through 5 have only
white space or numbers.

Fixes #89560

In fixed form, an initial line can have an OpenMP conditional
compilation sentinel only if columns 3 through 5 have only
white space or numbers.

Fixes llvm#89560
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:openmp flang:parser labels Feb 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/pr-subscribers-flang-parser

@llvm/pr-subscribers-flang-openmp

Author: Leandro Lupori (luporl)

Changes

In fixed form, an initial line can have an OpenMP conditional
compilation sentinel only if columns 3 through 5 have only
white space or numbers.

Fixes #89560


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

2 Files Affected:

  • (modified) flang/lib/Parser/prescan.cpp (+15)
  • (modified) flang/test/Parser/OpenMP/sentinels.f (+30)
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index c5939a1e0b6c2c7..f479cc51871ef77 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -1432,6 +1432,21 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
     }
     *sp++ = ToLowerCaseLetter(*p);
   }
+  // A fixed form OpenMP conditional compilation sentinel must satisfy the
+  // following criteria, for initial lines:
+  // - Columns 3 through 5 must have only white space or numbers.
+  // - Column 6 must be space or zero.
+  if (column == 3 && sentinel[0] == '$') {
+    const char *q{p};
+    for (int col{3}; col < 6; ++col, ++q) {
+      if (!IsSpaceOrTab(q) && !IsDecimalDigit(*q)) {
+        return std::nullopt;
+      }
+    }
+    if (*q != ' ' && *q != '0') {
+      return std::nullopt;
+    }
+  }
   if (column == 6) {
     if (*p == '0') {
       ++p;
diff --git a/flang/test/Parser/OpenMP/sentinels.f b/flang/test/Parser/OpenMP/sentinels.f
index 98d4bad19f6a274..f41ff13bcdd3479 100644
--- a/flang/test/Parser/OpenMP/sentinels.f
+++ b/flang/test/Parser/OpenMP/sentinels.f
@@ -39,4 +39,34 @@ subroutine sub(a, b)
 C $   This is a comment line
 c $   his is a comment line
 * $   This is a comment line
+
+! Test non-space/non-number char in columns 3-5, for initial lines.
+! CHECK-NOT:  "comment"
+c$x   PRINT *, "comment"
+c$ +  PRINT *, "comment"
+c$  * PRINT *, "comment"
+
+! Test non-space/non-number char in columns 3-5, for continuation lines.
+! CHECK:      "msg1"
+! CHECK-NOT:  "comment"
+c$ x  PRINT *, "comment"
+c$1  &         , "comment"
+c$ x &         , "comment"
+c$  +&         , "comment"
+
+c$    PRINT *, "msg1"
+c$1  &         , "comment"
+c$ x &         , "comment"
+c$  +&         , "comment"
+
+! Test valid chars in initial and continuation lines.
+! CHECK:      "msg2"
+! CHECK-SAME: "msg3"
+c$ 20 PRINT *, "msg2"
+c$   &         , "msg3"
+
+! CHECK:      "msg4"
+! CHECK-SAME: "msg5"
+c$   0PRINT *, "msg4",
+c$   +         "msg5"
       end

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

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

Thanks for the patch. A. couple of questions.

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

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

LG.

@luporl luporl merged commit 46ffacc into llvm:main Feb 13, 2025
12 checks passed
@luporl luporl deleted the luporl-omp-cond-comp branch February 13, 2025 12:36
@luporl
Copy link
Contributor Author

luporl commented Feb 13, 2025

Thanks for the review @kiranchandramohan.

joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…126282)

In fixed form, an initial line can have an OpenMP conditional
compilation sentinel only if columns 3 through 5 have only
white space or numbers.

Fixes llvm#89560
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…126282)

In fixed form, an initial line can have an OpenMP conditional
compilation sentinel only if columns 3 through 5 have only
white space or numbers.

Fixes llvm#89560
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:openmp flang:parser flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Flang][OpenMP] Compilation error of Fixed Source Form Conditional Compilation Sentinels

3 participants