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

via flang-commits flang-commits at lists.llvm.org
Sat May 30 00:26:13 PDT 2026


Author: CHANDRA GHALE
Date: 2026-05-30T12:56:07+05:30
New Revision: 666a8cf6ebb7b1cf502b5963c81c0ea3e19e21b1

URL: https://github.com/llvm/llvm-project/commit/666a8cf6ebb7b1cf502b5963c81c0ea3e19e21b1
DIFF: https://github.com/llvm/llvm-project/commit/666a8cf6ebb7b1cf502b5963c81c0ea3e19e21b1.diff

LOG: [Flang][Parser] Handle compiler directives inside INTERFACE blocks (#198516)

Unrecognized !DIR$ directives between interface specifications currently
cause cascading parse errors because the grammar for
InterfaceSpecification has no path to consume them. This patch adds
CompilerDirective as a valid alternative — matching how
InternalSubprogram and ModuleSubprogram already handle this — so that
unrecognized directives produce the expected warning instead of a fatal
parse failure.

Fixes :
[https://github.com/llvm/llvm-project/issues/198289](https://github.com/llvm/llvm-project/issues/198289)

---------

Co-authored-by: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>

Added: 
    flang/test/Parser/compiler-directive-in-interface.f90

Modified: 
    flang/include/flang/Parser/parse-tree.h
    flang/lib/Parser/program-parsers.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h
index 71d471c3deb18..2ffd068ccb44e 100644
--- a/flang/include/flang/Parser/parse-tree.h
+++ b/flang/include/flang/Parser/parse-tree.h
@@ -3193,10 +3193,13 @@ 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>> 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 a8373390d93cb..591487083a483 100644
--- a/flang/lib/Parser/program-parsers.cpp
+++ b/flang/lib/Parser/program-parsers.cpp
@@ -371,9 +371,11 @@ 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>(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..4b62f46418c34
--- /dev/null
+++ b/flang/test/Parser/compiler-directive-in-interface.f90
@@ -0,0 +1,60 @@
+! 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
+
+! 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


        


More information about the flang-commits mailing list