Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flang/include/flang/ISO_Fortran_binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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>
Expand Down
33 changes: 33 additions & 0 deletions flang/test/Integration/iso-fortran-binding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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
// clang-format off
//--- runtest.sh
#!/bin/bash
TMPDIR=$1
CPPFILE=$2
FLANG=$3
BINDIR=`dirname $FLANG`
CPPCOMP=$BINDIR/clang++
if [ -x $CPPCOMP ]
then
$CPPCOMP $CPPFILE -o $TMPDIR/a.out
$TMPDIR/a.out # should print "PASS"
else
# No clang compiler, just pass by default
echo "PASS"
fi