[flang-commits] [flang] [flang] Allow extended char set in `BIND(C, name="...")` (PR #172457)
via flang-commits
flang-commits at lists.llvm.org
Sun Jan 25 06:13:19 PST 2026
https://github.com/foxtran updated https://github.com/llvm/llvm-project/pull/172457
>From da0cabc74a31adc93109f5b2a306217d322e8eb7 Mon Sep 17 00:00:00 2001
From: "Igor S. Gerasimov" <foxtranigor at gmail.com>
Date: Tue, 16 Dec 2025 12:02:12 +0100
Subject: [PATCH 1/9] Update flang/test/Semantics/bind-c10.f90 test
---
flang/test/Semantics/bind-c10.f90 | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/flang/test/Semantics/bind-c10.f90 b/flang/test/Semantics/bind-c10.f90
index c562e6aee31e8..b4ae18909cb86 100644
--- a/flang/test/Semantics/bind-c10.f90
+++ b/flang/test/Semantics/bind-c10.f90
@@ -1,10 +1,14 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
-!ERROR: Symbol has a BIND(C) name that is not a valid C language identifier
subroutine bang() bind(C,name='!')
end
-!ERROR: Symbol has a BIND(C) name that is not a valid C language identifier
+!ERROR: Symbol has a BIND(C) name containing non-visible ASCII character(s)
subroutine cr() bind(C,name=achar(13))
end
-!ERROR: Symbol has a BIND(C) name that is not a valid C language identifier
subroutine beast() bind(C,name="666")
end
+!ERROR: Symbol has a BIND(C) name with leading dot that may have special meaning to the toolchain
+subroutine llvm() bind(C,name=".L1")
+end
+!ERROR: Symbol has a BIND(C) name with leading dot that may have special meaning to the toolchain
+subroutine ld() bind(C,name=".text")
+end
>From 63f8cb382c93ced39e0e58d5231c6361f7532b23 Mon Sep 17 00:00:00 2001
From: "Igor S. Gerasimov" <foxtranigor at gmail.com>
Date: Tue, 16 Dec 2025 12:07:36 +0100
Subject: [PATCH 2/9] Detect non-visible characters
---
flang/lib/Semantics/check-declarations.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 9a6b3ff3cdc2c..29342439ee956 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -3476,13 +3476,13 @@ void CheckHelper::CheckBindC(const Symbol &symbol) {
if (const std::string * bindName{symbol.GetBindName()};
bindName) { // has a binding name
if (!bindName->empty()) {
- bool ok{bindName->front() == '_' || parser::IsLetter(bindName->front())};
+ bool visible_ascii{true};
for (char ch : *bindName) {
- ok &= ch == '_' || parser::IsLetter(ch) || parser::IsDecimalDigit(ch);
+ visible_ascii &= parser::IsPrintable(ch);
}
- if (!ok) {
+ if (!visible_ascii) {
messages_.Say(symbol.name(),
- "Symbol has a BIND(C) name that is not a valid C language identifier"_err_en_US);
+ "Symbol has a BIND(C) name containing non-visible ASCII character(s)"_err_en_US);
context_.SetError(symbol);
}
}
>From e4ad5e418dc01a01b98e5b2d549d677c84f4f819 Mon Sep 17 00:00:00 2001
From: "Igor S. Gerasimov" <foxtranigor at gmail.com>
Date: Tue, 16 Dec 2025 12:09:46 +0100
Subject: [PATCH 3/9] Catch leading dot to avoid misleading with toolchain
internals
---
flang/lib/Semantics/check-declarations.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 29342439ee956..2d3df87f6e240 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -3476,6 +3476,12 @@ void CheckHelper::CheckBindC(const Symbol &symbol) {
if (const std::string * bindName{symbol.GetBindName()};
bindName) { // has a binding name
if (!bindName->empty()) {
+ bool toolchain_error{bindName->front() == '.'};
+ if (toolchain_error) {
+ messages_.Say(symbol.name(),
+ "Symbol has a BIND(C) name with leading dot that may have special meaning to the toolchain"_err_en_US);
+ context_.SetError(symbol);
+ }
bool visible_ascii{true};
for (char ch : *bindName) {
visible_ascii &= parser::IsPrintable(ch);
>From a6a0f4c66780d12a697768a22aabba0f138df234 Mon Sep 17 00:00:00 2001
From: "Igor S. Gerasimov" <foxtranigor at gmail.com>
Date: Tue, 16 Dec 2025 17:05:08 +0100
Subject: [PATCH 4/9] Remove toolchain_error variable
---
flang/lib/Semantics/check-declarations.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 2d3df87f6e240..23f42684f6b48 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -3476,8 +3476,7 @@ void CheckHelper::CheckBindC(const Symbol &symbol) {
if (const std::string * bindName{symbol.GetBindName()};
bindName) { // has a binding name
if (!bindName->empty()) {
- bool toolchain_error{bindName->front() == '.'};
- if (toolchain_error) {
+ if (bindName->front() == '.') {
messages_.Say(symbol.name(),
"Symbol has a BIND(C) name with leading dot that may have special meaning to the toolchain"_err_en_US);
context_.SetError(symbol);
>From df631b5475ef88b1f1f25dbdf7bedc9d9bb58131 Mon Sep 17 00:00:00 2001
From: "Igor S. Gerasimov" <foxtranigor at gmail.com>
Date: Wed, 17 Dec 2025 06:49:25 +0100
Subject: [PATCH 5/9] Refactor visibleAscii variable name
---
flang/lib/Semantics/check-declarations.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 23f42684f6b48..eabc3286ea478 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -3481,11 +3481,11 @@ void CheckHelper::CheckBindC(const Symbol &symbol) {
"Symbol has a BIND(C) name with leading dot that may have special meaning to the toolchain"_err_en_US);
context_.SetError(symbol);
}
- bool visible_ascii{true};
+ bool visibleAscii{true};
for (char ch : *bindName) {
- visible_ascii &= parser::IsPrintable(ch);
+ visibleAscii &= parser::IsPrintable(ch);
}
- if (!visible_ascii) {
+ if (!visibleAscii) {
messages_.Say(symbol.name(),
"Symbol has a BIND(C) name containing non-visible ASCII character(s)"_err_en_US);
context_.SetError(symbol);
>From a608ca854830f5fdf55773361a35740760be0e15 Mon Sep 17 00:00:00 2001
From: "Igor S. Gerasimov" <foxtranigor at gmail.com>
Date: Fri, 9 Jan 2026 07:56:22 +0100
Subject: [PATCH 6/9] Do not check leading dot: llvm-mc does it (if used)
---
flang/lib/Semantics/check-declarations.cpp | 5 -----
flang/test/Semantics/bind-c10.f90 | 2 --
2 files changed, 7 deletions(-)
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index eabc3286ea478..4767e70947d88 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -3476,11 +3476,6 @@ void CheckHelper::CheckBindC(const Symbol &symbol) {
if (const std::string * bindName{symbol.GetBindName()};
bindName) { // has a binding name
if (!bindName->empty()) {
- if (bindName->front() == '.') {
- messages_.Say(symbol.name(),
- "Symbol has a BIND(C) name with leading dot that may have special meaning to the toolchain"_err_en_US);
- context_.SetError(symbol);
- }
bool visibleAscii{true};
for (char ch : *bindName) {
visibleAscii &= parser::IsPrintable(ch);
diff --git a/flang/test/Semantics/bind-c10.f90 b/flang/test/Semantics/bind-c10.f90
index b4ae18909cb86..4dfbf43df89f5 100644
--- a/flang/test/Semantics/bind-c10.f90
+++ b/flang/test/Semantics/bind-c10.f90
@@ -6,9 +6,7 @@ subroutine cr() bind(C,name=achar(13))
end
subroutine beast() bind(C,name="666")
end
-!ERROR: Symbol has a BIND(C) name with leading dot that may have special meaning to the toolchain
subroutine llvm() bind(C,name=".L1")
end
-!ERROR: Symbol has a BIND(C) name with leading dot that may have special meaning to the toolchain
subroutine ld() bind(C,name=".text")
end
>From 8dbf7ed12c2a0f74e4d761708454f23dc3459b69 Mon Sep 17 00:00:00 2001
From: "Igor S. Gerasimov" <foxtranigor at gmail.com>
Date: Fri, 9 Jan 2026 08:21:44 +0100
Subject: [PATCH 7/9] Add note about examples (and one more example)
---
flang/test/Semantics/bind-c10.f90 | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/flang/test/Semantics/bind-c10.f90 b/flang/test/Semantics/bind-c10.f90
index 4dfbf43df89f5..8ce82e95bd749 100644
--- a/flang/test/Semantics/bind-c10.f90
+++ b/flang/test/Semantics/bind-c10.f90
@@ -4,9 +4,15 @@ subroutine bang() bind(C,name='!')
!ERROR: Symbol has a BIND(C) name containing non-visible ASCII character(s)
subroutine cr() bind(C,name=achar(13))
end
+!! depending on used assembler it can be threated as error or not
subroutine beast() bind(C,name="666")
end
+!! depending on used assembler it can be threated as error or not
subroutine llvm() bind(C,name=".L1")
end
-subroutine ld() bind(C,name=".text")
+!! depending on used assembler it can be threated as error or not
+subroutine as() bind(C,name=".text")
+end
+!! depending on used assembler it can be threated as error or not
+subroutine printable_ascii() bind(C,name="! ""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'")
end
>From c5bab5857423b91ebaa1aa130038101cc609f9d6 Mon Sep 17 00:00:00 2001
From: "Igor S. Gerasimov" <foxtranigor at gmail.com>
Date: Sun, 25 Jan 2026 15:01:38 +0100
Subject: [PATCH 8/9] Update tests for being valid on many platforms
---
flang/test/Semantics/bind-c10.f90 | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)
diff --git a/flang/test/Semantics/bind-c10.f90 b/flang/test/Semantics/bind-c10.f90
index 8ce82e95bd749..f45d2cfa39ec6 100644
--- a/flang/test/Semantics/bind-c10.f90
+++ b/flang/test/Semantics/bind-c10.f90
@@ -1,18 +1,9 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
-subroutine bang() bind(C,name='!')
+subroutine currency() bind(C,name='$')
end
!ERROR: Symbol has a BIND(C) name containing non-visible ASCII character(s)
subroutine cr() bind(C,name=achar(13))
end
-!! depending on used assembler it can be threated as error or not
-subroutine beast() bind(C,name="666")
-end
-!! depending on used assembler it can be threated as error or not
-subroutine llvm() bind(C,name=".L1")
-end
-!! depending on used assembler it can be threated as error or not
-subroutine as() bind(C,name=".text")
-end
-!! depending on used assembler it can be threated as error or not
-subroutine printable_ascii() bind(C,name="! ""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'")
+!ERROR: Symbol has a BIND(C) name containing non-visible ASCII character(s)
+subroutine null_terminator() bind(C,name="null_terminator" // achar(0))
end
>From 1a41dc17e03b686d03e37427c32143ee06af57eb Mon Sep 17 00:00:00 2001
From: "Igor S. Gerasimov" <foxtranigor at gmail.com>
Date: Sun, 25 Jan 2026 15:13:00 +0100
Subject: [PATCH 9/9] Add positive test
---
flang/test/Semantics/bind-c10.f90 | 2 ++
1 file changed, 2 insertions(+)
diff --git a/flang/test/Semantics/bind-c10.f90 b/flang/test/Semantics/bind-c10.f90
index f45d2cfa39ec6..ed82cbb431dae 100644
--- a/flang/test/Semantics/bind-c10.f90
+++ b/flang/test/Semantics/bind-c10.f90
@@ -1,4 +1,6 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
+subroutine foo() bind(C,name='bar')
+end
subroutine currency() bind(C,name='$')
end
!ERROR: Symbol has a BIND(C) name containing non-visible ASCII character(s)
More information about the flang-commits
mailing list