Skip to content

Conversation

@Meinersbur
Copy link
Member

@Meinersbur Meinersbur commented Nov 4, 2025

Directives cannot be nested. A directive sentinel that appears within another directive should be ignored, and instead fall back to be treated as a line comment.

Fixes: #165874

@Meinersbur Meinersbur requested a review from klausler November 4, 2025 11:06
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:openmp flang:parser labels Nov 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 4, 2025

@llvm/pr-subscribers-flang-openmp

@llvm/pr-subscribers-flang-parser

Author: Michael Kruse (Meinersbur)

Changes

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


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

2 Files Affected:

  • (modified) flang/lib/Parser/prescan.cpp (+1-1)
  • (added) flang/test/Parser/OpenMP/nested-directive.f90 (+7)
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

@Meinersbur
Copy link
Member Author

@scamp-nvidia @EbinJose2002 I cannot add you as a reviewer. Feel free to review anyway.

@Meinersbur Meinersbur merged commit 2dc0fa1 into llvm:main Nov 4, 2025
14 checks passed
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] Flang inline directive regression

3 participants