[flang-commits] [flang] 69373a5 - [flang] Check for invalid BIND(C) names

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Feb 13 17:27:14 PST 2023


Author: Peter Klausler
Date: 2023-02-13T16:53:43-08:00
New Revision: 69373a5d3f45771cfb1896f9938e2444bedb7b33

URL: https://github.com/llvm/llvm-project/commit/69373a5d3f45771cfb1896f9938e2444bedb7b33
DIFF: https://github.com/llvm/llvm-project/commit/69373a5d3f45771cfb1896f9938e2444bedb7b33.diff

LOG: [flang] Check for invalid BIND(C) names

Require BIND(C) interoperable names to be valid C identifiers.

Differential Revision: https://reviews.llvm.org/D143833

Added: 
    flang/test/Semantics/bind-c10.f90

Modified: 
    flang/lib/Semantics/check-declarations.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index caaea48825d8..73477867b140 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -13,6 +13,7 @@
 #include "flang/Evaluate/check-expression.h"
 #include "flang/Evaluate/fold.h"
 #include "flang/Evaluate/tools.h"
+#include "flang/Parser/characters.h"
 #include "flang/Semantics/scope.h"
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/symbol.h"
@@ -2173,6 +2174,18 @@ void CheckHelper::CheckBindC(const Symbol &symbol) {
   }
   CheckConflicting(symbol, Attr::BIND_C, Attr::PARAMETER);
   CheckConflicting(symbol, Attr::BIND_C, Attr::ELEMENTAL);
+  if (const std::string * bindName{symbol.GetBindName()};
+      bindName && !bindName->empty()) {
+    bool ok{bindName->front() == '_' || parser::IsLetter(bindName->front())};
+    for (char ch : *bindName) {
+      ok &= ch == '_' || parser::IsLetter(ch) || parser::IsDecimalDigit(ch);
+    }
+    if (!ok) {
+      messages_.Say(symbol.name(),
+          "Symbol has a BIND(C) name that is not a valid C language identifier"_err_en_US);
+      context_.SetError(symbol);
+    }
+  }
   if (symbol.has<ObjectEntityDetails>() && !symbol.owner().IsModule()) {
     messages_.Say(symbol.name(),
         "A variable with BIND(C) attribute may only appear in the specification part of a module"_err_en_US);

diff  --git a/flang/test/Semantics/bind-c10.f90 b/flang/test/Semantics/bind-c10.f90
new file mode 100644
index 000000000000..c562e6aee31e
--- /dev/null
+++ b/flang/test/Semantics/bind-c10.f90
@@ -0,0 +1,10 @@
+! 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
+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


        


More information about the flang-commits mailing list