llvm-project/flang/test/Integration/iso-fortran-binding.cpp
Pete Steinfeld 796d26a37d
[flang] Fix ISO_Fortran_binding.h to work better with C++ code (#82556)
[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.

In the accompanying test, I needed to account for the fact that some
people build the compiler by doing a `make check-flang`. In this case,
the clang compiler which is required by the test will not be built.

Here's an example of a C++ program that shows the problem:
```
  extern "C" {
  #include "ISO_Fortran_binding.h"
  }
  int main() {
    return 0;
  }
```
2024-02-26 14:29:49 -08:00

34 lines
597 B
C++

// 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