[flang-commits] [flang] [flang] Add support for additional compiler directive sentinel (PR #178941)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Fri Jan 30 11:06:57 PST 2026


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/178941

This patch allows to set up additional compiler directive sentinel in addition to the default `!dir$`. Some user code could use other vendor specific compiler directive sentinel and this solution allows to add them to the parser options. 

>From 38fae3adb042af535bd4147b5bf0792e97be6640 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Fri, 30 Jan 2026 11:04:01 -0800
Subject: [PATCH] [flang] Add support for additonnal compiler directive
 sentinel

---
 flang/include/flang/Parser/options.h              |  1 +
 flang/lib/Parser/Fortran-parsers.cpp              |  2 +-
 flang/lib/Parser/parsing.cpp                      |  3 +++
 flang/test/Parser/compiler-directive-sentinel.f90 | 14 ++++++++++++++
 flang/tools/bbc/bbc.cpp                           |  8 ++++++++
 5 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Parser/compiler-directive-sentinel.f90

diff --git a/flang/include/flang/Parser/options.h b/flang/include/flang/Parser/options.h
index dc363acb7c28a..6efe14a357615 100644
--- a/flang/include/flang/Parser/options.h
+++ b/flang/include/flang/Parser/options.h
@@ -36,6 +36,7 @@ struct Options {
   bool prescanAndReformat{false}; // -E
   bool expandIncludeLinesInPreprocessedOutput{true};
   bool showColors{false};
+  std::vector<std::string> compilerDirectivesSentinels;
 };
 
 } // namespace Fortran::parser
diff --git a/flang/lib/Parser/Fortran-parsers.cpp b/flang/lib/Parser/Fortran-parsers.cpp
index 229da72605bee..e86b5e7f79c74 100644
--- a/flang/lib/Parser/Fortran-parsers.cpp
+++ b/flang/lib/Parser/Fortran-parsers.cpp
@@ -1340,7 +1340,7 @@ constexpr auto noinlineDir{
     "NOINLINE" >> construct<CompilerDirective::NoInline>()};
 constexpr auto inlineDir{"INLINE" >> construct<CompilerDirective::Inline>()};
 constexpr auto ivdep{"IVDEP" >> construct<CompilerDirective::IVDep>()};
-TYPE_PARSER(beginDirective >> "DIR$ "_tok >>
+TYPE_PARSER(beginDirective >> some(letter) >> "$ "_tok >>
     sourced((construct<CompilerDirective>(ignore_tkr) ||
                 construct<CompilerDirective>(loopCount) ||
                 construct<CompilerDirective>(assumeAligned) ||
diff --git a/flang/lib/Parser/parsing.cpp b/flang/lib/Parser/parsing.cpp
index 2df6881146af8..c8fb51e228830 100644
--- a/flang/lib/Parser/parsing.cpp
+++ b/flang/lib/Parser/parsing.cpp
@@ -97,6 +97,9 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
     prescanner.AddCompilerDirectiveSentinel("$cuf");
     prescanner.AddCompilerDirectiveSentinel("@cuf");
   }
+  for (const auto &sentinel : options.compilerDirectivesSentinels) {
+    prescanner.AddCompilerDirectiveSentinel(sentinel);
+  }
   ProvenanceRange range{allSources.AddIncludedFile(
       *sourceFile, ProvenanceRange{}, options.isModuleFile)};
   prescanner.Prescan(range);
diff --git a/flang/test/Parser/compiler-directive-sentinel.f90 b/flang/test/Parser/compiler-directive-sentinel.f90
new file mode 100644
index 0000000000000..baa1b116f950d
--- /dev/null
+++ b/flang/test/Parser/compiler-directive-sentinel.f90
@@ -0,0 +1,14 @@
+! RUN: bbc -sentinel-test=sen$ --pft-test %s -o - 2>&1 | FileCheck %s
+
+! Test support additional compiler directive sentinels.
+
+module test_sentinel
+  interface
+  subroutine alternative_sentinel(a)
+    integer(4) :: a
+    !sen$ ignore_tkr a
+    !CHECK: CompilerDirective: !ignore_tkr a
+  end subroutine
+end interface
+
+end module test_sentinel
diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp
index 0b87dd634af1e..283366abf2605 100644
--- a/flang/tools/bbc/bbc.cpp
+++ b/flang/tools/bbc/bbc.cpp
@@ -237,6 +237,11 @@ static llvm::cl::opt<std::string>
     enableGPUMode("gpu", llvm::cl::desc("Enable GPU Mode managed|unified"),
                   llvm::cl::init(""));
 
+static llvm::cl::opt<std::string>
+    compilerDirectiveSentinel("sentinel-test",
+                              llvm::cl::desc("Test additional sentinel"),
+                              llvm::cl::init("dir$"));
+
 static llvm::cl::opt<bool> fixedForm("ffixed-form",
                                      llvm::cl::desc("enable fixed form"),
                                      llvm::cl::init(false));
@@ -369,6 +374,9 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
 
   // prep for prescan and parse
   Fortran::parser::Parsing parsing{semanticsContext.allCookedSources()};
+  if (!compilerDirectiveSentinel.empty()) {
+    options.compilerDirectivesSentinels.push_back(compilerDirectiveSentinel);
+  }
   parsing.Prescan(path, options);
   if (!parsing.messages().empty() && (parsing.messages().AnyFatalError())) {
     llvm::errs() << programPrefix << "could not scan " << path << '\n';



More information about the flang-commits mailing list