[flang-commits] [PATCH] D143833: [flang] Check for invalid BIND(C) names

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Sat Feb 11 17:08:42 PST 2023


klausler created this revision.
klausler added a reviewer: jeanPerier.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a reviewer: sscalpone.
Herald added a project: All.
klausler requested review of this revision.

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


https://reviews.llvm.org/D143833

Files:
  flang/lib/Semantics/check-declarations.cpp
  flang/test/Semantics/bind-c10.f90


Index: flang/test/Semantics/bind-c10.f90
===================================================================
--- /dev/null
+++ 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
Index: flang/lib/Semantics/check-declarations.cpp
===================================================================
--- flang/lib/Semantics/check-declarations.cpp
+++ 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 @@
   }
   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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143833.496725.patch
Type: text/x-patch
Size: 1938 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230212/b237bd75/attachment.bin>


More information about the flang-commits mailing list