[flang-commits] [flang] [Flang] Nested directives are comments (PR #166348)

Michael Kruse via flang-commits flang-commits at lists.llvm.org
Tue Nov 4 03:06:27 PST 2025


https://github.com/Meinersbur created https://github.com/llvm/llvm-project/pull/166348

Directives cannot be nested. A directive sentinel that appears within another directive should be ignored, and instead follow the handling of a line comment (`!`).

Fixes: #165874

>From 158bba4e487718b740f92fa6c95d0663a0964981 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Tue, 4 Nov 2025 12:02:31 +0100
Subject: [PATCH] Nested directives are comments

---
 flang/lib/Parser/prescan.cpp                  | 2 +-
 flang/test/Parser/OpenMP/nested-directive.f90 | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Parser/OpenMP/nested-directive.f90

diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index efce8fc3d2e35..8cccd84f9fa19 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -557,7 +557,7 @@ bool Prescanner::MustSkipToEndOfLine() const {
     return true; // skip over ignored columns in right margin (73:80)
   } else if (*at_ == '!' && !inCharLiteral_ &&
       (!inFixedForm_ || tabInCurrentLine_ || column_ != 6)) {
-    return !IsCompilerDirectiveSentinel(at_ + 1);
+    return InCompilerDirective() || !IsCompilerDirectiveSentinel(at_ + 1);
   } else {
     return false;
   }
diff --git a/flang/test/Parser/OpenMP/nested-directive.f90 b/flang/test/Parser/OpenMP/nested-directive.f90
new file mode 100644
index 0000000000000..2a10bbe666bb8
--- /dev/null
+++ b/flang/test/Parser/OpenMP/nested-directive.f90
@@ -0,0 +1,7 @@
+! RUN: %flang_fc1 -fdebug-unparse -fopenmp %s 2>&1 | FileCheck %s --match-full-lines
+
+subroutine func
+  implicit none
+! CHECK: !$OMP NOTHING
+  !$omp nothing !$omp Cannot nest directives inside directives; must be interpreted as a comment
+end subroutine func



More information about the flang-commits mailing list