[flang-commits] [flang] [flang] Avoid crash on conflicting BIND(C) declaration (PR #231816)
via flang-commits
flang-commits at lists.llvm.org
Sat Oct 17 13:58:28 PDT 2026
https://github.com/Anastasia-Artemova updated https://github.com/llvm/llvm-project/pull/231816
>From 87899de447e0df2a0c14efad9a0b044a07899432 Mon Sep 17 00:00:00 2001
From: Anastasia-Artemova <anastasia.r.artemova at gmail.com>
Date: Wed, 14 Oct 2026 10:41:21 +0200
Subject: [PATCH 1/2] [flang] Avoid crash on conflicting BIND(C) declaration
---
flang/lib/Semantics/resolve-names.cpp | 2 +-
flang/test/Semantics/bind-c22.f90 | 8 ++++++++
2 files changed, 9 insertions(+), 1 deletion(-)
create mode 100644 flang/test/Semantics/bind-c22.f90
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 67690d6f47d067..903cb14b6d9021 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -6831,7 +6831,7 @@ Symbol &DeclarationVisitor::DeclareUnknownEntity(
Attr::SAVE, Attr::TARGET, Attr::VALUE, Attr::VOLATILE})) {
ConvertToObjectEntity(symbol);
}
- if (attrs.test(Attr::BIND_C)) {
+ if (attrs.test(Attr::BIND_C) && !context().HasError(symbol)) {
SetBindNameOn(symbol);
}
return symbol;
diff --git a/flang/test/Semantics/bind-c22.f90 b/flang/test/Semantics/bind-c22.f90
new file mode 100644
index 00000000000000..2a8530b8be2e7a
--- /dev/null
+++ b/flang/test/Semantics/bind-c22.f90
@@ -0,0 +1,8 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+
+module bind_c_conflicting_type_name
+ type :: x
+ end type
+ !ERROR: 'x' is already declared in this scoping unit
+ integer, bind(c) :: x
+end module
>From ddfe15c58d3a5a9dad0a5af82184b6578e87acd3 Mon Sep 17 00:00:00 2001
From: Anastasia-Artemova <anastasia.r.artemova at gmail.com>
Date: Sat, 17 Oct 2026 22:58:14 +0200
Subject: [PATCH 2/2] Refined BIND(C) declaration conflict handling
---
flang/lib/Semantics/resolve-names.cpp | 6 ++--
flang/test/Semantics/bind-c22.f90 | 45 ++++++++++++++++++++++++++-
2 files changed, 48 insertions(+), 3 deletions(-)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 903cb14b6d9021..ed45674dc0eb01 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -6831,8 +6831,10 @@ Symbol &DeclarationVisitor::DeclareUnknownEntity(
Attr::SAVE, Attr::TARGET, Attr::VALUE, Attr::VOLATILE})) {
ConvertToObjectEntity(symbol);
}
- if (attrs.test(Attr::BIND_C) && !context().HasError(symbol)) {
- SetBindNameOn(symbol);
+ if (attrs.test(Attr::BIND_C)) {
+ if (symbol.detailsIf<ProcEntityDetails>() || symbol.detailsIf<ObjectEntityDetails>() || symbol.detailsIf<EntityDetails>()) {
+ SetBindNameOn(symbol);
+ }
}
return symbol;
}
diff --git a/flang/test/Semantics/bind-c22.f90 b/flang/test/Semantics/bind-c22.f90
index 2a8530b8be2e7a..045da1ef09aa99 100644
--- a/flang/test/Semantics/bind-c22.f90
+++ b/flang/test/Semantics/bind-c22.f90
@@ -1,8 +1,51 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
-module bind_c_conflicting_type_name
+module bind_c_derived_type_conflict
type :: x
end type
!ERROR: 'x' is already declared in this scoping unit
integer, bind(c) :: x
end module
+
+module bind_c_multiple_binding_names
+ !ERROR: The entity 'x' has multiple BIND names ('a' and 'b')
+ integer, bind(c, name="a") :: x
+ !ERROR: BIND_C attribute was already specified on 'x'
+ !ERROR: The type of 'x' has already been declared as INTEGER(4)
+ real, bind(c, name="b") :: x
+end module
+
+module bind_c_parameter_conflict
+ integer, parameter :: n = 10
+ !ERROR: The type of 'n' has already been declared as INTEGER(4)
+ integer, bind(c) :: n
+end module
+
+module bind_c_generic_interface_conflict
+ interface g
+ end interface
+ !ERROR: 'g' is already declared in this scoping unit
+ integer, bind(c) :: g
+end module
+
+module bind_c_namelist_conflict
+ integer :: a
+ namelist /nl/ a
+ !ERROR: 'nl' is already declared in this scoping unit
+ integer, bind(c) :: nl
+end module
+
+module bind_c_subsequent_module_procedure_conflict
+ !ERROR: Declaration of 's' conflicts with its use as module procedure
+ integer, bind(c) :: s
+contains
+ subroutine s()
+ end subroutine
+end module
+
+module bind_c_derived_type_array_conflict
+ type :: t
+ end type
+ !ERROR: 't' is already declared in this scoping unit
+ integer, bind(c) :: t(2)
+end module
More information about the flang-commits
mailing list