[flang-commits] [flang] [Flang][Parser] Handle compiler directives inside INTERFACE blocks (PR #198516)

CHANDRA GHALE via flang-commits flang-commits at lists.llvm.org
Wed May 27 09:58:03 PDT 2026


https://github.com/chandraghale updated https://github.com/llvm/llvm-project/pull/198516

>From c97d7064bea5cd9d0be35aac83e7ccf82b2099c8 Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Date: Tue, 19 May 2026 07:51:49 -0500
Subject: [PATCH 1/3] Handle compiler directives inside INTERFACE blocks

---
 flang/include/flang/Parser/parse-tree.h       |  4 ++-
 flang/lib/Parser/program-parsers.cpp          |  3 ++-
 .../compiler-directive-in-interface.f90       | 25 +++++++++++++++++++
 3 files changed, 30 insertions(+), 2 deletions(-)
 create mode 100644 flang/test/Parser/compiler-directive-in-interface.f90

diff --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h
index 28a565d7e27f8..8505ad2c50227 100644
--- a/flang/include/flang/Parser/parse-tree.h
+++ b/flang/include/flang/Parser/parse-tree.h
@@ -3195,7 +3195,9 @@ struct ProcedureStmt {
 // R1502 interface-specification -> interface-body | procedure-stmt
 struct InterfaceSpecification {
   UNION_CLASS_BOILERPLATE(InterfaceSpecification);
-  std::variant<InterfaceBody, Statement<ProcedureStmt>> u;
+  std::variant<InterfaceBody, Statement<ProcedureStmt>,
+      common::Indirection<CompilerDirective>>
+      u;
 };
 
 // R1504 end-interface-stmt -> END INTERFACE [generic-spec]
diff --git a/flang/lib/Parser/program-parsers.cpp b/flang/lib/Parser/program-parsers.cpp
index b4e56213d9f02..048255898022c 100644
--- a/flang/lib/Parser/program-parsers.cpp
+++ b/flang/lib/Parser/program-parsers.cpp
@@ -373,7 +373,8 @@ TYPE_PARSER(construct<InterfaceBlock>(statement(Parser<InterfaceStmt>{}),
 
 // R1502 interface-specification -> interface-body | procedure-stmt
 TYPE_PARSER(construct<InterfaceSpecification>(Parser<InterfaceBody>{}) ||
-    construct<InterfaceSpecification>(statement(Parser<ProcedureStmt>{})))
+    construct<InterfaceSpecification>(statement(Parser<ProcedureStmt>{})) ||
+    construct<InterfaceSpecification>(indirect(compilerDirective)))
 
 // R1503 interface-stmt -> INTERFACE [generic-spec] | ABSTRACT INTERFACE
 TYPE_PARSER(construct<InterfaceStmt>("INTERFACE" >> maybe(genericSpec)) ||
diff --git a/flang/test/Parser/compiler-directive-in-interface.f90 b/flang/test/Parser/compiler-directive-in-interface.f90
new file mode 100644
index 0000000000000..77c3053b757b9
--- /dev/null
+++ b/flang/test/Parser/compiler-directive-in-interface.f90
@@ -0,0 +1,25 @@
+! RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s
+
+! Test that unrecognized compiler directives inside INTERFACE blocks
+! produce a warning rather than a parse error.
+
+module m
+  interface
+    subroutine ccff
+    end subroutine ccff
+!CHECK: warning: Unrecognized compiler directive was ignored
+    !dir$ id "test"
+  end interface
+end module
+
+module m2
+  interface
+!CHECK: warning: Unrecognized compiler directive was ignored
+    !dir$ id "test"
+    subroutine foo(a)
+      integer, intent(in) :: a
+    end subroutine foo
+  end interface
+end module
+
+

>From 9f81a36549cab7a85da4d887725ff96c2381b049 Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Date: Wed, 27 May 2026 07:13:19 -0500
Subject: [PATCH 2/3] update R1502 grammar comments, added updated tests

---
 flang/include/flang/Parser/parse-tree.h       |  3 +-
 flang/lib/Parser/program-parsers.cpp          |  3 +-
 .../compiler-directive-in-interface.f90       | 35 ++++++++++++++++
 flang/test/Semantics/modfile84.f90            | 42 +++++++++++++++++++
 4 files changed, 81 insertions(+), 2 deletions(-)
 create mode 100644 flang/test/Semantics/modfile84.f90

diff --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h
index 8505ad2c50227..d88d41a609152 100644
--- a/flang/include/flang/Parser/parse-tree.h
+++ b/flang/include/flang/Parser/parse-tree.h
@@ -3192,7 +3192,8 @@ struct ProcedureStmt {
   std::tuple<Kind, std::list<Name>> t;
 };
 
-// R1502 interface-specification -> interface-body | procedure-stmt
+// R1502 interface-specification ->
+//         interface-body | procedure-stmt | compiler-directive
 struct InterfaceSpecification {
   UNION_CLASS_BOILERPLATE(InterfaceSpecification);
   std::variant<InterfaceBody, Statement<ProcedureStmt>,
diff --git a/flang/lib/Parser/program-parsers.cpp b/flang/lib/Parser/program-parsers.cpp
index 048255898022c..42801b900fb69 100644
--- a/flang/lib/Parser/program-parsers.cpp
+++ b/flang/lib/Parser/program-parsers.cpp
@@ -371,7 +371,8 @@ TYPE_PARSER(construct<InterfaceBlock>(statement(Parser<InterfaceStmt>{}),
     many(Parser<InterfaceSpecification>{}),
     statement(Parser<EndInterfaceStmt>{})))
 
-// R1502 interface-specification -> interface-body | procedure-stmt
+// R1502 interface-specification ->
+//         interface-body | procedure-stmt | compiler-directive
 TYPE_PARSER(construct<InterfaceSpecification>(Parser<InterfaceBody>{}) ||
     construct<InterfaceSpecification>(statement(Parser<ProcedureStmt>{})) ||
     construct<InterfaceSpecification>(indirect(compilerDirective)))
diff --git a/flang/test/Parser/compiler-directive-in-interface.f90 b/flang/test/Parser/compiler-directive-in-interface.f90
index 77c3053b757b9..4b62f46418c34 100644
--- a/flang/test/Parser/compiler-directive-in-interface.f90
+++ b/flang/test/Parser/compiler-directive-in-interface.f90
@@ -22,4 +22,39 @@ end subroutine foo
   end interface
 end module
 
+! Directive between two procedures in a plain interface
+module m3
+  interface
+    subroutine s1()
+    end subroutine
+!CHECK: warning: Unrecognized compiler directive was ignored
+    !dir$ unknown_directive
+    subroutine s2()
+    end subroutine
+  end interface
+end module
+
+! Generic (named) interface
+module m4
+  interface iface
+    subroutine sub1(x)
+      real, intent(in) :: x
+    end subroutine sub1
+!CHECK: warning: Unrecognized compiler directive was ignored
+    !dir$ unknown_directive
+    subroutine sub2(x)
+      integer, intent(in) :: x
+    end subroutine sub2
+  end interface iface
+end module
 
+! Abstract interface
+module m5
+  abstract interface
+!CHECK: warning: Unrecognized compiler directive was ignored
+    !dir$ unknown_directive
+    subroutine abs_sub(x)
+      real, intent(in) :: x
+    end subroutine abs_sub
+  end interface
+end module
diff --git a/flang/test/Semantics/modfile84.f90 b/flang/test/Semantics/modfile84.f90
new file mode 100644
index 0000000000000..22b12dbcc9391
--- /dev/null
+++ b/flang/test/Semantics/modfile84.f90
@@ -0,0 +1,42 @@
+! RUN: %python %S/test_modfile.py %s %flang_fc1
+! Check that recognized compiler directives in interface blocks
+! survive module file serialization and parsing.
+
+module m
+  interface
+    subroutine sub(x)
+!dir$ ignore_tkr(t) x
+      real, intent(in) :: x
+    end subroutine
+  end interface
+end module
+
+!Expect: m.mod
+!module m
+! interface
+!  subroutine sub(x)
+!   real(4),intent(in)::x
+!   !dir$ ignore_tkr(t) x
+!  end
+! end interface
+!end
+
+module m2
+  interface
+    subroutine sub2(a)
+!dir$ ignore_tkr(kr) a
+      integer, intent(in) :: a
+    end subroutine
+  end interface
+end module
+
+!Expect: m2.mod
+!module m2
+! interface
+!  subroutine sub2(a)
+!   integer(4),intent(in)::a
+!   !dir$ ignore_tkr(kr) a
+!  end
+! end interface
+!end
+

>From 02a50100ceb724cf8d579cae643cf58b64da4878 Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Date: Wed, 27 May 2026 11:57:26 -0500
Subject: [PATCH 3/3] remove redundant test

---
 flang/test/Semantics/modfile84.f90 | 42 ------------------------------
 1 file changed, 42 deletions(-)
 delete mode 100644 flang/test/Semantics/modfile84.f90

diff --git a/flang/test/Semantics/modfile84.f90 b/flang/test/Semantics/modfile84.f90
deleted file mode 100644
index 22b12dbcc9391..0000000000000
--- a/flang/test/Semantics/modfile84.f90
+++ /dev/null
@@ -1,42 +0,0 @@
-! RUN: %python %S/test_modfile.py %s %flang_fc1
-! Check that recognized compiler directives in interface blocks
-! survive module file serialization and parsing.
-
-module m
-  interface
-    subroutine sub(x)
-!dir$ ignore_tkr(t) x
-      real, intent(in) :: x
-    end subroutine
-  end interface
-end module
-
-!Expect: m.mod
-!module m
-! interface
-!  subroutine sub(x)
-!   real(4),intent(in)::x
-!   !dir$ ignore_tkr(t) x
-!  end
-! end interface
-!end
-
-module m2
-  interface
-    subroutine sub2(a)
-!dir$ ignore_tkr(kr) a
-      integer, intent(in) :: a
-    end subroutine
-  end interface
-end module
-
-!Expect: m2.mod
-!module m2
-! interface
-!  subroutine sub2(a)
-!   integer(4),intent(in)::a
-!   !dir$ ignore_tkr(kr) a
-!  end
-! end interface
-!end
-



More information about the flang-commits mailing list