[flang-commits] [flang] [flang] Downgrade error to warning for IGNORE_TKR case (PR #180994)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Wed Feb 11 10:55:01 PST 2026
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/180994
The IGNORE_TKR directive has meaning only in the specification part of a subroutine or function subprogram or interface. Presently, it is an error when the directive appears elsewhere.
At user request, this patch softens the error to a warning for when this directive appears in a program unit other than a subroutine or function, and when it appears in a subroutine or function subprogram outside the specification part of its top scope.
>From 8c0ef266561cccf1b90fdc91ebcc5065d79114ee Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 11 Feb 2026 10:49:14 -0800
Subject: [PATCH] [flang] Downgrade error to warning for IGNORE_TKR case
The IGNORE_TKR directive has meaning only in the specification
part of a subroutine or function subprogram or interface.
Presently, it is an error when the directive appears elsewhere.
At user request, this patch softens the error to a warning for when
this directive appears in a program unit other than a subroutine
or function, and when it appears in a subroutine or function
subprogram outside the specification part of its top scope.
---
flang/include/flang/Support/Fortran-features.h | 3 ++-
flang/lib/Semantics/resolve-names.cpp | 18 ++++++++++--------
flang/lib/Support/Fortran-features.cpp | 1 +
.../Semantics/OpenMP/compiler-directive.f90 | 2 +-
flang/test/Semantics/ignore_tkr01.f90 | 8 ++++----
5 files changed, 18 insertions(+), 14 deletions(-)
diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index fa300da48ffe1..f6c963d05fd54 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -81,7 +81,8 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram,
HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile,
RealConstantWidening, VolatileOrAsynchronousTemporary, UnusedVariable,
- UsedUndefinedVariable, BadValueInDeadCode, AssumedTypeSizeDummy)
+ UsedUndefinedVariable, BadValueInDeadCode, AssumedTypeSizeDummy,
+ MisplacedIgnoreTKR)
using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 164a9bedcc393..9036defa0c711 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -10239,16 +10239,18 @@ void ResolveNamesVisitor::Post(const parser::CompilerDirective &x) {
}
if (const auto *tkr{
std::get_if<std::list<parser::CompilerDirective::IgnoreTKR>>(&x.u)}) {
- if (currScope().IsTopLevel() ||
- GetProgramUnitContaining(currScope()).kind() !=
- Scope::Kind::Subprogram) {
+ if (currScope().IsTopLevel()) {
Say(x.source,
- "!DIR$ IGNORE_TKR directive must appear in a subroutine or function"_err_en_US);
+ "!DIR$ IGNORE_TKR directive must appear in a program unit"_err_en_US);
return;
- }
- if (!inSpecificationPart_) {
- Say(x.source,
- "!DIR$ IGNORE_TKR directive must appear in the specification part"_err_en_US);
+ } else if (GetProgramUnitContaining(currScope()).kind() !=
+ Scope::Kind::Subprogram) {
+ context().Warn(common::UsageWarning::MisplacedIgnoreTKR, x.source,
+ "!DIR$ IGNORE_TKR directive should appear in a subroutine or function"_warn_en_US);
+ return;
+ } else if (!inSpecificationPart_) {
+ context().Warn(common::UsageWarning::MisplacedIgnoreTKR, x.source,
+ "!DIR$ IGNORE_TKR directive should appear in the specification part"_warn_en_US);
return;
}
if (tkr->empty()) {
diff --git a/flang/lib/Support/Fortran-features.cpp b/flang/lib/Support/Fortran-features.cpp
index 521bf35294154..83d1affba5ed2 100644
--- a/flang/lib/Support/Fortran-features.cpp
+++ b/flang/lib/Support/Fortran-features.cpp
@@ -152,6 +152,7 @@ LanguageFeatureControl::LanguageFeatureControl() {
warnLanguage_.set(LanguageFeature::SavedLocalInSpecExpr);
warnLanguage_.set(LanguageFeature::NullActualForAllocatable);
warnUsage_.set(UsageWarning::BadValueInDeadCode);
+ warnUsage_.set(UsageWarning::MisplacedIgnoreTKR);
}
std::optional<LanguageControlFlag> LanguageFeatureControl::FindWarning(
diff --git a/flang/test/Semantics/OpenMP/compiler-directive.f90 b/flang/test/Semantics/OpenMP/compiler-directive.f90
index 5d3e9bae27fd8..e882f26c85b53 100644
--- a/flang/test/Semantics/OpenMP/compiler-directive.f90
+++ b/flang/test/Semantics/OpenMP/compiler-directive.f90
@@ -1,7 +1,7 @@
! RUN: %python %S/../test_errors.py %s %flang -fopenmp
! CompilerDirective with openmp tests
-!ERROR: !DIR$ IGNORE_TKR directive must appear in a subroutine or function
+!ERROR: !DIR$ IGNORE_TKR directive must appear in a program unit
!dir$ ignore_tkr
program main
diff --git a/flang/test/Semantics/ignore_tkr01.f90 b/flang/test/Semantics/ignore_tkr01.f90
index d069c97331bf6..c1e1e5d25cd56 100644
--- a/flang/test/Semantics/ignore_tkr01.f90
+++ b/flang/test/Semantics/ignore_tkr01.f90
@@ -1,12 +1,12 @@
! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic
! !DIR$ IGNORE_TKR tests
-!ERROR: !DIR$ IGNORE_TKR directive must appear in a subroutine or function
+!ERROR: !DIR$ IGNORE_TKR directive must appear in a program unit
!dir$ ignore_tkr
module m
-!ERROR: !DIR$ IGNORE_TKR directive must appear in a subroutine or function
+!WARNING: !DIR$ IGNORE_TKR directive should appear in a subroutine or function [-Wmisplaced-ignore-tkr]
!dir$ ignore_tkr
interface
@@ -115,7 +115,7 @@ subroutine t16(x)
subroutine t17(x)
real x
x = x + 1.
-!ERROR: !DIR$ IGNORE_TKR directive must appear in the specification part
+!WARNING: !DIR$ IGNORE_TKR directive should appear in the specification part [-Wmisplaced-ignore-tkr]
!dir$ ignore_tkr x
end
@@ -173,7 +173,7 @@ module subroutine t24(x)
program test
-!ERROR: !DIR$ IGNORE_TKR directive must appear in a subroutine or function
+!WARNING: !DIR$ IGNORE_TKR directive should appear in a subroutine or function [-Wmisplaced-ignore-tkr]
!dir$ ignore_tkr
use m
More information about the flang-commits
mailing list