[flang-commits] [flang] [flang] Fix ISO_Fortran_binding.h to work better with C++ code (PR #82556)

Pete Steinfeld via flang-commits flang-commits at lists.llvm.org
Thu Feb 22 11:40:36 PST 2024


https://github.com/psteinfeld updated https://github.com/llvm/llvm-project/pull/82556

>From dbfa4c1664bcb8632b4499c37db56ff2289434c1 Mon Sep 17 00:00:00 2001
From: Peter Steinfeld <psteinfeld at nvidia.com>
Date: Wed, 21 Feb 2024 16:15:25 -0800
Subject: [PATCH 1/4] [flang] Fix ISO_Fortran_binding.h to work better with C++
 code

[flang] Fix ISO_Fortran_binding.h to work better with C++ code

This stems from working on LANL's "dopey" project --
https://github.com/lanl/dopey.

That project contains C++ code which includes the header file
"ISO_Fortran_binding.h".  The dopey code  wraps that include with an
'extern "C"' clause since the standard (18.5.1, paragraph 1) says that
the file" shall contain C structure definitions, typedef declarations,
...".  But the clang++ compiler emits error messages objecting to the
fact that ISO_Fortran_binding.h contains templates.

This change fixes that by preceding the problematic code in
ISO_Fortran_binding.h with language linkage clauses that specify that
they contain C++ code rather than C code.

Here's an example of a C++ program that shows the problem:
```
  extern "C" {
  #include "ISO_Fortran_binding.h"
  }
  int main() {
    return 0;
  }
```
---
 flang/include/flang/ISO_Fortran_binding.h |  8 +++---
 flang/test/Driver/iso-fortran-binding.cpp | 32 +++++++++++++++++++++++
 2 files changed, 36 insertions(+), 4 deletions(-)
 create mode 100644 flang/test/Driver/iso-fortran-binding.cpp

diff --git a/flang/include/flang/ISO_Fortran_binding.h b/flang/include/flang/ISO_Fortran_binding.h
index 4a28d3322a38f0..3f74a7e56f1755 100644
--- a/flang/include/flang/ISO_Fortran_binding.h
+++ b/flang/include/flang/ISO_Fortran_binding.h
@@ -125,7 +125,7 @@ namespace cfi_internal {
 // The below structure emulates a flexible array. This structure does not take
 // care of getting the memory storage. Note that it already contains one element
 // because a struct cannot be empty.
-template <typename T> struct FlexibleArray : T {
+extern "C++" template <typename T> struct FlexibleArray : T {
   RT_API_ATTRS T &operator[](int index) { return *(this + index); }
   const RT_API_ATTRS T &operator[](int index) const { return *(this + index); }
   RT_API_ATTRS operator T *() { return this; }
@@ -163,12 +163,12 @@ typedef struct CFI_cdesc_t {
 // needed, for C++'s CFI_cdesc_t's emulated flexible
 // dim[] array.
 namespace cfi_internal {
-template <int r> struct CdescStorage : public CFI_cdesc_t {
+extern "C++" template <int r> struct CdescStorage : public CFI_cdesc_t {
   static_assert((r > 1 && r <= CFI_MAX_RANK), "CFI_INVALID_RANK");
   CFI_dim_t dim[r - 1];
 };
-template <> struct CdescStorage<1> : public CFI_cdesc_t {};
-template <> struct CdescStorage<0> : public CFI_cdesc_t {};
+extern "C++" template <> struct CdescStorage<1> : public CFI_cdesc_t {};
+extern "C++" template <> struct CdescStorage<0> : public CFI_cdesc_t {};
 } // namespace cfi_internal
 #define CFI_CDESC_T(rank) \
   FORTRAN_ISO_NAMESPACE_::cfi_internal::CdescStorage<rank>
diff --git a/flang/test/Driver/iso-fortran-binding.cpp b/flang/test/Driver/iso-fortran-binding.cpp
new file mode 100644
index 00000000000000..f977451c729f8a
--- /dev/null
+++ b/flang/test/Driver/iso-fortran-binding.cpp
@@ -0,0 +1,32 @@
+! UNSUPPORTED: system-windows
+! RUN: split-file %s %t
+! RUN: chmod +x %t/runtest.sh
+! RUN: %t/runtest.sh %t %t/cppfile.cpp %flang | FileCheck %s
+
+!--- cppfile.cpp
+extern "C" {
+#include "ISO_Fortran_binding.h"
+}
+#include<iostream>
+
+int main() {
+  std::cout << "PASS\n";
+  return 0;
+}
+
+// CHECK: PASS
+!--- runtest.sh
+#!/bin/bash
+TMPDIR=$1
+CPPFILE=$2
+FLANG=$3
+BINDIR=`dirname $FLANG`
+CPPCOMP=$BINDIR/clang++
+if [ -x $CCOMP ]
+then
+  $CPPCOMP $CPPFILE -o $TMPDIR/a.out
+  $TMPDIR/a.out # should print "PASS"
+else
+  # No clang compiler, just pass by default
+  echo "PASS"
+fi

>From fae9bb462bb1bd016dc8bfafaabb136d480d22ff Mon Sep 17 00:00:00 2001
From: Peter Steinfeld <psteinfeld at nvidia.com>
Date: Wed, 21 Feb 2024 16:36:35 -0800
Subject: [PATCH 2/4] [flang] Fix lit test for changes to ISO_Fortran_binding.h

I mistakely used Fortran comments for the lit metacommands.
---
 flang/test/Driver/iso-fortran-binding.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/flang/test/Driver/iso-fortran-binding.cpp b/flang/test/Driver/iso-fortran-binding.cpp
index f977451c729f8a..b62389a17cc70a 100644
--- a/flang/test/Driver/iso-fortran-binding.cpp
+++ b/flang/test/Driver/iso-fortran-binding.cpp
@@ -1,9 +1,9 @@
-! UNSUPPORTED: system-windows
-! RUN: split-file %s %t
-! RUN: chmod +x %t/runtest.sh
-! RUN: %t/runtest.sh %t %t/cppfile.cpp %flang | FileCheck %s
+// UNSUPPORTED: system-windows
+// RUN: split-file %s %t
+// RUN: chmod +x %t/runtest.sh
+// RUN: %t/runtest.sh %t %t/cppfile.cpp %flang | FileCheck %s
 
-!--- cppfile.cpp
+//--- cppfile.cpp
 extern "C" {
 #include "ISO_Fortran_binding.h"
 }
@@ -15,7 +15,7 @@ int main() {
 }
 
 // CHECK: PASS
-!--- runtest.sh
+//--- runtest.sh
 #!/bin/bash
 TMPDIR=$1
 CPPFILE=$2

>From b554747b02f612dfe6855b59f8ed8e2c1ee1d6da Mon Sep 17 00:00:00 2001
From: Peter Steinfeld <psteinfeld at nvidia.com>
Date: Wed, 21 Feb 2024 18:57:16 -0800
Subject: [PATCH 3/4] [flang] Another attempt to format the test for
 ISO_Fortran_binding.h

---
 flang/test/Driver/iso-fortran-binding.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/flang/test/Driver/iso-fortran-binding.cpp b/flang/test/Driver/iso-fortran-binding.cpp
index b62389a17cc70a..403b7f638d7633 100644
--- a/flang/test/Driver/iso-fortran-binding.cpp
+++ b/flang/test/Driver/iso-fortran-binding.cpp
@@ -7,7 +7,7 @@
 extern "C" {
 #include "ISO_Fortran_binding.h"
 }
-#include<iostream>
+#include <iostream>
 
 int main() {
   std::cout << "PASS\n";
@@ -15,6 +15,7 @@ int main() {
 }
 
 // CHECK: PASS
+// clang-format off
 //--- runtest.sh
 #!/bin/bash
 TMPDIR=$1

>From 12eb5ea2f82adfa68140f34159b39be1ce2c39ac Mon Sep 17 00:00:00 2001
From: Peter Steinfeld <psteinfeld at nvidia.com>
Date: Thu, 22 Feb 2024 11:38:02 -0800
Subject: [PATCH 4/4] [flang] Fixing the script for ISO_Fortran_binding.h test

Fixing a typo.
---
 flang/test/Driver/iso-fortran-binding.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/test/Driver/iso-fortran-binding.cpp b/flang/test/Driver/iso-fortran-binding.cpp
index 403b7f638d7633..aaafd7cccd07d0 100644
--- a/flang/test/Driver/iso-fortran-binding.cpp
+++ b/flang/test/Driver/iso-fortran-binding.cpp
@@ -23,7 +23,7 @@ CPPFILE=$2
 FLANG=$3
 BINDIR=`dirname $FLANG`
 CPPCOMP=$BINDIR/clang++
-if [ -x $CCOMP ]
+if [ -x $CPPCOMP ]
 then
   $CPPCOMP $CPPFILE -o $TMPDIR/a.out
   $TMPDIR/a.out # should print "PASS"



More information about the flang-commits mailing list